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

Commit 99c7e917 authored by Tommy Webb's avatar Tommy Webb Committed by Michael Bestas
Browse files

fixup! Fix background data clobbering other policies

Fetching the state of policies was not working due to an incorrect use
of Arrays.asList() - if you give it an int array, you just get a list
with that int[] as a single element, so using the contains method with
an int will always return false. This has now all been refactored.

Test: Manual: Open Settings > Network & Internet > Data Saver >
Unrestricted data. Turn it on for something. Go back, and then
return to the page. It should still show as on.

Issue: calyxos#2547
Change-Id: I671544f6fdf9897484c6265c31c8b3cd29ad4a92
parent 79d43224
Loading
Loading
Loading
Loading
+32 −13
Original line number Diff line number Diff line
@@ -30,7 +30,6 @@ import com.android.settingslib.utils.ThreadUtils;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;

public class DataSaverBackend {

@@ -124,32 +123,52 @@ public class DataSaverBackend {
    }

    private void loadUidPolicies(int policy) {
        final int[] uidsWithPolicy = mPolicyManager.getUidsWithPolicy(policy);
        for (int uid : uidsWithPolicy) {
            setCachedUidPolicyFlag(uid, policy, true);
        final int[] uidsWithPolicyArray = mPolicyManager.getUidsWithPolicy(policy);
        final ArrayList<Integer> uidsWithPolicy = new ArrayList<>(uidsWithPolicyArray.length);
        for (final int uid : uidsWithPolicyArray) {
            uidsWithPolicy.add(uid);
        }
        // Update existing cached UID policies.
        for (int i = 0; i < mUidPolicies.size(); i++) {
            final int uid = mUidPolicies.keyAt(i);
            if (!Arrays.asList(uidsWithPolicy).contains(uid)) {
                setCachedUidPolicyFlag(uid, policy, false);
            final Integer cachedEntryUid = mUidPolicies.keyAt(i);
            if (uidsWithPolicy.remove(cachedEntryUid)) {
                // UID had the policy. It was removed so we don't have to process it twice.
                setCachedUidPolicyFlagAt(i, policy, true);
            } else {
                // UID does not have the policy.
                setCachedUidPolicyFlagAt(i, policy, false);
            }
        }
        // Add policies for remaining UIDs, which did not have cached policies, so we're it.
        for (final int uid : uidsWithPolicy) {
            mUidPolicies.put(uid, policy);
        }
    }

    private int setCachedUidPolicyFlag(int uid, int policy, boolean add) {
        final int currentPolicy = mUidPolicies.get(uid, POLICY_NONE);
    private void setCachedUidPolicyFlag(int uid, int policy, boolean add) {
        final int index = mUidPolicies.indexOfKey(uid);
        if (index < 0) {
            if (add) {
                mUidPolicies.put(uid, policy);
            }
            return;
        }
        setCachedUidPolicyFlagAt(index, policy, add);
    }

    private void setCachedUidPolicyFlagAt(int index, int policy, boolean add) {
        final int currentPolicy = mUidPolicies.valueAt(index);
        final int newPolicy = add ? (currentPolicy | policy) : (currentPolicy & ~policy);
        mUidPolicies.put(uid, newPolicy);
        return newPolicy;
        mUidPolicies.setValueAt(index, newPolicy);
    }

    private int setUidPolicyFlag(int uid, int policy, boolean add) {
    private void setUidPolicyFlag(int uid, int policy, boolean add) {
        if (add) {
            mPolicyManager.addUidPolicy(uid, policy);
        } else {
            mPolicyManager.removeUidPolicy(uid, policy);
        }
        return setCachedUidPolicyFlag(uid, policy, add);
        setCachedUidPolicyFlag(uid, policy, add);
    }

    private boolean isUidPolicyFlagSet(int uid, int policy) {