OSDN Git Service

0173a127aaa073b44f43c7a26bd3ee685507e984
[android-x86/packages-apps-DeskClock.git] / src / com / android / deskclock / AlarmAlertFullScreen.java
1 /*
2  * Copyright (C) 2009 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.deskclock;
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. This activity is the full screen version which shows over the lock
46  * screen with the wallpaper as the background.
47  */
48 public class AlarmAlertFullScreen extends Activity {
49
50     // These defaults must match the values in res/xml/settings.xml
51     private static final String DEFAULT_SNOOZE = "10";
52     private static final String DEFAULT_VOLUME_BEHAVIOR = "2";
53     protected static final String SCREEN_OFF = "screen_off";
54
55     protected Alarm mAlarm;
56     private int mVolumeBehavior;
57
58     // Receives the ALARM_KILLED action from the AlarmKlaxon.
59     private BroadcastReceiver mReceiver = new BroadcastReceiver() {
60         @Override
61         public void onReceive(Context context, Intent intent) {
62             Alarm alarm = intent.getParcelableExtra(Alarms.ALARM_INTENT_EXTRA);
63             if (alarm != null && mAlarm.id == alarm.id) {
64                 dismiss(true);
65             }
66         }
67     };
68
69     @Override
70     protected void onCreate(Bundle icicle) {
71         super.onCreate(icicle);
72
73         mAlarm = getIntent().getParcelableExtra(Alarms.ALARM_INTENT_EXTRA);
74
75         // Get the volume/camera button behavior setting
76         final String vol =
77                 PreferenceManager.getDefaultSharedPreferences(this)
78                 .getString(SettingsActivity.KEY_VOLUME_BEHAVIOR,
79                         DEFAULT_VOLUME_BEHAVIOR);
80         mVolumeBehavior = Integer.parseInt(vol);
81
82         requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
83
84         final Window win = getWindow();
85         win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
86                 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
87         // Turn on the screen unless we are being launched from the AlarmAlert
88         // subclass.
89         if (!getIntent().getBooleanExtra(SCREEN_OFF, false)) {
90             win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
91                     | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
92         }
93
94         updateLayout();
95
96         // Register to get the alarm killed intent.
97         registerReceiver(mReceiver, new IntentFilter(Alarms.ALARM_KILLED));
98     }
99
100     private void setTitle() {
101         String label = mAlarm.getLabelOrDefault(this);
102         TextView title = (TextView) findViewById(R.id.alertTitle);
103         title.setText(label);
104     }
105
106     private void updateLayout() {
107         LayoutInflater inflater = LayoutInflater.from(this);
108
109         setContentView(inflater.inflate(R.layout.alarm_alert, null));
110
111         /* snooze behavior: pop a snooze confirmation view, kick alarm
112            manager. */
113         Button snooze = (Button) findViewById(R.id.snooze);
114         snooze.requestFocus();
115         snooze.setOnClickListener(new Button.OnClickListener() {
116             public void onClick(View v) {
117                 snooze();
118             }
119         });
120
121         /* dismiss button: close notification */
122         findViewById(R.id.dismiss).setOnClickListener(
123                 new Button.OnClickListener() {
124                     public void onClick(View v) {
125                         dismiss(false);
126                     }
127                 });
128
129         /* Set the title from the passed in alarm */
130         setTitle();
131     }
132
133     // Attempt to snooze this alert.
134     private void snooze() {
135         // Do not snooze if the snooze button is disabled.
136         if (!findViewById(R.id.snooze).isEnabled()) {
137             dismiss(false);
138             return;
139         }
140         final String snooze =
141                 PreferenceManager.getDefaultSharedPreferences(this)
142                 .getString(SettingsActivity.KEY_ALARM_SNOOZE, DEFAULT_SNOOZE);
143         int snoozeMinutes = Integer.parseInt(snooze);
144
145         final long snoozeTime = System.currentTimeMillis()
146                 + (1000 * 60 * snoozeMinutes);
147         Alarms.saveSnoozeAlert(AlarmAlertFullScreen.this, mAlarm.id,
148                 snoozeTime);
149
150         // Get the display time for the snooze and update the notification.
151         final Calendar c = Calendar.getInstance();
152         c.setTimeInMillis(snoozeTime);
153
154         // Append (snoozed) to the label.
155         String label = mAlarm.getLabelOrDefault(this);
156         label = getString(R.string.alarm_notify_snooze_label, label);
157
158         // Notify the user that the alarm has been snoozed.
159         Intent cancelSnooze = new Intent(this, AlarmReceiver.class);
160         cancelSnooze.setAction(Alarms.CANCEL_SNOOZE);
161         cancelSnooze.putExtra(Alarms.ALARM_ID, mAlarm.id);
162         PendingIntent broadcast =
163                 PendingIntent.getBroadcast(this, mAlarm.id, cancelSnooze, 0);
164         NotificationManager nm = getNotificationManager();
165         Notification n = new Notification(R.drawable.stat_notify_alarm,
166                 label, 0);
167         n.setLatestEventInfo(this, label,
168                 getString(R.string.alarm_notify_snooze_text,
169                     Alarms.formatTime(this, c)), broadcast);
170         n.flags |= Notification.FLAG_AUTO_CANCEL
171                 | Notification.FLAG_ONGOING_EVENT;
172         nm.notify(mAlarm.id, n);
173
174         String displayTime = getString(R.string.alarm_alert_snooze_set,
175                 snoozeMinutes);
176         // Intentionally log the snooze time for debugging.
177         Log.v(displayTime);
178
179         // Display the snooze minutes in a toast.
180         Toast.makeText(AlarmAlertFullScreen.this, displayTime,
181                 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 onResume() {
220         super.onResume();
221         // If the alarm was deleted at some point, disable snooze.
222         if (Alarms.getAlarm(getContentResolver(), mAlarm.id) == null) {
223             Button snooze = (Button) findViewById(R.id.snooze);
224             snooze.setEnabled(false);
225         }
226     }
227
228     @Override
229     protected void onStop() {
230         super.onStop();
231         if (!isFinishing()) {
232             // Don't hang around.
233             finish();
234         }
235     }
236     
237     @Override
238     public void onDestroy() {
239         super.onDestroy();
240         if (Log.LOGV) Log.v("AlarmAlert.onDestroy()");
241         // No longer care about the alarm being killed.
242         unregisterReceiver(mReceiver);
243     }
244
245     @Override
246     public boolean dispatchKeyEvent(KeyEvent event) {
247         // Do this on key down to handle a few of the system keys.
248         boolean up = event.getAction() == KeyEvent.ACTION_UP;
249         switch (event.getKeyCode()) {
250             // Volume keys and camera keys dismiss the alarm
251             case KeyEvent.KEYCODE_VOLUME_UP:
252             case KeyEvent.KEYCODE_VOLUME_DOWN:
253             case KeyEvent.KEYCODE_CAMERA:
254             case KeyEvent.KEYCODE_FOCUS:
255                 if (up) {
256                     switch (mVolumeBehavior) {
257                         case 1:
258                             snooze();
259                             break;
260
261                         case 2:
262                             dismiss(false);
263                             break;
264
265                         default:
266                             break;
267                     }
268                 }
269                 return true;
270             default:
271                 break;
272         }
273         return super.dispatchKeyEvent(event);
274     }
275
276     @Override
277     public void onBackPressed() {
278         // Don't allow back to dismiss. This method is overriden by AlarmAlert
279         // so that the dialog is dismissed.
280         return;
281     }
282 }