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