From 262f9492541fcb8b6ac45f3be4fe63eaf8e23b33 Mon Sep 17 00:00:00 2001 From: Rahul Sabnis Date: Thu, 14 Apr 2022 10:43:17 -0700 Subject: Listen for Intent.ACTION_USER_SWITCHED in BluetoothManagerService instead of using the SystemService#onUserSwitching method Tag: #feature Bug: 228920751 Test: Manual Ignore-AOSP-First: Change only present in tm-dev Change-Id: Ic8c32c29662389bd62fa1684de9679c369545874 (cherry picked from commit 320618b16ff4923727d81be41dafe81a3876b123) Merged-In: Ic8c32c29662389bd62fa1684de9679c369545874 --- .../server/bluetooth/BluetoothManagerService.java | 27 +++++++++++++--------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'service/java/com/android/server/bluetooth/BluetoothManagerService.java') diff --git a/service/java/com/android/server/bluetooth/BluetoothManagerService.java b/service/java/com/android/server/bluetooth/BluetoothManagerService.java index 4ee392f08d..b9af522955 100644 --- a/service/java/com/android/server/bluetooth/BluetoothManagerService.java +++ b/service/java/com/android/server/bluetooth/BluetoothManagerService.java @@ -216,7 +216,6 @@ public class BluetoothManagerService extends IBluetoothManager.Stub { private final ReentrantReadWriteLock mBluetoothLock = new ReentrantReadWriteLock(); private boolean mBinding; private boolean mUnbinding; - private int mForegroundUserId; private BluetoothModeChangeHelper mBluetoothModeChangeHelper; @@ -544,7 +543,6 @@ public class BluetoothManagerService extends IBluetoothManager.Stub { String value = SystemProperties.get( "persist.sys.fflag.override.settings_bluetooth_hearing_aid"); - mForegroundUserId = UserHandle.SYSTEM.getIdentifier(); if (!TextUtils.isEmpty(value)) { boolean isHearingAidEnabled = Boolean.parseBoolean(value); @@ -567,11 +565,23 @@ public class BluetoothManagerService extends IBluetoothManager.Stub { IntentFilter filterUser = new IntentFilter(); filterUser.addAction(UserManager.ACTION_USER_RESTRICTIONS_CHANGED); + filterUser.addAction(Intent.ACTION_USER_SWITCHED); filterUser.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); mContext.registerReceiverForAllUsers(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - onUserRestrictionsChanged(getSendingUser()); + switch (intent.getAction()) { + case Intent.ACTION_USER_SWITCHED: + int foregroundUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); + propagateForegroundUserId(foregroundUserId); + break; + case UserManager.ACTION_USER_RESTRICTIONS_CHANGED: + onUserRestrictionsChanged(getSendingUser()); + break; + default: + Log.e(TAG, "Unknown broadcast received in BluetoothManagerService receiver" + + " registered across all users"); + } } }, filterUser, null, null); @@ -2199,6 +2209,9 @@ public class BluetoothManagerService extends IBluetoothManager.Stub { mBluetoothBinder = service; mBluetooth = IBluetooth.Stub.asInterface(service); + int foregroundUserId = ActivityManager.getCurrentUser(); + propagateForegroundUserId(foregroundUserId); + if (!isNameAndAddressSet()) { Message getMsg = mHandler.obtainMessage(MESSAGE_GET_NAME_AND_ADDRESS); mHandler.sendMessage(getMsg); @@ -2238,8 +2251,6 @@ public class BluetoothManagerService extends IBluetoothManager.Stub { BluetoothAdapter.STATE_BLE_TURNING_ON, BluetoothAdapter.STATE_BLE_ON, BluetoothAdapter.STATE_BLE_TURNING_OFF)); - } else { - propagateForegroundUserId(mForegroundUserId); } break; } @@ -2377,12 +2388,6 @@ public class BluetoothManagerService extends IBluetoothManager.Stub { } mHandler.removeMessages(MESSAGE_USER_SWITCHED); - // Save the foreground user id and propagate to BT process after it's restarted - int toUserId = msg.arg1; - if (mForegroundUserId != toUserId) { - mForegroundUserId = toUserId; - } - /* disable and enable BT when detect a user switch */ if (mBluetooth != null && isEnabled()) { restartForReason(BluetoothProtoEnums.ENABLE_DISABLE_REASON_USER_SWITCH); -- cgit v1.2.3