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 | |
| 19 | import android.bluetooth.BluetoothClass; |
| 20 | import android.bluetooth.BluetoothDevice; |
| 21 | import android.bluetooth.BluetoothProfile; |
| 22 | import android.bluetooth.BluetoothUuid; |
| 23 | import android.content.Context; |
| 24 | import android.content.SharedPreferences; |
| 25 | import android.os.ParcelUuid; |
| 26 | import android.os.SystemClock; |
| 27 | import android.text.TextUtils; |
| 28 | import android.util.Log; |
| 29 | import android.bluetooth.BluetoothAdapter; |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame^] | 30 | import android.support.annotation.VisibleForTesting; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 31 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 32 | import com.android.settingslib.R; |
| 33 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 34 | import java.util.ArrayList; |
| 35 | import java.util.Collection; |
| 36 | import java.util.Collections; |
| 37 | import java.util.HashMap; |
| 38 | import java.util.List; |
| 39 | |
| 40 | /** |
| 41 | * CachedBluetoothDevice represents a remote Bluetooth device. It contains |
| 42 | * attributes of the device (such as the address, name, RSSI, etc.) and |
| 43 | * functionality that can be performed on the device (connect, pair, disconnect, |
| 44 | * etc.). |
| 45 | */ |
Fan Zhang | 82dd3b0 | 2016-12-27 13:13:00 -0800 | [diff] [blame] | 46 | public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 47 | private static final String TAG = "CachedBluetoothDevice"; |
| 48 | private static final boolean DEBUG = Utils.V; |
| 49 | |
| 50 | private final Context mContext; |
| 51 | private final LocalBluetoothAdapter mLocalAdapter; |
| 52 | private final LocalBluetoothProfileManager mProfileManager; |
| 53 | private final BluetoothDevice mDevice; |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 54 | //TODO: consider remove, BluetoothDevice.getName() is already cached |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 55 | private String mName; |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 56 | // Need this since there is no method for getting RSSI |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 57 | private short mRssi; |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 58 | //TODO: consider remove, BluetoothDevice.getBluetoothClass() is already cached |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 59 | private BluetoothClass mBtClass; |
| 60 | private HashMap<LocalBluetoothProfile, Integer> mProfileConnectionState; |
| 61 | |
| 62 | private final List<LocalBluetoothProfile> mProfiles = |
| 63 | new ArrayList<LocalBluetoothProfile>(); |
| 64 | |
| 65 | // List of profiles that were previously in mProfiles, but have been removed |
| 66 | private final List<LocalBluetoothProfile> mRemovedProfiles = |
| 67 | new ArrayList<LocalBluetoothProfile>(); |
| 68 | |
| 69 | // Device supports PANU but not NAP: remove PanProfile after device disconnects from NAP |
| 70 | private boolean mLocalNapRoleConnected; |
| 71 | |
Jack He | 5152047 | 2017-07-24 12:30:08 -0700 | [diff] [blame] | 72 | private boolean mJustDiscovered; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 73 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 74 | private int mMessageRejectionCount; |
| 75 | |
| 76 | private final Collection<Callback> mCallbacks = new ArrayList<Callback>(); |
| 77 | |
| 78 | // Following constants indicate the user's choices of Phone book/message access settings |
| 79 | // User hasn't made any choice or settings app has wiped out the memory |
| 80 | public final static int ACCESS_UNKNOWN = 0; |
| 81 | // User has accepted the connection and let Settings app remember the decision |
| 82 | public final static int ACCESS_ALLOWED = 1; |
| 83 | // User has rejected the connection and let Settings app remember the decision |
| 84 | public final static int ACCESS_REJECTED = 2; |
| 85 | |
| 86 | // How many times user should reject the connection to make the choice persist. |
| 87 | private final static int MESSAGE_REJECTION_COUNT_LIMIT_TO_PERSIST = 2; |
| 88 | |
| 89 | private final static String MESSAGE_REJECTION_COUNT_PREFS_NAME = "bluetooth_message_reject"; |
| 90 | |
| 91 | /** |
| 92 | * When we connect to multiple profiles, we only want to display a single |
| 93 | * error even if they all fail. This tracks that state. |
| 94 | */ |
| 95 | private boolean mIsConnectingErrorPossible; |
| 96 | |
| 97 | /** |
| 98 | * Last time a bt profile auto-connect was attempted. |
| 99 | * If an ACTION_UUID intent comes in within |
| 100 | * MAX_UUID_DELAY_FOR_AUTO_CONNECT milliseconds, we will try auto-connect |
| 101 | * again with the new UUIDs |
| 102 | */ |
| 103 | private long mConnectAttempted; |
| 104 | |
| 105 | // See mConnectAttempted |
| 106 | private static final long MAX_UUID_DELAY_FOR_AUTO_CONNECT = 5000; |
Etan Cohen | 50d4761 | 2015-03-31 12:45:23 -0700 | [diff] [blame] | 107 | private static final long MAX_HOGP_DELAY_FOR_AUTO_CONNECT = 30000; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 108 | |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 109 | // Active device state |
| 110 | private boolean mIsActiveDeviceA2dp = false; |
| 111 | private boolean mIsActiveDeviceHeadset = false; |
| 112 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 113 | /** |
| 114 | * Describes the current device and profile for logging. |
| 115 | * |
| 116 | * @param profile Profile to describe |
| 117 | * @return Description of the device and profile |
| 118 | */ |
| 119 | private String describe(LocalBluetoothProfile profile) { |
| 120 | StringBuilder sb = new StringBuilder(); |
| 121 | sb.append("Address:").append(mDevice); |
| 122 | if (profile != null) { |
| 123 | sb.append(" Profile:").append(profile); |
| 124 | } |
| 125 | |
| 126 | return sb.toString(); |
| 127 | } |
| 128 | |
| 129 | void onProfileStateChanged(LocalBluetoothProfile profile, int newProfileState) { |
| 130 | if (Utils.D) { |
| 131 | Log.d(TAG, "onProfileStateChanged: profile " + profile + |
| 132 | " newProfileState " + newProfileState); |
| 133 | } |
| 134 | if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_TURNING_OFF) |
| 135 | { |
| 136 | if (Utils.D) Log.d(TAG, " BT Turninig Off...Profile conn state change ignored..."); |
| 137 | return; |
| 138 | } |
| 139 | mProfileConnectionState.put(profile, newProfileState); |
| 140 | if (newProfileState == BluetoothProfile.STATE_CONNECTED) { |
| 141 | if (profile instanceof MapProfile) { |
| 142 | profile.setPreferred(mDevice, true); |
Hemant Gupta | dbc3d8d | 2017-05-12 21:14:44 +0530 | [diff] [blame] | 143 | } |
| 144 | if (!mProfiles.contains(profile)) { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 145 | mRemovedProfiles.remove(profile); |
| 146 | mProfiles.add(profile); |
| 147 | if (profile instanceof PanProfile && |
| 148 | ((PanProfile) profile).isLocalRoleNap(mDevice)) { |
| 149 | // Device doesn't support NAP, so remove PanProfile on disconnect |
| 150 | mLocalNapRoleConnected = true; |
| 151 | } |
| 152 | } |
| 153 | } else if (profile instanceof MapProfile && |
| 154 | newProfileState == BluetoothProfile.STATE_DISCONNECTED) { |
| 155 | profile.setPreferred(mDevice, false); |
| 156 | } else if (mLocalNapRoleConnected && profile instanceof PanProfile && |
| 157 | ((PanProfile) profile).isLocalRoleNap(mDevice) && |
| 158 | newProfileState == BluetoothProfile.STATE_DISCONNECTED) { |
| 159 | Log.d(TAG, "Removing PanProfile from device after NAP disconnect"); |
| 160 | mProfiles.remove(profile); |
| 161 | mRemovedProfiles.add(profile); |
| 162 | mLocalNapRoleConnected = false; |
| 163 | } |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 164 | fetchActiveDevices(); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | CachedBluetoothDevice(Context context, |
| 168 | LocalBluetoothAdapter adapter, |
| 169 | LocalBluetoothProfileManager profileManager, |
| 170 | BluetoothDevice device) { |
| 171 | mContext = context; |
| 172 | mLocalAdapter = adapter; |
| 173 | mProfileManager = profileManager; |
| 174 | mDevice = device; |
| 175 | mProfileConnectionState = new HashMap<LocalBluetoothProfile, Integer>(); |
| 176 | fillData(); |
| 177 | } |
| 178 | |
| 179 | public void disconnect() { |
| 180 | for (LocalBluetoothProfile profile : mProfiles) { |
| 181 | disconnect(profile); |
| 182 | } |
| 183 | // Disconnect PBAP server in case its connected |
| 184 | // This is to ensure all the profiles are disconnected as some CK/Hs do not |
| 185 | // disconnect PBAP connection when HF connection is brought down |
| 186 | PbapServerProfile PbapProfile = mProfileManager.getPbapProfile(); |
| 187 | if (PbapProfile.getConnectionStatus(mDevice) == BluetoothProfile.STATE_CONNECTED) |
| 188 | { |
| 189 | PbapProfile.disconnect(mDevice); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | public void disconnect(LocalBluetoothProfile profile) { |
| 194 | if (profile.disconnect(mDevice)) { |
| 195 | if (Utils.D) { |
| 196 | Log.d(TAG, "Command sent successfully:DISCONNECT " + describe(profile)); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | public void connect(boolean connectAllProfiles) { |
| 202 | if (!ensurePaired()) { |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | mConnectAttempted = SystemClock.elapsedRealtime(); |
| 207 | connectWithoutResettingTimer(connectAllProfiles); |
| 208 | } |
| 209 | |
| 210 | void onBondingDockConnect() { |
| 211 | // Attempt to connect if UUIDs are available. Otherwise, |
| 212 | // we will connect when the ACTION_UUID intent arrives. |
| 213 | connect(false); |
| 214 | } |
| 215 | |
| 216 | private void connectWithoutResettingTimer(boolean connectAllProfiles) { |
| 217 | // Try to initialize the profiles if they were not. |
| 218 | if (mProfiles.isEmpty()) { |
| 219 | // if mProfiles is empty, then do not invoke updateProfiles. This causes a race |
| 220 | // condition with carkits during pairing, wherein RemoteDevice.UUIDs have been updated |
| 221 | // from bluetooth stack but ACTION.uuid is not sent yet. |
| 222 | // Eventually ACTION.uuid will be received which shall trigger the connection of the |
| 223 | // various profiles |
| 224 | // If UUIDs are not available yet, connect will be happen |
| 225 | // upon arrival of the ACTION_UUID intent. |
| 226 | Log.d(TAG, "No profiles. Maybe we will connect later"); |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | // Reset the only-show-one-error-dialog tracking variable |
| 231 | mIsConnectingErrorPossible = true; |
| 232 | |
| 233 | int preferredProfiles = 0; |
| 234 | for (LocalBluetoothProfile profile : mProfiles) { |
| 235 | if (connectAllProfiles ? profile.isConnectable() : profile.isAutoConnectable()) { |
| 236 | if (profile.isPreferred(mDevice)) { |
| 237 | ++preferredProfiles; |
| 238 | connectInt(profile); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | if (DEBUG) Log.d(TAG, "Preferred profiles = " + preferredProfiles); |
| 243 | |
| 244 | if (preferredProfiles == 0) { |
| 245 | connectAutoConnectableProfiles(); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | private void connectAutoConnectableProfiles() { |
| 250 | if (!ensurePaired()) { |
| 251 | return; |
| 252 | } |
| 253 | // Reset the only-show-one-error-dialog tracking variable |
| 254 | mIsConnectingErrorPossible = true; |
| 255 | |
| 256 | for (LocalBluetoothProfile profile : mProfiles) { |
| 257 | if (profile.isAutoConnectable()) { |
| 258 | profile.setPreferred(mDevice, true); |
| 259 | connectInt(profile); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Connect this device to the specified profile. |
| 266 | * |
| 267 | * @param profile the profile to use with the remote device |
| 268 | */ |
| 269 | public void connectProfile(LocalBluetoothProfile profile) { |
| 270 | mConnectAttempted = SystemClock.elapsedRealtime(); |
| 271 | // Reset the only-show-one-error-dialog tracking variable |
| 272 | mIsConnectingErrorPossible = true; |
| 273 | connectInt(profile); |
| 274 | // Refresh the UI based on profile.connect() call |
| 275 | refresh(); |
| 276 | } |
| 277 | |
| 278 | synchronized void connectInt(LocalBluetoothProfile profile) { |
| 279 | if (!ensurePaired()) { |
| 280 | return; |
| 281 | } |
| 282 | if (profile.connect(mDevice)) { |
| 283 | if (Utils.D) { |
| 284 | Log.d(TAG, "Command sent successfully:CONNECT " + describe(profile)); |
| 285 | } |
| 286 | return; |
| 287 | } |
| 288 | Log.i(TAG, "Failed to connect " + profile.toString() + " to " + mName); |
| 289 | } |
| 290 | |
| 291 | private boolean ensurePaired() { |
| 292 | if (getBondState() == BluetoothDevice.BOND_NONE) { |
| 293 | startPairing(); |
| 294 | return false; |
| 295 | } else { |
| 296 | return true; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | public boolean startPairing() { |
| 301 | // Pairing is unreliable while scanning, so cancel discovery |
| 302 | if (mLocalAdapter.isDiscovering()) { |
| 303 | mLocalAdapter.cancelDiscovery(); |
| 304 | } |
| 305 | |
| 306 | if (!mDevice.createBond()) { |
| 307 | return false; |
| 308 | } |
| 309 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 310 | return true; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Return true if user initiated pairing on this device. The message text is |
| 315 | * slightly different for local vs. remote initiated pairing dialogs. |
| 316 | */ |
| 317 | boolean isUserInitiatedPairing() { |
Jakub Pawlowski | 0278ab9 | 2016-07-20 11:55:48 -0700 | [diff] [blame] | 318 | return mDevice.isBondingInitiatedLocally(); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | public void unpair() { |
| 322 | int state = getBondState(); |
| 323 | |
| 324 | if (state == BluetoothDevice.BOND_BONDING) { |
| 325 | mDevice.cancelBondProcess(); |
| 326 | } |
| 327 | |
| 328 | if (state != BluetoothDevice.BOND_NONE) { |
| 329 | final BluetoothDevice dev = mDevice; |
| 330 | if (dev != null) { |
| 331 | final boolean successful = dev.removeBond(); |
| 332 | if (successful) { |
| 333 | if (Utils.D) { |
| 334 | Log.d(TAG, "Command sent successfully:REMOVE_BOND " + describe(null)); |
| 335 | } |
| 336 | } else if (Utils.V) { |
| 337 | Log.v(TAG, "Framework rejected command immediately:REMOVE_BOND " + |
| 338 | describe(null)); |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | public int getProfileConnectionState(LocalBluetoothProfile profile) { |
xutianguo | 22bbb819 | 2016-06-22 11:32:00 +0800 | [diff] [blame] | 345 | if (mProfileConnectionState.get(profile) == null) { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 346 | // If cache is empty make the binder call to get the state |
| 347 | int state = profile.getConnectionStatus(mDevice); |
| 348 | mProfileConnectionState.put(profile, state); |
| 349 | } |
| 350 | return mProfileConnectionState.get(profile); |
| 351 | } |
| 352 | |
| 353 | public void clearProfileConnectionState () |
| 354 | { |
| 355 | if (Utils.D) { |
| 356 | Log.d(TAG," Clearing all connection state for dev:" + mDevice.getName()); |
| 357 | } |
| 358 | for (LocalBluetoothProfile profile :getProfiles()) { |
| 359 | mProfileConnectionState.put(profile, BluetoothProfile.STATE_DISCONNECTED); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // TODO: do any of these need to run async on a background thread? |
| 364 | private void fillData() { |
| 365 | fetchName(); |
| 366 | fetchBtClass(); |
| 367 | updateProfiles(); |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 368 | fetchActiveDevices(); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 369 | migratePhonebookPermissionChoice(); |
| 370 | migrateMessagePermissionChoice(); |
| 371 | fetchMessageRejectionCount(); |
| 372 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 373 | dispatchAttributesChanged(); |
| 374 | } |
| 375 | |
| 376 | public BluetoothDevice getDevice() { |
| 377 | return mDevice; |
| 378 | } |
| 379 | |
Antony Sargent | 7ad051e | 2017-06-29 15:23:13 -0700 | [diff] [blame] | 380 | /** |
| 381 | * Convenience method that can be mocked - it lets tests avoid having to call getDevice() which |
| 382 | * causes problems in tests since BluetoothDevice is final and cannot be mocked. |
| 383 | * @return the address of this device |
| 384 | */ |
| 385 | public String getAddress() { |
| 386 | return mDevice.getAddress(); |
| 387 | } |
| 388 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 389 | public String getName() { |
| 390 | return mName; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Populate name from BluetoothDevice.ACTION_FOUND intent |
| 395 | */ |
| 396 | void setNewName(String name) { |
| 397 | if (mName == null) { |
| 398 | mName = name; |
| 399 | if (mName == null || TextUtils.isEmpty(mName)) { |
| 400 | mName = mDevice.getAddress(); |
| 401 | } |
| 402 | dispatchAttributesChanged(); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | /** |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 407 | * User changes the device name |
| 408 | * @param name new alias name to be set, should never be null |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 409 | */ |
| 410 | public void setName(String name) { |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 411 | // Prevent mName to be set to null if setName(null) is called |
| 412 | if (name != null && !TextUtils.equals(name, mName)) { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 413 | mName = name; |
| 414 | mDevice.setAlias(name); |
| 415 | dispatchAttributesChanged(); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | void refreshName() { |
| 420 | fetchName(); |
| 421 | dispatchAttributesChanged(); |
| 422 | } |
| 423 | |
| 424 | private void fetchName() { |
| 425 | mName = mDevice.getAliasName(); |
| 426 | |
| 427 | if (TextUtils.isEmpty(mName)) { |
| 428 | mName = mDevice.getAddress(); |
| 429 | if (DEBUG) Log.d(TAG, "Device has no name (yet), use address: " + mName); |
| 430 | } |
| 431 | } |
| 432 | |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 433 | /** |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 434 | * Checks if device has a human readable name besides MAC address |
| 435 | * @return true if device's alias name is not null nor empty, false otherwise |
| 436 | */ |
| 437 | public boolean hasHumanReadableName() { |
| 438 | return !TextUtils.isEmpty(mDevice.getAliasName()); |
| 439 | } |
| 440 | |
| 441 | /** |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 442 | * Get battery level from remote device |
| 443 | * @return battery level in percentage [0-100], or {@link BluetoothDevice#BATTERY_LEVEL_UNKNOWN} |
| 444 | */ |
| 445 | public int getBatteryLevel() { |
| 446 | return mDevice.getBatteryLevel(); |
| 447 | } |
| 448 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 449 | void refresh() { |
| 450 | dispatchAttributesChanged(); |
| 451 | } |
| 452 | |
Jack He | 5152047 | 2017-07-24 12:30:08 -0700 | [diff] [blame] | 453 | public void setJustDiscovered(boolean justDiscovered) { |
| 454 | if (mJustDiscovered != justDiscovered) { |
| 455 | mJustDiscovered = justDiscovered; |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 456 | dispatchAttributesChanged(); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | public int getBondState() { |
| 461 | return mDevice.getBondState(); |
| 462 | } |
| 463 | |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 464 | /** |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame^] | 465 | * Update the device status as active or non-active per Bluetooth profile. |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 466 | * |
| 467 | * @param isActive true if the device is active |
| 468 | * @param bluetoothProfile the Bluetooth profile |
| 469 | */ |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame^] | 470 | public void onActiveDeviceChanged(boolean isActive, int bluetoothProfile) { |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 471 | boolean changed = false; |
| 472 | switch (bluetoothProfile) { |
| 473 | case BluetoothProfile.A2DP: |
| 474 | changed = (mIsActiveDeviceA2dp != isActive); |
| 475 | mIsActiveDeviceA2dp = isActive; |
| 476 | break; |
| 477 | case BluetoothProfile.HEADSET: |
| 478 | changed = (mIsActiveDeviceHeadset != isActive); |
| 479 | mIsActiveDeviceHeadset = isActive; |
| 480 | break; |
| 481 | default: |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame^] | 482 | Log.w(TAG, "onActiveDeviceChanged: unknown profile " + bluetoothProfile + |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 483 | " isActive " + isActive); |
| 484 | break; |
| 485 | } |
| 486 | if (changed) { |
| 487 | dispatchAttributesChanged(); |
| 488 | } |
| 489 | } |
| 490 | |
Pavlin Radoslavov | c285d55 | 2018-02-06 16:14:00 -0800 | [diff] [blame^] | 491 | /** |
| 492 | * Get the device status as active or non-active per Bluetooth profile. |
| 493 | * |
| 494 | * @param bluetoothProfile the Bluetooth profile |
| 495 | * @return true if the device is active |
| 496 | */ |
| 497 | @VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE) |
| 498 | public boolean isActiveDevice(int bluetoothProfile) { |
| 499 | switch (bluetoothProfile) { |
| 500 | case BluetoothProfile.A2DP: |
| 501 | return mIsActiveDeviceA2dp; |
| 502 | case BluetoothProfile.HEADSET: |
| 503 | return mIsActiveDeviceHeadset; |
| 504 | default: |
| 505 | Log.w(TAG, "getActiveDevice: unknown profile " + bluetoothProfile); |
| 506 | break; |
| 507 | } |
| 508 | return false; |
| 509 | } |
| 510 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 511 | void setRssi(short rssi) { |
| 512 | if (mRssi != rssi) { |
| 513 | mRssi = rssi; |
| 514 | dispatchAttributesChanged(); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Checks whether we are connected to this device (any profile counts). |
| 520 | * |
| 521 | * @return Whether it is connected. |
| 522 | */ |
| 523 | public boolean isConnected() { |
| 524 | for (LocalBluetoothProfile profile : mProfiles) { |
| 525 | int status = getProfileConnectionState(profile); |
| 526 | if (status == BluetoothProfile.STATE_CONNECTED) { |
| 527 | return true; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | return false; |
| 532 | } |
| 533 | |
| 534 | public boolean isConnectedProfile(LocalBluetoothProfile profile) { |
| 535 | int status = getProfileConnectionState(profile); |
| 536 | return status == BluetoothProfile.STATE_CONNECTED; |
| 537 | |
| 538 | } |
| 539 | |
| 540 | public boolean isBusy() { |
| 541 | for (LocalBluetoothProfile profile : mProfiles) { |
| 542 | int status = getProfileConnectionState(profile); |
| 543 | if (status == BluetoothProfile.STATE_CONNECTING |
| 544 | || status == BluetoothProfile.STATE_DISCONNECTING) { |
| 545 | return true; |
| 546 | } |
| 547 | } |
| 548 | return getBondState() == BluetoothDevice.BOND_BONDING; |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * Fetches a new value for the cached BT class. |
| 553 | */ |
| 554 | private void fetchBtClass() { |
| 555 | mBtClass = mDevice.getBluetoothClass(); |
| 556 | } |
| 557 | |
| 558 | private boolean updateProfiles() { |
| 559 | ParcelUuid[] uuids = mDevice.getUuids(); |
| 560 | if (uuids == null) return false; |
| 561 | |
| 562 | ParcelUuid[] localUuids = mLocalAdapter.getUuids(); |
| 563 | if (localUuids == null) return false; |
| 564 | |
Jack He | c219bc9 | 2017-07-24 14:55:59 -0700 | [diff] [blame] | 565 | /* |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 566 | * Now we know if the device supports PBAP, update permissions... |
| 567 | */ |
| 568 | processPhonebookAccess(); |
| 569 | |
| 570 | mProfileManager.updateProfiles(uuids, localUuids, mProfiles, mRemovedProfiles, |
| 571 | mLocalNapRoleConnected, mDevice); |
| 572 | |
| 573 | if (DEBUG) { |
| 574 | Log.e(TAG, "updating profiles for " + mDevice.getAliasName()); |
| 575 | BluetoothClass bluetoothClass = mDevice.getBluetoothClass(); |
| 576 | |
| 577 | if (bluetoothClass != null) Log.v(TAG, "Class: " + bluetoothClass.toString()); |
| 578 | Log.v(TAG, "UUID:"); |
| 579 | for (ParcelUuid uuid : uuids) { |
| 580 | Log.v(TAG, " " + uuid); |
| 581 | } |
| 582 | } |
| 583 | return true; |
| 584 | } |
| 585 | |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 586 | private void fetchActiveDevices() { |
| 587 | A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile(); |
| 588 | if (a2dpProfile != null) { |
| 589 | mIsActiveDeviceA2dp = mDevice.equals(a2dpProfile.getActiveDevice()); |
| 590 | } |
| 591 | HeadsetProfile headsetProfile = mProfileManager.getHeadsetProfile(); |
| 592 | if (headsetProfile != null) { |
| 593 | mIsActiveDeviceHeadset = mDevice.equals(headsetProfile.getActiveDevice()); |
| 594 | } |
| 595 | } |
| 596 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 597 | /** |
| 598 | * Refreshes the UI for the BT class, including fetching the latest value |
| 599 | * for the class. |
| 600 | */ |
| 601 | void refreshBtClass() { |
| 602 | fetchBtClass(); |
| 603 | dispatchAttributesChanged(); |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * Refreshes the UI when framework alerts us of a UUID change. |
| 608 | */ |
| 609 | void onUuidChanged() { |
| 610 | updateProfiles(); |
Etan Cohen | 50d4761 | 2015-03-31 12:45:23 -0700 | [diff] [blame] | 611 | ParcelUuid[] uuids = mDevice.getUuids(); |
Venkat Raghavan | 05e08c3 | 2015-04-06 16:26:11 -0700 | [diff] [blame] | 612 | |
Etan Cohen | 50d4761 | 2015-03-31 12:45:23 -0700 | [diff] [blame] | 613 | long timeout = MAX_UUID_DELAY_FOR_AUTO_CONNECT; |
Venkat Raghavan | 05e08c3 | 2015-04-06 16:26:11 -0700 | [diff] [blame] | 614 | if (BluetoothUuid.isUuidPresent(uuids, BluetoothUuid.Hogp)) { |
| 615 | timeout = MAX_HOGP_DELAY_FOR_AUTO_CONNECT; |
| 616 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 617 | |
| 618 | if (DEBUG) { |
Etan Cohen | 50d4761 | 2015-03-31 12:45:23 -0700 | [diff] [blame] | 619 | Log.d(TAG, "onUuidChanged: Time since last connect" |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 620 | + (SystemClock.elapsedRealtime() - mConnectAttempted)); |
| 621 | } |
| 622 | |
| 623 | /* |
Venkat Raghavan | 05e08c3 | 2015-04-06 16:26:11 -0700 | [diff] [blame] | 624 | * If a connect was attempted earlier without any UUID, we will do the connect now. |
| 625 | * Otherwise, allow the connect on UUID change. |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 626 | */ |
| 627 | if (!mProfiles.isEmpty() |
Andre Eisenbach | 7be83c5 | 2015-09-03 15:20:09 -0700 | [diff] [blame] | 628 | && ((mConnectAttempted + timeout) > SystemClock.elapsedRealtime())) { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 629 | connectWithoutResettingTimer(false); |
| 630 | } |
Andre Eisenbach | 7be83c5 | 2015-09-03 15:20:09 -0700 | [diff] [blame] | 631 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 632 | dispatchAttributesChanged(); |
| 633 | } |
| 634 | |
| 635 | void onBondingStateChanged(int bondState) { |
| 636 | if (bondState == BluetoothDevice.BOND_NONE) { |
| 637 | mProfiles.clear(); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 638 | setPhonebookPermissionChoice(ACCESS_UNKNOWN); |
| 639 | setMessagePermissionChoice(ACCESS_UNKNOWN); |
Casper Bonde | 424681e | 2015-05-04 22:07:45 -0700 | [diff] [blame] | 640 | setSimPermissionChoice(ACCESS_UNKNOWN); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 641 | mMessageRejectionCount = 0; |
| 642 | saveMessageRejectionCount(); |
| 643 | } |
| 644 | |
| 645 | refresh(); |
| 646 | |
| 647 | if (bondState == BluetoothDevice.BOND_BONDED) { |
| 648 | if (mDevice.isBluetoothDock()) { |
| 649 | onBondingDockConnect(); |
Jakub Pawlowski | 0278ab9 | 2016-07-20 11:55:48 -0700 | [diff] [blame] | 650 | } else if (mDevice.isBondingInitiatedLocally()) { |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 651 | connect(false); |
| 652 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
| 656 | void setBtClass(BluetoothClass btClass) { |
| 657 | if (btClass != null && mBtClass != btClass) { |
| 658 | mBtClass = btClass; |
| 659 | dispatchAttributesChanged(); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | public BluetoothClass getBtClass() { |
| 664 | return mBtClass; |
| 665 | } |
| 666 | |
| 667 | public List<LocalBluetoothProfile> getProfiles() { |
| 668 | return Collections.unmodifiableList(mProfiles); |
| 669 | } |
| 670 | |
| 671 | public List<LocalBluetoothProfile> getConnectableProfiles() { |
| 672 | List<LocalBluetoothProfile> connectableProfiles = |
| 673 | new ArrayList<LocalBluetoothProfile>(); |
| 674 | for (LocalBluetoothProfile profile : mProfiles) { |
| 675 | if (profile.isConnectable()) { |
| 676 | connectableProfiles.add(profile); |
| 677 | } |
| 678 | } |
| 679 | return connectableProfiles; |
| 680 | } |
| 681 | |
| 682 | public List<LocalBluetoothProfile> getRemovedProfiles() { |
| 683 | return mRemovedProfiles; |
| 684 | } |
| 685 | |
| 686 | public void registerCallback(Callback callback) { |
| 687 | synchronized (mCallbacks) { |
| 688 | mCallbacks.add(callback); |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | public void unregisterCallback(Callback callback) { |
| 693 | synchronized (mCallbacks) { |
| 694 | mCallbacks.remove(callback); |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | private void dispatchAttributesChanged() { |
| 699 | synchronized (mCallbacks) { |
| 700 | for (Callback callback : mCallbacks) { |
| 701 | callback.onDeviceAttributesChanged(); |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | @Override |
| 707 | public String toString() { |
| 708 | return mDevice.toString(); |
| 709 | } |
| 710 | |
| 711 | @Override |
| 712 | public boolean equals(Object o) { |
| 713 | if ((o == null) || !(o instanceof CachedBluetoothDevice)) { |
| 714 | return false; |
| 715 | } |
| 716 | return mDevice.equals(((CachedBluetoothDevice) o).mDevice); |
| 717 | } |
| 718 | |
| 719 | @Override |
| 720 | public int hashCode() { |
| 721 | return mDevice.getAddress().hashCode(); |
| 722 | } |
| 723 | |
| 724 | // This comparison uses non-final fields so the sort order may change |
| 725 | // when device attributes change (such as bonding state). Settings |
| 726 | // will completely refresh the device list when this happens. |
| 727 | public int compareTo(CachedBluetoothDevice another) { |
| 728 | // Connected above not connected |
| 729 | int comparison = (another.isConnected() ? 1 : 0) - (isConnected() ? 1 : 0); |
| 730 | if (comparison != 0) return comparison; |
| 731 | |
| 732 | // Paired above not paired |
| 733 | comparison = (another.getBondState() == BluetoothDevice.BOND_BONDED ? 1 : 0) - |
| 734 | (getBondState() == BluetoothDevice.BOND_BONDED ? 1 : 0); |
| 735 | if (comparison != 0) return comparison; |
| 736 | |
Jack He | 5152047 | 2017-07-24 12:30:08 -0700 | [diff] [blame] | 737 | // Just discovered above discovered in the past |
| 738 | comparison = (another.mJustDiscovered ? 1 : 0) - (mJustDiscovered ? 1 : 0); |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 739 | if (comparison != 0) return comparison; |
| 740 | |
| 741 | // Stronger signal above weaker signal |
| 742 | comparison = another.mRssi - mRssi; |
| 743 | if (comparison != 0) return comparison; |
| 744 | |
| 745 | // Fallback on name |
| 746 | return mName.compareTo(another.mName); |
| 747 | } |
| 748 | |
| 749 | public interface Callback { |
| 750 | void onDeviceAttributesChanged(); |
| 751 | } |
| 752 | |
| 753 | public int getPhonebookPermissionChoice() { |
| 754 | int permission = mDevice.getPhonebookAccessPermission(); |
| 755 | if (permission == BluetoothDevice.ACCESS_ALLOWED) { |
| 756 | return ACCESS_ALLOWED; |
| 757 | } else if (permission == BluetoothDevice.ACCESS_REJECTED) { |
| 758 | return ACCESS_REJECTED; |
| 759 | } |
| 760 | return ACCESS_UNKNOWN; |
| 761 | } |
| 762 | |
| 763 | public void setPhonebookPermissionChoice(int permissionChoice) { |
| 764 | int permission = BluetoothDevice.ACCESS_UNKNOWN; |
| 765 | if (permissionChoice == ACCESS_ALLOWED) { |
| 766 | permission = BluetoothDevice.ACCESS_ALLOWED; |
| 767 | } else if (permissionChoice == ACCESS_REJECTED) { |
| 768 | permission = BluetoothDevice.ACCESS_REJECTED; |
| 769 | } |
| 770 | mDevice.setPhonebookAccessPermission(permission); |
| 771 | } |
| 772 | |
| 773 | // Migrates data from old data store (in Settings app's shared preferences) to new (in Bluetooth |
| 774 | // app's shared preferences). |
| 775 | private void migratePhonebookPermissionChoice() { |
| 776 | SharedPreferences preferences = mContext.getSharedPreferences( |
| 777 | "bluetooth_phonebook_permission", Context.MODE_PRIVATE); |
| 778 | if (!preferences.contains(mDevice.getAddress())) { |
| 779 | return; |
| 780 | } |
| 781 | |
| 782 | if (mDevice.getPhonebookAccessPermission() == BluetoothDevice.ACCESS_UNKNOWN) { |
| 783 | int oldPermission = preferences.getInt(mDevice.getAddress(), ACCESS_UNKNOWN); |
| 784 | if (oldPermission == ACCESS_ALLOWED) { |
| 785 | mDevice.setPhonebookAccessPermission(BluetoothDevice.ACCESS_ALLOWED); |
| 786 | } else if (oldPermission == ACCESS_REJECTED) { |
| 787 | mDevice.setPhonebookAccessPermission(BluetoothDevice.ACCESS_REJECTED); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | SharedPreferences.Editor editor = preferences.edit(); |
| 792 | editor.remove(mDevice.getAddress()); |
| 793 | editor.commit(); |
| 794 | } |
| 795 | |
| 796 | public int getMessagePermissionChoice() { |
| 797 | int permission = mDevice.getMessageAccessPermission(); |
| 798 | if (permission == BluetoothDevice.ACCESS_ALLOWED) { |
| 799 | return ACCESS_ALLOWED; |
| 800 | } else if (permission == BluetoothDevice.ACCESS_REJECTED) { |
| 801 | return ACCESS_REJECTED; |
| 802 | } |
| 803 | return ACCESS_UNKNOWN; |
| 804 | } |
| 805 | |
| 806 | public void setMessagePermissionChoice(int permissionChoice) { |
| 807 | int permission = BluetoothDevice.ACCESS_UNKNOWN; |
| 808 | if (permissionChoice == ACCESS_ALLOWED) { |
| 809 | permission = BluetoothDevice.ACCESS_ALLOWED; |
| 810 | } else if (permissionChoice == ACCESS_REJECTED) { |
| 811 | permission = BluetoothDevice.ACCESS_REJECTED; |
| 812 | } |
| 813 | mDevice.setMessageAccessPermission(permission); |
| 814 | } |
| 815 | |
Casper Bonde | 424681e | 2015-05-04 22:07:45 -0700 | [diff] [blame] | 816 | public int getSimPermissionChoice() { |
| 817 | int permission = mDevice.getSimAccessPermission(); |
| 818 | if (permission == BluetoothDevice.ACCESS_ALLOWED) { |
| 819 | return ACCESS_ALLOWED; |
| 820 | } else if (permission == BluetoothDevice.ACCESS_REJECTED) { |
| 821 | return ACCESS_REJECTED; |
| 822 | } |
| 823 | return ACCESS_UNKNOWN; |
| 824 | } |
| 825 | |
| 826 | void setSimPermissionChoice(int permissionChoice) { |
| 827 | int permission = BluetoothDevice.ACCESS_UNKNOWN; |
| 828 | if (permissionChoice == ACCESS_ALLOWED) { |
| 829 | permission = BluetoothDevice.ACCESS_ALLOWED; |
| 830 | } else if (permissionChoice == ACCESS_REJECTED) { |
| 831 | permission = BluetoothDevice.ACCESS_REJECTED; |
| 832 | } |
| 833 | mDevice.setSimAccessPermission(permission); |
| 834 | } |
| 835 | |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 836 | // Migrates data from old data store (in Settings app's shared preferences) to new (in Bluetooth |
| 837 | // app's shared preferences). |
| 838 | private void migrateMessagePermissionChoice() { |
| 839 | SharedPreferences preferences = mContext.getSharedPreferences( |
| 840 | "bluetooth_message_permission", Context.MODE_PRIVATE); |
| 841 | if (!preferences.contains(mDevice.getAddress())) { |
| 842 | return; |
| 843 | } |
| 844 | |
| 845 | if (mDevice.getMessageAccessPermission() == BluetoothDevice.ACCESS_UNKNOWN) { |
| 846 | int oldPermission = preferences.getInt(mDevice.getAddress(), ACCESS_UNKNOWN); |
| 847 | if (oldPermission == ACCESS_ALLOWED) { |
| 848 | mDevice.setMessageAccessPermission(BluetoothDevice.ACCESS_ALLOWED); |
| 849 | } else if (oldPermission == ACCESS_REJECTED) { |
| 850 | mDevice.setMessageAccessPermission(BluetoothDevice.ACCESS_REJECTED); |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | SharedPreferences.Editor editor = preferences.edit(); |
| 855 | editor.remove(mDevice.getAddress()); |
| 856 | editor.commit(); |
| 857 | } |
| 858 | |
| 859 | /** |
| 860 | * @return Whether this rejection should persist. |
| 861 | */ |
| 862 | public boolean checkAndIncreaseMessageRejectionCount() { |
| 863 | if (mMessageRejectionCount < MESSAGE_REJECTION_COUNT_LIMIT_TO_PERSIST) { |
| 864 | mMessageRejectionCount++; |
| 865 | saveMessageRejectionCount(); |
| 866 | } |
| 867 | return mMessageRejectionCount >= MESSAGE_REJECTION_COUNT_LIMIT_TO_PERSIST; |
| 868 | } |
| 869 | |
| 870 | private void fetchMessageRejectionCount() { |
| 871 | SharedPreferences preference = mContext.getSharedPreferences( |
| 872 | MESSAGE_REJECTION_COUNT_PREFS_NAME, Context.MODE_PRIVATE); |
| 873 | mMessageRejectionCount = preference.getInt(mDevice.getAddress(), 0); |
| 874 | } |
| 875 | |
| 876 | private void saveMessageRejectionCount() { |
| 877 | SharedPreferences.Editor editor = mContext.getSharedPreferences( |
| 878 | MESSAGE_REJECTION_COUNT_PREFS_NAME, Context.MODE_PRIVATE).edit(); |
| 879 | if (mMessageRejectionCount == 0) { |
| 880 | editor.remove(mDevice.getAddress()); |
| 881 | } else { |
| 882 | editor.putInt(mDevice.getAddress(), mMessageRejectionCount); |
| 883 | } |
| 884 | editor.commit(); |
| 885 | } |
| 886 | |
| 887 | private void processPhonebookAccess() { |
| 888 | if (mDevice.getBondState() != BluetoothDevice.BOND_BONDED) return; |
| 889 | |
| 890 | ParcelUuid[] uuids = mDevice.getUuids(); |
| 891 | if (BluetoothUuid.containsAnyUuid(uuids, PbapServerProfile.PBAB_CLIENT_UUIDS)) { |
| 892 | // The pairing dialog now warns of phone-book access for paired devices. |
| 893 | // No separate prompt is displayed after pairing. |
Sanket Padawe | 78c2376 | 2015-06-01 15:55:30 -0700 | [diff] [blame] | 894 | if (getPhonebookPermissionChoice() == CachedBluetoothDevice.ACCESS_UNKNOWN) { |
Sanket Padawe | 07533db | 2015-11-11 15:01:35 -0800 | [diff] [blame] | 895 | if (mDevice.getBluetoothClass().getDeviceClass() |
Hemant Gupta | b8d267a | 2016-06-07 12:53:59 +0530 | [diff] [blame] | 896 | == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE || |
| 897 | mDevice.getBluetoothClass().getDeviceClass() |
| 898 | == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET) { |
Sanket Padawe | 07533db | 2015-11-11 15:01:35 -0800 | [diff] [blame] | 899 | setPhonebookPermissionChoice(CachedBluetoothDevice.ACCESS_ALLOWED); |
| 900 | } else { |
| 901 | setPhonebookPermissionChoice(CachedBluetoothDevice.ACCESS_REJECTED); |
| 902 | } |
Sanket Padawe | 78c2376 | 2015-06-01 15:55:30 -0700 | [diff] [blame] | 903 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 904 | } |
| 905 | } |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 906 | |
| 907 | public int getMaxConnectionState() { |
| 908 | int maxState = BluetoothProfile.STATE_DISCONNECTED; |
| 909 | for (LocalBluetoothProfile profile : getProfiles()) { |
| 910 | int connectionStatus = getProfileConnectionState(profile); |
| 911 | if (connectionStatus > maxState) { |
| 912 | maxState = connectionStatus; |
| 913 | } |
| 914 | } |
| 915 | return maxState; |
| 916 | } |
| 917 | |
| 918 | /** |
| 919 | * @return resource for string that discribes the connection state of this device. |
| 920 | */ |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 921 | public String getConnectionSummary() { |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 922 | boolean profileConnected = false; // at least one profile is connected |
| 923 | boolean a2dpNotConnected = false; // A2DP is preferred but not connected |
Sanket Agarwal | f8a1c91 | 2016-01-26 20:12:52 -0800 | [diff] [blame] | 924 | boolean hfpNotConnected = false; // HFP is preferred but not connected |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 925 | |
| 926 | for (LocalBluetoothProfile profile : getProfiles()) { |
| 927 | int connectionStatus = getProfileConnectionState(profile); |
| 928 | |
| 929 | switch (connectionStatus) { |
| 930 | case BluetoothProfile.STATE_CONNECTING: |
| 931 | case BluetoothProfile.STATE_DISCONNECTING: |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 932 | return mContext.getString(Utils.getConnectionStateSummary(connectionStatus)); |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 933 | |
| 934 | case BluetoothProfile.STATE_CONNECTED: |
| 935 | profileConnected = true; |
| 936 | break; |
| 937 | |
| 938 | case BluetoothProfile.STATE_DISCONNECTED: |
| 939 | if (profile.isProfileReady()) { |
Sanket Agarwal | f8a1c91 | 2016-01-26 20:12:52 -0800 | [diff] [blame] | 940 | if ((profile instanceof A2dpProfile) || |
Sanket Agarwal | 1bec6a5 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 941 | (profile instanceof A2dpSinkProfile)){ |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 942 | a2dpNotConnected = true; |
Sanket Agarwal | f8a1c91 | 2016-01-26 20:12:52 -0800 | [diff] [blame] | 943 | } else if ((profile instanceof HeadsetProfile) || |
| 944 | (profile instanceof HfpClientProfile)) { |
| 945 | hfpNotConnected = true; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 946 | } |
| 947 | } |
| 948 | break; |
| 949 | } |
| 950 | } |
| 951 | |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 952 | String batteryLevelPercentageString = null; |
| 953 | // Android framework should only set mBatteryLevel to valid range [0-100] or |
| 954 | // BluetoothDevice.BATTERY_LEVEL_UNKNOWN, any other value should be a framework bug. |
| 955 | // Thus assume here that if value is not BluetoothDevice.BATTERY_LEVEL_UNKNOWN, it must |
| 956 | // be valid |
| 957 | final int batteryLevel = getBatteryLevel(); |
| 958 | if (batteryLevel != BluetoothDevice.BATTERY_LEVEL_UNKNOWN) { |
| 959 | // TODO: name com.android.settingslib.bluetooth.Utils something different |
| 960 | batteryLevelPercentageString = |
| 961 | com.android.settingslib.Utils.formatPercentage(batteryLevel); |
| 962 | } |
| 963 | |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 964 | // Prepare the string for the Active Device summary |
| 965 | String[] activeDeviceStringsArray = mContext.getResources().getStringArray( |
| 966 | R.array.bluetooth_audio_active_device_summaries); |
| 967 | String activeDeviceString = activeDeviceStringsArray[0]; // Default value: not active |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 968 | if (mIsActiveDeviceA2dp && mIsActiveDeviceHeadset) { |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 969 | activeDeviceString = activeDeviceStringsArray[1]; // Active for Media and Phone |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 970 | } else { |
| 971 | if (mIsActiveDeviceA2dp) { |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 972 | activeDeviceString = activeDeviceStringsArray[2]; // Active for Media only |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 973 | } |
| 974 | if (mIsActiveDeviceHeadset) { |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 975 | activeDeviceString = activeDeviceStringsArray[3]; // Active for Phone only |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 976 | } |
| 977 | } |
Pavlin Radoslavov | 1af33a1 | 2018-01-21 02:59:15 -0800 | [diff] [blame] | 978 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 979 | if (profileConnected) { |
Sanket Agarwal | f8a1c91 | 2016-01-26 20:12:52 -0800 | [diff] [blame] | 980 | if (a2dpNotConnected && hfpNotConnected) { |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 981 | if (batteryLevelPercentageString != null) { |
| 982 | return mContext.getString( |
| 983 | R.string.bluetooth_connected_no_headset_no_a2dp_battery_level, |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 984 | batteryLevelPercentageString, activeDeviceString); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 985 | } else { |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 986 | return mContext.getString(R.string.bluetooth_connected_no_headset_no_a2dp, |
| 987 | activeDeviceString); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 988 | } |
| 989 | |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 990 | } else if (a2dpNotConnected) { |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 991 | if (batteryLevelPercentageString != null) { |
| 992 | return mContext.getString(R.string.bluetooth_connected_no_a2dp_battery_level, |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 993 | batteryLevelPercentageString, activeDeviceString); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 994 | } else { |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 995 | return mContext.getString(R.string.bluetooth_connected_no_a2dp, |
| 996 | activeDeviceString); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 997 | } |
| 998 | |
Sanket Agarwal | f8a1c91 | 2016-01-26 20:12:52 -0800 | [diff] [blame] | 999 | } else if (hfpNotConnected) { |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 1000 | if (batteryLevelPercentageString != null) { |
| 1001 | return mContext.getString(R.string.bluetooth_connected_no_headset_battery_level, |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 1002 | batteryLevelPercentageString, activeDeviceString); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 1003 | } else { |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 1004 | return mContext.getString(R.string.bluetooth_connected_no_headset, |
| 1005 | activeDeviceString); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 1006 | } |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 1007 | } else { |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 1008 | if (batteryLevelPercentageString != null) { |
| 1009 | return mContext.getString(R.string.bluetooth_connected_battery_level, |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 1010 | batteryLevelPercentageString, activeDeviceString); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 1011 | } else { |
Pavlin Radoslavov | e6e080f | 2018-02-06 12:21:34 -0800 | [diff] [blame] | 1012 | return mContext.getString(R.string.bluetooth_connected, activeDeviceString); |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 1013 | } |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 1014 | } |
| 1015 | } |
| 1016 | |
Jack He | 6258aae | 2017-06-29 17:01:23 -0700 | [diff] [blame] | 1017 | return getBondState() == BluetoothDevice.BOND_BONDING ? |
| 1018 | mContext.getString(R.string.bluetooth_pairing) : null; |
Jason Monk | be3c5db | 2015-02-04 13:00:55 -0500 | [diff] [blame] | 1019 | } |
Jason Monk | 7ce96b9 | 2015-02-02 11:27:58 -0500 | [diff] [blame] | 1020 | } |