1 /* 2 * Copyright (C) 2024 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.audiostreams; 18 19 import android.bluetooth.BluetoothAdapter; 20 import android.content.Intent; 21 import android.os.Bundle; 22 import android.util.Log; 23 24 import androidx.annotation.Nullable; 25 26 import com.android.settings.SettingsActivity; 27 import com.android.settings.bluetooth.Utils; 28 import com.android.settings.connecteddevice.audiosharing.AudioSharingUtils; 29 import com.android.settingslib.bluetooth.BluetoothUtils; 30 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager; 31 32 public class AudioStreamConfirmDialogActivity extends SettingsActivity 33 implements LocalBluetoothProfileManager.ServiceListener { 34 private static final String TAG = "AudioStreamConfirmDialogActivity"; 35 @Nullable private LocalBluetoothProfileManager mProfileManager; 36 @Nullable private Bundle mSavedState; 37 @Nullable private Intent mIntent; 38 39 @Override isToolbarEnabled()40 protected boolean isToolbarEnabled() { 41 return false; 42 } 43 44 @Override onCreate(Bundle savedState)45 protected void onCreate(Bundle savedState) { 46 var localBluetoothManager = Utils.getLocalBluetoothManager(this); 47 mProfileManager = 48 localBluetoothManager == null ? null : localBluetoothManager.getProfileManager(); 49 super.onCreate(savedState); 50 } 51 52 @Override createUiFromIntent(@ullable Bundle savedState, Intent intent)53 protected void createUiFromIntent(@Nullable Bundle savedState, Intent intent) { 54 if (BluetoothUtils.isAudioSharingUIAvailable(this) 55 && !isBluetoothUnsupportedOrOff() 56 && !AudioSharingUtils.isAudioSharingProfileReady(mProfileManager)) { 57 Log.d(TAG, "createUiFromIntent() : supported but not ready, skip createUiFromIntent"); 58 mSavedState = savedState; 59 mIntent = intent; 60 return; 61 } 62 63 Log.d( 64 TAG, 65 "createUiFromIntent() : not supported or already connected, starting" 66 + " createUiFromIntent"); 67 super.createUiFromIntent(savedState, intent); 68 } 69 70 @Override onStart()71 public void onStart() { 72 if (BluetoothUtils.isAudioSharingUIAvailable(this) 73 && !isBluetoothUnsupportedOrOff() 74 && !AudioSharingUtils.isAudioSharingProfileReady(mProfileManager)) { 75 Log.d(TAG, "onStart() : supported but not ready, listen to service ready"); 76 if (mProfileManager != null) { 77 mProfileManager.addServiceListener(this); 78 } 79 } 80 super.onStart(); 81 } 82 83 @Override onStop()84 public void onStop() { 85 if (mProfileManager != null) { 86 mProfileManager.removeServiceListener(this); 87 } 88 super.onStop(); 89 } 90 91 @Override onServiceConnected()92 public void onServiceConnected() { 93 if (BluetoothUtils.isAudioSharingUIAvailable(this) 94 && !isBluetoothUnsupportedOrOff() 95 && AudioSharingUtils.isAudioSharingProfileReady(mProfileManager)) { 96 if (mProfileManager != null) { 97 mProfileManager.removeServiceListener(this); 98 } 99 if (mIntent != null) { 100 Log.d(TAG, "onServiceConnected() : service ready, starting createUiFromIntent"); 101 super.createUiFromIntent(mSavedState, mIntent); 102 } 103 } 104 } 105 106 @Override onServiceDisconnected()107 public void onServiceDisconnected() {} 108 109 @Override isValidFragment(String fragmentName)110 protected boolean isValidFragment(String fragmentName) { 111 return AudioStreamConfirmDialog.class.getName().equals(fragmentName); 112 } 113 isBluetoothUnsupportedOrOff()114 private static boolean isBluetoothUnsupportedOrOff() { 115 var adapter = BluetoothAdapter.getDefaultAdapter(); 116 return adapter == null || !adapter.isEnabled(); 117 } 118 } 119