OSDN Git Service

Remove the clock face from the alarm list.
[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.provider.Settings;
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.View.OnCreateContextMenuListener;
39 import android.view.ViewGroup;
40 import android.widget.AdapterView;
41 import android.widget.AdapterView.AdapterContextMenuInfo;
42 import android.widget.AdapterView.OnItemClickListener;
43 import android.widget.CursorAdapter;
44 import android.widget.ListView;
45 import android.widget.TextView;
46 import android.widget.CheckBox;
47
48 import java.util.Calendar;
49 import java.text.DateFormatSymbols;
50
51 /**
52  * AlarmClock application.
53  */
54 public class AlarmClock extends Activity implements OnItemClickListener {
55
56     final static String PREFERENCES = "AlarmClock";
57
58     /** Cap alarm count at this number */
59     final static int MAX_ALARM_COUNT = 12;
60
61     /** This must be false for production.  If true, turns on logging,
62         test code, etc. */
63     final static 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 = (DigitalClock)ret.findViewById(R.id.digitalClock);
84             digitalClock.setLive(false);
85             if (Log.LOGV) Log.v("newView " + cursor.getPosition());
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         mAlarmsList.setAdapter(new AlarmTimeAdapter(this, mCursor));
197         mAlarmsList.setVerticalScrollBarEnabled(true);
198         mAlarmsList.setOnItemClickListener(this);
199         mAlarmsList.setOnCreateContextMenuListener(this);
200     }
201
202     @Override
203     protected void onDestroy() {
204         super.onDestroy();
205         ToastMaster.cancelToast();
206         mCursor.deactivate();
207     }
208
209     @Override
210     public boolean onCreateOptionsMenu(Menu menu) {
211         // Inflate our menu.
212         getMenuInflater().inflate(R.menu.main_menu, menu);
213
214         return super.onCreateOptionsMenu(menu);
215     }
216
217     @Override
218     public void onCreateContextMenu(ContextMenu menu, View view,
219             ContextMenuInfo menuInfo) {
220         // Inflate the menu from xml.
221         getMenuInflater().inflate(R.menu.context_menu, menu);
222
223         // Use the current item to create a custom view for the header.
224         final AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
225         final Cursor c =
226                 (Cursor) mAlarmsList.getAdapter().getItem((int) info.position);
227         final Alarm alarm = new Alarm(c);
228
229         // Construct the Calendar to compute the time.
230         final Calendar cal = Calendar.getInstance();
231         cal.set(Calendar.HOUR_OF_DAY, alarm.hour);
232         cal.set(Calendar.MINUTE, alarm.minutes);
233         final String time = Alarms.formatTime(this, cal);
234
235         // Inflate the custom view and set each TextView's text.
236         final View v = mFactory.inflate(R.layout.context_menu_header, null);
237         TextView textView = (TextView) v.findViewById(R.id.header_time);
238         textView.setText(time);
239         textView = (TextView) v.findViewById(R.id.header_label);
240         textView.setText(alarm.label);
241
242         // Set the custom view on the menu.
243         menu.setHeaderView(v);
244         // Change the text to "disable" if the alarm is already enabled.
245         if (alarm.enabled) {
246             menu.findItem(R.id.enable_alarm).setTitle(R.string.disable_alarm);
247         }
248     }
249
250     public void onItemClick(AdapterView parent, View v, int pos, long id) {
251         Intent intent = new Intent(this, SetAlarm.class);
252         intent.putExtra(Alarms.ALARM_ID, (int) id);
253         startActivity(intent);
254     }
255
256     /**
257      * Only allow user to add a new alarm if there are fewer than
258      * MAX_ALARM_COUNT
259      */
260     @Override
261     public boolean onPrepareOptionsMenu(Menu menu) {
262         menu.findItem(R.id.menu_add_alarm).setVisible(
263                 mAlarmsList.getAdapter().getCount() < MAX_ALARM_COUNT);
264         return super.onPrepareOptionsMenu(menu);
265     }
266
267     @Override
268     public boolean onOptionsItemSelected(MenuItem item) {
269         switch (item.getItemId()) {
270             case R.id.menu_add_alarm:
271                 Uri uri = Alarms.addAlarm(getContentResolver());
272                 // FIXME: scroll to new item?
273                 String segment = uri.getPathSegments().get(1);
274                 int newId = Integer.parseInt(segment);
275                 if (Log.LOGV) {
276                     Log.v("In AlarmClock, new alarm id = " + newId);
277                 }
278                 Intent intent = new Intent(this, SetAlarm.class);
279                 intent.putExtra(Alarms.ALARM_ID, newId);
280                 startActivity(intent);
281                 return true;
282
283             case R.id.menu_settings:
284                 startActivity(new Intent(this, SettingsActivity.class));
285                 return true;
286         }
287
288         return super.onOptionsItemSelected(item);
289     }
290 }