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

Commit 29d65f5e authored by Fabian Kozynski's avatar Fabian Kozynski
Browse files

Add multi-user support for AutoTileManager

Before this CL, AutoTileManager (and AutoAddTracker) would only work
"corectly" for user 0, as all the settings (read and write) were done in
user 0. This meant that tiles that got auto added as user 0 would not be
auto-added for other users. Similarly, tiles that got auto added as a
secondary user would not be auto added as user 0.

This CL makes sure that everything is done with respect to the current
user (as reported by QSTileHost).

Test: manual
Test: atest AutoTileManagerTest AutoAddTrackerTest
Fixes: 154607890

Change-Id: I90717dedc8935dd2597e504acd57f5b44236ef3e
Merged-In: I518334cca0a0960e0b3b74b43ac9fce4610c7bd0
parent 22e135e1
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ import android.content.ComponentName
import android.service.controls.Control
import android.service.controls.ControlsProviderService
import android.service.controls.actions.ControlAction
import com.android.systemui.controls.UserAwareController
import com.android.systemui.util.UserAwareController
import java.util.function.Consumer

/**
+1 −1
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ import android.service.controls.Control
import android.service.controls.ControlsProviderService
import android.service.controls.actions.ControlAction
import com.android.systemui.controls.ControlStatus
import com.android.systemui.controls.UserAwareController
import com.android.systemui.util.UserAwareController
import com.android.systemui.controls.management.ControlsFavoritingActivity
import com.android.systemui.controls.ui.ControlsUiController
import java.util.function.Consumer
+2 −2
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ package com.android.systemui.controls.management

import android.content.ComponentName
import com.android.systemui.controls.ControlsServiceInfo
import com.android.systemui.controls.UserAwareController
import com.android.systemui.util.UserAwareController
import com.android.systemui.statusbar.policy.CallbackController

/**
+53 −11
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import static com.android.systemui.statusbar.phone.AutoTileManager.WORK;
import android.content.Context;
import android.database.ContentObserver;
import android.os.Handler;
import android.os.UserHandle;
import android.provider.Settings.Secure;
import android.text.TextUtils;
import android.util.ArraySet;
@@ -30,6 +31,7 @@ import android.util.ArraySet;
import com.android.internal.annotations.VisibleForTesting;
import com.android.systemui.Prefs;
import com.android.systemui.Prefs.Key;
import com.android.systemui.util.UserAwareController;

import java.util.Arrays;
import java.util.Collection;
@@ -37,7 +39,7 @@ import java.util.Collections;

import javax.inject.Inject;

public class AutoAddTracker {
public class AutoAddTracker implements UserAwareController {

    private static final String[][] CONVERT_PREFS = {
            {Key.QS_HOTSPOT_ADDED, HOTSPOT},
@@ -49,20 +51,39 @@ public class AutoAddTracker {

    private final ArraySet<String> mAutoAdded;
    private final Context mContext;
    private int mUserId;

    @Inject
    public AutoAddTracker(Context context) {
    public AutoAddTracker(Context context, int userId) {
        mContext = context;
        mUserId = userId;
        mAutoAdded = new ArraySet<>(getAdded());
        // TODO: remove migration code and shared preferences keys after P release
        if (mUserId == UserHandle.USER_SYSTEM) {
            for (String[] convertPref : CONVERT_PREFS) {
                if (Prefs.getBoolean(context, convertPref[0], false)) {
                    setTileAdded(convertPref[1]);
                    Prefs.remove(context, convertPref[0]);
                }
            }
        }
        mContext.getContentResolver().registerContentObserver(
                Secure.getUriFor(Secure.QS_AUTO_ADDED_TILES), false, mObserver);
                Secure.getUriFor(Secure.QS_AUTO_ADDED_TILES), false, mObserver,
                UserHandle.USER_ALL);
    }

    @Override
    public void changeUser(UserHandle newUser) {
        if (newUser.getIdentifier() == mUserId) {
            return;
        }
        mUserId = newUser.getIdentifier();
        mAutoAdded.clear();
        mAutoAdded.addAll(getAdded());
    }

    @Override
    public int getCurrentUserId() {
        return mUserId;
    }

    public boolean isAdded(String tile) {
@@ -86,12 +107,13 @@ public class AutoAddTracker {
    }

    private void saveTiles() {
        Secure.putString(mContext.getContentResolver(), Secure.QS_AUTO_ADDED_TILES,
                TextUtils.join(",", mAutoAdded));
        Secure.putStringForUser(mContext.getContentResolver(), Secure.QS_AUTO_ADDED_TILES,
                TextUtils.join(",", mAutoAdded), mUserId);
    }

    private Collection<String> getAdded() {
        String current = Secure.getString(mContext.getContentResolver(), Secure.QS_AUTO_ADDED_TILES);
        String current = Secure.getStringForUser(mContext.getContentResolver(),
                Secure.QS_AUTO_ADDED_TILES, mUserId);
        if (current == null) {
            return Collections.emptyList();
        }
@@ -102,7 +124,27 @@ public class AutoAddTracker {
    protected final ContentObserver mObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
            mAutoAdded.clear();
            mAutoAdded.addAll(getAdded());
        }
    };

    public static class Builder {
        private final Context mContext;
        private int mUserId;

        @Inject
        public Builder(Context context) {
            mContext = context;
        }

        public Builder setUserId(int userId) {
            mUserId = userId;
            return this;
        }

        public AutoAddTracker build() {
            return new AutoAddTracker(mContext, mUserId);
        }
    }
}
+3 −0
Original line number Diff line number Diff line
@@ -255,6 +255,9 @@ public class QSTileHost implements QSHost, Tunable, PluginListener<QSFactory>, D
        int currentUser = ActivityManager.getCurrentUser();
        if (currentUser != mCurrentUser) {
            mUserContext = mContext.createContextAsUser(UserHandle.of(currentUser), 0);
            if (mAutoTiles != null) {
                mAutoTiles.changeUser(UserHandle.of(currentUser));
            }
        }
        if (tileSpecs.equals(mTileSpecs) && currentUser == mCurrentUser) return;
        mTiles.entrySet().stream().filter(tile -> !tileSpecs.contains(tile.getKey())).forEach(
Loading