[Audiosharing] Update the media device based on assistant callback

Bug: 305620450
Test: atest AvailableMediaDeviceGroupControllerTest
Change-Id: I7a19edb4e408c59ead553ec10fd9e963674518a6
This commit is contained in:
Yiyi Shen
2023-12-13 18:14:42 +08:00
parent 928942c80f
commit bf6e273ccb
2 changed files with 209 additions and 59 deletions

View File

@@ -17,11 +17,16 @@ package com.android.settings.connecteddevice;
import static com.android.settingslib.Utils.isAudioModeOngoingCall;
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.content.pm.PackageManager;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.fragment.app.FragmentManager;
import androidx.preference.Preference;
@@ -33,36 +38,94 @@ import com.android.settings.accessibility.HearingAidUtils;
import com.android.settings.bluetooth.AvailableMediaBluetoothDeviceUpdater;
import com.android.settings.bluetooth.BluetoothDeviceUpdater;
import com.android.settings.bluetooth.Utils;
import com.android.settings.connecteddevice.audiosharing.AudioSharingUtils;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
/**
* Controller to maintain the {@link androidx.preference.PreferenceGroup} for all
* available media devices. It uses {@link DevicePreferenceCallback}
* to add/remove {@link Preference}
* Controller to maintain the {@link androidx.preference.PreferenceGroup} for all available media
* devices. It uses {@link DevicePreferenceCallback} to add/remove {@link Preference}
*/
public class AvailableMediaDeviceGroupController extends BasePreferenceController
implements LifecycleObserver, OnStart, OnStop, DevicePreferenceCallback, BluetoothCallback {
private static final boolean DEBUG = BluetoothUtils.D;
private static final String TAG = "AvailableMediaDeviceGroupController";
private static final String KEY = "available_device_list";
@VisibleForTesting
PreferenceGroup mPreferenceGroup;
@VisibleForTesting
LocalBluetoothManager mLocalBluetoothManager;
@VisibleForTesting PreferenceGroup mPreferenceGroup;
@VisibleForTesting LocalBluetoothManager mLocalBluetoothManager;
private final Executor mExecutor;
private BluetoothDeviceUpdater mBluetoothDeviceUpdater;
private FragmentManager mFragmentManager;
private BluetoothLeBroadcastAssistant.Callback mAssistantCallback =
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) {
mBluetoothDeviceUpdater.forceUpdate();
}
@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) {
mBluetoothDeviceUpdater.forceUpdate();
}
@Override
public void onSourceRemoveFailed(
@NonNull BluetoothDevice sink, int sourceId, int reason) {}
@Override
public void onReceiveStateChanged(
BluetoothDevice sink,
int sourceId,
BluetoothLeBroadcastReceiveState state) {}
};
public AvailableMediaDeviceGroupController(Context context) {
super(context, KEY);
mLocalBluetoothManager = Utils.getLocalBtManager(mContext);
mExecutor = Executors.newSingleThreadExecutor();
}
@Override
@@ -71,6 +134,18 @@ public class AvailableMediaDeviceGroupController extends BasePreferenceControlle
Log.e(TAG, "onStart() Bluetooth is not supported on this device");
return;
}
if (AudioSharingUtils.isFeatureEnabled()) {
LocalBluetoothLeBroadcastAssistant assistant =
mLocalBluetoothManager
.getProfileManager()
.getLeAudioBroadcastAssistantProfile();
if (assistant != null) {
if (DEBUG) {
Log.d(TAG, "onStart() Register callbacks for assistant.");
}
assistant.registerServiceCallBack(mExecutor, mAssistantCallback);
}
}
mBluetoothDeviceUpdater.registerCallback();
mLocalBluetoothManager.getEventManager().registerCallback(this);
mBluetoothDeviceUpdater.refreshPreference();
@@ -82,6 +157,18 @@ public class AvailableMediaDeviceGroupController extends BasePreferenceControlle
Log.e(TAG, "onStop() Bluetooth is not supported on this device");
return;
}
if (AudioSharingUtils.isFeatureEnabled()) {
LocalBluetoothLeBroadcastAssistant assistant =
mLocalBluetoothManager
.getProfileManager()
.getLeAudioBroadcastAssistantProfile();
if (assistant != null) {
if (DEBUG) {
Log.d(TAG, "onStop() Register callbacks for assistant.");
}
assistant.unregisterServiceCallBack(mAssistantCallback);
}
}
mBluetoothDeviceUpdater.unregisterCallback();
mLocalBluetoothManager.getEventManager().unregisterCallback(this);
}
@@ -130,8 +217,11 @@ public class AvailableMediaDeviceGroupController extends BasePreferenceControlle
public void init(DashboardFragment fragment) {
mFragmentManager = fragment.getParentFragmentManager();
mBluetoothDeviceUpdater = new AvailableMediaBluetoothDeviceUpdater(fragment.getContext(),
AvailableMediaDeviceGroupController.this, fragment.getMetricsCategory());
mBluetoothDeviceUpdater =
new AvailableMediaBluetoothDeviceUpdater(
fragment.getContext(),
AvailableMediaDeviceGroupController.this,
fragment.getMetricsCategory());
}
@VisibleForTesting
@@ -157,20 +247,20 @@ public class AvailableMediaDeviceGroupController extends BasePreferenceControlle
}
if (bluetoothProfile == BluetoothProfile.HEARING_AID) {
HearingAidUtils.launchHearingAidPairingDialog(mFragmentManager, activeDevice,
getMetricsCategory());
HearingAidUtils.launchHearingAidPairingDialog(
mFragmentManager, activeDevice, getMetricsCategory());
}
}
private void updateTitle() {
if (isAudioModeOngoingCall(mContext)) {
// in phone call
mPreferenceGroup.
setTitle(mContext.getString(R.string.connected_device_call_device_title));
mPreferenceGroup.setTitle(
mContext.getString(R.string.connected_device_call_device_title));
} else {
// without phone call
mPreferenceGroup.
setTitle(mContext.getString(R.string.connected_device_media_device_title));
mPreferenceGroup.setTitle(
mContext.getString(R.string.connected_device_media_device_title));
}
}
}