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.Activity;
20 import android.content.Intent;
21 import android.content.SharedPreferences;
22 import android.content.res.Configuration;
23 import android.os.Bundle;
24 import android.view.KeyEvent;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.view.LayoutInflater;
28 import android.view.Window;
29 import android.view.WindowManager;
30 import android.widget.Button;
31 import android.widget.Toast;
32 import android.widget.TextView;
34 import java.util.Calendar;
37 * Alarm Clock alarm alert: pops visible indicator and plays alarm
40 public class AlarmAlert extends Activity {
42 private static final int SNOOZE_MINUTES = 10;
43 private static final int UNKNOWN = 0;
44 private static final int SNOOZE = 1;
45 private static final int DISMISS = 2;
46 private static final int KILLED = 3;
47 private Button mSnoozeButton;
48 private int mState = UNKNOWN;
50 private AlarmKlaxon mKlaxon;
52 private String mLabel;
55 protected void onCreate(Bundle icicle) {
56 super.onCreate(icicle);
58 // Maintain a lock during the playback of the alarm. This lock may have
59 // already been acquired in AlarmReceiver. If the process was killed,
60 // the global wake lock is gone. Acquire again just to be sure.
61 // AlarmAlertWakeLock.acquire(this);
63 /* FIXME Intentionally verbose: always log this until we've
64 fully debugged the app failing to start up */
65 Log.v("AlarmAlert.onCreate()");
67 // Popup alert over black screen
68 WindowManager.LayoutParams lp = getWindow().getAttributes();
69 lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
70 lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
71 // XXX DO NOT COPY THIS!!! THIS IS BOGUS! Making an activity have
72 // a system alert type is completely broken, because the activity
73 // manager will still hide/show it as if it is part of the normal
74 // activity stack. If this is really what you want and you want it
75 // to work correctly, you should create and show your own custom window.
76 lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
78 Window win = getWindow();
79 win.setAttributes(lp);
80 // TODO Make the activity full screen for FLAG_SHOW_WHEN_LOCKED to bypass
82 win.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND |
83 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
85 Intent i = getIntent();
86 mAlarmId = i.getIntExtra(Alarms.ID, -1);
88 mKlaxon = new AlarmKlaxon();
89 mKlaxon.postPlay(this, mAlarmId);
91 /* Set the title from the passed in label */
92 setTitleFromIntent(i);
94 /* allow next alarm to trigger while this activity is
96 Alarms.disableSnoozeAlert(AlarmAlert.this);
97 Alarms.disableAlert(AlarmAlert.this, mAlarmId);
98 Alarms.setNextAlert(this);
100 mKlaxon.setKillerCallback(new AlarmKlaxon.KillerCallback() {
101 public void onKilled() {
102 if (Log.LOGV) Log.v("onKilled()");
103 updateSilencedText();
105 /* don't allow snooze */
106 mSnoozeButton.setEnabled(false);
108 // Dismiss the alarm but mark the state as killed so if the
109 // config changes, we show the silenced message and disable
119 private void setTitleFromIntent(Intent i) {
120 mLabel = i.getStringExtra(Alarms.LABEL);
121 if (mLabel == null || mLabel.length() == 0) {
122 mLabel = getString(R.string.default_label);
127 private void updateSilencedText() {
128 TextView silenced = (TextView) findViewById(R.id.silencedText);
129 silenced.setText(getString(R.string.alarm_alert_alert_silenced,
130 AlarmKlaxon.ALARM_TIMEOUT_SECONDS / 60));
131 silenced.setVisibility(View.VISIBLE);
134 private void updateLayout() {
135 setContentView(R.layout.alarm_alert);
138 LayoutInflater mFactory = LayoutInflater.from(this);
139 SharedPreferences settings =
140 getSharedPreferences(AlarmClock.PREFERENCES, 0);
141 int face = settings.getInt(AlarmClock.PREF_CLOCK_FACE, 0);
142 if (face < 0 || face >= AlarmClock.CLOCKS.length) {
146 (View) mFactory.inflate(AlarmClock.CLOCKS[face], null);
147 ViewGroup clockView = (ViewGroup) findViewById(R.id.clockView);
148 clockView.addView(clockLayout);
149 if (clockLayout instanceof DigitalClock) {
150 ((DigitalClock) clockLayout).setAnimate();
153 /* snooze behavior: pop a snooze confirmation view, kick alarm
155 mSnoozeButton = (Button) findViewById(R.id.snooze);
156 mSnoozeButton.requestFocus();
157 // If this was a configuration change, keep the silenced text if the
159 if (mState == KILLED) {
160 updateSilencedText();
161 mSnoozeButton.setEnabled(false);
163 mSnoozeButton.setOnClickListener(new Button.OnClickListener() {
164 public void onClick(View v) {
171 /* dismiss button: close notification */
172 findViewById(R.id.dismiss).setOnClickListener(
173 new Button.OnClickListener() {
174 public void onClick(View v) {
181 // Attempt to snooze this alert.
182 private void snooze() {
183 if (mState != UNKNOWN) {
186 // If the next alarm is set for sooner than the snooze interval, don't
187 // snooze. Instead, toast the user that the snooze will not be set.
188 final long snoozeTime = System.currentTimeMillis()
189 + (1000 * 60 * SNOOZE_MINUTES);
190 final long nextAlarm =
191 Alarms.calculateNextAlert(AlarmAlert.this).getAlert();
192 String displayTime = null;
193 if (nextAlarm < snoozeTime) {
194 final Calendar c = Calendar.getInstance();
195 c.setTimeInMillis(nextAlarm);
196 displayTime = getString(R.string.alarm_alert_snooze_not_set,
197 Alarms.formatTime(AlarmAlert.this, c));
200 Alarms.saveSnoozeAlert(AlarmAlert.this, mAlarmId, snoozeTime,
202 Alarms.setNextAlert(AlarmAlert.this);
203 displayTime = getString(R.string.alarm_alert_snooze_set,
207 // Intentionally log the snooze time for debugging.
209 // Display the snooze minutes in a toast.
210 Toast.makeText(AlarmAlert.this, displayTime, Toast.LENGTH_LONG).show();
211 mKlaxon.stop(this, mState == SNOOZE);
215 // Dismiss the alarm.
216 private void dismiss() {
217 if (mState != UNKNOWN) {
221 mKlaxon.stop(this, false);
226 * this is called when a second alarm is triggered while a
227 * previous alert window is still active.
230 protected void onNewIntent(Intent intent) {
231 super.onNewIntent(intent);
232 if (Log.LOGV) Log.v("AlarmAlert.OnNewIntent()");
234 mSnoozeButton.setEnabled(true);
236 mAlarmId = intent.getIntExtra(Alarms.ID, -1);
237 // Play the new alarm sound.
238 mKlaxon.postPlay(this, mAlarmId);
240 setTitleFromIntent(intent);
242 /* unset silenced message */
243 TextView silenced = (TextView)findViewById(R.id.silencedText);
244 silenced.setVisibility(View.GONE);
246 Alarms.setNextAlert(this);
251 protected void onResume() {
253 if (Log.LOGV) Log.v("AlarmAlert.onResume()");
254 AlarmAlertWakeLock.acquire(this);
258 protected void onStop() {
260 if (Log.LOGV) Log.v("AlarmAlert.onStop()");
261 // As a last resort, try to snooze if this activity is stopped.
263 // We might have been killed by the KillerCallback so always release
264 // the lock and keyguard.
269 public void onConfigurationChanged(Configuration config) {
270 super.onConfigurationChanged(config);
275 public boolean dispatchKeyEvent(KeyEvent event) {
276 // Do this on key down to handle a few of the system keys. Only handle
277 // the snooze and dismiss this alert if the state is unknown.
278 boolean up = event.getAction() == KeyEvent.ACTION_UP;
279 boolean dismiss = false;
280 switch (event.getKeyCode()) {
281 case KeyEvent.KEYCODE_DPAD_UP:
282 case KeyEvent.KEYCODE_DPAD_DOWN:
283 case KeyEvent.KEYCODE_DPAD_LEFT:
284 case KeyEvent.KEYCODE_DPAD_RIGHT:
285 case KeyEvent.KEYCODE_DPAD_CENTER:
286 // Ignore ENDCALL because we do not receive the event if the screen
287 // is on. However, we do receive the key up for ENDCALL if the
289 case KeyEvent.KEYCODE_ENDCALL:
291 // Volume keys dismiss the alarm
292 case KeyEvent.KEYCODE_VOLUME_UP:
293 case KeyEvent.KEYCODE_VOLUME_DOWN:
295 // All other keys will snooze the alarm
297 // Check for UNKNOWN here so that we intercept both key events
298 // and prevent the volume keys from triggering their default
300 if (mState == UNKNOWN && up) {
310 return super.dispatchKeyEvent(event);
314 * release wake and keyguard locks
316 private synchronized void releaseLocks() {
317 AlarmAlertWakeLock.release();