OSDN Git Service

am cda260fb: (-s ours) Import revised translations. DO NOT MERGE
[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         final String snooze =
136                 PreferenceManager.getDefaultSharedPreferences(this)
137                 .getString(SettingsActivity.KEY_ALARM_SNOOZE, DEFAULT_SNOOZE);
138         int snoozeMinutes = Integer.parseInt(snooze);
139
140         final long snoozeTime = System.currentTimeMillis()
141                 + (1000 * 60 * snoozeMinutes);
142         Alarms.saveSnoozeAlert(AlarmAlertFullScreen.this, mAlarm.id,
143                 snoozeTime);
144
145         // Get the display time for the snooze and update the notification.
146         final Calendar c = Calendar.getInstance();
147         c.setTimeInMillis(snoozeTime);
148
149         // Append (snoozed) to the label.
150         String label = mAlarm.getLabelOrDefault(this);
151         label = getString(R.string.alarm_notify_snooze_label, label);
152
153         // Notify the user that the alarm has been snoozed.
154         Intent cancelSnooze = new Intent(this, AlarmReceiver.class);
155         cancelSnooze.setAction(Alarms.CANCEL_SNOOZE);
156         cancelSnooze.putExtra(Alarms.ALARM_ID, mAlarm.id);
157         PendingIntent broadcast =
158                 PendingIntent.getBroadcast(this, mAlarm.id, cancelSnooze, 0);
159         NotificationManager nm = getNotificationManager();
160         Notification n = new Notification(R.drawable.stat_notify_alarm,
161                 label, 0);
162         n.setLatestEventInfo(this, label,
163                 getString(R.string.alarm_notify_snooze_text,
164                     Alarms.formatTime(this, c)), broadcast);
165         n.flags |= Notification.FLAG_AUTO_CANCEL
166                 | Notification.FLAG_ONGOING_EVENT;
167         nm.notify(mAlarm.id, n);
168
169         String displayTime = getString(R.string.alarm_alert_snooze_set,
170                 snoozeMinutes);
171         // Intentionally log the snooze time for debugging.
172         Log.v(displayTime);
173
174         // Display the snooze minutes in a toast.
175         Toast.makeText(AlarmAlertFullScreen.this, displayTime,
176                 Toast.LENGTH_LONG).show();
177         stopService(new Intent(Alarms.ALARM_ALERT_ACTION));
178         finish();
179     }
180
181     private NotificationManager getNotificationManager() {
182         return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
183     }
184
185     // Dismiss the alarm.
186     private void dismiss(boolean killed) {
187         // The service told us that the alarm has been killed, do not modify
188         // the notification or stop the service.
189         if (!killed) {
190             // Cancel the notification and stop playing the alarm
191             NotificationManager nm = getNotificationManager();
192             nm.cancel(mAlarm.id);
193             stopService(new Intent(Alarms.ALARM_ALERT_ACTION));
194         }
195         finish();
196     }
197
198     /**
199      * this is called when a second alarm is triggered while a
200      * previous alert window is still active.
201      */
202     @Override
203     protected void onNewIntent(Intent intent) {
204         super.onNewIntent(intent);
205
206         if (Log.LOGV) Log.v("AlarmAlert.OnNewIntent()");
207
208         mAlarm = intent.getParcelableExtra(Alarms.ALARM_INTENT_EXTRA);
209
210         setTitle();
211     }
212
213     @Override
214     protected void onStop() {
215         super.onStop();
216         if (!isFinishing()) {
217             // Don't hang around.
218             finish();
219         }
220     }
221     
222     @Override
223     public void onDestroy() {
224         super.onDestroy();
225         if (Log.LOGV) Log.v("AlarmAlert.onDestroy()");
226         // No longer care about the alarm being killed.
227         unregisterReceiver(mReceiver);
228     }
229
230     @Override
231     public boolean dispatchKeyEvent(KeyEvent event) {
232         // Do this on key down to handle a few of the system keys.
233         boolean up = event.getAction() == KeyEvent.ACTION_UP;
234         switch (event.getKeyCode()) {
235             // Volume keys and camera keys dismiss the alarm
236             case KeyEvent.KEYCODE_VOLUME_UP:
237             case KeyEvent.KEYCODE_VOLUME_DOWN:
238             case KeyEvent.KEYCODE_CAMERA:
239             case KeyEvent.KEYCODE_FOCUS:
240                 if (up) {
241                     switch (mVolumeBehavior) {
242                         case 1:
243                             snooze();
244                             break;
245
246                         case 2:
247                             dismiss(false);
248                             break;
249
250                         default:
251                             break;
252                     }
253                 }
254                 return true;
255             default:
256                 break;
257         }
258         return super.dispatchKeyEvent(event);
259     }
260
261     @Override
262     public void onBackPressed() {
263         // Don't allow back to dismiss. This method is overriden by AlarmAlert
264         // so that the dialog is dismissed.
265         return;
266     }
267 }