blob: d6a12f3efbb6f45e721ded6815441efd4eaae3b7 [file] [log] [blame]
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -07001/*
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
17package com.android.settings;
18
19import android.app.Notification;
20import android.app.NotificationManager;
21import android.app.PendingIntent;
22import android.bluetooth.BluetoothDevice;
23import android.bluetooth.BluetoothIntent;
24import android.content.BroadcastReceiver;
25import android.content.Context;
26import android.content.Intent;
27import android.content.res.Resources;
28
29/**
30 * This class handles the Bluetooth pairing PIN request from the bluetooth service
31 * It checks if the BluetoothSettings activity is currently visible and lets that
32 * activity handle the request. Otherwise it puts a Notification in the status bar,
33 * which can be clicked to bring up the PIN entry dialog.
34 */
35public class BluetoothPinRequest extends BroadcastReceiver {
36
37 @Override
38 public void onReceive(Context context, Intent intent) {
39 if (intent.getAction().equals(BluetoothIntent.PAIRING_REQUEST_ACTION)) {
40 if (BluetoothSettings.isRunning()) {
41 // Let the BluetoothSettings activity handle it
42 return;
43 } else {
44 Resources res = context.getResources();
45 String address = intent.getStringExtra(BluetoothIntent.ADDRESS);
46 Notification pair = new Notification(
47 android.R.drawable.stat_sys_data_bluetooth,
48 res.getString(R.string.bluetooth_notif_ticker),
49 System.currentTimeMillis());
50
51 Intent pinIntent = new Intent();
52 pinIntent.setClass(context, BluetoothPINEntry.class);
53 pinIntent.putExtra(BluetoothIntent.ADDRESS, address);
54 pinIntent.setAction(BluetoothIntent.PAIRING_REQUEST_ACTION);
55 PendingIntent pending = PendingIntent.getActivity(context, 0,
56 pinIntent, PendingIntent.FLAG_ONE_SHOT);
57
58 String name = intent.getStringExtra(BluetoothIntent.NAME);
59
60 if (name == null) {
61 BluetoothDevice bluetooth =
62 (BluetoothDevice)context.getSystemService(Context.BLUETOOTH_SERVICE);
63 name = bluetooth.getRemoteName(address);
64 if (name == null) {
65 name = address;
66 }
67 }
68
69 pair.setLatestEventInfo(context,
70 res.getString(R.string.bluetooth_notif_title),
71 res.getString(R.string.bluetooth_notif_message) + name,
72 pending);
73
74 NotificationManager manager = (NotificationManager)
75 context.getSystemService(Context.NOTIFICATION_SERVICE);
76 manager.notify(0xb100ceee, pair);
77 }
78 }
79 }
80}