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

Unified 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: rebase on base chagnes 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/android/java/net.xml ('k') | net/android/network_change_notifier.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/android/java/org/chromium/net/NetworkChangeNotifier.java
diff --git a/net/android/java/org/chromium/net/NetworkChangeNotifier.java b/net/android/java/org/chromium/net/NetworkChangeNotifier.java
new file mode 100644
index 0000000000000000000000000000000000000000..e21f61f78bd833176dac330eaa45ab8eee0dd730
--- /dev/null
+++ b/net/android/java/org/chromium/net/NetworkChangeNotifier.java
@@ -0,0 +1,129 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.net;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
+import android.util.Log;
+
+import org.chromium.base.ActivityStatus;
+import org.chromium.base.CalledByNative;
+
+/**
+ * Triggers updates to the underlying network state in native Chrome
+ */
+public class NetworkChangeNotifier extends BroadcastReceiver implements ActivityStatus.Listener {
+
+ private static final String TAG = "NetworkChangeNotifier";
+
+ private final NetworkConnectivityIntentFilter mIntentFilter =
+ new NetworkConnectivityIntentFilter();
+
+ private Context mContext;
+ private int mNativeChangeNotifier;
+ private boolean mRegistered;
+ private boolean mIsConnected;
+
+ private static NetworkChangeNotifier sNetworkChangeNotifierForTest;
+
+ public static NetworkChangeNotifier getNetworkChangeNotifierForTest() {
+ return sNetworkChangeNotifierForTest;
+ }
+
+ private NetworkChangeNotifier(Context context, int nativeChangeNotifier) {
+ mContext = context;
+ mNativeChangeNotifier = nativeChangeNotifier;
+ mIsConnected = checkIfConnected(mContext);
+ ActivityStatus status = ActivityStatus.getInstance();
+ if (!status.isPaused()) {
+ registerReceiver();
+ }
+ status.registerListener(this);
+ sNetworkChangeNotifierForTest = this;
+ }
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)) {
+ if (mIsConnected) {
+ mIsConnected = false;
+ Log.d(TAG, "Network connectivity changed, no connectivity.");
+ nativeNotifyObservers(mNativeChangeNotifier);
+ }
+ } else {
+ boolean isConnected = checkIfConnected(context);
+ if (isConnected != mIsConnected) {
+ mIsConnected = isConnected;
+ Log.d(TAG, "Network connectivity changed, status is: " + isConnected);
+ nativeNotifyObservers(mNativeChangeNotifier);
+ }
+ }
+ }
+
+ private boolean checkIfConnected(Context context) {
+ ConnectivityManager manager = (ConnectivityManager)
+ context.getSystemService(Context.CONNECTIVITY_SERVICE);
+ boolean isConnected = false;
+ for (NetworkInfo info: manager.getAllNetworkInfo()) {
+ if (info.isConnected()) {
+ isConnected = true;
+ break;
+ }
+ }
+ return isConnected;
+ }
+
+ @CalledByNative
+ private boolean isConnected() {
+ return mIsConnected;
+ }
+
+ /**
+ * Register a BroadcastReceiver in the given context.
+ */
+ private void registerReceiver() {
+ if (!mRegistered) {
+ mRegistered = true;
+ mContext.registerReceiver(this, mIntentFilter);
+ }
+ }
+ /**
+ * Unregister the BroadcastReceiver in the given context.
+ */
+ @CalledByNative
+ private void unregisterReceiver() {
+ if (mRegistered) {
+ mRegistered = false;
+ mContext.unregisterReceiver(this);
+ }
+ }
+
+ @Override
+ public void onActivityStatusChanged(boolean isPaused) {
+ if (isPaused) {
+ unregisterReceiver();
+ } else {
+ registerReceiver();
+ }
+ }
+
+ @CalledByNative
+ static NetworkChangeNotifier create(Context context, int nativeNetworkChangeNotifier) {
+ return new NetworkChangeNotifier(context, nativeNetworkChangeNotifier);
+ }
+
+ private static class NetworkConnectivityIntentFilter extends IntentFilter {
+ NetworkConnectivityIntentFilter() {
+ addAction(ConnectivityManager.CONNECTIVITY_ACTION);
+ }
+ }
+
+ private native void nativeNotifyObservers(
+ int nativeNetworkChangeNotifier /* net::android::NetworkChangeNotifier */);
+}
« no previous file with comments | « net/android/java/net.xml ('k') | net/android/network_change_notifier.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698