OSDN Git Service

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