[Audiosharing] Impl audio sharing feature provider in Settings
Add createAvailableMediaDeviceGroupController interface to provide different controller in Settings and SettingsGoogle. Bug: 324023639 Test: atest Change-Id: Ibf2ea2620c878e609eb937ff6947f5aaa0b89e7a
This commit is contained in:
@@ -17,18 +17,17 @@ 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.Nullable;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.lifecycle.DefaultLifecycleObserver;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceGroup;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
@@ -38,138 +37,66 @@ 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;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
implements DefaultLifecycleObserver, 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 @Nullable 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) {}
|
||||
@Nullable private BluetoothDeviceUpdater mBluetoothDeviceUpdater;
|
||||
@Nullable private FragmentManager mFragmentManager;
|
||||
|
||||
@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) {
|
||||
public AvailableMediaDeviceGroupController(
|
||||
Context context,
|
||||
@Nullable DashboardFragment fragment,
|
||||
@Nullable Lifecycle lifecycle) {
|
||||
super(context, KEY);
|
||||
if (fragment != null) {
|
||||
init(fragment);
|
||||
}
|
||||
if (lifecycle != null) {
|
||||
lifecycle.addObserver(this);
|
||||
}
|
||||
mLocalBluetoothManager = Utils.getLocalBtManager(mContext);
|
||||
mExecutor = Executors.newSingleThreadExecutor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
public void onStart(@NonNull LifecycleOwner owner) {
|
||||
if (mLocalBluetoothManager == null) {
|
||||
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();
|
||||
if (mBluetoothDeviceUpdater != null) {
|
||||
mBluetoothDeviceUpdater.registerCallback();
|
||||
mBluetoothDeviceUpdater.refreshPreference();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
public void onStop(@NonNull LifecycleOwner owner) {
|
||||
if (mLocalBluetoothManager == null) {
|
||||
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);
|
||||
}
|
||||
if (mBluetoothDeviceUpdater != null) {
|
||||
mBluetoothDeviceUpdater.unregisterCallback();
|
||||
}
|
||||
mBluetoothDeviceUpdater.unregisterCallback();
|
||||
mLocalBluetoothManager.getEventManager().unregisterCallback(this);
|
||||
}
|
||||
|
||||
@@ -178,12 +105,16 @@ public class AvailableMediaDeviceGroupController extends BasePreferenceControlle
|
||||
super.displayPreference(screen);
|
||||
|
||||
mPreferenceGroup = screen.findPreference(KEY);
|
||||
mPreferenceGroup.setVisible(false);
|
||||
if (mPreferenceGroup != null) {
|
||||
mPreferenceGroup.setVisible(false);
|
||||
}
|
||||
|
||||
if (isAvailable()) {
|
||||
updateTitle();
|
||||
mBluetoothDeviceUpdater.setPrefContext(screen.getContext());
|
||||
mBluetoothDeviceUpdater.forceUpdate();
|
||||
if (mBluetoothDeviceUpdater != null) {
|
||||
mBluetoothDeviceUpdater.setPrefContext(screen.getContext());
|
||||
mBluetoothDeviceUpdater.forceUpdate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -201,17 +132,21 @@ public class AvailableMediaDeviceGroupController extends BasePreferenceControlle
|
||||
|
||||
@Override
|
||||
public void onDeviceAdded(Preference preference) {
|
||||
if (mPreferenceGroup.getPreferenceCount() == 0) {
|
||||
mPreferenceGroup.setVisible(true);
|
||||
if (mPreferenceGroup != null) {
|
||||
if (mPreferenceGroup.getPreferenceCount() == 0) {
|
||||
mPreferenceGroup.setVisible(true);
|
||||
}
|
||||
mPreferenceGroup.addPreference(preference);
|
||||
}
|
||||
mPreferenceGroup.addPreference(preference);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeviceRemoved(Preference preference) {
|
||||
mPreferenceGroup.removePreference(preference);
|
||||
if (mPreferenceGroup.getPreferenceCount() == 0) {
|
||||
mPreferenceGroup.setVisible(false);
|
||||
if (mPreferenceGroup != null) {
|
||||
mPreferenceGroup.removePreference(preference);
|
||||
if (mPreferenceGroup.getPreferenceCount() == 0) {
|
||||
mPreferenceGroup.setVisible(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -253,14 +188,16 @@ public class AvailableMediaDeviceGroupController extends BasePreferenceControlle
|
||||
}
|
||||
|
||||
private void updateTitle() {
|
||||
if (isAudioModeOngoingCall(mContext)) {
|
||||
// in phone call
|
||||
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));
|
||||
if (mPreferenceGroup != null) {
|
||||
if (isAudioModeOngoingCall(mContext)) {
|
||||
// in phone call
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user