OSDN Git Service

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