1 /* 2 * Copyright (C) 2023 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.connecteddevice.audiosharing; 18 19 import android.bluetooth.BluetoothLeBroadcast; 20 import android.bluetooth.BluetoothLeBroadcastMetadata; 21 import android.content.Context; 22 import android.util.Log; 23 24 import androidx.annotation.NonNull; 25 import androidx.annotation.Nullable; 26 import androidx.annotation.VisibleForTesting; 27 import androidx.lifecycle.DefaultLifecycleObserver; 28 import androidx.lifecycle.LifecycleOwner; 29 import androidx.preference.Preference; 30 import androidx.preference.PreferenceScreen; 31 32 import com.android.settings.R; 33 import com.android.settings.bluetooth.Utils; 34 import com.android.settings.core.BasePreferenceController; 35 import com.android.settingslib.bluetooth.BluetoothCallback; 36 import com.android.settingslib.bluetooth.BluetoothEventManager; 37 import com.android.settingslib.bluetooth.BluetoothUtils; 38 import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast; 39 import com.android.settingslib.bluetooth.LocalBluetoothManager; 40 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager; 41 import com.android.settingslib.utils.ThreadUtils; 42 43 import java.util.concurrent.Executor; 44 import java.util.concurrent.Executors; 45 46 public class AudioSharingPreferenceController extends BasePreferenceController 47 implements DefaultLifecycleObserver, BluetoothCallback, 48 LocalBluetoothProfileManager.ServiceListener { 49 private static final String TAG = "AudioSharingPreferenceController"; 50 private static final String CONNECTED_DEVICES_PREF_KEY = 51 "connected_device_audio_sharing_settings"; 52 private static final String CONNECTION_PREFERENCES_PREF_KEY = "audio_sharing_settings"; 53 54 @Nullable private final LocalBluetoothManager mBtManager; 55 @Nullable private final LocalBluetoothProfileManager mProfileManager; 56 @Nullable private final BluetoothEventManager mEventManager; 57 @Nullable private final LocalBluetoothLeBroadcast mBroadcast; 58 @Nullable private Preference mPreference; 59 private final Executor mExecutor; 60 61 @VisibleForTesting 62 final BluetoothLeBroadcast.Callback mBroadcastCallback = 63 new BluetoothLeBroadcast.Callback() { 64 @Override 65 public void onBroadcastStarted(int reason, int broadcastId) { 66 refreshPreference(); 67 } 68 69 @Override 70 public void onBroadcastStartFailed(int reason) {} 71 72 @Override 73 public void onBroadcastMetadataChanged( 74 int broadcastId, @NonNull BluetoothLeBroadcastMetadata metadata) {} 75 76 @Override 77 public void onBroadcastStopped(int reason, int broadcastId) { 78 refreshPreference(); 79 } 80 81 @Override 82 public void onBroadcastStopFailed(int reason) {} 83 84 @Override 85 public void onBroadcastUpdated(int reason, int broadcastId) {} 86 87 @Override 88 public void onBroadcastUpdateFailed(int reason, int broadcastId) {} 89 90 @Override 91 public void onPlaybackStarted(int reason, int broadcastId) {} 92 93 @Override 94 public void onPlaybackStopped(int reason, int broadcastId) {} 95 }; 96 AudioSharingPreferenceController(Context context, String preferenceKey)97 public AudioSharingPreferenceController(Context context, String preferenceKey) { 98 super(context, preferenceKey); 99 mBtManager = Utils.getLocalBtManager(context); 100 mProfileManager = mBtManager == null ? null : mBtManager.getProfileManager(); 101 mEventManager = mBtManager == null ? null : mBtManager.getEventManager(); 102 mBroadcast = mProfileManager == null ? null : mProfileManager.getLeAudioBroadcastProfile(); 103 mExecutor = Executors.newSingleThreadExecutor(); 104 } 105 106 @Override onStart(@onNull LifecycleOwner owner)107 public void onStart(@NonNull LifecycleOwner owner) { 108 if (!isAvailable()) { 109 Log.d(TAG, "Skip register callbacks, feature not support"); 110 return; 111 } 112 if (mEventManager == null || mBroadcast == null) { 113 Log.d(TAG, "Skip register callbacks, profile not ready"); 114 return; 115 } 116 mEventManager.registerCallback(this); 117 mBroadcast.registerServiceCallBack(mExecutor, mBroadcastCallback); 118 if (!AudioSharingUtils.isAudioSharingProfileReady(mProfileManager)) { 119 if (mProfileManager != null) { 120 mProfileManager.addServiceListener(this); 121 } 122 Log.d(TAG, "Skip updateVisibility. Profile is not ready."); 123 return; 124 } 125 updateVisibility(); 126 } 127 128 @Override onStop(@onNull LifecycleOwner owner)129 public void onStop(@NonNull LifecycleOwner owner) { 130 if (!isAvailable()) { 131 Log.d(TAG, "Skip unregister callbacks, feature not support"); 132 return; 133 } 134 if (mEventManager == null || mBroadcast == null) { 135 Log.d(TAG, "Skip register callbacks, profile not ready"); 136 return; 137 } 138 mEventManager.unregisterCallback(this); 139 mBroadcast.unregisterServiceCallBack(mBroadcastCallback); 140 if (mProfileManager != null) { 141 mProfileManager.removeServiceListener(this); 142 } 143 } 144 145 @Override displayPreference(@onNull PreferenceScreen screen)146 public void displayPreference(@NonNull PreferenceScreen screen) { 147 super.displayPreference(screen); 148 mPreference = screen.findPreference(getPreferenceKey()); 149 // super.displayPreference set the visibility based on isAvailable() 150 // immediately set the preference invisible on Connected devices page to avoid the audio 151 // sharing entrance being shown before updateVisibility(need binder call) take effects. 152 if (mPreference != null && CONNECTED_DEVICES_PREF_KEY.equals(getPreferenceKey())) { 153 mPreference.setVisible(false); 154 } 155 } 156 157 @Override getAvailabilityStatus()158 public int getAvailabilityStatus() { 159 return BluetoothUtils.isAudioSharingUIAvailable(mContext) ? AVAILABLE 160 : UNSUPPORTED_ON_DEVICE; 161 } 162 163 @Override onServiceConnected()164 public void onServiceConnected() { 165 if (AudioSharingUtils.isAudioSharingProfileReady(mProfileManager)) { 166 Log.d(TAG, "onServiceConnected, audio sharing ready"); 167 refreshPreference(); 168 if (mProfileManager != null) { 169 mProfileManager.removeServiceListener(this); 170 } 171 } 172 } 173 174 @Override onServiceDisconnected()175 public void onServiceDisconnected() {} 176 177 @Override getSummary()178 public CharSequence getSummary() { 179 return switch (getPreferenceKey()) { 180 case CONNECTION_PREFERENCES_PREF_KEY -> BluetoothUtils.isBroadcasting(mBtManager) 181 ? mContext.getString(R.string.audio_sharing_summary_on) 182 : mContext.getString(R.string.audio_sharing_summary_off); 183 default -> ""; 184 }; 185 } 186 187 @Override onBluetoothStateChanged(@dapterState int bluetoothState)188 public void onBluetoothStateChanged(@AdapterState int bluetoothState) { 189 refreshPreference(); 190 } 191 refreshPreference()192 private void refreshPreference() { 193 switch (getPreferenceKey()) { 194 // Audio sharing entrance on Connected devices page has no summary, but its visibility 195 // will change based on audio sharing state 196 case CONNECTED_DEVICES_PREF_KEY -> updateVisibility(); 197 // Audio sharing entrance on Connection preferences page always show up, but its summary 198 // will change based on audio sharing state 199 case CONNECTION_PREFERENCES_PREF_KEY -> refreshSummary(); 200 } 201 } 202 updateVisibility()203 private void updateVisibility() { 204 if (mPreference == null) { 205 return; 206 } 207 switch (getPreferenceKey()) { 208 case CONNECTED_DEVICES_PREF_KEY -> { 209 var unused = 210 ThreadUtils.postOnBackgroundThread( 211 () -> { 212 boolean visible = BluetoothUtils.isBroadcasting(mBtManager); 213 AudioSharingUtils.postOnMainThread( 214 mContext, 215 () -> { 216 // Check nullability to pass NullAway check 217 if (mPreference != null) { 218 mPreference.setVisible(visible); 219 } 220 }); 221 }); 222 } 223 } 224 } 225 refreshSummary()226 private void refreshSummary() { 227 if (mPreference == null) { 228 return; 229 } 230 var unused = 231 ThreadUtils.postOnBackgroundThread( 232 () -> { 233 final CharSequence summary = getSummary(); 234 AudioSharingUtils.postOnMainThread( 235 mContext, 236 () -> { 237 // Check nullability to pass NullAway check 238 if (mPreference != null) { 239 mPreference.setSummary(summary); 240 } 241 }); 242 }); 243 } 244 } 245