Refactor getting allowlist set from feature provider.

(1) Use Set<CharSequence> for all the allowlists.
(2) The contains() method of Set<CharSequence> also works for String. No
    need an extra contains() util function.

Bug: 262802531
Fix: 262802531
Test: presubmit
Change-Id: Ib2aaf9a3b8db4618a8c46f138e8d35f15b77c104
This commit is contained in:
Zaiyue Xue
2022-12-15 17:48:12 +08:00
parent 6c4f83f33d
commit bce700f1db
6 changed files with 28 additions and 52 deletions

View File

@@ -146,9 +146,9 @@ public interface PowerUsageFeatureProvider {
Set<CharSequence> getHideBackgroundUsageTimeSet(Context context);
/**
* Returns package names for hiding application in the usage screen.
* Returns {@link Set} for hiding application package names in the usage screen.
*/
CharSequence[] getHideApplicationEntries(Context context);
Set<CharSequence> getHideApplicationSet(Context context);
/**
* Returns {@link Set} for ignoring task root class names for screen on time.

View File

@@ -162,8 +162,8 @@ public class PowerUsageFeatureProviderImpl implements PowerUsageFeatureProvider
}
@Override
public CharSequence[] getHideApplicationEntries(Context context) {
return new CharSequence[0];
public Set<CharSequence> getHideApplicationSet(Context context) {
return new ArraySet<>();
}
@Override

View File

@@ -280,9 +280,7 @@ public final class DataProcessor {
case Event.DEVICE_SHUTDOWN:
final String taskRootClassName = event.getTaskRootClassName();
if (!TextUtils.isEmpty(taskRootClassName)
&& !ignoreScreenOnTimeTaskRootSet.isEmpty()
&& contains(
taskRootClassName, ignoreScreenOnTimeTaskRootSet)) {
&& ignoreScreenOnTimeTaskRootSet.contains(taskRootClassName)) {
Log.w(TAG, String.format(
"Ignoring a usage event with task root class name %s, "
+ "(timestamp=%d, type=%d)",
@@ -1238,14 +1236,14 @@ public final class DataProcessor {
private static void purgeFakeAndHiddenPackages(
final Context context,
final Map<Integer, Map<Integer, BatteryDiffData>> resultMap) {
final Set<CharSequence> backgroundUsageTimeHideList =
final Set<CharSequence> hideBackgroundUsageTimeSet =
FeatureFactory.getFactory(context)
.getPowerUsageFeatureProvider(context)
.getHideBackgroundUsageTimeSet(context);
final CharSequence[] notAllowShowEntryPackages =
final Set<CharSequence> hideApplicationSet =
FeatureFactory.getFactory(context)
.getPowerUsageFeatureProvider(context)
.getHideApplicationEntries(context);
.getHideApplicationSet(context);
resultMap.keySet().forEach(dailyKey -> {
final Map<Integer, BatteryDiffData> dailyUsageMap = resultMap.get(dailyKey);
dailyUsageMap.values().forEach(diffEntryLists -> {
@@ -1253,30 +1251,31 @@ public final class DataProcessor {
return;
}
purgeFakeAndHiddenPackages(
diffEntryLists.getAppDiffEntryList(), backgroundUsageTimeHideList,
notAllowShowEntryPackages);
diffEntryLists.getAppDiffEntryList(), hideBackgroundUsageTimeSet,
hideApplicationSet);
purgeFakeAndHiddenPackages(
diffEntryLists.getSystemDiffEntryList(), backgroundUsageTimeHideList,
notAllowShowEntryPackages);
diffEntryLists.getSystemDiffEntryList(), hideBackgroundUsageTimeSet,
hideApplicationSet);
});
});
}
private static void purgeFakeAndHiddenPackages(
final List<BatteryDiffEntry> entries,
final Set<CharSequence> backgroundUsageTimeHideList,
final CharSequence[] notAllowShowEntryPackages) {
final Set<CharSequence> hideBackgroundUsageTimeSet,
final Set<CharSequence> hideApplicationSet) {
final Iterator<BatteryDiffEntry> iterator = entries.iterator();
while (iterator.hasNext()) {
final BatteryDiffEntry entry = iterator.next();
final String packageName = entry.getPackageName();
if (packageName == null) {
continue;
}
if (ConvertUtils.FAKE_PACKAGE_NAME.equals(packageName)
|| contains(packageName, notAllowShowEntryPackages)) {
|| hideApplicationSet.contains(packageName)) {
iterator.remove();
}
if (packageName != null
&& !backgroundUsageTimeHideList.isEmpty()
&& contains(packageName, backgroundUsageTimeHideList)) {
if (hideBackgroundUsageTimeSet.contains(packageName)) {
entry.mBackgroundUsageTimeInMs = 0;
}
}
@@ -1465,18 +1464,6 @@ public final class DataProcessor {
return calendar.getTimeInMillis();
}
/** Whether the Set contains the target. */
private static boolean contains(String target, Set<CharSequence> packageNames) {
if (target != null && packageNames != null) {
for (CharSequence packageName : packageNames) {
if (TextUtils.equals(target, packageName)) {
return true;
}
}
}
return false;
}
private static long getDiffValue(long v1, long v2, long v3) {
return (v2 > v1 ? v2 - v1 : 0) + (v3 > v2 ? v3 - v2 : 0);
}
@@ -1521,20 +1508,6 @@ public final class DataProcessor {
return sFakeCurrentTimeMillis > 0 ? sFakeCurrentTimeMillis : System.currentTimeMillis();
}
/**
* @return Returns whether the target is in the CharSequence array.
*/
private static boolean contains(String target, CharSequence[] packageNames) {
if (target != null && packageNames != null) {
for (CharSequence packageName : packageNames) {
if (TextUtils.equals(target, packageName)) {
return true;
}
}
}
return false;
}
private static void log(Context context, final String content, final long timestamp,
final BatteryHistEntry entry) {
if (DEBUG) {

View File

@@ -57,6 +57,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
@RunWith(RobolectricTestRunner.class)
@@ -92,9 +93,9 @@ public final class BatteryChartPreferenceControllerTest {
final Resources resources = spy(mContext.getResources());
resources.getConfiguration().setLocales(new LocaleList(new Locale("en_US")));
doReturn(resources).when(mContext).getResources();
doReturn(new String[]{"com.android.gms.persistent"})
doReturn(Set.of("com.android.gms.persistent"))
.when(mFeatureFactory.powerUsageFeatureProvider)
.getHideApplicationEntries(mContext);
.getHideApplicationSet(mContext);
doReturn(mLayoutParams).when(mDailyChartView).getLayoutParams();
doReturn(mIntent).when(mContext).registerReceiver(any(), any());
doReturn(100).when(mIntent).getIntExtra(eq(BatteryManager.EXTRA_SCALE), anyInt());

View File

@@ -48,6 +48,7 @@ import org.robolectric.RuntimeEnvironment;
import java.util.Arrays;
import java.util.Locale;
import java.util.Set;
import java.util.TimeZone;
@RunWith(RobolectricTestRunner.class)
@@ -86,9 +87,9 @@ public final class BatteryUsageBreakdownControllerTest {
final Resources resources = spy(mContext.getResources());
resources.getConfiguration().setLocales(new LocaleList(new Locale("en_US")));
doReturn(resources).when(mContext).getResources();
doReturn(new String[]{"com.android.gms.persistent"})
doReturn(Set.of("com.android.gms.persistent"))
.when(mFeatureFactory.powerUsageFeatureProvider)
.getHideApplicationEntries(mContext);
.getHideApplicationSet(mContext);
mBatteryUsageBreakdownController = createController();
mBatteryUsageBreakdownController.mAppListPreferenceGroup = mAppListPreferenceGroup;
mBatteryDiffEntry = new BatteryDiffEntry(

View File

@@ -60,6 +60,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
@RunWith(RobolectricTestRunner.class)
@@ -1062,8 +1063,8 @@ public class DataProcessorTest {
final List<Integer> levels = List.of(100, 100);
hourlyBatteryLevelsPerDay.add(
new BatteryLevelData.PeriodBatteryLevelData(timestamps, levels));
when(mPowerUsageFeatureProvider.getHideApplicationEntries(mContext))
.thenReturn(new CharSequence[]{"package1"});
when(mPowerUsageFeatureProvider.getHideApplicationSet(mContext))
.thenReturn(Set.of("package1"));
final Map<Integer, Map<Integer, BatteryDiffData>> resultMap =
DataProcessor.getBatteryUsageMap(