Add audio sharing entrypoint in device details

BUG: 383935069
Test: local tested
Flag: com.android.settingslib.flags.enable_le_audio_sharing
Change-Id: Ib90422d262eba84c3a308d8e4c5652d8c5f96808
This commit is contained in:
Haijie Hong
2025-01-08 12:57:56 +08:00
parent 45926f698b
commit cdcf50e3d2
5 changed files with 309 additions and 1 deletions

View File

@@ -0,0 +1,132 @@
/*
* Copyright (C) 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.bluetooth;
import static com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast.EXTRA_START_LE_AUDIO_SHARING;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.connecteddevice.audiosharing.AudioSharingDashboardFragment;
import com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsDashboardFragment;
import com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsHelper;
import com.android.settings.core.SubSettingLauncher;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.core.lifecycle.Lifecycle;
/** Controller for audio sharing control preferences. */
public class BluetoothDetailsAudioSharingController extends BluetoothDetailsController {
private static final String KEY_AUDIO_SHARING_CONTROL = "audio_sharing_control";
private static final String KEY_AUDIO_SHARING = "audio_sharing";
private static final String KEY_FIND_AUDIO_STREAM = "find_audio_stream";
@Nullable PreferenceCategory mProfilesContainer;
LocalBluetoothManager mLocalBluetoothManager;
public BluetoothDetailsAudioSharingController(
@NonNull Context context,
@NonNull PreferenceFragmentCompat fragment,
@NonNull LocalBluetoothManager localBtManager,
@NonNull CachedBluetoothDevice device,
@NonNull Lifecycle lifecycle) {
super(context, fragment, device, lifecycle);
mLocalBluetoothManager = localBtManager;
}
@Override
public boolean isAvailable() {
return BluetoothUtils.isAudioSharingUIAvailable(mContext)
&& mCachedDevice.isConnectedLeAudioDevice();
}
@Override
protected void init(PreferenceScreen screen) {
mProfilesContainer = screen.findPreference(KEY_AUDIO_SHARING_CONTROL);
}
@Override
protected void refresh() {
if (mProfilesContainer == null) {
return;
}
if (!isAvailable()) {
mProfilesContainer.setVisible(false);
return;
}
mProfilesContainer.setVisible(true);
mProfilesContainer.removeAll();
mProfilesContainer.addPreference(createAudioSharingPreference());
if ((BluetoothUtils.isActiveLeAudioDevice(mCachedDevice)
|| AudioStreamsHelper.hasConnectedBroadcastSource(
mCachedDevice, mLocalBluetoothManager))
&& !BluetoothUtils.isBroadcasting(mLocalBluetoothManager)) {
mProfilesContainer.addPreference(createFindAudioStreamPreference());
}
}
private Preference createAudioSharingPreference() {
Preference audioSharingPref = new Preference(mContext);
audioSharingPref.setKey(KEY_AUDIO_SHARING);
audioSharingPref.setTitle(R.string.audio_sharing_title);
audioSharingPref.setIcon(com.android.settingslib.R.drawable.ic_bt_le_audio_sharing);
audioSharingPref.setOnPreferenceClickListener(
preference -> {
Bundle args = new Bundle();
args.putBoolean(EXTRA_START_LE_AUDIO_SHARING, true);
new SubSettingLauncher(mContext)
.setDestination(AudioSharingDashboardFragment.class.getName())
.setSourceMetricsCategory(SettingsEnums.BLUETOOTH_DEVICE_DETAILS)
.setArguments(args)
.launch();
return true;
});
return audioSharingPref;
}
private Preference createFindAudioStreamPreference() {
Preference findAudioStreamPref = new Preference(mContext);
findAudioStreamPref.setKey(KEY_FIND_AUDIO_STREAM);
findAudioStreamPref.setTitle(R.string.audio_streams_main_page_title);
findAudioStreamPref.setIcon(com.android.settingslib.R.drawable.ic_bt_le_audio_sharing);
findAudioStreamPref.setOnPreferenceClickListener(
preference -> {
new SubSettingLauncher(mContext)
.setDestination(AudioStreamsDashboardFragment.class.getName())
.setSourceMetricsCategory(SettingsEnums.BLUETOOTH_DEVICE_DETAILS)
.launch();
return true;
});
return findAudioStreamPref;
}
@Override
@NonNull
public String getPreferenceKey() {
return KEY_AUDIO_SHARING_CONTROL;
}
}

View File

@@ -440,6 +440,9 @@ public class BluetoothDeviceDetailsFragment extends RestrictedDashboardFragment
context, this, mCachedDevice, lifecycle));
controllers.add(new BluetoothDetailsButtonsController(context, this, mCachedDevice,
lifecycle));
controllers.add(
new BluetoothDetailsAudioSharingController(
context, this, mManager, mCachedDevice, lifecycle));
controllers.add(new BluetoothDetailsCompanionAppsController(context, this,
mCachedDevice, lifecycle));
controllers.add(new BluetoothDetailsAudioDeviceTypeController(context, this, mManager,

View File

@@ -269,7 +269,7 @@ public class AudioStreamsHelper {
* @param localBtManager The BT manager to provide BT functions.
* @return Whether the device has connected to a broadcast source.
*/
private static boolean hasConnectedBroadcastSource(
public static boolean hasConnectedBroadcastSource(
CachedBluetoothDevice cachedDevice, LocalBluetoothManager localBtManager) {
if (localBtManager == null) {
Log.d(TAG, "Skip check hasConnectedBroadcastSource due to bt manager is null");