summaryrefslogtreecommitdiffstats
path: root/net/android
diff options
context:
space:
mode:
authoryfriedman@chromium.org <yfriedman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-04-20 03:24:07 +0000
committeryfriedman@chromium.org <yfriedman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-04-20 03:24:07 +0000
commitad116b2e8ccdb745d9310de17db9fe3aeb311b04 (patch)
tree95f9b52f8b4dc9e79fd9448d4ed29b28213d684f /net/android
parentd587575ca2f7233baf8fd2bc677c238b9a80c521 (diff)
downloadchromium_src-ad116b2e8ccdb745d9310de17db9fe3aeb311b04.zip
chromium_src-ad116b2e8ccdb745d9310de17db9fe3aeb311b04.tar.gz
chromium_src-ad116b2e8ccdb745d9310de17db9fe3aeb311b04.tar.bz2
Add NetworkChangeNotifier for Android.
The native notifier spawns a Java-side notifier that registers for notifications from Android's ConnectivityManager. As for the lack of tests, they're written at a higher-level downstream. Review URL: http://codereview.chromium.org/10073024 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@133142 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/android')
-rw-r--r--net/android/OWNERS2
-rw-r--r--net/android/java/net.xml59
-rw-r--r--net/android/java/org/chromium/net/NetworkChangeNotifier.java129
-rw-r--r--net/android/network_change_notifier.cc49
-rw-r--r--net/android/network_change_notifier.h41
-rw-r--r--net/android/network_change_notifier_factory.cc19
-rw-r--r--net/android/network_change_notifier_factory.h31
7 files changed, 330 insertions, 0 deletions
diff --git a/net/android/OWNERS b/net/android/OWNERS
new file mode 100644
index 0000000..fc6109a
--- /dev/null
+++ b/net/android/OWNERS
@@ -0,0 +1,2 @@
+jrg@chromium.org
+satish@chromium.org
diff --git a/net/android/java/net.xml b/net/android/java/net.xml
new file mode 100644
index 0000000..76dddcc
--- /dev/null
+++ b/net/android/java/net.xml
@@ -0,0 +1,59 @@
+<project name="net" default="dist" basedir=".">
+ <description>
+ Building net/ java source code with ant.
+ </description>
+ <!-- Set global properties for this build -->
+ <property environment="env"/>
+ <property name="sdk.dir" location="${env.ANDROID_SDK_ROOT}"/>
+ <!-- TODO(jrg): The apk-runner's version is hardcoded to SDK version 14. These
+ two should be unified. -->
+ <property name="sdk.version" value="${env.ANDROID_SDK_VERSION}"/>
+ <property name="src" location="."/>
+ <property name="dist" location="dist"/>
+ <property name="out.dir" location="${PRODUCT_DIR}/lib.java"/>
+ <!-- TODO(jrg): establish a standard for the intermediate java
+ directories. Settle on a standard once ant/jar build files
+ like this are androidified -->
+ <property name="dest.dir" location="${PRODUCT_DIR}/java/net"/>
+
+ <!-- Set path depending on the type of repository. If ANDROID_BUILD_TOP is
+ set then build using the provided location. Otherwise, assume the build
+ is using the released SDK and set the path accordingly. -->
+ <condition property="location.base"
+ value="${sdk.dir}"
+ else="${sdk.dir}/platforms/android-${sdk.version}">
+ <isset property="env.ANDROID_BUILD_TOP"/>
+ </condition>
+
+ <target name="init">
+ <!-- Create the time stamp -->
+ <tstamp/>
+ <!-- Create the build directory structure used by compile -->
+ <mkdir dir="${out.dir}"/>
+ <mkdir dir="${dest.dir}"/>
+ </target>
+
+ <target name="compile" depends="init"
+ description="compile the source " >
+ <javac srcdir="${src}" destdir="${dest.dir}">
+ <classpath>
+ <path location="${location.base}/android.jar"/>
+ <path location="${PRODUCT_DIR}/chromium_base.jar"/>
+ </classpath>
+ </javac>
+ </target>
+
+ <target name="dist" depends="compile"
+ description="generate the distribution" >
+ <!-- Create the distribution directory -->
+ <mkdir dir="${dist}/lib"/>
+
+ <jar jarfile="${out.dir}/chromium_net.jar" basedir="${dest.dir}"/>
+ </target>
+
+ <target name="clean"
+ description="clean up" >
+ <delete dir="${dest.dir}"/>
+ <delete dir="${dist}"/>
+ </target>
+</project>
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 0000000..e21f61f
--- /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 */);
+}
diff --git a/net/android/network_change_notifier.cc b/net/android/network_change_notifier.cc
new file mode 100644
index 0000000..562f3f1
--- /dev/null
+++ b/net/android/network_change_notifier.cc
@@ -0,0 +1,49 @@
+// 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.
+
+#include "net/android/network_change_notifier.h"
+
+#include "base/logging.h"
+#include "base/android/jni_android.h"
+#include "jni/network_change_notifier_jni.h"
+
+namespace net {
+namespace android {
+
+NetworkChangeNotifier::NetworkChangeNotifier() {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ CreateJavaObject(env);
+}
+
+NetworkChangeNotifier::~NetworkChangeNotifier() {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ Java_NetworkChangeNotifier_unregisterReceiver(
+ env, java_network_change_notifier_.obj());
+}
+
+void NetworkChangeNotifier::CreateJavaObject(JNIEnv* env) {
+ java_network_change_notifier_.Reset(
+ Java_NetworkChangeNotifier_create(
+ env,
+ base::android::GetApplicationContext(),
+ reinterpret_cast<jint>(this)));
+}
+
+void NetworkChangeNotifier::NotifyObservers(JNIEnv* env, jobject obj) {
+ NotifyObserversOfOnlineStateChange();
+}
+
+bool NetworkChangeNotifier::IsCurrentlyOffline() const {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ return !Java_NetworkChangeNotifier_isConnected(
+ env, java_network_change_notifier_.obj());
+}
+
+// static
+bool NetworkChangeNotifier::Register(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+} // namespace android
+} // namespace net
diff --git a/net/android/network_change_notifier.h b/net/android/network_change_notifier.h
new file mode 100644
index 0000000..5691a00
--- /dev/null
+++ b/net/android/network_change_notifier.h
@@ -0,0 +1,41 @@
+// 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.
+
+#ifndef NET_ANDROID_NETWORK_CHANGE_NOTIFIER_ANDROID_H_
+#define NET_ANDROID_NETWORK_CHANGE_NOTIFIER_ANDROID_H_
+#pragma once
+
+#include "base/android/scoped_java_ref.h"
+#include "base/basictypes.h"
+#include "base/compiler_specific.h"
+#include "base/memory/scoped_ptr.h"
+#include "net/base/network_change_notifier.h"
+
+namespace net {
+namespace android {
+
+class NetworkChangeNotifier : public net::NetworkChangeNotifier {
+ public:
+ NetworkChangeNotifier();
+ virtual ~NetworkChangeNotifier();
+
+ void NotifyObservers(JNIEnv* env, jobject obj);
+
+ static bool Register(JNIEnv* env);
+
+ private:
+ void CreateJavaObject(JNIEnv* env);
+
+ // NetworkChangeNotifier:
+ virtual bool IsCurrentlyOffline() const OVERRIDE;
+
+ base::android::ScopedJavaGlobalRef<jobject> java_network_change_notifier_;
+
+ DISALLOW_COPY_AND_ASSIGN(NetworkChangeNotifier);
+};
+
+} // namespace android
+} // namespace net
+
+#endif // NET_ANDROID_NETWORK_CHANGE_NOTIFIER_ANDROID_H_
diff --git a/net/android/network_change_notifier_factory.cc b/net/android/network_change_notifier_factory.cc
new file mode 100644
index 0000000..0bff8b0
--- /dev/null
+++ b/net/android/network_change_notifier_factory.cc
@@ -0,0 +1,19 @@
+// 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.
+
+#include "net/android/network_change_notifier_factory.h"
+
+#include "net/android/network_change_notifier.h"
+
+namespace net {
+namespace android {
+
+NetworkChangeNotifierFactory::NetworkChangeNotifierFactory() {}
+
+net::NetworkChangeNotifier* NetworkChangeNotifierFactory::CreateInstance() {
+ return new NetworkChangeNotifier();
+}
+
+} // namespace android
+} // namespace net
diff --git a/net/android/network_change_notifier_factory.h b/net/android/network_change_notifier_factory.h
new file mode 100644
index 0000000..db51aa2
--- /dev/null
+++ b/net/android/network_change_notifier_factory.h
@@ -0,0 +1,31 @@
+// 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.
+
+#ifndef NET_ANDROID_NETWORK_CHANGE_NOTIFIER_FACTORY_H_
+#define NET_ANDROID_NETWORK_CHANGE_NOTIFIER_FACTORY_H_
+#pragma once
+
+#include "base/compiler_specific.h"
+#include "net/base/network_change_notifier_factory.h"
+
+namespace net {
+
+class NetworkChangeNotifier;
+
+namespace android {
+
+// NetworkChangeNotifierFactory creates Android-specific specialization of
+// NetworkChangeNotifier.
+class NetworkChangeNotifierFactory : public net::NetworkChangeNotifierFactory {
+ public:
+ NetworkChangeNotifierFactory();
+
+ // Overrides of net::NetworkChangeNotifierFactory.
+ virtual net::NetworkChangeNotifier* CreateInstance() OVERRIDE;
+};
+
+} // namespace android
+} // namespace net
+
+#endif // NET_ANDROID_NETWORK_CHANGE_NOTIFIER_FACTORY_H_