OSDN Git Service

auto import from //branches/cupcake/...@131421
[android-x86/packages-apps-DeskClock.git] / src / com / android / alarmclock / AlarmAlert.java
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.alarmclock;
18
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;
34
35 import java.util.Calendar;
36
37 /**
38  * Alarm Clock alarm alert: pops visible indicator and plays alarm
39  * tone
40  */
41 public class AlarmAlert extends Activity {
42
43     private final static int SNOOZE_MINUTES = 10;
44
45     private KeyguardManager mKeyguardManager;
46     private KeyguardManager.KeyguardLock mKeyguardLock = null;
47     private Button mSnoozeButton;
48     private boolean mSnoozed;
49
50     private AlarmKlaxon mKlaxon;
51     private int mAlarmId;
52
53     @Override
54     protected void onCreate(Bundle icicle) {
55         super.onCreate(icicle);
56
57         /* FIXME Intentionally verbose: always log this until we've
58            fully debugged the app failing to start up */
59         Log.v("AlarmAlert.onCreate()");
60
61         mKlaxon = AlarmKlaxon.getInstance();
62
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;
73         lp.token = null;
74         getWindow().setAttributes(lp);
75         getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
76
77         mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
78
79         mAlarmId = getIntent().getIntExtra(Alarms.ID, -1);
80
81         /* allow next alarm to trigger while this activity is
82            active */
83         Alarms.disableSnoozeAlert(AlarmAlert.this);
84         Alarms.disableAlert(AlarmAlert.this, mAlarmId);
85         Alarms.setNextAlert(this);
86
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);
91                 silenced.setText(
92                         getString(R.string.alarm_alert_alert_silenced,
93                                   AlarmKlaxon.ALARM_TIMEOUT_SECONDS / 60));
94                 silenced.setVisibility(View.VISIBLE);
95
96                 /* don't allow snooze */
97                 mSnoozeButton.setEnabled(false);
98
99                 mKlaxon.stop(AlarmAlert.this, mSnoozed);
100                 releaseLocks();
101             }
102         });
103
104         mKlaxon.restoreInstanceState(this, icicle);
105
106         updateLayout();
107     }
108
109     private void updateLayout() {
110         setContentView(R.layout.alarm_alert);
111
112         /* set clock face */
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) {
118             face = 0;
119         }
120         View clockLayout =
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();
126         }
127
128         /* snooze behavior: pop a snooze confirmation view, kick alarm
129            manager. */
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));
146                 } else {
147                     Alarms.saveSnoozeAlert(AlarmAlert.this, mAlarmId,
148                             snoozeTarget);
149                     Alarms.setNextAlert(AlarmAlert.this);
150                     mSnoozed = true;
151                     displayTime = getString(R.string.alarm_alert_snooze_set,
152                             SNOOZE_MINUTES);
153                 }
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);
158                 releaseLocks();
159                 finish();
160             }
161         });
162
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);
168                         releaseLocks();
169                         finish();
170                     }
171                 });
172     }
173
174     /**
175      * this is called when a second alarm is triggered while a
176      * previous alert window is still active.
177      */
178     @Override
179     protected void onNewIntent(Intent intent) {
180         super.onNewIntent(intent);
181         if (Log.LOGV) Log.v("AlarmAlert.OnNewIntent()");
182         mSnoozeButton.setEnabled(true);
183         disableKeyguard();
184
185         mAlarmId = intent.getIntExtra(Alarms.ID, -1);
186
187         /* unset silenced message */
188         TextView silenced = (TextView)findViewById(R.id.silencedText);
189         silenced.setVisibility(View.GONE);
190
191         Alarms.setNextAlert(this);
192         setIntent(intent);
193     }
194
195     @Override
196     protected void onResume() {
197         super.onResume();
198         if (Log.LOGV) Log.v("AlarmAlert.onResume()");
199         disableKeyguard();
200     }
201
202     @Override
203     protected void onStop() {
204         super.onStop();
205         if (Log.LOGV) Log.v("AlarmAlert.onStop()");
206         mKlaxon.stop(this, mSnoozed);
207         releaseLocks();
208     }
209
210     @Override
211     protected void onSaveInstanceState(Bundle icicle) {
212         mKlaxon.onSaveInstanceState(icicle);
213     }
214
215     @Override
216     public void onConfigurationChanged(Configuration config) {
217         super.onConfigurationChanged(config);
218         updateLayout();
219     }
220
221     private synchronized void enableKeyguard() {
222         if (mKeyguardLock != null) {
223             mKeyguardLock.reenableKeyguard();
224             mKeyguardLock = null;
225         }
226     }
227
228     private synchronized void disableKeyguard() {
229         if (mKeyguardLock == null) {
230             mKeyguardLock = mKeyguardManager.newKeyguardLock(Log.LOGTAG);
231             mKeyguardLock.disableKeyguard();
232         }
233     }
234
235     /**
236      * release wake and keyguard locks
237      */
238     private synchronized void releaseLocks() {
239         AlarmAlertWakeLock.release();
240         enableKeyguard();
241     }
242 }