OSDN Git Service

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