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