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 17 package com.android.settings.bluetooth; 18 19 import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS; 20 21 import android.bluetooth.BluetoothDevice; 22 import android.content.BroadcastReceiver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.IntentFilter; 26 import android.os.Bundle; 27 28 import androidx.annotation.Nullable; 29 import androidx.annotation.VisibleForTesting; 30 import androidx.fragment.app.FragmentActivity; 31 32 /** 33 * BluetoothPairingDialog asks the user to enter a PIN / Passkey / simple confirmation 34 * for pairing with a remote Bluetooth device. It is an activity that appears as a dialog. 35 */ 36 public class BluetoothPairingDialog extends FragmentActivity { 37 public static final String FRAGMENT_TAG = "bluetooth.pairing.fragment"; 38 39 private BluetoothPairingController mBluetoothPairingController; 40 private boolean mReceiverRegistered = false; 41 42 /** 43 * Dismiss the dialog if the bond state changes to bonded or none, 44 * or if pairing was canceled for {@link BluetoothPairingController#mDevice}. 45 */ 46 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 47 @Override 48 public void onReceive(Context context, Intent intent) { 49 String action = intent.getAction(); 50 if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) { 51 int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, 52 BluetoothDevice.ERROR); 53 if (bondState == BluetoothDevice.BOND_BONDED || 54 bondState == BluetoothDevice.BOND_NONE) { 55 dismiss(); 56 } 57 } else if (BluetoothDevice.ACTION_PAIRING_CANCEL.equals(action)) { 58 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 59 if (device == null || mBluetoothPairingController.deviceEquals(device)) { 60 dismiss(); 61 } 62 } 63 } 64 }; 65 66 @Override onCreate(@ullable Bundle savedInstanceState)67 protected void onCreate(@Nullable Bundle savedInstanceState) { 68 super.onCreate(savedInstanceState); 69 70 getWindow().addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS); 71 Intent intent = getIntent(); 72 if (intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) == null) { 73 // Error handler for the case that dialog is started from adb command. 74 finish(); 75 return; 76 } 77 mBluetoothPairingController = new BluetoothPairingController(intent, this); 78 // build the dialog fragment 79 boolean fragmentFound = true; 80 // check if the fragment has been preloaded 81 BluetoothPairingDialogFragment bluetoothFragment = 82 (BluetoothPairingDialogFragment) getSupportFragmentManager(). 83 findFragmentByTag(FRAGMENT_TAG); 84 // dismiss the fragment if it is already used 85 if (bluetoothFragment != null && (bluetoothFragment.isPairingControllerSet() 86 || bluetoothFragment.isPairingDialogActivitySet())) { 87 bluetoothFragment.dismiss(); 88 bluetoothFragment = null; 89 } 90 // build a new fragment if it is null 91 if (bluetoothFragment == null) { 92 fragmentFound = false; 93 bluetoothFragment = new BluetoothPairingDialogFragment(); 94 } 95 bluetoothFragment.setPairingController(mBluetoothPairingController); 96 bluetoothFragment.setPairingDialogActivity(this); 97 // pass the fragment to the manager when it is created from scratch 98 if (!fragmentFound) { 99 bluetoothFragment.show(getSupportFragmentManager(), FRAGMENT_TAG); 100 } 101 /* 102 * Leave this registered through pause/resume since we still want to 103 * finish the activity in the background if pairing is canceled. 104 */ 105 registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_PAIRING_CANCEL)); 106 registerReceiver(mReceiver, new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED)); 107 mReceiverRegistered = true; 108 109 closeSystemDialogs(); 110 } 111 112 @Override onDestroy()113 protected void onDestroy() { 114 super.onDestroy(); 115 if (mReceiverRegistered) { 116 mReceiverRegistered = false; 117 unregisterReceiver(mReceiver); 118 } 119 } 120 121 @VisibleForTesting dismiss()122 void dismiss() { 123 if (!isFinishing()) { 124 finish(); 125 } 126 } 127 } 128