Merge "[Audiosharing] Handle fallback device for calls and alarms." into main
This commit is contained in:
@@ -22,9 +22,12 @@ import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothLeBroadcastReceiveState;
|
||||
import android.bluetooth.BluetoothStatusCodes;
|
||||
import android.content.Context;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.android.settings.flags.Flags;
|
||||
import com.android.settingslib.bluetooth.BluetoothUtils;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
@@ -113,20 +116,7 @@ public class AudioSharingUtils {
|
||||
boolean filterByInSharing) {
|
||||
List<CachedBluetoothDevice> orderedDevices = new ArrayList<>();
|
||||
for (List<CachedBluetoothDevice> devices : groupedConnectedDevices.values()) {
|
||||
CachedBluetoothDevice leadDevice = null;
|
||||
for (CachedBluetoothDevice device : devices) {
|
||||
if (!device.getMemberDevice().isEmpty()) {
|
||||
leadDevice = device;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (leadDevice == null && !devices.isEmpty()) {
|
||||
leadDevice = devices.get(0);
|
||||
Log.d(
|
||||
TAG,
|
||||
"Empty member device, pick arbitrary device as the lead: "
|
||||
+ leadDevice.getDevice().getAnonymizedAddress());
|
||||
}
|
||||
@Nullable CachedBluetoothDevice leadDevice = getLeadDevice(devices);
|
||||
if (leadDevice == null) {
|
||||
Log.d(TAG, "Skip due to no lead device");
|
||||
continue;
|
||||
@@ -166,6 +156,29 @@ public class AudioSharingUtils {
|
||||
return orderedDevices;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the lead device from a list of devices with same group id.
|
||||
*
|
||||
* @param devices A list of devices with same group id.
|
||||
* @return The lead device
|
||||
*/
|
||||
@Nullable
|
||||
public static CachedBluetoothDevice getLeadDevice(
|
||||
@NonNull List<CachedBluetoothDevice> devices) {
|
||||
if (devices.isEmpty()) return null;
|
||||
for (CachedBluetoothDevice device : devices) {
|
||||
if (!device.getMemberDevice().isEmpty()) {
|
||||
return device;
|
||||
}
|
||||
}
|
||||
CachedBluetoothDevice leadDevice = devices.get(0);
|
||||
Log.d(
|
||||
TAG,
|
||||
"No lead device in the group, pick arbitrary device as the lead: "
|
||||
+ leadDevice.getDevice().getAnonymizedAddress());
|
||||
return leadDevice;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a list of ordered connected lead {@link AudioSharingDeviceItem}s eligible for audio
|
||||
* sharing. The active device is placed in the first place if it exists. The devices can be
|
||||
@@ -268,7 +281,7 @@ public class AudioSharingUtils {
|
||||
var groupedDevices = fetchConnectedDevicesByGroupId(manager);
|
||||
var leadDevices = buildOrderedConnectedLeadDevices(manager, groupedDevices, false);
|
||||
|
||||
if (!leadDevices.isEmpty() && AudioSharingUtils.isActiveLeAudioDevice(leadDevices.get(0))) {
|
||||
if (!leadDevices.isEmpty() && isActiveLeAudioDevice(leadDevices.get(0))) {
|
||||
return Optional.of(leadDevices.get(0));
|
||||
} else {
|
||||
Log.w(TAG, "getActiveSinksOnAssistant(): No active lead device!");
|
||||
@@ -379,4 +392,17 @@ public class AudioSharingUtils {
|
||||
Log.d(TAG, "getGroupId return invalid id for device: " + anonymizedAddress);
|
||||
return BluetoothCsipSetCoordinator.GROUP_ID_INVALID;
|
||||
}
|
||||
|
||||
/** Get the fallback active group id from SettingsProvider. */
|
||||
public static int getFallbackActiveGroupId(@NonNull Context context) {
|
||||
return Settings.Secure.getInt(
|
||||
context.getContentResolver(),
|
||||
"bluetooth_le_broadcast_fallback_active_group_id",
|
||||
BluetoothCsipSetCoordinator.GROUP_ID_INVALID);
|
||||
}
|
||||
|
||||
/** Post the runnable to main thread. */
|
||||
public static void postOnMainThread(@NonNull Context context, @NonNull Runnable runnable) {
|
||||
context.getMainExecutor().execute(runnable);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,8 +81,12 @@ public class CallsAndAlarmsDialogFragment extends InstrumentedDialogFragment {
|
||||
ArrayList<AudioSharingDeviceItem> deviceItems =
|
||||
arguments.getParcelableArrayList(BUNDLE_KEY_DEVICE_ITEMS);
|
||||
int checkedItem = -1;
|
||||
// deviceItems is ordered. The active device is put in the first place if it does exist
|
||||
if (!deviceItems.isEmpty() && deviceItems.get(0).isActive()) checkedItem = 0;
|
||||
for (AudioSharingDeviceItem item : deviceItems) {
|
||||
int fallbackActiveGroupId = AudioSharingUtils.getFallbackActiveGroupId(getContext());
|
||||
if (item.getGroupId() == fallbackActiveGroupId) {
|
||||
checkedItem = deviceItems.indexOf(item);
|
||||
}
|
||||
}
|
||||
String[] choices =
|
||||
deviceItems.stream().map(AudioSharingDeviceItem::getName).toArray(String[]::new);
|
||||
AlertDialog.Builder builder =
|
||||
|
||||
@@ -16,6 +16,12 @@
|
||||
|
||||
package com.android.settings.connecteddevice.audiosharing;
|
||||
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothCsipSetCoordinator;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothLeBroadcastAssistant;
|
||||
import android.bluetooth.BluetoothLeBroadcastMetadata;
|
||||
import android.bluetooth.BluetoothLeBroadcastReceiveState;
|
||||
import android.bluetooth.BluetoothProfile;
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
@@ -29,6 +35,7 @@ import com.android.settings.bluetooth.Utils;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settingslib.bluetooth.BluetoothCallback;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||
import com.android.settingslib.utils.ThreadUtils;
|
||||
|
||||
@@ -36,6 +43,8 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/** PreferenceController to control the dialog to choose the active device for calls and alarms */
|
||||
public class CallsAndAlarmsPreferenceController extends AudioSharingBasePreferenceController
|
||||
@@ -45,13 +54,74 @@ public class CallsAndAlarmsPreferenceController extends AudioSharingBasePreferen
|
||||
private static final String PREF_KEY = "calls_and_alarms";
|
||||
|
||||
private final LocalBluetoothManager mLocalBtManager;
|
||||
private final Executor mExecutor;
|
||||
@Nullable private LocalBluetoothLeBroadcastAssistant mAssistant = null;
|
||||
private DashboardFragment mFragment;
|
||||
Map<Integer, List<CachedBluetoothDevice>> mGroupedConnectedDevices = new HashMap<>();
|
||||
private ArrayList<AudioSharingDeviceItem> mDeviceItemsInSharingSession = new ArrayList<>();
|
||||
|
||||
private BluetoothLeBroadcastAssistant.Callback mBroadcastAssistantCallback =
|
||||
new BluetoothLeBroadcastAssistant.Callback() {
|
||||
@Override
|
||||
public void onSearchStarted(int reason) {}
|
||||
|
||||
@Override
|
||||
public void onSearchStartFailed(int reason) {}
|
||||
|
||||
@Override
|
||||
public void onSearchStopped(int reason) {}
|
||||
|
||||
@Override
|
||||
public void onSearchStopFailed(int reason) {}
|
||||
|
||||
@Override
|
||||
public void onSourceFound(@NonNull BluetoothLeBroadcastMetadata source) {}
|
||||
|
||||
@Override
|
||||
public void onSourceAdded(@NonNull BluetoothDevice sink, int sourceId, int reason) {
|
||||
Log.d(TAG, "onSourceAdded");
|
||||
updatePreference();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSourceAddFailed(
|
||||
@NonNull BluetoothDevice sink,
|
||||
@NonNull BluetoothLeBroadcastMetadata source,
|
||||
int reason) {}
|
||||
|
||||
@Override
|
||||
public void onSourceModified(
|
||||
@NonNull BluetoothDevice sink, int sourceId, int reason) {}
|
||||
|
||||
@Override
|
||||
public void onSourceModifyFailed(
|
||||
@NonNull BluetoothDevice sink, int sourceId, int reason) {}
|
||||
|
||||
@Override
|
||||
public void onSourceRemoved(
|
||||
@NonNull BluetoothDevice sink, int sourceId, int reason) {
|
||||
Log.d(TAG, "onSourceRemoved");
|
||||
updatePreference();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSourceRemoveFailed(
|
||||
@NonNull BluetoothDevice sink, int sourceId, int reason) {}
|
||||
|
||||
@Override
|
||||
public void onReceiveStateChanged(
|
||||
BluetoothDevice sink,
|
||||
int sourceId,
|
||||
BluetoothLeBroadcastReceiveState state) {}
|
||||
};
|
||||
|
||||
public CallsAndAlarmsPreferenceController(Context context) {
|
||||
super(context, PREF_KEY);
|
||||
mLocalBtManager = Utils.getLocalBtManager(mContext);
|
||||
if (mLocalBtManager != null) {
|
||||
mAssistant = mLocalBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
|
||||
}
|
||||
mExecutor = Executors.newSingleThreadExecutor();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -60,7 +130,7 @@ public class CallsAndAlarmsPreferenceController extends AudioSharingBasePreferen
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
public void displayPreference(@NonNull PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mPreference.setOnPreferenceClickListener(
|
||||
preference -> {
|
||||
@@ -69,14 +139,31 @@ public class CallsAndAlarmsPreferenceController extends AudioSharingBasePreferen
|
||||
return true;
|
||||
}
|
||||
updateDeviceItemsInSharingSession();
|
||||
if (mDeviceItemsInSharingSession.size() >= 2) {
|
||||
if (mDeviceItemsInSharingSession.size() >= 1) {
|
||||
CallsAndAlarmsDialogFragment.show(
|
||||
mFragment,
|
||||
mDeviceItemsInSharingSession,
|
||||
(AudioSharingDeviceItem item) -> {
|
||||
for (CachedBluetoothDevice device :
|
||||
mGroupedConnectedDevices.get(item.getGroupId())) {
|
||||
device.setActive();
|
||||
if (!mGroupedConnectedDevices.containsKey(item.getGroupId())) {
|
||||
return;
|
||||
}
|
||||
List<CachedBluetoothDevice> devices =
|
||||
mGroupedConnectedDevices.get(item.getGroupId());
|
||||
@Nullable
|
||||
CachedBluetoothDevice lead =
|
||||
AudioSharingUtils.getLeadDevice(devices);
|
||||
if (lead != null) {
|
||||
Log.d(
|
||||
TAG,
|
||||
"Set fallback active device: "
|
||||
+ lead.getDevice().getAnonymizedAddress());
|
||||
lead.setActive();
|
||||
updatePreference();
|
||||
} else {
|
||||
Log.w(
|
||||
TAG,
|
||||
"Fail to set fallback active device: no lead"
|
||||
+ " device");
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -90,6 +177,9 @@ public class CallsAndAlarmsPreferenceController extends AudioSharingBasePreferen
|
||||
if (mLocalBtManager != null) {
|
||||
mLocalBtManager.getEventManager().registerCallback(this);
|
||||
}
|
||||
if (mAssistant != null) {
|
||||
mAssistant.registerServiceCallBack(mExecutor, mBroadcastAssistantCallback);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -98,52 +188,58 @@ public class CallsAndAlarmsPreferenceController extends AudioSharingBasePreferen
|
||||
if (mLocalBtManager != null) {
|
||||
mLocalBtManager.getEventManager().unregisterCallback(this);
|
||||
}
|
||||
if (mAssistant != null) {
|
||||
mAssistant.unregisterServiceCallBack(mBroadcastAssistantCallback);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateVisibility() {
|
||||
if (mPreference == null) return;
|
||||
var unused =
|
||||
ThreadUtils.postOnBackgroundThread(
|
||||
() -> {
|
||||
boolean isVisible = isBroadcasting() && isBluetoothStateOn();
|
||||
if (!isVisible) {
|
||||
ThreadUtils.postOnMainThread(() -> mPreference.setVisible(false));
|
||||
} else {
|
||||
updateDeviceItemsInSharingSession();
|
||||
// mDeviceItemsInSharingSession is ordered. The active device is the
|
||||
// first
|
||||
// place if exits.
|
||||
if (!mDeviceItemsInSharingSession.isEmpty()
|
||||
&& mDeviceItemsInSharingSession.get(0).isActive()) {
|
||||
ThreadUtils.postOnMainThread(
|
||||
() -> {
|
||||
mPreference.setVisible(true);
|
||||
mPreference.setSummary(
|
||||
mDeviceItemsInSharingSession
|
||||
.get(0)
|
||||
.getName());
|
||||
});
|
||||
} else {
|
||||
ThreadUtils.postOnMainThread(
|
||||
() -> {
|
||||
mPreference.setVisible(true);
|
||||
mPreference.setSummary(
|
||||
"No active device in sharing");
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
var unused = ThreadUtils.postOnBackgroundThread(() -> updatePreference());
|
||||
}
|
||||
|
||||
private void updatePreference() {
|
||||
boolean isVisible = isBroadcasting() && isBluetoothStateOn();
|
||||
if (!isVisible) {
|
||||
AudioSharingUtils.postOnMainThread(mContext, () -> mPreference.setVisible(false));
|
||||
return;
|
||||
}
|
||||
updateDeviceItemsInSharingSession();
|
||||
int fallbackActiveGroupId = AudioSharingUtils.getFallbackActiveGroupId(mContext);
|
||||
Log.d(TAG, "updatePreference: get fallback active group " + fallbackActiveGroupId);
|
||||
if (fallbackActiveGroupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID) {
|
||||
for (AudioSharingDeviceItem item : mDeviceItemsInSharingSession) {
|
||||
if (item.getGroupId() == fallbackActiveGroupId) {
|
||||
AudioSharingUtils.postOnMainThread(
|
||||
mContext,
|
||||
() -> {
|
||||
mPreference.setSummary(item.getName());
|
||||
mPreference.setVisible(true);
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
AudioSharingUtils.postOnMainThread(
|
||||
mContext,
|
||||
() -> {
|
||||
mPreference.setSummary("No active device in sharing");
|
||||
mPreference.setVisible(true);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActiveDeviceChanged(
|
||||
@Nullable CachedBluetoothDevice activeDevice, int bluetoothProfile) {
|
||||
if (bluetoothProfile != BluetoothProfile.LE_AUDIO) {
|
||||
Log.d(TAG, "Ignore onActiveDeviceChanged, not LE_AUDIO profile");
|
||||
return;
|
||||
public void onProfileConnectionStateChanged(
|
||||
@NonNull CachedBluetoothDevice cachedDevice,
|
||||
@ConnectionState int state,
|
||||
int bluetoothProfile) {
|
||||
if (state == BluetoothAdapter.STATE_DISCONNECTED
|
||||
&& bluetoothProfile == BluetoothProfile.LE_AUDIO) {
|
||||
// The fallback active device could be updated if the previous fallback device is
|
||||
// disconnected.
|
||||
updatePreference();
|
||||
}
|
||||
mPreference.setSummary(activeDevice == null ? "" : activeDevice.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user