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