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
17package com.android.settingslib.bluetooth;
18
19/* Required to handle timeout notification when phone is suspended */
20import android.app.AlarmManager;
21import android.app.PendingIntent;
22import android.bluetooth.BluetoothAdapter;
23import android.content.BroadcastReceiver;
24import android.content.Context;
25import android.content.Intent;
26import android.util.Log;
27
28
29public 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
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
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
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