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