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; |
(...skipping 24 matching lines...) Expand all Loading... |
35 private NetworkChangeNotifierAutoDetect mAutoDetector; | 35 private NetworkChangeNotifierAutoDetect mAutoDetector; |
36 | 36 |
37 private static NetworkChangeNotifier sInstance; | 37 private static NetworkChangeNotifier sInstance; |
38 | 38 |
39 // Private constructor - instances are only created via the create factory | 39 // Private constructor - instances are only created via the create factory |
40 // function, which is only called from native. | 40 // function, which is only called from native. |
41 private NetworkChangeNotifier(Context context, int nativeChangeNotifier) { | 41 private NetworkChangeNotifier(Context context, int nativeChangeNotifier) { |
42 mContext = context; | 42 mContext = context; |
43 mNativeChangeNotifier = nativeChangeNotifier; | 43 mNativeChangeNotifier = nativeChangeNotifier; |
44 mConnectionType = CONNECTION_UNKNOWN; | 44 mConnectionType = CONNECTION_UNKNOWN; |
45 sInstance = this; | 45 } |
| 46 |
| 47 private void destroy() { |
| 48 if (mAutoDetector != null) { |
| 49 mAutoDetector.destroy(); |
| 50 } |
| 51 mNativeChangeNotifier = 0; |
46 } | 52 } |
47 | 53 |
48 /** | 54 /** |
| 55 * Creates the singleton used by the native-side NetworkChangeNotifier. |
| 56 */ |
| 57 @CalledByNative |
| 58 static NetworkChangeNotifier createInstance(Context context, int nativeChang
eNotifier) { |
| 59 assert sInstance == null; |
| 60 sInstance = new NetworkChangeNotifier(context, nativeChangeNotifier); |
| 61 return sInstance; |
| 62 } |
| 63 |
| 64 /** |
| 65 * Destroys the singleton used by the native-side NetworkChangeNotifier. |
| 66 */ |
| 67 @CalledByNative |
| 68 private static void destroyInstance() { |
| 69 assert sInstance != null; |
| 70 sInstance.destroy(); |
| 71 sInstance = null; |
| 72 } |
| 73 |
| 74 /** |
| 75 * Returns the instance used by the native-side NetworkChangeNotifier. |
| 76 */ |
| 77 public static NetworkChangeNotifier getInstance() { |
| 78 assert sInstance != null; |
| 79 return sInstance; |
| 80 } |
| 81 |
| 82 /** |
49 * Enable auto detection of the current network state based on notifications | 83 * Enable auto detection of the current network state based on notifications |
50 * from the system. Note that passing true here requires the embedding app | 84 * from the system. Note that passing true here requires the embedding app |
51 * have the platform ACCESS_NETWORK_STATE permission. | 85 * have the platform ACCESS_NETWORK_STATE permission. |
52 * | 86 * |
53 * @param shouldAutoDetect true if the NetworkChangeNotifier should listen | 87 * @param shouldAutoDetect true if the NetworkChangeNotifier should listen |
54 * for system changes in network connectivity. | 88 * for system changes in network connectivity. |
55 */ | 89 */ |
56 public static void setAutoDetectConnectivityState(boolean shouldAutoDetect)
{ | 90 public static void setAutoDetectConnectivityState(boolean shouldAutoDetect)
{ |
57 // We should only get a call to this after the native object is created
and | 91 // We should only get a call to this after the native object is created
and |
58 // hence the singleton initialised. | 92 // hence the singleton initialised. |
(...skipping 14 matching lines...) Expand all Loading... |
73 } | 107 } |
74 } | 108 } |
75 | 109 |
76 /** | 110 /** |
77 * Update the perceived network state when not auto-detecting | 111 * Update the perceived network state when not auto-detecting |
78 * changes to connectivity. | 112 * changes to connectivity. |
79 * | 113 * |
80 * @param networkAvailable True if the NetworkChangeNotifier should | 114 * @param networkAvailable True if the NetworkChangeNotifier should |
81 * perceive a "connected" state, false implies "disconnected". | 115 * perceive a "connected" state, false implies "disconnected". |
82 */ | 116 */ |
| 117 @CalledByNative |
83 public static void forceConnectivityState(boolean networkAvailable) { | 118 public static void forceConnectivityState(boolean networkAvailable) { |
84 assert sInstance != null; | 119 assert sInstance != null; |
85 setAutoDetectConnectivityState(false); | 120 setAutoDetectConnectivityState(false); |
86 sInstance.forceConnectivityStateInternal(networkAvailable); | 121 sInstance.forceConnectivityStateInternal(networkAvailable); |
87 } | 122 } |
88 | 123 |
89 private void forceConnectivityStateInternal(boolean forceOnline) { | 124 private void forceConnectivityStateInternal(boolean forceOnline) { |
90 boolean connectionCurrentlyExists = mConnectionType != CONNECTION_NONE; | 125 boolean connectionCurrentlyExists = mConnectionType != CONNECTION_NONE; |
91 if (connectionCurrentlyExists != forceOnline) { | 126 if (connectionCurrentlyExists != forceOnline) { |
92 mConnectionType = forceOnline ? CONNECTION_UNKNOWN : CONNECTION_NONE
; | 127 mConnectionType = forceOnline ? CONNECTION_UNKNOWN : CONNECTION_NONE
; |
93 notifyNativeObservers(); | 128 notifyObserversOfConnectionTypeChange(); |
94 } | 129 } |
95 } | 130 } |
96 | 131 |
97 void notifyNativeObservers() { | 132 void notifyObserversOfConnectionTypeChange() { |
98 if (mNativeChangeNotifier != 0) { | 133 if (mNativeChangeNotifier != 0) { |
99 nativeNotifyObservers(mNativeChangeNotifier); | 134 nativeNotifyObserversOfConnectionTypeChange(mNativeChangeNotifier); |
100 } | 135 } |
101 } | 136 } |
102 | 137 |
103 @CalledByNative | 138 @CalledByNative |
104 private int connectionType() { | 139 private int connectionType() { |
105 if (mAutoDetector != null) { | 140 if (mAutoDetector != null) { |
106 return mAutoDetector.connectionType(); | 141 return mAutoDetector.connectionType(); |
107 } | 142 } |
108 return mConnectionType; | 143 return mConnectionType; |
109 } | 144 } |
110 | 145 |
111 @CalledByNative | 146 @NativeClassQualifiedName("NetworkChangeNotifierAndroid") |
112 private void destroy() { | 147 private native void nativeNotifyObserversOfConnectionTypeChange(int nativePt
r); |
113 if (mAutoDetector != null) { | |
114 mAutoDetector.destroy(); | |
115 } | |
116 mNativeChangeNotifier = 0; | |
117 sInstance = null; | |
118 } | |
119 | |
120 @CalledByNative | |
121 private static NetworkChangeNotifier create(Context context, int nativeNetwo
rkChangeNotifier) { | |
122 return new NetworkChangeNotifier(context, nativeNetworkChangeNotifier); | |
123 } | |
124 | |
125 @NativeClassQualifiedName("android::NetworkChangeNotifier") | |
126 private native void nativeNotifyObservers(int nativePtr); | |
127 | 148 |
128 // For testing only. | 149 // For testing only. |
129 public static NetworkChangeNotifierAutoDetect getAutoDetectorForTest() { | 150 public static NetworkChangeNotifierAutoDetect getAutoDetectorForTest() { |
| 151 assert sInstance != null; |
130 return sInstance.mAutoDetector; | 152 return sInstance.mAutoDetector; |
131 } | 153 } |
132 } | 154 } |
OLD | NEW |