Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(348)

Unified Diff: net/android/java/src/org/chromium/net/NetworkChangeNotifier.java

Issue 1358163004: [Android] Introduce RegistrationPolicy for NetworkChangeNotifier. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: clean-up and rebase Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/android/java/src/org/chromium/net/NetworkChangeNotifier.java
diff --git a/net/android/java/src/org/chromium/net/NetworkChangeNotifier.java b/net/android/java/src/org/chromium/net/NetworkChangeNotifier.java
index 563ef5c0d2cda697938fedf3371f573a51ffa5ba..dec5c92900299258b84eb4ed3b7ff2d71b765280 100644
--- a/net/android/java/src/org/chromium/net/NetworkChangeNotifier.java
+++ b/net/android/java/src/org/chromium/net/NetworkChangeNotifier.java
@@ -111,20 +111,15 @@ public class NetworkChangeNotifier {
/**
* Enables auto detection of the current network state based on notifications from the system.
* Note that passing true here requires the embedding app have the platform ACCESS_NETWORK_STATE
- * permission.
+ * permission. Also note that in this case the auto detection is enabled based on the status of
+ * the application (@see ApplicationStatus).
*
* @param shouldAutoDetect true if the NetworkChangeNotifier should listen for system changes in
* network connectivity.
*/
public static void setAutoDetectConnectivityState(boolean shouldAutoDetect) {
- getInstance().setAutoDetectConnectivityStateInternal(shouldAutoDetect, false);
- }
-
- private void destroyAutoDetector() {
- if (mAutoDetector != null) {
- mAutoDetector.destroy();
- mAutoDetector = null;
- }
+ getInstance().setAutoDetectConnectivityStateInternal(
+ shouldAutoDetect, new RegistrationPolicyApplicationStatus());
}
/**
@@ -136,34 +131,47 @@ public class NetworkChangeNotifier {
* might perform expensive work depending on the network connectivity.
*/
public static void registerToReceiveNotificationsAlways() {
- getInstance().setAutoDetectConnectivityStateInternal(true, true);
+ getInstance().setAutoDetectConnectivityStateInternal(
+ true, new RegistrationPolicyAlwaysRegister());
+ }
+
+ /**
+ * Registers to receive network change notification based on the provided registration policy.
+ */
+ public static void setAutoDetectConnectivityState(RegistrationPolicy policy) {
+ getInstance().setAutoDetectConnectivityStateInternal(true, policy);
+ }
+
+ private void destroyAutoDetector() {
+ if (mAutoDetector != null) {
+ mAutoDetector.destroy();
+ mAutoDetector = null;
+ }
}
private void setAutoDetectConnectivityStateInternal(
- boolean shouldAutoDetect, boolean alwaysWatchForChanges) {
- if (shouldAutoDetect) {
- if (mAutoDetector == null) {
- mAutoDetector = new NetworkChangeNotifierAutoDetect(
- new NetworkChangeNotifierAutoDetect.Observer() {
- @Override
- public void onConnectionTypeChanged(int newConnectionType) {
- updateCurrentConnectionType(newConnectionType);
- }
- @Override
- public void onMaxBandwidthChanged(double maxBandwidthMbps) {
- updateCurrentMaxBandwidth(maxBandwidthMbps);
- }
- },
- mContext,
- alwaysWatchForChanges);
- final NetworkChangeNotifierAutoDetect.NetworkState networkState =
- mAutoDetector.getCurrentNetworkState();
- updateCurrentConnectionType(mAutoDetector.getCurrentConnectionType(networkState));
- updateCurrentMaxBandwidth(mAutoDetector.getCurrentMaxBandwidthInMbps(networkState));
- }
- } else {
+ boolean shouldAutoDetect, RegistrationPolicy policy) {
pauljensen 2015/10/01 12:06:36 It looks like you refactored the body of this func
timvolodine 2015/10/05 17:26:45 sure no problem, this is not essential anyway. don
+ if (!shouldAutoDetect) {
destroyAutoDetector();
+ return;
}
+ if (mAutoDetector != null) return;
+
+ mAutoDetector =
+ new NetworkChangeNotifierAutoDetect(new NetworkChangeNotifierAutoDetect.Observer() {
+ @Override
+ public void onConnectionTypeChanged(int newConnectionType) {
+ updateCurrentConnectionType(newConnectionType);
+ }
+ @Override
+ public void onMaxBandwidthChanged(double maxBandwidthMbps) {
+ updateCurrentMaxBandwidth(maxBandwidthMbps);
+ }
+ }, mContext, policy);
+ final NetworkChangeNotifierAutoDetect.NetworkState networkState =
+ mAutoDetector.getCurrentNetworkState();
+ updateCurrentConnectionType(mAutoDetector.getCurrentConnectionType(networkState));
+ updateCurrentMaxBandwidth(mAutoDetector.getCurrentMaxBandwidthInMbps(networkState));
}
/**

Powered by Google App Engine
This is Rietveld 408576698