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 // convert broadcast intent into activity intent (same action string) 41 Intent pairingIntent = BluetoothPairingService.getPairingDialogIntent(context, intent); 42 43 PowerManager powerManager = 44 (PowerManager)context.getSystemService(Context.POWER_SERVICE); 45 BluetoothDevice device = 46 intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 47 String deviceAddress = device != null ? device.getAddress() : null; 48 String deviceName = device != null ? device.getName() : null; 49 boolean shouldShowDialog = LocalBluetoothPreferences.shouldShowDialogInForeground( 50 context, deviceAddress, deviceName); 51 if (powerManager.isInteractive() && shouldShowDialog) { 52 // Since the screen is on and the BT-related activity is in the foreground, 53 // just open the dialog 54 context.startActivityAsUser(pairingIntent, UserHandle.CURRENT); 55 } else { 56 // Put up a notification that leads to the dialog 57 intent.setClass(context, BluetoothPairingService.class); 58 context.startServiceAsUser(intent, UserHandle.CURRENT); 59 } 60 } 61 } 62