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

Commit e5dc6a5c authored by YK Hung's avatar YK Hung Committed by Android (Google) Code Review
Browse files

Merge "Reset the dynamic set denylist after reboot the device" into main

parents fe934dd0 65260c7f
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -51,8 +51,7 @@ public final class BatterySettingsMigrateChecker extends BroadcastReceiver {
        context = context.getApplicationContext();
        verifySaverConfiguration(context);
        verifyBatteryOptimizeModes(context);
        // Initialize and sync settings into SharedPreferences for migration.
        DynamicDenylistManager.getInstance(context);
        DynamicDenylistManager.getInstance(context).onBootComplete();
    }

    /** Avoid users set important apps into the unexpected battery optimize modes */
+13 −4
Original line number Diff line number Diff line
@@ -139,16 +139,25 @@ public final class DynamicDenylistManager {
            return;
        }
        synchronized (mLock) {
            for (int uid : mNetworkPolicyManager
                    .getUidsWithPolicy(POLICY_REJECT_METERED_BACKGROUND)) {
            final int[] uids = mNetworkPolicyManager
                    .getUidsWithPolicy(POLICY_REJECT_METERED_BACKGROUND);
            if (uids != null && uids.length != 0) {
                for (int uid : uids) {
                    if (!getDenylistAllUids(getManualDenylistPref()).contains(uid)) {
                        mNetworkPolicyManager.setUidPolicy(uid, POLICY_NONE);
                    }
                }
            }
        }
        clearSharedPreferences();
    }

    /** Reset the POLICY_REJECT_METERED uids when device is boot completed. */
    public void onBootComplete() {
        resetDenylistIfNeeded(/* packageName= */ null, /* force= */ true);
        syncPolicyIfNeeded();
    }

    /** Dump the data stored in the {@link SharedPreferences}. */
    public void dump(PrintWriter writer) {
        writer.println("Dump of DynamicDenylistManager:");
+21 −0
Original line number Diff line number Diff line
@@ -393,6 +393,27 @@ public class DynamicDenylistManagerTest {
        assertThat(dumpResults.contains("DynamicDenylist: null")).isTrue();
    }

    @Test
    public void onBootComplete_resetIntoManualMode() {
        initDynamicDenylistManager(new int[] {FAKE_UID_1_INT});
        setDenylist(new ArraySet<>(List.of(FAKE_UID_2_INT)));
        // Ensure the testing environment for manual denylist.
        assertThat(mManualDenyListPref.getAll()).hasSize(2);
        assertTrue(mManualDenyListPref.contains(PREF_KEY_MANUAL_DENYLIST_SYNCED));
        assertTrue(mManualDenyListPref.contains(FAKE_UID_1));
        // Ensure the testing environment for dynamic denylist.
        assertThat(mDynamicDenyListPref.getAll()).hasSize(1);

        mDynamicDenylistManager.onBootComplete();

        // Keep the users set uids in the manual denylist.
        assertThat(mManualDenyListPref.getAll()).hasSize(2);
        assertTrue(mManualDenyListPref.contains(PREF_KEY_MANUAL_DENYLIST_SYNCED));
        assertTrue(mManualDenyListPref.contains(FAKE_UID_1));
        // Clear the uids in the dynamic denylist.
        assertThat(mDynamicDenyListPref.getAll()).isEmpty();
    }

    private void initDynamicDenylistManager(int[] preload) {
        initDynamicDenylistManager(preload, preload);
    }