OSDN Git Service

Update to use new unlock APIs, new wallpaper theme.
[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.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;
40
41 import java.util.Calendar;
42
43 /**
44  * Alarm Clock alarm alert: pops visible indicator and plays alarm
45  * tone
46  */
47 public class AlarmAlert extends Activity {
48
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";
52
53     private Alarm mAlarm;
54     private int mVolumeBehavior;
55
56     // Receives the ALARM_KILLED action from the AlarmKlaxon.
57     private BroadcastReceiver mReceiver = new BroadcastReceiver() {
58         @Override
59         public void onReceive(Context context, Intent intent) {
60             Alarm alarm = intent.getParcelableExtra(Alarms.ALARM_INTENT_EXTRA);
61             if (mAlarm.id == alarm.id) {
62                 dismiss(true);
63             }
64         }
65     };
66
67     @Override
68     protected void onCreate(Bundle icicle) {
69         super.onCreate(icicle);
70
71         mAlarm = getIntent().getParcelableExtra(Alarms.ALARM_INTENT_EXTRA);
72
73         // Get the volume/camera button behavior setting
74         final String vol =
75                 PreferenceManager.getDefaultSharedPreferences(this)
76                 .getString(SettingsActivity.KEY_VOLUME_BEHAVIOR,
77                         DEFAULT_VOLUME_BEHAVIOR);
78         mVolumeBehavior = Integer.parseInt(vol);
79
80         requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
81         updateLayout();
82
83         // Register to get the alarm killed intent.
84         registerReceiver(mReceiver, new IntentFilter(Alarms.ALARM_KILLED));
85     }
86
87     private void setTitle() {
88         String label = mAlarm.getLabelOrDefault(this);
89         TextView title = (TextView) findViewById(R.id.alertTitle);
90         title.setText(label);
91     }
92
93     // This method is overwritten in AlarmAlertFullScreen in order to show a
94     // full activity with the wallpaper as the background.
95     protected View inflateView(LayoutInflater inflater) {
96         return inflater.inflate(R.layout.alarm_alert, null);
97     }
98
99     private void updateLayout() {
100         LayoutInflater inflater = LayoutInflater.from(this);
101
102         setContentView(inflateView(inflater));
103
104         /* set clock face */
105         SharedPreferences settings =
106                 getSharedPreferences(AlarmClock.PREFERENCES, 0);
107         int face = settings.getInt(AlarmClock.PREF_CLOCK_FACE, 0);
108         if (face < 0 || face >= AlarmClock.CLOCKS.length) {
109             face = 0;
110         }
111         ViewGroup clockView = (ViewGroup) findViewById(R.id.clockView);
112         inflater.inflate(AlarmClock.CLOCKS[face], clockView);
113         View clockLayout = findViewById(R.id.clock);
114         if (clockLayout instanceof DigitalClock) {
115             ((DigitalClock) clockLayout).setAnimate();
116         }
117
118         /* snooze behavior: pop a snooze confirmation view, kick alarm
119            manager. */
120         Button snooze = (Button) findViewById(R.id.snooze);
121         snooze.requestFocus();
122         snooze.setOnClickListener(new Button.OnClickListener() {
123             public void onClick(View v) {
124                 snooze();
125             }
126         });
127
128         /* dismiss button: close notification */
129         findViewById(R.id.dismiss).setOnClickListener(
130                 new Button.OnClickListener() {
131                     public void onClick(View v) {
132                         dismiss(false);
133                     }
134                 });
135
136         /* Set the title from the passed in alarm */
137         setTitle();
138     }
139
140     // Attempt to snooze this alert.
141     private void snooze() {
142         final String snooze =
143                 PreferenceManager.getDefaultSharedPreferences(this)
144                 .getString(SettingsActivity.KEY_ALARM_SNOOZE, DEFAULT_SNOOZE);
145         int snoozeMinutes = Integer.parseInt(snooze);
146
147         final long snoozeTime = System.currentTimeMillis()
148                 + (1000 * 60 * snoozeMinutes);
149         Alarms.saveSnoozeAlert(AlarmAlert.this, mAlarm.id, snoozeTime);
150
151         // Get the display time for the snooze and update the notification.
152         final Calendar c = Calendar.getInstance();
153         c.setTimeInMillis(snoozeTime);
154
155         // Append (snoozed) to the label.
156         String label = mAlarm.getLabelOrDefault(this);
157         label = getString(R.string.alarm_notify_snooze_label, label);
158
159         // Notify the user that the alarm has been snoozed.
160         Intent cancelSnooze = new Intent(this, AlarmReceiver.class);
161         cancelSnooze.setAction(Alarms.CANCEL_SNOOZE);
162         cancelSnooze.putExtra(Alarms.ALARM_ID, mAlarm.id);
163         PendingIntent broadcast =
164                 PendingIntent.getBroadcast(this, mAlarm.id, cancelSnooze, 0);
165         NotificationManager nm = getNotificationManager();
166         Notification n = new Notification(R.drawable.stat_notify_alarm,
167                 label, 0);
168         n.setLatestEventInfo(this, label,
169                 getString(R.string.alarm_notify_snooze_text,
170                     Alarms.formatTime(this, c)), broadcast);
171         n.deleteIntent = broadcast;
172         n.flags |= Notification.FLAG_AUTO_CANCEL;
173         nm.notify(mAlarm.id, n);
174
175         String displayTime = getString(R.string.alarm_alert_snooze_set,
176                 snoozeMinutes);
177         // Intentionally log the snooze time for debugging.
178         Log.v(displayTime);
179
180         // Display the snooze minutes in a toast.
181         Toast.makeText(AlarmAlert.this, displayTime, Toast.LENGTH_LONG).show();
182         stopService(new Intent(Alarms.ALARM_ALERT_ACTION));
183         finish();
184     }
185
186     private NotificationManager getNotificationManager() {
187         return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
188     }
189
190     // Dismiss the alarm.
191     private void dismiss(boolean killed) {
192         // The service told us that the alarm has been killed, do not modify
193         // the notification or stop the service.
194         if (!killed) {
195             // Cancel the notification and stop playing the alarm
196             NotificationManager nm = getNotificationManager();
197             nm.cancel(mAlarm.id);
198             stopService(new Intent(Alarms.ALARM_ALERT_ACTION));
199         }
200         finish();
201     }
202
203     /**
204      * this is called when a second alarm is triggered while a
205      * previous alert window is still active.
206      */
207     @Override
208     protected void onNewIntent(Intent intent) {
209         super.onNewIntent(intent);
210
211         if (Log.LOGV) Log.v("AlarmAlert.OnNewIntent()");
212
213         mAlarm = intent.getParcelableExtra(Alarms.ALARM_INTENT_EXTRA);
214
215         setTitle();
216     }
217
218     @Override
219     protected void onStop() {
220         super.onStop();
221         // Don't hang around.
222         finish();
223     }
224     
225     @Override
226     public void onDestroy() {
227         super.onDestroy();
228         if (Log.LOGV) Log.v("AlarmAlert.onDestroy()");
229         // No longer care about the alarm being killed.
230         unregisterReceiver(mReceiver);
231     }
232
233     @Override
234     public boolean dispatchKeyEvent(KeyEvent event) {
235         // Do this on key down to handle a few of the system keys.
236         boolean up = event.getAction() == KeyEvent.ACTION_UP;
237         switch (event.getKeyCode()) {
238             // Volume keys and camera keys dismiss the alarm
239             case KeyEvent.KEYCODE_VOLUME_UP:
240             case KeyEvent.KEYCODE_VOLUME_DOWN:
241             case KeyEvent.KEYCODE_CAMERA:
242             case KeyEvent.KEYCODE_FOCUS:
243                 if (up) {
244                     switch (mVolumeBehavior) {
245                         case 1:
246                             snooze();
247                             break;
248
249                         case 2:
250                             dismiss(false);
251                             break;
252
253                         default:
254                             break;
255                     }
256                 }
257                 return true;
258             default:
259                 break;
260         }
261         return super.dispatchKeyEvent(event);
262     }
263 }