OSDN Git Service

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