Refactor processBatteryDiffData() from DataProcessor to BatteryDiffData class.

So that only hourly battery diff data needs purgeFakeAndHiddenPackages()
and combineBatteryDiffEntry(). This is also easy to set hourly threshold
in the next cl.

Bug: 264840285
Test: manual
Change-Id: Ie0bc6d53f96285285019dd83c1f39305eca79c71
This commit is contained in:
Zaiyue Xue
2023-01-09 17:04:57 +08:00
parent 7f3ff17bcf
commit f080429ddb
6 changed files with 387 additions and 276 deletions

View File

@@ -0,0 +1,176 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.fuelgauge.batteryusage;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import com.android.settingslib.applications.ApplicationsState;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
public class BatteryDiffDataTest {
private Context mContext;
@Mock private ApplicationsState mApplicationsState;
@Mock private ApplicationsState.AppEntry mAppEntry;
@Mock private ApplicationInfo mApplicationInfo;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
}
@Test
public void needsCombineInSystemApp_isHidden_returnTrue() {
final int currentUserId = mContext.getUserId();
final BatteryHistEntry hiddenHistEntry = createBatteryHistEntry(
ConvertUtils.FAKE_PACKAGE_NAME, "fake_label", /*consumePower=*/ 0,
/*foregroundUsageConsumePower=*/ 0, /*foregroundServiceUsageConsumePower=*/ 0,
/*backgroundUsageConsumePower=*/ 0, /*cachedUsageConsumePower=*/ 0,
/*uid=*/ 0L, currentUserId, ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
/*foregroundUsageTimeInMs=*/ 0L, /*backgroundUsageTimeInMs=*/ 0L, true);
final BatteryDiffEntry hiddenDiffEntry = new BatteryDiffEntry(
mContext,
/*foregroundUsageTimeInMs=*/ 0,
/*backgroundUsageTimeInMs=*/ 0,
/*screenOnTimeInMs=*/ 0,
/*consumePower=*/ 0,
/*foregroundUsageConsumePower=*/ 0,
/*foregroundServiceUsageConsumePower=*/ 0,
/*backgroundUsageConsumePower=*/ 0,
/*cachedUsageConsumePower=*/ 0,
hiddenHistEntry);
boolean needsCombineInSystemApp = BatteryDiffData.needsCombineInSystemApp(
hiddenDiffEntry, List.of(), mApplicationsState);
assertThat(needsCombineInSystemApp).isTrue();
}
@Test
public void needsCombineInSystemApp_isSystemApp_returnTrue() {
final int currentUserId = mContext.getUserId();
final BatteryHistEntry batteryHistEntry = createBatteryHistEntry(
ConvertUtils.FAKE_PACKAGE_NAME, "fake_label", /*consumePower=*/ 0,
/*foregroundUsageConsumePower=*/ 0, /*foregroundServiceUsageConsumePower=*/ 0,
/*backgroundUsageConsumePower=*/ 0, /*cachedUsageConsumePower=*/ 0,
/*uid=*/ 0L, currentUserId, ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
/*foregroundUsageTimeInMs=*/ 0L, /*backgroundUsageTimeInMs=*/ 0L, false);
final BatteryDiffEntry batteryDiffEntry = new BatteryDiffEntry(
mContext,
/*foregroundUsageTimeInMs=*/ 0,
/*backgroundUsageTimeInMs=*/ 0,
/*screenOnTimeInMs=*/ 0,
/*consumePower=*/ 0,
/*foregroundUsageConsumePower=*/ 0,
/*foregroundServiceUsageConsumePower=*/ 0,
/*backgroundUsageConsumePower=*/ 0,
/*cachedUsageConsumePower=*/ 0,
batteryHistEntry);
doReturn(mAppEntry).when(mApplicationsState).getEntry(anyString(), anyInt());
mAppEntry.info = mApplicationInfo;
mApplicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
boolean needsCombineInSystemApp = BatteryDiffData.needsCombineInSystemApp(
batteryDiffEntry, List.of(), mApplicationsState);
assertThat(needsCombineInSystemApp).isTrue();
}
@Test
public void needsCombineInSystemApp_notSystemApp_returnFalse() {
final int currentUserId = mContext.getUserId();
final BatteryHistEntry batteryHistEntry = createBatteryHistEntry(
ConvertUtils.FAKE_PACKAGE_NAME, "fake_label", /*consumePower=*/ 0,
/*foregroundUsageConsumePower=*/ 0, /*foregroundServiceUsageConsumePower=*/ 0,
/*backgroundUsageConsumePower=*/ 0, /*cachedUsageConsumePower=*/ 0,
/*uid=*/ 0L, currentUserId, ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
/*foregroundUsageTimeInMs=*/ 0L, /*backgroundUsageTimeInMs=*/ 0L, false);
final BatteryDiffEntry batteryDiffEntry = new BatteryDiffEntry(
mContext,
/*foregroundUsageTimeInMs=*/ 0,
/*backgroundUsageTimeInMs=*/ 0,
/*screenOnTimeInMs=*/ 0,
/*consumePower=*/ 0,
/*foregroundUsageConsumePower=*/ 0,
/*foregroundServiceUsageConsumePower=*/ 0,
/*backgroundUsageConsumePower=*/ 0,
/*cachedUsageConsumePower=*/ 0,
batteryHistEntry);
doReturn(mAppEntry).when(mApplicationsState).getEntry(anyString(), anyInt());
mAppEntry.info = mApplicationInfo;
mApplicationInfo.flags = 0;
boolean needsCombineInSystemApp = BatteryDiffData.needsCombineInSystemApp(
batteryDiffEntry, List.of(), mApplicationsState);
assertThat(needsCombineInSystemApp).isFalse();
}
private static BatteryHistEntry createBatteryHistEntry(
final String packageName, final String appLabel, final double consumePower,
final double foregroundUsageConsumePower,
final double foregroundServiceUsageConsumePower,
final double backgroundUsageConsumePower, final double cachedUsageConsumePower,
final long uid, final long userId, final int consumerType,
final long foregroundUsageTimeInMs, final long backgroundUsageTimeInMs,
final boolean isHidden) {
// Only insert required fields.
final BatteryInformation batteryInformation =
BatteryInformation
.newBuilder()
.setAppLabel(appLabel)
.setConsumePower(consumePower)
.setForegroundUsageConsumePower(foregroundUsageConsumePower)
.setForegroundServiceUsageConsumePower(foregroundServiceUsageConsumePower)
.setBackgroundUsageConsumePower(backgroundUsageConsumePower)
.setCachedUsageConsumePower(cachedUsageConsumePower)
.setForegroundUsageTimeInMs(foregroundUsageTimeInMs)
.setBackgroundUsageTimeInMs(backgroundUsageTimeInMs)
.setIsHidden(isHidden)
.build();
final ContentValues values = new ContentValues();
values.put(BatteryHistEntry.KEY_PACKAGE_NAME, packageName);
values.put(BatteryHistEntry.KEY_UID, uid);
values.put(BatteryHistEntry.KEY_USER_ID, userId);
values.put(BatteryHistEntry.KEY_CONSUMER_TYPE, consumerType);
values.put(BatteryHistEntry.KEY_BATTERY_INFORMATION,
ConvertUtils.convertBatteryInformationToString(batteryInformation));
return new BatteryHistEntry(values);
}
}

View File

@@ -105,10 +105,8 @@ public final class BatteryUsageBreakdownControllerTest {
/*cachedUsageConsumePower=*/ 0,
mBatteryHistEntry);
mBatteryDiffEntry = spy(mBatteryDiffEntry);
mBatteryUsageBreakdownController.mBatteryDiffData =
new BatteryDiffData(Arrays.asList(mBatteryDiffEntry), Arrays.asList());
mBatteryUsageBreakdownController.mBatteryDiffData.setTotalConsumePower();
mBatteryUsageBreakdownController.mBatteryDiffData.sortEntries();
mBatteryUsageBreakdownController.mBatteryDiffData = new BatteryDiffData(mContext,
Arrays.asList(mBatteryDiffEntry), Arrays.asList(), /* isAccumulated= */ false);
// Adds fake testing data.
BatteryDiffEntry.sResourceCache.put(
"fakeBatteryDiffEntryKey",

View File

@@ -21,7 +21,6 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.spy;
@@ -33,7 +32,6 @@ import android.app.usage.UsageEvents.Event;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.UserInfo;
import android.os.BatteryConsumer;
import android.os.BatteryManager;
@@ -45,7 +43,6 @@ import android.text.format.DateUtils;
import com.android.settings.fuelgauge.PowerUsageFeatureProvider;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.applications.ApplicationsState;
import org.junit.Before;
import org.junit.Test;
@@ -78,9 +75,6 @@ public final class DataProcessorTest {
@Mock private BatteryUsageStats mBatteryUsageStats;
@Mock private UserManager mUserManager;
@Mock private IUsageStatsManager mUsageStatsManager;
@Mock private ApplicationsState mApplicationsState;
@Mock private ApplicationsState.AppEntry mAppEntry;
@Mock private ApplicationInfo mApplicationInfo;
@Mock private BatteryEntry mMockBatteryEntry1;
@Mock private BatteryEntry mMockBatteryEntry2;
@Mock private BatteryEntry mMockBatteryEntry3;
@@ -1403,8 +1397,6 @@ public final class DataProcessorTest {
final BatteryDiffData batteryDiffData = DataProcessor.generateBatteryDiffData(mContext,
DataProcessor.convertToBatteryHistEntry(batteryEntryList, mBatteryUsageStats));
batteryDiffData.setTotalConsumePower();
batteryDiffData.sortEntries();
assertBatteryDiffEntry(
batteryDiffData.getAppDiffEntryList().get(0), 0, /*uid=*/ 2L,
@@ -1611,93 +1603,6 @@ public final class DataProcessorTest {
assertThat(DataProcessor.getScreenOnTime(appUsageMap, userId, packageName)).isEqualTo(0);
}
@Test
public void needsCombineInSystemApp_isHidden_returnTrue() {
final int currentUserId = mContext.getUserId();
final BatteryHistEntry hiddenHistEntry = createBatteryHistEntry(
ConvertUtils.FAKE_PACKAGE_NAME, "fake_label", /*consumePower=*/ 0,
/*foregroundUsageConsumePower=*/ 0, /*foregroundServiceUsageConsumePower=*/ 0,
/*backgroundUsageConsumePower=*/ 0, /*cachedUsageConsumePower=*/ 0,
/*uid=*/ 0L, currentUserId, ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
/*foregroundUsageTimeInMs=*/ 0L, /*backgroundUsageTimeInMs=*/ 0L, true);
final BatteryDiffEntry hiddenDiffEntry = new BatteryDiffEntry(
mContext,
/*foregroundUsageTimeInMs=*/ 0,
/*backgroundUsageTimeInMs=*/ 0,
/*screenOnTimeInMs=*/ 0,
/*consumePower=*/ 0,
/*foregroundUsageConsumePower=*/ 0,
/*foregroundServiceUsageConsumePower=*/ 0,
/*backgroundUsageConsumePower=*/ 0,
/*cachedUsageConsumePower=*/ 0,
hiddenHistEntry);
boolean needsCombineInSystemApp = DataProcessor.needsCombineInSystemApp(
hiddenDiffEntry, List.of(), mApplicationsState);
assertThat(needsCombineInSystemApp).isTrue();
}
@Test
public void needsCombineInSystemApp_isSystemApp_returnTrue() {
final int currentUserId = mContext.getUserId();
final BatteryHistEntry batteryHistEntry = createBatteryHistEntry(
ConvertUtils.FAKE_PACKAGE_NAME, "fake_label", /*consumePower=*/ 0,
/*foregroundUsageConsumePower=*/ 0, /*foregroundServiceUsageConsumePower=*/ 0,
/*backgroundUsageConsumePower=*/ 0, /*cachedUsageConsumePower=*/ 0,
/*uid=*/ 0L, currentUserId, ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
/*foregroundUsageTimeInMs=*/ 0L, /*backgroundUsageTimeInMs=*/ 0L, false);
final BatteryDiffEntry batteryDiffEntry = new BatteryDiffEntry(
mContext,
/*foregroundUsageTimeInMs=*/ 0,
/*backgroundUsageTimeInMs=*/ 0,
/*screenOnTimeInMs=*/ 0,
/*consumePower=*/ 0,
/*foregroundUsageConsumePower=*/ 0,
/*foregroundServiceUsageConsumePower=*/ 0,
/*backgroundUsageConsumePower=*/ 0,
/*cachedUsageConsumePower=*/ 0,
batteryHistEntry);
doReturn(mAppEntry).when(mApplicationsState).getEntry(anyString(), anyInt());
mAppEntry.info = mApplicationInfo;
mApplicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
boolean needsCombineInSystemApp = DataProcessor.needsCombineInSystemApp(
batteryDiffEntry, List.of(), mApplicationsState);
assertThat(needsCombineInSystemApp).isTrue();
}
@Test
public void needsCombineInSystemApp_notSystemApp_returnFalse() {
final int currentUserId = mContext.getUserId();
final BatteryHistEntry batteryHistEntry = createBatteryHistEntry(
ConvertUtils.FAKE_PACKAGE_NAME, "fake_label", /*consumePower=*/ 0,
/*foregroundUsageConsumePower=*/ 0, /*foregroundServiceUsageConsumePower=*/ 0,
/*backgroundUsageConsumePower=*/ 0, /*cachedUsageConsumePower=*/ 0,
/*uid=*/ 0L, currentUserId, ConvertUtils.CONSUMER_TYPE_UID_BATTERY,
/*foregroundUsageTimeInMs=*/ 0L, /*backgroundUsageTimeInMs=*/ 0L, false);
final BatteryDiffEntry batteryDiffEntry = new BatteryDiffEntry(
mContext,
/*foregroundUsageTimeInMs=*/ 0,
/*backgroundUsageTimeInMs=*/ 0,
/*screenOnTimeInMs=*/ 0,
/*consumePower=*/ 0,
/*foregroundUsageConsumePower=*/ 0,
/*foregroundServiceUsageConsumePower=*/ 0,
/*backgroundUsageConsumePower=*/ 0,
/*cachedUsageConsumePower=*/ 0,
batteryHistEntry);
doReturn(mAppEntry).when(mApplicationsState).getEntry(anyString(), anyInt());
mAppEntry.info = mApplicationInfo;
mApplicationInfo.flags = 0;
boolean needsCombineInSystemApp = DataProcessor.needsCombineInSystemApp(
batteryDiffEntry, List.of(), mApplicationsState);
assertThat(needsCombineInSystemApp).isFalse();
}
private static Map<Long, Map<String, BatteryHistEntry>> createHistoryMap(
final long[] timestamps, final int[] levels) {
final Map<Long, Map<String, BatteryHistEntry>> batteryHistoryMap = new HashMap<>();