Merge "[Audiosharing] Set earlist connected device active in sharing." into main

This commit is contained in:
Yiyi Shen
2023-11-23 09:39:44 +00:00
committed by Android (Google) Code Review
3 changed files with 36 additions and 3 deletions

View File

@@ -160,6 +160,7 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
+ ", reason = " + ", reason = "
+ reason); + reason);
mBluetoothDeviceUpdater.forceUpdate(); mBluetoothDeviceUpdater.forceUpdate();
AudioSharingUtils.updateActiveDeviceIfNeeded(mLocalBtManager);
} }
@Override @Override
@@ -204,6 +205,7 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
+ ", reason = " + ", reason = "
+ reason); + reason);
mBluetoothDeviceUpdater.forceUpdate(); mBluetoothDeviceUpdater.forceUpdate();
AudioSharingUtils.updateActiveDeviceIfNeeded(mLocalBtManager);
} }
@Override @Override
@@ -299,7 +301,7 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
mPreferenceGroup.setVisible(false); mPreferenceGroup.setVisible(false);
mAudioSharingSettingsPreference.setVisible(false); mAudioSharingSettingsPreference.setVisible(false);
if (isAvailable() && mBluetoothDeviceUpdater != null) { if (isAvailable()) {
mBluetoothDeviceUpdater.setPrefContext(screen.getContext()); mBluetoothDeviceUpdater.setPrefContext(screen.getContext());
mBluetoothDeviceUpdater.forceUpdate(); mBluetoothDeviceUpdater.forceUpdate();
} }
@@ -309,6 +311,7 @@ public class AudioSharingDevicePreferenceController extends BasePreferenceContro
public int getAvailabilityStatus() { public int getAvailabilityStatus() {
return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH) return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)
&& Flags.enableLeAudioSharing() && Flags.enableLeAudioSharing()
&& mBluetoothDeviceUpdater != null
? AVAILABLE_UNSEARCHABLE ? AVAILABLE_UNSEARCHABLE
: UNSUPPORTED_ON_DEVICE; : UNSUPPORTED_ON_DEVICE;
} }

View File

@@ -192,6 +192,7 @@ public class AudioSharingSwitchBarController extends BasePreferenceController
+ sourceId + sourceId
+ ", reason = " + ", reason = "
+ reason); + reason);
AudioSharingUtils.updateActiveDeviceIfNeeded(mBtManager);
} }
@Override @Override

View File

@@ -16,6 +16,7 @@
package com.android.settings.connecteddevice.audiosharing; package com.android.settings.connecteddevice.audiosharing;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothCsipSetCoordinator; import android.bluetooth.BluetoothCsipSetCoordinator;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcastReceiveState; import android.bluetooth.BluetoothLeBroadcastReceiveState;
@@ -89,11 +90,11 @@ public class AudioSharingUtils {
* @return A list of ordered connected devices eligible for the audio sharing. The active device * @return A list of ordered connected devices eligible for the audio sharing. The active device
* is placed in the first place if it exists. * is placed in the first place if it exists.
*/ */
public static ArrayList<CachedBluetoothDevice> buildOrderedConnectedLeadDevices( public static List<CachedBluetoothDevice> buildOrderedConnectedLeadDevices(
LocalBluetoothManager localBtManager, LocalBluetoothManager localBtManager,
Map<Integer, List<CachedBluetoothDevice>> groupedConnectedDevices, Map<Integer, List<CachedBluetoothDevice>> groupedConnectedDevices,
boolean filterByInSharing) { boolean filterByInSharing) {
ArrayList<CachedBluetoothDevice> orderedDevices = new ArrayList<>(); List<CachedBluetoothDevice> orderedDevices = new ArrayList<>();
LocalBluetoothLeBroadcastAssistant assistant = LocalBluetoothLeBroadcastAssistant assistant =
localBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile(); localBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
if (assistant == null) return orderedDevices; if (assistant == null) return orderedDevices;
@@ -234,4 +235,32 @@ public class AudioSharingUtils {
ThreadUtils.postOnMainThread( ThreadUtils.postOnMainThread(
() -> Toast.makeText(context, message, Toast.LENGTH_LONG).show()); () -> Toast.makeText(context, message, Toast.LENGTH_LONG).show());
} }
/** Automatically update active device if needed. */
public static void updateActiveDeviceIfNeeded(LocalBluetoothManager localBtManager) {
if (localBtManager == null) return;
Map<Integer, List<CachedBluetoothDevice>> groupedConnectedDevices =
fetchConnectedDevicesByGroupId(localBtManager);
List<CachedBluetoothDevice> devicesInSharing =
buildOrderedConnectedLeadDevices(
localBtManager, groupedConnectedDevices, /* filterByInSharing= */ true);
if (devicesInSharing.isEmpty()) return;
List<BluetoothDevice> devices =
BluetoothAdapter.getDefaultAdapter().getMostRecentlyConnectedDevices();
CachedBluetoothDevice targetDevice = null;
int targetDeviceIdx = -1;
for (CachedBluetoothDevice device : devicesInSharing) {
if (devices.contains(device.getDevice())) {
int idx = devices.indexOf(device.getDevice());
if (idx > targetDeviceIdx) {
targetDeviceIdx = idx;
targetDevice = device;
}
}
}
if (targetDevice != null && !isActiveLeAudioDevice(targetDevice)) {
Log.d(TAG, "Set active device: " + targetDevice.getDevice().getAnonymizedAddress());
targetDevice.setActive();
}
}
} }