private static final int BLUETOOTH_PIN_MAX_LENGTH = 16;
private static final int BLUETOOTH_PASSKEY_MAX_LENGTH = 6;
+
+ private LocalBluetoothManager mBluetoothManager;
+ private CachedBluetoothDeviceManager mCachedDeviceManager;
private BluetoothDevice mDevice;
private int mType;
private String mPairingKey;
return;
}
- LocalBluetoothManager manager = LocalBluetoothManager.getInstance(this);
- if (manager == null) {
+ mBluetoothManager = LocalBluetoothManager.getInstance(this);
+ if (mBluetoothManager == null) {
Log.e(TAG, "Error: BluetoothAdapter not supported by system");
finish();
return;
}
- CachedBluetoothDeviceManager deviceManager = manager.getCachedDeviceManager();
+ mCachedDeviceManager = mBluetoothManager.getCachedDeviceManager();
mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mType = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
switch (mType) {
case BluetoothDevice.PAIRING_VARIANT_PIN:
case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
- createUserEntryDialog(deviceManager);
+ createUserEntryDialog();
break;
case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION:
return;
}
mPairingKey = String.format(Locale.US, "%06d", passkey);
- createConfirmationDialog(deviceManager);
+ createConfirmationDialog();
break;
case BluetoothDevice.PAIRING_VARIANT_CONSENT:
case BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT:
- createConsentDialog(deviceManager);
+ createConsentDialog();
break;
case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:
} else {
mPairingKey = String.format("%04d", pairingKey);
}
- createDisplayPasskeyOrPinDialog(deviceManager);
+ createDisplayPasskeyOrPinDialog();
break;
default:
registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED));
}
- private void createUserEntryDialog(CachedBluetoothDeviceManager deviceManager) {
+ private void createUserEntryDialog() {
final AlertController.AlertParams p = mAlertParams;
p.mTitle = getString(R.string.bluetooth_pairing_request);
- p.mView = createPinEntryView(deviceManager.getName(mDevice));
+ p.mView = createPinEntryView();
p.mPositiveButtonText = getString(android.R.string.ok);
p.mPositiveButtonListener = this;
p.mNegativeButtonText = getString(android.R.string.cancel);
mOkButton.setEnabled(false);
}
- private View createPinEntryView(String deviceName) {
+ private View createPinEntryView() {
View view = getLayoutInflater().inflate(R.layout.bluetooth_pin_entry, null);
TextView messageViewCaption = (TextView) view.findViewById(R.id.message_caption);
TextView messageViewContent = (TextView) view.findViewById(R.id.message_subhead);
}
messageViewCaption.setText(messageId1);
- messageViewContent.setText(deviceName);
+ messageViewContent.setText(mCachedDeviceManager.getName(mDevice));
messageView2.setText(messageId2);
mPairingView.setInputType(InputType.TYPE_CLASS_NUMBER);
mPairingView.setFilters(new InputFilter[] {
return view;
}
- private View createView(CachedBluetoothDeviceManager deviceManager) {
+ private View createView() {
View view = getLayoutInflater().inflate(R.layout.bluetooth_pin_confirm, null);
// Escape device name to avoid HTML injection.
- String name = Html.escapeHtml(deviceManager.getName(mDevice));
+ String name = Html.escapeHtml(mCachedDeviceManager.getName(mDevice));
TextView messageViewCaption = (TextView) view.findViewById(R.id.message_caption);
TextView messageViewContent = (TextView) view.findViewById(R.id.message_subhead);
TextView pairingViewCaption = (TextView) view.findViewById(R.id.pairing_caption);
return view;
}
- private void createConfirmationDialog(CachedBluetoothDeviceManager deviceManager) {
+ private void createConfirmationDialog() {
final AlertController.AlertParams p = mAlertParams;
p.mTitle = getString(R.string.bluetooth_pairing_request);
- p.mView = createView(deviceManager);
+ p.mView = createView();
p.mPositiveButtonText = getString(R.string.bluetooth_pairing_accept);
p.mPositiveButtonListener = this;
p.mNegativeButtonText = getString(R.string.bluetooth_pairing_decline);
setupAlert();
}
- private void createConsentDialog(CachedBluetoothDeviceManager deviceManager) {
+ private void createConsentDialog() {
final AlertController.AlertParams p = mAlertParams;
p.mTitle = getString(R.string.bluetooth_pairing_request);
- p.mView = createView(deviceManager);
+ p.mView = createView();
p.mPositiveButtonText = getString(R.string.bluetooth_pairing_accept);
p.mPositiveButtonListener = this;
p.mNegativeButtonText = getString(R.string.bluetooth_pairing_decline);
setupAlert();
}
- private void createDisplayPasskeyOrPinDialog(
- CachedBluetoothDeviceManager deviceManager) {
+ private void createDisplayPasskeyOrPinDialog() {
final AlertController.AlertParams p = mAlertParams;
p.mTitle = getString(R.string.bluetooth_pairing_request);
- p.mView = createView(deviceManager);
+ p.mView = createView();
p.mNegativeButtonText = getString(android.R.string.cancel);
p.mNegativeButtonListener = this;
setupAlert();
}
}
+ private void allowPhonebookAccess() {
+ CachedBluetoothDevice cachedDevice = mCachedDeviceManager.findDevice(mDevice);
+ if (cachedDevice == null) {
+ cachedDevice = mCachedDeviceManager.addDevice(
+ mBluetoothManager.getBluetoothAdapter(),
+ mBluetoothManager.getProfileManager(),
+ mDevice);
+ }
+ cachedDevice.setPhonebookPermissionChoice(CachedBluetoothDevice.ACCESS_ALLOWED);
+ }
+
private void onPair(String value) {
+ allowPhonebookAccess();
+
switch (mType) {
case BluetoothDevice.PAIRING_VARIANT_PIN:
byte[] pinBytes = BluetoothDevice.convertPinToBytes(value);
private int mMessagePermissionChoice;
- private int mPhonebookRejectedTimes;
-
private int mMessageRejectedTimes;
private final Collection<Callback> mCallbacks = new ArrayList<Callback>();
private final static String PHONEBOOK_PREFS_NAME = "bluetooth_phonebook_permission";
private final static String MESSAGE_PREFS_NAME = "bluetooth_message_permission";
- private final static String PHONEBOOK_REJECT_TIMES = "bluetooth_phonebook_reject";
private final static String MESSAGE_REJECT_TIMES = "bluetooth_message_reject";
/**
updateProfiles();
fetchPhonebookPermissionChoice();
fetchMessagePermissionChoice();
- fetchPhonebookRejectTimes();
fetchMessageRejectTimes();
mVisible = false;
mConnectAfterPairing = false; // cancel auto-connect
setPhonebookPermissionChoice(ACCESS_UNKNOWN);
setMessagePermissionChoice(ACCESS_UNKNOWN);
- mPhonebookRejectedTimes = 0;
- savePhonebookRejectTimes();
mMessageRejectedTimes = 0;
saveMessageRejectTimes();
}
}
void setPhonebookPermissionChoice(int permissionChoice) {
- // if user reject it, only save it when reject exceed limit.
- if (permissionChoice == ACCESS_REJECTED) {
- mPhonebookRejectedTimes++;
- savePhonebookRejectTimes();
- if (mPhonebookRejectedTimes < PERSIST_REJECTED_TIMES_LIMIT) {
- return;
- }
- }
-
mPhonebookPermissionChoice = permissionChoice;
SharedPreferences.Editor editor =
ACCESS_UNKNOWN);
}
- private void fetchPhonebookRejectTimes() {
- SharedPreferences preference = mContext.getSharedPreferences(PHONEBOOK_REJECT_TIMES,
- Context.MODE_PRIVATE);
- mPhonebookRejectedTimes = preference.getInt(mDevice.getAddress(), 0);
- }
-
- private void savePhonebookRejectTimes() {
- SharedPreferences.Editor editor =
- mContext.getSharedPreferences(PHONEBOOK_REJECT_TIMES,
- Context.MODE_PRIVATE).edit();
- if (mPhonebookRejectedTimes == 0) {
- editor.remove(mDevice.getAddress());
- } else {
- editor.putInt(mDevice.getAddress(), mPhonebookRejectedTimes);
- }
- editor.commit();
- }
-
int getMessagePermissionChoice() {
return mMessagePermissionChoice;
}
if (pbapPermission != CachedBluetoothDevice.ACCESS_UNKNOWN) {
final PbapServerProfile psp = mManager.getProfileManager().getPbapProfile();
CheckBoxPreference pbapPref = createProfilePreference(psp);
- pbapPref.setChecked(pbapPermission == CachedBluetoothDevice.ACCESS_ALLOWED);
mProfileContainer.addPreference(pbapPref);
}
pref.setIcon(getResources().getDrawable(iconResource));
}
- /**
- * Gray out profile while connecting and disconnecting
- */
- pref.setEnabled(!mCachedDevice.isBusy());
-
refreshProfilePreference(pref, profile);
return pref;
LocalBluetoothProfile profile) {
BluetoothDevice device = mCachedDevice.getDevice();
- /*
- * Gray out checkbox while connecting and disconnecting
- */
+ // Gray out checkbox while connecting and disconnecting.
profilePref.setEnabled(!mCachedDevice.isBusy());
- profilePref.setChecked(profile.isPreferred(device));
+
+ if (profile instanceof PbapServerProfile) {
+ // Handle PBAP specially.
+ profilePref.setChecked(mCachedDevice.getPhonebookPermissionChoice()
+ == CachedBluetoothDevice.ACCESS_ALLOWED);
+ } else {
+ profilePref.setChecked(profile.isPreferred(device));
+ }
}
private LocalBluetoothProfile getProfileOf(Preference pref) {