To refresh/register/unregister all of the devices with same groupId

Since the LE audio have two or more devices in the same group and
the UI show their status at one preference, the UI need to register
callback for each of the devices, and also refresh the deviceList.

Bug: 278155752
Test: local test to pairing the device and check the battery part
Change-Id: I2fcde92a1f68b8437465b234820f7bad13dfc444
This commit is contained in:
SongFerngWang
2023-05-18 15:59:41 +08:00
parent aa46e3d39d
commit ef7a75c394
3 changed files with 67 additions and 52 deletions

View File

@@ -16,7 +16,6 @@
package com.android.settings.bluetooth;
import android.bluetooth.BluetoothCsipSetCoordinator;
import android.bluetooth.BluetoothLeAudio;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
@@ -48,7 +47,6 @@ 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.ArrayList;
import java.util.List;
/**
@@ -91,7 +89,7 @@ public class LeAudioBluetoothDetailsHeaderController extends BasePreferenceContr
@VisibleForTesting
LayoutPreference mLayoutPreference;
private CachedBluetoothDevice mCachedDevice;
private List<CachedBluetoothDevice> mLeAudioDevices;
private List<CachedBluetoothDevice> mAllOfCachedDevices;
@VisibleForTesting
Handler mHandler = new Handler(Looper.getMainLooper());
@VisibleForTesting
@@ -129,12 +127,8 @@ public class LeAudioBluetoothDetailsHeaderController extends BasePreferenceContr
return;
}
mIsRegisterCallback = true;
if (mLeAudioDevices != null && !mLeAudioDevices.isEmpty()) {
for (CachedBluetoothDevice item : mLeAudioDevices) {
item.registerCallback(this);
}
} else {
mCachedDevice.registerCallback(this);
for (CachedBluetoothDevice item : mAllOfCachedDevices) {
item.registerCallback(this);
}
refresh();
}
@@ -144,13 +138,10 @@ public class LeAudioBluetoothDetailsHeaderController extends BasePreferenceContr
if (!mIsRegisterCallback) {
return;
}
if (mLeAudioDevices != null && !mLeAudioDevices.isEmpty()) {
for (CachedBluetoothDevice item : mLeAudioDevices) {
item.unregisterCallback(this);
}
} else {
mCachedDevice.unregisterCallback(this);
for (CachedBluetoothDevice item : mAllOfCachedDevices) {
item.unregisterCallback(this);
}
mIsRegisterCallback = false;
}
@@ -162,8 +153,7 @@ public class LeAudioBluetoothDetailsHeaderController extends BasePreferenceContr
LocalBluetoothManager bluetoothManager) {
mCachedDevice = cachedBluetoothDevice;
mProfileManager = bluetoothManager.getProfileManager();
mLeAudioDevices = getAllOfLeAudioDevices();
mAllOfCachedDevices = Utils.getAllOfCachedBluetoothDevices(mContext, mCachedDevice);
}
@VisibleForTesting
@@ -234,26 +224,11 @@ public class LeAudioBluetoothDetailsHeaderController extends BasePreferenceContr
updateBatteryLayout(R.id.bt_battery_right, BluetoothUtils.META_INT_ERROR);
}
private List<CachedBluetoothDevice> getAllOfLeAudioDevices() {
if (mCachedDevice == null) {
return null;
}
List<CachedBluetoothDevice> leAudioDevices = new ArrayList<>();
leAudioDevices.add(mCachedDevice);
if (mCachedDevice.getGroupId() != BluetoothCsipSetCoordinator.GROUP_ID_INVALID) {
for (CachedBluetoothDevice member : mCachedDevice.getMemberDevice()) {
leAudioDevices.add(member);
}
}
Log.d(TAG, "mLeAudioDevices is " + mLeAudioDevices);
return leAudioDevices;
}
private void updateBatteryLayout() {
// Init the battery layouts.
hideAllOfBatteryLayouts();
LeAudioProfile leAudioProfile = mProfileManager.getLeAudioProfile();
if (mLeAudioDevices == null || mLeAudioDevices.isEmpty()) {
if (mAllOfCachedDevices.isEmpty()) {
Log.e(TAG, "There is no LeAudioProfile.");
return;
}
@@ -267,7 +242,7 @@ public class LeAudioBluetoothDetailsHeaderController extends BasePreferenceContr
return;
}
for (CachedBluetoothDevice cachedDevice : mLeAudioDevices) {
for (CachedBluetoothDevice cachedDevice : mAllOfCachedDevices) {
int deviceId = leAudioProfile.getAudioLocation(cachedDevice.getDevice());
Log.d(TAG, "LeAudioDevices:" + cachedDevice.getDevice().getAnonymizedAddress()
+ ", deviceId:" + deviceId);
@@ -322,7 +297,15 @@ public class LeAudioBluetoothDetailsHeaderController extends BasePreferenceContr
@Override
public void onDeviceAttributesChanged() {
if (mCachedDevice != null) {
for (CachedBluetoothDevice item : mAllOfCachedDevices) {
item.unregisterCallback(this);
}
mAllOfCachedDevices = Utils.getAllOfCachedBluetoothDevices(mContext, mCachedDevice);
for (CachedBluetoothDevice item : mAllOfCachedDevices) {
item.registerCallback(this);
}
if (!mAllOfCachedDevices.isEmpty()) {
refresh();
}
}