2 * Copyright (C) 2009 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.deskclock;
19 import android.app.Activity;
20 import android.app.Notification;
21 import android.app.NotificationManager;
22 import android.app.PendingIntent;
23 import android.content.Context;
24 import android.content.BroadcastReceiver;
25 import android.content.Intent;
26 import android.content.IntentFilter;
27 import android.content.SharedPreferences;
28 import android.content.res.Configuration;
29 import android.os.Bundle;
30 import android.preference.PreferenceManager;
31 import android.view.KeyEvent;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.view.LayoutInflater;
35 import android.view.Window;
36 import android.view.WindowManager;
37 import android.widget.Button;
38 import android.widget.Toast;
39 import android.widget.TextView;
41 import java.util.Calendar;
44 * Alarm Clock alarm alert: pops visible indicator and plays alarm
45 * tone. This activity is the full screen version which shows over the lock
46 * screen with the wallpaper as the background.
48 public class AlarmAlertFullScreen extends Activity {
50 // These defaults must match the values in res/xml/settings.xml
51 private static final String DEFAULT_SNOOZE = "10";
52 private static final String DEFAULT_VOLUME_BEHAVIOR = "2";
53 protected static final String SCREEN_OFF = "screen_off";
55 protected Alarm mAlarm;
56 private int mVolumeBehavior;
58 // Receives the ALARM_KILLED action from the AlarmKlaxon,
59 // and also ALARM_SNOOZE_ACTION / ALARM_DISMISS_ACTION from other applications
60 private BroadcastReceiver mReceiver = new BroadcastReceiver() {
62 public void onReceive(Context context, Intent intent) {
63 String action = intent.getAction();
64 if (action.equals(Alarms.ALARM_SNOOZE_ACTION)) {
66 } else if (action.equals(Alarms.ALARM_DISMISS_ACTION)) {
69 Alarm alarm = intent.getParcelableExtra(Alarms.ALARM_INTENT_EXTRA);
70 if (alarm != null && mAlarm.id == alarm.id) {
78 protected void onCreate(Bundle icicle) {
79 super.onCreate(icicle);
81 mAlarm = getIntent().getParcelableExtra(Alarms.ALARM_INTENT_EXTRA);
83 // Get the volume/camera button behavior setting
85 PreferenceManager.getDefaultSharedPreferences(this)
86 .getString(SettingsActivity.KEY_VOLUME_BEHAVIOR,
87 DEFAULT_VOLUME_BEHAVIOR);
88 mVolumeBehavior = Integer.parseInt(vol);
90 requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
92 final Window win = getWindow();
93 win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
94 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
95 // Turn on the screen unless we are being launched from the AlarmAlert
97 if (!getIntent().getBooleanExtra(SCREEN_OFF, false)) {
98 win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
99 | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
100 | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
105 // Register to get the alarm killed/snooze/dismiss intent.
106 IntentFilter filter = new IntentFilter(Alarms.ALARM_KILLED);
107 filter.addAction(Alarms.ALARM_SNOOZE_ACTION);
108 filter.addAction(Alarms.ALARM_DISMISS_ACTION);
109 registerReceiver(mReceiver, filter);
112 private void setTitle() {
113 String label = mAlarm.getLabelOrDefault(this);
114 TextView title = (TextView) findViewById(R.id.alertTitle);
115 title.setText(label);
118 private void updateLayout() {
119 LayoutInflater inflater = LayoutInflater.from(this);
121 setContentView(inflater.inflate(R.layout.alarm_alert, null));
123 /* snooze behavior: pop a snooze confirmation view, kick alarm
125 Button snooze = (Button) findViewById(R.id.snooze);
126 snooze.requestFocus();
127 snooze.setOnClickListener(new Button.OnClickListener() {
128 public void onClick(View v) {
133 /* dismiss button: close notification */
134 findViewById(R.id.dismiss).setOnClickListener(
135 new Button.OnClickListener() {
136 public void onClick(View v) {
141 /* Set the title from the passed in alarm */
145 // Attempt to snooze this alert.
146 private void snooze() {
147 // Do not snooze if the snooze button is disabled.
148 if (!findViewById(R.id.snooze).isEnabled()) {
152 final String snooze =
153 PreferenceManager.getDefaultSharedPreferences(this)
154 .getString(SettingsActivity.KEY_ALARM_SNOOZE, DEFAULT_SNOOZE);
155 int snoozeMinutes = Integer.parseInt(snooze);
157 final long snoozeTime = System.currentTimeMillis()
158 + (1000 * 60 * snoozeMinutes);
159 Alarms.saveSnoozeAlert(AlarmAlertFullScreen.this, mAlarm.id,
162 // Get the display time for the snooze and update the notification.
163 final Calendar c = Calendar.getInstance();
164 c.setTimeInMillis(snoozeTime);
166 // Append (snoozed) to the label.
167 String label = mAlarm.getLabelOrDefault(this);
168 label = getString(R.string.alarm_notify_snooze_label, label);
170 // Notify the user that the alarm has been snoozed.
171 Intent cancelSnooze = new Intent(this, AlarmReceiver.class);
172 cancelSnooze.setAction(Alarms.CANCEL_SNOOZE);
173 cancelSnooze.putExtra(Alarms.ALARM_ID, mAlarm.id);
174 PendingIntent broadcast =
175 PendingIntent.getBroadcast(this, mAlarm.id, cancelSnooze, 0);
176 NotificationManager nm = getNotificationManager();
177 Notification n = new Notification(R.drawable.stat_notify_alarm,
179 n.setLatestEventInfo(this, label,
180 getString(R.string.alarm_notify_snooze_text,
181 Alarms.formatTime(this, c)), broadcast);
182 n.flags |= Notification.FLAG_AUTO_CANCEL
183 | Notification.FLAG_ONGOING_EVENT;
184 nm.notify(mAlarm.id, n);
186 String displayTime = getString(R.string.alarm_alert_snooze_set,
188 // Intentionally log the snooze time for debugging.
191 // Display the snooze minutes in a toast.
192 Toast.makeText(AlarmAlertFullScreen.this, displayTime,
193 Toast.LENGTH_LONG).show();
194 stopService(new Intent(Alarms.ALARM_ALERT_ACTION));
198 private NotificationManager getNotificationManager() {
199 return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
202 // Dismiss the alarm.
203 private void dismiss(boolean killed) {
204 Log.i(killed ? "Alarm killed" : "Alarm dismissed by user");
205 // The service told us that the alarm has been killed, do not modify
206 // the notification or stop the service.
208 // Cancel the notification and stop playing the alarm
209 NotificationManager nm = getNotificationManager();
210 nm.cancel(mAlarm.id);
211 stopService(new Intent(Alarms.ALARM_ALERT_ACTION));
217 * this is called when a second alarm is triggered while a
218 * previous alert window is still active.
221 protected void onNewIntent(Intent intent) {
222 super.onNewIntent(intent);
224 if (Log.LOGV) Log.v("AlarmAlert.OnNewIntent()");
226 mAlarm = intent.getParcelableExtra(Alarms.ALARM_INTENT_EXTRA);
232 protected void onResume() {
234 // If the alarm was deleted at some point, disable snooze.
235 if (Alarms.getAlarm(getContentResolver(), mAlarm.id) == null) {
236 Button snooze = (Button) findViewById(R.id.snooze);
237 snooze.setEnabled(false);
242 public void onDestroy() {
244 if (Log.LOGV) Log.v("AlarmAlert.onDestroy()");
245 // No longer care about the alarm being killed.
246 unregisterReceiver(mReceiver);
250 public boolean dispatchKeyEvent(KeyEvent event) {
251 // Do this on key down to handle a few of the system keys.
252 boolean up = event.getAction() == KeyEvent.ACTION_UP;
253 switch (event.getKeyCode()) {
254 // Volume keys and camera keys dismiss the alarm
255 case KeyEvent.KEYCODE_VOLUME_UP:
256 case KeyEvent.KEYCODE_VOLUME_DOWN:
257 case KeyEvent.KEYCODE_CAMERA:
258 case KeyEvent.KEYCODE_FOCUS:
260 switch (mVolumeBehavior) {
277 return super.dispatchKeyEvent(event);
281 public void onBackPressed() {
282 // Don't allow back to dismiss. This method is overriden by AlarmAlert
283 // so that the dialog is dismissed.