• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.BluetoothAdapter;
20 import android.content.Context;
21 import android.util.Log;
22 
23 import androidx.annotation.NonNull;
24 import androidx.annotation.Nullable;
25 import androidx.lifecycle.DefaultLifecycleObserver;
26 import androidx.lifecycle.LifecycleOwner;
27 import androidx.preference.Preference;
28 import androidx.preference.PreferenceScreen;
29 
30 import com.android.settings.bluetooth.Utils;
31 import com.android.settings.core.BasePreferenceController;
32 import com.android.settingslib.bluetooth.BluetoothUtils;
33 import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
34 import com.android.settingslib.bluetooth.LocalBluetoothManager;
35 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
36 import com.android.settingslib.utils.ThreadUtils;
37 
38 public abstract class AudioSharingBasePreferenceController extends BasePreferenceController
39         implements DefaultLifecycleObserver {
40     private static final String TAG = "AudioSharingBasePreferenceController";
41 
42     private final BluetoothAdapter mBluetoothAdapter;
43     @Nullable private final LocalBluetoothManager mBtManager;
44     @Nullable private final LocalBluetoothProfileManager mProfileManager;
45     @Nullable protected final LocalBluetoothLeBroadcast mBroadcast;
46     @Nullable protected Preference mPreference;
47 
AudioSharingBasePreferenceController(Context context, String preferenceKey)48     public AudioSharingBasePreferenceController(Context context, String preferenceKey) {
49         super(context, preferenceKey);
50         mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
51         mBtManager = Utils.getLocalBtManager(context);
52         mProfileManager = mBtManager == null ? null : mBtManager.getProfileManager();
53         mBroadcast = mProfileManager == null ? null : mProfileManager.getLeAudioBroadcastProfile();
54     }
55 
56     @Override
getAvailabilityStatus()57     public int getAvailabilityStatus() {
58         return (BluetoothUtils.isAudioSharingUIAvailable(mContext))
59                 ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
60     }
61 
62     @Override
displayPreference(PreferenceScreen screen)63     public void displayPreference(PreferenceScreen screen) {
64         super.displayPreference(screen);
65         mPreference = screen.findPreference(getPreferenceKey());
66     }
67 
68     @Override
onStart(@onNull LifecycleOwner owner)69     public void onStart(@NonNull LifecycleOwner owner) {
70         updateVisibility();
71     }
72 
73     /** Update the visibility of the preference. */
updateVisibility()74     protected void updateVisibility() {
75         if (mPreference == null) {
76             Log.d(TAG, "Skip updateVisibility, null preference");
77             return;
78         }
79         var unused =
80                 ThreadUtils.postOnBackgroundThread(
81                         () -> {
82                             if (!isAvailable()) {
83                                 Log.w(TAG, "Skip updateVisibility, unavailable preference");
84                                 AudioSharingUtils.postOnMainThread(
85                                         mContext,
86                                         () -> { // Check nullability to pass NullAway check
87                                             if (mPreference != null) {
88                                                 mPreference.setVisible(false);
89                                             }
90                                         });
91                                 return;
92                             }
93                             boolean isBtOn = isBluetoothStateOn();
94                             boolean isProfileReady =
95                                     AudioSharingUtils.isAudioSharingProfileReady(mProfileManager);
96                             boolean isBroadcasting = isBroadcasting();
97                             boolean isVisible = isBtOn && isProfileReady && isBroadcasting;
98                             Log.d(
99                                     TAG,
100                                     "updateVisibility, isBtOn = "
101                                             + isBtOn
102                                             + ", isProfileReady = "
103                                             + isProfileReady
104                                             + ", isBroadcasting = "
105                                             + isBroadcasting);
106                             AudioSharingUtils.postOnMainThread(
107                                     mContext,
108                                     () -> { // Check nullability to pass NullAway check
109                                         if (mPreference != null) {
110                                             mPreference.setVisible(isVisible);
111                                         }
112                                     });
113                         });
114     }
115 
116     /**
117      * Triggered when {@link AudioSharingDashboardFragment} receive onAudioSharingProfilesConnected
118      * callbacks.
119      */
onAudioSharingProfilesConnected()120     protected void onAudioSharingProfilesConnected() {}
121 
isBroadcasting()122     protected boolean isBroadcasting() {
123         return mBroadcast != null && mBroadcast.isEnabled(null);
124     }
125 
isBluetoothStateOn()126     protected boolean isBluetoothStateOn() {
127         return mBluetoothAdapter != null && mBluetoothAdapter.isEnabled();
128     }
129 }
130