Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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; |
| 11 import org.chromium.base.NativeClassQualifiedName; | 11 import org.chromium.base.NativeClassQualifiedName; |
| 12 | 12 |
| 13 import java.util.ArrayList; | |
| 14 | |
| 13 /** | 15 /** |
| 14 * Triggers updates to the underlying network state in native Chrome. | 16 * Triggers updates to the underlying network state in Chrome. |
| 15 * By default, connectivity is assumed and changes must pushed from | 17 * By default, connectivity is assumed and changes must pushed from |
| 16 * the embedder via the forceConnectivityState function. | 18 * the embedder via the forceConnectivityState function. |
| 17 * Embedders may choose to have this class auto-detect changes in | 19 * Embedders may choose to have this class auto-detect changes in |
| 18 * network connectivity by invoking the autoDetectConnectivityState | 20 * network connectivity by invoking the autoDetectConnectivityState |
| 19 * function. | 21 * function. |
|
nilesh
2012/09/22 00:40:24
WOrth adding that this class is not thread safe.
gone
2012/09/24 18:28:24
Done.
| |
| 20 */ | 22 */ |
| 21 @JNINamespace("net") | 23 @JNINamespace("net") |
| 22 public class NetworkChangeNotifier { | 24 public class NetworkChangeNotifier { |
| 25 /** Alerted when the connection type of the network changes. */ | |
|
nilesh
2012/09/22 00:40:24
Please add a comment.
onConnectionTypeChanged will
gone
2012/09/24 18:28:24
Done.
| |
| 26 public interface ConnectionTypeObserver { | |
| 27 public void onConnectionTypeChanged(int connectionType); | |
| 28 } | |
| 29 | |
| 23 // These constants must always match the ones in network_change_notifier.h. | 30 // These constants must always match the ones in network_change_notifier.h. |
| 24 public static final int CONNECTION_UNKNOWN = 0; | 31 public static final int CONNECTION_UNKNOWN = 0; |
| 25 public static final int CONNECTION_ETHERNET = 1; | 32 public static final int CONNECTION_ETHERNET = 1; |
| 26 public static final int CONNECTION_WIFI = 2; | 33 public static final int CONNECTION_WIFI = 2; |
| 27 public static final int CONNECTION_2G = 3; | 34 public static final int CONNECTION_2G = 3; |
| 28 public static final int CONNECTION_3G = 4; | 35 public static final int CONNECTION_3G = 4; |
| 29 public static final int CONNECTION_4G = 5; | 36 public static final int CONNECTION_4G = 5; |
| 30 public static final int CONNECTION_NONE = 6; | 37 public static final int CONNECTION_NONE = 6; |
| 31 | 38 |
| 32 private final Context mContext; | 39 private final Context mContext; |
| 33 private int mNativeChangeNotifier; | 40 private int mNativeChangeNotifier; |
| 41 private ArrayList<ConnectionTypeObserver> mConnectionTypeObservers; | |
| 34 private NetworkChangeNotifierAutoDetect mAutoDetector; | 42 private NetworkChangeNotifierAutoDetect mAutoDetector; |
| 35 | 43 |
| 36 private static NetworkChangeNotifier sInstance; | 44 private static NetworkChangeNotifier sInstance; |
| 37 | 45 |
| 38 // Private constructor - instances are only created via the create factory | |
| 39 // function, which is only called from native. | |
| 40 private NetworkChangeNotifier(Context context, int nativeChangeNotifier) { | 46 private NetworkChangeNotifier(Context context, int nativeChangeNotifier) { |
| 41 mContext = context; | 47 mContext = context; |
| 42 mNativeChangeNotifier = nativeChangeNotifier; | 48 mNativeChangeNotifier = nativeChangeNotifier; |
| 49 mConnectionTypeObservers = new ArrayList<ConnectionTypeObserver>(); | |
| 43 } | 50 } |
| 44 | 51 |
| 45 private void destroy() { | 52 private void destroy() { |
| 46 if (mAutoDetector != null) { | 53 if (mAutoDetector != null) { |
| 47 mAutoDetector.destroy(); | 54 mAutoDetector.destroy(); |
| 48 } | 55 } |
| 49 mNativeChangeNotifier = 0; | 56 mNativeChangeNotifier = 0; |
| 57 mConnectionTypeObservers.clear(); | |
| 50 } | 58 } |
| 51 | 59 |
| 52 /** | 60 /** |
| 53 * Creates the singleton used by the native-side NetworkChangeNotifier. | 61 * Creates the singleton used by the native-side NetworkChangeNotifier. |
| 54 */ | 62 */ |
| 55 @CalledByNative | 63 @CalledByNative |
| 56 static NetworkChangeNotifier createInstance(Context context, int nativeChang eNotifier) { | 64 static NetworkChangeNotifier createInstance(Context context, int nativeChang eNotifier) { |
| 57 assert sInstance == null; | 65 assert sInstance == null; |
| 58 sInstance = new NetworkChangeNotifier(context, nativeChangeNotifier); | 66 sInstance = new NetworkChangeNotifier(context, nativeChangeNotifier); |
| 59 return sInstance; | 67 return sInstance; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 return; | 143 return; |
| 136 } | 144 } |
| 137 boolean connectionCurrentlyExists = | 145 boolean connectionCurrentlyExists = |
| 138 nativeGetConnectionType(mNativeChangeNotifier) != CONNECTION_NONE; | 146 nativeGetConnectionType(mNativeChangeNotifier) != CONNECTION_NONE; |
| 139 if (connectionCurrentlyExists != forceOnline) { | 147 if (connectionCurrentlyExists != forceOnline) { |
| 140 notifyObserversOfConnectionTypeChange( | 148 notifyObserversOfConnectionTypeChange( |
| 141 forceOnline ? CONNECTION_UNKNOWN : CONNECTION_NONE); | 149 forceOnline ? CONNECTION_UNKNOWN : CONNECTION_NONE); |
| 142 } | 150 } |
| 143 } | 151 } |
| 144 | 152 |
| 153 /** | |
| 154 * Alerts all observers of a connection change. | |
| 155 */ | |
| 145 void notifyObserversOfConnectionTypeChange(int newConnectionType) { | 156 void notifyObserversOfConnectionTypeChange(int newConnectionType) { |
| 146 if (mNativeChangeNotifier != 0) { | 157 if (mNativeChangeNotifier != 0) { |
| 147 nativeNotifyObserversOfConnectionTypeChange(mNativeChangeNotifier, n ewConnectionType); | 158 nativeNotifyObserversOfConnectionTypeChange(mNativeChangeNotifier, n ewConnectionType); |
| 148 } | 159 } |
| 160 | |
| 161 for (ConnectionTypeObserver observer : mConnectionTypeObservers) { | |
| 162 observer.onConnectionTypeChanged(newConnectionType); | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 /** | |
| 167 * Adds an observer for any connection type changes. | |
| 168 */ | |
| 169 public static void addConnectionTypeObserver(ConnectionTypeObserver observer ) { | |
|
nilesh
2012/09/22 00:40:24
No removeConnectionTypeObserver ?
I think this is
gone
2012/09/24 18:28:24
Done.
| |
| 170 assert sInstance != null; | |
| 171 sInstance.addConnectionTypeObserverInternal(observer); | |
| 172 } | |
| 173 | |
| 174 private void addConnectionTypeObserverInternal(ConnectionTypeObserver observ er) { | |
| 175 mConnectionTypeObservers.add(observer); | |
| 149 } | 176 } |
| 150 | 177 |
| 151 @NativeClassQualifiedName("NetworkChangeNotifierAndroid") | 178 @NativeClassQualifiedName("NetworkChangeNotifierAndroid") |
| 152 private native void nativeNotifyObserversOfConnectionTypeChange( | 179 private native void nativeNotifyObserversOfConnectionTypeChange( |
| 153 int nativePtr, int newConnectionType); | 180 int nativePtr, int newConnectionType); |
| 154 | 181 |
| 155 @NativeClassQualifiedName("NetworkChangeNotifierAndroid") | 182 @NativeClassQualifiedName("NetworkChangeNotifierAndroid") |
| 156 private native int nativeGetConnectionType(int nativePtr); | 183 private native int nativeGetConnectionType(int nativePtr); |
| 157 | 184 |
| 158 // For testing only. | 185 // For testing only. |
| 159 public static NetworkChangeNotifierAutoDetect getAutoDetectorForTest() { | 186 public static NetworkChangeNotifierAutoDetect getAutoDetectorForTest() { |
| 160 assert sInstance != null; | 187 assert sInstance != null; |
| 161 return sInstance.mAutoDetector; | 188 return sInstance.mAutoDetector; |
| 162 } | 189 } |
| 163 } | 190 } |
| OLD | NEW |