Chromium Code Reviews| Index: device/battery/android/java/src/org/chromium/device/battery/BatteryMonitorFactory.java |
| diff --git a/device/battery/android/java/src/org/chromium/device/battery/BatteryMonitorFactory.java b/device/battery/android/java/src/org/chromium/device/battery/BatteryMonitorFactory.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8e636b9ae8247bee8c645b3ea278aa5849b27738 |
| --- /dev/null |
| +++ b/device/battery/android/java/src/org/chromium/device/battery/BatteryMonitorFactory.java |
| @@ -0,0 +1,74 @@ |
| +// 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 android.content.Context; |
| +import android.util.Log; |
| + |
| +import org.chromium.base.ApplicationStatus; |
| +import org.chromium.base.ThreadUtils; |
| +import org.chromium.device.battery.BatteryStatusManager.BatteryStatusCallback; |
| +import org.chromium.mojom.device.BatteryMonitor; |
| +import org.chromium.mojom.device.BatteryStatus; |
| + |
| +import java.util.HashSet; |
| + |
| +/** |
| + * Factory that creates instances of BatteryMonitor implementations and notifies them about battery |
| + * status changes. |
| + */ |
| +public class BatteryMonitorFactory { |
|
qsr
2014/12/12 15:31:57
Can this class be merged into BatteryStatusManager
ppi
2014/12/12 16:02:16
I feel that a combined class would be harder to re
qsr
2014/12/12 16:49:08
Acknowledged.
|
| + static final String TAG = "BatteryMonitorFactory"; |
| + |
| + // Backing source of battery information. |
| + private final BatteryStatusManager mManager; |
| + // Monitors currently interested in the battery status notifications. |
| + private final HashSet<BatteryMonitorImpl> mSubscribedMonitors = |
| + new HashSet<BatteryMonitorImpl>(); |
| + |
| + private final BatteryStatusCallback mCallback = new BatteryStatusCallback() { |
| + @Override |
| + public void onBatteryStatusChanged(boolean charging, double chargingTime, |
| + double dischargingTime, double level) { |
| + ThreadUtils.assertOnUiThread(); |
| + |
| + BatteryStatus batteryStatus = new BatteryStatus(); |
| + batteryStatus.charging = charging; |
| + batteryStatus.chargingTime = chargingTime; |
| + batteryStatus.dischargingTime = dischargingTime; |
| + batteryStatus.level = level; |
| + for (BatteryMonitorImpl monitor : mSubscribedMonitors) { |
| + monitor.didChange(batteryStatus); |
| + } |
| + } |
| + }; |
| + |
| + public BatteryMonitorFactory() { |
| + Context applicationContext = ApplicationStatus.getApplicationContext(); |
| + assert applicationContext != null; |
| + mManager = new BatteryStatusManager(applicationContext, mCallback); |
| + } |
| + |
| + public BatteryMonitor createMonitor() { |
| + ThreadUtils.assertOnUiThread(); |
| + |
| + if (mSubscribedMonitors.isEmpty() && !mManager.start()) { |
| + Log.e(TAG, "BatteryStatusManager failed to start."); |
| + } |
| + BatteryMonitorImpl monitor = new BatteryMonitorImpl(this); |
| + mSubscribedMonitors.add(monitor); |
| + return monitor; |
| + } |
| + |
| + void unsubscribe(BatteryMonitorImpl monitor) { |
| + ThreadUtils.assertOnUiThread(); |
| + |
| + assert mSubscribedMonitors.contains(monitor); |
| + mSubscribedMonitors.remove(monitor); |
| + if (mSubscribedMonitors.isEmpty()) { |
| + mManager.stop(); |
| + } |
| + } |
| +} |