Home | History | Annotate | Download | only in bluetooth
      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 com.android.settings.R;
     20 
     21 import android.app.Notification;
     22 import android.app.NotificationManager;
     23 import android.app.PendingIntent;
     24 import android.bluetooth.BluetoothDevice;
     25 import android.content.BroadcastReceiver;
     26 import android.content.Context;
     27 import android.content.Intent;
     28 import android.content.res.Resources;
     29 import android.text.TextUtils;
     30 import android.os.PowerManager;
     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 puts a Notification in the status bar, which can
     36  * be clicked to bring up the Pairing entry dialog.
     37  */
     38 public class BluetoothPairingRequest extends BroadcastReceiver {
     39 
     40     public static final int NOTIFICATION_ID = android.R.drawable.stat_sys_data_bluetooth;
     41 
     42     @Override
     43     public void onReceive(Context context, Intent intent) {
     44         String action = intent.getAction();
     45         if (action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
     46 
     47             LocalBluetoothManager localManager = LocalBluetoothManager.getInstance(context);
     48 
     49             BluetoothDevice device =
     50                     intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
     51             int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
     52                     BluetoothDevice.ERROR);
     53             Intent pairingIntent = new Intent();
     54             pairingIntent.setClass(context, BluetoothPairingDialog.class);
     55             pairingIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
     56             pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, type);
     57             if (type == BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION ||
     58                     type == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY) {
     59                 int passkey = intent.getIntExtra(BluetoothDevice.EXTRA_PASSKEY, BluetoothDevice.ERROR);
     60                 pairingIntent.putExtra(BluetoothDevice.EXTRA_PASSKEY, passkey);
     61             }
     62             pairingIntent.setAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
     63             pairingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     64 
     65             PowerManager powerManager =
     66                     (PowerManager)context.getSystemService(Context.POWER_SERVICE);
     67             String deviceAddress = device != null ? device.getAddress() : null;
     68             if (powerManager.isScreenOn() &&
     69                 localManager.shouldShowDialogInForeground(deviceAddress)) {
     70                 // Since the screen is on and the BT-related activity is in the foreground,
     71                 // just open the dialog
     72                 context.startActivity(pairingIntent);
     73 
     74             } else {
     75 
     76                 // Put up a notification that leads to the dialog
     77                 Resources res = context.getResources();
     78                 Notification notification = new Notification(
     79                         android.R.drawable.stat_sys_data_bluetooth,
     80                         res.getString(R.string.bluetooth_notif_ticker),
     81                         System.currentTimeMillis());
     82 
     83                 PendingIntent pending = PendingIntent.getActivity(context, 0,
     84                         pairingIntent, PendingIntent.FLAG_ONE_SHOT);
     85 
     86                 String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
     87                 if (TextUtils.isEmpty(name)) {
     88                     name = device.getName();
     89                 }
     90 
     91                 notification.setLatestEventInfo(context,
     92                         res.getString(R.string.bluetooth_notif_title),
     93                         res.getString(R.string.bluetooth_notif_message) + name,
     94                         pending);
     95                 notification.flags |= Notification.FLAG_AUTO_CANCEL;
     96                 notification.defaults |= Notification.DEFAULT_SOUND;
     97 
     98                 NotificationManager manager = (NotificationManager)
     99                         context.getSystemService(Context.NOTIFICATION_SERVICE);
    100                 manager.notify(NOTIFICATION_ID, notification);
    101             }
    102 
    103         } else if (action.equals(BluetoothDevice.ACTION_PAIRING_CANCEL)) {
    104 
    105             // Remove the notification
    106             NotificationManager manager = (NotificationManager) context
    107                     .getSystemService(Context.NOTIFICATION_SERVICE);
    108             manager.cancel(NOTIFICATION_ID);
    109         }
    110     }
    111 }
    112