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 private BroadcastReceiver mReceiver = new BroadcastReceiver() {
61 public void onReceive(Context context, Intent intent) {
62 Alarm alarm = intent.getParcelableExtra(Alarms.ALARM_INTENT_EXTRA);
63 if (alarm != null && mAlarm.id == alarm.id) {
70 protected void onCreate(Bundle icicle) {
71 super.onCreate(icicle);
73 mAlarm = getIntent().getParcelableExtra(Alarms.ALARM_INTENT_EXTRA);
75 // Get the volume/camera button behavior setting
77 PreferenceManager.getDefaultSharedPreferences(this)
78 .getString(SettingsActivity.KEY_VOLUME_BEHAVIOR,
79 DEFAULT_VOLUME_BEHAVIOR);
80 mVolumeBehavior = Integer.parseInt(vol);
82 requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
84 final Window win = getWindow();
85 win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
86 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
87 // Turn on the screen unless we are being launched from the AlarmAlert
89 if (!getIntent().getBooleanExtra(SCREEN_OFF, false)) {
90 win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
91 | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
96 // Register to get the alarm killed intent.
97 registerReceiver(mReceiver, new IntentFilter(Alarms.ALARM_KILLED));
100 private void setTitle() {
101 String label = mAlarm.getLabelOrDefault(this);
102 TextView title = (TextView) findViewById(R.id.alertTitle);
103 title.setText(label);
106 private void updateLayout() {
107 LayoutInflater inflater = LayoutInflater.from(this);
109 setContentView(inflater.inflate(R.layout.alarm_alert, null));
111 /* snooze behavior: pop a snooze confirmation view, kick alarm
113 Button snooze = (Button) findViewById(R.id.snooze);
114 snooze.requestFocus();
115 snooze.setOnClickListener(new Button.OnClickListener() {
116 public void onClick(View v) {
121 /* dismiss button: close notification */
122 findViewById(R.id.dismiss).setOnClickListener(
123 new Button.OnClickListener() {
124 public void onClick(View v) {
129 /* Set the title from the passed in alarm */
133 // Attempt to snooze this alert.
134 private void snooze() {
135 // Do not snooze if the snooze button is disabled.
136 if (!findViewById(R.id.snooze).isEnabled()) {
140 final String snooze =
141 PreferenceManager.getDefaultSharedPreferences(this)
142 .getString(SettingsActivity.KEY_ALARM_SNOOZE, DEFAULT_SNOOZE);
143 int snoozeMinutes = Integer.parseInt(snooze);
145 final long snoozeTime = System.currentTimeMillis()
146 + (1000 * 60 * snoozeMinutes);
147 Alarms.saveSnoozeAlert(AlarmAlertFullScreen.this, mAlarm.id,
150 // Get the display time for the snooze and update the notification.
151 final Calendar c = Calendar.getInstance();
152 c.setTimeInMillis(snoozeTime);
154 // Append (snoozed) to the label.
155 String label = mAlarm.getLabelOrDefault(this);
156 label = getString(R.string.alarm_notify_snooze_label, label);
158 // Notify the user that the alarm has been snoozed.
159 Intent cancelSnooze = new Intent(this, AlarmReceiver.class);
160 cancelSnooze.setAction(Alarms.CANCEL_SNOOZE);
161 cancelSnooze.putExtra(Alarms.ALARM_ID, mAlarm.id);
162 PendingIntent broadcast =
163 PendingIntent.getBroadcast(this, mAlarm.id, cancelSnooze, 0);
164 NotificationManager nm = getNotificationManager();
165 Notification n = new Notification(R.drawable.stat_notify_alarm,
167 n.setLatestEventInfo(this, label,
168 getString(R.string.alarm_notify_snooze_text,
169 Alarms.formatTime(this, c)), broadcast);
170 n.flags |= Notification.FLAG_AUTO_CANCEL
171 | Notification.FLAG_ONGOING_EVENT;
172 nm.notify(mAlarm.id, n);
174 String displayTime = getString(R.string.alarm_alert_snooze_set,
176 // Intentionally log the snooze time for debugging.
179 // Display the snooze minutes in a toast.
180 Toast.makeText(AlarmAlertFullScreen.this, displayTime,
181 Toast.LENGTH_LONG).show();
182 stopService(new Intent(Alarms.ALARM_ALERT_ACTION));
186 private NotificationManager getNotificationManager() {
187 return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
190 // Dismiss the alarm.
191 private void dismiss(boolean killed) {
192 // The service told us that the alarm has been killed, do not modify
193 // the notification or stop the service.
195 // Cancel the notification and stop playing the alarm
196 NotificationManager nm = getNotificationManager();
197 nm.cancel(mAlarm.id);
198 stopService(new Intent(Alarms.ALARM_ALERT_ACTION));
204 * this is called when a second alarm is triggered while a
205 * previous alert window is still active.
208 protected void onNewIntent(Intent intent) {
209 super.onNewIntent(intent);
211 if (Log.LOGV) Log.v("AlarmAlert.OnNewIntent()");
213 mAlarm = intent.getParcelableExtra(Alarms.ALARM_INTENT_EXTRA);
219 protected void onResume() {
221 // If the alarm was deleted at some point, disable snooze.
222 if (Alarms.getAlarm(getContentResolver(), mAlarm.id) == null) {
223 Button snooze = (Button) findViewById(R.id.snooze);
224 snooze.setEnabled(false);
229 protected void onStop() {
231 if (!isFinishing()) {
232 // Don't hang around.
238 public void onDestroy() {
240 if (Log.LOGV) Log.v("AlarmAlert.onDestroy()");
241 // No longer care about the alarm being killed.
242 unregisterReceiver(mReceiver);
246 public boolean dispatchKeyEvent(KeyEvent event) {
247 // Do this on key down to handle a few of the system keys.
248 boolean up = event.getAction() == KeyEvent.ACTION_UP;
249 switch (event.getKeyCode()) {
250 // Volume keys and camera keys dismiss the alarm
251 case KeyEvent.KEYCODE_VOLUME_UP:
252 case KeyEvent.KEYCODE_VOLUME_DOWN:
253 case KeyEvent.KEYCODE_CAMERA:
254 case KeyEvent.KEYCODE_FOCUS:
256 switch (mVolumeBehavior) {
273 return super.dispatchKeyEvent(event);
277 public void onBackPressed() {
278 // Don't allow back to dismiss. This method is overriden by AlarmAlert
279 // so that the dialog is dismissed.