Refactor Bluetooth settings for readability and performance.
Major refactoring of Bluetooth settings classes. - Moved all functionality from LocalBluetoothManager into new LocalBluetoothAdapter and LocalBluetoothPreferences, and into existing classes. - Refactored functionality from BluetoothEventRedirector into new BluetoothEventManager class, deleting the original version. New version uses a HashMap from action Strings to implementers of the BluetoothEventManager.Handler interface. - Created new BluetoothDiscoveryReceiver to update shared preferences timestamp for Bluetooth discovery start/finish. This is the only event handling we need to do when the settings app is not visible, so it has its own receiver entry in AndroidManifest.xml. Edits are written using QueuedWork.singleThreadExecutor(), which BroadcastReceiver knows about and will wait for completion, eliminating the need for PendingResult. - Miscellaneous cleanups to code style and logic for readability. - Pulled some large switch statement code blocks into new methods. - Changed all Bluetooth state references to the new BluetoothProfile constants. - Changed use of deprecated Notification constructor in BluetoothPairingRequest to use Notification.Builder. - Moved Utf8ByteLengthFilter helper function from BluetoothNamePreference into its own class, and moved test cases into the same package. - Moved all LocalBluetoothProfileManager functionality related to specific profiles into new top-level classes (A2dpProfile, etc.), all implementing the LocalBluetoothProfile interface. - Moved all UI-related methods from CachedBluetoothDevice into the class that uses the method, or into the static Utils class for shared methods. Change-Id: I6d49b7f4ae0c7d7dcf62551ee40b51ecb5fe4f47
This commit is contained in:
@@ -42,33 +42,37 @@ import com.android.settings.R;
|
||||
* BluetoothPairingDialog asks the user to enter a PIN / Passkey / simple confirmation
|
||||
* for pairing with a remote Bluetooth device. It is an activity that appears as a dialog.
|
||||
*/
|
||||
public class BluetoothPairingDialog extends AlertActivity implements DialogInterface.OnClickListener,
|
||||
public final class BluetoothPairingDialog extends AlertActivity implements DialogInterface.OnClickListener,
|
||||
TextWatcher {
|
||||
private static final String TAG = "BluetoothPairingDialog";
|
||||
|
||||
private static final int BLUETOOTH_PIN_MAX_LENGTH = 16;
|
||||
private static final int BLUETOOTH_PASSKEY_MAX_LENGTH = 6;
|
||||
private LocalBluetoothManager mLocalManager;
|
||||
private BluetoothDevice mDevice;
|
||||
private int mType;
|
||||
private String mPairingKey;
|
||||
private EditText mPairingView;
|
||||
private Button mOkButton;
|
||||
|
||||
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
/**
|
||||
* Dismiss the dialog if the bond state changes to bonded or none,
|
||||
* or if pairing was canceled for {@link #mDevice}.
|
||||
*/
|
||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(intent.getAction())) {
|
||||
String action = intent.getAction();
|
||||
if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
|
||||
int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE,
|
||||
BluetoothDevice.ERROR);
|
||||
if (bondState == BluetoothDevice.BOND_BONDED ||
|
||||
bondState == BluetoothDevice.BOND_NONE) {
|
||||
dismissDialog();
|
||||
dismiss();
|
||||
}
|
||||
} else if(BluetoothDevice.ACTION_PAIRING_CANCEL.equals(intent.getAction())) {
|
||||
} else if (BluetoothDevice.ACTION_PAIRING_CANCEL.equals(action)) {
|
||||
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
||||
if (device == null || device.equals(mDevice)) {
|
||||
dismissDialog();
|
||||
dismiss();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -81,48 +85,63 @@ public class BluetoothPairingDialog extends AlertActivity implements DialogInter
|
||||
Intent intent = getIntent();
|
||||
if (!intent.getAction().equals(BluetoothDevice.ACTION_PAIRING_REQUEST))
|
||||
{
|
||||
Log.e(TAG,
|
||||
"Error: this activity may be started only with intent " +
|
||||
Log.e(TAG, "Error: this activity may be started only with intent " +
|
||||
BluetoothDevice.ACTION_PAIRING_REQUEST);
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
mLocalManager = LocalBluetoothManager.getInstance(this);
|
||||
LocalBluetoothManager manager = LocalBluetoothManager.getInstance(this);
|
||||
if (manager == null) {
|
||||
Log.e(TAG, "Error: BluetoothAdapter not supported by system");
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
CachedBluetoothDeviceManager deviceManager = manager.getCachedDeviceManager();
|
||||
|
||||
mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
||||
mType = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR);
|
||||
if (mType == BluetoothDevice.PAIRING_VARIANT_PIN) {
|
||||
createUserEntryDialog();
|
||||
} else if (mType == BluetoothDevice.PAIRING_VARIANT_PASSKEY) {
|
||||
createUserEntryDialog();
|
||||
} else if (mType == BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION){
|
||||
int passkey =
|
||||
intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY, BluetoothDevice.ERROR);
|
||||
if (passkey == BluetoothDevice.ERROR) {
|
||||
Log.e(TAG, "Invalid ConfirmationPasskey received, not showing any dialog");
|
||||
return;
|
||||
}
|
||||
mPairingKey = String.format("%06d", passkey);
|
||||
createConfirmationDialog();
|
||||
} else if (mType == BluetoothDevice.PAIRING_VARIANT_CONSENT) {
|
||||
createConsentDialog();
|
||||
} else if (mType == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY ||
|
||||
mType == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN) {
|
||||
int pairingKey =
|
||||
intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY, BluetoothDevice.ERROR);
|
||||
if (pairingKey == BluetoothDevice.ERROR) {
|
||||
Log.e(TAG, "Invalid Confirmation Passkey or PIN received, not showing any dialog");
|
||||
return;
|
||||
}
|
||||
if (mType == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY) {
|
||||
mPairingKey = String.format("%06d", pairingKey);
|
||||
} else {
|
||||
mPairingKey = String.format("%04d", pairingKey);
|
||||
}
|
||||
createDisplayPasskeyOrPinDialog();
|
||||
} else if (mType == BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT) {
|
||||
createConsentDialog();
|
||||
} else {
|
||||
Log.e(TAG, "Incorrect pairing type received, not showing any dialog");
|
||||
|
||||
switch (mType) {
|
||||
case BluetoothDevice.PAIRING_VARIANT_PIN:
|
||||
case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
|
||||
createUserEntryDialog(deviceManager);
|
||||
break;
|
||||
|
||||
case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION:
|
||||
int passkey =
|
||||
intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY, BluetoothDevice.ERROR);
|
||||
if (passkey == BluetoothDevice.ERROR) {
|
||||
Log.e(TAG, "Invalid Confirmation Passkey received, not showing any dialog");
|
||||
return;
|
||||
}
|
||||
mPairingKey = String.format("%06d", passkey);
|
||||
createConfirmationDialog(deviceManager);
|
||||
break;
|
||||
|
||||
case BluetoothDevice.PAIRING_VARIANT_CONSENT:
|
||||
case BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT:
|
||||
createConsentDialog(deviceManager);
|
||||
break;
|
||||
|
||||
case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:
|
||||
case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN:
|
||||
int pairingKey =
|
||||
intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY, BluetoothDevice.ERROR);
|
||||
if (pairingKey == BluetoothDevice.ERROR) {
|
||||
Log.e(TAG, "Invalid Confirmation Passkey or PIN received, not showing any dialog");
|
||||
return;
|
||||
}
|
||||
if (mType == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY) {
|
||||
mPairingKey = String.format("%06d", pairingKey);
|
||||
} else {
|
||||
mPairingKey = String.format("%04d", pairingKey);
|
||||
}
|
||||
createDisplayPasskeyOrPinDialog(deviceManager);
|
||||
break;
|
||||
|
||||
default:
|
||||
Log.e(TAG, "Incorrect pairing type received, not showing any dialog");
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -133,67 +152,79 @@ public class BluetoothPairingDialog extends AlertActivity implements DialogInter
|
||||
registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED));
|
||||
}
|
||||
|
||||
private void createUserEntryDialog() {
|
||||
private void createUserEntryDialog(CachedBluetoothDeviceManager deviceManager) {
|
||||
final AlertController.AlertParams p = mAlertParams;
|
||||
p.mIconId = android.R.drawable.ic_dialog_info;
|
||||
p.mTitle = getString(R.string.bluetooth_pairing_request);
|
||||
p.mView = createView();
|
||||
p.mView = createView(deviceManager);
|
||||
p.mPositiveButtonText = getString(android.R.string.ok);
|
||||
p.mPositiveButtonListener = this;
|
||||
p.mNegativeButtonText = getString(android.R.string.cancel);
|
||||
p.mNegativeButtonListener = this;
|
||||
setupAlert();
|
||||
|
||||
mOkButton = mAlert.getButton(DialogInterface.BUTTON_POSITIVE);
|
||||
mOkButton = mAlert.getButton(BUTTON_POSITIVE);
|
||||
mOkButton.setEnabled(false);
|
||||
}
|
||||
|
||||
private View createView() {
|
||||
private View createView(CachedBluetoothDeviceManager deviceManager) {
|
||||
View view = getLayoutInflater().inflate(R.layout.bluetooth_pin_entry, null);
|
||||
|
||||
String name = mLocalManager.getCachedDeviceManager().getName(mDevice);
|
||||
String name = deviceManager.getName(mDevice);
|
||||
TextView messageView = (TextView) view.findViewById(R.id.message);
|
||||
mPairingView = (EditText) view.findViewById(R.id.text);
|
||||
mPairingView.addTextChangedListener(this);
|
||||
|
||||
if (mType == BluetoothDevice.PAIRING_VARIANT_PIN) {
|
||||
messageView.setText(getString(R.string.bluetooth_enter_pin_msg, name));
|
||||
// Maximum of 16 characters in a PIN adb sync
|
||||
mPairingView.setFilters(new InputFilter[] {
|
||||
new LengthFilter(BLUETOOTH_PIN_MAX_LENGTH) });
|
||||
} else if (mType == BluetoothDevice.PAIRING_VARIANT_PASSKEY){
|
||||
messageView.setText(getString(R.string.bluetooth_enter_passkey_msg, name));
|
||||
// Maximum of 6 digits for passkey
|
||||
mPairingView.setInputType(InputType.TYPE_CLASS_NUMBER |
|
||||
InputType.TYPE_NUMBER_FLAG_SIGNED);
|
||||
mPairingView.setFilters(new InputFilter[] {
|
||||
new LengthFilter(BLUETOOTH_PASSKEY_MAX_LENGTH)});
|
||||
} else if (mType == BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION) {
|
||||
mPairingView.setVisibility(View.GONE);
|
||||
messageView.setText(getString(R.string.bluetooth_confirm_passkey_msg, name,
|
||||
mPairingKey));
|
||||
} else if (mType == BluetoothDevice.PAIRING_VARIANT_CONSENT) {
|
||||
mPairingView.setVisibility(View.GONE);
|
||||
messageView.setText(getString(R.string.bluetooth_incoming_pairing_msg, name));
|
||||
} else if (mType == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY ||
|
||||
mType == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN) {
|
||||
mPairingView.setVisibility(View.GONE);
|
||||
messageView.setText(getString(R.string.bluetooth_display_passkey_pin_msg, name,
|
||||
mPairingKey));
|
||||
} else if (mType == BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT) {
|
||||
mPairingView.setVisibility(View.GONE);
|
||||
messageView.setText(getString(R.string.bluetooth_incoming_pairing_msg, name));
|
||||
} else {
|
||||
Log.e(TAG, "Incorrect pairing type received, not creating view");
|
||||
switch (mType) {
|
||||
case BluetoothDevice.PAIRING_VARIANT_PIN:
|
||||
messageView.setText(getString(R.string.bluetooth_enter_pin_msg, name));
|
||||
// Maximum of 16 characters in a PIN adb sync
|
||||
mPairingView.setFilters(new InputFilter[] {
|
||||
new LengthFilter(BLUETOOTH_PIN_MAX_LENGTH) });
|
||||
break;
|
||||
|
||||
case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
|
||||
messageView.setText(getString(R.string.bluetooth_enter_passkey_msg, name));
|
||||
// Maximum of 6 digits for passkey
|
||||
mPairingView.setInputType(InputType.TYPE_CLASS_NUMBER |
|
||||
InputType.TYPE_NUMBER_FLAG_SIGNED);
|
||||
mPairingView.setFilters(new InputFilter[] {
|
||||
new LengthFilter(BLUETOOTH_PASSKEY_MAX_LENGTH)});
|
||||
break;
|
||||
|
||||
case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION:
|
||||
mPairingView.setVisibility(View.GONE);
|
||||
messageView.setText(getString(R.string.bluetooth_confirm_passkey_msg, name,
|
||||
mPairingKey));
|
||||
break;
|
||||
|
||||
case BluetoothDevice.PAIRING_VARIANT_CONSENT:
|
||||
mPairingView.setVisibility(View.GONE);
|
||||
messageView.setText(getString(R.string.bluetooth_incoming_pairing_msg, name));
|
||||
break;
|
||||
|
||||
case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:
|
||||
case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN:
|
||||
mPairingView.setVisibility(View.GONE);
|
||||
messageView.setText(getString(R.string.bluetooth_display_passkey_pin_msg, name,
|
||||
mPairingKey));
|
||||
break;
|
||||
|
||||
case BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT:
|
||||
mPairingView.setVisibility(View.GONE);
|
||||
messageView.setText(getString(R.string.bluetooth_incoming_pairing_msg, name));
|
||||
break;
|
||||
|
||||
default:
|
||||
Log.e(TAG, "Incorrect pairing type received, not creating view");
|
||||
}
|
||||
return view;
|
||||
}
|
||||
|
||||
private void createConfirmationDialog() {
|
||||
private void createConfirmationDialog(CachedBluetoothDeviceManager deviceManager) {
|
||||
final AlertController.AlertParams p = mAlertParams;
|
||||
p.mIconId = android.R.drawable.ic_dialog_info;
|
||||
p.mTitle = getString(R.string.bluetooth_pairing_request);
|
||||
p.mView = createView();
|
||||
p.mView = createView(deviceManager);
|
||||
p.mPositiveButtonText = getString(R.string.bluetooth_pairing_accept);
|
||||
p.mPositiveButtonListener = this;
|
||||
p.mNegativeButtonText = getString(R.string.bluetooth_pairing_decline);
|
||||
@@ -201,11 +232,11 @@ public class BluetoothPairingDialog extends AlertActivity implements DialogInter
|
||||
setupAlert();
|
||||
}
|
||||
|
||||
private void createConsentDialog() {
|
||||
private void createConsentDialog(CachedBluetoothDeviceManager deviceManager) {
|
||||
final AlertController.AlertParams p = mAlertParams;
|
||||
p.mIconId = android.R.drawable.ic_dialog_info;
|
||||
p.mTitle = getString(R.string.bluetooth_pairing_request);
|
||||
p.mView = createView();
|
||||
p.mView = createView(deviceManager);
|
||||
p.mPositiveButtonText = getString(R.string.bluetooth_pairing_accept);
|
||||
p.mPositiveButtonListener = this;
|
||||
p.mNegativeButtonText = getString(R.string.bluetooth_pairing_decline);
|
||||
@@ -213,11 +244,12 @@ public class BluetoothPairingDialog extends AlertActivity implements DialogInter
|
||||
setupAlert();
|
||||
}
|
||||
|
||||
private void createDisplayPasskeyOrPinDialog() {
|
||||
private void createDisplayPasskeyOrPinDialog(
|
||||
CachedBluetoothDeviceManager deviceManager) {
|
||||
final AlertController.AlertParams p = mAlertParams;
|
||||
p.mIconId = android.R.drawable.ic_dialog_info;
|
||||
p.mTitle = getString(R.string.bluetooth_pairing_request);
|
||||
p.mView = createView();
|
||||
p.mView = createView(deviceManager);
|
||||
p.mNegativeButtonText = getString(android.R.string.cancel);
|
||||
p.mNegativeButtonListener = this;
|
||||
setupAlert();
|
||||
@@ -244,32 +276,37 @@ public class BluetoothPairingDialog extends AlertActivity implements DialogInter
|
||||
}
|
||||
}
|
||||
|
||||
private void dismissDialog() {
|
||||
this.dismiss();
|
||||
}
|
||||
|
||||
private void onPair(String value) {
|
||||
if (mType == BluetoothDevice.PAIRING_VARIANT_PIN) {
|
||||
byte[] pinBytes = BluetoothDevice.convertPinToBytes(value);
|
||||
if (pinBytes == null) {
|
||||
return;
|
||||
}
|
||||
mDevice.setPin(pinBytes);
|
||||
} else if (mType == BluetoothDevice.PAIRING_VARIANT_PASSKEY) {
|
||||
int passkey = Integer.parseInt(value);
|
||||
mDevice.setPasskey(passkey);
|
||||
} else if (mType == BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION) {
|
||||
mDevice.setPairingConfirmation(true);
|
||||
} else if (mType == BluetoothDevice.PAIRING_VARIANT_CONSENT) {
|
||||
mDevice.setPairingConfirmation(true);
|
||||
} else if (mType == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY) {
|
||||
// Do Nothing.
|
||||
} else if (mType == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN) {
|
||||
// Do Nothing
|
||||
} else if (mType == BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT) {
|
||||
mDevice.setRemoteOutOfBandData();
|
||||
} else {
|
||||
Log.e(TAG, "Incorrect pairing type received");
|
||||
switch (mType) {
|
||||
case BluetoothDevice.PAIRING_VARIANT_PIN:
|
||||
byte[] pinBytes = BluetoothDevice.convertPinToBytes(value);
|
||||
if (pinBytes == null) {
|
||||
return;
|
||||
}
|
||||
mDevice.setPin(pinBytes);
|
||||
break;
|
||||
|
||||
case BluetoothDevice.PAIRING_VARIANT_PASSKEY:
|
||||
int passkey = Integer.parseInt(value);
|
||||
mDevice.setPasskey(passkey);
|
||||
break;
|
||||
|
||||
case BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION:
|
||||
case BluetoothDevice.PAIRING_VARIANT_CONSENT:
|
||||
mDevice.setPairingConfirmation(true);
|
||||
break;
|
||||
|
||||
case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY:
|
||||
case BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN:
|
||||
// Do nothing.
|
||||
break;
|
||||
|
||||
case BluetoothDevice.PAIRING_VARIANT_OOB_CONSENT:
|
||||
mDevice.setRemoteOutOfBandData();
|
||||
break;
|
||||
|
||||
default:
|
||||
Log.e(TAG, "Incorrect pairing type received");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -279,11 +316,12 @@ public class BluetoothPairingDialog extends AlertActivity implements DialogInter
|
||||
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
switch (which) {
|
||||
case DialogInterface.BUTTON_POSITIVE:
|
||||
case BUTTON_POSITIVE:
|
||||
onPair(mPairingView.getText().toString());
|
||||
break;
|
||||
|
||||
case DialogInterface.BUTTON_NEGATIVE:
|
||||
case BUTTON_NEGATIVE:
|
||||
default:
|
||||
onCancel();
|
||||
break;
|
||||
}
|
||||
|
Reference in New Issue
Block a user