Add power save action to BatteryBroadcastReceiver
This cl makes BatteryBroadcastReceiver also listen to update about battery saver. Bug: 70530651 Test: RunSettingsRoboTests Change-Id: I76b2f1b1047aa195ee9d8ff2a8a330cea31039d4
This commit is contained in:
@@ -20,16 +20,18 @@ import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.PowerManager;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.settings.Utils;
|
||||
|
||||
/**
|
||||
* Use this broadcastReceiver to listen to the battery change, and it will invoke
|
||||
* {@link OnBatteryChangedListener} if any of the following happens:
|
||||
* {@link OnBatteryChangedListener} if any of the followings has been changed:
|
||||
*
|
||||
* 1. Battery level has been changed
|
||||
* 2. Battery status has been changed
|
||||
* 1. Battery level(e.g. 100%->99%)
|
||||
* 2. Battery status(e.g. plugged->unplugged)
|
||||
* 3. Battery saver(e.g. off->on)
|
||||
*/
|
||||
public class BatteryBroadcastReceiver extends BroadcastReceiver {
|
||||
|
||||
@@ -58,8 +60,11 @@ public class BatteryBroadcastReceiver extends BroadcastReceiver {
|
||||
}
|
||||
|
||||
public void register() {
|
||||
final Intent intent = mContext.registerReceiver(this,
|
||||
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
final IntentFilter intentFilter = new IntentFilter();
|
||||
intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
|
||||
intentFilter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);
|
||||
|
||||
final Intent intent = mContext.registerReceiver(this, intentFilter);
|
||||
updateBatteryStatus(intent, true /* forceUpdate */);
|
||||
}
|
||||
|
||||
@@ -68,10 +73,10 @@ public class BatteryBroadcastReceiver extends BroadcastReceiver {
|
||||
}
|
||||
|
||||
private void updateBatteryStatus(Intent intent, boolean forceUpdate) {
|
||||
if (intent != null && mBatteryListener != null && Intent.ACTION_BATTERY_CHANGED.equals(
|
||||
intent.getAction())) {
|
||||
String batteryLevel = Utils.getBatteryPercentage(intent);
|
||||
String batteryStatus = Utils.getBatteryStatus(
|
||||
if (intent != null && mBatteryListener != null) {
|
||||
if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
|
||||
final String batteryLevel = Utils.getBatteryPercentage(intent);
|
||||
final String batteryStatus = Utils.getBatteryStatus(
|
||||
mContext.getResources(), intent);
|
||||
if (forceUpdate || !batteryLevel.equals(mBatteryLevel) || !batteryStatus.equals(
|
||||
mBatteryStatus)) {
|
||||
@@ -79,6 +84,9 @@ public class BatteryBroadcastReceiver extends BroadcastReceiver {
|
||||
mBatteryStatus = batteryStatus;
|
||||
mBatteryListener.onBatteryChanged();
|
||||
}
|
||||
} else if (PowerManager.ACTION_POWER_SAVE_MODE_CHANGED.equals(intent.getAction())) {
|
||||
mBatteryListener.onBatteryChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -18,6 +18,7 @@ package com.android.settings.fuelgauge;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.BatteryManager;
|
||||
import android.os.PowerManager;
|
||||
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.TestConfig;
|
||||
@@ -82,6 +83,14 @@ public class BatteryBroadcastReceiverTest {
|
||||
verify(mBatteryListener).onBatteryChanged();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnReceive_powerSaveModeChanged_listenerInvoked() {
|
||||
mBatteryBroadcastReceiver.onReceive(mContext,
|
||||
new Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED));
|
||||
|
||||
verify(mBatteryListener).onBatteryChanged();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnReceive_batteryDataNotChanged_listenerNotInvoked() {
|
||||
final String batteryLevel = Utils.getBatteryPercentage(mChargingIntent);
|
||||
|
Reference in New Issue
Block a user