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

@@ -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) {