Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
| 17 | package com.android.settingslib.bluetooth; |
| 18 | |
Fan Zhang | f7802ea | 2018-08-28 15:15:19 -0700 | [diff] [blame] | 19 | import android.bluetooth.BluetoothAdapter; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 20 | import android.bluetooth.BluetoothClass; |
| 21 | import android.bluetooth.BluetoothDevice; |
Isha Bobra | c3d9413 | 2018-02-08 16:04:36 -0800 | [diff] [blame] | 22 | import android.bluetooth.BluetoothHearingAid; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 23 | import android.bluetooth.BluetoothProfile; |
| 24 | import android.bluetooth.BluetoothUuid; |
| 25 | import android.content.Context; |
| 26 | import android.content.SharedPreferences; |
hughchen | 00ce025 | 2019-10-28 17:03:04 +0800 | [diff] [blame] | 27 | import android.os.Handler; |
| 28 | import android.os.Looper; |
| 29 | import android.os.Message; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 30 | import android.os.ParcelUuid; |
| 31 | import android.os.SystemClock; |
| 32 | import android.text.TextUtils; |
Zongheng Wang | 810c6d2 | 2019-09-05 13:44:28 -0700 | [diff] [blame] | 33 | import android.util.EventLog; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 34 | import android.util.Log; |
Fan Zhang | f7802ea | 2018-08-28 15:15:19 -0700 | [diff] [blame] | 35 | |
timhypeng | 5c62ebb | 2018-08-28 09:59:03 +0800 | [diff] [blame] | 36 | import androidx.annotation.VisibleForTesting; |
| 37 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 38 | import com.android.settingslib.R; |
jackqdyulei | 1f9c171 | 2019-03-05 16:42:57 -0800 | [diff] [blame] | 39 | import com.android.settingslib.Utils; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 40 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 41 | import java.util.ArrayList; |
| 42 | import java.util.Collection; |
| 43 | import java.util.Collections; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 44 | import java.util.List; |
davidln | 0bc952a | 2019-06-17 14:48:11 -0700 | [diff] [blame] | 45 | import java.util.concurrent.CopyOnWriteArrayList; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * CachedBluetoothDevice represents a remote Bluetooth device. It contains |
| 49 | * attributes of the device (such as the address, name, RSSI, etc.) and |
| 50 | * functionality that can be performed on the device (connect, pair, disconnect, |
| 51 | * etc.). |
| 52 | */ |
Fan Zhang | 82dd3b0 | 2016-12-27 13:13:00 -0800 | [diff] [blame] | 53 | public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 54 | private static final String TAG = "CachedBluetoothDevice"; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 55 | |
jackqdyulei | 1751eaa | 2018-09-14 11:22:32 -0700 | [diff] [blame] | 56 | // See mConnectAttempted |
| 57 | private static final long MAX_UUID_DELAY_FOR_AUTO_CONNECT = 5000; |
Stanley Tng | ca33f5b | 2019-05-02 10:36:40 -0700 | [diff] [blame] | 58 | // Some Hearing Aids (especially the 2nd device) needs more time to do service discovery |
| 59 | private static final long MAX_HEARING_AIDS_DELAY_FOR_AUTO_CONNECT = 15000; |
jackqdyulei | 1751eaa | 2018-09-14 11:22:32 -0700 | [diff] [blame] | 60 | private static final long MAX_HOGP_DELAY_FOR_AUTO_CONNECT = 30000; |
hughchen | 00ce025 | 2019-10-28 17:03:04 +0800 | [diff] [blame] | 61 | private static final long MAX_MEDIA_PROFILE_CONNECT_DELAY = 60000; |
jackqdyulei | 1751eaa | 2018-09-14 11:22:32 -0700 | [diff] [blame] | 62 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 63 | private final Context mContext; |
timhypeng | f64f690 | 2018-07-31 15:40:15 +0800 | [diff] [blame] | 64 | private final BluetoothAdapter mLocalAdapter; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 65 | private final LocalBluetoothProfileManager mProfileManager; |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 66 | private final Object mProfileLock = new Object(); |
timhypeng | 5c62ebb | 2018-08-28 09:59:03 +0800 | [diff] [blame] | 67 | BluetoothDevice mDevice; |
Isha Bobra | c3d9413 | 2018-02-08 16:04:36 -0800 | [diff] [blame] | 68 | private long mHiSyncId; |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 69 | // Need this since there is no method for getting RSSI |
timhypeng | 5c62ebb | 2018-08-28 09:59:03 +0800 | [diff] [blame] | 70 | short mRssi; |
| 71 | // mProfiles and mRemovedProfiles does not do swap() between main and sub device. It is |
| 72 | // because current sub device is only for HearingAid and its profile is the same. |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 73 | private final List<LocalBluetoothProfile> mProfiles = new ArrayList<>(); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 74 | |
| 75 | // List of profiles that were previously in mProfiles, but have been removed |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 76 | private final List<LocalBluetoothProfile> mRemovedProfiles = new ArrayList<>(); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 77 | |
| 78 | // Device supports PANU but not NAP: remove PanProfile after device disconnects from NAP |
| 79 | private boolean mLocalNapRoleConnected; |
| 80 | |
timhypeng | 5c62ebb | 2018-08-28 09:59:03 +0800 | [diff] [blame] | 81 | boolean mJustDiscovered; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 82 | |
davidln | 0bc952a | 2019-06-17 14:48:11 -0700 | [diff] [blame] | 83 | private final Collection<Callback> mCallbacks = new CopyOnWriteArrayList<>(); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 84 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 85 | /** |
| 86 | * Last time a bt profile auto-connect was attempted. |
| 87 | * If an ACTION_UUID intent comes in within |
| 88 | * MAX_UUID_DELAY_FOR_AUTO_CONNECT milliseconds, we will try auto-connect |
| 89 | * again with the new UUIDs |
| 90 | */ |
| 91 | private long mConnectAttempted; |
| 92 | |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 93 | // Active device state |
| 94 | private boolean mIsActiveDeviceA2dp = false; |
| 95 | private boolean mIsActiveDeviceHeadset = false; |
Hansong Zhang | d7b3591 | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 96 | private boolean mIsActiveDeviceHearingAid = false; |
hughchen | 00ce025 | 2019-10-28 17:03:04 +0800 | [diff] [blame] | 97 | // Media profile connect state |
| 98 | private boolean mIsA2dpProfileConnectedFail = false; |
| 99 | private boolean mIsHeadsetProfileConnectedFail = false; |
| 100 | private boolean mIsHearingAidProfileConnectedFail = false; |
timhypeng | 5c62ebb | 2018-08-28 09:59:03 +0800 | [diff] [blame] | 101 | // Group second device for Hearing Aid |
| 102 | private CachedBluetoothDevice mSubDevice; |
jackqdyulei | 1751eaa | 2018-09-14 11:22:32 -0700 | [diff] [blame] | 103 | |
hughchen | 00ce025 | 2019-10-28 17:03:04 +0800 | [diff] [blame] | 104 | private final Handler mHandler = new Handler(Looper.getMainLooper()) { |
| 105 | @Override |
| 106 | public void handleMessage(Message msg) { |
| 107 | switch (msg.what) { |
| 108 | case BluetoothProfile.A2DP: |
| 109 | mIsA2dpProfileConnectedFail = true; |
| 110 | break; |
| 111 | case BluetoothProfile.HEADSET: |
| 112 | mIsHeadsetProfileConnectedFail = true; |
| 113 | break; |
| 114 | case BluetoothProfile.HEARING_AID: |
| 115 | mIsHearingAidProfileConnectedFail = true; |
| 116 | break; |
| 117 | default: |
| 118 | Log.w(TAG, "handleMessage(): unknown message : " + msg.what); |
| 119 | break; |
| 120 | } |
| 121 | Log.w(TAG, "Connect to profile : " + msg.what + " timeout, show error message !"); |
| 122 | refresh(); |
| 123 | } |
| 124 | }; |
| 125 | |
jackqdyulei | 1751eaa | 2018-09-14 11:22:32 -0700 | [diff] [blame] | 126 | CachedBluetoothDevice(Context context, LocalBluetoothProfileManager profileManager, |
| 127 | BluetoothDevice device) { |
| 128 | mContext = context; |
| 129 | mLocalAdapter = BluetoothAdapter.getDefaultAdapter(); |
| 130 | mProfileManager = profileManager; |
| 131 | mDevice = device; |
jackqdyulei | 1751eaa | 2018-09-14 11:22:32 -0700 | [diff] [blame] | 132 | fillData(); |
| 133 | mHiSyncId = BluetoothHearingAid.HI_SYNC_ID_INVALID; |
| 134 | } |
| 135 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 136 | /** |
| 137 | * Describes the current device and profile for logging. |
| 138 | * |
| 139 | * @param profile Profile to describe |
| 140 | * @return Description of the device and profile |
| 141 | */ |
| 142 | private String describe(LocalBluetoothProfile profile) { |
| 143 | StringBuilder sb = new StringBuilder(); |
| 144 | sb.append("Address:").append(mDevice); |
| 145 | if (profile != null) { |
| 146 | sb.append(" Profile:").append(profile); |
| 147 | } |
| 148 | |
| 149 | return sb.toString(); |
| 150 | } |
| 151 | |
| 152 | void onProfileStateChanged(LocalBluetoothProfile profile, int newProfileState) { |
Kunhung Li | e7b7cb8 | 2018-05-17 11:04:54 +0800 | [diff] [blame] | 153 | if (BluetoothUtils.D) { |
Chienyuan | 559b7f1 | 2018-12-17 15:30:20 +0800 | [diff] [blame] | 154 | Log.d(TAG, "onProfileStateChanged: profile " + profile + ", device=" + mDevice |
| 155 | + ", newProfileState " + newProfileState); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 156 | } |
timhypeng | f64f690 | 2018-07-31 15:40:15 +0800 | [diff] [blame] | 157 | if (mLocalAdapter.getState() == BluetoothAdapter.STATE_TURNING_OFF) |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 158 | { |
Kunhung Li | e7b7cb8 | 2018-05-17 11:04:54 +0800 | [diff] [blame] | 159 | if (BluetoothUtils.D) { |
| 160 | Log.d(TAG, " BT Turninig Off...Profile conn state change ignored..."); |
| 161 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 162 | return; |
| 163 | } |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 164 | |
| 165 | synchronized (mProfileLock) { |
hughchen | 00ce025 | 2019-10-28 17:03:04 +0800 | [diff] [blame] | 166 | if (profile instanceof A2dpProfile || profile instanceof HeadsetProfile |
| 167 | || profile instanceof HearingAidProfile) { |
| 168 | setProfileConnectedStatus(profile.getProfileId(), false); |
| 169 | switch (newProfileState) { |
| 170 | case BluetoothProfile.STATE_CONNECTED: |
| 171 | mHandler.removeMessages(profile.getProfileId()); |
| 172 | break; |
| 173 | case BluetoothProfile.STATE_CONNECTING: |
| 174 | mHandler.sendEmptyMessageDelayed(profile.getProfileId(), |
| 175 | MAX_MEDIA_PROFILE_CONNECT_DELAY); |
| 176 | break; |
| 177 | case BluetoothProfile.STATE_DISCONNECTING: |
| 178 | if (mHandler.hasMessages(profile.getProfileId())) { |
| 179 | mHandler.removeMessages(profile.getProfileId()); |
| 180 | } |
| 181 | break; |
| 182 | case BluetoothProfile.STATE_DISCONNECTED: |
| 183 | if (mHandler.hasMessages(profile.getProfileId())) { |
| 184 | mHandler.removeMessages(profile.getProfileId()); |
| 185 | setProfileConnectedStatus(profile.getProfileId(), true); |
| 186 | } |
| 187 | break; |
| 188 | default: |
| 189 | Log.w(TAG, "onProfileStateChanged(): unknown profile state : " |
| 190 | + newProfileState); |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 195 | if (newProfileState == BluetoothProfile.STATE_CONNECTED) { |
| 196 | if (profile instanceof MapProfile) { |
| 197 | profile.setPreferred(mDevice, true); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 198 | } |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 199 | if (!mProfiles.contains(profile)) { |
| 200 | mRemovedProfiles.remove(profile); |
| 201 | mProfiles.add(profile); |
| 202 | if (profile instanceof PanProfile |
| 203 | && ((PanProfile) profile).isLocalRoleNap(mDevice)) { |
| 204 | // Device doesn't support NAP, so remove PanProfile on disconnect |
| 205 | mLocalNapRoleConnected = true; |
| 206 | } |
| 207 | } |
| 208 | } else if (profile instanceof MapProfile |
| 209 | && newProfileState == BluetoothProfile.STATE_DISCONNECTED) { |
| 210 | profile.setPreferred(mDevice, false); |
| 211 | } else if (mLocalNapRoleConnected && profile instanceof PanProfile |
| 212 | && ((PanProfile) profile).isLocalRoleNap(mDevice) |
| 213 | && newProfileState == BluetoothProfile.STATE_DISCONNECTED) { |
| 214 | Log.d(TAG, "Removing PanProfile from device after NAP disconnect"); |
| 215 | mProfiles.remove(profile); |
| 216 | mRemovedProfiles.add(profile); |
| 217 | mLocalNapRoleConnected = false; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 218 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 219 | } |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 220 | |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 221 | fetchActiveDevices(); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 222 | } |
| 223 | |
hughchen | 00ce025 | 2019-10-28 17:03:04 +0800 | [diff] [blame] | 224 | @VisibleForTesting |
| 225 | void setProfileConnectedStatus(int profileId, boolean isFailed) { |
| 226 | switch (profileId) { |
| 227 | case BluetoothProfile.A2DP: |
| 228 | mIsA2dpProfileConnectedFail = isFailed; |
| 229 | break; |
| 230 | case BluetoothProfile.HEADSET: |
| 231 | mIsHeadsetProfileConnectedFail = isFailed; |
| 232 | break; |
| 233 | case BluetoothProfile.HEARING_AID: |
| 234 | mIsHearingAidProfileConnectedFail = isFailed; |
| 235 | break; |
| 236 | default: |
| 237 | Log.w(TAG, "setProfileConnectedStatus(): unknown profile id : " + profileId); |
| 238 | break; |
| 239 | } |
| 240 | } |
| 241 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 242 | public void disconnect() { |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 243 | synchronized (mProfileLock) { |
hughchen | fb0c7ad | 2019-11-25 14:48:46 +0800 | [diff] [blame] | 244 | mLocalAdapter.disconnectAllEnabledProfiles(mDevice); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 245 | } |
| 246 | // Disconnect PBAP server in case its connected |
| 247 | // This is to ensure all the profiles are disconnected as some CK/Hs do not |
| 248 | // disconnect PBAP connection when HF connection is brought down |
| 249 | PbapServerProfile PbapProfile = mProfileManager.getPbapProfile(); |
Joseph Pirozzo | cdbab12 | 2019-01-31 10:58:22 -0800 | [diff] [blame] | 250 | if (PbapProfile != null && isConnectedProfile(PbapProfile)) |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 251 | { |
| 252 | PbapProfile.disconnect(mDevice); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | public void disconnect(LocalBluetoothProfile profile) { |
| 257 | if (profile.disconnect(mDevice)) { |
Kunhung Li | e7b7cb8 | 2018-05-17 11:04:54 +0800 | [diff] [blame] | 258 | if (BluetoothUtils.D) { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 259 | Log.d(TAG, "Command sent successfully:DISCONNECT " + describe(profile)); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | public void connect(boolean connectAllProfiles) { |
| 265 | if (!ensurePaired()) { |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | mConnectAttempted = SystemClock.elapsedRealtime(); |
| 270 | connectWithoutResettingTimer(connectAllProfiles); |
| 271 | } |
| 272 | |
jackqdyulei | 1751eaa | 2018-09-14 11:22:32 -0700 | [diff] [blame] | 273 | public long getHiSyncId() { |
| 274 | return mHiSyncId; |
| 275 | } |
| 276 | |
| 277 | public void setHiSyncId(long id) { |
| 278 | if (BluetoothUtils.D) { |
| 279 | Log.d(TAG, "setHiSyncId: mDevice " + mDevice + ", id " + id); |
| 280 | } |
| 281 | mHiSyncId = id; |
| 282 | } |
| 283 | |
Stanley Tng | c118294 | 2018-11-29 13:47:45 -0800 | [diff] [blame] | 284 | public boolean isHearingAidDevice() { |
| 285 | return mHiSyncId != BluetoothHearingAid.HI_SYNC_ID_INVALID; |
| 286 | } |
| 287 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 288 | void onBondingDockConnect() { |
| 289 | // Attempt to connect if UUIDs are available. Otherwise, |
| 290 | // we will connect when the ACTION_UUID intent arrives. |
| 291 | connect(false); |
| 292 | } |
| 293 | |
| 294 | private void connectWithoutResettingTimer(boolean connectAllProfiles) { |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 295 | synchronized (mProfileLock) { |
| 296 | // Try to initialize the profiles if they were not. |
| 297 | if (mProfiles.isEmpty()) { |
| 298 | // if mProfiles is empty, then do not invoke updateProfiles. This causes a race |
| 299 | // condition with carkits during pairing, wherein RemoteDevice.UUIDs have been |
| 300 | // updated from bluetooth stack but ACTION.uuid is not sent yet. |
| 301 | // Eventually ACTION.uuid will be received which shall trigger the connection of the |
| 302 | // various profiles |
| 303 | // If UUIDs are not available yet, connect will be happen |
| 304 | // upon arrival of the ACTION_UUID intent. |
Stanley Tng | ca33f5b | 2019-05-02 10:36:40 -0700 | [diff] [blame] | 305 | Log.d(TAG, "No profiles. Maybe we will connect later for device " + mDevice); |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 306 | return; |
| 307 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 308 | |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 309 | int preferredProfiles = 0; |
| 310 | for (LocalBluetoothProfile profile : mProfiles) { |
| 311 | if (connectAllProfiles ? profile.accessProfileEnabled() |
| 312 | : profile.isAutoConnectable()) { |
| 313 | if (profile.isPreferred(mDevice)) { |
| 314 | ++preferredProfiles; |
| 315 | connectInt(profile); |
| 316 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 317 | } |
| 318 | } |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 319 | if (BluetoothUtils.D) Log.d(TAG, "Preferred profiles = " + preferredProfiles); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 320 | |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 321 | if (preferredProfiles == 0) { |
| 322 | connectAutoConnectableProfiles(); |
| 323 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
| 327 | private void connectAutoConnectableProfiles() { |
| 328 | if (!ensurePaired()) { |
| 329 | return; |
| 330 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 331 | |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 332 | synchronized (mProfileLock) { |
| 333 | for (LocalBluetoothProfile profile : mProfiles) { |
| 334 | if (profile.isAutoConnectable()) { |
| 335 | profile.setPreferred(mDevice, true); |
| 336 | connectInt(profile); |
| 337 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Connect this device to the specified profile. |
| 344 | * |
| 345 | * @param profile the profile to use with the remote device |
| 346 | */ |
| 347 | public void connectProfile(LocalBluetoothProfile profile) { |
| 348 | mConnectAttempted = SystemClock.elapsedRealtime(); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 349 | connectInt(profile); |
| 350 | // Refresh the UI based on profile.connect() call |
| 351 | refresh(); |
| 352 | } |
| 353 | |
| 354 | synchronized void connectInt(LocalBluetoothProfile profile) { |
| 355 | if (!ensurePaired()) { |
| 356 | return; |
| 357 | } |
| 358 | if (profile.connect(mDevice)) { |
Kunhung Li | e7b7cb8 | 2018-05-17 11:04:54 +0800 | [diff] [blame] | 359 | if (BluetoothUtils.D) { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 360 | Log.d(TAG, "Command sent successfully:CONNECT " + describe(profile)); |
| 361 | } |
| 362 | return; |
| 363 | } |
hughchen | cb6e197 | 2018-08-15 16:13:21 +0800 | [diff] [blame] | 364 | Log.i(TAG, "Failed to connect " + profile.toString() + " to " + getName()); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | private boolean ensurePaired() { |
| 368 | if (getBondState() == BluetoothDevice.BOND_NONE) { |
| 369 | startPairing(); |
| 370 | return false; |
| 371 | } else { |
| 372 | return true; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | public boolean startPairing() { |
| 377 | // Pairing is unreliable while scanning, so cancel discovery |
| 378 | if (mLocalAdapter.isDiscovering()) { |
| 379 | mLocalAdapter.cancelDiscovery(); |
| 380 | } |
| 381 | |
| 382 | if (!mDevice.createBond()) { |
| 383 | return false; |
| 384 | } |
| 385 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 386 | return true; |
| 387 | } |
| 388 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 389 | public void unpair() { |
| 390 | int state = getBondState(); |
| 391 | |
| 392 | if (state == BluetoothDevice.BOND_BONDING) { |
| 393 | mDevice.cancelBondProcess(); |
| 394 | } |
| 395 | |
| 396 | if (state != BluetoothDevice.BOND_NONE) { |
| 397 | final BluetoothDevice dev = mDevice; |
| 398 | if (dev != null) { |
| 399 | final boolean successful = dev.removeBond(); |
| 400 | if (successful) { |
Kunhung Li | e7b7cb8 | 2018-05-17 11:04:54 +0800 | [diff] [blame] | 401 | if (BluetoothUtils.D) { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 402 | Log.d(TAG, "Command sent successfully:REMOVE_BOND " + describe(null)); |
| 403 | } |
Kunhung Li | e7b7cb8 | 2018-05-17 11:04:54 +0800 | [diff] [blame] | 404 | } else if (BluetoothUtils.V) { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 405 | Log.v(TAG, "Framework rejected command immediately:REMOVE_BOND " + |
Isha Bobra | c3d9413 | 2018-02-08 16:04:36 -0800 | [diff] [blame] | 406 | describe(null)); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | public int getProfileConnectionState(LocalBluetoothProfile profile) { |
jackqdyulei | bf50905 | 2018-09-24 14:00:26 -0700 | [diff] [blame] | 413 | return profile != null |
| 414 | ? profile.getConnectionStatus(mDevice) |
| 415 | : BluetoothProfile.STATE_DISCONNECTED; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | // TODO: do any of these need to run async on a background thread? |
| 419 | private void fillData() { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 420 | updateProfiles(); |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 421 | fetchActiveDevices(); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 422 | migratePhonebookPermissionChoice(); |
| 423 | migrateMessagePermissionChoice(); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 424 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 425 | dispatchAttributesChanged(); |
| 426 | } |
| 427 | |
| 428 | public BluetoothDevice getDevice() { |
| 429 | return mDevice; |
| 430 | } |
| 431 | |
Antony Sargent | 7ad051e | 2017-06-29 15:23:13 -0700 | [diff] [blame] | 432 | /** |
| 433 | * Convenience method that can be mocked - it lets tests avoid having to call getDevice() which |
| 434 | * causes problems in tests since BluetoothDevice is final and cannot be mocked. |
| 435 | * @return the address of this device |
| 436 | */ |
| 437 | public String getAddress() { |
| 438 | return mDevice.getAddress(); |
| 439 | } |
| 440 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 441 | /** |
hughchen | cb6e197 | 2018-08-15 16:13:21 +0800 | [diff] [blame] | 442 | * Get name from remote device |
Rahul Sabnis | 24ad4b5 | 2019-10-22 13:37:29 -0700 | [diff] [blame] | 443 | * @return {@link BluetoothDevice#getAlias()} if |
| 444 | * {@link BluetoothDevice#getAlias()} is not null otherwise return |
hughchen | cb6e197 | 2018-08-15 16:13:21 +0800 | [diff] [blame] | 445 | * {@link BluetoothDevice#getAddress()} |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 446 | */ |
hughchen | cb6e197 | 2018-08-15 16:13:21 +0800 | [diff] [blame] | 447 | public String getName() { |
Rahul Sabnis | 24ad4b5 | 2019-10-22 13:37:29 -0700 | [diff] [blame] | 448 | final String aliasName = mDevice.getAlias(); |
hughchen | cb6e197 | 2018-08-15 16:13:21 +0800 | [diff] [blame] | 449 | return TextUtils.isEmpty(aliasName) ? getAddress() : aliasName; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /** |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 453 | * User changes the device name |
| 454 | * @param name new alias name to be set, should never be null |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 455 | */ |
| 456 | public void setName(String name) { |
hughchen | cb6e197 | 2018-08-15 16:13:21 +0800 | [diff] [blame] | 457 | // Prevent getName() to be set to null if setName(null) is called |
| 458 | if (name != null && !TextUtils.equals(name, getName())) { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 459 | mDevice.setAlias(name); |
| 460 | dispatchAttributesChanged(); |
| 461 | } |
| 462 | } |
| 463 | |
Hansong Zhang | 6a41632 | 2018-03-19 18:20:38 -0700 | [diff] [blame] | 464 | /** |
| 465 | * Set this device as active device |
| 466 | * @return true if at least one profile on this device is set to active, false otherwise |
| 467 | */ |
| 468 | public boolean setActive() { |
| 469 | boolean result = false; |
| 470 | A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile(); |
| 471 | if (a2dpProfile != null && isConnectedProfile(a2dpProfile)) { |
| 472 | if (a2dpProfile.setActiveDevice(getDevice())) { |
| 473 | Log.i(TAG, "OnPreferenceClickListener: A2DP active device=" + this); |
| 474 | result = true; |
| 475 | } |
| 476 | } |
| 477 | HeadsetProfile headsetProfile = mProfileManager.getHeadsetProfile(); |
| 478 | if ((headsetProfile != null) && isConnectedProfile(headsetProfile)) { |
| 479 | if (headsetProfile.setActiveDevice(getDevice())) { |
| 480 | Log.i(TAG, "OnPreferenceClickListener: Headset active device=" + this); |
| 481 | result = true; |
| 482 | } |
| 483 | } |
Hansong Zhang | d7b3591 | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 484 | HearingAidProfile hearingAidProfile = mProfileManager.getHearingAidProfile(); |
| 485 | if ((hearingAidProfile != null) && isConnectedProfile(hearingAidProfile)) { |
| 486 | if (hearingAidProfile.setActiveDevice(getDevice())) { |
| 487 | Log.i(TAG, "OnPreferenceClickListener: Hearing Aid active device=" + this); |
| 488 | result = true; |
| 489 | } |
| 490 | } |
Hansong Zhang | 6a41632 | 2018-03-19 18:20:38 -0700 | [diff] [blame] | 491 | return result; |
| 492 | } |
| 493 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 494 | void refreshName() { |
hughchen | cb6e197 | 2018-08-15 16:13:21 +0800 | [diff] [blame] | 495 | if (BluetoothUtils.D) { |
| 496 | Log.d(TAG, "Device name: " + getName()); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 497 | } |
hughchen | cb6e197 | 2018-08-15 16:13:21 +0800 | [diff] [blame] | 498 | dispatchAttributesChanged(); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 499 | } |
| 500 | |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 501 | /** |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 502 | * Checks if device has a human readable name besides MAC address |
| 503 | * @return true if device's alias name is not null nor empty, false otherwise |
| 504 | */ |
| 505 | public boolean hasHumanReadableName() { |
Rahul Sabnis | 24ad4b5 | 2019-10-22 13:37:29 -0700 | [diff] [blame] | 506 | return !TextUtils.isEmpty(mDevice.getAlias()); |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | /** |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 510 | * Get battery level from remote device |
| 511 | * @return battery level in percentage [0-100], or {@link BluetoothDevice#BATTERY_LEVEL_UNKNOWN} |
| 512 | */ |
| 513 | public int getBatteryLevel() { |
| 514 | return mDevice.getBatteryLevel(); |
| 515 | } |
| 516 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 517 | void refresh() { |
| 518 | dispatchAttributesChanged(); |
| 519 | } |
| 520 | |
Jack He | 5152047 | 2017-07-24 12:30:08 -0700 | [diff] [blame] | 521 | public void setJustDiscovered(boolean justDiscovered) { |
| 522 | if (mJustDiscovered != justDiscovered) { |
| 523 | mJustDiscovered = justDiscovered; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 524 | dispatchAttributesChanged(); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | public int getBondState() { |
| 529 | return mDevice.getBondState(); |
| 530 | } |
| 531 | |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 532 | /** |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 533 | * Update the device status as active or non-active per Bluetooth profile. |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 534 | * |
| 535 | * @param isActive true if the device is active |
| 536 | * @param bluetoothProfile the Bluetooth profile |
| 537 | */ |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 538 | public void onActiveDeviceChanged(boolean isActive, int bluetoothProfile) { |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 539 | boolean changed = false; |
| 540 | switch (bluetoothProfile) { |
| 541 | case BluetoothProfile.A2DP: |
| 542 | changed = (mIsActiveDeviceA2dp != isActive); |
| 543 | mIsActiveDeviceA2dp = isActive; |
| 544 | break; |
| 545 | case BluetoothProfile.HEADSET: |
| 546 | changed = (mIsActiveDeviceHeadset != isActive); |
| 547 | mIsActiveDeviceHeadset = isActive; |
| 548 | break; |
Hansong Zhang | d7b3591 | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 549 | case BluetoothProfile.HEARING_AID: |
| 550 | changed = (mIsActiveDeviceHearingAid != isActive); |
| 551 | mIsActiveDeviceHearingAid = isActive; |
| 552 | break; |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 553 | default: |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 554 | Log.w(TAG, "onActiveDeviceChanged: unknown profile " + bluetoothProfile + |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 555 | " isActive " + isActive); |
| 556 | break; |
| 557 | } |
| 558 | if (changed) { |
| 559 | dispatchAttributesChanged(); |
| 560 | } |
| 561 | } |
| 562 | |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 563 | /** |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 564 | * Update the profile audio state. |
| 565 | */ |
| 566 | void onAudioModeChanged() { |
| 567 | dispatchAttributesChanged(); |
| 568 | } |
| 569 | /** |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 570 | * Get the device status as active or non-active per Bluetooth profile. |
| 571 | * |
| 572 | * @param bluetoothProfile the Bluetooth profile |
| 573 | * @return true if the device is active |
| 574 | */ |
| 575 | @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE) |
| 576 | public boolean isActiveDevice(int bluetoothProfile) { |
| 577 | switch (bluetoothProfile) { |
| 578 | case BluetoothProfile.A2DP: |
| 579 | return mIsActiveDeviceA2dp; |
| 580 | case BluetoothProfile.HEADSET: |
| 581 | return mIsActiveDeviceHeadset; |
Hansong Zhang | d7b3591 | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 582 | case BluetoothProfile.HEARING_AID: |
| 583 | return mIsActiveDeviceHearingAid; |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame] | 584 | default: |
| 585 | Log.w(TAG, "getActiveDevice: unknown profile " + bluetoothProfile); |
| 586 | break; |
| 587 | } |
| 588 | return false; |
| 589 | } |
| 590 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 591 | void setRssi(short rssi) { |
| 592 | if (mRssi != rssi) { |
| 593 | mRssi = rssi; |
| 594 | dispatchAttributesChanged(); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * Checks whether we are connected to this device (any profile counts). |
| 600 | * |
| 601 | * @return Whether it is connected. |
| 602 | */ |
| 603 | public boolean isConnected() { |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 604 | synchronized (mProfileLock) { |
| 605 | for (LocalBluetoothProfile profile : mProfiles) { |
| 606 | int status = getProfileConnectionState(profile); |
| 607 | if (status == BluetoothProfile.STATE_CONNECTED) { |
| 608 | return true; |
| 609 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 610 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 611 | |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 612 | return false; |
| 613 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | public boolean isConnectedProfile(LocalBluetoothProfile profile) { |
| 617 | int status = getProfileConnectionState(profile); |
| 618 | return status == BluetoothProfile.STATE_CONNECTED; |
| 619 | |
| 620 | } |
| 621 | |
| 622 | public boolean isBusy() { |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 623 | synchronized (mProfileLock) { |
| 624 | for (LocalBluetoothProfile profile : mProfiles) { |
| 625 | int status = getProfileConnectionState(profile); |
| 626 | if (status == BluetoothProfile.STATE_CONNECTING |
| 627 | || status == BluetoothProfile.STATE_DISCONNECTING) { |
| 628 | return true; |
| 629 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 630 | } |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 631 | return getBondState() == BluetoothDevice.BOND_BONDING; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 632 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 633 | } |
| 634 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 635 | private boolean updateProfiles() { |
| 636 | ParcelUuid[] uuids = mDevice.getUuids(); |
| 637 | if (uuids == null) return false; |
| 638 | |
| 639 | ParcelUuid[] localUuids = mLocalAdapter.getUuids(); |
| 640 | if (localUuids == null) return false; |
| 641 | |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 642 | /* |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 643 | * Now we know if the device supports PBAP, update permissions... |
| 644 | */ |
| 645 | processPhonebookAccess(); |
| 646 | |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 647 | synchronized (mProfileLock) { |
| 648 | mProfileManager.updateProfiles(uuids, localUuids, mProfiles, mRemovedProfiles, |
| 649 | mLocalNapRoleConnected, mDevice); |
| 650 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 651 | |
Kunhung Li | e7b7cb8 | 2018-05-17 11:04:54 +0800 | [diff] [blame] | 652 | if (BluetoothUtils.D) { |
Rahul Sabnis | 24ad4b5 | 2019-10-22 13:37:29 -0700 | [diff] [blame] | 653 | Log.e(TAG, "updating profiles for " + mDevice.getAlias() + ", " + mDevice); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 654 | BluetoothClass bluetoothClass = mDevice.getBluetoothClass(); |
| 655 | |
| 656 | if (bluetoothClass != null) Log.v(TAG, "Class: " + bluetoothClass.toString()); |
| 657 | Log.v(TAG, "UUID:"); |
| 658 | for (ParcelUuid uuid : uuids) { |
| 659 | Log.v(TAG, " " + uuid); |
| 660 | } |
| 661 | } |
| 662 | return true; |
| 663 | } |
| 664 | |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 665 | private void fetchActiveDevices() { |
| 666 | A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile(); |
| 667 | if (a2dpProfile != null) { |
| 668 | mIsActiveDeviceA2dp = mDevice.equals(a2dpProfile.getActiveDevice()); |
| 669 | } |
| 670 | HeadsetProfile headsetProfile = mProfileManager.getHeadsetProfile(); |
| 671 | if (headsetProfile != null) { |
| 672 | mIsActiveDeviceHeadset = mDevice.equals(headsetProfile.getActiveDevice()); |
| 673 | } |
Hansong Zhang | d7b3591 | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 674 | HearingAidProfile hearingAidProfile = mProfileManager.getHearingAidProfile(); |
| 675 | if (hearingAidProfile != null) { |
Hansong Zhang | 3b8f09b | 2018-03-28 16:53:10 -0700 | [diff] [blame] | 676 | mIsActiveDeviceHearingAid = hearingAidProfile.getActiveDevices().contains(mDevice); |
Hansong Zhang | d7b3591 | 2018-03-16 09:15:48 -0700 | [diff] [blame] | 677 | } |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 678 | } |
| 679 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 680 | /** |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 681 | * Refreshes the UI when framework alerts us of a UUID change. |
| 682 | */ |
| 683 | void onUuidChanged() { |
| 684 | updateProfiles(); |
Etan Cohen | 50d4761 | 2015-03-31 12:45:23 -0700 | [diff] [blame] | 685 | ParcelUuid[] uuids = mDevice.getUuids(); |
Venkat Raghavan | 05e08c3 | 2015-04-06 16:26:11 -0700 | [diff] [blame] | 686 | |
Etan Cohen | 50d4761 | 2015-03-31 12:45:23 -0700 | [diff] [blame] | 687 | long timeout = MAX_UUID_DELAY_FOR_AUTO_CONNECT; |
Venkat Raghavan | 05e08c3 | 2015-04-06 16:26:11 -0700 | [diff] [blame] | 688 | if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.Hogp)) { |
| 689 | timeout = MAX_HOGP_DELAY_FOR_AUTO_CONNECT; |
Stanley Tng | ca33f5b | 2019-05-02 10:36:40 -0700 | [diff] [blame] | 690 | } else if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.HearingAid)) { |
| 691 | timeout = MAX_HEARING_AIDS_DELAY_FOR_AUTO_CONNECT; |
Venkat Raghavan | 05e08c3 | 2015-04-06 16:26:11 -0700 | [diff] [blame] | 692 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 693 | |
Kunhung Li | e7b7cb8 | 2018-05-17 11:04:54 +0800 | [diff] [blame] | 694 | if (BluetoothUtils.D) { |
Stanley Tng | ca33f5b | 2019-05-02 10:36:40 -0700 | [diff] [blame] | 695 | Log.d(TAG, "onUuidChanged: Time since last connect=" |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 696 | + (SystemClock.elapsedRealtime() - mConnectAttempted)); |
| 697 | } |
| 698 | |
| 699 | /* |
Venkat Raghavan | 05e08c3 | 2015-04-06 16:26:11 -0700 | [diff] [blame] | 700 | * If a connect was attempted earlier without any UUID, we will do the connect now. |
| 701 | * Otherwise, allow the connect on UUID change. |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 702 | */ |
| 703 | if (!mProfiles.isEmpty() |
Andre Eisenbach | 7be83c5 | 2015-09-03 15:20:09 -0700 | [diff] [blame] | 704 | && ((mConnectAttempted + timeout) > SystemClock.elapsedRealtime())) { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 705 | connectWithoutResettingTimer(false); |
| 706 | } |
Andre Eisenbach | 7be83c5 | 2015-09-03 15:20:09 -0700 | [diff] [blame] | 707 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 708 | dispatchAttributesChanged(); |
| 709 | } |
| 710 | |
| 711 | void onBondingStateChanged(int bondState) { |
| 712 | if (bondState == BluetoothDevice.BOND_NONE) { |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 713 | synchronized (mProfileLock) { |
| 714 | mProfiles.clear(); |
| 715 | } |
hughchen | 783a80b | 2018-08-20 17:04:56 +0800 | [diff] [blame] | 716 | mDevice.setPhonebookAccessPermission(BluetoothDevice.ACCESS_UNKNOWN); |
| 717 | mDevice.setMessageAccessPermission(BluetoothDevice.ACCESS_UNKNOWN); |
| 718 | mDevice.setSimAccessPermission(BluetoothDevice.ACCESS_UNKNOWN); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | refresh(); |
| 722 | |
| 723 | if (bondState == BluetoothDevice.BOND_BONDED) { |
| 724 | if (mDevice.isBluetoothDock()) { |
| 725 | onBondingDockConnect(); |
Jakub Pawlowski | 0278ab9 | 2016-07-20 11:55:48 -0700 | [diff] [blame] | 726 | } else if (mDevice.isBondingInitiatedLocally()) { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 727 | connect(false); |
| 728 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 732 | public BluetoothClass getBtClass() { |
hughchen | 8df81e3 | 2018-08-16 16:02:16 +0800 | [diff] [blame] | 733 | return mDevice.getBluetoothClass(); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | public List<LocalBluetoothProfile> getProfiles() { |
| 737 | return Collections.unmodifiableList(mProfiles); |
| 738 | } |
| 739 | |
| 740 | public List<LocalBluetoothProfile> getConnectableProfiles() { |
| 741 | List<LocalBluetoothProfile> connectableProfiles = |
| 742 | new ArrayList<LocalBluetoothProfile>(); |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 743 | synchronized (mProfileLock) { |
| 744 | for (LocalBluetoothProfile profile : mProfiles) { |
| 745 | if (profile.accessProfileEnabled()) { |
| 746 | connectableProfiles.add(profile); |
| 747 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | return connectableProfiles; |
| 751 | } |
| 752 | |
| 753 | public List<LocalBluetoothProfile> getRemovedProfiles() { |
| 754 | return mRemovedProfiles; |
| 755 | } |
| 756 | |
| 757 | public void registerCallback(Callback callback) { |
davidln | 0bc952a | 2019-06-17 14:48:11 -0700 | [diff] [blame] | 758 | mCallbacks.add(callback); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | public void unregisterCallback(Callback callback) { |
davidln | 0bc952a | 2019-06-17 14:48:11 -0700 | [diff] [blame] | 762 | mCallbacks.remove(callback); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 763 | } |
| 764 | |
hughchen | 8df81e3 | 2018-08-16 16:02:16 +0800 | [diff] [blame] | 765 | void dispatchAttributesChanged() { |
davidln | 0bc952a | 2019-06-17 14:48:11 -0700 | [diff] [blame] | 766 | for (Callback callback : mCallbacks) { |
| 767 | callback.onDeviceAttributesChanged(); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | |
| 771 | @Override |
| 772 | public String toString() { |
| 773 | return mDevice.toString(); |
| 774 | } |
| 775 | |
| 776 | @Override |
| 777 | public boolean equals(Object o) { |
| 778 | if ((o == null) || !(o instanceof CachedBluetoothDevice)) { |
| 779 | return false; |
| 780 | } |
| 781 | return mDevice.equals(((CachedBluetoothDevice) o).mDevice); |
| 782 | } |
| 783 | |
| 784 | @Override |
| 785 | public int hashCode() { |
| 786 | return mDevice.getAddress().hashCode(); |
| 787 | } |
| 788 | |
| 789 | // This comparison uses non-final fields so the sort order may change |
| 790 | // when device attributes change (such as bonding state). Settings |
| 791 | // will completely refresh the device list when this happens. |
| 792 | public int compareTo(CachedBluetoothDevice another) { |
| 793 | // Connected above not connected |
| 794 | int comparison = (another.isConnected() ? 1 : 0) - (isConnected() ? 1 : 0); |
| 795 | if (comparison != 0) return comparison; |
| 796 | |
| 797 | // Paired above not paired |
| 798 | comparison = (another.getBondState() == BluetoothDevice.BOND_BONDED ? 1 : 0) - |
| 799 | (getBondState() == BluetoothDevice.BOND_BONDED ? 1 : 0); |
| 800 | if (comparison != 0) return comparison; |
| 801 | |
Jack He | 5152047 | 2017-07-24 12:30:08 -0700 | [diff] [blame] | 802 | // Just discovered above discovered in the past |
| 803 | comparison = (another.mJustDiscovered ? 1 : 0) - (mJustDiscovered ? 1 : 0); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 804 | if (comparison != 0) return comparison; |
| 805 | |
| 806 | // Stronger signal above weaker signal |
| 807 | comparison = another.mRssi - mRssi; |
| 808 | if (comparison != 0) return comparison; |
| 809 | |
| 810 | // Fallback on name |
hughchen | cb6e197 | 2018-08-15 16:13:21 +0800 | [diff] [blame] | 811 | return getName().compareTo(another.getName()); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | public interface Callback { |
| 815 | void onDeviceAttributesChanged(); |
| 816 | } |
| 817 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 818 | // Migrates data from old data store (in Settings app's shared preferences) to new (in Bluetooth |
| 819 | // app's shared preferences). |
| 820 | private void migratePhonebookPermissionChoice() { |
| 821 | SharedPreferences preferences = mContext.getSharedPreferences( |
| 822 | "bluetooth_phonebook_permission", Context.MODE_PRIVATE); |
| 823 | if (!preferences.contains(mDevice.getAddress())) { |
| 824 | return; |
| 825 | } |
| 826 | |
| 827 | if (mDevice.getPhonebookAccessPermission() == BluetoothDevice.ACCESS_UNKNOWN) { |
hughchen | 783a80b | 2018-08-20 17:04:56 +0800 | [diff] [blame] | 828 | int oldPermission = |
| 829 | preferences.getInt(mDevice.getAddress(), BluetoothDevice.ACCESS_UNKNOWN); |
| 830 | if (oldPermission == BluetoothDevice.ACCESS_ALLOWED) { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 831 | mDevice.setPhonebookAccessPermission(BluetoothDevice.ACCESS_ALLOWED); |
hughchen | 783a80b | 2018-08-20 17:04:56 +0800 | [diff] [blame] | 832 | } else if (oldPermission == BluetoothDevice.ACCESS_REJECTED) { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 833 | mDevice.setPhonebookAccessPermission(BluetoothDevice.ACCESS_REJECTED); |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | SharedPreferences.Editor editor = preferences.edit(); |
| 838 | editor.remove(mDevice.getAddress()); |
| 839 | editor.commit(); |
| 840 | } |
| 841 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 842 | // Migrates data from old data store (in Settings app's shared preferences) to new (in Bluetooth |
| 843 | // app's shared preferences). |
| 844 | private void migrateMessagePermissionChoice() { |
| 845 | SharedPreferences preferences = mContext.getSharedPreferences( |
| 846 | "bluetooth_message_permission", Context.MODE_PRIVATE); |
| 847 | if (!preferences.contains(mDevice.getAddress())) { |
| 848 | return; |
| 849 | } |
| 850 | |
| 851 | if (mDevice.getMessageAccessPermission() == BluetoothDevice.ACCESS_UNKNOWN) { |
hughchen | 783a80b | 2018-08-20 17:04:56 +0800 | [diff] [blame] | 852 | int oldPermission = |
| 853 | preferences.getInt(mDevice.getAddress(), BluetoothDevice.ACCESS_UNKNOWN); |
| 854 | if (oldPermission == BluetoothDevice.ACCESS_ALLOWED) { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 855 | mDevice.setMessageAccessPermission(BluetoothDevice.ACCESS_ALLOWED); |
hughchen | 783a80b | 2018-08-20 17:04:56 +0800 | [diff] [blame] | 856 | } else if (oldPermission == BluetoothDevice.ACCESS_REJECTED) { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 857 | mDevice.setMessageAccessPermission(BluetoothDevice.ACCESS_REJECTED); |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | SharedPreferences.Editor editor = preferences.edit(); |
| 862 | editor.remove(mDevice.getAddress()); |
| 863 | editor.commit(); |
| 864 | } |
| 865 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 866 | private void processPhonebookAccess() { |
| 867 | if (mDevice.getBondState() != BluetoothDevice.BOND_BONDED) return; |
| 868 | |
| 869 | ParcelUuid[] uuids = mDevice.getUuids(); |
| 870 | if (BluetoothUuid.containsAnyUuid(uuids, PbapServerProfile.PBAB_CLIENT_UUIDS)) { |
| 871 | // The pairing dialog now warns of phone-book access for paired devices. |
| 872 | // No separate prompt is displayed after pairing. |
hughchen | 783a80b | 2018-08-20 17:04:56 +0800 | [diff] [blame] | 873 | if (mDevice.getPhonebookAccessPermission() == BluetoothDevice.ACCESS_UNKNOWN) { |
Sanket Padawe | 07533db | 2015-11-11 15:01:35 -0800 | [diff] [blame] | 874 | if (mDevice.getBluetoothClass().getDeviceClass() |
Hemant Gupta | b8d267a | 2016-06-07 12:53:59 +0530 | [diff] [blame] | 875 | == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE || |
| 876 | mDevice.getBluetoothClass().getDeviceClass() |
| 877 | == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET) { |
Zongheng Wang | 810c6d2 | 2019-09-05 13:44:28 -0700 | [diff] [blame] | 878 | EventLog.writeEvent(0x534e4554, "138529441", -1, ""); |
Sanket Padawe | 07533db | 2015-11-11 15:01:35 -0800 | [diff] [blame] | 879 | } |
Zongheng Wang | 810c6d2 | 2019-09-05 13:44:28 -0700 | [diff] [blame] | 880 | mDevice.setPhonebookAccessPermission(BluetoothDevice.ACCESS_REJECTED); |
Sanket Padawe | 78c2376 | 2015-06-01 15:55:30 -0700 | [diff] [blame] | 881 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 882 | } |
| 883 | } |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 884 | |
| 885 | public int getMaxConnectionState() { |
| 886 | int maxState = BluetoothProfile.STATE_DISCONNECTED; |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 887 | synchronized (mProfileLock) { |
| 888 | for (LocalBluetoothProfile profile : getProfiles()) { |
| 889 | int connectionStatus = getProfileConnectionState(profile); |
| 890 | if (connectionStatus > maxState) { |
| 891 | maxState = connectionStatus; |
| 892 | } |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 893 | } |
| 894 | } |
| 895 | return maxState; |
| 896 | } |
| 897 | |
| 898 | /** |
Lei Yu | e63ca1b | 2019-05-13 16:07:57 -0700 | [diff] [blame] | 899 | * Return full summary that describes connection state of this device |
| 900 | * |
| 901 | * @see #getConnectionSummary(boolean shortSummary) |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 902 | */ |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 903 | public String getConnectionSummary() { |
Lei Yu | e63ca1b | 2019-05-13 16:07:57 -0700 | [diff] [blame] | 904 | return getConnectionSummary(false /* shortSummary */); |
| 905 | } |
| 906 | |
| 907 | /** |
| 908 | * Return summary that describes connection state of this device. Summary depends on: |
| 909 | * 1. Whether device has battery info |
| 910 | * 2. Whether device is in active usage(or in phone call) |
| 911 | * |
| 912 | * @param shortSummary {@code true} if need to return short version summary |
| 913 | */ |
| 914 | public String getConnectionSummary(boolean shortSummary) { |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 915 | boolean profileConnected = false; // Updated as long as BluetoothProfile is connected |
| 916 | boolean a2dpConnected = true; // A2DP is connected |
| 917 | boolean hfpConnected = true; // HFP is connected |
| 918 | boolean hearingAidConnected = true; // Hearing Aid is connected |
jackqdyulei | 1f9c171 | 2019-03-05 16:42:57 -0800 | [diff] [blame] | 919 | int leftBattery = -1; |
| 920 | int rightBattery = -1; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 921 | |
hughchen | 00ce025 | 2019-10-28 17:03:04 +0800 | [diff] [blame] | 922 | if (isProfileConnectedFail() && isConnected()) { |
| 923 | return mContext.getString(R.string.profile_connect_timeout_subtext); |
| 924 | } |
| 925 | |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 926 | synchronized (mProfileLock) { |
| 927 | for (LocalBluetoothProfile profile : getProfiles()) { |
| 928 | int connectionStatus = getProfileConnectionState(profile); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 929 | |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 930 | switch (connectionStatus) { |
| 931 | case BluetoothProfile.STATE_CONNECTING: |
| 932 | case BluetoothProfile.STATE_DISCONNECTING: |
| 933 | return mContext.getString( |
| 934 | BluetoothUtils.getConnectionStateSummary(connectionStatus)); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 935 | |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 936 | case BluetoothProfile.STATE_CONNECTED: |
| 937 | profileConnected = true; |
| 938 | break; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 939 | |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 940 | case BluetoothProfile.STATE_DISCONNECTED: |
| 941 | if (profile.isProfileReady()) { |
| 942 | if (profile instanceof A2dpProfile |
| 943 | || profile instanceof A2dpSinkProfile) { |
| 944 | a2dpConnected = false; |
| 945 | } else if (profile instanceof HeadsetProfile |
| 946 | || profile instanceof HfpClientProfile) { |
| 947 | hfpConnected = false; |
| 948 | } else if (profile instanceof HearingAidProfile) { |
| 949 | hearingAidConnected = false; |
| 950 | } |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 951 | } |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 952 | break; |
| 953 | } |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 957 | String batteryLevelPercentageString = null; |
| 958 | // Android framework should only set mBatteryLevel to valid range [0-100] or |
| 959 | // BluetoothDevice.BATTERY_LEVEL_UNKNOWN, any other value should be a framework bug. |
| 960 | // Thus assume here that if value is not BluetoothDevice.BATTERY_LEVEL_UNKNOWN, it must |
| 961 | // be valid |
| 962 | final int batteryLevel = getBatteryLevel(); |
| 963 | if (batteryLevel != BluetoothDevice.BATTERY_LEVEL_UNKNOWN) { |
| 964 | // TODO: name com.android.settingslib.bluetooth.Utils something different |
| 965 | batteryLevelPercentageString = |
| 966 | com.android.settingslib.Utils.formatPercentage(batteryLevel); |
| 967 | } |
| 968 | |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 969 | int stringRes = R.string.bluetooth_pairing; |
| 970 | //when profile is connected, information would be available |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 971 | if (profileConnected) { |
jackqdyulei | 1f9c171 | 2019-03-05 16:42:57 -0800 | [diff] [blame] | 972 | // Update Meta data for connected device |
Ugo Yu | 73c387d | 2019-03-20 19:50:58 +0800 | [diff] [blame] | 973 | if (BluetoothUtils.getBooleanMetaData( |
| 974 | mDevice, BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET)) { |
| 975 | leftBattery = BluetoothUtils.getIntMetaData(mDevice, |
| 976 | BluetoothDevice.METADATA_UNTETHERED_LEFT_BATTERY); |
| 977 | rightBattery = BluetoothUtils.getIntMetaData(mDevice, |
| 978 | BluetoothDevice.METADATA_UNTETHERED_RIGHT_BATTERY); |
jackqdyulei | 1f9c171 | 2019-03-05 16:42:57 -0800 | [diff] [blame] | 979 | } |
| 980 | |
wengsu | 6d263c2 | 2018-11-15 16:51:45 +0800 | [diff] [blame] | 981 | // Set default string with battery level in device connected situation. |
jackqdyulei | 1f9c171 | 2019-03-05 16:42:57 -0800 | [diff] [blame] | 982 | if (isTwsBatteryAvailable(leftBattery, rightBattery)) { |
| 983 | stringRes = R.string.bluetooth_battery_level_untethered; |
| 984 | } else if (batteryLevelPercentageString != null) { |
wengsu | 6d263c2 | 2018-11-15 16:51:45 +0800 | [diff] [blame] | 985 | stringRes = R.string.bluetooth_battery_level; |
| 986 | } |
| 987 | |
| 988 | // Set active string in following device connected situation. |
| 989 | // 1. Hearing Aid device active. |
| 990 | // 2. Headset device active with in-calling state. |
| 991 | // 3. A2DP device active without in-calling state. |
timhypeng | f050932 | 2018-03-29 14:23:21 +0800 | [diff] [blame] | 992 | if (a2dpConnected || hfpConnected || hearingAidConnected) { |
jackqdyulei | 1f9c171 | 2019-03-05 16:42:57 -0800 | [diff] [blame] | 993 | final boolean isOnCall = Utils.isAudioModeOngoingCall(mContext); |
wengsu | 6d263c2 | 2018-11-15 16:51:45 +0800 | [diff] [blame] | 994 | if ((mIsActiveDeviceHearingAid) |
| 995 | || (mIsActiveDeviceHeadset && isOnCall) |
| 996 | || (mIsActiveDeviceA2dp && !isOnCall)) { |
Lei Yu | e63ca1b | 2019-05-13 16:07:57 -0700 | [diff] [blame] | 997 | if (isTwsBatteryAvailable(leftBattery, rightBattery) && !shortSummary) { |
jackqdyulei | 1f9c171 | 2019-03-05 16:42:57 -0800 | [diff] [blame] | 998 | stringRes = R.string.bluetooth_active_battery_level_untethered; |
Lei Yu | e63ca1b | 2019-05-13 16:07:57 -0700 | [diff] [blame] | 999 | } else if (batteryLevelPercentageString != null && !shortSummary) { |
jackqdyulei | 1f9c171 | 2019-03-05 16:42:57 -0800 | [diff] [blame] | 1000 | stringRes = R.string.bluetooth_active_battery_level; |
| 1001 | } else { |
| 1002 | stringRes = R.string.bluetooth_active_no_battery_level; |
| 1003 | } |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 1004 | } |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 1005 | } |
| 1006 | } |
| 1007 | |
jackqdyulei | 1f9c171 | 2019-03-05 16:42:57 -0800 | [diff] [blame] | 1008 | if (stringRes != R.string.bluetooth_pairing |
| 1009 | || getBondState() == BluetoothDevice.BOND_BONDING) { |
| 1010 | if (isTwsBatteryAvailable(leftBattery, rightBattery)) { |
| 1011 | return mContext.getString(stringRes, Utils.formatPercentage(leftBattery), |
| 1012 | Utils.formatPercentage(rightBattery)); |
| 1013 | } else { |
| 1014 | return mContext.getString(stringRes, batteryLevelPercentageString); |
| 1015 | } |
| 1016 | } else { |
| 1017 | return null; |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | private boolean isTwsBatteryAvailable(int leftBattery, int rightBattery) { |
| 1022 | return leftBattery >= 0 && rightBattery >= 0; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 1023 | } |
hughchen | 23b947e | 2018-03-31 17:32:53 +0800 | [diff] [blame] | 1024 | |
hughchen | 00ce025 | 2019-10-28 17:03:04 +0800 | [diff] [blame] | 1025 | private boolean isProfileConnectedFail() { |
| 1026 | return mIsA2dpProfileConnectedFail || mIsHearingAidProfileConnectedFail |
| 1027 | || mIsHeadsetProfileConnectedFail; |
| 1028 | } |
| 1029 | |
hughchen | 23b947e | 2018-03-31 17:32:53 +0800 | [diff] [blame] | 1030 | /** |
ryanywlin | e26aecd | 2018-05-15 14:20:50 +0800 | [diff] [blame] | 1031 | * @return resource for android auto string that describes the connection state of this device. |
| 1032 | */ |
| 1033 | public String getCarConnectionSummary() { |
| 1034 | boolean profileConnected = false; // at least one profile is connected |
| 1035 | boolean a2dpNotConnected = false; // A2DP is preferred but not connected |
| 1036 | boolean hfpNotConnected = false; // HFP is preferred but not connected |
| 1037 | boolean hearingAidNotConnected = false; // Hearing Aid is preferred but not connected |
| 1038 | |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 1039 | synchronized (mProfileLock) { |
| 1040 | for (LocalBluetoothProfile profile : getProfiles()) { |
| 1041 | int connectionStatus = getProfileConnectionState(profile); |
ryanywlin | e26aecd | 2018-05-15 14:20:50 +0800 | [diff] [blame] | 1042 | |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 1043 | switch (connectionStatus) { |
| 1044 | case BluetoothProfile.STATE_CONNECTING: |
| 1045 | case BluetoothProfile.STATE_DISCONNECTING: |
| 1046 | return mContext.getString( |
| 1047 | BluetoothUtils.getConnectionStateSummary(connectionStatus)); |
ryanywlin | e26aecd | 2018-05-15 14:20:50 +0800 | [diff] [blame] | 1048 | |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 1049 | case BluetoothProfile.STATE_CONNECTED: |
| 1050 | profileConnected = true; |
| 1051 | break; |
ryanywlin | e26aecd | 2018-05-15 14:20:50 +0800 | [diff] [blame] | 1052 | |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 1053 | case BluetoothProfile.STATE_DISCONNECTED: |
| 1054 | if (profile.isProfileReady()) { |
| 1055 | if (profile instanceof A2dpProfile |
| 1056 | || profile instanceof A2dpSinkProfile) { |
| 1057 | a2dpNotConnected = true; |
| 1058 | } else if (profile instanceof HeadsetProfile |
| 1059 | || profile instanceof HfpClientProfile) { |
| 1060 | hfpNotConnected = true; |
| 1061 | } else if (profile instanceof HearingAidProfile) { |
| 1062 | hearingAidNotConnected = true; |
| 1063 | } |
ryanywlin | e26aecd | 2018-05-15 14:20:50 +0800 | [diff] [blame] | 1064 | } |
jackqdyulei | 1eb10ec | 2018-11-01 13:10:09 -0700 | [diff] [blame] | 1065 | break; |
| 1066 | } |
ryanywlin | e26aecd | 2018-05-15 14:20:50 +0800 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | String batteryLevelPercentageString = null; |
| 1071 | // Android framework should only set mBatteryLevel to valid range [0-100] or |
| 1072 | // BluetoothDevice.BATTERY_LEVEL_UNKNOWN, any other value should be a framework bug. |
| 1073 | // Thus assume here that if value is not BluetoothDevice.BATTERY_LEVEL_UNKNOWN, it must |
| 1074 | // be valid |
| 1075 | final int batteryLevel = getBatteryLevel(); |
| 1076 | if (batteryLevel != BluetoothDevice.BATTERY_LEVEL_UNKNOWN) { |
| 1077 | // TODO: name com.android.settingslib.bluetooth.Utils something different |
| 1078 | batteryLevelPercentageString = |
| 1079 | com.android.settingslib.Utils.formatPercentage(batteryLevel); |
| 1080 | } |
| 1081 | |
| 1082 | // Prepare the string for the Active Device summary |
| 1083 | String[] activeDeviceStringsArray = mContext.getResources().getStringArray( |
| 1084 | R.array.bluetooth_audio_active_device_summaries); |
| 1085 | String activeDeviceString = activeDeviceStringsArray[0]; // Default value: not active |
| 1086 | if (mIsActiveDeviceA2dp && mIsActiveDeviceHeadset) { |
| 1087 | activeDeviceString = activeDeviceStringsArray[1]; // Active for Media and Phone |
| 1088 | } else { |
| 1089 | if (mIsActiveDeviceA2dp) { |
| 1090 | activeDeviceString = activeDeviceStringsArray[2]; // Active for Media only |
| 1091 | } |
| 1092 | if (mIsActiveDeviceHeadset) { |
| 1093 | activeDeviceString = activeDeviceStringsArray[3]; // Active for Phone only |
| 1094 | } |
| 1095 | } |
| 1096 | if (!hearingAidNotConnected && mIsActiveDeviceHearingAid) { |
| 1097 | activeDeviceString = activeDeviceStringsArray[1]; |
| 1098 | return mContext.getString(R.string.bluetooth_connected, activeDeviceString); |
| 1099 | } |
| 1100 | |
| 1101 | if (profileConnected) { |
| 1102 | if (a2dpNotConnected && hfpNotConnected) { |
| 1103 | if (batteryLevelPercentageString != null) { |
| 1104 | return mContext.getString( |
| 1105 | R.string.bluetooth_connected_no_headset_no_a2dp_battery_level, |
| 1106 | batteryLevelPercentageString, activeDeviceString); |
| 1107 | } else { |
| 1108 | return mContext.getString(R.string.bluetooth_connected_no_headset_no_a2dp, |
| 1109 | activeDeviceString); |
| 1110 | } |
| 1111 | |
| 1112 | } else if (a2dpNotConnected) { |
| 1113 | if (batteryLevelPercentageString != null) { |
| 1114 | return mContext.getString(R.string.bluetooth_connected_no_a2dp_battery_level, |
| 1115 | batteryLevelPercentageString, activeDeviceString); |
| 1116 | } else { |
| 1117 | return mContext.getString(R.string.bluetooth_connected_no_a2dp, |
| 1118 | activeDeviceString); |
| 1119 | } |
| 1120 | |
| 1121 | } else if (hfpNotConnected) { |
| 1122 | if (batteryLevelPercentageString != null) { |
| 1123 | return mContext.getString(R.string.bluetooth_connected_no_headset_battery_level, |
| 1124 | batteryLevelPercentageString, activeDeviceString); |
| 1125 | } else { |
| 1126 | return mContext.getString(R.string.bluetooth_connected_no_headset, |
| 1127 | activeDeviceString); |
| 1128 | } |
| 1129 | } else { |
| 1130 | if (batteryLevelPercentageString != null) { |
| 1131 | return mContext.getString(R.string.bluetooth_connected_battery_level, |
| 1132 | batteryLevelPercentageString, activeDeviceString); |
| 1133 | } else { |
| 1134 | return mContext.getString(R.string.bluetooth_connected, activeDeviceString); |
| 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | return getBondState() == BluetoothDevice.BOND_BONDING ? |
| 1140 | mContext.getString(R.string.bluetooth_pairing) : null; |
| 1141 | } |
| 1142 | |
| 1143 | /** |
hughchen | 23b947e | 2018-03-31 17:32:53 +0800 | [diff] [blame] | 1144 | * @return {@code true} if {@code cachedBluetoothDevice} is a2dp device |
| 1145 | */ |
timhypeng | 0cd2ecc | 2018-08-20 15:51:04 +0800 | [diff] [blame] | 1146 | public boolean isConnectedA2dpDevice() { |
Hansong Zhang | 28963cd | 2018-06-13 11:30:59 -0700 | [diff] [blame] | 1147 | A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile(); |
| 1148 | return a2dpProfile != null && a2dpProfile.getConnectionStatus(mDevice) == |
hughchen | 23b947e | 2018-03-31 17:32:53 +0800 | [diff] [blame] | 1149 | BluetoothProfile.STATE_CONNECTED; |
| 1150 | } |
| 1151 | |
| 1152 | /** |
| 1153 | * @return {@code true} if {@code cachedBluetoothDevice} is HFP device |
| 1154 | */ |
timhypeng | a025ad3 | 2018-08-20 16:01:16 +0800 | [diff] [blame] | 1155 | public boolean isConnectedHfpDevice() { |
Hansong Zhang | 28963cd | 2018-06-13 11:30:59 -0700 | [diff] [blame] | 1156 | HeadsetProfile headsetProfile = mProfileManager.getHeadsetProfile(); |
| 1157 | return headsetProfile != null && headsetProfile.getConnectionStatus(mDevice) == |
hughchen | 23b947e | 2018-03-31 17:32:53 +0800 | [diff] [blame] | 1158 | BluetoothProfile.STATE_CONNECTED; |
| 1159 | } |
timhypeng | c509a65 | 2018-05-25 14:23:44 +0800 | [diff] [blame] | 1160 | |
| 1161 | /** |
| 1162 | * @return {@code true} if {@code cachedBluetoothDevice} is Hearing Aid device |
| 1163 | */ |
| 1164 | public boolean isConnectedHearingAidDevice() { |
Hansong Zhang | 28963cd | 2018-06-13 11:30:59 -0700 | [diff] [blame] | 1165 | HearingAidProfile hearingAidProfile = mProfileManager.getHearingAidProfile(); |
| 1166 | return hearingAidProfile != null && hearingAidProfile.getConnectionStatus(mDevice) == |
timhypeng | c509a65 | 2018-05-25 14:23:44 +0800 | [diff] [blame] | 1167 | BluetoothProfile.STATE_CONNECTED; |
| 1168 | } |
timhypeng | 5c62ebb | 2018-08-28 09:59:03 +0800 | [diff] [blame] | 1169 | |
| 1170 | public CachedBluetoothDevice getSubDevice() { |
| 1171 | return mSubDevice; |
| 1172 | } |
| 1173 | |
| 1174 | public void setSubDevice(CachedBluetoothDevice subDevice) { |
| 1175 | mSubDevice = subDevice; |
| 1176 | } |
| 1177 | |
| 1178 | public void switchSubDeviceContent() { |
| 1179 | // Backup from main device |
| 1180 | BluetoothDevice tmpDevice = mDevice; |
| 1181 | short tmpRssi = mRssi; |
| 1182 | boolean tmpJustDiscovered = mJustDiscovered; |
| 1183 | // Set main device from sub device |
| 1184 | mDevice = mSubDevice.mDevice; |
| 1185 | mRssi = mSubDevice.mRssi; |
| 1186 | mJustDiscovered = mSubDevice.mJustDiscovered; |
| 1187 | // Set sub device from backup |
| 1188 | mSubDevice.mDevice = tmpDevice; |
| 1189 | mSubDevice.mRssi = tmpRssi; |
| 1190 | mSubDevice.mJustDiscovered = tmpJustDiscovered; |
| 1191 | fetchActiveDevices(); |
| 1192 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 1193 | } |