1 /*
2  * Copyright 2018 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 package com.android.settings.bluetooth;
17 
18 import android.bluetooth.BluetoothProfile;
19 import android.content.Context;
20 import android.media.AudioManager;
21 import android.util.Log;
22 
23 import androidx.preference.Preference;
24 
25 import com.android.settings.connecteddevice.DevicePreferenceCallback;
26 import com.android.settings.connecteddevice.audiosharing.AudioSharingUtils;
27 import com.android.settingslib.bluetooth.BluetoothUtils;
28 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
29 import com.android.settingslib.bluetooth.LocalBluetoothManager;
30 
31 /** Controller to maintain available media Bluetooth devices */
32 public class AvailableMediaBluetoothDeviceUpdater extends BluetoothDeviceUpdater
33         implements Preference.OnPreferenceClickListener {
34 
35     private static final String TAG = "AvailableMediaBluetoothDeviceUpdater";
36     private static final boolean DBG = Log.isLoggable(BluetoothDeviceUpdater.TAG, Log.DEBUG);
37 
38     private static final String PREF_KEY = "available_media_bt";
39 
40     private final AudioManager mAudioManager;
41     private final LocalBluetoothManager mLocalBtManager;
42 
AvailableMediaBluetoothDeviceUpdater( Context context, DevicePreferenceCallback devicePreferenceCallback, int metricsCategory)43     public AvailableMediaBluetoothDeviceUpdater(
44             Context context,
45             DevicePreferenceCallback devicePreferenceCallback,
46             int metricsCategory) {
47         super(context, devicePreferenceCallback, metricsCategory);
48         mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
49         mLocalBtManager = Utils.getLocalBtManager(context);
50     }
51 
52     @Override
onAudioModeChanged()53     public void onAudioModeChanged() {
54         forceUpdate();
55     }
56 
57     @Override
isFilterMatched(CachedBluetoothDevice cachedDevice)58     public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) {
59         final int audioMode = mAudioManager.getMode();
60         final int currentAudioProfile;
61 
62         if (audioMode == AudioManager.MODE_RINGTONE
63                 || audioMode == AudioManager.MODE_IN_CALL
64                 || audioMode == AudioManager.MODE_IN_COMMUNICATION) {
65             // in phone call
66             currentAudioProfile = BluetoothProfile.HEADSET;
67         } else {
68             // without phone call
69             currentAudioProfile = BluetoothProfile.A2DP;
70         }
71 
72         boolean isFilterMatched = false;
73         if (isDeviceConnected(cachedDevice) && isDeviceInCachedDevicesList(cachedDevice)) {
74             Log.d(TAG, "isFilterMatched() current audio profile : " + currentAudioProfile);
75 
76             // If device is LE Audio, it is compatible with HFP and A2DP.
77             // It would show in Available Devices group if the audio sharing flag is disabled or
78             // the device is not in the audio sharing session.
79             if (cachedDevice.isConnectedLeAudioDevice()) {
80                 if (AudioSharingUtils.isFeatureEnabled()
81                         && BluetoothUtils.hasConnectedBroadcastSource(
82                                 cachedDevice, mLocalBtManager)) {
83                     Log.d(
84                             TAG,
85                             "Filter out device : "
86                                     + cachedDevice.getName()
87                                     + ", it is in audio sharing.");
88                     return false;
89 
90                 } else {
91                     Log.d(
92                             TAG,
93                             "isFilterMatched() device : "
94                                     + cachedDevice.getName()
95                                     + ", the LE Audio profile is connected and not in sharing "
96                                     + "if broadcast enabled.");
97                     return true;
98                 }
99             }
100 
101             // If device is Hearing Aid, it is compatible with HFP and A2DP.
102             // It would show in Available Devices group.
103             if (cachedDevice.isConnectedAshaHearingAidDevice()) {
104                 Log.d(
105                         TAG,
106                         "isFilterMatched() device : "
107                                 + cachedDevice.getName()
108                                 + ", the Hearing Aid profile is connected.");
109                 return true;
110             }
111 
112             // According to the current audio profile type,
113             // this page will show the bluetooth device that have corresponding profile.
114             // For example:
115             // If current audio profile is a2dp, show the bluetooth device that have a2dp profile.
116             // If current audio profile is headset,
117             // show the bluetooth device that have headset profile.
118             switch (currentAudioProfile) {
119                 case BluetoothProfile.A2DP:
120                     isFilterMatched = cachedDevice.isConnectedA2dpDevice();
121                     break;
122                 case BluetoothProfile.HEADSET:
123                     isFilterMatched = cachedDevice.isConnectedHfpDevice();
124                     break;
125             }
126             Log.d(
127                     TAG,
128                     "isFilterMatched() device : "
129                             + cachedDevice.getName()
130                             + ", isFilterMatched : "
131                             + isFilterMatched);
132         }
133         return isFilterMatched;
134     }
135 
136     @Override
onPreferenceClick(Preference preference)137     public boolean onPreferenceClick(Preference preference) {
138         mMetricsFeatureProvider.logClickedPreference(preference, mMetricsCategory);
139         mDevicePreferenceCallback.onDeviceClick(preference);
140         return true;
141     }
142 
143     @Override
getPreferenceKey()144     protected String getPreferenceKey() {
145         return PREF_KEY;
146     }
147 
148     @Override
getLogTag()149     protected String getLogTag() {
150         return TAG;
151     }
152 
153     @Override
update(CachedBluetoothDevice cachedBluetoothDevice)154     protected void update(CachedBluetoothDevice cachedBluetoothDevice) {
155         super.update(cachedBluetoothDevice);
156         Log.d(TAG, "Map : " + mPreferenceMap);
157     }
158 }
159