Files
app_Settings/src/com/android/settings/bluetooth/BluetoothPairingDialog.java
Jack He e51972868c Fix Bluetooth pairing dialogue crash
When pairing passkey is invalid, onCreate() should return early to
prevent any dialog from displaying, which will then call onDestroy()
to destroy the activity. However mReceiver should be registered before
onDestroy() is called as onDestroy() will call unregisterReceiver(mReceiver)
that will throw an IllegalArgumentException when mReceiver was not registered.

This fix adds a flag that tracks whether mReceiver has been registered.
In addition, this fix fails the passkey confirmation request upon invalid
passkey.

Bug: 32577328
Test: Test with PTS TC_SERVER_SA_BI_01_C
Change-Id: Ifc047bad6bd99abca04404af9cd54d2034cd35aa
(cherry picked from commit 6f8e9eda2d9ed830d47971d93327792906db2dc1)
2017-01-19 22:39:17 +00:00

110 lines
4.1 KiB
Java

/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.bluetooth;
import android.annotation.Nullable;
import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
/**
* 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 final class BluetoothPairingDialog extends Activity {
public static final String FRAGMENT_TAG = "bluetooth.pairing.fragment";
private BluetoothPairingController mBluetoothPairingController;
private boolean mReceiverRegistered;
/**
* Dismiss the dialog if the bond state changes to bonded or none,
* or if pairing was canceled for {@link BluetoothPairingController#mDevice}.
*/
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
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) {
dismiss();
}
} else if (BluetoothDevice.ACTION_PAIRING_CANCEL.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (device == null || mBluetoothPairingController.deviceEquals(device)) {
dismiss();
}
}
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean fragmentFound = true;
BluetoothPairingDialogFragment bluetoothFragment =
(BluetoothPairingDialogFragment) getFragmentManager()
.findFragmentByTag(FRAGMENT_TAG);
Intent intent = getIntent();
mBluetoothPairingController = new BluetoothPairingController(intent, this);
// check if the fragment exists already
if (bluetoothFragment == null) {
fragmentFound = false;
bluetoothFragment = new BluetoothPairingDialogFragment();
}
// set the controller
bluetoothFragment.setPairingController(mBluetoothPairingController);
// pass the fragment to the manager when it is created from scratch
if (!fragmentFound) {
bluetoothFragment.show(getFragmentManager(), FRAGMENT_TAG);
}
/*
* Leave this registered through pause/resume since we still want to
* finish the activity in the background if pairing is canceled.
*/
registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_PAIRING_CANCEL));
registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED));
mReceiverRegistered = true;
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mReceiverRegistered) {
mReceiverRegistered = false;
unregisterReceiver(mReceiver);
}
}
private void dismiss() {
if (!isFinishing()) {
finish();
}
}
}