Index: base/android/java/src/org/chromium/base/SystemMonitor.java |
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 0000000000000000000000000000000000000000..72a4fb870c7019a1ba041f37d55496a51ee7c762 |
--- /dev/null |
+++ b/base/android/java/src/org/chromium/base/SystemMonitor.java |
@@ -0,0 +1,51 @@ |
+// 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; |
+ |
+ public static void create(Context context) { |
+ // Can only be called on the UI thread. |
+ assert Looper.myLooper() == Looper.getMainLooper(); |
+ 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 status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); |
+ boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || |
+ status == BatteryManager.BATTERY_STATUS_FULL; |
+ nativeOnBatteryChargingChanged(isCharging); |
+ } |
+ |
+ private static native void nativeOnBatteryChargingChanged(boolean isCharging); |
+ |
+} |