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

Commit 7f8b7bbf authored by William Escande's avatar William Escande
Browse files

BMS: keep userHandle during User switch retry

Pass the userHandle directly and change method name since it is not a
generic method but specific to userSwitch
Also MESSAGE_USER_SWITCHED will now keep the user it is switching to in
it

Bug: 262605980
Bug: 285046954
Test: None | no-op change
Change-Id: I8e37744aa85acc3596480b741b953cb1dae04668
parent 9c72ad88
Loading
Loading
Loading
Loading
+25 −10
Original line number Original line Diff line number Diff line
@@ -1554,11 +1554,11 @@ class BluetoothManagerService {
    }
    }


    /** Called when switching to a different foreground user. */
    /** Called when switching to a different foreground user. */
    void handleSwitchUser(UserHandle userHandle) {
    private void handleSwitchUser(UserHandle userHandle) {
        if (DBG) {
        if (DBG) {
            Log.d(TAG, "User " + userHandle + " switched");
            Log.d(TAG, "User " + userHandle + " switched");
        }
        }
        mHandler.obtainMessage(MESSAGE_USER_SWITCHED, userHandle.getIdentifier(), 0).sendToTarget();
        mHandler.obtainMessage(MESSAGE_USER_SWITCHED, userHandle).sendToTarget();
    }
    }


    /** Called when user is unlocked. */
    /** Called when user is unlocked. */
@@ -1566,7 +1566,7 @@ class BluetoothManagerService {
        if (DBG) {
        if (DBG) {
            Log.d(TAG, "User " + userHandle + " unlocked");
            Log.d(TAG, "User " + userHandle + " unlocked");
        }
        }
        mHandler.obtainMessage(MESSAGE_USER_UNLOCKED, userHandle.getIdentifier(), 0).sendToTarget();
        mHandler.obtainMessage(MESSAGE_USER_UNLOCKED, userHandle).sendToTarget();
    }
    }


    /**
    /**
@@ -2350,17 +2350,23 @@ class BluetoothManagerService {
                    }
                    }
                    mHandler.removeMessages(MESSAGE_USER_SWITCHED);
                    mHandler.removeMessages(MESSAGE_USER_SWITCHED);
                    mBluetoothNotificationManager.createNotificationChannels();
                    mBluetoothNotificationManager.createNotificationChannels();
                    UserHandle userTo = (UserHandle) msg.obj;


                    /* disable and enable BT when detect a user switch */
                    /* disable and enable BT when detect a user switch */
                    if (mAdapter != null && isEnabled()) {
                    if (mAdapter != null && isEnabled()) {
                        restartForReason(BluetoothProtoEnums.ENABLE_DISABLE_REASON_USER_SWITCH);
                        restartForNewUser(userTo);
                    } else if (isBinding() || mAdapter != null) {
                    } else if (isBinding() || mAdapter != null) {
                        Message userMsg = mHandler.obtainMessage(MESSAGE_USER_SWITCHED);
                        Message userMsg = Message.obtain(msg);
                        userMsg.arg2 = 1 + msg.arg2;
                        userMsg.arg1++;
                        // if user is switched when service is binding retry after a delay
                        // if user is switched when service is binding retry after a delay
                        mHandler.sendMessageDelayed(userMsg, USER_SWITCHED_TIME_MS);
                        mHandler.sendMessageDelayed(userMsg, USER_SWITCHED_TIME_MS);
                        if (DBG) {
                        if (DBG) {
                            Log.d(TAG, "Retry MESSAGE_USER_SWITCHED " + userMsg.arg2);
                            Log.d(
                                    TAG,
                                    "MESSAGE_USER_SWITCHED user="
                                            + userTo
                                            + " number of retry attempt="
                                            + userMsg.arg1);
                        }
                        }
                    }
                    }
                    break;
                    break;
@@ -2389,7 +2395,7 @@ class BluetoothManagerService {
                    android.Manifest.permission.BLUETOOTH_CONNECT,
                    android.Manifest.permission.BLUETOOTH_CONNECT,
                    android.Manifest.permission.BLUETOOTH_PRIVILEGED
                    android.Manifest.permission.BLUETOOTH_PRIVILEGED
                })
                })
        private void restartForReason(int reason) {
        private void restartForNewUser(UserHandle newUser) {
            mAdapterLock.readLock().lock();
            mAdapterLock.readLock().lock();
            try {
            try {
                if (mAdapter != null) {
                if (mAdapter != null) {
@@ -2412,6 +2418,9 @@ class BluetoothManagerService {
                mState.set(STATE_TURNING_ON);
                mState.set(STATE_TURNING_ON);
            }
            }


            // TODO(b/285046954): We call unregisterCallback, and yet the code is calling
            // waitForState(STATE_ON)
            // This is a non-sense
            waitForState(STATE_ON);
            waitForState(STATE_ON);


            if (mState.oneOf(STATE_TURNING_ON)) {
            if (mState.oneOf(STATE_TURNING_ON)) {
@@ -2420,7 +2429,10 @@ class BluetoothManagerService {


            unbindAllBluetoothProfileServices();
            unbindAllBluetoothProfileServices();
            // disable
            // disable
            addActiveLog(reason, mContext.getPackageName(), false);
            addActiveLog(
                    BluetoothProtoEnums.ENABLE_DISABLE_REASON_USER_SWITCH,
                    mContext.getPackageName(),
                    false);
            handleDisable();
            handleDisable();
            // Pbap service need receive STATE_TURNING_OFF intent to close
            // Pbap service need receive STATE_TURNING_OFF intent to close
            bluetoothStateChangeHandler(STATE_ON, STATE_TURNING_OFF);
            bluetoothStateChangeHandler(STATE_ON, STATE_TURNING_OFF);
@@ -2443,7 +2455,10 @@ class BluetoothManagerService {
            mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
            mHandler.removeMessages(MESSAGE_BLUETOOTH_STATE_CHANGE);
            mState.set(STATE_OFF);
            mState.set(STATE_OFF);
            // enable
            // enable
            addActiveLog(reason, mContext.getPackageName(), true);
            addActiveLog(
                    BluetoothProtoEnums.ENABLE_DISABLE_REASON_USER_SWITCH,
                    mContext.getPackageName(),
                    true);
            // mEnable flag could have been reset on stopBle. Reenable it.
            // mEnable flag could have been reset on stopBle. Reenable it.
            mEnable = true;
            mEnable = true;
            handleEnable(mQuietEnable);
            handleEnable(mQuietEnable);