[Audiosharing] Impl active device switch for calls and alarms

Flagged with enable_le_audio_sharing

Bug: 305620450
Test: Manual
Change-Id: Id1e417cd1ae48bbb4ddebb4b30e2001e18bef7ee
This commit is contained in:
Yiyi Shen
2023-11-16 14:00:11 +08:00
parent 1a2bf18eae
commit d1f0efc282
4 changed files with 124 additions and 12 deletions

View File

@@ -16,23 +16,42 @@
package com.android.settings.connecteddevice.audiosharing;
import android.annotation.Nullable;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceScreen;
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.LocalBluetoothManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** PreferenceController to control the dialog to choose the active device for calls and alarms */
public class CallsAndAlarmsPreferenceController extends AudioSharingBasePreferenceController {
public class CallsAndAlarmsPreferenceController extends AudioSharingBasePreferenceController
implements BluetoothCallback, DefaultLifecycleObserver {
private static final String TAG = "CallsAndAlarmsPreferenceController";
private static final String PREF_KEY = "calls_and_alarms";
private final LocalBluetoothManager mLocalBtManager;
private DashboardFragment mFragment;
Map<Integer, List<CachedBluetoothDevice>> mGroupedConnectedDevices = new HashMap<>();
private ArrayList<AudioSharingDeviceItem> mDeviceItemsInSharingSession = new ArrayList<>();
public CallsAndAlarmsPreferenceController(Context context) {
super(context, PREF_KEY);
mLocalBtManager = Utils.getLocalBtManager(mContext);
}
@Override
@@ -43,17 +62,60 @@ public class CallsAndAlarmsPreferenceController extends AudioSharingBasePreferen
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
updateDeviceItemsInSharingSession();
// mDeviceItemsInSharingSession is ordered. The active device is the first place if exits.
if (!mDeviceItemsInSharingSession.isEmpty()
&& mDeviceItemsInSharingSession.get(0).isActive()) {
mPreference.setSummary(mDeviceItemsInSharingSession.get(0).getName());
} else {
mPreference.setSummary("");
}
mPreference.setOnPreferenceClickListener(
preference -> {
if (mFragment != null) {
CallsAndAlarmsDialogFragment.show(mFragment);
} else {
if (mFragment == null) {
Log.w(TAG, "Dialog fail to show due to null host.");
return true;
}
updateDeviceItemsInSharingSession();
if (mDeviceItemsInSharingSession.size() >= 2) {
CallsAndAlarmsDialogFragment.show(
mFragment,
mDeviceItemsInSharingSession,
(AudioSharingDeviceItem item) -> {
for (CachedBluetoothDevice device :
mGroupedConnectedDevices.get(item.getGroupId())) {
device.setActive();
}
});
}
return true;
});
}
@Override
public void onStart(@NonNull LifecycleOwner owner) {
if (mLocalBtManager != null) {
mLocalBtManager.getEventManager().registerCallback(this);
}
}
@Override
public void onStop(@NonNull LifecycleOwner owner) {
if (mLocalBtManager != null) {
mLocalBtManager.getEventManager().unregisterCallback(this);
}
}
@Override
public void onActiveDeviceChanged(
@Nullable CachedBluetoothDevice activeDevice, int bluetoothProfile) {
if (bluetoothProfile != BluetoothProfile.LE_AUDIO) {
Log.d(TAG, "Ignore onActiveDeviceChanged, not LE_AUDIO profile");
return;
}
mPreference.setSummary(activeDevice == null ? "" : activeDevice.getName());
}
/**
* Initialize the controller.
*
@@ -62,4 +124,12 @@ public class CallsAndAlarmsPreferenceController extends AudioSharingBasePreferen
public void init(DashboardFragment fragment) {
this.mFragment = fragment;
}
private void updateDeviceItemsInSharingSession() {
mGroupedConnectedDevices =
AudioSharingUtils.fetchConnectedDevicesByGroupId(mLocalBtManager);
mDeviceItemsInSharingSession =
AudioSharingUtils.buildOrderedDeviceItemsInSharingSession(
mGroupedConnectedDevices, mLocalBtManager);
}
}