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

Commit 28605dcf authored by ykhung's avatar ykhung Committed by YUKAI HUNG
Browse files

Should not show internal package entry in the usage list

Bug: 187770266
Test: make SettingsRoboTests
Change-Id: I56897c8f91295ba2139b7fda8c59a863130ecd86
parent 899845b1
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -1512,12 +1512,20 @@
        <item>@string/enhanced_4g_lte_mode_summary_4g_calling</item>
    </string-array>

    <!-- An allowlist which packages won't show summary in battery usage screen.  [CHAR LIMIT=NONE] -->
    <!-- An allowlist which packages won't show summary in battery usage screen.
         [CHAR LIMIT=NONE] -->
    <string-array name="allowlist_hide_summary_in_battery_usage" translatable="false">
        <!-- Google -->
        <item>"com.google.android.googlequicksearchbox"</item>
    </string-array>

    <!-- An allowlist which packages won't show entry in battery usage screen.
        [CHAR LIMIT=NONE] -->
    <string-array name="allowlist_hide_entry_in_battery_usage" translatable="false">
        <item>"com.google.android.gms.persistent"</item>
        <item>"dex2oat64"</item>
    </string-array>

    <!-- Array of titles palette list for accessibility. -->
    <string-array name="setting_palette_data" translatable="false" >
        <item>@string/color_red</item>
+29 −11
Original line number Diff line number Diff line
@@ -86,6 +86,7 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
    private final InstrumentedPreferenceFragment mFragment;
    private final Handler mHandler = new Handler(Looper.getMainLooper());
    private final CharSequence[] mNotAllowShowSummaryPackages;
    private final CharSequence[] mNotAllowShowEntryPackages;

    // Preference cache to avoid create new instance each time.
    @VisibleForTesting
@@ -103,6 +104,8 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
        mPreferenceKey = preferenceKey;
        mNotAllowShowSummaryPackages = context.getResources()
            .getTextArray(R.array.allowlist_hide_summary_in_battery_usage);
        mNotAllowShowEntryPackages = context.getResources()
            .getTextArray(R.array.allowlist_hide_entry_in_battery_usage);
        if (lifecycle != null) {
            lifecycle.addObserver(this);
        }
@@ -180,8 +183,8 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
            isValidPackage = mBatteryUtils.getPackageUid(packageName)
                != BatteryUtils.UID_NULL;
        }
        Log.d(TAG, String.format("handleClick() label=%s key=%s isValid:%b %s",
            diffEntry.getAppLabel(), histEntry.getKey(), isValidPackage, packageName));
        Log.d(TAG, String.format("handleClick() label=%s key=%s isValid:%b\n%s",
            diffEntry.getAppLabel(), histEntry.getKey(), isValidPackage, histEntry));
        if (isValidPackage) {
            AdvancedPowerUsageDetail.startBatteryDetailPage(
                mActivity, mFragment, diffEntry, powerPref.getPercent(),
@@ -315,6 +318,11 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
        final List<BatteryDiffEntry> appEntries = new ArrayList<>();
        mSystemEntries.clear();
        entries.forEach(entry -> {
            final String packageName = entry.getPackageName();
            if (!isValidToShowEntry(packageName)) {
                Log.w(TAG, "ignore showing item:" + packageName);
                return;
            }
            if (entry.isSystemEntry()) {
                mSystemEntries.add(entry);
            } else {
@@ -510,15 +518,14 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
        return mPrefContext.getString(resourceId, timeSequence);
    }

    private boolean isValidToShowSummary(String packageName) {
        if (mNotAllowShowSummaryPackages != null) {
            for (CharSequence notAllowPackageName : mNotAllowShowSummaryPackages) {
                if (TextUtils.equals(packageName, notAllowPackageName)) {
                    return false;
                }
            }
    @VisibleForTesting
    boolean isValidToShowSummary(String packageName) {
        return !contains(packageName, mNotAllowShowSummaryPackages);
    }
        return true;

    @VisibleForTesting
    boolean isValidToShowEntry(String packageName) {
        return !contains(packageName, mNotAllowShowEntryPackages);
    }

    @VisibleForTesting
@@ -552,6 +559,17 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
        return builder.toString();
    }

    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;
    }

    @VisibleForTesting
    static boolean validateUsageTime(BatteryDiffEntry entry) {
        final long foregroundUsageTimeInMs = entry.mForegroundUsageTimeInMs;
+27 −0
Original line number Diff line number Diff line
@@ -581,6 +581,33 @@ public final class BatteryChartPreferenceControllerTest {
        assertThat(mBatteryChartPreferenceController.mIsExpanded).isTrue();
    }

    @Test
    public void testIsValidToShowSummary_returnExpectedResult() {
        assertThat(mBatteryChartPreferenceController
                .isValidToShowSummary("com.google.android.apps.scone"))
            .isTrue();

        // Verifies the item which is defined in the array list.
        assertThat(mBatteryChartPreferenceController
                .isValidToShowSummary("com.google.android.googlequicksearchbox"))
            .isFalse();
    }

    @Test
    public void testIsValidToShowEntry_returnExpectedResult() {
        assertThat(mBatteryChartPreferenceController
                .isValidToShowEntry("com.google.android.apps.scone"))
            .isTrue();

        // Verifies the items which are defined in the array list.
        assertThat(mBatteryChartPreferenceController
                .isValidToShowEntry("com.google.android.gms.persistent"))
            .isFalse();
        assertThat(mBatteryChartPreferenceController
                .isValidToShowEntry("dex2oat64"))
            .isFalse();
    }

    private static Map<Long, Map<String, BatteryHistEntry>> createBatteryHistoryMap() {
        final Map<Long, Map<String, BatteryHistEntry>> batteryHistoryMap = new HashMap<>();
        for (int index = 0; index < DESIRED_HISTORY_SIZE; index++) {