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

@@ -19,6 +19,7 @@ package com.android.settings.bluetooth;
import static android.os.Process.BLUETOOTH_UID;
import android.app.settings.SettingsEnums;
import android.bluetooth.BluetoothCsipSetCoordinator;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
@@ -39,9 +40,12 @@ import com.android.settings.R;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.BluetoothUtils.ErrorListener;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothManager.BluetoothManagerCallback;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
@@ -225,4 +229,47 @@ public final class Utils {
}
throw new NameNotFoundException("Could not find main bluetooth package");
}
/**
* Returns all cachedBluetoothDevices with the same groupId.
* @param cachedBluetoothDevice The main cachedBluetoothDevice.
* @return all cachedBluetoothDevices with the same groupId.
*/
public static List<CachedBluetoothDevice> getAllOfCachedBluetoothDevices(Context context,
CachedBluetoothDevice cachedBluetoothDevice) {
List<CachedBluetoothDevice> cachedBluetoothDevices = new ArrayList<>();
if (cachedBluetoothDevice == null) {
Log.e(TAG, "getAllOfCachedBluetoothDevices: no cachedBluetoothDevice");
return cachedBluetoothDevices;
}
int deviceGroupId = cachedBluetoothDevice.getGroupId();
if (deviceGroupId == BluetoothCsipSetCoordinator.GROUP_ID_INVALID) {
cachedBluetoothDevices.add(cachedBluetoothDevice);
return cachedBluetoothDevices;
}
final LocalBluetoothManager localBtMgr = Utils.getLocalBtManager(context);
if (localBtMgr == null) {
Log.e(TAG, "getAllOfCachedBluetoothDevices: no LocalBluetoothManager");
return cachedBluetoothDevices;
}
CachedBluetoothDevice mainDevice =
localBtMgr.getCachedDeviceManager().getCachedDevicesCopy().stream()
.filter(cachedDevice -> cachedDevice.getGroupId() == deviceGroupId)
.findFirst().orElse(null);
if (mainDevice == null) {
Log.e(TAG, "getAllOfCachedBluetoothDevices: 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
+ " , cachedBluetoothDevice = " + cachedBluetoothDevice
+ " , deviceList = " + cachedBluetoothDevices);
return cachedBluetoothDevices;
}
}