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

Unified Diff: net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.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/NetworkChangeNotifierAutoDetect.java
diff --git a/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java b/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
index 466d03f194e83a9caa43a54e57062f09292d04b6..468a1b42c5de393bc565311f839a7614051a5a96 100644
--- a/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
+++ b/net/android/java/src/org/chromium/net/NetworkChangeNotifierAutoDetect.java
@@ -17,8 +17,6 @@ import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.util.Log;
-import org.chromium.base.ApplicationState;
-import org.chromium.base.ApplicationStatus;
import org.chromium.base.VisibleForTesting;
/**
@@ -26,9 +24,8 @@ import org.chromium.base.VisibleForTesting;
* Note that use of this class requires that the app have the platform
* ACCESS_NETWORK_STATE permission.
*/
-public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
- implements ApplicationStatus.ApplicationStateListener {
-
+public class NetworkChangeNotifierAutoDetect
+ extends BroadcastReceiver implements RegistrationPolicy.Listener {
static class NetworkState {
private final boolean mConnected;
private final int mType;
@@ -139,15 +136,15 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
private static final String TAG = "NetworkChangeNotifierAutoDetect";
private static final int UNKNOWN_LINK_SPEED = -1;
- private final NetworkConnectivityIntentFilter mIntentFilter;
+ private final NetworkConnectivityIntentFilter mIntentFilter;
private final Observer mObserver;
-
private final Context mContext;
+ private final RegistrationPolicy mRegistrationPolicy;
+
private ConnectivityManagerDelegate mConnectivityManagerDelegate;
private WifiManagerDelegate mWifiManagerDelegate;
private boolean mRegistered;
- private final boolean mApplicationStateRegistered;
private int mConnectionType;
private String mWifiSSID;
private double mMaxBandwidthMbps;
@@ -166,8 +163,8 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
* @param alwaysWatchForChanges If true, always watch for network changes.
* Otherwise, only watch if app is in foreground.
*/
- public NetworkChangeNotifierAutoDetect(Observer observer, Context context,
- boolean alwaysWatchForChanges) {
+ public NetworkChangeNotifierAutoDetect(
+ Observer observer, Context context, RegistrationPolicy policy) {
mObserver = observer;
mContext = context.getApplicationContext();
mConnectivityManagerDelegate = new ConnectivityManagerDelegate(context);
@@ -178,15 +175,8 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
mMaxBandwidthMbps = getCurrentMaxBandwidthInMbps(networkState);
mIntentFilter =
new NetworkConnectivityIntentFilter(mWifiManagerDelegate.getHasWifiPermission());
-
- if (alwaysWatchForChanges) {
- registerReceiver();
- mApplicationStateRegistered = false;
- } else {
- ApplicationStatus.registerApplicationStateListener(this);
- onApplicationStateChange(getApplicationState());
- mApplicationStateRegistered = true;
- }
+ mRegistrationPolicy = policy;
+ mRegistrationPolicy.init(this);
}
/**
@@ -203,13 +193,9 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
mWifiManagerDelegate = delegate;
}
- /**
- * Returns the activity's status.
- * @return an {@code int} that is one of {@code ApplicationState.HAS_*_ACTIVITIES}.
- */
@VisibleForTesting
- int getApplicationState() {
- return ApplicationStatus.getStateForApplication();
+ RegistrationPolicy getRegistrationPolicy() {
+ return mRegistrationPolicy;
}
/**
@@ -221,28 +207,8 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
}
public void destroy() {
- if (mApplicationStateRegistered) ApplicationStatus.unregisterApplicationStateListener(this);
- unregisterReceiver();
- }
-
- /**
- * Register a BroadcastReceiver in the given context.
- */
- private void registerReceiver() {
- if (!mRegistered) {
- mRegistered = true;
- mContext.registerReceiver(this, mIntentFilter);
- }
- }
-
- /**
- * Unregister the BroadcastReceiver in the given context.
- */
- private void unregisterReceiver() {
- if (mRegistered) {
- mRegistered = false;
- mContext.unregisterReceiver(this);
- }
+ mRegistrationPolicy.destroy();
+ unregister();
}
public NetworkState getCurrentNetworkState() {
@@ -379,17 +345,23 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
}
}
- // ApplicationStatus.ApplicationStateListener
+ // RegistrationPolicy.Listener
@Override
- public void onApplicationStateChange(int newState) {
+ public void register() {
+ if (mRegistered) return;
+
final NetworkState networkState = getCurrentNetworkState();
- if (newState == ApplicationState.HAS_RUNNING_ACTIVITIES) {
- connectionTypeChanged(networkState);
- maxBandwidthChanged(networkState);
- registerReceiver();
- } else if (newState == ApplicationState.HAS_PAUSED_ACTIVITIES) {
- unregisterReceiver();
- }
+ connectionTypeChanged(networkState);
+ maxBandwidthChanged(networkState);
+ mContext.registerReceiver(this, mIntentFilter);
+ mRegistered = true;
+ }
+
+ @Override
+ public void unregister() {
+ if (!mRegistered) return;
+ mContext.unregisterReceiver(this);
+ mRegistered = false;
}
private void connectionTypeChanged(NetworkState networkState) {

Powered by Google App Engine
This is Rietveld 408576698