| 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.content.Context; | 7 import android.content.Context; |
| 8 | 8 |
| 9 import org.chromium.base.CalledByNative; | 9 import org.chromium.base.CalledByNative; |
| 10 import org.chromium.base.JNINamespace; | 10 import org.chromium.base.JNINamespace; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 @JNINamespace("net") | 26 @JNINamespace("net") |
| 27 public class NetworkChangeNotifier { | 27 public class NetworkChangeNotifier { |
| 28 /** | 28 /** |
| 29 * Alerted when the connection type of the network changes. | 29 * Alerted when the connection type of the network changes. |
| 30 * The alert is fired on the UI thread. | 30 * The alert is fired on the UI thread. |
| 31 */ | 31 */ |
| 32 public interface ConnectionTypeObserver { | 32 public interface ConnectionTypeObserver { |
| 33 public void onConnectionTypeChanged(int connectionType); | 33 public void onConnectionTypeChanged(int connectionType); |
| 34 } | 34 } |
| 35 | 35 |
| 36 // These constants must always match the ones in network_change_notifier.h. | |
| 37 public static final int CONNECTION_UNKNOWN = 0; | |
| 38 public static final int CONNECTION_ETHERNET = 1; | |
| 39 public static final int CONNECTION_WIFI = 2; | |
| 40 public static final int CONNECTION_2G = 3; | |
| 41 public static final int CONNECTION_3G = 4; | |
| 42 public static final int CONNECTION_4G = 5; | |
| 43 public static final int CONNECTION_NONE = 6; | |
| 44 public static final int CONNECTION_BLUETOOTH = 7; | |
| 45 | |
| 46 private final Context mContext; | 36 private final Context mContext; |
| 47 private final ArrayList<Long> mNativeChangeNotifiers; | 37 private final ArrayList<Long> mNativeChangeNotifiers; |
| 48 private final ObserverList<ConnectionTypeObserver> mConnectionTypeObservers; | 38 private final ObserverList<ConnectionTypeObserver> mConnectionTypeObservers; |
| 49 private NetworkChangeNotifierAutoDetect mAutoDetector; | 39 private NetworkChangeNotifierAutoDetect mAutoDetector; |
| 50 private int mCurrentConnectionType = CONNECTION_UNKNOWN; | 40 private int mCurrentConnectionType = ConnectionType.CONNECTION_UNKNOWN; |
| 41 private double mCurrentMaxBandwidth = Double.POSITIVE_INFINITY; |
| 51 | 42 |
| 52 private static NetworkChangeNotifier sInstance; | 43 private static NetworkChangeNotifier sInstance; |
| 53 | 44 |
| 54 private NetworkChangeNotifier(Context context) { | 45 private NetworkChangeNotifier(Context context) { |
| 55 mContext = context.getApplicationContext(); | 46 mContext = context.getApplicationContext(); |
| 56 mNativeChangeNotifiers = new ArrayList<Long>(); | 47 mNativeChangeNotifiers = new ArrayList<Long>(); |
| 57 mConnectionTypeObservers = new ObserverList<ConnectionTypeObserver>(); | 48 mConnectionTypeObservers = new ObserverList<ConnectionTypeObserver>(); |
| 58 } | 49 } |
| 59 | 50 |
| 60 /** | 51 /** |
| (...skipping 13 matching lines...) Expand all Loading... |
| 74 | 65 |
| 75 static void resetInstanceForTests(Context context) { | 66 static void resetInstanceForTests(Context context) { |
| 76 sInstance = new NetworkChangeNotifier(context); | 67 sInstance = new NetworkChangeNotifier(context); |
| 77 } | 68 } |
| 78 | 69 |
| 79 @CalledByNative | 70 @CalledByNative |
| 80 public int getCurrentConnectionType() { | 71 public int getCurrentConnectionType() { |
| 81 return mCurrentConnectionType; | 72 return mCurrentConnectionType; |
| 82 } | 73 } |
| 83 | 74 |
| 75 @CalledByNative |
| 76 public double getCurrentMaxBandwidth() { |
| 77 return mCurrentMaxBandwidth; |
| 78 } |
| 79 |
| 80 /** |
| 81 * Calls a native map lookup of subtype to max bandwidth. |
| 82 */ |
| 83 public static double getMaxBandwidthForConnectionSubtype(int subtype) { |
| 84 return nativeGetMaxBandwidthForConnectionSubtype(subtype); |
| 85 } |
| 86 |
| 84 /** | 87 /** |
| 85 * Adds a native-side observer. | 88 * Adds a native-side observer. |
| 86 */ | 89 */ |
| 87 @CalledByNative | 90 @CalledByNative |
| 88 public void addNativeObserver(long nativeChangeNotifier) { | 91 public void addNativeObserver(long nativeChangeNotifier) { |
| 89 mNativeChangeNotifiers.add(nativeChangeNotifier); | 92 mNativeChangeNotifiers.add(nativeChangeNotifier); |
| 90 } | 93 } |
| 91 | 94 |
| 92 /** | 95 /** |
| 93 * Removes a native-side observer. | 96 * Removes a native-side observer. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 } | 140 } |
| 138 | 141 |
| 139 private void setAutoDetectConnectivityStateInternal( | 142 private void setAutoDetectConnectivityStateInternal( |
| 140 boolean shouldAutoDetect, boolean alwaysWatchForChanges) { | 143 boolean shouldAutoDetect, boolean alwaysWatchForChanges) { |
| 141 if (shouldAutoDetect) { | 144 if (shouldAutoDetect) { |
| 142 if (mAutoDetector == null) { | 145 if (mAutoDetector == null) { |
| 143 mAutoDetector = new NetworkChangeNotifierAutoDetect( | 146 mAutoDetector = new NetworkChangeNotifierAutoDetect( |
| 144 new NetworkChangeNotifierAutoDetect.Observer() { | 147 new NetworkChangeNotifierAutoDetect.Observer() { |
| 145 @Override | 148 @Override |
| 146 public void onConnectionTypeChanged(int newConnectionTyp
e) { | 149 public void onConnectionTypeChanged(int newConnectionTyp
e) { |
| 150 updateCurrentMaxBandwidth(mAutoDetector.getCurrentMa
xBandwidthInMbps()); |
| 147 updateCurrentConnectionType(newConnectionType); | 151 updateCurrentConnectionType(newConnectionType); |
| 148 } | 152 } |
| 149 }, | 153 }, |
| 150 mContext, | 154 mContext, |
| 151 alwaysWatchForChanges); | 155 alwaysWatchForChanges); |
| 156 updateCurrentMaxBandwidth(mAutoDetector.getCurrentMaxBandwidthIn
Mbps()); |
| 152 updateCurrentConnectionType(mAutoDetector.getCurrentConnectionTy
pe()); | 157 updateCurrentConnectionType(mAutoDetector.getCurrentConnectionTy
pe()); |
| 153 } | 158 } |
| 154 } else { | 159 } else { |
| 155 destroyAutoDetector(); | 160 destroyAutoDetector(); |
| 156 } | 161 } |
| 157 } | 162 } |
| 158 | 163 |
| 159 /** | 164 /** |
| 160 * Updates the perceived network state when not auto-detecting changes to co
nnectivity. | 165 * Updates the perceived network state when not auto-detecting changes to co
nnectivity. |
| 161 * | 166 * |
| 162 * @param networkAvailable True if the NetworkChangeNotifier should perceive
a "connected" | 167 * @param networkAvailable True if the NetworkChangeNotifier should perceive
a "connected" |
| 163 * state, false implies "disconnected". | 168 * state, false implies "disconnected". |
| 164 */ | 169 */ |
| 165 @CalledByNative | 170 @CalledByNative |
| 166 public static void forceConnectivityState(boolean networkAvailable) { | 171 public static void forceConnectivityState(boolean networkAvailable) { |
| 167 setAutoDetectConnectivityState(false); | 172 setAutoDetectConnectivityState(false); |
| 168 getInstance().forceConnectivityStateInternal(networkAvailable); | 173 getInstance().forceConnectivityStateInternal(networkAvailable); |
| 169 } | 174 } |
| 170 | 175 |
| 171 private void forceConnectivityStateInternal(boolean forceOnline) { | 176 private void forceConnectivityStateInternal(boolean forceOnline) { |
| 172 boolean connectionCurrentlyExists = mCurrentConnectionType != CONNECTION
_NONE; | 177 boolean connectionCurrentlyExists = |
| 178 mCurrentConnectionType != ConnectionType.CONNECTION_NONE; |
| 173 if (connectionCurrentlyExists != forceOnline) { | 179 if (connectionCurrentlyExists != forceOnline) { |
| 174 updateCurrentConnectionType(forceOnline ? CONNECTION_UNKNOWN : CONNE
CTION_NONE); | 180 updateCurrentMaxBandwidth(forceOnline ? Double.POSITIVE_INFINITY : 0
.0); |
| 181 updateCurrentConnectionType(forceOnline ? ConnectionType.CONNECTION_
UNKNOWN |
| 182 : ConnectionType.CONNECTION_NONE); |
| 175 } | 183 } |
| 176 } | 184 } |
| 177 | 185 |
| 178 private void updateCurrentConnectionType(int newConnectionType) { | 186 private void updateCurrentConnectionType(int newConnectionType) { |
| 179 mCurrentConnectionType = newConnectionType; | 187 mCurrentConnectionType = newConnectionType; |
| 180 notifyObserversOfConnectionTypeChange(newConnectionType); | 188 notifyObserversOfConnectionTypeChange(newConnectionType); |
| 181 } | 189 } |
| 182 | 190 |
| 191 private void updateCurrentMaxBandwidth(double maxBandwidth) { |
| 192 mCurrentMaxBandwidth = maxBandwidth; |
| 193 } |
| 194 |
| 183 /** | 195 /** |
| 184 * Alerts all observers of a connection change. | 196 * Alerts all observers of a connection change. |
| 185 */ | 197 */ |
| 186 void notifyObserversOfConnectionTypeChange(int newConnectionType) { | 198 void notifyObserversOfConnectionTypeChange(int newConnectionType) { |
| 187 for (Long nativeChangeNotifier : mNativeChangeNotifiers) { | 199 for (Long nativeChangeNotifier : mNativeChangeNotifiers) { |
| 188 nativeNotifyConnectionTypeChanged(nativeChangeNotifier, newConnectio
nType); | 200 nativeNotifyConnectionTypeChanged(nativeChangeNotifier, newConnectio
nType); |
| 189 } | 201 } |
| 190 for (ConnectionTypeObserver observer : mConnectionTypeObservers) { | 202 for (ConnectionTypeObserver observer : mConnectionTypeObservers) { |
| 191 observer.onConnectionTypeChanged(newConnectionType); | 203 observer.onConnectionTypeChanged(newConnectionType); |
| 192 } | 204 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 210 getInstance().removeConnectionTypeObserverInternal(observer); | 222 getInstance().removeConnectionTypeObserverInternal(observer); |
| 211 } | 223 } |
| 212 | 224 |
| 213 private void removeConnectionTypeObserverInternal(ConnectionTypeObserver obs
erver) { | 225 private void removeConnectionTypeObserverInternal(ConnectionTypeObserver obs
erver) { |
| 214 mConnectionTypeObservers.removeObserver(observer); | 226 mConnectionTypeObservers.removeObserver(observer); |
| 215 } | 227 } |
| 216 | 228 |
| 217 @NativeClassQualifiedName("NetworkChangeNotifierDelegateAndroid") | 229 @NativeClassQualifiedName("NetworkChangeNotifierDelegateAndroid") |
| 218 private native void nativeNotifyConnectionTypeChanged(long nativePtr, int ne
wConnectionType); | 230 private native void nativeNotifyConnectionTypeChanged(long nativePtr, int ne
wConnectionType); |
| 219 | 231 |
| 232 private static native double nativeGetMaxBandwidthForConnectionSubtype(int s
ubtype); |
| 233 |
| 220 // For testing only. | 234 // For testing only. |
| 221 public static NetworkChangeNotifierAutoDetect getAutoDetectorForTest() { | 235 public static NetworkChangeNotifierAutoDetect getAutoDetectorForTest() { |
| 222 return getInstance().mAutoDetector; | 236 return getInstance().mAutoDetector; |
| 223 } | 237 } |
| 224 | 238 |
| 225 /** | 239 /** |
| 226 * Checks if there currently is connectivity. | 240 * Checks if there currently is connectivity. |
| 227 */ | 241 */ |
| 228 public static boolean isOnline() { | 242 public static boolean isOnline() { |
| 229 int connectionType = getInstance().getCurrentConnectionType(); | 243 int connectionType = getInstance().getCurrentConnectionType(); |
| 230 return connectionType != CONNECTION_UNKNOWN && connectionType != CONNECT
ION_NONE; | 244 return connectionType != ConnectionType.CONNECTION_UNKNOWN |
| 245 && connectionType != ConnectionType.CONNECTION_NONE; |
| 231 } | 246 } |
| 232 } | 247 } |
| OLD | NEW |