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

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

Issue 10073024: Add NetworkChangeNotifier for Android. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 8 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 package org.chromium.net;
6
7 import android.content.BroadcastReceiver;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.content.IntentFilter;
11 import android.net.ConnectivityManager;
12 import android.net.NetworkInfo;
13 import android.util.Log;
14
15 import org.chromium.base.ActivityStatus;
16 import org.chromium.base.CalledByNative;
17
18 /**
19 * Triggers updates to the underlying network state in native Chrome
20 */
21 public class NetworkChangeNotifier extends BroadcastReceiver implements Activity Status.Listener {
22
23 private static final String TAG = "NetworkChangeNotifier";
24
25 private final NetworkConnectivityIntentFilter mIntentFilter =
26 new NetworkConnectivityIntentFilter();
27
28 private Context mContext;
29 private int mNativeChangeNotifier;
30 private boolean mRegistered;
31 private boolean mIsConnected;
32
33 private static NetworkChangeNotifier sNetworkChangeNotifierForTest;
34
35 public static NetworkChangeNotifier getNetworkChangeNotifierForTest() {
36 return sNetworkChangeNotifierForTest;
37 }
38
39 private NetworkChangeNotifier(Context context, int nativeChangeNotifier) {
40 mContext = context;
41 mNativeChangeNotifier = nativeChangeNotifier;
42 mIsConnected = checkIfConnected(mContext);
43 ActivityStatus status = ActivityStatus.getInstance();
44 if (!status.isPaused()) {
45 registerReceiver();
46 }
47 status.registerListener(this);
48 sNetworkChangeNotifierForTest = this;
49 }
50
51 @Override
52 public void onReceive(Context context, Intent intent) {
53 if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, fa lse)) {
54 if (mIsConnected) {
55 mIsConnected = false;
56 Log.d(TAG, "Network connectivity changed, no connectivity.");
57 nativeNotifyObservers(mNativeChangeNotifier);
58 }
59 } else {
60 boolean isConnected = checkIfConnected(context);
61 if (isConnected != mIsConnected) {
62 mIsConnected = isConnected;
63 Log.d(TAG, "Network connectivity changed, status is: " + isConnect ed);
64 nativeNotifyObservers(mNativeChangeNotifier);
65 }
66 }
67 }
68
69 private boolean checkIfConnected(Context context) {
70 ConnectivityManager manager = (ConnectivityManager)
71 context.getSystemService(Context.CONNECTIVITY_SERVICE);
72 boolean isConnected = false;
73 for (NetworkInfo info: manager.getAllNetworkInfo()) {
74 if (info.isConnected()) {
75 isConnected = true;
76 break;
77 }
78 }
79 return isConnected;
80 }
81
82 @CalledByNative
83 private boolean isConnected() {
84 return mIsConnected;
85 }
86
87 /**
88 * Register a BroadcastReceiver in the given context.
89 */
90 private void registerReceiver() {
91 if (!mRegistered) {
92 mRegistered = true;
93 mContext.registerReceiver(this, mIntentFilter);
94 }
95 }
96 /**
97 * Unregister the BroadcastReceiver in the given context.
98 */
99 @CalledByNative
100 private void unregisterReceiver() {
101 if (mRegistered) {
102 mRegistered = false;
103 mContext.unregisterReceiver(this);
104 }
105 }
106
107 @Override
108 public void onActivityStatusChanged(boolean isPaused) {
109 if (isPaused) {
110 unregisterReceiver();
111 } else {
112 registerReceiver();
113 }
114 }
115
116 @CalledByNative
117 static NetworkChangeNotifier create(Context context, int nativeNetworkChange Notifier) {
118 return new NetworkChangeNotifier(context, nativeNetworkChangeNotifier);
119 }
120
121 private static class NetworkConnectivityIntentFilter extends IntentFilter {
122 NetworkConnectivityIntentFilter() {
123 addAction(ConnectivityManager.CONNECTIVITY_ACTION);
124 }
125 }
126
127 private native void nativeNotifyObservers(int nativeNetworkChangeNotifier);
128 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698