• 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.support.annotation.VisibleForTesting;
23 import android.support.v7.preference.Preference;
24 import android.util.Log;
25 
26 import com.android.settings.connecteddevice.DevicePreferenceCallback;
27 import com.android.settings.dashboard.DashboardFragment;
28 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
29 import com.android.settingslib.bluetooth.LocalBluetoothManager;
30 
31 /**
32  * Controller to maintain connected bluetooth devices
33  */
34 public class ConnectedBluetoothDeviceUpdater extends BluetoothDeviceUpdater {
35 
36     private static final String TAG = "ConnBluetoothDeviceUpdater";
37     private static final boolean DBG = false;
38 
39     private final AudioManager mAudioManager;
40 
ConnectedBluetoothDeviceUpdater(Context context, DashboardFragment fragment, DevicePreferenceCallback devicePreferenceCallback)41     public ConnectedBluetoothDeviceUpdater(Context context, DashboardFragment fragment,
42             DevicePreferenceCallback devicePreferenceCallback) {
43         super(context, fragment, devicePreferenceCallback);
44         mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
45     }
46 
47     @VisibleForTesting
ConnectedBluetoothDeviceUpdater(DashboardFragment fragment, DevicePreferenceCallback devicePreferenceCallback, LocalBluetoothManager localBluetoothManager)48     ConnectedBluetoothDeviceUpdater(DashboardFragment fragment,
49             DevicePreferenceCallback devicePreferenceCallback,
50             LocalBluetoothManager localBluetoothManager) {
51         super(fragment, devicePreferenceCallback, localBluetoothManager);
52         mAudioManager = (AudioManager) fragment.getContext().
53                 getSystemService(Context.AUDIO_SERVICE);
54     }
55 
56     @Override
onAudioModeChanged()57     public void onAudioModeChanged() {
58         forceUpdate();
59     }
60 
61     @Override
onProfileConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state, int bluetoothProfile)62     public void onProfileConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state,
63             int bluetoothProfile) {
64         if (DBG) {
65             Log.d(TAG, "onProfileConnectionStateChanged() device: " +
66                     cachedDevice.getName() + ", state: " + state + ", bluetoothProfile: "
67                     + bluetoothProfile);
68         }
69         if (state == BluetoothProfile.STATE_CONNECTED) {
70             if (isFilterMatched(cachedDevice)) {
71                 addPreference(cachedDevice);
72             } else {
73                 removePreference(cachedDevice);
74             }
75         } else if (state == BluetoothProfile.STATE_DISCONNECTED) {
76             removePreference(cachedDevice);
77         }
78     }
79 
80     @Override
isFilterMatched(CachedBluetoothDevice cachedDevice)81     public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) {
82         final int audioMode = mAudioManager.getMode();
83         final int currentAudioProfile;
84 
85         if (audioMode == AudioManager.MODE_RINGTONE
86                 || audioMode == AudioManager.MODE_IN_CALL
87                 || audioMode == AudioManager.MODE_IN_COMMUNICATION) {
88             // in phone call
89             currentAudioProfile = BluetoothProfile.HEADSET;
90         } else {
91             // without phone call
92             currentAudioProfile = BluetoothProfile.A2DP;
93         }
94 
95         boolean isFilterMatched = false;
96         if (isDeviceConnected(cachedDevice)) {
97             if (DBG) {
98                 Log.d(TAG, "isFilterMatched() current audio profile : " + currentAudioProfile);
99             }
100             // According to the current audio profile type,
101             // this page will show the bluetooth device that doesn't have corresponding profile.
102             // For example:
103             // If current audio profile is a2dp,
104             // show the bluetooth device that doesn't have a2dp profile.
105             // If current audio profile is headset,
106             // show the bluetooth device that doesn't have headset profile.
107             switch (currentAudioProfile) {
108                 case BluetoothProfile.A2DP:
109                     isFilterMatched = !cachedDevice.isA2dpDevice();
110                     break;
111                 case BluetoothProfile.HEADSET:
112                     isFilterMatched = !cachedDevice.isHfpDevice();
113                     break;
114             }
115             if (DBG) {
116                 Log.d(TAG, "isFilterMatched() device : " +
117                         cachedDevice.getName() + ", isFilterMatched : " + isFilterMatched);
118             }
119         }
120         return isFilterMatched;
121     }
122 
123     @Override
addPreference(CachedBluetoothDevice cachedDevice)124     protected void addPreference(CachedBluetoothDevice cachedDevice) {
125         super.addPreference(cachedDevice);
126         final BluetoothDevice device = cachedDevice.getDevice();
127         if (mPreferenceMap.containsKey(device)) {
128             final BluetoothDevicePreference btPreference =
129                     (BluetoothDevicePreference) mPreferenceMap.get(device);
130             btPreference.setOnGearClickListener(null);
131             btPreference.hideSecondTarget(true);
132             btPreference.setOnPreferenceClickListener((Preference p) -> {
133                 launchDeviceDetails(p);
134                 return true;
135             });
136         }
137     }
138 }
139