Commit 6ee8cc80 authored by Ryan Sturm's avatar Ryan Sturm Committed by Commit Bot

Adding battery status updates for new observers for Android

This CL adds parity between Android and Desktop for new battery
monitors. Currently, the second observer that is added for battery will
only receive new updates. This cl pushes the latest battery status
update to the new observer.

Bug: 999063
Change-Id: I6cfa7eeb6a355d03f2b1f0d9e8b7a73ffd8452e3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1829023Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Commit-Queue: Ryan Sturm <ryansturm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#700827}
parent 3b5f8efa
...@@ -27,12 +27,18 @@ public class BatteryMonitorFactory implements InterfaceFactory<BatteryMonitor> { ...@@ -27,12 +27,18 @@ public class BatteryMonitorFactory implements InterfaceFactory<BatteryMonitor> {
// Monitors currently interested in the battery status notifications. // Monitors currently interested in the battery status notifications.
private final HashSet<BatteryMonitorImpl> mSubscribedMonitors = private final HashSet<BatteryMonitorImpl> mSubscribedMonitors =
new HashSet<BatteryMonitorImpl>(); new HashSet<BatteryMonitorImpl>();
// Tracks the latest battery status update for newly added observers.
private boolean mHasStatusUpdate;
private BatteryStatus mBatteryStatus;
private final BatteryStatusCallback mCallback = new BatteryStatusCallback() { private final BatteryStatusCallback mCallback = new BatteryStatusCallback() {
@Override @Override
public void onBatteryStatusChanged(BatteryStatus batteryStatus) { public void onBatteryStatusChanged(BatteryStatus batteryStatus) {
ThreadUtils.assertOnUiThread(); ThreadUtils.assertOnUiThread();
mHasStatusUpdate = true;
mBatteryStatus = batteryStatus;
List<BatteryMonitorImpl> monitors = new ArrayList<>(mSubscribedMonitors); List<BatteryMonitorImpl> monitors = new ArrayList<>(mSubscribedMonitors);
for (BatteryMonitorImpl monitor : monitors) { for (BatteryMonitorImpl monitor : monitors) {
monitor.didChange(batteryStatus); monitor.didChange(batteryStatus);
...@@ -41,6 +47,7 @@ public class BatteryMonitorFactory implements InterfaceFactory<BatteryMonitor> { ...@@ -41,6 +47,7 @@ public class BatteryMonitorFactory implements InterfaceFactory<BatteryMonitor> {
}; };
public BatteryMonitorFactory() { public BatteryMonitorFactory() {
mHasStatusUpdate = false;
mManager = new BatteryStatusManager(mCallback); mManager = new BatteryStatusManager(mCallback);
} }
...@@ -55,6 +62,10 @@ public class BatteryMonitorFactory implements InterfaceFactory<BatteryMonitor> { ...@@ -55,6 +62,10 @@ public class BatteryMonitorFactory implements InterfaceFactory<BatteryMonitor> {
// for UMA - http://crbug.com/442300. // for UMA - http://crbug.com/442300.
BatteryMonitorImpl monitor = new BatteryMonitorImpl(this); BatteryMonitorImpl monitor = new BatteryMonitorImpl(this);
if (mHasStatusUpdate) {
monitor.didChange(mBatteryStatus);
}
mSubscribedMonitors.add(monitor); mSubscribedMonitors.add(monitor);
return monitor; return monitor;
} }
...@@ -66,6 +77,7 @@ public class BatteryMonitorFactory implements InterfaceFactory<BatteryMonitor> { ...@@ -66,6 +77,7 @@ public class BatteryMonitorFactory implements InterfaceFactory<BatteryMonitor> {
mSubscribedMonitors.remove(monitor); mSubscribedMonitors.remove(monitor);
if (mSubscribedMonitors.isEmpty()) { if (mSubscribedMonitors.isEmpty()) {
mManager.stop(); mManager.stop();
mHasStatusUpdate = false;
} }
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment