| 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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 static void resetInstanceForTests(Context context) { | 66 static void resetInstanceForTests(Context context) { |
| 67 sInstance = new NetworkChangeNotifier(context); | 67 sInstance = new NetworkChangeNotifier(context); |
| 68 } | 68 } |
| 69 | 69 |
| 70 @CalledByNative | 70 @CalledByNative |
| 71 public int getCurrentConnectionType() { | 71 public int getCurrentConnectionType() { |
| 72 return mCurrentConnectionType; | 72 return mCurrentConnectionType; |
| 73 } | 73 } |
| 74 | 74 |
| 75 @CalledByNative | 75 @CalledByNative |
| 76 public double getCurrentMaxBandwidth() { | 76 public double getCurrentMaxBandwidthInMbps() { |
| 77 return mCurrentMaxBandwidth; | 77 return mCurrentMaxBandwidth; |
| 78 } | 78 } |
| 79 | 79 |
| 80 /** | 80 /** |
| 81 * Calls a native map lookup of subtype to max bandwidth. | 81 * Calls a native map lookup of subtype to max bandwidth. |
| 82 */ | 82 */ |
| 83 public static double getMaxBandwidthForConnectionSubtype(int subtype) { | 83 public static double getMaxBandwidthForConnectionSubtype(int subtype) { |
| 84 return nativeGetMaxBandwidthForConnectionSubtype(subtype); | 84 return nativeGetMaxBandwidthForConnectionSubtype(subtype); |
| 85 } | 85 } |
| 86 | 86 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 } | 140 } |
| 141 | 141 |
| 142 private void setAutoDetectConnectivityStateInternal( | 142 private void setAutoDetectConnectivityStateInternal( |
| 143 boolean shouldAutoDetect, boolean alwaysWatchForChanges) { | 143 boolean shouldAutoDetect, boolean alwaysWatchForChanges) { |
| 144 if (shouldAutoDetect) { | 144 if (shouldAutoDetect) { |
| 145 if (mAutoDetector == null) { | 145 if (mAutoDetector == null) { |
| 146 mAutoDetector = new NetworkChangeNotifierAutoDetect( | 146 mAutoDetector = new NetworkChangeNotifierAutoDetect( |
| 147 new NetworkChangeNotifierAutoDetect.Observer() { | 147 new NetworkChangeNotifierAutoDetect.Observer() { |
| 148 @Override | 148 @Override |
| 149 public void onConnectionTypeChanged(int newConnectionTyp
e) { | 149 public void onConnectionTypeChanged(int newConnectionTyp
e) { |
| 150 updateCurrentMaxBandwidth(mAutoDetector.getCurrentMa
xBandwidthInMbps()); | |
| 151 updateCurrentConnectionType(newConnectionType); | 150 updateCurrentConnectionType(newConnectionType); |
| 152 } | 151 } |
| 152 @Override |
| 153 public void onMaxBandwidthChanged(double maxBandwidthMbp
s) { |
| 154 updateCurrentMaxBandwidth(maxBandwidthMbps); |
| 155 } |
| 153 }, | 156 }, |
| 154 mContext, | 157 mContext, |
| 155 alwaysWatchForChanges); | 158 alwaysWatchForChanges); |
| 156 updateCurrentMaxBandwidth(mAutoDetector.getCurrentMaxBandwidthIn
Mbps()); | 159 final NetworkChangeNotifierAutoDetect.NetworkState networkState
= |
| 157 updateCurrentConnectionType(mAutoDetector.getCurrentConnectionTy
pe()); | 160 mAutoDetector.getCurrentNetworkState(); |
| 161 updateCurrentConnectionType(mAutoDetector.getCurrentConnectionTy
pe(networkState)); |
| 162 updateCurrentMaxBandwidth(mAutoDetector.getCurrentMaxBandwidthIn
Mbps(networkState)); |
| 158 } | 163 } |
| 159 } else { | 164 } else { |
| 160 destroyAutoDetector(); | 165 destroyAutoDetector(); |
| 161 } | 166 } |
| 162 } | 167 } |
| 163 | 168 |
| 164 /** | 169 /** |
| 165 * Updates the perceived network state when not auto-detecting changes to co
nnectivity. | 170 * Updates the perceived network state when not auto-detecting changes to co
nnectivity. |
| 166 * | 171 * |
| 167 * @param networkAvailable True if the NetworkChangeNotifier should perceive
a "connected" | 172 * @param networkAvailable True if the NetworkChangeNotifier should perceive
a "connected" |
| 168 * state, false implies "disconnected". | 173 * state, false implies "disconnected". |
| 169 */ | 174 */ |
| 170 @CalledByNative | 175 @CalledByNative |
| 171 public static void forceConnectivityState(boolean networkAvailable) { | 176 public static void forceConnectivityState(boolean networkAvailable) { |
| 172 setAutoDetectConnectivityState(false); | 177 setAutoDetectConnectivityState(false); |
| 173 getInstance().forceConnectivityStateInternal(networkAvailable); | 178 getInstance().forceConnectivityStateInternal(networkAvailable); |
| 174 } | 179 } |
| 175 | 180 |
| 176 private void forceConnectivityStateInternal(boolean forceOnline) { | 181 private void forceConnectivityStateInternal(boolean forceOnline) { |
| 177 boolean connectionCurrentlyExists = | 182 boolean connectionCurrentlyExists = |
| 178 mCurrentConnectionType != ConnectionType.CONNECTION_NONE; | 183 mCurrentConnectionType != ConnectionType.CONNECTION_NONE; |
| 179 if (connectionCurrentlyExists != forceOnline) { | 184 if (connectionCurrentlyExists != forceOnline) { |
| 180 updateCurrentMaxBandwidth(forceOnline ? Double.POSITIVE_INFINITY : 0
.0); | |
| 181 updateCurrentConnectionType(forceOnline ? ConnectionType.CONNECTION_
UNKNOWN | 185 updateCurrentConnectionType(forceOnline ? ConnectionType.CONNECTION_
UNKNOWN |
| 182 : ConnectionType.CONNECTION_NONE); | 186 : ConnectionType.CONNECTION_NONE); |
| 187 updateCurrentMaxBandwidth(forceOnline ? Double.POSITIVE_INFINITY : 0
.0); |
| 183 } | 188 } |
| 184 } | 189 } |
| 185 | 190 |
| 186 private void updateCurrentConnectionType(int newConnectionType) { | 191 private void updateCurrentConnectionType(int newConnectionType) { |
| 187 mCurrentConnectionType = newConnectionType; | 192 mCurrentConnectionType = newConnectionType; |
| 188 notifyObserversOfConnectionTypeChange(newConnectionType); | 193 notifyObserversOfConnectionTypeChange(newConnectionType); |
| 189 } | 194 } |
| 190 | 195 |
| 191 private void updateCurrentMaxBandwidth(double maxBandwidth) { | 196 private void updateCurrentMaxBandwidth(double maxBandwidthMbps) { |
| 192 mCurrentMaxBandwidth = maxBandwidth; | 197 if (maxBandwidthMbps == mCurrentMaxBandwidth) return; |
| 198 mCurrentMaxBandwidth = maxBandwidthMbps; |
| 199 notifyObserversOfMaxBandwidthChange(maxBandwidthMbps); |
| 193 } | 200 } |
| 194 | 201 |
| 195 /** | 202 /** |
| 196 * Alerts all observers of a connection change. | 203 * Alerts all observers of a connection change. |
| 197 */ | 204 */ |
| 198 void notifyObserversOfConnectionTypeChange(int newConnectionType) { | 205 void notifyObserversOfConnectionTypeChange(int newConnectionType) { |
| 199 for (Long nativeChangeNotifier : mNativeChangeNotifiers) { | 206 for (Long nativeChangeNotifier : mNativeChangeNotifiers) { |
| 200 nativeNotifyConnectionTypeChanged(nativeChangeNotifier, newConnectio
nType); | 207 nativeNotifyConnectionTypeChanged(nativeChangeNotifier, newConnectio
nType); |
| 201 } | 208 } |
| 202 for (ConnectionTypeObserver observer : mConnectionTypeObservers) { | 209 for (ConnectionTypeObserver observer : mConnectionTypeObservers) { |
| 203 observer.onConnectionTypeChanged(newConnectionType); | 210 observer.onConnectionTypeChanged(newConnectionType); |
| 204 } | 211 } |
| 205 } | 212 } |
| 206 | 213 |
| 207 /** | 214 /** |
| 215 * Alerts all observers of a bandwidth change. |
| 216 */ |
| 217 void notifyObserversOfMaxBandwidthChange(double maxBandwidthMbps) { |
| 218 for (Long nativeChangeNotifier : mNativeChangeNotifiers) { |
| 219 nativeNotifyMaxBandwidthChanged(nativeChangeNotifier, maxBandwidthMb
ps); |
| 220 } |
| 221 } |
| 222 |
| 223 /** |
| 208 * Adds an observer for any connection type changes. | 224 * Adds an observer for any connection type changes. |
| 209 */ | 225 */ |
| 210 public static void addConnectionTypeObserver(ConnectionTypeObserver observer
) { | 226 public static void addConnectionTypeObserver(ConnectionTypeObserver observer
) { |
| 211 getInstance().addConnectionTypeObserverInternal(observer); | 227 getInstance().addConnectionTypeObserverInternal(observer); |
| 212 } | 228 } |
| 213 | 229 |
| 214 private void addConnectionTypeObserverInternal(ConnectionTypeObserver observ
er) { | 230 private void addConnectionTypeObserverInternal(ConnectionTypeObserver observ
er) { |
| 215 mConnectionTypeObservers.addObserver(observer); | 231 mConnectionTypeObservers.addObserver(observer); |
| 216 } | 232 } |
| 217 | 233 |
| 218 /** | 234 /** |
| 219 * Removes an observer for any connection type changes. | 235 * Removes an observer for any connection type changes. |
| 220 */ | 236 */ |
| 221 public static void removeConnectionTypeObserver(ConnectionTypeObserver obser
ver) { | 237 public static void removeConnectionTypeObserver(ConnectionTypeObserver obser
ver) { |
| 222 getInstance().removeConnectionTypeObserverInternal(observer); | 238 getInstance().removeConnectionTypeObserverInternal(observer); |
| 223 } | 239 } |
| 224 | 240 |
| 225 private void removeConnectionTypeObserverInternal(ConnectionTypeObserver obs
erver) { | 241 private void removeConnectionTypeObserverInternal(ConnectionTypeObserver obs
erver) { |
| 226 mConnectionTypeObservers.removeObserver(observer); | 242 mConnectionTypeObservers.removeObserver(observer); |
| 227 } | 243 } |
| 228 | 244 |
| 229 @NativeClassQualifiedName("NetworkChangeNotifierDelegateAndroid") | 245 @NativeClassQualifiedName("NetworkChangeNotifierDelegateAndroid") |
| 230 private native void nativeNotifyConnectionTypeChanged(long nativePtr, int ne
wConnectionType); | 246 private native void nativeNotifyConnectionTypeChanged(long nativePtr, int ne
wConnectionType); |
| 231 | 247 |
| 248 @NativeClassQualifiedName("NetworkChangeNotifierDelegateAndroid") |
| 249 private native void nativeNotifyMaxBandwidthChanged(long nativePtr, double m
axBandwidthMbps); |
| 250 |
| 232 private static native double nativeGetMaxBandwidthForConnectionSubtype(int s
ubtype); | 251 private static native double nativeGetMaxBandwidthForConnectionSubtype(int s
ubtype); |
| 233 | 252 |
| 234 // For testing only. | 253 // For testing only. |
| 235 public static NetworkChangeNotifierAutoDetect getAutoDetectorForTest() { | 254 public static NetworkChangeNotifierAutoDetect getAutoDetectorForTest() { |
| 236 return getInstance().mAutoDetector; | 255 return getInstance().mAutoDetector; |
| 237 } | 256 } |
| 238 | 257 |
| 239 /** | 258 /** |
| 240 * Checks if there currently is connectivity. | 259 * Checks if there currently is connectivity. |
| 241 */ | 260 */ |
| 242 public static boolean isOnline() { | 261 public static boolean isOnline() { |
| 243 int connectionType = getInstance().getCurrentConnectionType(); | 262 int connectionType = getInstance().getCurrentConnectionType(); |
| 244 return connectionType != ConnectionType.CONNECTION_UNKNOWN | 263 return connectionType != ConnectionType.CONNECTION_UNKNOWN |
| 245 && connectionType != ConnectionType.CONNECTION_NONE; | 264 && connectionType != ConnectionType.CONNECTION_NONE; |
| 246 } | 265 } |
| 247 } | 266 } |
| OLD | NEW |