summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorbulach@chromium.org <bulach@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-10-16 11:58:08 +0000
committerbulach@chromium.org <bulach@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-10-16 11:58:08 +0000
commita614f15662de81caafbf02c7afc6beb0026fb827 (patch)
tree3ab396cb558b73c1719c08a7946414a8972056dc /base
parent8a429d0f0a8300776fc65eeec0831c3b3840a5a3 (diff)
downloadchromium_src-a614f15662de81caafbf02c7afc6beb0026fb827.zip
chromium_src-a614f15662de81caafbf02c7afc6beb0026fb827.tar.gz
chromium_src-a614f15662de81caafbf02c7afc6beb0026fb827.tar.bz2
Android: Integrates native and java SystemMonitor.
BUG=154293 Review URL: https://chromiumcodereview.appspot.com/11027024 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@162124 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/android/base_jni_registrar.cc4
-rw-r--r--base/android/java/src/org/chromium/base/PowerStatusReceiver.java23
-rw-r--r--base/android/java/src/org/chromium/base/SystemMonitor.java64
-rw-r--r--base/base.gyp1
-rw-r--r--base/base.gypi1
-rw-r--r--base/system_monitor/system_monitor_android.cc19
-rw-r--r--base/system_monitor/system_monitor_android.h17
7 files changed, 126 insertions, 3 deletions
diff --git a/base/android/base_jni_registrar.cc b/base/android/base_jni_registrar.cc
index df490db..fb93217 100644
--- a/base/android/base_jni_registrar.cc
+++ b/base/android/base_jni_registrar.cc
@@ -5,13 +5,14 @@
#include "base/android/base_jni_registrar.h"
#include "base/basictypes.h"
-#include "base/message_pump_android.h"
#include "base/android/build_info.h"
#include "base/android/jni_android.h"
#include "base/android/jni_registrar.h"
#include "base/android/locale_utils.h"
#include "base/android/path_service_android.h"
#include "base/android/path_utils.h"
+#include "base/message_pump_android.h"
+#include "base/system_monitor/system_monitor_android.h"
namespace base {
namespace android {
@@ -22,6 +23,7 @@ static RegistrationMethod kBaseRegisteredMethods[] = {
{ "PathService", base::android::RegisterPathService },
{ "PathUtils", base::android::RegisterPathUtils },
{ "SystemMessageHandler", base::MessagePumpForUI::RegisterBindings },
+ { "SystemMonitor", base::RegisterSystemMonitor },
};
bool RegisterJni(JNIEnv* env) {
diff --git a/base/android/java/src/org/chromium/base/PowerStatusReceiver.java b/base/android/java/src/org/chromium/base/PowerStatusReceiver.java
new file mode 100644
index 0000000..89594b8
--- /dev/null
+++ b/base/android/java/src/org/chromium/base/PowerStatusReceiver.java
@@ -0,0 +1,23 @@
+// 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.base;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+
+
+/**
+ * A BroadcastReceiver that listens to changes in power status and notifies
+ * SystemMonitor.
+ * It's instantiated by the framework via the application intent-filter
+ * declared in its manifest.
+ */
+public class PowerStatusReceiver extends BroadcastReceiver {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ SystemMonitor.onBatteryChargingChanged(intent);
+ }
+}
diff --git a/base/android/java/src/org/chromium/base/SystemMonitor.java b/base/android/java/src/org/chromium/base/SystemMonitor.java
new file mode 100644
index 0000000..f43f115
--- /dev/null
+++ b/base/android/java/src/org/chromium/base/SystemMonitor.java
@@ -0,0 +1,64 @@
+// 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.base;
+
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.os.BatteryManager;
+import android.os.Looper;
+
+
+/**
+ * Integrates native SystemMonitor with the java side.
+ */
+@JNINamespace("base::android")
+public class SystemMonitor {
+ private static SystemMonitor sInstance;
+
+ private boolean mIsBatteryPower;
+
+ public static void createForTests(Context context) {
+ // Applications will create this once the
+ // JNI side has been fully wired up both sides.
+ // For tests, we just need native -> java, that is,
+ // we don't need to notify java -> native on creation.
+ sInstance = new SystemMonitor();
+ }
+
+ public static void create(Context context) {
+ if (sInstance == null) {
+ sInstance = new SystemMonitor();
+ IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
+ Intent batteryStatusIntent = context.registerReceiver(null, ifilter);
+ onBatteryChargingChanged(batteryStatusIntent);
+ }
+ }
+
+ private SystemMonitor() {
+ }
+
+ public static void onBatteryChargingChanged(Intent intent) {
+ if (sInstance == null) {
+ // We may be called by the framework intent-filter before being
+ // fully initialized. This is not a problem, since our constructor
+ // will check for the state later on.
+ return;
+ }
+ int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
+ // If we're not plugged, assume we're running on battery power.
+ sInstance.mIsBatteryPower = chargePlug != BatteryManager.BATTERY_PLUGGED_USB &&
+ chargePlug != BatteryManager.BATTERY_PLUGGED_AC;
+ nativeOnBatteryChargingChanged();
+ }
+
+ @CalledByNative
+ private static boolean isBatteryPower() {
+ return sInstance.mIsBatteryPower;
+ }
+
+ private static native void nativeOnBatteryChargingChanged();
+
+}
diff --git a/base/base.gyp b/base/base.gyp
index eb586e8..bf596fd 100644
--- a/base/base.gyp
+++ b/base/base.gyp
@@ -968,6 +968,7 @@
'android/java/src/org/chromium/base/PathService.java',
'android/java/src/org/chromium/base/PathUtils.java',
'android/java/src/org/chromium/base/SystemMessageHandler.java',
+ 'android/java/src/org/chromium/base/SystemMonitor.java',
],
'variables': {
'jni_gen_dir': 'base',
diff --git a/base/base.gypi b/base/base.gypi
index 8bd90a1..a1e005e 100644
--- a/base/base.gypi
+++ b/base/base.gypi
@@ -391,6 +391,7 @@
'system_monitor/system_monitor.cc',
'system_monitor/system_monitor.h',
'system_monitor/system_monitor_android.cc',
+ 'system_monitor/system_monitor_android.h',
'system_monitor/system_monitor_ios.mm',
'system_monitor/system_monitor_mac.mm',
'system_monitor/system_monitor_posix.cc',
diff --git a/base/system_monitor/system_monitor_android.cc b/base/system_monitor/system_monitor_android.cc
index c86076d..4e1a16b 100644
--- a/base/system_monitor/system_monitor_android.cc
+++ b/base/system_monitor/system_monitor_android.cc
@@ -3,12 +3,27 @@
// found in the LICENSE file.
#include "base/system_monitor/system_monitor.h"
+#include "jni/SystemMonitor_jni.h"
namespace base {
+namespace android {
+
+// Native implementation of SystemMonitor.java.
+void OnBatteryChargingChanged(JNIEnv* env,
+ jclass clazz) {
+ SystemMonitor::Get()->ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT);
+}
+
+} // namespace android
+
bool SystemMonitor::IsBatteryPower() {
- NOTIMPLEMENTED();
- return true;
+ JNIEnv* env = base::android::AttachCurrentThread();
+ return base::android::Java_SystemMonitor_isBatteryPower(env);
+}
+
+bool RegisterSystemMonitor(JNIEnv* env) {
+ return base::android::RegisterNativesImpl(env);
}
} // namespace base
diff --git a/base/system_monitor/system_monitor_android.h b/base/system_monitor/system_monitor_android.h
new file mode 100644
index 0000000..9f60560
--- /dev/null
+++ b/base/system_monitor/system_monitor_android.h
@@ -0,0 +1,17 @@
+// 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 BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_ANDROID_H_
+#define BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_ANDROID_H_
+
+#include <jni.h>
+
+namespace base {
+
+// Registers the JNI bindings for SystemMonitor.
+bool RegisterSystemMonitor(JNIEnv* env);
+
+} // namespace base
+
+#endif // BASE_SYSTEM_MONITOR_SYSTEM_MONITOR_ANDROID_H_