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.ObserverList; | 9 import org.chromium.base.ObserverList; |
10 import org.chromium.base.annotations.CalledByNative; | 10 import org.chromium.base.annotations.CalledByNative; |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 * Returns the singleton instance. | 127 * Returns the singleton instance. |
128 */ | 128 */ |
129 public static NetworkChangeNotifier getInstance() { | 129 public static NetworkChangeNotifier getInstance() { |
130 assert sInstance != null; | 130 assert sInstance != null; |
131 return sInstance; | 131 return sInstance; |
132 } | 132 } |
133 | 133 |
134 /** | 134 /** |
135 * Enables auto detection of the current network state based on notification
s from the system. | 135 * Enables auto detection of the current network state based on notification
s from the system. |
136 * Note that passing true here requires the embedding app have the platform
ACCESS_NETWORK_STATE | 136 * Note that passing true here requires the embedding app have the platform
ACCESS_NETWORK_STATE |
137 * permission. | 137 * permission. Also note that in this case the auto detection is enabled bas
ed on the status of |
| 138 * the application (@see ApplicationStatus). |
138 * | 139 * |
139 * @param shouldAutoDetect true if the NetworkChangeNotifier should listen f
or system changes in | 140 * @param shouldAutoDetect true if the NetworkChangeNotifier should listen f
or system changes in |
140 * network connectivity. | 141 * network connectivity. |
141 */ | 142 */ |
142 public static void setAutoDetectConnectivityState(boolean shouldAutoDetect)
{ | 143 public static void setAutoDetectConnectivityState(boolean shouldAutoDetect)
{ |
143 getInstance().setAutoDetectConnectivityStateInternal(shouldAutoDetect, f
alse); | 144 getInstance().setAutoDetectConnectivityStateInternal( |
144 } | 145 shouldAutoDetect, new RegistrationPolicyApplicationStatus()); |
145 | |
146 private void destroyAutoDetector() { | |
147 if (mAutoDetector != null) { | |
148 mAutoDetector.destroy(); | |
149 mAutoDetector = null; | |
150 } | |
151 } | 146 } |
152 | 147 |
153 /** | 148 /** |
154 * Registers to always receive network change notifications no matter if | 149 * Registers to always receive network change notifications no matter if |
155 * the app is in the background or foreground. | 150 * the app is in the background or foreground. |
156 * Note that in normal circumstances, chrome embedders should use | 151 * Note that in normal circumstances, chrome embedders should use |
157 * {@code setAutoDetectConnectivityState} to listen to network changes only | 152 * {@code setAutoDetectConnectivityState} to listen to network changes only |
158 * when the app is in the foreground, because network change observers | 153 * when the app is in the foreground, because network change observers |
159 * might perform expensive work depending on the network connectivity. | 154 * might perform expensive work depending on the network connectivity. |
160 */ | 155 */ |
161 public static void registerToReceiveNotificationsAlways() { | 156 public static void registerToReceiveNotificationsAlways() { |
162 getInstance().setAutoDetectConnectivityStateInternal(true, true); | 157 getInstance().setAutoDetectConnectivityStateInternal( |
| 158 true, new RegistrationPolicyAlwaysRegister()); |
| 159 } |
| 160 |
| 161 /** |
| 162 * Registers to receive network change notification based on the provided re
gistration policy. |
| 163 */ |
| 164 public static void setAutoDetectConnectivityState( |
| 165 NetworkChangeNotifierAutoDetect.RegistrationPolicy policy) { |
| 166 getInstance().setAutoDetectConnectivityStateInternal(true, policy); |
| 167 } |
| 168 |
| 169 private void destroyAutoDetector() { |
| 170 if (mAutoDetector != null) { |
| 171 mAutoDetector.destroy(); |
| 172 mAutoDetector = null; |
| 173 } |
163 } | 174 } |
164 | 175 |
165 private void setAutoDetectConnectivityStateInternal( | 176 private void setAutoDetectConnectivityStateInternal( |
166 boolean shouldAutoDetect, boolean alwaysWatchForChanges) { | 177 boolean shouldAutoDetect, NetworkChangeNotifierAutoDetect.Registrati
onPolicy policy) { |
167 if (shouldAutoDetect) { | 178 if (shouldAutoDetect) { |
168 if (mAutoDetector == null) { | 179 if (mAutoDetector == null) { |
169 mAutoDetector = new NetworkChangeNotifierAutoDetect( | 180 mAutoDetector = new NetworkChangeNotifierAutoDetect( |
170 new NetworkChangeNotifierAutoDetect.Observer() { | 181 new NetworkChangeNotifierAutoDetect.Observer() { |
171 @Override | 182 @Override |
172 public void onConnectionTypeChanged(int newConnectionTyp
e) { | 183 public void onConnectionTypeChanged(int newConnectio
nType) { |
173 updateCurrentConnectionType(newConnectionType); | 184 updateCurrentConnectionType(newConnectionType); |
174 } | 185 } |
175 @Override | 186 @Override |
176 public void onMaxBandwidthChanged(double maxBandwidthMbp
s) { | 187 public void onMaxBandwidthChanged(double maxBandwidt
hMbps) { |
177 updateCurrentMaxBandwidth(maxBandwidthMbps); | 188 updateCurrentMaxBandwidth(maxBandwidthMbps); |
178 } | 189 } |
179 @Override | 190 @Override |
180 public void onNetworkConnect(int netId, int connectionTy
pe) { | 191 public void onNetworkConnect(int netId, int connecti
onType) { |
181 notifyObserversOfNetworkConnect(netId, connectionTyp
e); | 192 notifyObserversOfNetworkConnect(netId, connectio
nType); |
182 } | 193 } |
183 @Override | 194 @Override |
184 public void onNetworkSoonToDisconnect(int netId) { | 195 public void onNetworkSoonToDisconnect(int netId) { |
185 notifyObserversOfNetworkSoonToDisconnect(netId); | 196 notifyObserversOfNetworkSoonToDisconnect(netId); |
186 } | 197 } |
187 @Override | 198 @Override |
188 public void onNetworkDisconnect(int netId) { | 199 public void onNetworkDisconnect(int netId) { |
189 notifyObserversOfNetworkDisconnect(netId); | 200 notifyObserversOfNetworkDisconnect(netId); |
190 } | 201 } |
191 @Override | 202 @Override |
192 public void updateActiveNetworkList(int[] activeNetIds)
{ | 203 public void updateActiveNetworkList(int[] activeNetI
ds) { |
193 notifyObserversToUpdateActiveNetworkList(activeNetId
s); | 204 notifyObserversToUpdateActiveNetworkList(activeN
etIds); |
194 } | 205 } |
195 }, | 206 }, |
196 mContext, | 207 mContext, policy); |
197 alwaysWatchForChanges); | |
198 final NetworkChangeNotifierAutoDetect.NetworkState networkState
= | 208 final NetworkChangeNotifierAutoDetect.NetworkState networkState
= |
199 mAutoDetector.getCurrentNetworkState(); | 209 mAutoDetector.getCurrentNetworkState(); |
200 updateCurrentConnectionType(mAutoDetector.getCurrentConnectionTy
pe(networkState)); | 210 updateCurrentConnectionType(mAutoDetector.getCurrentConnectionTy
pe(networkState)); |
201 updateCurrentMaxBandwidth(mAutoDetector.getCurrentMaxBandwidthIn
Mbps(networkState)); | 211 updateCurrentMaxBandwidth(mAutoDetector.getCurrentMaxBandwidthIn
Mbps(networkState)); |
202 } | 212 } |
203 } else { | 213 } else { |
204 destroyAutoDetector(); | 214 destroyAutoDetector(); |
205 } | 215 } |
206 } | 216 } |
207 | 217 |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 | 398 |
389 /** | 399 /** |
390 * Checks if there currently is connectivity. | 400 * Checks if there currently is connectivity. |
391 */ | 401 */ |
392 public static boolean isOnline() { | 402 public static boolean isOnline() { |
393 int connectionType = getInstance().getCurrentConnectionType(); | 403 int connectionType = getInstance().getCurrentConnectionType(); |
394 return connectionType != ConnectionType.CONNECTION_UNKNOWN | 404 return connectionType != ConnectionType.CONNECTION_UNKNOWN |
395 && connectionType != ConnectionType.CONNECTION_NONE; | 405 && connectionType != ConnectionType.CONNECTION_NONE; |
396 } | 406 } |
397 } | 407 } |
OLD | NEW |