• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.bluetooth;
18 
19 import static com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast.EXTRA_START_LE_AUDIO_SHARING;
20 
21 import android.app.settings.SettingsEnums;
22 import android.content.Context;
23 import android.os.Bundle;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.Nullable;
27 import androidx.preference.Preference;
28 import androidx.preference.PreferenceCategory;
29 import androidx.preference.PreferenceFragmentCompat;
30 import androidx.preference.PreferenceScreen;
31 
32 import com.android.settings.R;
33 import com.android.settings.connecteddevice.audiosharing.AudioSharingDashboardFragment;
34 import com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsDashboardFragment;
35 import com.android.settings.connecteddevice.audiosharing.audiostreams.AudioStreamsHelper;
36 import com.android.settings.core.SubSettingLauncher;
37 import com.android.settingslib.bluetooth.BluetoothUtils;
38 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
39 import com.android.settingslib.bluetooth.LocalBluetoothManager;
40 import com.android.settingslib.core.lifecycle.Lifecycle;
41 
42 /** Controller for audio sharing control preferences. */
43 public class BluetoothDetailsAudioSharingController extends BluetoothDetailsController {
44     private static final String KEY_AUDIO_SHARING_CONTROL = "audio_sharing_control";
45     private static final String KEY_AUDIO_SHARING = "audio_sharing";
46     private static final String KEY_FIND_AUDIO_STREAM = "find_audio_stream";
47 
48     @Nullable PreferenceCategory mProfilesContainer;
49     LocalBluetoothManager mLocalBluetoothManager;
50 
BluetoothDetailsAudioSharingController( @onNull Context context, @NonNull PreferenceFragmentCompat fragment, @NonNull LocalBluetoothManager localBtManager, @NonNull CachedBluetoothDevice device, @NonNull Lifecycle lifecycle)51     public BluetoothDetailsAudioSharingController(
52             @NonNull Context context,
53             @NonNull PreferenceFragmentCompat fragment,
54             @NonNull LocalBluetoothManager localBtManager,
55             @NonNull CachedBluetoothDevice device,
56             @NonNull Lifecycle lifecycle) {
57         super(context, fragment, device, lifecycle);
58         mLocalBluetoothManager = localBtManager;
59     }
60 
61     @Override
isAvailable()62     public boolean isAvailable() {
63         return BluetoothUtils.isAudioSharingUIAvailable(mContext)
64                 && mCachedDevice.isConnectedLeAudioDevice();
65     }
66 
67     @Override
init(PreferenceScreen screen)68     protected void init(PreferenceScreen screen) {
69         mProfilesContainer = screen.findPreference(KEY_AUDIO_SHARING_CONTROL);
70     }
71 
72     @Override
refresh()73     protected void refresh() {
74         if (mProfilesContainer == null) {
75             return;
76         }
77         if (!isAvailable()) {
78             mProfilesContainer.setVisible(false);
79             return;
80         }
81         mProfilesContainer.setVisible(true);
82         mProfilesContainer.removeAll();
83         mProfilesContainer.addPreference(createAudioSharingPreference());
84         if ((BluetoothUtils.isActiveLeAudioDevice(mCachedDevice)
85                         || AudioStreamsHelper.hasBroadcastSource(
86                                 mCachedDevice, mLocalBluetoothManager))
87                 && !BluetoothUtils.isBroadcasting(mLocalBluetoothManager)) {
88             mProfilesContainer.addPreference(createFindAudioStreamPreference());
89         }
90     }
91 
createAudioSharingPreference()92     private Preference createAudioSharingPreference() {
93         Preference audioSharingPref = new Preference(mContext);
94         audioSharingPref.setKey(KEY_AUDIO_SHARING);
95         audioSharingPref.setTitle(R.string.audio_sharing_title);
96         audioSharingPref.setIcon(com.android.settingslib.R.drawable.ic_bt_le_audio_sharing);
97         audioSharingPref.setOnPreferenceClickListener(
98                 preference -> {
99                     Bundle args = new Bundle();
100                     args.putBoolean(EXTRA_START_LE_AUDIO_SHARING, true);
101                     new SubSettingLauncher(mContext)
102                             .setDestination(AudioSharingDashboardFragment.class.getName())
103                             .setSourceMetricsCategory(SettingsEnums.BLUETOOTH_DEVICE_DETAILS)
104                             .setArguments(args)
105                             .launch();
106                     return true;
107                 });
108         return audioSharingPref;
109     }
110 
createFindAudioStreamPreference()111     private Preference createFindAudioStreamPreference() {
112         Preference findAudioStreamPref = new Preference(mContext);
113         findAudioStreamPref.setKey(KEY_FIND_AUDIO_STREAM);
114         findAudioStreamPref.setTitle(R.string.audio_streams_main_page_title);
115         findAudioStreamPref.setIcon(com.android.settingslib.R.drawable.ic_bt_le_audio_sharing);
116         findAudioStreamPref.setOnPreferenceClickListener(
117                 preference -> {
118                     new SubSettingLauncher(mContext)
119                             .setDestination(AudioStreamsDashboardFragment.class.getName())
120                             .setSourceMetricsCategory(SettingsEnums.BLUETOOTH_DEVICE_DETAILS)
121                             .launch();
122                     return true;
123                 });
124         return findAudioStreamPref;
125     }
126 
127     @Override
128     @NonNull
getPreferenceKey()129     public String getPreferenceKey() {
130         return KEY_AUDIO_SHARING_CONTROL;
131     }
132 }
133