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.app.Notification;
21 import android.app.NotificationManager;
22 import android.app.PendingIntent;
23 import android.content.ContentUris;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.BroadcastReceiver;
27 import android.database.Cursor;
28 import android.os.Parcel;
31 * Glue class: connects AlarmAlert IntentReceiver to AlarmAlert
32 * activity. Passes through Alarm ID.
34 public class AlarmReceiver extends BroadcastReceiver {
36 /** If the alarm is older than STALE_WINDOW seconds, ignore. It
37 is probably the result of a time or timezone change */
38 private final static int STALE_WINDOW = 60 * 30;
41 public void onReceive(Context context, Intent intent) {
42 // Take care of the easy intents first.
43 if (Alarms.CLEAR_NOTIFICATION.equals(intent.getAction())) {
44 // If this is the "Clear All Notifications" intent, stop the alarm
45 // service and return.
46 context.stopService(new Intent(Alarms.ALARM_ALERT_ACTION));
48 } else if (Alarms.ALARM_KILLED.equals(intent.getAction())) {
49 // The alarm has been killed, update the notification
50 updateNotification(context, (Alarm)
51 intent.getParcelableExtra(Alarms.ALARM_INTENT_EXTRA),
52 intent.getIntExtra(Alarms.ALARM_KILLED_TIMEOUT, -1));
54 } else if (Alarms.CANCEL_SNOOZE.equals(intent.getAction())) {
55 Alarms.saveSnoozeAlert(context, -1, -1);
60 // Grab the alarm from the intent. Since the remote AlarmManagerService
61 // fills in the Intent to add some extra data, it must unparcel the
62 // Alarm object. It throws a ClassNotFoundException when unparcelling.
63 // To avoid this, do the marshalling ourselves.
64 final byte[] data = intent.getByteArrayExtra(Alarms.ALARM_RAW_DATA);
66 Parcel in = Parcel.obtain();
67 in.unmarshall(data, 0, data.length);
68 in.setDataPosition(0);
69 alarm = Alarm.CREATOR.createFromParcel(in);
73 Log.v("AlarmReceiver failed to parse the alarm from the intent");
77 long now = System.currentTimeMillis();
78 /* FIXME Intentionally verbose: always log this until we've
79 fully debugged the app failing to start up */
80 Log.v("AlarmReceiver.onReceive() id " + alarm.id + " setFor "
81 + alarm.time + " now " + now);
83 if (now > alarm.time + STALE_WINDOW * 1000) {
85 Log.v("AlarmReceiver ignoring stale alarm intent id" + alarm.id
86 + " setFor " + alarm.time + " now " + now);
91 // Maintain a cpu wake lock until the AlarmAlert and AlarmKlaxon can
93 AlarmAlertWakeLock.acquireCpuWakeLock(context);
95 /* Close dialogs and window shade */
96 Intent closeDialogs = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
97 context.sendBroadcast(closeDialogs);
99 // Decide which activity to start based on the state of the keyguard.
100 Class c = AlarmAlert.class;
101 KeyguardManager km = (KeyguardManager) context.getSystemService(
102 Context.KEYGUARD_SERVICE);
103 if (km.inKeyguardRestrictedInputMode()) {
104 // Use the full screen activity for security.
105 c = AlarmAlertFullScreen.class;
108 /* launch UI, explicitly stating that this is not due to user action
109 * so that the current app's notification management is not disturbed */
110 Intent alarmAlert = new Intent(context, c);
111 alarmAlert.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm);
112 alarmAlert.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
113 | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
114 context.startActivity(alarmAlert);
116 // Disable the snooze alert if this alarm is the snooze.
117 Alarms.disableSnoozeAlert(context, alarm.id);
118 // Disable this alarm if it does not repeat.
119 if (!alarm.daysOfWeek.isRepeatSet()) {
120 Alarms.enableAlarm(context, alarm.id, false);
122 // Enable the next alert if there is one.
123 Alarms.setNextAlert(context);
125 // Play the alarm alert and vibrate the device.
126 Intent playAlarm = new Intent(Alarms.ALARM_ALERT_ACTION);
127 playAlarm.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm);
128 context.startService(playAlarm);
130 // Trigger a notification that, when clicked, will show the alarm alert
131 // dialog. No need to check for fullscreen since this will always be
132 // launched from a user action.
133 Intent notify = new Intent(context, AlarmAlert.class);
134 notify.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm);
135 PendingIntent pendingNotify = PendingIntent.getActivity(context,
136 alarm.id, notify, 0);
138 // Use the alarm's label or the default label as the ticker text and
139 // main text of the notification.
140 String label = alarm.getLabelOrDefault(context);
141 Notification n = new Notification(R.drawable.stat_notify_alarm,
143 n.setLatestEventInfo(context, label,
144 context.getString(R.string.alarm_notify_text),
146 n.flags |= Notification.FLAG_SHOW_LIGHTS;
147 n.ledARGB = 0xFF00FF00;
151 // Set the deleteIntent for when the user clicks "Clear All
153 Intent clearAll = new Intent(context, AlarmReceiver.class);
154 clearAll.setAction(Alarms.CLEAR_NOTIFICATION);
155 n.deleteIntent = PendingIntent.getBroadcast(context, 0, clearAll, 0);
157 // Send the notification using the alarm id to easily identify the
158 // correct notification.
159 NotificationManager nm = getNotificationManager(context);
160 nm.notify(alarm.id, n);
163 private NotificationManager getNotificationManager(Context context) {
164 return (NotificationManager)
165 context.getSystemService(Context.NOTIFICATION_SERVICE);
168 private void updateNotification(Context context, Alarm alarm, int timeout) {
169 NotificationManager nm = getNotificationManager(context);
171 // If the alarm is null, just cancel the notification.
174 Log.v("Cannot update notification for killer callback");
179 // Launch SetAlarm when clicked.
180 Intent viewAlarm = new Intent(context, SetAlarm.class);
181 viewAlarm.putExtra(Alarms.ALARM_ID, alarm.id);
182 PendingIntent intent =
183 PendingIntent.getActivity(context, alarm.id, viewAlarm, 0);
185 // Update the notification to indicate that the alert has been
187 String label = alarm.getLabelOrDefault(context);
188 Notification n = new Notification(R.drawable.stat_notify_alarm,
190 n.setLatestEventInfo(context, label,
191 context.getString(R.string.alarm_alert_alert_silenced, timeout),
193 n.flags |= Notification.FLAG_AUTO_CANCEL;
194 nm.notify(alarm.id, n);