OSDN Git Service

e9d5e7866c7ea4abc6d432f2a961336dc3465671
[android-x86/packages-apps-DeskClock.git] / src / com / android / deskclock / AlarmClock.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.AlertDialog;
21 import android.content.Context;
22 import android.content.DialogInterface;
23 import android.content.Intent;
24 import android.content.SharedPreferences;
25 import android.content.res.Configuration;
26 import android.database.Cursor;
27 import android.database.DataSetObserver;
28 import android.net.Uri;
29 import android.os.Bundle;
30 import android.os.Handler;
31 import android.provider.Settings;
32 import android.view.ContextMenu;
33 import android.view.ContextMenu.ContextMenuInfo;
34 import android.view.LayoutInflater;
35 import android.view.Menu;
36 import android.view.MenuItem;
37 import android.view.View;
38 import android.view.View.OnClickListener;
39 import android.view.View.OnCreateContextMenuListener;
40 import android.view.ViewGroup;
41 import android.widget.AdapterView;
42 import android.widget.AdapterView.AdapterContextMenuInfo;
43 import android.widget.AdapterView.OnItemClickListener;
44 import android.widget.Button;
45 import android.widget.CheckBox;
46 import android.widget.CursorAdapter;
47 import android.widget.ListView;
48 import android.widget.TextView;
49
50 import java.util.Calendar;
51
52 /**
53  * AlarmClock application.
54  */
55 public class AlarmClock extends Activity implements OnItemClickListener {
56
57     final static String PREFERENCES = "AlarmClock";
58
59     /** Cap alarm count at this number */
60     final static int MAX_ALARM_COUNT = 12;
61
62     /** This must be false for production.  If true, turns on logging,
63         test code, etc. */
64     final static boolean DEBUG = false;
65
66     private SharedPreferences mPrefs;
67     private LayoutInflater mFactory;
68     private ListView mAlarmsList;
69     private Cursor mCursor;
70
71     private class AlarmTimeAdapter extends CursorAdapter {
72         public AlarmTimeAdapter(Context context, Cursor cursor) {
73             super(context, cursor);
74         }
75
76         public View newView(Context context, Cursor cursor, ViewGroup parent) {
77             View ret = mFactory.inflate(R.layout.alarm_time, parent, false);
78
79             DigitalClock digitalClock = (DigitalClock)ret.findViewById(R.id.digitalClock);
80             digitalClock.setLive(false);
81             if (Log.LOGV) Log.v("newView " + cursor.getPosition());
82             return ret;
83         }
84
85         public void bindView(View view, Context context, Cursor cursor) {
86             final Alarm alarm = new Alarm(cursor);
87
88             CheckBox onButton = (CheckBox)view.findViewById(R.id.alarmButton);
89             onButton.setChecked(alarm.enabled);
90             onButton.setOnClickListener(new OnClickListener() {
91                     public void onClick(View v) {
92                         boolean isChecked = ((CheckBox) v).isChecked();
93                         Alarms.enableAlarm(AlarmClock.this, alarm.id,
94                             isChecked);
95                         if (isChecked) {
96                             SetAlarm.popAlarmSetToast(AlarmClock.this,
97                                 alarm.hour, alarm.minutes, alarm.daysOfWeek);
98                         }
99                     }
100             });
101
102             DigitalClock digitalClock =
103                     (DigitalClock) view.findViewById(R.id.digitalClock);
104
105             // set the alarm text
106             final Calendar c = Calendar.getInstance();
107             c.set(Calendar.HOUR_OF_DAY, alarm.hour);
108             c.set(Calendar.MINUTE, alarm.minutes);
109             digitalClock.updateTime(c);
110
111             // Set the repeat text or leave it blank if it does not repeat.
112             TextView daysOfWeekView =
113                     (TextView) digitalClock.findViewById(R.id.daysOfWeek);
114             final String daysOfWeekStr =
115                     alarm.daysOfWeek.toString(AlarmClock.this, false);
116             if (daysOfWeekStr != null && daysOfWeekStr.length() != 0) {
117                 daysOfWeekView.setText(daysOfWeekStr);
118                 daysOfWeekView.setVisibility(View.VISIBLE);
119             } else {
120                 daysOfWeekView.setVisibility(View.GONE);
121             }
122
123             // Display the label
124             TextView labelView =
125                     (TextView) digitalClock.findViewById(R.id.label);
126             if (alarm.label != null && alarm.label.length() != 0) {
127                 labelView.setText(alarm.label);
128                 labelView.setVisibility(View.VISIBLE);
129             } else {
130                 labelView.setVisibility(View.GONE);
131             }
132         }
133     };
134
135     @Override
136     public boolean onContextItemSelected(final MenuItem item) {
137         final AdapterContextMenuInfo info =
138                 (AdapterContextMenuInfo) item.getMenuInfo();
139         final int id = (int) info.id;
140         switch (item.getItemId()) {
141             case R.id.delete_alarm:
142                 // Confirm that the alarm will be deleted.
143                 new AlertDialog.Builder(this)
144                         .setTitle(getString(R.string.delete_alarm))
145                         .setMessage(getString(R.string.delete_alarm_confirm))
146                         .setPositiveButton(android.R.string.ok,
147                                 new DialogInterface.OnClickListener() {
148                                     public void onClick(DialogInterface d,
149                                             int w) {
150                                         Alarms.deleteAlarm(AlarmClock.this, id);
151                                     }
152                                 })
153                         .setNegativeButton(android.R.string.cancel, null)
154                         .show();
155                 return true;
156
157             case R.id.enable_alarm:
158                 final Cursor c = (Cursor) mAlarmsList.getAdapter()
159                         .getItem(info.position);
160                 final Alarm alarm = new Alarm(c);
161                 Alarms.enableAlarm(this, alarm.id, !alarm.enabled);
162                 if (!alarm.enabled) {
163                     SetAlarm.popAlarmSetToast(this, alarm.hour, alarm.minutes,
164                             alarm.daysOfWeek);
165                 }
166                 return true;
167
168             default:
169                 break;
170         }
171         return super.onContextItemSelected(item);
172     }
173
174     @Override
175     protected void onCreate(Bundle icicle) {
176         super.onCreate(icicle);
177
178         mFactory = LayoutInflater.from(this);
179         mPrefs = getSharedPreferences(PREFERENCES, 0);
180         mCursor = Alarms.getAlarmsCursor(getContentResolver());
181
182         updateLayout();
183     }
184
185     private void updateLayout() {
186         setContentView(R.layout.alarm_clock);
187         mAlarmsList = (ListView) findViewById(R.id.alarms_list);
188         AlarmTimeAdapter adapter = new AlarmTimeAdapter(this, mCursor);
189         mAlarmsList.setAdapter(adapter);
190         mAlarmsList.setVerticalScrollBarEnabled(true);
191         mAlarmsList.setOnItemClickListener(this);
192         mAlarmsList.setOnCreateContextMenuListener(this);
193
194         final Button addAlarm = (Button) findViewById(R.id.add_alarm);
195         addAlarm.setOnClickListener(new View.OnClickListener() {
196                 public void onClick(View v) {
197                     Uri uri = Alarms.addAlarm(getContentResolver());
198                     // FIXME: scroll to new item?
199                     String segment = uri.getPathSegments().get(1);
200                     int newId = Integer.parseInt(segment);
201                     if (Log.LOGV) {
202                         Log.v("In AlarmClock, new alarm id = " + newId);
203                     }
204                     Intent intent =
205                         new Intent(AlarmClock.this, SetAlarm.class);
206                     intent.putExtra(Alarms.ALARM_ID, newId);
207                     startActivity(intent);
208                 }
209             });
210         // Update the enabled state of the "Add alarm" menu item depending on
211         // how many alarms are set.
212         adapter.registerDataSetObserver(new DataSetObserver() {
213             public void onChanged() {
214                 updateAddAlarmButton(addAlarm);
215             }
216             public void onInvalidate() {
217                 updateAddAlarmButton(addAlarm);
218             }
219         });
220
221         Button settings = (Button) findViewById(R.id.settings);
222         settings.setOnClickListener(new View.OnClickListener() {
223                 public void onClick(View v) {
224                     startActivity(
225                         new Intent(AlarmClock.this, SettingsActivity.class));
226                 }
227             });
228     }
229
230     private void updateAddAlarmButton(Button b) {
231         b.setEnabled(mAlarmsList.getAdapter().getCount() < MAX_ALARM_COUNT);
232     }
233
234     @Override
235     protected void onDestroy() {
236         super.onDestroy();
237         ToastMaster.cancelToast();
238         mCursor.deactivate();
239     }
240
241     @Override
242     public void onCreateContextMenu(ContextMenu menu, View view,
243             ContextMenuInfo menuInfo) {
244         // Inflate the menu from xml.
245         getMenuInflater().inflate(R.menu.context_menu, menu);
246
247         // Use the current item to create a custom view for the header.
248         final AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
249         final Cursor c =
250                 (Cursor) mAlarmsList.getAdapter().getItem((int) info.position);
251         final Alarm alarm = new Alarm(c);
252
253         // Construct the Calendar to compute the time.
254         final Calendar cal = Calendar.getInstance();
255         cal.set(Calendar.HOUR_OF_DAY, alarm.hour);
256         cal.set(Calendar.MINUTE, alarm.minutes);
257         final String time = Alarms.formatTime(this, cal);
258
259         // Inflate the custom view and set each TextView's text.
260         final View v = mFactory.inflate(R.layout.context_menu_header, null);
261         TextView textView = (TextView) v.findViewById(R.id.header_time);
262         textView.setText(time);
263         textView = (TextView) v.findViewById(R.id.header_label);
264         textView.setText(alarm.label);
265
266         // Set the custom view on the menu.
267         menu.setHeaderView(v);
268         // Change the text to "disable" if the alarm is already enabled.
269         if (alarm.enabled) {
270             menu.findItem(R.id.enable_alarm).setTitle(R.string.disable_alarm);
271         }
272     }
273
274     public void onItemClick(AdapterView parent, View v, int pos, long id) {
275         Intent intent = new Intent(this, SetAlarm.class);
276         intent.putExtra(Alarms.ALARM_ID, (int) id);
277         startActivity(intent);
278     }
279 }