2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.alarmclock;
19 import android.app.KeyguardManager;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.BroadcastReceiver;
25 * Glue class: connects AlarmAlert IntentReceiver to AlarmAlert
26 * activity. Passes through Alarm ID.
28 public class AlarmReceiver extends BroadcastReceiver {
30 /** If the alarm is older than STALE_WINDOW seconds, ignore. It
31 is probably the result of a time or timezone change */
32 private final static int STALE_WINDOW = 60 * 30;
35 public void onReceive(Context context, Intent intent) {
36 long now = System.currentTimeMillis();
37 int id = intent.getIntExtra(Alarms.ID, 0);
38 long setFor = intent.getLongExtra(Alarms.TIME, 0);
40 /* FIXME Intentionally verbose: always log this until we've
41 fully debugged the app failing to start up */
42 Log.v("AlarmReceiver.onReceive() id " + id + " setFor " + setFor +
45 if (now > setFor + STALE_WINDOW * 1000) {
46 if (Log.LOGV) Log.v("AlarmReceiver ignoring stale alarm intent id"
47 + id + " setFor " + setFor + " now " + now);
51 // Wake the device and stay awake until the AlarmAlert intent is
52 // handled. Also acquire the screen lock so that if the AlarmAlert
53 // activity is paused, it will be resumed.
54 AlarmAlertWakeLock.acquireCpuWakeLock(context);
55 AlarmAlertWakeLock.acquireScreenWakeLock(context);
57 /* Close dialogs and window shade */
58 Intent i = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
59 context.sendBroadcast(i);
61 // Decide which activity to start based on the state of the keyguard.
62 Class c = AlarmAlert.class;
63 KeyguardManager km = (KeyguardManager) context.getSystemService(
64 Context.KEYGUARD_SERVICE);
65 if (km.inKeyguardRestrictedInputMode()) {
66 // Use the full screen activity for security.
67 c = AlarmAlertFullScreen.class;
70 /* launch UI, explicitly stating that this is not due to user action
71 * so that the current app's notification management is not disturbed */
72 Intent fireAlarm = new Intent(context, c);
73 fireAlarm.putExtra(Alarms.ID, id);
74 fireAlarm.putExtra(Alarms.LABEL, intent.getStringExtra(Alarms.LABEL));
75 fireAlarm.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
76 context.startActivity(fireAlarm);