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.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
47 public class AlarmAlert extends Activity {
49 // These defaults must match the values in res/xml/settings.xml
50 private static final String DEFAULT_SNOOZE = "10";
51 private static final String DEFAULT_VOLUME_BEHAVIOR = "2";
54 private int mVolumeBehavior;
56 // Receives the ALARM_KILLED action from the AlarmKlaxon.
57 private BroadcastReceiver mReceiver = new BroadcastReceiver() {
59 public void onReceive(Context context, Intent intent) {
60 Alarm alarm = intent.getParcelableExtra(Alarms.ALARM_INTENT_EXTRA);
61 if (mAlarm.id == alarm.id) {
68 protected void onCreate(Bundle icicle) {
69 super.onCreate(icicle);
70 AlarmClock.setVolumeControlForPlatform(this);
71 mAlarm = getIntent().getParcelableExtra(Alarms.ALARM_INTENT_EXTRA);
73 // Get the volume/camera button behavior setting
75 PreferenceManager.getDefaultSharedPreferences(this)
76 .getString(SettingsActivity.KEY_VOLUME_BEHAVIOR,
77 DEFAULT_VOLUME_BEHAVIOR);
78 mVolumeBehavior = Integer.parseInt(vol);
80 requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
81 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
82 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
83 | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
84 | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
87 // Register to get the alarm killed intent.
88 registerReceiver(mReceiver, new IntentFilter(Alarms.ALARM_KILLED));
91 private void setTitle() {
92 String label = mAlarm.getLabelOrDefault(this);
93 TextView title = (TextView) findViewById(R.id.alertTitle);
97 // This method is overwritten in AlarmAlertFullScreen in order to show a
98 // full activity with the wallpaper as the background.
99 protected View inflateView(LayoutInflater inflater) {
100 return inflater.inflate(R.layout.alarm_alert, null);
103 private void updateLayout() {
104 LayoutInflater inflater = LayoutInflater.from(this);
106 setContentView(inflateView(inflater));
108 /* snooze behavior: pop a snooze confirmation view, kick alarm
110 Button snooze = (Button) findViewById(R.id.snooze);
111 snooze.requestFocus();
112 snooze.setOnClickListener(new Button.OnClickListener() {
113 public void onClick(View v) {
118 /* dismiss button: close notification */
119 findViewById(R.id.dismiss).setOnClickListener(
120 new Button.OnClickListener() {
121 public void onClick(View v) {
126 /* Set the title from the passed in alarm */
130 // Attempt to snooze this alert.
131 private void snooze() {
132 final String snooze =
133 PreferenceManager.getDefaultSharedPreferences(this)
134 .getString(SettingsActivity.KEY_ALARM_SNOOZE, DEFAULT_SNOOZE);
135 int snoozeMinutes = Integer.parseInt(snooze);
137 final long snoozeTime = System.currentTimeMillis()
138 + (1000 * 60 * snoozeMinutes);
139 Alarms.saveSnoozeAlert(AlarmAlert.this, mAlarm.id, snoozeTime);
141 // Get the display time for the snooze and update the notification.
142 final Calendar c = Calendar.getInstance();
143 c.setTimeInMillis(snoozeTime);
145 // Append (snoozed) to the label.
146 String label = mAlarm.getLabelOrDefault(this);
147 label = getString(R.string.alarm_notify_snooze_label, label);
149 // Notify the user that the alarm has been snoozed.
150 Intent cancelSnooze = new Intent(this, AlarmReceiver.class);
151 cancelSnooze.setAction(Alarms.CANCEL_SNOOZE);
152 cancelSnooze.putExtra(Alarms.ALARM_ID, mAlarm.id);
153 PendingIntent broadcast =
154 PendingIntent.getBroadcast(this, mAlarm.id, cancelSnooze, 0);
155 NotificationManager nm = getNotificationManager();
156 Notification n = new Notification(R.drawable.stat_notify_alarm,
158 n.setLatestEventInfo(this, label,
159 getString(R.string.alarm_notify_snooze_text,
160 Alarms.formatTime(this, c)), broadcast);
161 n.flags |= Notification.FLAG_AUTO_CANCEL
162 | Notification.FLAG_ONGOING_EVENT;
163 nm.notify(mAlarm.id, n);
165 String displayTime = getString(R.string.alarm_alert_snooze_set,
167 // Intentionally log the snooze time for debugging.
170 // Display the snooze minutes in a toast.
171 Toast.makeText(AlarmAlert.this, displayTime, Toast.LENGTH_LONG).show();
172 stopService(new Intent(Alarms.ALARM_ALERT_ACTION));
176 private NotificationManager getNotificationManager() {
177 return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
180 // Dismiss the alarm.
181 private void dismiss(boolean killed) {
182 // The service told us that the alarm has been killed, do not modify
183 // the notification or stop the service.
185 // Cancel the notification and stop playing the alarm
186 NotificationManager nm = getNotificationManager();
187 nm.cancel(mAlarm.id);
188 stopService(new Intent(Alarms.ALARM_ALERT_ACTION));
194 * this is called when a second alarm is triggered while a
195 * previous alert window is still active.
198 protected void onNewIntent(Intent intent) {
199 super.onNewIntent(intent);
201 if (Log.LOGV) Log.v("AlarmAlert.OnNewIntent()");
203 mAlarm = intent.getParcelableExtra(Alarms.ALARM_INTENT_EXTRA);
209 protected void onStop() {
211 // Don't hang around.
216 public void onDestroy() {
218 if (Log.LOGV) Log.v("AlarmAlert.onDestroy()");
219 // No longer care about the alarm being killed.
220 unregisterReceiver(mReceiver);
224 public boolean dispatchKeyEvent(KeyEvent event) {
225 // Do this on key down to handle a few of the system keys.
226 boolean up = event.getAction() == KeyEvent.ACTION_UP;
227 switch (event.getKeyCode()) {
228 // Volume keys and camera keys dismiss the alarm
229 case KeyEvent.KEYCODE_VOLUME_UP:
230 case KeyEvent.KEYCODE_VOLUME_DOWN:
231 case KeyEvent.KEYCODE_CAMERA:
232 case KeyEvent.KEYCODE_FOCUS:
234 switch (mVolumeBehavior) {
251 return super.dispatchKeyEvent(event);