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.dashboard.DashboardFragment;
27 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
28 
29 /**
30  * Controller to maintain available media Bluetooth devices
31  */
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(TAG, Log.DEBUG);
37 
38     private static final String PREF_KEY = "available_media_bt";
39 
40     private final AudioManager mAudioManager;
41 
AvailableMediaBluetoothDeviceUpdater(Context context, DashboardFragment fragment, DevicePreferenceCallback devicePreferenceCallback)42     public AvailableMediaBluetoothDeviceUpdater(Context context, DashboardFragment fragment,
43             DevicePreferenceCallback devicePreferenceCallback) {
44         super(context, fragment, devicePreferenceCallback);
45         mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
46     }
47 
48     @Override
onAudioModeChanged()49     public void onAudioModeChanged() {
50         forceUpdate();
51     }
52 
53     @Override
isFilterMatched(CachedBluetoothDevice cachedDevice)54     public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) {
55         final int audioMode = mAudioManager.getMode();
56         final int currentAudioProfile;
57 
58         if (audioMode == AudioManager.MODE_RINGTONE
59                 || audioMode == AudioManager.MODE_IN_CALL
60                 || audioMode == AudioManager.MODE_IN_COMMUNICATION) {
61             // in phone call
62             currentAudioProfile = BluetoothProfile.HEADSET;
63         } else {
64             // without phone call
65             currentAudioProfile = BluetoothProfile.A2DP;
66         }
67 
68         boolean isFilterMatched = false;
69         if (isDeviceConnected(cachedDevice)) {
70             if (DBG) {
71                 Log.d(TAG, "isFilterMatched() current audio profile : " + currentAudioProfile);
72             }
73             // If device is Hearing Aid, it is compatible with HFP and A2DP.
74             // It would show in Available Devices group.
75             if (cachedDevice.isConnectedHearingAidDevice()) {
76                 return true;
77             }
78             // According to the current audio profile type,
79             // this page will show the bluetooth device that have corresponding profile.
80             // For example:
81             // If current audio profile is a2dp, show the bluetooth device that have a2dp profile.
82             // If current audio profile is headset,
83             // show the bluetooth device that have headset profile.
84             switch (currentAudioProfile) {
85                 case BluetoothProfile.A2DP:
86                     isFilterMatched = cachedDevice.isConnectedA2dpDevice();
87                     break;
88                 case BluetoothProfile.HEADSET:
89                     isFilterMatched = cachedDevice.isConnectedHfpDevice();
90                     break;
91             }
92             if (DBG) {
93                 Log.d(TAG, "isFilterMatched() device : " +
94                         cachedDevice.getName() + ", isFilterMatched : " + isFilterMatched);
95             }
96         }
97         return isFilterMatched;
98     }
99 
100     @Override
onPreferenceClick(Preference preference)101     public boolean onPreferenceClick(Preference preference) {
102         mMetricsFeatureProvider.logClickedPreference(preference, mFragment.getMetricsCategory());
103         final CachedBluetoothDevice device = ((BluetoothDevicePreference) preference)
104                 .getBluetoothDevice();
105         return device.setActive();
106     }
107 
108     @Override
getPreferenceKey()109     protected String getPreferenceKey() {
110         return PREF_KEY;
111     }
112 }
113