OLD | NEW |
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; |
(...skipping 10 matching lines...) Expand all Loading... |
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 private static final String TAG = "NetworkChangeNotifierAutoDetect"; | 26 private static final String TAG = "NetworkChangeNotifierAutoDetect"; |
27 | 27 |
28 private final NetworkConnectivityIntentFilter mIntentFilter = | 28 private final NetworkConnectivityIntentFilter mIntentFilter = |
29 new NetworkConnectivityIntentFilter(); | 29 new NetworkConnectivityIntentFilter(); |
30 | 30 |
31 private final NetworkChangeNotifier mOwner; | 31 private final Observer mObserver; |
32 | 32 |
33 private final Context mContext; | 33 private final Context mContext; |
34 private boolean mRegistered; | 34 private boolean mRegistered; |
35 private int mConnectionType; | 35 private int mConnectionType; |
36 | 36 |
37 public NetworkChangeNotifierAutoDetect(NetworkChangeNotifier owner, Context
context) { | 37 /** |
38 mOwner = owner; | 38 * Observer notified on the UI thread whenever a new connection type was det
ected. |
| 39 */ |
| 40 public static interface Observer { |
| 41 public void onConnectionTypeChanged(int newConnectionType); |
| 42 } |
| 43 |
| 44 public NetworkChangeNotifierAutoDetect(Observer observer, Context context) { |
| 45 mObserver = observer; |
39 mContext = context; | 46 mContext = context; |
40 mConnectionType = currentConnectionType(context); | 47 mConnectionType = currentConnectionType(context); |
41 | 48 |
42 ActivityStatus status = ActivityStatus.getInstance(); | 49 ActivityStatus status = ActivityStatus.getInstance(); |
43 if (!status.isPaused()) { | 50 if (!status.isPaused()) { |
44 registerReceiver(); | 51 registerReceiver(); |
45 } | 52 } |
46 status.registerListener(this); | 53 status.registerListener(this); |
47 } | 54 } |
48 | 55 |
49 public int connectionType() { | |
50 return mConnectionType; | |
51 } | |
52 | |
53 public void destroy() { | 56 public void destroy() { |
54 unregisterReceiver(); | 57 unregisterReceiver(); |
55 } | 58 } |
56 | 59 |
57 /** | 60 /** |
58 * Register a BroadcastReceiver in the given context. | 61 * Register a BroadcastReceiver in the given context. |
59 */ | 62 */ |
60 private void registerReceiver() { | 63 private void registerReceiver() { |
61 if (!mRegistered) { | 64 if (!mRegistered) { |
62 mRegistered = true; | 65 mRegistered = true; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 // BroadcastReceiver | 125 // BroadcastReceiver |
123 @Override | 126 @Override |
124 public void onReceive(Context context, Intent intent) { | 127 public void onReceive(Context context, Intent intent) { |
125 boolean noConnection = | 128 boolean noConnection = |
126 intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY
, false); | 129 intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY
, false); |
127 int newConnectionType = noConnection ? | 130 int newConnectionType = noConnection ? |
128 NetworkChangeNotifier.CONNECTION_NONE : currentConnectionType(co
ntext); | 131 NetworkChangeNotifier.CONNECTION_NONE : currentConnectionType(co
ntext); |
129 | 132 |
130 if (newConnectionType != mConnectionType) { | 133 if (newConnectionType != mConnectionType) { |
131 mConnectionType = newConnectionType; | 134 mConnectionType = newConnectionType; |
132 Log.d(TAG, "Network connectivity changed, type is: " + mConnectionTy
pe); | 135 Log.d(TAG, "Network connectivity changed, type is: " + newConnection
Type); |
133 mOwner.notifyNativeObservers(); | 136 mObserver.onConnectionTypeChanged(newConnectionType); |
134 } | 137 } |
135 } | 138 } |
136 | 139 |
137 // AcitivityStatus.Listener | 140 // AcitivityStatus.Listener |
138 @Override | 141 @Override |
139 public void onActivityStatusChanged(boolean isPaused) { | 142 public void onActivityStatusChanged(boolean isPaused) { |
140 if (isPaused) { | 143 if (isPaused) { |
141 unregisterReceiver(); | 144 unregisterReceiver(); |
142 } else { | 145 } else { |
143 registerReceiver(); | 146 registerReceiver(); |
144 } | 147 } |
145 } | 148 } |
146 | 149 |
147 private static class NetworkConnectivityIntentFilter extends IntentFilter { | 150 private static class NetworkConnectivityIntentFilter extends IntentFilter { |
148 NetworkConnectivityIntentFilter() { | 151 NetworkConnectivityIntentFilter() { |
149 addAction(ConnectivityManager.CONNECTIVITY_ACTION); | 152 addAction(ConnectivityManager.CONNECTIVITY_ACTION); |
150 } | 153 } |
151 } | 154 } |
152 } | 155 } |
OLD | NEW |