Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: net/android/java/src/org/chromium/net/NetworkChangeNotifier.java

Issue 1358163004: [Android] Introduce RegistrationPolicy for NetworkChangeNotifier. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: clean-up and rebase Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 * Returns the singleton instance. 104 * Returns the singleton instance.
105 */ 105 */
106 public static NetworkChangeNotifier getInstance() { 106 public static NetworkChangeNotifier getInstance() {
107 assert sInstance != null; 107 assert sInstance != null;
108 return sInstance; 108 return sInstance;
109 } 109 }
110 110
111 /** 111 /**
112 * Enables auto detection of the current network state based on notification s from the system. 112 * Enables auto detection of the current network state based on notification s from the system.
113 * Note that passing true here requires the embedding app have the platform ACCESS_NETWORK_STATE 113 * Note that passing true here requires the embedding app have the platform ACCESS_NETWORK_STATE
114 * permission. 114 * permission. Also note that in this case the auto detection is enabled bas ed on the status of
115 * the application (@see ApplicationStatus).
115 * 116 *
116 * @param shouldAutoDetect true if the NetworkChangeNotifier should listen f or system changes in 117 * @param shouldAutoDetect true if the NetworkChangeNotifier should listen f or system changes in
117 * network connectivity. 118 * network connectivity.
118 */ 119 */
119 public static void setAutoDetectConnectivityState(boolean shouldAutoDetect) { 120 public static void setAutoDetectConnectivityState(boolean shouldAutoDetect) {
120 getInstance().setAutoDetectConnectivityStateInternal(shouldAutoDetect, f alse); 121 getInstance().setAutoDetectConnectivityStateInternal(
121 } 122 shouldAutoDetect, new RegistrationPolicyApplicationStatus());
122
123 private void destroyAutoDetector() {
124 if (mAutoDetector != null) {
125 mAutoDetector.destroy();
126 mAutoDetector = null;
127 }
128 } 123 }
129 124
130 /** 125 /**
131 * Registers to always receive network change notifications no matter if 126 * Registers to always receive network change notifications no matter if
132 * the app is in the background or foreground. 127 * the app is in the background or foreground.
133 * Note that in normal circumstances, chrome embedders should use 128 * Note that in normal circumstances, chrome embedders should use
134 * {@code setAutoDetectConnectivityState} to listen to network changes only 129 * {@code setAutoDetectConnectivityState} to listen to network changes only
135 * when the app is in the foreground, because network change observers 130 * when the app is in the foreground, because network change observers
136 * might perform expensive work depending on the network connectivity. 131 * might perform expensive work depending on the network connectivity.
137 */ 132 */
138 public static void registerToReceiveNotificationsAlways() { 133 public static void registerToReceiveNotificationsAlways() {
139 getInstance().setAutoDetectConnectivityStateInternal(true, true); 134 getInstance().setAutoDetectConnectivityStateInternal(
135 true, new RegistrationPolicyAlwaysRegister());
136 }
137
138 /**
139 * Registers to receive network change notification based on the provided re gistration policy.
140 */
141 public static void setAutoDetectConnectivityState(RegistrationPolicy policy) {
142 getInstance().setAutoDetectConnectivityStateInternal(true, policy);
143 }
144
145 private void destroyAutoDetector() {
146 if (mAutoDetector != null) {
147 mAutoDetector.destroy();
148 mAutoDetector = null;
149 }
140 } 150 }
141 151
142 private void setAutoDetectConnectivityStateInternal( 152 private void setAutoDetectConnectivityStateInternal(
143 boolean shouldAutoDetect, boolean alwaysWatchForChanges) { 153 boolean shouldAutoDetect, RegistrationPolicy policy) {
pauljensen 2015/10/01 12:06:36 It looks like you refactored the body of this func
timvolodine 2015/10/05 17:26:45 sure no problem, this is not essential anyway. don
144 if (shouldAutoDetect) { 154 if (!shouldAutoDetect) {
145 if (mAutoDetector == null) {
146 mAutoDetector = new NetworkChangeNotifierAutoDetect(
147 new NetworkChangeNotifierAutoDetect.Observer() {
148 @Override
149 public void onConnectionTypeChanged(int newConnectionTyp e) {
150 updateCurrentConnectionType(newConnectionType);
151 }
152 @Override
153 public void onMaxBandwidthChanged(double maxBandwidthMbp s) {
154 updateCurrentMaxBandwidth(maxBandwidthMbps);
155 }
156 },
157 mContext,
158 alwaysWatchForChanges);
159 final NetworkChangeNotifierAutoDetect.NetworkState networkState =
160 mAutoDetector.getCurrentNetworkState();
161 updateCurrentConnectionType(mAutoDetector.getCurrentConnectionTy pe(networkState));
162 updateCurrentMaxBandwidth(mAutoDetector.getCurrentMaxBandwidthIn Mbps(networkState));
163 }
164 } else {
165 destroyAutoDetector(); 155 destroyAutoDetector();
156 return;
166 } 157 }
158 if (mAutoDetector != null) return;
159
160 mAutoDetector =
161 new NetworkChangeNotifierAutoDetect(new NetworkChangeNotifierAut oDetect.Observer() {
162 @Override
163 public void onConnectionTypeChanged(int newConnectionType) {
164 updateCurrentConnectionType(newConnectionType);
165 }
166 @Override
167 public void onMaxBandwidthChanged(double maxBandwidthMbps) {
168 updateCurrentMaxBandwidth(maxBandwidthMbps);
169 }
170 }, mContext, policy);
171 final NetworkChangeNotifierAutoDetect.NetworkState networkState =
172 mAutoDetector.getCurrentNetworkState();
173 updateCurrentConnectionType(mAutoDetector.getCurrentConnectionType(netwo rkState));
174 updateCurrentMaxBandwidth(mAutoDetector.getCurrentMaxBandwidthInMbps(net workState));
167 } 175 }
168 176
169 /** 177 /**
170 * Updates the perceived network state when not auto-detecting changes to co nnectivity. 178 * Updates the perceived network state when not auto-detecting changes to co nnectivity.
171 * 179 *
172 * @param networkAvailable True if the NetworkChangeNotifier should perceive a "connected" 180 * @param networkAvailable True if the NetworkChangeNotifier should perceive a "connected"
173 * state, false implies "disconnected". 181 * state, false implies "disconnected".
174 */ 182 */
175 @CalledByNative 183 @CalledByNative
176 public static void forceConnectivityState(boolean networkAvailable) { 184 public static void forceConnectivityState(boolean networkAvailable) {
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 265
258 /** 266 /**
259 * Checks if there currently is connectivity. 267 * Checks if there currently is connectivity.
260 */ 268 */
261 public static boolean isOnline() { 269 public static boolean isOnline() {
262 int connectionType = getInstance().getCurrentConnectionType(); 270 int connectionType = getInstance().getCurrentConnectionType();
263 return connectionType != ConnectionType.CONNECTION_UNKNOWN 271 return connectionType != ConnectionType.CONNECTION_UNKNOWN
264 && connectionType != ConnectionType.CONNECTION_NONE; 272 && connectionType != ConnectionType.CONNECTION_NONE;
265 } 273 }
266 } 274 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698