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

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

Issue 10968016: [Android] Add Java observers to the NetworkChangeNotifier (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Double rebase Created 8 years, 3 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 (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.BroadcastReceiver; 7 import android.content.BroadcastReceiver;
8 import android.content.Context; 8 import android.content.Context;
9 import android.content.Intent; 9 import android.content.Intent;
10 import android.content.IntentFilter; 10 import android.content.IntentFilter;
11 import android.net.ConnectivityManager; 11 import android.net.ConnectivityManager;
12 import android.net.NetworkInfo; 12 import android.net.NetworkInfo;
13 import android.telephony.TelephonyManager; 13 import android.telephony.TelephonyManager;
14 import android.util.Log; 14 import android.util.Log;
15 15
16 import org.chromium.base.ActivityStatus; 16 import org.chromium.base.ActivityStatus;
17 17
18 /** 18 /**
19 * Used by the NetworkChangeNotifier to listens to platform changes in connectiv ity. 19 * Used by the NetworkChangeNotifier to listens to platform changes in connectiv ity.
20 * Note that use of this class requires that the app have the platform 20 * Note that use of this class requires that the app have the platform
21 * ACCESS_NETWORK_STATE permission. 21 * ACCESS_NETWORK_STATE permission.
22 */ 22 */
23 public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver 23 public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
24 implements ActivityStatus.Listener { 24 implements ActivityStatus.Listener {
25 25
26 /** Queries the ConnectivityManager for information about the current connec tion. */
27 static class ConnectivityManagerDelegate {
28 private ConnectivityManager mConnectivityManager;
29
30 ConnectivityManagerDelegate(Context context) {
31 if (context != null) {
32 mConnectivityManager = (ConnectivityManager)
33 context.getSystemService(Context.CONNECTIVITY_SERVICE);
34 }
35 }
36
37 boolean activeNetworkExists() {
38 return mConnectivityManager != null &&
39 mConnectivityManager.getActiveNetworkInfo() != null;
40 }
41
42 int getNetworkType() {
43 return mConnectivityManager.getActiveNetworkInfo().getType();
44 }
45
46 int getNetworkSubtype() {
47 return mConnectivityManager.getActiveNetworkInfo().getSubtype();
48 }
49 }
50
26 private static final String TAG = "NetworkChangeNotifierAutoDetect"; 51 private static final String TAG = "NetworkChangeNotifierAutoDetect";
27 52
28 private final NetworkConnectivityIntentFilter mIntentFilter = 53 private final NetworkConnectivityIntentFilter mIntentFilter =
29 new NetworkConnectivityIntentFilter(); 54 new NetworkConnectivityIntentFilter();
30 55
31 private final Observer mObserver; 56 private final Observer mObserver;
32 57
33 private final Context mContext; 58 private final Context mContext;
59 private ConnectivityManagerDelegate mConnectivityManagerDelegate;
34 private boolean mRegistered; 60 private boolean mRegistered;
35 private int mConnectionType; 61 private int mConnectionType;
36 62
37 /** 63 /**
38 * Observer notified on the UI thread whenever a new connection type was det ected. 64 * Observer notified on the UI thread whenever a new connection type was det ected.
39 */ 65 */
40 public static interface Observer { 66 public static interface Observer {
41 public void onConnectionTypeChanged(int newConnectionType); 67 public void onConnectionTypeChanged(int newConnectionType);
42 } 68 }
43 69
44 public NetworkChangeNotifierAutoDetect(Observer observer, Context context) { 70 public NetworkChangeNotifierAutoDetect(Observer observer, Context context) {
45 mObserver = observer; 71 mObserver = observer;
46 mContext = context; 72 mContext = context;
73 mConnectivityManagerDelegate = new ConnectivityManagerDelegate(context);
47 mConnectionType = currentConnectionType(context); 74 mConnectionType = currentConnectionType(context);
48 75
49 ActivityStatus status = ActivityStatus.getInstance(); 76 ActivityStatus status = ActivityStatus.getInstance();
50 if (!status.isPaused()) { 77 if (!status.isPaused()) {
51 registerReceiver(); 78 registerReceiver();
52 } 79 }
53 status.registerListener(this); 80 status.registerListener(this);
54 } 81 }
55 82
83 /**
84 * Allows overriding the ConnectivityManagerDelegate for tests.
85 */
86 void setConnectivityManagerDelegateForTests(ConnectivityManagerDelegate dele gate) {
87 mConnectivityManagerDelegate = delegate;
88 }
89
56 public void destroy() { 90 public void destroy() {
57 unregisterReceiver(); 91 unregisterReceiver();
58 } 92 }
59 93
60 /** 94 /**
61 * Register a BroadcastReceiver in the given context. 95 * Register a BroadcastReceiver in the given context.
62 */ 96 */
63 private void registerReceiver() { 97 private void registerReceiver() {
64 if (!mRegistered) { 98 if (!mRegistered) {
65 mRegistered = true; 99 mRegistered = true;
66 mContext.registerReceiver(this, mIntentFilter); 100 mContext.registerReceiver(this, mIntentFilter);
67 } 101 }
68 } 102 }
69 103
70 /** 104 /**
71 * Unregister the BroadcastReceiver in the given context. 105 * Unregister the BroadcastReceiver in the given context.
72 */ 106 */
73 private void unregisterReceiver() { 107 private void unregisterReceiver() {
74 if (mRegistered) { 108 if (mRegistered) {
75 mRegistered = false; 109 mRegistered = false;
76 mContext.unregisterReceiver(this); 110 mContext.unregisterReceiver(this);
77 } 111 }
78 } 112 }
79 113
80 private int currentConnectionType(Context context) { 114 private int currentConnectionType(Context context) {
81 // Track exactly what type of connection we have. 115 // Track exactly what type of connection we have.
82 ConnectivityManager connectivityManager = (ConnectivityManager) 116 if (!mConnectivityManagerDelegate.activeNetworkExists()) {
83 context.getSystemService(Context.CONNECTIVITY_SERVICE);
84 NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo ();
85 if (activeNetworkInfo == null) {
86 return NetworkChangeNotifier.CONNECTION_NONE; 117 return NetworkChangeNotifier.CONNECTION_NONE;
87 } 118 }
88 119
89 switch (activeNetworkInfo.getType()) { 120 switch (mConnectivityManagerDelegate.getNetworkType()) {
90 case ConnectivityManager.TYPE_ETHERNET: 121 case ConnectivityManager.TYPE_ETHERNET:
91 return NetworkChangeNotifier.CONNECTION_ETHERNET; 122 return NetworkChangeNotifier.CONNECTION_ETHERNET;
92 case ConnectivityManager.TYPE_WIFI: 123 case ConnectivityManager.TYPE_WIFI:
93 return NetworkChangeNotifier.CONNECTION_WIFI; 124 return NetworkChangeNotifier.CONNECTION_WIFI;
94 case ConnectivityManager.TYPE_WIMAX: 125 case ConnectivityManager.TYPE_WIMAX:
95 return NetworkChangeNotifier.CONNECTION_4G; 126 return NetworkChangeNotifier.CONNECTION_4G;
96 case ConnectivityManager.TYPE_MOBILE: 127 case ConnectivityManager.TYPE_MOBILE:
97 // Use information from TelephonyManager to classify the connect ion. 128 // Use information from TelephonyManager to classify the connect ion.
98 switch (activeNetworkInfo.getSubtype()) { 129 switch (mConnectivityManagerDelegate.getNetworkSubtype()) {
99 case TelephonyManager.NETWORK_TYPE_GPRS: 130 case TelephonyManager.NETWORK_TYPE_GPRS:
100 case TelephonyManager.NETWORK_TYPE_EDGE: 131 case TelephonyManager.NETWORK_TYPE_EDGE:
101 case TelephonyManager.NETWORK_TYPE_CDMA: 132 case TelephonyManager.NETWORK_TYPE_CDMA:
102 case TelephonyManager.NETWORK_TYPE_1xRTT: 133 case TelephonyManager.NETWORK_TYPE_1xRTT:
103 case TelephonyManager.NETWORK_TYPE_IDEN: 134 case TelephonyManager.NETWORK_TYPE_IDEN:
104 return NetworkChangeNotifier.CONNECTION_2G; 135 return NetworkChangeNotifier.CONNECTION_2G;
105 case TelephonyManager.NETWORK_TYPE_UMTS: 136 case TelephonyManager.NETWORK_TYPE_UMTS:
106 case TelephonyManager.NETWORK_TYPE_EVDO_0: 137 case TelephonyManager.NETWORK_TYPE_EVDO_0:
107 case TelephonyManager.NETWORK_TYPE_EVDO_A: 138 case TelephonyManager.NETWORK_TYPE_EVDO_A:
108 case TelephonyManager.NETWORK_TYPE_HSDPA: 139 case TelephonyManager.NETWORK_TYPE_HSDPA:
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 registerReceiver(); 177 registerReceiver();
147 } 178 }
148 } 179 }
149 180
150 private static class NetworkConnectivityIntentFilter extends IntentFilter { 181 private static class NetworkConnectivityIntentFilter extends IntentFilter {
151 NetworkConnectivityIntentFilter() { 182 NetworkConnectivityIntentFilter() {
152 addAction(ConnectivityManager.CONNECTIVITY_ACTION); 183 addAction(ConnectivityManager.CONNECTIVITY_ACTION);
153 } 184 }
154 } 185 }
155 } 186 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698