Chromium Code Reviews| 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 getCurrentMaxBandwidth() { |
|
pauljensen
2014/12/11 16:13:56
Can we add the InMbps suffix to this method simila
jkarlin
2014/12/15 19:43:55
Done.
| |
| 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); |
| 159 updateCurrentConnectionType(mAutoDetector.getCurrentConnectionTy pe()); | |
| 156 updateCurrentMaxBandwidth(mAutoDetector.getCurrentMaxBandwidthIn Mbps()); | 160 updateCurrentMaxBandwidth(mAutoDetector.getCurrentMaxBandwidthIn Mbps()); |
| 157 updateCurrentConnectionType(mAutoDetector.getCurrentConnectionTy pe()); | |
| 158 } | 161 } |
| 159 } else { | 162 } else { |
| 160 destroyAutoDetector(); | 163 destroyAutoDetector(); |
| 161 } | 164 } |
| 162 } | 165 } |
| 163 | 166 |
| 164 /** | 167 /** |
| 165 * Updates the perceived network state when not auto-detecting changes to co nnectivity. | 168 * Updates the perceived network state when not auto-detecting changes to co nnectivity. |
| 166 * | 169 * |
| 167 * @param networkAvailable True if the NetworkChangeNotifier should perceive a "connected" | 170 * @param networkAvailable True if the NetworkChangeNotifier should perceive a "connected" |
| 168 * state, false implies "disconnected". | 171 * state, false implies "disconnected". |
| 169 */ | 172 */ |
| 170 @CalledByNative | 173 @CalledByNative |
| 171 public static void forceConnectivityState(boolean networkAvailable) { | 174 public static void forceConnectivityState(boolean networkAvailable) { |
| 172 setAutoDetectConnectivityState(false); | 175 setAutoDetectConnectivityState(false); |
| 173 getInstance().forceConnectivityStateInternal(networkAvailable); | 176 getInstance().forceConnectivityStateInternal(networkAvailable); |
| 174 } | 177 } |
| 175 | 178 |
| 176 private void forceConnectivityStateInternal(boolean forceOnline) { | 179 private void forceConnectivityStateInternal(boolean forceOnline) { |
| 177 boolean connectionCurrentlyExists = | 180 boolean connectionCurrentlyExists = |
| 178 mCurrentConnectionType != ConnectionType.CONNECTION_NONE; | 181 mCurrentConnectionType != ConnectionType.CONNECTION_NONE; |
| 179 if (connectionCurrentlyExists != forceOnline) { | 182 if (connectionCurrentlyExists != forceOnline) { |
| 180 updateCurrentMaxBandwidth(forceOnline ? Double.POSITIVE_INFINITY : 0 .0); | |
| 181 updateCurrentConnectionType(forceOnline ? ConnectionType.CONNECTION_ UNKNOWN | 183 updateCurrentConnectionType(forceOnline ? ConnectionType.CONNECTION_ UNKNOWN |
| 182 : ConnectionType.CONNECTION_NONE); | 184 : ConnectionType.CONNECTION_NONE); |
| 185 updateCurrentMaxBandwidth(forceOnline ? Double.POSITIVE_INFINITY : 0 .0); | |
| 183 } | 186 } |
| 184 } | 187 } |
| 185 | 188 |
| 186 private void updateCurrentConnectionType(int newConnectionType) { | 189 private void updateCurrentConnectionType(int newConnectionType) { |
| 187 mCurrentConnectionType = newConnectionType; | 190 mCurrentConnectionType = newConnectionType; |
| 188 notifyObserversOfConnectionTypeChange(newConnectionType); | 191 notifyObserversOfConnectionTypeChange(newConnectionType); |
| 189 } | 192 } |
| 190 | 193 |
| 191 private void updateCurrentMaxBandwidth(double maxBandwidth) { | 194 private void updateCurrentMaxBandwidth(double maxBandwidthMbps) { |
| 192 mCurrentMaxBandwidth = maxBandwidth; | 195 if (maxBandwidthMbps == mCurrentMaxBandwidth) return; |
| 196 mCurrentMaxBandwidth = maxBandwidthMbps; | |
| 197 notifyObserversOfMaxBandwidthChange(maxBandwidthMbps); | |
| 193 } | 198 } |
| 194 | 199 |
| 195 /** | 200 /** |
| 196 * Alerts all observers of a connection change. | 201 * Alerts all observers of a connection change. |
| 197 */ | 202 */ |
| 198 void notifyObserversOfConnectionTypeChange(int newConnectionType) { | 203 void notifyObserversOfConnectionTypeChange(int newConnectionType) { |
| 199 for (Long nativeChangeNotifier : mNativeChangeNotifiers) { | 204 for (Long nativeChangeNotifier : mNativeChangeNotifiers) { |
| 200 nativeNotifyConnectionTypeChanged(nativeChangeNotifier, newConnectio nType); | 205 nativeNotifyConnectionTypeChanged(nativeChangeNotifier, newConnectio nType); |
| 201 } | 206 } |
| 202 for (ConnectionTypeObserver observer : mConnectionTypeObservers) { | 207 for (ConnectionTypeObserver observer : mConnectionTypeObservers) { |
| 203 observer.onConnectionTypeChanged(newConnectionType); | 208 observer.onConnectionTypeChanged(newConnectionType); |
| 204 } | 209 } |
| 205 } | 210 } |
| 206 | 211 |
| 207 /** | 212 /** |
| 213 * Alerts all observers of a bandwidth change. | |
| 214 */ | |
| 215 void notifyObserversOfMaxBandwidthChange(double maxBandwidthMbps) { | |
| 216 for (Long nativeChangeNotifier : mNativeChangeNotifiers) { | |
| 217 nativeNotifyMaxBandwidthChanged(nativeChangeNotifier, maxBandwidthMb ps); | |
| 218 } | |
| 219 } | |
| 220 | |
| 221 | |
| 222 /** | |
| 208 * Adds an observer for any connection type changes. | 223 * Adds an observer for any connection type changes. |
| 209 */ | 224 */ |
| 210 public static void addConnectionTypeObserver(ConnectionTypeObserver observer ) { | 225 public static void addConnectionTypeObserver(ConnectionTypeObserver observer ) { |
| 211 getInstance().addConnectionTypeObserverInternal(observer); | 226 getInstance().addConnectionTypeObserverInternal(observer); |
| 212 } | 227 } |
| 213 | 228 |
| 214 private void addConnectionTypeObserverInternal(ConnectionTypeObserver observ er) { | 229 private void addConnectionTypeObserverInternal(ConnectionTypeObserver observ er) { |
| 215 mConnectionTypeObservers.addObserver(observer); | 230 mConnectionTypeObservers.addObserver(observer); |
| 216 } | 231 } |
| 217 | 232 |
| 218 /** | 233 /** |
| 219 * Removes an observer for any connection type changes. | 234 * Removes an observer for any connection type changes. |
| 220 */ | 235 */ |
| 221 public static void removeConnectionTypeObserver(ConnectionTypeObserver obser ver) { | 236 public static void removeConnectionTypeObserver(ConnectionTypeObserver obser ver) { |
| 222 getInstance().removeConnectionTypeObserverInternal(observer); | 237 getInstance().removeConnectionTypeObserverInternal(observer); |
| 223 } | 238 } |
| 224 | 239 |
| 225 private void removeConnectionTypeObserverInternal(ConnectionTypeObserver obs erver) { | 240 private void removeConnectionTypeObserverInternal(ConnectionTypeObserver obs erver) { |
| 226 mConnectionTypeObservers.removeObserver(observer); | 241 mConnectionTypeObservers.removeObserver(observer); |
| 227 } | 242 } |
| 228 | 243 |
| 229 @NativeClassQualifiedName("NetworkChangeNotifierDelegateAndroid") | 244 @NativeClassQualifiedName("NetworkChangeNotifierDelegateAndroid") |
| 230 private native void nativeNotifyConnectionTypeChanged(long nativePtr, int ne wConnectionType); | 245 private native void nativeNotifyConnectionTypeChanged(long nativePtr, int ne wConnectionType); |
| 231 | 246 |
| 247 @NativeClassQualifiedName("NetworkChangeNotifierDelegateAndroid") | |
| 248 private native void nativeNotifyMaxBandwidthChanged(long nativePtr, double m axBandwidthMbps); | |
| 249 | |
| 232 private static native double nativeGetMaxBandwidthForConnectionSubtype(int s ubtype); | 250 private static native double nativeGetMaxBandwidthForConnectionSubtype(int s ubtype); |
| 233 | 251 |
| 234 // For testing only. | 252 // For testing only. |
| 235 public static NetworkChangeNotifierAutoDetect getAutoDetectorForTest() { | 253 public static NetworkChangeNotifierAutoDetect getAutoDetectorForTest() { |
| 236 return getInstance().mAutoDetector; | 254 return getInstance().mAutoDetector; |
| 237 } | 255 } |
| 238 | 256 |
| 239 /** | 257 /** |
| 240 * Checks if there currently is connectivity. | 258 * Checks if there currently is connectivity. |
| 241 */ | 259 */ |
| 242 public static boolean isOnline() { | 260 public static boolean isOnline() { |
| 243 int connectionType = getInstance().getCurrentConnectionType(); | 261 int connectionType = getInstance().getCurrentConnectionType(); |
| 244 return connectionType != ConnectionType.CONNECTION_UNKNOWN | 262 return connectionType != ConnectionType.CONNECTION_UNKNOWN |
| 245 && connectionType != ConnectionType.CONNECTION_NONE; | 263 && connectionType != ConnectionType.CONNECTION_NONE; |
| 246 } | 264 } |
| 247 } | 265 } |
| OLD | NEW |