17ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk/* 27ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * Copyright (C) 2012 The Android Open Source Project 37ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * 47ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * Licensed under the Apache License, Version 2.0 (the "License"); 57ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * you may not use this file except in compliance with the License. 67ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * You may obtain a copy of the License at 77ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * 87ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * http://www.apache.org/licenses/LICENSE-2.0 97ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * 107ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * Unless required by applicable law or agreed to in writing, software 117ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * distributed under the License is distributed on an "AS IS" BASIS, 127ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 137ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * See the License for the specific language governing permissions and 147ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk * limitations under the License. 157ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk */ 167ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk 177ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkpackage com.android.settingslib.bluetooth; 187ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk 197ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk/* Required to handle timeout notification when phone is suspended */ 207ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkimport android.app.AlarmManager; 217ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkimport android.app.PendingIntent; 227ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkimport android.bluetooth.BluetoothAdapter; 237ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkimport android.content.BroadcastReceiver; 247ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkimport android.content.Context; 257ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkimport android.content.Intent; 267ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkimport android.util.Log; 277ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk 287ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk 297ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monkpublic class BluetoothDiscoverableTimeoutReceiver extends BroadcastReceiver { 307ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk private static final String TAG = "BluetoothDiscoverableTimeoutReceiver"; 317ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk 327ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk private static final String INTENT_DISCOVERABLE_TIMEOUT = 337ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk "android.bluetooth.intent.DISCOVERABLE_TIMEOUT"; 347ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk 357ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk public static void setDiscoverableAlarm(Context context, long alarmTime) { 367ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk Log.d(TAG, "setDiscoverableAlarm(): alarmTime = " + alarmTime); 377ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk 387ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk Intent intent = new Intent(INTENT_DISCOVERABLE_TIMEOUT); 397ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk intent.setClass(context, BluetoothDiscoverableTimeoutReceiver.class); 407ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk PendingIntent pending = PendingIntent.getBroadcast( 417ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk context, 0, intent, 0); 427ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk AlarmManager alarmManager = 437ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk (AlarmManager) context.getSystemService (Context.ALARM_SERVICE); 447ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk 457ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk if (pending != null) { 467ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk // Cancel any previous alarms that do the same thing. 477ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk alarmManager.cancel(pending); 487ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk Log.d(TAG, "setDiscoverableAlarm(): cancel prev alarm"); 497ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk } 507ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk pending = PendingIntent.getBroadcast( 517ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk context, 0, intent, 0); 527ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk 537ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pending); 547ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk } 557ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk 567ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk public static void cancelDiscoverableAlarm(Context context) { 577ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk Log.d(TAG, "cancelDiscoverableAlarm(): Enter"); 587ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk 597ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk Intent intent = new Intent(INTENT_DISCOVERABLE_TIMEOUT); 607ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk intent.setClass(context, BluetoothDiscoverableTimeoutReceiver.class); 617ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk PendingIntent pending = PendingIntent.getBroadcast( 627ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk context, 0, intent, PendingIntent.FLAG_NO_CREATE); 637ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk if (pending != null) { 647ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk // Cancel any previous alarms that do the same thing. 657ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk AlarmManager alarmManager = 667ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk (AlarmManager) context.getSystemService (Context.ALARM_SERVICE); 677ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk 687ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk alarmManager.cancel(pending); 697ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk } 707ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk } 717ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk 727ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk @Override 737ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk public void onReceive(Context context, Intent intent) { 7442a25ba82e4c24ebfa311528fdf4a4eaea040ff8Jack He if (intent.getAction() == null || !intent.getAction().equals(INTENT_DISCOVERABLE_TIMEOUT)) { 7542a25ba82e4c24ebfa311528fdf4a4eaea040ff8Jack He return; 7642a25ba82e4c24ebfa311528fdf4a4eaea040ff8Jack He } 777ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk LocalBluetoothAdapter localBluetoothAdapter = LocalBluetoothAdapter.getInstance(); 7842a25ba82e4c24ebfa311528fdf4a4eaea040ff8Jack He if(localBluetoothAdapter != null && 797ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk localBluetoothAdapter.getState() == BluetoothAdapter.STATE_ON) { 807ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk Log.d(TAG, "Disable discoverable..."); 817ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk localBluetoothAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE); 8242a25ba82e4c24ebfa311528fdf4a4eaea040ff8Jack He } else { 837ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk Log.e(TAG, "localBluetoothAdapter is NULL!!"); 847ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk } 857ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk } 867ce96b9e610de2782ec5f2af806e7bc0f90c8578Jason Monk}; 87