• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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.settingslib.bluetooth;
18 
19 /* Required to handle timeout notification when phone is suspended */
20 import android.app.AlarmManager;
21 import android.app.PendingIntent;
22 import android.bluetooth.BluetoothAdapter;
23 import android.content.BroadcastReceiver;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.util.Log;
27 
28 
29 public class BluetoothDiscoverableTimeoutReceiver extends BroadcastReceiver {
30     private static final String TAG = "BluetoothDiscoverableTimeoutReceiver";
31 
32     private static final String INTENT_DISCOVERABLE_TIMEOUT =
33         "android.bluetooth.intent.DISCOVERABLE_TIMEOUT";
34 
setDiscoverableAlarm(Context context, long alarmTime)35     public static void setDiscoverableAlarm(Context context, long alarmTime) {
36         Log.d(TAG, "setDiscoverableAlarm(): alarmTime = " + alarmTime);
37 
38         Intent intent = new Intent(INTENT_DISCOVERABLE_TIMEOUT);
39         intent.setClass(context, BluetoothDiscoverableTimeoutReceiver.class);
40         PendingIntent pending = PendingIntent.getBroadcast(
41             context, 0, intent, 0);
42         AlarmManager alarmManager =
43               (AlarmManager) context.getSystemService (Context.ALARM_SERVICE);
44 
45         if (pending != null) {
46             // Cancel any previous alarms that do the same thing.
47             alarmManager.cancel(pending);
48             Log.d(TAG, "setDiscoverableAlarm(): cancel prev alarm");
49         }
50         pending = PendingIntent.getBroadcast(
51             context, 0, intent, 0);
52 
53         alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pending);
54     }
55 
cancelDiscoverableAlarm(Context context)56     public static void cancelDiscoverableAlarm(Context context) {
57         Log.d(TAG, "cancelDiscoverableAlarm(): Enter");
58 
59         Intent intent = new Intent(INTENT_DISCOVERABLE_TIMEOUT);
60         intent.setClass(context, BluetoothDiscoverableTimeoutReceiver.class);
61         PendingIntent pending = PendingIntent.getBroadcast(
62             context, 0, intent, PendingIntent.FLAG_NO_CREATE);
63         if (pending != null) {
64             // Cancel any previous alarms that do the same thing.
65             AlarmManager alarmManager =
66               (AlarmManager) context.getSystemService (Context.ALARM_SERVICE);
67 
68             alarmManager.cancel(pending);
69         }
70     }
71 
72     @Override
onReceive(Context context, Intent intent)73     public void onReceive(Context context, Intent intent) {
74         if (intent.getAction() == null || !intent.getAction().equals(INTENT_DISCOVERABLE_TIMEOUT)) {
75             return;
76         }
77         LocalBluetoothAdapter localBluetoothAdapter = LocalBluetoothAdapter.getInstance();
78         if(localBluetoothAdapter != null  &&
79             localBluetoothAdapter.getState() == BluetoothAdapter.STATE_ON) {
80             Log.d(TAG, "Disable discoverable...");
81             localBluetoothAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
82         } else {
83             Log.e(TAG, "localBluetoothAdapter is NULL!!");
84         }
85     }
86 };
87