1 /* 2 * Copyright (C) 2007 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.deskclock; 18 19 import android.app.KeyguardManager; 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.IntentFilter; 24 import android.os.Bundle; 25 import android.os.Handler; 26 import android.os.Message; 27 28 /** 29 * Full screen alarm alert: pops visible indicator and plays alarm tone. This 30 * activity shows the alert as a dialog. 31 */ 32 public class AlarmAlert extends AlarmAlertFullScreen { 33 34 // If we try to check the keyguard more than 5 times, just launch the full 35 // screen activity. 36 private int mKeyguardRetryCount; 37 private final int MAX_KEYGUARD_CHECKS = 5; 38 39 private final Handler mHandler = new Handler() { 40 @Override 41 public void handleMessage(Message msg) { 42 handleScreenOff((KeyguardManager) msg.obj); 43 } 44 }; 45 46 private final BroadcastReceiver mScreenOffReceiver = 47 new BroadcastReceiver() { 48 @Override 49 public void onReceive(Context context, Intent intent) { 50 KeyguardManager km = 51 (KeyguardManager) context.getSystemService( 52 Context.KEYGUARD_SERVICE); 53 handleScreenOff(km); 54 } 55 }; 56 57 @Override 58 protected void onCreate(Bundle icicle) { 59 super.onCreate(icicle); 60 61 // Listen for the screen turning off so that when the screen comes back 62 // on, the user does not need to unlock the phone to dismiss the alarm. 63 registerReceiver(mScreenOffReceiver, 64 new IntentFilter(Intent.ACTION_SCREEN_OFF)); 65 } 66 67 @Override 68 public void onDestroy() { 69 super.onDestroy(); 70 unregisterReceiver(mScreenOffReceiver); 71 // Remove any of the keyguard messages just in case 72 mHandler.removeMessages(0); 73 } 74 75 @Override 76 public void onBackPressed() { 77 finish(); 78 } 79 80 @Override 81 protected int getLayoutResId() { 82 return R.layout.alarm_alert; 83 } 84 85 private boolean checkRetryCount() { 86 if (mKeyguardRetryCount++ >= MAX_KEYGUARD_CHECKS) { 87 Log.e("Tried to read keyguard status too many times, bailing..."); 88 return false; 89 } 90 return true; 91 } 92 93 private void handleScreenOff(final KeyguardManager km) { 94 if (!km.inKeyguardRestrictedInputMode() && checkRetryCount()) { 95 if (checkRetryCount()) { 96 mHandler.sendMessageDelayed(mHandler.obtainMessage(0, km), 500); 97 } 98 } else { 99 // Launch the full screen activity but do not turn the screen on. 100 Intent i = new Intent(this, AlarmAlertFullScreen.class); 101 i.putExtra(Alarms.ALARM_INTENT_EXTRA, mAlarm); 102 i.putExtra(SCREEN_OFF, true); 103 startActivity(i); 104 finish(); 105 } 106 } 107 } 108