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.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
|
import android.os.PowerManager;
|
||||||
import android.support.annotation.VisibleForTesting;
|
import android.support.annotation.VisibleForTesting;
|
||||||
|
|
||||||
import com.android.settings.Utils;
|
import com.android.settings.Utils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use this broadcastReceiver to listen to the battery change, and it will invoke
|
* 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
|
* 1. Battery level(e.g. 100%->99%)
|
||||||
* 2. Battery status has been changed
|
* 2. Battery status(e.g. plugged->unplugged)
|
||||||
|
* 3. Battery saver(e.g. off->on)
|
||||||
*/
|
*/
|
||||||
public class BatteryBroadcastReceiver extends BroadcastReceiver {
|
public class BatteryBroadcastReceiver extends BroadcastReceiver {
|
||||||
|
|
||||||
@@ -58,8 +60,11 @@ public class BatteryBroadcastReceiver extends BroadcastReceiver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void register() {
|
public void register() {
|
||||||
final Intent intent = mContext.registerReceiver(this,
|
final IntentFilter intentFilter = new IntentFilter();
|
||||||
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
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 */);
|
updateBatteryStatus(intent, true /* forceUpdate */);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,15 +73,18 @@ public class BatteryBroadcastReceiver extends BroadcastReceiver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void updateBatteryStatus(Intent intent, boolean forceUpdate) {
|
private void updateBatteryStatus(Intent intent, boolean forceUpdate) {
|
||||||
if (intent != null && mBatteryListener != null && Intent.ACTION_BATTERY_CHANGED.equals(
|
if (intent != null && mBatteryListener != null) {
|
||||||
intent.getAction())) {
|
if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
|
||||||
String batteryLevel = Utils.getBatteryPercentage(intent);
|
final String batteryLevel = Utils.getBatteryPercentage(intent);
|
||||||
String batteryStatus = Utils.getBatteryStatus(
|
final String batteryStatus = Utils.getBatteryStatus(
|
||||||
mContext.getResources(), intent);
|
mContext.getResources(), intent);
|
||||||
if (forceUpdate || !batteryLevel.equals(mBatteryLevel) || !batteryStatus.equals(
|
if (forceUpdate || !batteryLevel.equals(mBatteryLevel) || !batteryStatus.equals(
|
||||||
mBatteryStatus)) {
|
mBatteryStatus)) {
|
||||||
mBatteryLevel = batteryLevel;
|
mBatteryLevel = batteryLevel;
|
||||||
mBatteryStatus = batteryStatus;
|
mBatteryStatus = batteryStatus;
|
||||||
|
mBatteryListener.onBatteryChanged();
|
||||||
|
}
|
||||||
|
} else if (PowerManager.ACTION_POWER_SAVE_MODE_CHANGED.equals(intent.getAction())) {
|
||||||
mBatteryListener.onBatteryChanged();
|
mBatteryListener.onBatteryChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -18,6 +18,7 @@ package com.android.settings.fuelgauge;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.BatteryManager;
|
import android.os.BatteryManager;
|
||||||
|
import android.os.PowerManager;
|
||||||
|
|
||||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
import com.android.settings.TestConfig;
|
import com.android.settings.TestConfig;
|
||||||
@@ -82,6 +83,14 @@ public class BatteryBroadcastReceiverTest {
|
|||||||
verify(mBatteryListener).onBatteryChanged();
|
verify(mBatteryListener).onBatteryChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOnReceive_powerSaveModeChanged_listenerInvoked() {
|
||||||
|
mBatteryBroadcastReceiver.onReceive(mContext,
|
||||||
|
new Intent(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED));
|
||||||
|
|
||||||
|
verify(mBatteryListener).onBatteryChanged();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOnReceive_batteryDataNotChanged_listenerNotInvoked() {
|
public void testOnReceive_batteryDataNotChanged_listenerNotInvoked() {
|
||||||
final String batteryLevel = Utils.getBatteryPercentage(mChargingIntent);
|
final String batteryLevel = Utils.getBatteryPercentage(mChargingIntent);
|
||||||
|
Reference in New Issue
Block a user