Merge "Call detect anomaly in period job only when there is new battery usage data." into main

This commit is contained in:
Treehugger Robot
2023-09-20 09:56:12 +00:00
committed by Android (Google) Code Review
3 changed files with 14 additions and 17 deletions

View File

@@ -23,8 +23,6 @@ import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.LowBatteryTip;
import java.util.concurrent.TimeUnit;
/**
* Detect whether the battery is too low
*/
@@ -46,9 +44,7 @@ public class LowBatteryDetector implements BatteryTipDetector {
@Override
public BatteryTip detect() {
final boolean lowBattery = mBatteryInfo.batteryLevel <= mWarningLevel
|| (mBatteryInfo.discharging && mBatteryInfo.remainingTimeUs != 0
&& mBatteryInfo.remainingTimeUs < TimeUnit.HOURS.toMicros(mPolicy.lowBatteryHour));
final boolean lowBattery = mBatteryInfo.batteryLevel <= mWarningLevel;
final boolean lowBatteryEnabled = mPolicy.lowBatteryEnabled && !mIsPowerSaveMode;
final boolean dischargingLowBatteryState =
mPolicy.testLowBatteryTip || (mBatteryInfo.discharging && lowBattery);

View File

@@ -116,8 +116,16 @@ public final class BatteryUsageDataLoader {
final Handler handler = new Handler(Looper.getMainLooper());
final BatteryLevelData batteryLevelData = DataProcessManager.getBatteryLevelData(
context, handler, /*isFromPeriodJob=*/ true,
batteryDiffDataMap -> DatabaseUtils.sendBatteryUsageSlotData(context,
ConvertUtils.convertToBatteryUsageSlotList(batteryDiffDataMap)));
batteryDiffDataMap -> {
DatabaseUtils.sendBatteryUsageSlotData(context,
ConvertUtils.convertToBatteryUsageSlotList(batteryDiffDataMap));
if (batteryDiffDataMap.values().stream().anyMatch(data ->
(!data.getAppDiffEntryList().isEmpty()
|| !data.getSystemDiffEntryList().isEmpty()))) {
FeatureFactory.getFeatureFactory().getPowerUsageFeatureProvider()
.detectSettingsAnomaly(context, /* displayDrain= */ 0);
}
});
if (batteryLevelData == null) {
Log.d(TAG, "preprocessBatteryUsageSlots() no new battery usage data.");
return;
@@ -139,8 +147,6 @@ public final class BatteryUsageDataLoader {
// No app usage data or battery diff data at this time.
loadAppUsageData(context);
preprocessBatteryUsageSlots(context);
FeatureFactory.getFeatureFactory().getPowerUsageFeatureProvider()
.detectSettingsAnomaly(context, /* displayDrain= */ 0);
}
Log.d(TAG, String.format(
"loadUsageDataSafely() in %d/ms", System.currentTimeMillis() - start));

View File

@@ -53,7 +53,6 @@ public class LowBatteryDetectorTest {
mPolicy = spy(new BatteryTipPolicy(RuntimeEnvironment.application));
mContext = RuntimeEnvironment.application;
ReflectionHelpers.setField(mPolicy, "lowBatteryEnabled", true);
ReflectionHelpers.setField(mPolicy, "lowBatteryHour", 3);
mBatteryInfo.discharging = true;
mLowBatteryDetector = new LowBatteryDetector(mContext, mPolicy, mBatteryInfo,
@@ -78,13 +77,9 @@ public class LowBatteryDetectorTest {
@Test
public void testDetect_lowBattery_tipNew() {
mBatteryInfo.batteryLevel = 3;
mBatteryInfo.batteryLevel = 20;
mBatteryInfo.remainingTimeUs = TimeUnit.DAYS.toMillis(1);
assertThat(mLowBatteryDetector.detect().getState()).isEqualTo(BatteryTip.StateType.NEW);
mBatteryInfo.batteryLevel = 50;
mBatteryInfo.remainingTimeUs = TimeUnit.MINUTES.toMillis(1);
assertThat(mLowBatteryDetector.detect().getState()).isEqualTo(BatteryTip.StateType.NEW);
}
@Test
@@ -104,9 +99,9 @@ public class LowBatteryDetectorTest {
}
@Test
public void testDetect_timeEstimationZero_tipInvisible() {
public void testDetect_lowTimeEstimation_tipInvisible() {
mBatteryInfo.batteryLevel = 50;
mBatteryInfo.remainingTimeUs = 0;
mBatteryInfo.remainingTimeUs = TimeUnit.MINUTES.toMillis(1);
assertThat(mLowBatteryDetector.detect().isVisible()).isFalse();
}