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)) { 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 not show in Connected Devices group. 75 if (cachedDevice.isConnectedHearingAidDevice()) { 76 return false; 77 } 78 // According to the current audio profile type, 79 // this page will show the bluetooth device that doesn't have corresponding profile. 80 // For example: 81 // If current audio profile is a2dp, 82 // show the bluetooth device that doesn't have a2dp profile. 83 // If current audio profile is headset, 84 // show the bluetooth device that doesn't have headset profile. 85 switch (currentAudioProfile) { 86 case BluetoothProfile.A2DP: 87 isFilterMatched = !cachedDevice.isConnectedA2dpDevice(); 88 break; 89 case BluetoothProfile.HEADSET: 90 isFilterMatched = !cachedDevice.isConnectedHfpDevice(); 91 break; 92 } 93 if (DBG) { 94 Log.d(TAG, "isFilterMatched() device : " + 95 cachedDevice.getName() + ", isFilterMatched : " + isFilterMatched); 96 } 97 } 98 return isFilterMatched; 99 } 100 101 @Override addPreference(CachedBluetoothDevice cachedDevice)102 protected void addPreference(CachedBluetoothDevice cachedDevice) { 103 super.addPreference(cachedDevice); 104 final BluetoothDevice device = cachedDevice.getDevice(); 105 if (mPreferenceMap.containsKey(device)) { 106 final BluetoothDevicePreference btPreference = 107 (BluetoothDevicePreference) mPreferenceMap.get(device); 108 btPreference.setOnGearClickListener(null); 109 btPreference.hideSecondTarget(true); 110 btPreference.setOnPreferenceClickListener((Preference p) -> { 111 launchDeviceDetails(p); 112 return true; 113 }); 114 } 115 } 116 117 @Override getPreferenceKey()118 protected String getPreferenceKey() { 119 return PREF_KEY; 120 } 121 } 122