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.BluetoothCsipSetCoordinator;
20 import android.bluetooth.BluetoothDevice;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.os.PowerManager;
25 import android.os.UserHandle;
26 import android.text.TextUtils;
27 import android.util.Log;
28 
29 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
30 import com.android.settingslib.bluetooth.LocalBluetoothManager;
31 
32 /**
33  * BluetoothPairingRequest is a receiver for any Bluetooth pairing request. It
34  * checks if the Bluetooth Settings is currently visible and brings up the PIN, the passkey or a
35  * confirmation entry dialog. Otherwise it starts the BluetoothPairingService which
36  * starts a notification in the status bar that can be clicked to bring up the same dialog.
37  */
38 public final class BluetoothPairingRequest extends BroadcastReceiver {
39     private static final String TAG = "BluetoothPairingRequest";
40 
41     @Override
onReceive(Context context, Intent intent)42     public void onReceive(Context context, Intent intent) {
43         String action = intent.getAction();
44         if (action == null) {
45             return;
46         }
47 
48         BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
49         final LocalBluetoothManager mBluetoothManager = Utils.getLocalBtManager(context);
50         if (TextUtils.equals(action, BluetoothDevice.ACTION_PAIRING_REQUEST)) {
51             PowerManager powerManager = context.getSystemService(PowerManager.class);
52             int pairingVariant = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
53                     BluetoothDevice.ERROR);
54             boolean shouldShowDialog = LocalBluetoothPreferences.shouldShowDialogInForeground(
55                     context, device);
56 
57             Log.d(TAG,
58                 "Receive ACTION_PAIRING_REQUEST pairingVariant=" + pairingVariant
59                     + " canBondWithoutDialog=" + device.canBondWithoutDialog()
60                     + " isOngoingPairByCsip="
61                     + mBluetoothManager.getCachedDeviceManager().isOngoingPairByCsip(device)
62                     + " isLateBonding="
63                     + mBluetoothManager.getCachedDeviceManager().isLateBonding(device));
64 
65             /* Skips consent pairing dialog if the device was recently associated with CDM
66              * or if the device is a member of the coordinated set and is not bonding late.
67              */
68             if (pairingVariant == BluetoothDevice.PAIRING_VARIANT_CONSENT
69                 && (device.canBondWithoutDialog()
70                     || (mBluetoothManager.getCachedDeviceManager().isOngoingPairByCsip(device)
71                         && !mBluetoothManager.getCachedDeviceManager().isLateBonding(device)))) {
72                 device.setPairingConfirmation(true);
73             } else if (powerManager.isInteractive() && shouldShowDialog) {
74                 // Since the screen is on and the BT-related activity is in the foreground,
75                 // just open the dialog
76                 // convert broadcast intent into activity intent (same action string)
77                 Intent pairingIntent = BluetoothPairingService.getPairingDialogIntent(
78                     context, intent, BluetoothDevice.EXTRA_PAIRING_INITIATOR_FOREGROUND);
79 
80                 context.startActivityAsUser(pairingIntent, UserHandle.CURRENT);
81             } else {
82                 // Put up a notification that leads to the dialog
83                 intent.setClass(context, BluetoothPairingService.class);
84                 intent.setAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
85                 context.startServiceAsUser(intent, UserHandle.CURRENT);
86             }
87         } else if (TextUtils.equals(action,
88                 BluetoothCsipSetCoordinator.ACTION_CSIS_SET_MEMBER_AVAILABLE)) {
89             Log.d(TAG, "Receive ACTION_CSIS_SET_MEMBER_AVAILABLE");
90             if (device == null) {
91                 return;
92             }
93 
94             final int groupId = intent.getIntExtra(BluetoothCsipSetCoordinator.EXTRA_CSIS_GROUP_ID,
95                     BluetoothCsipSetCoordinator.GROUP_ID_INVALID);
96             if (groupId == BluetoothCsipSetCoordinator.GROUP_ID_INVALID) {
97                 return;
98             }
99 
100             mBluetoothManager.getCachedDeviceManager().pairDeviceByCsip(device, groupId);
101         }
102     }
103 }
104