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 final String snooze =
136 PreferenceManager.getDefaultSharedPreferences(this)
137 .getString(SettingsActivity.KEY_ALARM_SNOOZE, DEFAULT_SNOOZE);
138 int snoozeMinutes = Integer.parseInt(snooze);
140 final long snoozeTime = System.currentTimeMillis()
141 + (1000 * 60 * snoozeMinutes);
142 Alarms.saveSnoozeAlert(AlarmAlertFullScreen.this, mAlarm.id,
145 // Get the display time for the snooze and update the notification.
146 final Calendar c = Calendar.getInstance();
147 c.setTimeInMillis(snoozeTime);
149 // Append (snoozed) to the label.
150 String label = mAlarm.getLabelOrDefault(this);
151 label = getString(R.string.alarm_notify_snooze_label, label);
153 // Notify the user that the alarm has been snoozed.
154 Intent cancelSnooze = new Intent(this, AlarmReceiver.class);
155 cancelSnooze.setAction(Alarms.CANCEL_SNOOZE);
156 cancelSnooze.putExtra(Alarms.ALARM_ID, mAlarm.id);
157 PendingIntent broadcast =
158 PendingIntent.getBroadcast(this, mAlarm.id, cancelSnooze, 0);
159 NotificationManager nm = getNotificationManager();
160 Notification n = new Notification(R.drawable.stat_notify_alarm,
162 n.setLatestEventInfo(this, label,
163 getString(R.string.alarm_notify_snooze_text,
164 Alarms.formatTime(this, c)), broadcast);
165 n.flags |= Notification.FLAG_AUTO_CANCEL
166 | Notification.FLAG_ONGOING_EVENT;
167 nm.notify(mAlarm.id, n);
169 String displayTime = getString(R.string.alarm_alert_snooze_set,
171 // Intentionally log the snooze time for debugging.
174 // Display the snooze minutes in a toast.
175 Toast.makeText(AlarmAlertFullScreen.this, displayTime,
176 Toast.LENGTH_LONG).show();
177 stopService(new Intent(Alarms.ALARM_ALERT_ACTION));
181 private NotificationManager getNotificationManager() {
182 return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
185 // Dismiss the alarm.
186 private void dismiss(boolean killed) {
187 // The service told us that the alarm has been killed, do not modify
188 // the notification or stop the service.
190 // Cancel the notification and stop playing the alarm
191 NotificationManager nm = getNotificationManager();
192 nm.cancel(mAlarm.id);
193 stopService(new Intent(Alarms.ALARM_ALERT_ACTION));
199 * this is called when a second alarm is triggered while a
200 * previous alert window is still active.
203 protected void onNewIntent(Intent intent) {
204 super.onNewIntent(intent);
206 if (Log.LOGV) Log.v("AlarmAlert.OnNewIntent()");
208 mAlarm = intent.getParcelableExtra(Alarms.ALARM_INTENT_EXTRA);
214 protected void onStop() {
216 if (!isFinishing()) {
217 // Don't hang around.
223 public void onDestroy() {
225 if (Log.LOGV) Log.v("AlarmAlert.onDestroy()");
226 // No longer care about the alarm being killed.
227 unregisterReceiver(mReceiver);
231 public boolean dispatchKeyEvent(KeyEvent event) {
232 // Do this on key down to handle a few of the system keys.
233 boolean up = event.getAction() == KeyEvent.ACTION_UP;
234 switch (event.getKeyCode()) {
235 // Volume keys and camera keys dismiss the alarm
236 case KeyEvent.KEYCODE_VOLUME_UP:
237 case KeyEvent.KEYCODE_VOLUME_DOWN:
238 case KeyEvent.KEYCODE_CAMERA:
239 case KeyEvent.KEYCODE_FOCUS:
241 switch (mVolumeBehavior) {
258 return super.dispatchKeyEvent(event);
262 public void onBackPressed() {
263 // Don't allow back to dismiss. This method is overriden by AlarmAlert
264 // so that the dialog is dismissed.