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.settings.dashboard.DashboardFragment; 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(TAG, Log.DEBUG); 37 38 private static final String PREF_KEY = "connected_bt"; 39 40 private final AudioManager mAudioManager; 41 ConnectedBluetoothDeviceUpdater(Context context, DashboardFragment fragment, DevicePreferenceCallback devicePreferenceCallback)42 public ConnectedBluetoothDeviceUpdater(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) && isDeviceInCachedDevicesList(cachedDevice)) { 70 if (DBG) { 71 Log.d(TAG, "isFilterMatched() current audio profile : " + currentAudioProfile); 72 } 73 // If device is Hearing Aid or LE Audio, it is compatible with HFP and A2DP. 74 // It would not show in Connected Devices group. 75 if (cachedDevice.isConnectedHearingAidDevice() 76 || cachedDevice.isConnectedLeAudioDevice()) { 77 return false; 78 } 79 // According to the current audio profile type, 80 // this page will show the bluetooth device that doesn't have corresponding profile. 81 // For example: 82 // If current audio profile is a2dp, 83 // show the bluetooth device that doesn't have a2dp profile. 84 // If current audio profile is headset, 85 // show the bluetooth device that doesn't 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 addPreference(CachedBluetoothDevice cachedDevice)103 protected void addPreference(CachedBluetoothDevice cachedDevice) { 104 super.addPreference(cachedDevice); 105 final BluetoothDevice device = cachedDevice.getDevice(); 106 if (mPreferenceMap.containsKey(device)) { 107 final BluetoothDevicePreference btPreference = 108 (BluetoothDevicePreference) mPreferenceMap.get(device); 109 btPreference.setOnGearClickListener(null); 110 btPreference.hideSecondTarget(true); 111 btPreference.setOnPreferenceClickListener((Preference p) -> { 112 launchDeviceDetails(p); 113 return true; 114 }); 115 } 116 } 117 118 @Override getPreferenceKey()119 protected String getPreferenceKey() { 120 return PREF_KEY; 121 } 122 } 123