Connected devices page did not show correct summary when member device connect

Root Cause: CsipDeviceManager only refreshes UI when switching member device content.

Solution:
* CsipDeviceManager needs to call refresh() on main device when it's new
  member device added.
* UI widget Settings/BluetoothDevice also need to monitor it's member device status to refresh UI.

Bug: 344947362
Test: atest BluetoothDevicePreferenceTest
Flag: EXEMPT bugfix
Change-Id: I58f9e2fc209d4e87631784d0538b1647228f4c1a
This commit is contained in:
jasonwshsu
2024-07-19 19:16:11 +08:00
parent 93b093033a
commit 15c6533ef9
5 changed files with 204 additions and 74 deletions

View File

@@ -89,7 +89,7 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
private LocalBluetoothManager mManager;
private LocalBluetoothProfileManager mProfileManager;
private CachedBluetoothDevice mCachedDevice;
private List<CachedBluetoothDevice> mAllOfCachedDevices;
private Set<CachedBluetoothDevice> mCachedDeviceGroup;
private Map<String, List<CachedBluetoothDevice>> mProfileDeviceMap =
new HashMap<String, List<CachedBluetoothDevice>>();
private boolean mIsLeContactSharingEnabled = false;
@@ -105,7 +105,7 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
mManager = manager;
mProfileManager = mManager.getProfileManager();
mCachedDevice = device;
mAllOfCachedDevices = Utils.getAllOfCachedBluetoothDevices(mManager, mCachedDevice);
mCachedDeviceGroup = Utils.findAllCachedBluetoothDevicesByGroupId(mManager, mCachedDevice);
}
@Override
@@ -310,10 +310,10 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
private List<LocalBluetoothProfile> getProfiles() {
List<LocalBluetoothProfile> result = new ArrayList<>();
mProfileDeviceMap.clear();
if (mAllOfCachedDevices == null || mAllOfCachedDevices.isEmpty()) {
if (mCachedDeviceGroup == null || mCachedDeviceGroup.isEmpty()) {
return result;
}
for (CachedBluetoothDevice cachedItem : mAllOfCachedDevices) {
for (CachedBluetoothDevice cachedItem : mCachedDeviceGroup) {
List<LocalBluetoothProfile> tmpResult = cachedItem.getUiAccessibleProfiles();
for (LocalBluetoothProfile profile : tmpResult) {
if (mProfileDeviceMap.containsKey(profile.toString())) {
@@ -514,7 +514,7 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
@Override
public void onPause() {
for (CachedBluetoothDevice item : mAllOfCachedDevices) {
for (CachedBluetoothDevice item : mCachedDeviceGroup) {
item.unregisterCallback(this);
}
mProfileManager.removeServiceListener(this);
@@ -523,7 +523,7 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
@Override
public void onResume() {
updateLeAudioConfig();
for (CachedBluetoothDevice item : mAllOfCachedDevices) {
for (CachedBluetoothDevice item : mCachedDeviceGroup) {
item.registerCallback(this);
}
mProfileManager.addServiceListener(this);
@@ -545,11 +545,11 @@ public class BluetoothDetailsProfilesController extends BluetoothDetailsControll
@Override
public void onDeviceAttributesChanged() {
for (CachedBluetoothDevice item : mAllOfCachedDevices) {
for (CachedBluetoothDevice item : mCachedDeviceGroup) {
item.unregisterCallback(this);
}
mAllOfCachedDevices = Utils.getAllOfCachedBluetoothDevices(mManager, mCachedDevice);
for (CachedBluetoothDevice item : mAllOfCachedDevices) {
mCachedDeviceGroup = Utils.findAllCachedBluetoothDevicesByGroupId(mManager, mCachedDevice);
for (CachedBluetoothDevice item : mCachedDeviceGroup) {
item.registerCallback(this);
}

View File

@@ -46,6 +46,7 @@ import com.android.settings.R;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.widget.GearPreference;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.utils.ThreadUtils;
@@ -55,6 +56,7 @@ import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/**
* BluetoothDevicePreference is the preference type used to display each remote
@@ -76,7 +78,10 @@ public final class BluetoothDevicePreference extends GearPreference {
}
private final CachedBluetoothDevice mCachedDevice;
private Set<CachedBluetoothDevice> mCachedDeviceGroup;
private final UserManager mUserManager;
private final LocalBluetoothManager mLocalBtManager;
private Set<BluetoothDevice> mBluetoothDevices;
@VisibleForTesting
@@ -113,6 +118,21 @@ public final class BluetoothDevicePreference extends GearPreference {
@Override
public void onDeviceAttributesChanged() {
onPreferenceAttributesChanged();
Set<CachedBluetoothDevice> newCachedDeviceGroup = new HashSet<>(
Utils.findAllCachedBluetoothDevicesByGroupId(mLocalBtManager, mCachedDevice));
if (!mCachedDeviceGroup.equals(newCachedDeviceGroup)) {
for (CachedBluetoothDevice cachedBluetoothDevice : mCachedDeviceGroup) {
cachedBluetoothDevice.unregisterCallback(this);
}
unregisterMetadataChangedListener();
mCachedDeviceGroup = newCachedDeviceGroup;
for (CachedBluetoothDevice cachedBluetoothDevice : mCachedDeviceGroup) {
cachedBluetoothDevice.registerCallback(getContext().getMainExecutor(), this);
}
registerMetadataChangedListener();
}
}
}
@@ -121,6 +141,7 @@ public final class BluetoothDevicePreference extends GearPreference {
super(context, null);
mResources = getContext().getResources();
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
mLocalBtManager = Utils.getLocalBluetoothManager(context);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mShowDevicesWithoutNames = showDeviceWithoutNames;
@@ -131,6 +152,8 @@ public final class BluetoothDevicePreference extends GearPreference {
}
mCachedDevice = cachedDevice;
mCachedDeviceGroup = new HashSet<>(
Utils.findAllCachedBluetoothDevicesByGroupId(mLocalBtManager, mCachedDevice));
mCallback = new BluetoothDevicePreferenceCallback();
mId = sNextId.getAndIncrement();
mType = type;
@@ -164,7 +187,9 @@ public final class BluetoothDevicePreference extends GearPreference {
protected void onPrepareForRemoval() {
super.onPrepareForRemoval();
if (!mIsCallbackRemoved) {
mCachedDevice.unregisterCallback(mCallback);
for (CachedBluetoothDevice cachedBluetoothDevice : mCachedDeviceGroup) {
cachedBluetoothDevice.unregisterCallback(mCallback);
}
unregisterMetadataChangedListener();
mIsCallbackRemoved = true;
}
@@ -178,7 +203,9 @@ public final class BluetoothDevicePreference extends GearPreference {
public void onAttached() {
super.onAttached();
if (mIsCallbackRemoved) {
mCachedDevice.registerCallback(mCallback);
for (CachedBluetoothDevice cachedBluetoothDevice : mCachedDeviceGroup) {
cachedBluetoothDevice.registerCallback(getContext().getMainExecutor(), mCallback);
}
registerMetadataChangedListener();
mIsCallbackRemoved = false;
}
@@ -189,7 +216,9 @@ public final class BluetoothDevicePreference extends GearPreference {
public void onDetached() {
super.onDetached();
if (!mIsCallbackRemoved) {
mCachedDevice.unregisterCallback(mCallback);
for (CachedBluetoothDevice cachedBluetoothDevice : mCachedDeviceGroup) {
cachedBluetoothDevice.unregisterCallback(mCallback);
}
unregisterMetadataChangedListener();
mIsCallbackRemoved = true;
}
@@ -200,16 +229,11 @@ public final class BluetoothDevicePreference extends GearPreference {
Log.d(TAG, "No mBluetoothAdapter");
return;
}
if (mBluetoothDevices == null) {
mBluetoothDevices = new HashSet<>();
}
mBluetoothDevices.clear();
if (mCachedDevice.getDevice() != null) {
mBluetoothDevices.add(mCachedDevice.getDevice());
}
for (CachedBluetoothDevice cbd : mCachedDevice.getMemberDevice()) {
mBluetoothDevices.add(cbd.getDevice());
}
mBluetoothDevices = mCachedDeviceGroup.stream()
.map(CachedBluetoothDevice::getDevice)
.collect(Collectors.toCollection(HashSet::new));
if (mBluetoothDevices.isEmpty()) {
Log.d(TAG, "No BT device to register.");
return;

View File

@@ -47,7 +47,7 @@ import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
import com.android.settingslib.widget.LayoutPreference;
import java.util.List;
import java.util.Set;
/**
* This class adds a header with device name and status (connected/disconnected, etc.).
@@ -90,7 +90,7 @@ public class LeAudioBluetoothDetailsHeaderController extends BasePreferenceContr
LayoutPreference mLayoutPreference;
LocalBluetoothManager mManager;
private CachedBluetoothDevice mCachedDevice;
private List<CachedBluetoothDevice> mAllOfCachedDevices;
private Set<CachedBluetoothDevice> mCachedDeviceGroup;
@VisibleForTesting
Handler mHandler = new Handler(Looper.getMainLooper());
@VisibleForTesting
@@ -128,7 +128,7 @@ public class LeAudioBluetoothDetailsHeaderController extends BasePreferenceContr
return;
}
mIsRegisterCallback = true;
for (CachedBluetoothDevice item : mAllOfCachedDevices) {
for (CachedBluetoothDevice item : mCachedDeviceGroup) {
item.registerCallback(this);
}
refresh();
@@ -139,7 +139,7 @@ public class LeAudioBluetoothDetailsHeaderController extends BasePreferenceContr
if (!mIsRegisterCallback) {
return;
}
for (CachedBluetoothDevice item : mAllOfCachedDevices) {
for (CachedBluetoothDevice item : mCachedDeviceGroup) {
item.unregisterCallback(this);
}
@@ -155,7 +155,7 @@ public class LeAudioBluetoothDetailsHeaderController extends BasePreferenceContr
mCachedDevice = cachedBluetoothDevice;
mManager = bluetoothManager;
mProfileManager = bluetoothManager.getProfileManager();
mAllOfCachedDevices = Utils.getAllOfCachedBluetoothDevices(mManager, mCachedDevice);
mCachedDeviceGroup = Utils.findAllCachedBluetoothDevicesByGroupId(mManager, mCachedDevice);
}
@VisibleForTesting
@@ -230,7 +230,7 @@ public class LeAudioBluetoothDetailsHeaderController extends BasePreferenceContr
// Init the battery layouts.
hideAllOfBatteryLayouts();
LeAudioProfile leAudioProfile = mProfileManager.getLeAudioProfile();
if (mAllOfCachedDevices.isEmpty()) {
if (mCachedDeviceGroup.isEmpty()) {
Log.e(TAG, "There is no LeAudioProfile.");
return;
}
@@ -244,7 +244,7 @@ public class LeAudioBluetoothDetailsHeaderController extends BasePreferenceContr
return;
}
for (CachedBluetoothDevice cachedDevice : mAllOfCachedDevices) {
for (CachedBluetoothDevice cachedDevice : mCachedDeviceGroup) {
int deviceId = leAudioProfile.getAudioLocation(cachedDevice.getDevice());
Log.d(TAG, "LeAudioDevices:" + cachedDevice.getDevice().getAnonymizedAddress()
+ ", deviceId:" + deviceId);
@@ -300,15 +300,15 @@ public class LeAudioBluetoothDetailsHeaderController extends BasePreferenceContr
@Override
public void onDeviceAttributesChanged() {
for (CachedBluetoothDevice item : mAllOfCachedDevices) {
for (CachedBluetoothDevice item : mCachedDeviceGroup) {
item.unregisterCallback(this);
}
mAllOfCachedDevices = Utils.getAllOfCachedBluetoothDevices(mManager, mCachedDevice);
for (CachedBluetoothDevice item : mAllOfCachedDevices) {
mCachedDeviceGroup = Utils.findAllCachedBluetoothDevicesByGroupId(mManager, mCachedDevice);
for (CachedBluetoothDevice item : mCachedDeviceGroup) {
item.registerCallback(this);
}
if (!mAllOfCachedDevices.isEmpty()) {
if (!mCachedDeviceGroup.isEmpty()) {
refresh();
}
}

View File

@@ -48,8 +48,9 @@ import com.android.settingslib.utils.ThreadUtils;
import com.google.common.base.Supplier;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
@@ -239,12 +240,12 @@ public final class Utils {
* @param cachedBluetoothDevice The main cachedBluetoothDevice.
* @return all cachedBluetoothDevices with the same groupId.
*/
public static List<CachedBluetoothDevice> getAllOfCachedBluetoothDevices(
public static Set<CachedBluetoothDevice> findAllCachedBluetoothDevicesByGroupId(
LocalBluetoothManager localBtMgr,
CachedBluetoothDevice cachedBluetoothDevice) {
List<CachedBluetoothDevice> cachedBluetoothDevices = new ArrayList<>();
Set<CachedBluetoothDevice> cachedBluetoothDevices = new HashSet<>();
if (cachedBluetoothDevice == null) {
Log.e(TAG, "getAllOfCachedBluetoothDevices: no cachedBluetoothDevice");
Log.e(TAG, "findAllCachedBluetoothDevicesByGroupId: no cachedBluetoothDevice");
return cachedBluetoothDevices;
}
int deviceGroupId = cachedBluetoothDevice.getGroupId();
@@ -254,7 +255,7 @@ public final class Utils {
}
if (localBtMgr == null) {
Log.e(TAG, "getAllOfCachedBluetoothDevices: no LocalBluetoothManager");
Log.e(TAG, "findAllCachedBluetoothDevicesByGroupId: no LocalBluetoothManager");
return cachedBluetoothDevices;
}
CachedBluetoothDevice mainDevice =
@@ -262,16 +263,14 @@ public final class Utils {
.filter(cachedDevice -> cachedDevice.getGroupId() == deviceGroupId)
.findFirst().orElse(null);
if (mainDevice == null) {
Log.e(TAG, "getAllOfCachedBluetoothDevices: groupId = " + deviceGroupId
Log.e(TAG, "findAllCachedBluetoothDevicesByGroupId: groupId = " + deviceGroupId
+ ", no main device.");
return cachedBluetoothDevices;
}
cachedBluetoothDevice = mainDevice;
cachedBluetoothDevices.add(cachedBluetoothDevice);
for (CachedBluetoothDevice member : cachedBluetoothDevice.getMemberDevice()) {
cachedBluetoothDevices.add(member);
}
Log.d(TAG, "getAllOfCachedBluetoothDevices: groupId = " + deviceGroupId
cachedBluetoothDevices.addAll(cachedBluetoothDevice.getMemberDevice());
Log.d(TAG, "findAllCachedBluetoothDevicesByGroupId: groupId = " + deviceGroupId
+ " , cachedBluetoothDevice = " + cachedBluetoothDevice
+ " , deviceList = " + cachedBluetoothDevices);
return cachedBluetoothDevices;