Index: device/battery/android/java/src/org/chromium/device/battery/BatteryMonitorImpl.java |
diff --git a/device/battery/android/java/src/org/chromium/device/battery/BatteryMonitorImpl.java b/device/battery/android/java/src/org/chromium/device/battery/BatteryMonitorImpl.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e44158912644a43af4c2c4aaeb1f88e6cd139517 |
--- /dev/null |
+++ b/device/battery/android/java/src/org/chromium/device/battery/BatteryMonitorImpl.java |
@@ -0,0 +1,58 @@ |
+// Copyright 2014 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.device.battery; |
+ |
+import org.chromium.mojo.system.MojoException; |
+import org.chromium.mojom.device.BatteryMonitor; |
+import org.chromium.mojom.device.BatteryStatus; |
+import org.chromium.mojom.device.BatteryStatusObserver; |
+ |
+/** |
+ * Android implementation of the battery monitor service defined in |
+ * device/battery/battery_monitor.mojom. |
+ */ |
+public class BatteryMonitorImpl implements BatteryMonitor { |
+ // Factory that created this instance and notifies it about battery status changes. |
+ private final BatteryMonitorFactory mFactory; |
+ private boolean mSubscribed; |
+ |
+ private BatteryStatusObserver mClient; |
+ |
+ public BatteryMonitorImpl(BatteryMonitorFactory batteryMonitorFactory) { |
+ mFactory = batteryMonitorFactory; |
+ mSubscribed = true; |
+ } |
+ |
+ private void unsubscribe() { |
+ if (mSubscribed) { |
+ mFactory.unsubscribe(this); |
+ mSubscribed = false; |
+ } |
+ } |
+ |
+ @Override |
+ public void setClient(BatteryStatusObserver client) { |
+ mClient = client; |
+ } |
+ |
+ @Override |
+ public void close() { |
+ unsubscribe(); |
+ } |
+ |
+ @Override |
+ public void onConnectionError(MojoException e) { |
+ unsubscribe(); |
+ } |
+ |
+ /** |
+ * Notifies the client passing the given battery status information. |
+ */ |
+ void didChange(BatteryStatus batteryStatus) { |
+ if (mClient != null) { |
+ mClient.didChange(batteryStatus); |
+ } |
+ }; |
+} |