1 /* 2 * Copyright (C) 2017 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.BluetoothDevice; 19 import android.bluetooth.BluetoothProfile; 20 import android.content.Context; 21 import android.media.AudioManager; 22 import android.util.Log; 23 24 import androidx.preference.Preference; 25 26 import com.android.settings.connecteddevice.DevicePreferenceCallback; 27 import com.android.settingslib.bluetooth.BluetoothUtils; 28 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 29 import com.android.settingslib.flags.Flags; 30 31 /** 32 * Controller to maintain connected bluetooth devices 33 */ 34 public class ConnectedBluetoothDeviceUpdater extends BluetoothDeviceUpdater { 35 36 private static final String TAG = "ConnBluetoothDeviceUpdater"; 37 private static final boolean DBG = Log.isLoggable(BluetoothDeviceUpdater.TAG, Log.DEBUG); 38 39 private static final String PREF_KEY = "connected_bt"; 40 41 private final AudioManager mAudioManager; 42 ConnectedBluetoothDeviceUpdater(Context context, DevicePreferenceCallback devicePreferenceCallback, int metricsCategory)43 public ConnectedBluetoothDeviceUpdater(Context context, 44 DevicePreferenceCallback devicePreferenceCallback, int metricsCategory) { 45 super(context, devicePreferenceCallback, metricsCategory); 46 mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 47 } 48 49 @Override onAudioModeChanged()50 public void onAudioModeChanged() { 51 forceUpdate(); 52 } 53 54 @Override isFilterMatched(CachedBluetoothDevice cachedDevice)55 public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) { 56 final int audioMode = mAudioManager.getMode(); 57 final int currentAudioProfile; 58 59 if (audioMode == AudioManager.MODE_RINGTONE 60 || audioMode == AudioManager.MODE_IN_CALL 61 || audioMode == AudioManager.MODE_IN_COMMUNICATION) { 62 // in phone call 63 currentAudioProfile = BluetoothProfile.HEADSET; 64 } else { 65 // without phone call 66 currentAudioProfile = BluetoothProfile.A2DP; 67 } 68 69 boolean isFilterMatched = false; 70 if (isDeviceConnected(cachedDevice) && isDeviceInCachedDevicesList(cachedDevice)) { 71 if (DBG) { 72 Log.d(TAG, "isFilterMatched() current audio profile : " + currentAudioProfile); 73 } 74 // If device is Hearing Aid or LE Audio, it is compatible with HFP and A2DP. 75 // It would not show in Connected Devices group. 76 if (cachedDevice.isConnectedAshaHearingAidDevice() 77 || cachedDevice.isConnectedLeAudioDevice()) { 78 return false; 79 } 80 // According to the current audio profile type, 81 // this page will show the bluetooth device that doesn't have corresponding profile. 82 // For example: 83 // If current audio profile is a2dp, 84 // show the bluetooth device that doesn't have a2dp profile. 85 // If current audio profile is headset, 86 // show the bluetooth device that doesn't have headset profile. 87 switch (currentAudioProfile) { 88 case BluetoothProfile.A2DP: 89 isFilterMatched = !cachedDevice.isConnectedA2dpDevice(); 90 break; 91 case BluetoothProfile.HEADSET: 92 isFilterMatched = !cachedDevice.isConnectedHfpDevice(); 93 break; 94 } 95 if (DBG) { 96 Log.d(TAG, "isFilterMatched() device : " + 97 cachedDevice.getName() + ", isFilterMatched : " + isFilterMatched); 98 } 99 } 100 if (Flags.enableHideExclusivelyManagedBluetoothDevice()) { 101 if (BluetoothUtils.isExclusivelyManagedBluetoothDevice(mContext, 102 cachedDevice.getDevice())) { 103 if (DBG) { 104 Log.d(TAG, "isFilterMatched() hide BluetoothDevice with exclusive manager"); 105 } 106 return false; 107 } 108 } 109 return isFilterMatched; 110 } 111 112 @Override addPreference(CachedBluetoothDevice cachedDevice)113 protected void addPreference(CachedBluetoothDevice cachedDevice) { 114 super.addPreference(cachedDevice); 115 final BluetoothDevice device = cachedDevice.getDevice(); 116 if (mPreferenceMap.containsKey(device)) { 117 final BluetoothDevicePreference btPreference = 118 (BluetoothDevicePreference) mPreferenceMap.get(device); 119 btPreference.setOnGearClickListener(null); 120 btPreference.hideSecondTarget(true); 121 btPreference.setOnPreferenceClickListener((Preference p) -> { 122 launchDeviceDetails(p); 123 return true; 124 }); 125 } 126 } 127 128 @Override getPreferenceKey()129 protected String getPreferenceKey() { 130 return PREF_KEY; 131 } 132 133 @Override getLogTag()134 protected String getLogTag() { 135 return TAG; 136 } 137 138 @Override update(CachedBluetoothDevice cachedBluetoothDevice)139 protected void update(CachedBluetoothDevice cachedBluetoothDevice) { 140 super.update(cachedBluetoothDevice); 141 Log.d(TAG, "Map : " + mPreferenceMap); 142 } 143 } 144