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

Side by Side Diff: webrtc/sdk/android/api/org/webrtc/NetworkMonitor.java

Issue 2990693002: Fix issues with NetworkMonitor singleton when used by multiple clients. (Closed)
Patch Set: Created 3 years, 5 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
« no previous file with comments | « no previous file | webrtc/sdk/android/instrumentationtests/src/org/webrtc/NetworkMonitorTest.java » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2015 The WebRTC project authors. All Rights Reserved. 2 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 /** 63 /**
64 * Returns the singleton instance. 64 * Returns the singleton instance.
65 */ 65 */
66 public static NetworkMonitor getInstance() { 66 public static NetworkMonitor getInstance() {
67 if (instance == null) { 67 if (instance == null) {
68 instance = new NetworkMonitor(); 68 instance = new NetworkMonitor();
69 } 69 }
70 return instance; 70 return instance;
71 } 71 }
72 72
73 /**
74 * Enables auto detection of the current network state based on notifications from the system.
75 * Note that passing true here requires the embedding app have the platform AC CESS_NETWORK_STATE
76 * permission.
77 *
78 * @param shouldAutoDetect true if the NetworkMonitor should listen for system changes in
79 * network connectivity.
80 */
81 public static void setAutoDetectConnectivityState(boolean shouldAutoDetect) {
82 getInstance().setAutoDetectConnectivityStateInternal(shouldAutoDetect);
83 }
Taylor Brandstetter 2017/07/26 02:39:07 This method is only called by tests, and *should*
84
85 private static void assertIsTrue(boolean condition) { 73 private static void assertIsTrue(boolean condition) {
86 if (!condition) { 74 if (!condition) {
87 throw new AssertionError("Expected to be true"); 75 throw new AssertionError("Expected to be true");
88 } 76 }
89 } 77 }
90 78
91 // Called by the native code. 79 /**
80 * Called by the native code.
81 *
82 * Enables auto detection of the current network state based on notifications
83 * from the system. Note that this requires the embedding app have the
84 * platform ACCESS_NETWORK_STATE permission.
85 */
92 private void startMonitoring(long nativeObserver) { 86 private void startMonitoring(long nativeObserver) {
93 Logging.d(TAG, "Start monitoring from native observer " + nativeObserver); 87 Logging.d(TAG, "Start monitoring from native observer " + nativeObserver);
94 nativeNetworkObservers.add(nativeObserver); 88 nativeNetworkObservers.add(nativeObserver);
95 setAutoDetectConnectivityStateInternal(true); 89 if (autoDetector == null) {
90 createAutoDetector();
91 }
92 // The observers expect a network list update after they call startMonitorin g.
93 final NetworkMonitorAutoDetect.NetworkState networkState =
94 autoDetector.getCurrentNetworkState();
95 updateCurrentConnectionType(NetworkMonitorAutoDetect.getConnectionType(netwo rkState));
96 updateObserverActiveNetworkList(nativeObserver);
96 } 97 }
97 98
98 // Called by the native code. 99 // Called by the native code.
99 private void stopMonitoring(long nativeObserver) { 100 private void stopMonitoring(long nativeObserver) {
100 Logging.d(TAG, "Stop monitoring from native observer " + nativeObserver); 101 Logging.d(TAG, "Stop monitoring from native observer " + nativeObserver);
101 setAutoDetectConnectivityStateInternal(false);
102 nativeNetworkObservers.remove(nativeObserver); 102 nativeNetworkObservers.remove(nativeObserver);
103 if (nativeNetworkObservers.isEmpty()) {
104 autoDetector.destroy();
105 autoDetector = null;
106 }
103 } 107 }
104 108
105 // Called by the native code to determine if network binding is supported 109 // Called by the native code to determine if network binding is supported
106 // on this platform. 110 // on this platform.
107 private boolean networkBindingSupported() { 111 private boolean networkBindingSupported() {
108 return autoDetector != null && autoDetector.supportNetworkCallback(); 112 return autoDetector != null && autoDetector.supportNetworkCallback();
109 } 113 }
110 114
111 // Called by the native code to get the Android SDK version. 115 // Called by the native code to get the Android SDK version.
112 private static int androidSdkInt() { 116 private static int androidSdkInt() {
113 return Build.VERSION.SDK_INT; 117 return Build.VERSION.SDK_INT;
114 } 118 }
115 119
116 private ConnectionType getCurrentConnectionType() { 120 private ConnectionType getCurrentConnectionType() {
117 return currentConnectionType; 121 return currentConnectionType;
118 } 122 }
119 123
120 private long getCurrentDefaultNetId() { 124 private long getCurrentDefaultNetId() {
121 return autoDetector == null ? INVALID_NET_ID : autoDetector.getDefaultNetId( ); 125 return autoDetector == null ? INVALID_NET_ID : autoDetector.getDefaultNetId( );
122 } 126 }
123 127
124 private void destroyAutoDetector() { 128 private void createAutoDetector() {
125 if (autoDetector != null) { 129 autoDetector = new NetworkMonitorAutoDetect(new NetworkMonitorAutoDetect.Obs erver() {
126 autoDetector.destroy();
127 autoDetector = null;
128 }
129 }
130 130
131 private void setAutoDetectConnectivityStateInternal(boolean shouldAutoDetect) { 131 @Override
132 if (!shouldAutoDetect) { 132 public void onConnectionTypeChanged(ConnectionType newConnectionType) {
133 destroyAutoDetector(); 133 updateCurrentConnectionType(newConnectionType);
134 return; 134 }
135 }
136 if (autoDetector == null) {
137 autoDetector = new NetworkMonitorAutoDetect(new NetworkMonitorAutoDetect.O bserver() {
138 135
139 @Override 136 @Override
140 public void onConnectionTypeChanged(ConnectionType newConnectionType) { 137 public void onNetworkConnect(NetworkInformation networkInfo) {
141 updateCurrentConnectionType(newConnectionType); 138 notifyObserversOfNetworkConnect(networkInfo);
142 } 139 }
143 140
144 @Override 141 @Override
145 public void onNetworkConnect(NetworkInformation networkInfo) { 142 public void onNetworkDisconnect(long networkHandle) {
146 notifyObserversOfNetworkConnect(networkInfo); 143 notifyObserversOfNetworkDisconnect(networkHandle);
147 } 144 }
148 145 }, ContextUtils.getApplicationContext());
149 @Override
150 public void onNetworkDisconnect(long networkHandle) {
151 notifyObserversOfNetworkDisconnect(networkHandle);
152 }
153 }, ContextUtils.getApplicationContext());
154 final NetworkMonitorAutoDetect.NetworkState networkState =
155 autoDetector.getCurrentNetworkState();
156 updateCurrentConnectionType(NetworkMonitorAutoDetect.getConnectionType(net workState));
157 updateActiveNetworkList();
158 }
159 } 146 }
160 147
161 private void updateCurrentConnectionType(ConnectionType newConnectionType) { 148 private void updateCurrentConnectionType(ConnectionType newConnectionType) {
162 currentConnectionType = newConnectionType; 149 currentConnectionType = newConnectionType;
163 notifyObserversOfConnectionTypeChange(newConnectionType); 150 notifyObserversOfConnectionTypeChange(newConnectionType);
164 } 151 }
165 152
166 /** 153 /**
167 * Alerts all observers of a connection change. 154 * Alerts all observers of a connection change.
168 */ 155 */
(...skipping 11 matching lines...) Expand all
180 nativeNotifyOfNetworkConnect(nativeObserver, networkInfo); 167 nativeNotifyOfNetworkConnect(nativeObserver, networkInfo);
181 } 168 }
182 } 169 }
183 170
184 private void notifyObserversOfNetworkDisconnect(long networkHandle) { 171 private void notifyObserversOfNetworkDisconnect(long networkHandle) {
185 for (long nativeObserver : nativeNetworkObservers) { 172 for (long nativeObserver : nativeNetworkObservers) {
186 nativeNotifyOfNetworkDisconnect(nativeObserver, networkHandle); 173 nativeNotifyOfNetworkDisconnect(nativeObserver, networkHandle);
187 } 174 }
188 } 175 }
189 176
190 private void updateActiveNetworkList() { 177 private void updateObserverActiveNetworkList(long nativeObserver) {
191 List<NetworkInformation> networkInfoList = autoDetector.getActiveNetworkList (); 178 List<NetworkInformation> networkInfoList = autoDetector.getActiveNetworkList ();
192 if (networkInfoList == null || networkInfoList.size() == 0) { 179 if (networkInfoList == null || networkInfoList.size() == 0) {
193 return; 180 return;
194 } 181 }
195 182
196 NetworkInformation[] networkInfos = new NetworkInformation[networkInfoList.s ize()]; 183 NetworkInformation[] networkInfos = new NetworkInformation[networkInfoList.s ize()];
197 networkInfos = networkInfoList.toArray(networkInfos); 184 networkInfos = networkInfoList.toArray(networkInfos);
198 for (long nativeObserver : nativeNetworkObservers) { 185 nativeNotifyOfActiveNetworkList(nativeObserver, networkInfos);
199 nativeNotifyOfActiveNetworkList(nativeObserver, networkInfos);
200 }
201 } 186 }
202 187
203 /** 188 /**
204 * Adds an observer for any connection type changes. 189 * Adds an observer for any connection type changes.
205 */ 190 */
206 public static void addNetworkObserver(NetworkObserver observer) { 191 public static void addNetworkObserver(NetworkObserver observer) {
207 getInstance().addNetworkObserverInternal(observer); 192 getInstance().addNetworkObserverInternal(observer);
208 } 193 }
209 194
210 private void addNetworkObserverInternal(NetworkObserver observer) { 195 private void addNetworkObserverInternal(NetworkObserver observer) {
(...skipping 24 matching lines...) Expand all
235 private native void nativeNotifyOfNetworkDisconnect(long nativePtr, long netwo rkHandle); 220 private native void nativeNotifyOfNetworkDisconnect(long nativePtr, long netwo rkHandle);
236 private native void nativeNotifyOfActiveNetworkList( 221 private native void nativeNotifyOfActiveNetworkList(
237 long nativePtr, NetworkInformation[] networkInfos); 222 long nativePtr, NetworkInformation[] networkInfos);
238 223
239 // For testing only. 224 // For testing only.
240 static void resetInstanceForTests() { 225 static void resetInstanceForTests() {
241 instance = new NetworkMonitor(); 226 instance = new NetworkMonitor();
242 } 227 }
243 228
244 // For testing only. 229 // For testing only.
245 public static NetworkMonitorAutoDetect getAutoDetectorForTest() { 230 static void createAutoDetectorForTest() {
231 getInstance().createAutoDetector();
232 }
233
234 // For testing only.
235 static NetworkMonitorAutoDetect getAutoDetectorForTest() {
246 return getInstance().autoDetector; 236 return getInstance().autoDetector;
247 } 237 }
248 } 238 }
OLDNEW
« no previous file with comments | « no previous file | webrtc/sdk/android/instrumentationtests/src/org/webrtc/NetworkMonitorTest.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698