• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = false;
37 
38     private final AudioManager mAudioManager;
39 
ConnectedBluetoothDeviceUpdater(Context context, DashboardFragment fragment, DevicePreferenceCallback devicePreferenceCallback)40     public ConnectedBluetoothDeviceUpdater(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 not show in Connected Devices group.
73             if (cachedDevice.isConnectedHearingAidDevice()) {
74                 return false;
75             }
76             // According to the current audio profile type,
77             // this page will show the bluetooth device that doesn't have corresponding profile.
78             // For example:
79             // If current audio profile is a2dp,
80             // show the bluetooth device that doesn't have a2dp profile.
81             // If current audio profile is headset,
82             // show the bluetooth device that doesn't have headset profile.
83             switch (currentAudioProfile) {
84                 case BluetoothProfile.A2DP:
85                     isFilterMatched = !cachedDevice.isConnectedA2dpDevice();
86                     break;
87                 case BluetoothProfile.HEADSET:
88                     isFilterMatched = !cachedDevice.isConnectedHfpDevice();
89                     break;
90             }
91             if (DBG) {
92                 Log.d(TAG, "isFilterMatched() device : " +
93                         cachedDevice.getName() + ", isFilterMatched : " + isFilterMatched);
94             }
95         }
96         return isFilterMatched;
97     }
98 
99     @Override
addPreference(CachedBluetoothDevice cachedDevice)100     protected void addPreference(CachedBluetoothDevice cachedDevice) {
101         super.addPreference(cachedDevice);
102         final BluetoothDevice device = cachedDevice.getDevice();
103         if (mPreferenceMap.containsKey(device)) {
104             final BluetoothDevicePreference btPreference =
105                     (BluetoothDevicePreference) mPreferenceMap.get(device);
106             btPreference.setOnGearClickListener(null);
107             btPreference.hideSecondTarget(true);
108             btPreference.setOnPreferenceClickListener((Preference p) -> {
109                 launchDeviceDetails(p);
110                 return true;
111             });
112         }
113     }
114 }
115