Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.settings.bluetooth; |
| 17 | |
| 18 | import android.bluetooth.BluetoothClass; |
| 19 | import android.bluetooth.BluetoothDevice; |
SongFerngWang | 51cabc5 | 2022-09-23 21:21:25 +0800 | [diff] [blame] | 20 | import android.bluetooth.BluetoothProfile; |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
SongFerngWang | 51cabc5 | 2022-09-23 21:21:25 +0800 | [diff] [blame] | 23 | import android.provider.DeviceConfig; |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 24 | import android.text.Editable; |
| 25 | import android.util.Log; |
| 26 | import android.widget.CompoundButton; |
| 27 | import android.widget.CompoundButton.OnCheckedChangeListener; |
jackqdyulei | 7015e20 | 2018-06-19 16:52:07 -0700 | [diff] [blame] | 28 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 29 | import androidx.annotation.VisibleForTesting; |
| 30 | |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 31 | import com.android.settings.R; |
| 32 | import com.android.settings.bluetooth.BluetoothPairingDialogFragment.BluetoothPairingDialogListener; |
SongFerngWang | 51cabc5 | 2022-09-23 21:21:25 +0800 | [diff] [blame] | 33 | import com.android.settings.core.SettingsUIDeviceConfig; |
Shen Lin | bdcface | 2023-01-03 14:16:58 +0800 | [diff] [blame] | 34 | import com.android.settingslib.bluetooth.BluetoothUtils; |
Alice Kuo | f57f720 | 2021-01-29 15:37:39 +0800 | [diff] [blame] | 35 | import com.android.settingslib.bluetooth.CachedBluetoothDevice; |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 36 | import com.android.settingslib.bluetooth.LocalBluetoothManager; |
| 37 | import com.android.settingslib.bluetooth.LocalBluetoothProfile; |
jackqdyulei | 7015e20 | 2018-06-19 16:52:07 -0700 | [diff] [blame] | 38 | |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 39 | import java.util.Locale; |
| 40 | |
| 41 | /** |
| 42 | * A controller used by {@link BluetoothPairingDialog} to manage connection state while we try to |
| 43 | * pair with a bluetooth device. It includes methods that allow the |
| 44 | * {@link BluetoothPairingDialogFragment} to interrogate the current state as well. |
| 45 | */ |
| 46 | public class BluetoothPairingController implements OnCheckedChangeListener, |
| 47 | BluetoothPairingDialogListener { |
| 48 | |
| 49 | private static final String TAG = "BTPairingController"; |
| 50 | |
| 51 | // Different types of dialogs we can map to |
| 52 | public static final int INVALID_DIALOG_TYPE = -1; |
| 53 | public static final int USER_ENTRY_DIALOG = 0; |
| 54 | public static final int CONFIRMATION_DIALOG = 1; |
| 55 | public static final int DISPLAY_PASSKEY_DIALOG = 2; |
| 56 | |
| 57 | private static final int BLUETOOTH_PIN_MAX_LENGTH = 16; |
| 58 | private static final int BLUETOOTH_PASSKEY_MAX_LENGTH = 6; |
| 59 | |
| 60 | // Bluetooth dependencies for the connection we are trying to establish |
SongFerngWang | 51cabc5 | 2022-09-23 21:21:25 +0800 | [diff] [blame] | 61 | LocalBluetoothManager mBluetoothManager; |
timhypeng | eb230c7 | 2018-09-19 15:54:13 +0800 | [diff] [blame] | 62 | private BluetoothDevice mDevice; |
jackqdyulei | 7015e20 | 2018-06-19 16:52:07 -0700 | [diff] [blame] | 63 | @VisibleForTesting |
| 64 | int mType; |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 65 | private String mUserInput; |
| 66 | private String mPasskeyFormatted; |
| 67 | private int mPasskey; |
Myles Watson | 0d31c37 | 2020-06-08 15:07:23 -0700 | [diff] [blame] | 68 | private int mInitiator; |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 69 | private String mDeviceName; |
| 70 | private LocalBluetoothProfile mPbapClientProfile; |
Hansong Zhang | 500c83c | 2018-05-17 13:54:00 -0700 | [diff] [blame] | 71 | private boolean mPbapAllowed; |
Alice Kuo | f57f720 | 2021-01-29 15:37:39 +0800 | [diff] [blame] | 72 | private boolean mIsCoordinatedSetMember; |
SongFerngWang | 51cabc5 | 2022-09-23 21:21:25 +0800 | [diff] [blame] | 73 | private boolean mIsLeAudio; |
| 74 | private boolean mIsLeContactSharingEnabled; |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Creates an instance of a BluetoothPairingController. |
| 78 | * |
| 79 | * @param intent - must contain {@link BluetoothDevice#EXTRA_PAIRING_VARIANT}, {@link |
| 80 | * BluetoothDevice#EXTRA_PAIRING_KEY}, and {@link BluetoothDevice#EXTRA_DEVICE}. Missing extra |
| 81 | * will lead to undefined behavior. |
| 82 | */ |
| 83 | public BluetoothPairingController(Intent intent, Context context) { |
| 84 | mBluetoothManager = Utils.getLocalBtManager(context); |
| 85 | mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); |
| 86 | |
| 87 | String message = ""; |
| 88 | if (mBluetoothManager == null) { |
| 89 | throw new IllegalStateException("Could not obtain LocalBluetoothManager"); |
| 90 | } else if (mDevice == null) { |
| 91 | throw new IllegalStateException("Could not find BluetoothDevice"); |
| 92 | } |
| 93 | |
| 94 | mType = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR); |
| 95 | mPasskey = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY, BluetoothDevice.ERROR); |
Myles Watson | 0d31c37 | 2020-06-08 15:07:23 -0700 | [diff] [blame] | 96 | mInitiator = |
| 97 | intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_INITIATOR, BluetoothDevice.ERROR); |
Lei Yu | 499013b | 2018-09-17 18:22:57 +0000 | [diff] [blame] | 98 | mDeviceName = mBluetoothManager.getCachedDeviceManager().getName(mDevice); |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 99 | mPbapClientProfile = mBluetoothManager.getProfileManager().getPbapClientProfile(); |
| 100 | mPasskeyFormatted = formatKey(mPasskey); |
SongFerngWang | 51cabc5 | 2022-09-23 21:21:25 +0800 | [diff] [blame] | 101 | |
Alice Kuo | f57f720 | 2021-01-29 15:37:39 +0800 | [diff] [blame] | 102 | final CachedBluetoothDevice cachedDevice = |
| 103 | mBluetoothManager.getCachedDeviceManager().findDevice(mDevice); |
SongFerngWang | 51cabc5 | 2022-09-23 21:21:25 +0800 | [diff] [blame] | 104 | |
| 105 | mIsCoordinatedSetMember = false; |
| 106 | mIsLeAudio = false; |
| 107 | mIsLeContactSharingEnabled = true; |
| 108 | if (cachedDevice != null) { |
| 109 | mIsCoordinatedSetMember = cachedDevice.isCoordinatedSetMemberDevice(); |
| 110 | |
| 111 | for (LocalBluetoothProfile profile : cachedDevice.getProfiles()) { |
| 112 | if (profile.getProfileId() == BluetoothProfile.LE_AUDIO) { |
| 113 | mIsLeAudio = true; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | mIsLeContactSharingEnabled = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_SETTINGS_UI, |
| 118 | SettingsUIDeviceConfig.BT_LE_AUDIO_CONTACT_SHARING_ENABLED, true); |
| 119 | Log.d(TAG, "BT_LE_AUDIO_CONTACT_SHARING_ENABLED is " + mIsLeContactSharingEnabled); |
| 120 | } |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 125 | if (isChecked) { |
Hansong Zhang | 500c83c | 2018-05-17 13:54:00 -0700 | [diff] [blame] | 126 | mPbapAllowed = true; |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 127 | } else { |
Hansong Zhang | 500c83c | 2018-05-17 13:54:00 -0700 | [diff] [blame] | 128 | mPbapAllowed = false; |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void onDialogPositiveClick(BluetoothPairingDialogFragment dialog) { |
jackqdyulei | 7015e20 | 2018-06-19 16:52:07 -0700 | [diff] [blame] | 134 | if (mPbapAllowed) { |
| 135 | mDevice.setPhonebookAccessPermission(BluetoothDevice.ACCESS_ALLOWED); |
| 136 | } else { |
| 137 | mDevice.setPhonebookAccessPermission(BluetoothDevice.ACCESS_REJECTED); |
| 138 | } |
| 139 | |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 140 | if (getDialogType() == USER_ENTRY_DIALOG) { |
| 141 | onPair(mUserInput); |
| 142 | } else { |
| 143 | onPair(null); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void onDialogNegativeClick(BluetoothPairingDialogFragment dialog) { |
Hansong Zhang | 500c83c | 2018-05-17 13:54:00 -0700 | [diff] [blame] | 149 | mDevice.setPhonebookAccessPermission(BluetoothDevice.ACCESS_REJECTED); |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 150 | onCancel(); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * A method for querying which bluetooth pairing dialog fragment variant this device requires. |
| 155 | * |
| 156 | * @return - The dialog view variant needed for this device. |
| 157 | */ |
| 158 | public int getDialogType() { |
| 159 | switch (mType) { |
| 160 | case BluetoothDevice.PAIRING_VARIANT_PIN: |
| 161 | case BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS: |
| 162 | case BluetoothDevice.PAIRING_VARIANT_PASSKEY: |
| 163 | return USER_ENTRY_DIALOG; |
| 164 | |
| 165 | case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION: |
| 166 | case BluetoothDevice.PAIRING_VARIANT_CONSENT: |
| 167 | case BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT: |
| 168 | return CONFIRMATION_DIALOG; |
| 169 | |
| 170 | case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY: |
| 171 | case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN: |
| 172 | return DISPLAY_PASSKEY_DIALOG; |
| 173 | |
| 174 | default: |
| 175 | return INVALID_DIALOG_TYPE; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * @return - A string containing the name provided by the device. |
| 181 | */ |
| 182 | public String getDeviceName() { |
| 183 | return mDeviceName; |
| 184 | } |
| 185 | |
| 186 | /** |
Alice Kuo | f57f720 | 2021-01-29 15:37:39 +0800 | [diff] [blame] | 187 | * A method for querying if the bluetooth device is a LE coordinated set member device. |
| 188 | * |
| 189 | * @return - A boolean indicating if the device is a CSIP supported device. |
| 190 | */ |
| 191 | public boolean isCoordinatedSetMemberDevice() { |
| 192 | return mIsCoordinatedSetMember; |
| 193 | } |
| 194 | |
| 195 | /** |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 196 | * A method for querying if the bluetooth device has a profile already set up on this device. |
| 197 | * |
| 198 | * @return - A boolean indicating if the device has previous knowledge of a profile for this |
| 199 | * device. |
| 200 | */ |
| 201 | public boolean isProfileReady() { |
| 202 | return mPbapClientProfile != null && mPbapClientProfile.isProfileReady(); |
| 203 | } |
| 204 | |
SongFerngWang | 51cabc5 | 2022-09-23 21:21:25 +0800 | [diff] [blame] | 205 | @VisibleForTesting |
| 206 | boolean isLeAudio() { |
| 207 | return mIsLeAudio; |
| 208 | } |
| 209 | |
| 210 | @VisibleForTesting |
| 211 | boolean isLeContactSharingEnabled() { |
| 212 | return mIsLeContactSharingEnabled; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * A method whether the device allows to show the le audio's contact sharing. |
| 217 | * |
| 218 | * @return A boolean whether the device allows to show the contact sharing. |
| 219 | */ |
| 220 | public boolean isContactSharingVisible() { |
| 221 | boolean isContactSharingVisible = !isProfileReady(); |
| 222 | // If device do not support the ContactSharing of LE audio device, hiding ContactSharing UI |
| 223 | if (isLeAudio() && !isLeContactSharingEnabled()) { |
| 224 | isContactSharingVisible = false; |
| 225 | } |
| 226 | return isContactSharingVisible; |
| 227 | } |
| 228 | |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 229 | /** |
| 230 | * A method for querying if the bluetooth device has access to contacts on the device. |
| 231 | * |
| 232 | * @return - A boolean indicating if the bluetooth device has permission to access the device |
| 233 | * contacts |
| 234 | */ |
| 235 | public boolean getContactSharingState() { |
| 236 | switch (mDevice.getPhonebookAccessPermission()) { |
| 237 | case BluetoothDevice.ACCESS_ALLOWED: |
| 238 | return true; |
| 239 | case BluetoothDevice.ACCESS_REJECTED: |
| 240 | return false; |
| 241 | default: |
Shen Lin | bdcface | 2023-01-03 14:16:58 +0800 | [diff] [blame] | 242 | if (BluetoothUtils.isDeviceClassMatched( |
| 243 | mDevice, BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE)) { |
Myles Watson | 0d31c37 | 2020-06-08 15:07:23 -0700 | [diff] [blame] | 244 | return BluetoothDevice.EXTRA_PAIRING_INITIATOR_FOREGROUND == mInitiator; |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 245 | } |
| 246 | return false; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /** |
Hemant Gupta | 5b84cfa | 2017-09-22 12:24:14 +0530 | [diff] [blame] | 251 | * Update Phone book permission |
| 252 | * |
| 253 | */ |
Shen Lin | bdcface | 2023-01-03 14:16:58 +0800 | [diff] [blame] | 254 | public void setContactSharingState() { |
timhypeng | eb230c7 | 2018-09-19 15:54:13 +0800 | [diff] [blame] | 255 | final int permission = mDevice.getPhonebookAccessPermission(); |
| 256 | if (permission == BluetoothDevice.ACCESS_ALLOWED |
Shen Lin | bdcface | 2023-01-03 14:16:58 +0800 | [diff] [blame] | 257 | || (permission == BluetoothDevice.ACCESS_UNKNOWN |
| 258 | && BluetoothUtils.isDeviceClassMatched(mDevice, |
| 259 | BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE))) { |
timhypeng | eb230c7 | 2018-09-19 15:54:13 +0800 | [diff] [blame] | 260 | onCheckedChanged(null, true); |
| 261 | } else { |
| 262 | onCheckedChanged(null, false); |
| 263 | } |
| 264 | |
| 265 | } |
Hemant Gupta | 5b84cfa | 2017-09-22 12:24:14 +0530 | [diff] [blame] | 266 | |
| 267 | /** |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 268 | * A method for querying if the provided editable is a valid passkey/pin format for this device. |
| 269 | * |
| 270 | * @param s - The passkey/pin |
| 271 | * @return - A boolean indicating if the passkey/pin is of the correct format. |
| 272 | */ |
| 273 | public boolean isPasskeyValid(Editable s) { |
| 274 | boolean requires16Digits = mType == BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS; |
| 275 | return s.length() >= 16 && requires16Digits || s.length() > 0 && !requires16Digits; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * A method for querying what message should be shown to the user as additional text in the |
| 280 | * dialog for this device. Returns -1 to indicate a device type that does not use this message. |
| 281 | * |
| 282 | * @return - The message ID to show the user. |
| 283 | */ |
Salvador Martinez | f492c28 | 2016-10-24 15:51:58 -0700 | [diff] [blame] | 284 | public int getDeviceVariantMessageId() { |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 285 | switch (mType) { |
| 286 | case BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS: |
| 287 | case BluetoothDevice.PAIRING_VARIANT_PIN: |
| 288 | return R.string.bluetooth_enter_pin_other_device; |
| 289 | |
| 290 | case BluetoothDevice.PAIRING_VARIANT_PASSKEY: |
| 291 | return R.string.bluetooth_enter_passkey_other_device; |
| 292 | |
| 293 | default: |
Salvador Martinez | f492c28 | 2016-10-24 15:51:58 -0700 | [diff] [blame] | 294 | return INVALID_DIALOG_TYPE; |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * A method for querying what message hint should be shown to the user as additional text in the |
| 300 | * dialog for this device. Returns -1 to indicate a device type that does not use this message. |
| 301 | * |
| 302 | * @return - The message ID to show the user. |
| 303 | */ |
Salvador Martinez | f492c28 | 2016-10-24 15:51:58 -0700 | [diff] [blame] | 304 | public int getDeviceVariantMessageHintId() { |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 305 | switch (mType) { |
| 306 | case BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS: |
| 307 | return R.string.bluetooth_pin_values_hint_16_digits; |
| 308 | |
| 309 | case BluetoothDevice.PAIRING_VARIANT_PIN: |
| 310 | case BluetoothDevice.PAIRING_VARIANT_PASSKEY: |
| 311 | return R.string.bluetooth_pin_values_hint; |
| 312 | |
| 313 | default: |
Salvador Martinez | f492c28 | 2016-10-24 15:51:58 -0700 | [diff] [blame] | 314 | return INVALID_DIALOG_TYPE; |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * A method for querying the maximum passkey/pin length for this device. |
| 320 | * |
| 321 | * @return - An int indicating the maximum length |
| 322 | */ |
| 323 | public int getDeviceMaxPasskeyLength() { |
| 324 | switch (mType) { |
| 325 | case BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS: |
| 326 | case BluetoothDevice.PAIRING_VARIANT_PIN: |
| 327 | return BLUETOOTH_PIN_MAX_LENGTH; |
| 328 | |
| 329 | case BluetoothDevice.PAIRING_VARIANT_PASSKEY: |
| 330 | return BLUETOOTH_PASSKEY_MAX_LENGTH; |
| 331 | |
| 332 | default: |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * A method for querying if the device uses an alphanumeric passkey. |
| 340 | * |
| 341 | * @return - a boolean indicating if the passkey can be alphanumeric. |
| 342 | */ |
| 343 | public boolean pairingCodeIsAlphanumeric() { |
| 344 | switch (mType) { |
| 345 | case BluetoothDevice.PAIRING_VARIANT_PASSKEY: |
| 346 | return false; |
| 347 | |
| 348 | default: |
| 349 | return true; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * A method used by the dialogfragment to notify the controller that the dialog has been |
| 355 | * displayed for bluetooth device types that just care about it being displayed. |
| 356 | */ |
| 357 | protected void notifyDialogDisplayed() { |
| 358 | // send an OK to the framework, indicating that the dialog has been displayed. |
| 359 | if (mType == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY) { |
| 360 | mDevice.setPairingConfirmation(true); |
| 361 | } else if (mType == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN) { |
Rahul Sabnis | 9348f31 | 2019-12-04 16:36:21 -0800 | [diff] [blame] | 362 | mDevice.setPin(mPasskeyFormatted); |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * A method for querying if this bluetooth device type has a key it would like displayed |
| 368 | * to the user. |
| 369 | * |
| 370 | * @return - A boolean indicating if a key exists which should be displayed to the user. |
| 371 | */ |
| 372 | public boolean isDisplayPairingKeyVariant() { |
| 373 | switch (mType) { |
| 374 | case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY: |
| 375 | case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN: |
| 376 | case BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT: |
| 377 | return true; |
| 378 | default: |
| 379 | return false; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * A method for querying if this bluetooth device type has other content it would like displayed |
| 385 | * to the user. |
| 386 | * |
| 387 | * @return - A boolean indicating if content exists which should be displayed to the user. |
| 388 | */ |
| 389 | public boolean hasPairingContent() { |
| 390 | switch (mType) { |
| 391 | case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY: |
| 392 | case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN: |
| 393 | case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION: |
| 394 | return true; |
| 395 | |
| 396 | default: |
| 397 | return false; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * A method for obtaining any additional content this bluetooth device has for displaying to the |
| 403 | * user. |
| 404 | * |
| 405 | * @return - A string containing the additional content, null if none exists. |
| 406 | * @see {@link BluetoothPairingController#hasPairingContent()} |
| 407 | */ |
| 408 | public String getPairingContent() { |
| 409 | if (hasPairingContent()) { |
| 410 | return mPasskeyFormatted; |
| 411 | } else { |
| 412 | return null; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * A method that exists to allow the fragment to update the controller with input the user has |
| 418 | * provided in the fragment. |
| 419 | * |
| 420 | * @param input - A string containing the user input. |
| 421 | */ |
| 422 | protected void updateUserInput(String input) { |
| 423 | mUserInput = input; |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Returns the provided passkey in a format that this device expects. Only works for numeric |
| 428 | * passkeys/pins. |
| 429 | * |
| 430 | * @param passkey - An integer containing the passkey to format. |
| 431 | * @return - A string containing the formatted passkey/pin |
| 432 | */ |
| 433 | private String formatKey(int passkey) { |
| 434 | switch (mType) { |
| 435 | case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION: |
| 436 | case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY: |
| 437 | return String.format(Locale.US, "%06d", passkey); |
| 438 | |
| 439 | case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN: |
| 440 | return String.format("%04d", passkey); |
| 441 | |
| 442 | default: |
| 443 | return null; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * handles the necessary communication with the bluetooth device to establish a successful |
| 449 | * pairing |
| 450 | * |
| 451 | * @param passkey - The passkey we will attempt to pair to the device with. |
| 452 | */ |
| 453 | private void onPair(String passkey) { |
| 454 | Log.d(TAG, "Pairing dialog accepted"); |
| 455 | switch (mType) { |
| 456 | case BluetoothDevice.PAIRING_VARIANT_PIN: |
| 457 | case BluetoothDevice.PAIRING_VARIANT_PIN_16_DIGITS: |
Rahul Sabnis | 9348f31 | 2019-12-04 16:36:21 -0800 | [diff] [blame] | 458 | mDevice.setPin(passkey); |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 459 | break; |
| 460 | |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 461 | |
| 462 | case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION: |
| 463 | case BluetoothDevice.PAIRING_VARIANT_CONSENT: |
| 464 | mDevice.setPairingConfirmation(true); |
| 465 | break; |
| 466 | |
| 467 | case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY: |
| 468 | case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN: |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 469 | case BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT: |
Rahul Sabnis | 9348f31 | 2019-12-04 16:36:21 -0800 | [diff] [blame] | 470 | case BluetoothDevice.PAIRING_VARIANT_PASSKEY: |
| 471 | // Do nothing. |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 472 | break; |
| 473 | |
| 474 | default: |
| 475 | Log.e(TAG, "Incorrect pairing type received"); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * A method for properly ending communication with the bluetooth device. Will be called by the |
| 481 | * {@link BluetoothPairingDialogFragment} when it is dismissed. |
| 482 | */ |
| 483 | public void onCancel() { |
| 484 | Log.d(TAG, "Pairing dialog canceled"); |
William Escande | 4a5c128 | 2022-02-08 15:33:16 +0100 | [diff] [blame] | 485 | mDevice.cancelBondProcess(); |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | /** |
| 489 | * A method for checking if this device is equal to another device. |
| 490 | * |
| 491 | * @param device - The other device being compared to this device. |
| 492 | * @return - A boolean indicating if the devices were equal. |
| 493 | */ |
| 494 | public boolean deviceEquals(BluetoothDevice device) { |
| 495 | return mDevice == device; |
| 496 | } |
SongFerngWang | 51cabc5 | 2022-09-23 21:21:25 +0800 | [diff] [blame] | 497 | |
| 498 | @VisibleForTesting |
| 499 | void mockPbapClientProfile(LocalBluetoothProfile mockPbapClientProfile) { |
| 500 | mPbapClientProfile = mockPbapClientProfile; |
| 501 | } |
Salvador Martinez | d98d083 | 2016-10-03 17:52:51 -0700 | [diff] [blame] | 502 | } |