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.app.KeyguardManager;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.SharedPreferences;
24 import android.content.res.Configuration;
25 import android.graphics.PixelFormat;
26 import android.os.Bundle;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.view.LayoutInflater;
30 import android.view.WindowManager;
31 import android.widget.Button;
32 import android.widget.Toast;
33 import android.widget.TextView;
35 import java.util.Calendar;
38 * Alarm Clock alarm alert: pops visible indicator and plays alarm
41 public class AlarmAlert extends Activity {
43 private final static int SNOOZE_MINUTES = 10;
45 private KeyguardManager mKeyguardManager;
46 private KeyguardManager.KeyguardLock mKeyguardLock = null;
47 private Button mSnoozeButton;
48 private boolean mSnoozed;
50 private AlarmKlaxon mKlaxon;
54 protected void onCreate(Bundle icicle) {
55 super.onCreate(icicle);
57 /* FIXME Intentionally verbose: always log this until we've
58 fully debugged the app failing to start up */
59 Log.v("AlarmAlert.onCreate()");
61 mKlaxon = AlarmKlaxon.getInstance();
63 // Popup alert over black screen
64 WindowManager.LayoutParams lp = getWindow().getAttributes();
65 lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
66 lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
67 // XXX DO NOT COPY THIS!!! THIS IS BOGUS! Making an activity have
68 // a system alert type is completely broken, because the activity
69 // manager will still hide/show it as if it is part of the normal
70 // activity stack. If this is really what you want and you want it
71 // to work correctly, you should create and show your own custom window.
72 lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
74 getWindow().setAttributes(lp);
75 getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
77 mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
79 mAlarmId = getIntent().getIntExtra(Alarms.ID, -1);
81 /* allow next alarm to trigger while this activity is
83 Alarms.disableSnoozeAlert(AlarmAlert.this);
84 Alarms.disableAlert(AlarmAlert.this, mAlarmId);
85 Alarms.setNextAlert(this);
87 mKlaxon.setKillerCallback(new AlarmKlaxon.KillerCallback() {
88 public void onKilled() {
89 if (Log.LOGV) Log.v("onKilled()");
90 TextView silenced = (TextView)findViewById(R.id.silencedText);
92 getString(R.string.alarm_alert_alert_silenced,
93 AlarmKlaxon.ALARM_TIMEOUT_SECONDS / 60));
94 silenced.setVisibility(View.VISIBLE);
96 /* don't allow snooze */
97 mSnoozeButton.setEnabled(false);
99 mKlaxon.stop(AlarmAlert.this, mSnoozed);
104 mKlaxon.restoreInstanceState(this, icicle);
109 private void updateLayout() {
110 setContentView(R.layout.alarm_alert);
113 LayoutInflater mFactory = LayoutInflater.from(this);
114 SharedPreferences settings =
115 getSharedPreferences(AlarmClock.PREFERENCES, 0);
116 int face = settings.getInt(AlarmClock.PREF_CLOCK_FACE, 0);
117 if (face < 0 || face >= AlarmClock.CLOCKS.length) {
121 (View) mFactory.inflate(AlarmClock.CLOCKS[face], null);
122 ViewGroup clockView = (ViewGroup) findViewById(R.id.clockView);
123 clockView.addView(clockLayout);
124 if (clockLayout instanceof DigitalClock) {
125 ((DigitalClock) clockLayout).setAnimate();
128 /* snooze behavior: pop a snooze confirmation view, kick alarm
130 mSnoozeButton = (Button) findViewById(R.id.snooze);
131 mSnoozeButton.requestFocus();
132 mSnoozeButton.setOnClickListener(new Button.OnClickListener() {
133 public void onClick(View v) {
134 // If next alarm is set for sooner than the snooze interval,
135 // don't snooze: instead toast user that snooze will not be set
136 final long snoozeTarget = System.currentTimeMillis()
137 + (1000 * 60 * SNOOZE_MINUTES);
138 final long nextAlarm =
139 Alarms.calculateNextAlert(AlarmAlert.this).getAlert();
140 String displayTime = null;
141 if (nextAlarm < snoozeTarget) {
142 Calendar c = Calendar.getInstance();
143 c.setTimeInMillis(nextAlarm);
144 displayTime = getString(R.string.alarm_alert_snooze_set,
145 Alarms.formatTime(AlarmAlert.this, c));
147 Alarms.saveSnoozeAlert(AlarmAlert.this, mAlarmId,
149 Alarms.setNextAlert(AlarmAlert.this);
151 displayTime = getString(R.string.alarm_alert_snooze_set,
154 // Display the snooze minutes in a toast.
155 Toast.makeText(AlarmAlert.this, displayTime,
156 Toast.LENGTH_LONG).show();
157 mKlaxon.stop(AlarmAlert.this, mSnoozed);
163 /* dismiss button: close notification */
164 findViewById(R.id.dismiss).setOnClickListener(
165 new Button.OnClickListener() {
166 public void onClick(View v) {
167 mKlaxon.stop(AlarmAlert.this, mSnoozed);
175 * this is called when a second alarm is triggered while a
176 * previous alert window is still active.
179 protected void onNewIntent(Intent intent) {
180 super.onNewIntent(intent);
181 if (Log.LOGV) Log.v("AlarmAlert.OnNewIntent()");
182 mSnoozeButton.setEnabled(true);
185 mAlarmId = intent.getIntExtra(Alarms.ID, -1);
187 /* unset silenced message */
188 TextView silenced = (TextView)findViewById(R.id.silencedText);
189 silenced.setVisibility(View.GONE);
191 Alarms.setNextAlert(this);
196 protected void onResume() {
198 if (Log.LOGV) Log.v("AlarmAlert.onResume()");
203 protected void onStop() {
205 if (Log.LOGV) Log.v("AlarmAlert.onStop()");
206 mKlaxon.stop(this, mSnoozed);
211 protected void onSaveInstanceState(Bundle icicle) {
212 mKlaxon.onSaveInstanceState(icicle);
216 public void onConfigurationChanged(Configuration config) {
217 super.onConfigurationChanged(config);
221 private synchronized void enableKeyguard() {
222 if (mKeyguardLock != null) {
223 mKeyguardLock.reenableKeyguard();
224 mKeyguardLock = null;
228 private synchronized void disableKeyguard() {
229 if (mKeyguardLock == null) {
230 mKeyguardLock = mKeyguardManager.newKeyguardLock(Log.LOGTAG);
231 mKeyguardLock.disableKeyguard();
236 * release wake and keyguard locks
238 private synchronized void releaseLocks() {
239 AlarmAlertWakeLock.release();