Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f8b88357 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Refactor getting allowlist set from feature provider."

parents 3b583fc3 bce700f1
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -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.
+2 −2
Original line number Diff line number Diff line
@@ -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
+15 −42
Original line number Diff line number Diff line
@@ -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) {
+3 −2
Original line number Diff line number Diff line
@@ -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());
+3 −2
Original line number Diff line number Diff line
@@ -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(
Loading