OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.net; | 5 package org.chromium.net; |
6 | 6 |
7 import android.Manifest.permission; | 7 import android.Manifest.permission; |
8 import android.content.BroadcastReceiver; | 8 import android.content.BroadcastReceiver; |
9 import android.content.Context; | 9 import android.content.Context; |
10 import android.content.Intent; | 10 import android.content.Intent; |
11 import android.content.IntentFilter; | 11 import android.content.IntentFilter; |
12 import android.content.pm.PackageManager; | 12 import android.content.pm.PackageManager; |
13 import android.net.ConnectivityManager; | 13 import android.net.ConnectivityManager; |
14 import android.net.NetworkInfo; | 14 import android.net.NetworkInfo; |
15 import android.net.wifi.WifiInfo; | 15 import android.net.wifi.WifiInfo; |
16 import android.net.wifi.WifiManager; | 16 import android.net.wifi.WifiManager; |
17 import android.telephony.TelephonyManager; | 17 import android.telephony.TelephonyManager; |
18 import android.util.Log; | 18 import android.util.Log; |
19 | 19 |
20 import org.chromium.base.ApplicationState; | |
21 import org.chromium.base.ApplicationStatus; | |
22 import org.chromium.base.VisibleForTesting; | 20 import org.chromium.base.VisibleForTesting; |
23 | 21 |
24 /** | 22 /** |
25 * Used by the NetworkChangeNotifier to listens to platform changes in connectiv
ity. | 23 * Used by the NetworkChangeNotifier to listens to platform changes in connectiv
ity. |
26 * Note that use of this class requires that the app have the platform | 24 * Note that use of this class requires that the app have the platform |
27 * ACCESS_NETWORK_STATE permission. | 25 * ACCESS_NETWORK_STATE permission. |
28 */ | 26 */ |
29 public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver | 27 public class NetworkChangeNotifierAutoDetect |
30 implements ApplicationStatus.ApplicationStateListener { | 28 extends BroadcastReceiver implements RegistrationPolicy.Listener { |
31 | |
32 static class NetworkState { | 29 static class NetworkState { |
33 private final boolean mConnected; | 30 private final boolean mConnected; |
34 private final int mType; | 31 private final int mType; |
35 private final int mSubtype; | 32 private final int mSubtype; |
36 | 33 |
37 public NetworkState(boolean connected, int type, int subtype) { | 34 public NetworkState(boolean connected, int type, int subtype) { |
38 mConnected = connected; | 35 mConnected = connected; |
39 mType = type; | 36 mType = type; |
40 mSubtype = subtype; | 37 mSubtype = subtype; |
41 } | 38 } |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 return wifiInfo.getLinkSpeed(); | 129 return wifiInfo.getLinkSpeed(); |
133 } | 130 } |
134 | 131 |
135 boolean getHasWifiPermission() { | 132 boolean getHasWifiPermission() { |
136 return mHasWifiPermission; | 133 return mHasWifiPermission; |
137 } | 134 } |
138 } | 135 } |
139 | 136 |
140 private static final String TAG = "NetworkChangeNotifierAutoDetect"; | 137 private static final String TAG = "NetworkChangeNotifierAutoDetect"; |
141 private static final int UNKNOWN_LINK_SPEED = -1; | 138 private static final int UNKNOWN_LINK_SPEED = -1; |
| 139 |
142 private final NetworkConnectivityIntentFilter mIntentFilter; | 140 private final NetworkConnectivityIntentFilter mIntentFilter; |
| 141 private final Observer mObserver; |
| 142 private final Context mContext; |
| 143 private final RegistrationPolicy mRegistrationPolicy; |
143 | 144 |
144 private final Observer mObserver; | |
145 | |
146 private final Context mContext; | |
147 private ConnectivityManagerDelegate mConnectivityManagerDelegate; | 145 private ConnectivityManagerDelegate mConnectivityManagerDelegate; |
148 private WifiManagerDelegate mWifiManagerDelegate; | 146 private WifiManagerDelegate mWifiManagerDelegate; |
149 private boolean mRegistered; | 147 private boolean mRegistered; |
150 private final boolean mApplicationStateRegistered; | |
151 private int mConnectionType; | 148 private int mConnectionType; |
152 private String mWifiSSID; | 149 private String mWifiSSID; |
153 private double mMaxBandwidthMbps; | 150 private double mMaxBandwidthMbps; |
154 | 151 |
155 /** | 152 /** |
156 * Observer notified on the UI thread whenever a new connection type was det
ected or max | 153 * Observer notified on the UI thread whenever a new connection type was det
ected or max |
157 * bandwidth is changed. | 154 * bandwidth is changed. |
158 */ | 155 */ |
159 public static interface Observer { | 156 public static interface Observer { |
160 public void onConnectionTypeChanged(int newConnectionType); | 157 public void onConnectionTypeChanged(int newConnectionType); |
161 public void onMaxBandwidthChanged(double maxBandwidthMbps); | 158 public void onMaxBandwidthChanged(double maxBandwidthMbps); |
162 } | 159 } |
163 | 160 |
164 /** | 161 /** |
165 * Constructs a NetworkChangeNotifierAutoDetect. | 162 * Constructs a NetworkChangeNotifierAutoDetect. |
166 * @param alwaysWatchForChanges If true, always watch for network changes. | 163 * @param alwaysWatchForChanges If true, always watch for network changes. |
167 * Otherwise, only watch if app is in foreground. | 164 * Otherwise, only watch if app is in foreground. |
168 */ | 165 */ |
169 public NetworkChangeNotifierAutoDetect(Observer observer, Context context, | 166 public NetworkChangeNotifierAutoDetect( |
170 boolean alwaysWatchForChanges) { | 167 Observer observer, Context context, RegistrationPolicy policy) { |
171 mObserver = observer; | 168 mObserver = observer; |
172 mContext = context.getApplicationContext(); | 169 mContext = context.getApplicationContext(); |
173 mConnectivityManagerDelegate = new ConnectivityManagerDelegate(context); | 170 mConnectivityManagerDelegate = new ConnectivityManagerDelegate(context); |
174 mWifiManagerDelegate = new WifiManagerDelegate(context); | 171 mWifiManagerDelegate = new WifiManagerDelegate(context); |
175 final NetworkState networkState = mConnectivityManagerDelegate.getNetwor
kState(); | 172 final NetworkState networkState = mConnectivityManagerDelegate.getNetwor
kState(); |
176 mConnectionType = getCurrentConnectionType(networkState); | 173 mConnectionType = getCurrentConnectionType(networkState); |
177 mWifiSSID = getCurrentWifiSSID(networkState); | 174 mWifiSSID = getCurrentWifiSSID(networkState); |
178 mMaxBandwidthMbps = getCurrentMaxBandwidthInMbps(networkState); | 175 mMaxBandwidthMbps = getCurrentMaxBandwidthInMbps(networkState); |
179 mIntentFilter = | 176 mIntentFilter = |
180 new NetworkConnectivityIntentFilter(mWifiManagerDelegate.getHasW
ifiPermission()); | 177 new NetworkConnectivityIntentFilter(mWifiManagerDelegate.getHasW
ifiPermission()); |
181 | 178 mRegistrationPolicy = policy; |
182 if (alwaysWatchForChanges) { | 179 mRegistrationPolicy.init(this); |
183 registerReceiver(); | |
184 mApplicationStateRegistered = false; | |
185 } else { | |
186 ApplicationStatus.registerApplicationStateListener(this); | |
187 onApplicationStateChange(getApplicationState()); | |
188 mApplicationStateRegistered = true; | |
189 } | |
190 } | 180 } |
191 | 181 |
192 /** | 182 /** |
193 * Allows overriding the ConnectivityManagerDelegate for tests. | 183 * Allows overriding the ConnectivityManagerDelegate for tests. |
194 */ | 184 */ |
195 void setConnectivityManagerDelegateForTests(ConnectivityManagerDelegate dele
gate) { | 185 void setConnectivityManagerDelegateForTests(ConnectivityManagerDelegate dele
gate) { |
196 mConnectivityManagerDelegate = delegate; | 186 mConnectivityManagerDelegate = delegate; |
197 } | 187 } |
198 | 188 |
199 /** | 189 /** |
200 * Allows overriding the WifiManagerDelegate for tests. | 190 * Allows overriding the WifiManagerDelegate for tests. |
201 */ | 191 */ |
202 void setWifiManagerDelegateForTests(WifiManagerDelegate delegate) { | 192 void setWifiManagerDelegateForTests(WifiManagerDelegate delegate) { |
203 mWifiManagerDelegate = delegate; | 193 mWifiManagerDelegate = delegate; |
204 } | 194 } |
205 | 195 |
206 /** | |
207 * Returns the activity's status. | |
208 * @return an {@code int} that is one of {@code ApplicationState.HAS_*_ACTIV
ITIES}. | |
209 */ | |
210 @VisibleForTesting | 196 @VisibleForTesting |
211 int getApplicationState() { | 197 RegistrationPolicy getRegistrationPolicy() { |
212 return ApplicationStatus.getStateForApplication(); | 198 return mRegistrationPolicy; |
213 } | 199 } |
214 | 200 |
215 /** | 201 /** |
216 * Returns whether the object has registered to receive network connectivity
intents. | 202 * Returns whether the object has registered to receive network connectivity
intents. |
217 */ | 203 */ |
218 @VisibleForTesting | 204 @VisibleForTesting |
219 boolean isReceiverRegisteredForTesting() { | 205 boolean isReceiverRegisteredForTesting() { |
220 return mRegistered; | 206 return mRegistered; |
221 } | 207 } |
222 | 208 |
223 public void destroy() { | 209 public void destroy() { |
224 if (mApplicationStateRegistered) ApplicationStatus.unregisterApplication
StateListener(this); | 210 mRegistrationPolicy.destroy(); |
225 unregisterReceiver(); | 211 unregister(); |
226 } | |
227 | |
228 /** | |
229 * Register a BroadcastReceiver in the given context. | |
230 */ | |
231 private void registerReceiver() { | |
232 if (!mRegistered) { | |
233 mRegistered = true; | |
234 mContext.registerReceiver(this, mIntentFilter); | |
235 } | |
236 } | |
237 | |
238 /** | |
239 * Unregister the BroadcastReceiver in the given context. | |
240 */ | |
241 private void unregisterReceiver() { | |
242 if (mRegistered) { | |
243 mRegistered = false; | |
244 mContext.unregisterReceiver(this); | |
245 } | |
246 } | 212 } |
247 | 213 |
248 public NetworkState getCurrentNetworkState() { | 214 public NetworkState getCurrentNetworkState() { |
249 return mConnectivityManagerDelegate.getNetworkState(); | 215 return mConnectivityManagerDelegate.getNetworkState(); |
250 } | 216 } |
251 | 217 |
252 public int getCurrentConnectionType(NetworkState networkState) { | 218 public int getCurrentConnectionType(NetworkState networkState) { |
253 if (!networkState.isConnected()) { | 219 if (!networkState.isConnected()) { |
254 return ConnectionType.CONNECTION_NONE; | 220 return ConnectionType.CONNECTION_NONE; |
255 } | 221 } |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 public void onReceive(Context context, Intent intent) { | 338 public void onReceive(Context context, Intent intent) { |
373 final NetworkState networkState = getCurrentNetworkState(); | 339 final NetworkState networkState = getCurrentNetworkState(); |
374 if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction()))
{ | 340 if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction()))
{ |
375 connectionTypeChanged(networkState); | 341 connectionTypeChanged(networkState); |
376 maxBandwidthChanged(networkState); | 342 maxBandwidthChanged(networkState); |
377 } else if (WifiManager.RSSI_CHANGED_ACTION.equals(intent.getAction())) { | 343 } else if (WifiManager.RSSI_CHANGED_ACTION.equals(intent.getAction())) { |
378 maxBandwidthChanged(networkState); | 344 maxBandwidthChanged(networkState); |
379 } | 345 } |
380 } | 346 } |
381 | 347 |
382 // ApplicationStatus.ApplicationStateListener | 348 // RegistrationPolicy.Listener |
383 @Override | 349 @Override |
384 public void onApplicationStateChange(int newState) { | 350 public void register() { |
| 351 if (mRegistered) return; |
| 352 |
385 final NetworkState networkState = getCurrentNetworkState(); | 353 final NetworkState networkState = getCurrentNetworkState(); |
386 if (newState == ApplicationState.HAS_RUNNING_ACTIVITIES) { | 354 connectionTypeChanged(networkState); |
387 connectionTypeChanged(networkState); | 355 maxBandwidthChanged(networkState); |
388 maxBandwidthChanged(networkState); | 356 mContext.registerReceiver(this, mIntentFilter); |
389 registerReceiver(); | 357 mRegistered = true; |
390 } else if (newState == ApplicationState.HAS_PAUSED_ACTIVITIES) { | 358 } |
391 unregisterReceiver(); | 359 |
392 } | 360 @Override |
| 361 public void unregister() { |
| 362 if (!mRegistered) return; |
| 363 mContext.unregisterReceiver(this); |
| 364 mRegistered = false; |
393 } | 365 } |
394 | 366 |
395 private void connectionTypeChanged(NetworkState networkState) { | 367 private void connectionTypeChanged(NetworkState networkState) { |
396 int newConnectionType = getCurrentConnectionType(networkState); | 368 int newConnectionType = getCurrentConnectionType(networkState); |
397 String newWifiSSID = getCurrentWifiSSID(networkState); | 369 String newWifiSSID = getCurrentWifiSSID(networkState); |
398 if (newConnectionType == mConnectionType && newWifiSSID.equals(mWifiSSID
)) return; | 370 if (newConnectionType == mConnectionType && newWifiSSID.equals(mWifiSSID
)) return; |
399 | 371 |
400 mConnectionType = newConnectionType; | 372 mConnectionType = newConnectionType; |
401 mWifiSSID = newWifiSSID; | 373 mWifiSSID = newWifiSSID; |
402 Log.d(TAG, "Network connectivity changed, type is: " + mConnectionType); | 374 Log.d(TAG, "Network connectivity changed, type is: " + mConnectionType); |
403 mObserver.onConnectionTypeChanged(newConnectionType); | 375 mObserver.onConnectionTypeChanged(newConnectionType); |
404 } | 376 } |
405 | 377 |
406 private void maxBandwidthChanged(NetworkState networkState) { | 378 private void maxBandwidthChanged(NetworkState networkState) { |
407 double newMaxBandwidthMbps = getCurrentMaxBandwidthInMbps(networkState); | 379 double newMaxBandwidthMbps = getCurrentMaxBandwidthInMbps(networkState); |
408 if (newMaxBandwidthMbps == mMaxBandwidthMbps) return; | 380 if (newMaxBandwidthMbps == mMaxBandwidthMbps) return; |
409 mMaxBandwidthMbps = newMaxBandwidthMbps; | 381 mMaxBandwidthMbps = newMaxBandwidthMbps; |
410 mObserver.onMaxBandwidthChanged(newMaxBandwidthMbps); | 382 mObserver.onMaxBandwidthChanged(newMaxBandwidthMbps); |
411 } | 383 } |
412 | 384 |
413 private static class NetworkConnectivityIntentFilter extends IntentFilter { | 385 private static class NetworkConnectivityIntentFilter extends IntentFilter { |
414 NetworkConnectivityIntentFilter(boolean monitorRSSI) { | 386 NetworkConnectivityIntentFilter(boolean monitorRSSI) { |
415 addAction(ConnectivityManager.CONNECTIVITY_ACTION); | 387 addAction(ConnectivityManager.CONNECTIVITY_ACTION); |
416 if (monitorRSSI) addAction(WifiManager.RSSI_CHANGED_ACTION); | 388 if (monitorRSSI) addAction(WifiManager.RSSI_CHANGED_ACTION); |
417 } | 389 } |
418 } | 390 } |
419 } | 391 } |
OLD | NEW |