The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 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; |
| 18 | |
| 19 | import android.app.Notification; |
| 20 | import android.app.NotificationManager; |
| 21 | import android.app.PendingIntent; |
| 22 | import android.bluetooth.BluetoothDevice; |
| 23 | import android.bluetooth.BluetoothIntent; |
| 24 | import android.content.BroadcastReceiver; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import 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 | */ |
| 35 | public 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 | } |