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

Commit c0b37557 authored by Alex Stetson's avatar Alex Stetson Committed by Android (Google) Code Review
Browse files

Merge "Replace user switching events with UserTracker" into tm-qpr-dev

parents ef7f9416 d98718db
Loading
Loading
Loading
Loading
+11 −27
Original line number Diff line number Diff line
@@ -73,11 +73,9 @@ import static com.android.systemui.statusbar.policy.DevicePostureController.DEVI
import android.annotation.AnyThread;
import android.annotation.MainThread;
import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityTaskManager;
import android.app.ActivityTaskManager.RootTaskInfo;
import android.app.AlarmManager;
import android.app.UserSwitchObserver;
import android.app.admin.DevicePolicyManager;
import android.app.trust.TrustManager;
import android.content.BroadcastReceiver;
@@ -104,7 +102,6 @@ import android.hardware.fingerprint.FingerprintSensorPropertiesInternal;
import android.nfc.NfcAdapter;
import android.os.CancellationSignal;
import android.os.Handler;
import android.os.IRemoteCallback;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;
@@ -175,6 +172,7 @@ import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;

@@ -2158,7 +2156,7 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
                        handleDevicePolicyManagerStateChanged(msg.arg1);
                        break;
                    case MSG_USER_SWITCHING:
                        handleUserSwitching(msg.arg1, (IRemoteCallback) msg.obj);
                        handleUserSwitching(msg.arg1, (CountDownLatch) msg.obj);
                        break;
                    case MSG_USER_SWITCH_COMPLETE:
                        handleUserSwitchComplete(msg.arg1);
@@ -2283,11 +2281,7 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
                mHandler, UserHandle.ALL);

        mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionListener);
        try {
            ActivityManager.getService().registerUserSwitchObserver(mUserSwitchObserver, TAG);
        } catch (RemoteException e) {
            e.rethrowAsRuntimeException();
        }
        mUserTracker.addCallback(mUserChangedCallback, mainExecutor);

        mTrustManager.registerTrustListener(this);

@@ -2423,17 +2417,17 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
        return mIsFaceEnrolled;
    }

    private final UserSwitchObserver mUserSwitchObserver = new UserSwitchObserver() {
    private final UserTracker.Callback mUserChangedCallback = new UserTracker.Callback() {
        @Override
        public void onUserSwitching(int newUserId, IRemoteCallback reply) {
        public void onUserChanging(int newUser, Context userContext, CountDownLatch latch) {
            mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCHING,
                    newUserId, 0, reply));
                    newUser, 0, latch));
        }

        @Override
        public void onUserSwitchComplete(int newUserId) {
        public void onUserChanged(int newUser, Context userContext) {
            mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCH_COMPLETE,
                    newUserId, 0));
                    newUser, 0));
        }
    };

@@ -3152,7 +3146,7 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
     * Handle {@link #MSG_USER_SWITCHING}
     */
    @VisibleForTesting
    void handleUserSwitching(int userId, IRemoteCallback reply) {
    void handleUserSwitching(int userId, CountDownLatch latch) {
        Assert.isMainThread();
        clearBiometricRecognized();
        mUserTrustIsUsuallyManaged.put(userId, mTrustManager.isTrustUsuallyManaged(userId));
@@ -3162,11 +3156,7 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
                cb.onUserSwitching(userId);
            }
        }
        try {
            reply.sendResult(null);
        } catch (RemoteException e) {
            mLogger.logException(e, "Ignored exception while userSwitching");
        }
        latch.countDown();
    }

    /**
@@ -3936,13 +3926,7 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
            mContext.getContentResolver().unregisterContentObserver(mTimeFormatChangeObserver);
        }

        try {
            ActivityManager.getService().unregisterUserSwitchObserver(mUserSwitchObserver);
        } catch (RemoteException e) {
            mLogger.logException(
                    e,
                    "RemoteException onDestroy. cannot unregister userSwitchObserver");
        }
        mUserTracker.removeCallback(mUserChangedCallback);

        TaskStackChangeListeners.getInstance().unregisterTaskStackListener(mTaskStackListener);

+14 −2
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.systemui.settings
import android.content.Context
import android.content.pm.UserInfo
import android.os.UserHandle
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Executor

/**
@@ -66,15 +67,26 @@ interface UserTracker : UserContentResolverProvider, UserContextProvider {
     */
    interface Callback {

        /**
         * Same as {@link onUserChanging(Int, Context, CountDownLatch)} but the latch will be
         * auto-decremented after the completion of this method.
         */
        @JvmDefault
        fun onUserChanging(newUser: Int, userContext: Context) {}

        /**
         * Notifies that the current user is being changed.
         * Override this method to run things while the screen is frozen for the user switch.
         * Please use {@link #onUserChanged} if the task doesn't need to push the unfreezing of the
         * screen further. Please be aware that code executed in this callback will lengthen the
         * user switch duration.
         * user switch duration. When overriding this method, countDown() MUST be called on the
         * latch once execution is complete.
         */
        @JvmDefault
        fun onUserChanging(newUser: Int, userContext: Context) {}
        fun onUserChanging(newUser: Int, userContext: Context, latch: CountDownLatch) {
            onUserChanging(newUser, userContext)
            latch.countDown()
        }

        /**
         * Notifies that the current user has changed.
+17 −11
Original line number Diff line number Diff line
@@ -183,9 +183,22 @@ class UserTrackerImpl internal constructor(
        Log.i(TAG, "Switching to user $newUserId")

        setUserIdInternal(newUserId)
        notifySubscribers {
            onUserChanging(newUserId, userContext)
        }.await()

        val list = synchronized(callbacks) {
            callbacks.toList()
        }
        val latch = CountDownLatch(list.size)
        list.forEach {
            val callback = it.callback.get()
            if (callback != null) {
                it.executor.execute {
                    callback.onUserChanging(userId, userContext, latch)
                }
            } else {
                latch.countDown()
            }
        }
        latch.await()
    }

    @WorkerThread
@@ -225,25 +238,18 @@ class UserTrackerImpl internal constructor(
        }
    }

    private inline fun notifySubscribers(
            crossinline action: UserTracker.Callback.() -> Unit
    ): CountDownLatch {
    private inline fun notifySubscribers(crossinline action: UserTracker.Callback.() -> Unit) {
        val list = synchronized(callbacks) {
            callbacks.toList()
        }
        val latch = CountDownLatch(list.size)

        list.forEach {
            if (it.callback.get() != null) {
                it.executor.execute {
                    it.callback.get()?.action()
                    latch.countDown()
                }
            } else {
                latch.countDown()
            }
        }
        return latch
    }

    override fun dump(pw: PrintWriter, args: Array<out String>) {
+12 −12
Original line number Diff line number Diff line
@@ -29,7 +29,6 @@ import android.app.AppGlobals;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.SynchronousUserSwitchObserver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
@@ -52,7 +51,9 @@ import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
import com.android.systemui.CoreStartable;
import com.android.systemui.R;
import com.android.systemui.dagger.SysUISingleton;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.dagger.qualifiers.UiBackground;
import com.android.systemui.settings.UserTracker;
import com.android.systemui.statusbar.CommandQueue;
import com.android.systemui.statusbar.policy.KeyguardStateController;
import com.android.systemui.util.NotificationChannels;
@@ -73,6 +74,8 @@ public class InstantAppNotifier

    private final Context mContext;
    private final Handler mHandler = new Handler();
    private final UserTracker mUserTracker;
    private final Executor mMainExecutor;
    private final Executor mUiBgExecutor;
    private final ArraySet<Pair<String, Integer>> mCurrentNotifs = new ArraySet<>();
    private final CommandQueue mCommandQueue;
@@ -82,10 +85,14 @@ public class InstantAppNotifier
    public InstantAppNotifier(
            Context context,
            CommandQueue commandQueue,
            UserTracker userTracker,
            @Main Executor mainExecutor,
            @UiBackground Executor uiBgExecutor,
            KeyguardStateController keyguardStateController) {
        mContext = context;
        mCommandQueue = commandQueue;
        mUserTracker = userTracker;
        mMainExecutor = mainExecutor;
        mUiBgExecutor = uiBgExecutor;
        mKeyguardStateController = keyguardStateController;
    }
@@ -93,11 +100,7 @@ public class InstantAppNotifier
    @Override
    public void start() {
        // listen for user / profile change.
        try {
            ActivityManager.getService().registerUserSwitchObserver(mUserSwitchListener, TAG);
        } catch (RemoteException e) {
            // Ignore
        }
        mUserTracker.addCallback(mUserSwitchListener, mMainExecutor);

        mCommandQueue.addCallback(this);
        mKeyguardStateController.addCallback(this);
@@ -129,13 +132,10 @@ public class InstantAppNotifier
        updateForegroundInstantApps();
    }

    private final SynchronousUserSwitchObserver mUserSwitchListener =
            new SynchronousUserSwitchObserver() {
                @Override
                public void onUserSwitching(int newUserId) throws RemoteException {}

    private final UserTracker.Callback mUserSwitchListener =
            new UserTracker.Callback() {
                @Override
                public void onUserSwitchComplete(int newUserId) throws RemoteException {
                public void onUserChanged(int newUser, Context userContext) {
                    mHandler.post(
                            () -> {
                                updateForegroundInstantApps();
+15 −20
Original line number Diff line number Diff line
@@ -22,8 +22,6 @@ import android.annotation.Nullable;
import android.app.ActivityTaskManager;
import android.app.AlarmManager;
import android.app.AlarmManager.AlarmClockInfo;
import android.app.IActivityManager;
import android.app.SynchronousUserSwitchObserver;
import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.Context;
@@ -134,7 +132,6 @@ public class PhoneStatusBarPolicy
    private final NextAlarmController mNextAlarmController;
    private final AlarmManager mAlarmManager;
    private final UserInfoController mUserInfoController;
    private final IActivityManager mIActivityManager;
    private final UserManager mUserManager;
    private final UserTracker mUserTracker;
    private final DevicePolicyManager mDevicePolicyManager;
@@ -149,6 +146,7 @@ public class PhoneStatusBarPolicy
    private final KeyguardStateController mKeyguardStateController;
    private final LocationController mLocationController;
    private final PrivacyItemController mPrivacyItemController;
    private final Executor mMainExecutor;
    private final Executor mUiBgExecutor;
    private final SensorPrivacyController mSensorPrivacyController;
    private final RecordingController mRecordingController;
@@ -168,16 +166,17 @@ public class PhoneStatusBarPolicy
    @Inject
    public PhoneStatusBarPolicy(StatusBarIconController iconController,
            CommandQueue commandQueue, BroadcastDispatcher broadcastDispatcher,
            @UiBackground Executor uiBgExecutor, @Main Looper looper, @Main Resources resources,
            CastController castController, HotspotController hotspotController,
            BluetoothController bluetoothController, NextAlarmController nextAlarmController,
            UserInfoController userInfoController, RotationLockController rotationLockController,
            DataSaverController dataSaverController, ZenModeController zenModeController,
            @Main Executor mainExecutor, @UiBackground Executor uiBgExecutor, @Main Looper looper,
            @Main Resources resources, CastController castController,
            HotspotController hotspotController, BluetoothController bluetoothController,
            NextAlarmController nextAlarmController, UserInfoController userInfoController,
            RotationLockController rotationLockController, DataSaverController dataSaverController,
            ZenModeController zenModeController,
            DeviceProvisionedController deviceProvisionedController,
            KeyguardStateController keyguardStateController,
            LocationController locationController,
            SensorPrivacyController sensorPrivacyController, IActivityManager iActivityManager,
            AlarmManager alarmManager, UserManager userManager, UserTracker userTracker,
            SensorPrivacyController sensorPrivacyController, AlarmManager alarmManager,
            UserManager userManager, UserTracker userTracker,
            DevicePolicyManager devicePolicyManager, RecordingController recordingController,
            @Nullable TelecomManager telecomManager, @DisplayId int displayId,
            @Main SharedPreferences sharedPreferences, DateFormatUtil dateFormatUtil,
@@ -195,7 +194,6 @@ public class PhoneStatusBarPolicy
        mNextAlarmController = nextAlarmController;
        mAlarmManager = alarmManager;
        mUserInfoController = userInfoController;
        mIActivityManager = iActivityManager;
        mUserManager = userManager;
        mUserTracker = userTracker;
        mDevicePolicyManager = devicePolicyManager;
@@ -208,6 +206,7 @@ public class PhoneStatusBarPolicy
        mPrivacyItemController = privacyItemController;
        mSensorPrivacyController = sensorPrivacyController;
        mRecordingController = recordingController;
        mMainExecutor = mainExecutor;
        mUiBgExecutor = uiBgExecutor;
        mTelecomManager = telecomManager;
        mRingerModeTracker = ringerModeTracker;
@@ -256,11 +255,7 @@ public class PhoneStatusBarPolicy
        mRingerModeTracker.getRingerModeInternal().observeForever(observer);

        // listen for user / profile change.
        try {
            mIActivityManager.registerUserSwitchObserver(mUserSwitchListener, TAG);
        } catch (RemoteException e) {
            // Ignore
        }
        mUserTracker.addCallback(mUserSwitchListener, mMainExecutor);

        // TTY status
        updateTTY();
@@ -555,15 +550,15 @@ public class PhoneStatusBarPolicy
        });
    }

    private final SynchronousUserSwitchObserver mUserSwitchListener =
            new SynchronousUserSwitchObserver() {
    private final UserTracker.Callback mUserSwitchListener =
            new UserTracker.Callback() {
                @Override
                public void onUserSwitching(int newUserId) throws RemoteException {
                public void onUserChanging(int newUser, Context userContext) {
                    mHandler.post(() -> mUserInfoController.reloadUserInfo());
                }

                @Override
                public void onUserSwitchComplete(int newUserId) throws RemoteException {
                public void onUserChanged(int newUser, Context userContext) {
                    mHandler.post(() -> {
                        updateAlarm();
                        updateManagedProfile();
Loading