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

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

Issue 780293003: [NetInfo] Add MaxBandwidthChanged notification and implement on Android (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@maxbandwidth_android
Patch Set: Nits Created 6 years 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.Manifest.permission; 7 import android.Manifest.permission;
8 import android.content.BroadcastReceiver; 8 import android.content.BroadcastReceiver;
9 import android.content.Context; 9 import android.content.Context;
10 import android.content.Intent; 10 import android.content.Intent;
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 new NetworkConnectivityIntentFilter(); 138 new NetworkConnectivityIntentFilter();
139 139
140 private final Observer mObserver; 140 private final Observer mObserver;
141 141
142 private final Context mContext; 142 private final Context mContext;
143 private ConnectivityManagerDelegate mConnectivityManagerDelegate; 143 private ConnectivityManagerDelegate mConnectivityManagerDelegate;
144 private WifiManagerDelegate mWifiManagerDelegate; 144 private WifiManagerDelegate mWifiManagerDelegate;
145 private boolean mRegistered; 145 private boolean mRegistered;
146 private int mConnectionType; 146 private int mConnectionType;
147 private String mWifiSSID; 147 private String mWifiSSID;
148 private double mMaxBandwidthMbps;
148 149
149 /** 150 /**
150 * Observer notified on the UI thread whenever a new connection type was det ected. 151 * Observer notified on the UI thread whenever a new connection type was det ected or max
152 * bandwidth is changed.
151 */ 153 */
152 public static interface Observer { 154 public static interface Observer {
153 public void onConnectionTypeChanged(int newConnectionType); 155 public void onConnectionTypeChanged(int newConnectionType);
156 public void onMaxBandwidthChanged(double maxBandwidthMbps);
154 } 157 }
155 158
156 /** 159 /**
157 * Constructs a NetworkChangeNotifierAutoDetect. 160 * Constructs a NetworkChangeNotifierAutoDetect.
158 * @param alwaysWatchForChanges If true, always watch for network changes. 161 * @param alwaysWatchForChanges If true, always watch for network changes.
159 * Otherwise, only watch if app is in foreground. 162 * Otherwise, only watch if app is in foreground.
160 */ 163 */
161 public NetworkChangeNotifierAutoDetect(Observer observer, Context context, 164 public NetworkChangeNotifierAutoDetect(Observer observer, Context context,
162 boolean alwaysWatchForChanges) { 165 boolean alwaysWatchForChanges) {
163 mObserver = observer; 166 mObserver = observer;
164 mContext = context.getApplicationContext(); 167 mContext = context.getApplicationContext();
165 mConnectivityManagerDelegate = new ConnectivityManagerDelegate(context); 168 mConnectivityManagerDelegate = new ConnectivityManagerDelegate(context);
166 mWifiManagerDelegate = new WifiManagerDelegate(context); 169 mWifiManagerDelegate = new WifiManagerDelegate(context);
167 mConnectionType = getCurrentConnectionType(); 170 mConnectionType = getCurrentConnectionType();
168 mWifiSSID = getCurrentWifiSSID(); 171 mWifiSSID = getCurrentWifiSSID();
172 mMaxBandwidthMbps = getCurrentMaxBandwidthInMbps();
169 if (alwaysWatchForChanges) { 173 if (alwaysWatchForChanges) {
170 registerReceiver(); 174 registerReceiver();
171 } else { 175 } else {
172 ApplicationStatus.registerApplicationStateListener(this); 176 ApplicationStatus.registerApplicationStateListener(this);
173 } 177 }
174 } 178 }
175 179
176 /** 180 /**
177 * Allows overriding the ConnectivityManagerDelegate for tests. 181 * Allows overriding the ConnectivityManagerDelegate for tests.
178 */ 182 */
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 default: 258 default:
255 return ConnectionType.CONNECTION_UNKNOWN; 259 return ConnectionType.CONNECTION_UNKNOWN;
256 } 260 }
257 } 261 }
258 262
259 /* 263 /*
260 * Returns the bandwidth of the current connection in Mbps. The result is 264 * Returns the bandwidth of the current connection in Mbps. The result is
261 * derived from the NetInfo v3 specification's mapping from network type to 265 * derived from the NetInfo v3 specification's mapping from network type to
262 * max link speed. In cases where more information is available, such as wif i, 266 * max link speed. In cases where more information is available, such as wif i,
263 * that is used instead. For more on NetInfo, see http://w3c.github.io/netin fo/. 267 * that is used instead. For more on NetInfo, see http://w3c.github.io/netin fo/.
264 *
265 * TODO(jkarlin): Add a notification of bandwidth change to the NetworkChang eNotifier.
266 * Without that the MaxBandwidth value will be stale until the network type or address
267 * changes again.
268 */ 268 */
269 public double getCurrentMaxBandwidthInMbps() { 269 public double getCurrentMaxBandwidthInMbps() {
270 if (getCurrentConnectionType() == ConnectionType.CONNECTION_WIFI) { 270 if (getCurrentConnectionType() == ConnectionType.CONNECTION_WIFI) {
271 final int link_speed = mWifiManagerDelegate.getLinkSpeedInMbps(); 271 final int link_speed = mWifiManagerDelegate.getLinkSpeedInMbps();
272 if (link_speed != UNKNOWN_LINK_SPEED) { 272 if (link_speed != UNKNOWN_LINK_SPEED) {
273 return link_speed; 273 return link_speed;
274 } 274 }
275 } 275 }
276 276
277 return NetworkChangeNotifier.getMaxBandwidthForConnectionSubtype( 277 return NetworkChangeNotifier.getMaxBandwidthForConnectionSubtype(
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 333
334 private String getCurrentWifiSSID() { 334 private String getCurrentWifiSSID() {
335 if (getCurrentConnectionType() != ConnectionType.CONNECTION_WIFI) 335 if (getCurrentConnectionType() != ConnectionType.CONNECTION_WIFI)
336 return ""; 336 return "";
337 return mWifiManagerDelegate.getWifiSSID(); 337 return mWifiManagerDelegate.getWifiSSID();
338 } 338 }
339 339
340 // BroadcastReceiver 340 // BroadcastReceiver
341 @Override 341 @Override
342 public void onReceive(Context context, Intent intent) { 342 public void onReceive(Context context, Intent intent) {
343 connectionTypeChanged(); 343 if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
344 connectionTypeChanged();
345 maxBandwidthChanged();
346 } else if (WifiManager.RSSI_CHANGED_ACTION.equals(intent.getAction())) {
347 maxBandwidthChanged();
348 }
344 } 349 }
345 350
346 // ApplicationStatus.ApplicationStateListener 351 // ApplicationStatus.ApplicationStateListener
347 @Override 352 @Override
348 public void onApplicationStateChange(int newState) { 353 public void onApplicationStateChange(int newState) {
349 if (newState == ApplicationState.HAS_RUNNING_ACTIVITIES) { 354 if (newState == ApplicationState.HAS_RUNNING_ACTIVITIES) {
350 connectionTypeChanged(); 355 connectionTypeChanged();
351 registerReceiver(); 356 registerReceiver();
352 } else if (newState == ApplicationState.HAS_PAUSED_ACTIVITIES) { 357 } else if (newState == ApplicationState.HAS_PAUSED_ACTIVITIES) {
353 unregisterReceiver(); 358 unregisterReceiver();
354 } 359 }
355 } 360 }
356 361
357 private void connectionTypeChanged() { 362 private void connectionTypeChanged() {
358 int newConnectionType = getCurrentConnectionType(); 363 int newConnectionType = getCurrentConnectionType();
359 String newWifiSSID = getCurrentWifiSSID(); 364 String newWifiSSID = getCurrentWifiSSID();
360 if (newConnectionType == mConnectionType && newWifiSSID.equals(mWifiSSID )) 365 if (newConnectionType == mConnectionType && newWifiSSID.equals(mWifiSSID ))
361 return; 366 return;
362 367
363 mConnectionType = newConnectionType; 368 mConnectionType = newConnectionType;
364 mWifiSSID = newWifiSSID; 369 mWifiSSID = newWifiSSID;
365 Log.d(TAG, "Network connectivity changed, type is: " + mConnectionType); 370 Log.d(TAG, "Network connectivity changed, type is: " + mConnectionType);
366 mObserver.onConnectionTypeChanged(newConnectionType); 371 mObserver.onConnectionTypeChanged(newConnectionType);
367 } 372 }
368 373
374 private void maxBandwidthChanged() {
375 double newMaxBandwidthMbps = getCurrentMaxBandwidthInMbps();
376 if (newMaxBandwidthMbps == mMaxBandwidthMbps) return;
377 mMaxBandwidthMbps = newMaxBandwidthMbps;
378 mObserver.onMaxBandwidthChanged(newMaxBandwidthMbps);
379 }
380
369 private static class NetworkConnectivityIntentFilter extends IntentFilter { 381 private static class NetworkConnectivityIntentFilter extends IntentFilter {
370 NetworkConnectivityIntentFilter() { 382 NetworkConnectivityIntentFilter() {
371 addAction(ConnectivityManager.CONNECTIVITY_ACTION); 383 addAction(ConnectivityManager.CONNECTIVITY_ACTION);
384 addAction(WifiManager.RSSI_CHANGED_ACTION);
pauljensen 2014/12/05 19:27:34 We should not be registering for another broadcast
jkarlin 2014/12/08 16:58:43 Done.
372 } 385 }
373 } 386 }
374 } 387 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698