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