• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
31 /**
32  * BluetoothPairingRequest is a receiver for any Bluetooth pairing request. It
33  * checks if the Bluetooth Settings is currently visible and brings up the PIN, the passkey or a
34  * confirmation entry dialog. Otherwise it puts a Notification in the status bar, which can
35  * be clicked to bring up the Pairing entry dialog.
36  */
37 public class BluetoothPairingRequest extends BroadcastReceiver {
38 
39     public static final int NOTIFICATION_ID = android.R.drawable.stat_sys_data_bluetooth;
40 
41     @Override
onReceive(Context context, Intent intent)42     public void onReceive(Context context, Intent intent) {
43         String action = intent.getAction();
44         if (action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
45 
46             LocalBluetoothManager localManager = LocalBluetoothManager.getInstance(context);
47 
48             BluetoothDevice device =
49                     intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
50             int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
51                     BluetoothDevice.ERROR);
52             Intent pairingIntent = new Intent();
53             pairingIntent.setClass(context, BluetoothPairingDialog.class);
54             pairingIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
55             pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, type);
56             if (type == BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION ||
57                     type == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY) {
58                 int passkey = intent.getIntExtra(BluetoothDevice.EXTRA_PASSKEY, BluetoothDevice.ERROR);
59                 pairingIntent.putExtra(BluetoothDevice.EXTRA_PASSKEY, passkey);
60             }
61             pairingIntent.setAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
62             pairingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
63 
64             String deviceAddress = device != null ? device.getAddress() : null;
65             if (localManager.shouldShowDialogInForeground(deviceAddress)) {
66                 // Since the BT-related activity is in the foreground, just open the dialog
67                 context.startActivity(pairingIntent);
68 
69             } else {
70 
71                 // Put up a notification that leads to the dialog
72                 Resources res = context.getResources();
73                 Notification notification = new Notification(
74                         android.R.drawable.stat_sys_data_bluetooth,
75                         res.getString(R.string.bluetooth_notif_ticker),
76                         System.currentTimeMillis());
77 
78                 PendingIntent pending = PendingIntent.getActivity(context, 0,
79                         pairingIntent, PendingIntent.FLAG_ONE_SHOT);
80 
81                 String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
82                 if (TextUtils.isEmpty(name)) {
83                     name = device.getName();
84                 }
85 
86                 notification.setLatestEventInfo(context,
87                         res.getString(R.string.bluetooth_notif_title),
88                         res.getString(R.string.bluetooth_notif_message) + name,
89                         pending);
90                 notification.flags |= Notification.FLAG_AUTO_CANCEL;
91 
92                 NotificationManager manager = (NotificationManager)
93                         context.getSystemService(Context.NOTIFICATION_SERVICE);
94                 manager.notify(NOTIFICATION_ID, notification);
95             }
96 
97         } else if (action.equals(BluetoothDevice.ACTION_PAIRING_CANCEL)) {
98 
99             // Remove the notification
100             NotificationManager manager = (NotificationManager) context
101                     .getSystemService(Context.NOTIFICATION_SERVICE);
102             manager.cancel(NOTIFICATION_ID);
103         }
104     }
105 }
106