1 /* 2 * Copyright (C) 2008 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 android.bluetooth.BluetoothDevice; 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.os.PowerManager; 24 import android.os.UserHandle; 25 26 /** 27 * BluetoothPairingRequest is a receiver for any Bluetooth pairing request. It 28 * checks if the Bluetooth Settings is currently visible and brings up the PIN, the passkey or a 29 * confirmation entry dialog. Otherwise it starts the BluetoothPairingService which 30 * starts a notification in the status bar that can be clicked to bring up the same dialog. 31 */ 32 public final class BluetoothPairingRequest extends BroadcastReceiver { 33 34 @Override onReceive(Context context, Intent intent)35 public void onReceive(Context context, Intent intent) { 36 String action = intent.getAction(); 37 if (action == null || !action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) { 38 return; 39 } 40 41 PowerManager powerManager = context.getSystemService(PowerManager.class); 42 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 43 int pairingVariant = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, 44 BluetoothDevice.ERROR); 45 String deviceAddress = device != null ? device.getAddress() : null; 46 String deviceName = device != null ? device.getName() : null; 47 boolean shouldShowDialog = LocalBluetoothPreferences.shouldShowDialogInForeground( 48 context, deviceAddress, deviceName); 49 50 // Skips consent pairing dialog if the device was recently associated with CDM 51 if (pairingVariant == BluetoothDevice.PAIRING_VARIANT_CONSENT 52 && device.canBondWithoutDialog()) { 53 device.setPairingConfirmation(true); 54 } else if (powerManager.isInteractive() && shouldShowDialog) { 55 // Since the screen is on and the BT-related activity is in the foreground, 56 // just open the dialog 57 // convert broadcast intent into activity intent (same action string) 58 Intent pairingIntent = BluetoothPairingService.getPairingDialogIntent(context, intent, 59 BluetoothDevice.EXTRA_PAIRING_INITIATOR_FOREGROUND); 60 61 context.startActivityAsUser(pairingIntent, UserHandle.CURRENT); 62 } else { 63 // Put up a notification that leads to the dialog 64 intent.setClass(context, BluetoothPairingService.class); 65 intent.setAction(BluetoothDevice.ACTION_PAIRING_REQUEST); 66 context.startServiceAsUser(intent, UserHandle.CURRENT); 67 } 68 } 69 } 70