OSDN Git Service

acf42b3dbb5ad440bdf5ded254a6c91ca41def05
[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.graphics.Typeface;
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.ImageButton;
48 import android.widget.ImageView;
49 import android.widget.ListView;
50 import android.widget.TextView;
51
52 import java.util.Calendar;
53
54 /**
55  * AlarmClock application.
56  */
57 public class AlarmClock extends Activity implements OnItemClickListener {
58
59     final static String PREFERENCES = "AlarmClock";
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 void updateIndicatorAndAlarm(boolean enabled, ImageView bar,
71             Alarm alarm) {
72         bar.setImageResource(enabled ? R.drawable.ic_indicator_on
73                 : R.drawable.ic_indicator_off);
74         Alarms.enableAlarm(this, alarm.id, enabled);
75         if (enabled) {
76             SetAlarm.popAlarmSetToast(this, alarm.hour, alarm.minutes,
77                     alarm.daysOfWeek);
78         }
79     }
80
81     private class AlarmTimeAdapter extends CursorAdapter {
82         public AlarmTimeAdapter(Context context, Cursor cursor) {
83             super(context, cursor);
84         }
85
86         public View newView(Context context, Cursor cursor, ViewGroup parent) {
87             View ret = mFactory.inflate(R.layout.alarm_time, parent, false);
88
89             DigitalClock digitalClock = (DigitalClock)ret.findViewById(R.id.digitalClock);
90             digitalClock.setLive(false);
91             if (Log.LOGV) Log.v("newView " + cursor.getPosition());
92             return ret;
93         }
94
95         public void bindView(View view, Context context, Cursor cursor) {
96             final Alarm alarm = new Alarm(cursor);
97
98             View indicator = view.findViewById(R.id.indicator);
99
100             // Set the initial resource for the bar image.
101             final ImageView barOnOff =
102                     (ImageView) indicator.findViewById(R.id.bar_onoff);
103             barOnOff.setImageResource(alarm.enabled ?
104                     R.drawable.ic_indicator_on : R.drawable.ic_indicator_off);
105
106             // Set the initial state of the clock "checkbox"
107             final CheckBox clockOnOff =
108                     (CheckBox) indicator.findViewById(R.id.clock_onoff);
109             clockOnOff.setChecked(alarm.enabled);
110
111             // Handle the "checkbox" click and toggle the alarm.
112             clockOnOff.setOnClickListener(new OnClickListener() {
113                     public void onClick(View v) {
114                         boolean isChecked = ((CheckBox) v).isChecked();
115                         updateIndicatorAndAlarm(isChecked, barOnOff, alarm);
116                     }
117             });
118
119             // Clicking outside the "checkbox" should also change the state.
120             indicator.setOnClickListener(new OnClickListener() {
121                     public void onClick(View v) {
122                         clockOnOff.toggle();
123                         updateIndicatorAndAlarm(clockOnOff.isChecked(),
124                                 barOnOff, alarm);
125                     }
126             });
127
128             DigitalClock digitalClock =
129                     (DigitalClock) view.findViewById(R.id.digitalClock);
130
131             // set the alarm text
132             final Calendar c = Calendar.getInstance();
133             c.set(Calendar.HOUR_OF_DAY, alarm.hour);
134             c.set(Calendar.MINUTE, alarm.minutes);
135             digitalClock.updateTime(c);
136             digitalClock.setTypeface(Typeface.DEFAULT);
137
138             // Set the repeat text or leave it blank if it does not repeat.
139             TextView daysOfWeekView =
140                     (TextView) digitalClock.findViewById(R.id.daysOfWeek);
141             final String daysOfWeekStr =
142                     alarm.daysOfWeek.toString(AlarmClock.this, false);
143             if (daysOfWeekStr != null && daysOfWeekStr.length() != 0) {
144                 daysOfWeekView.setText(daysOfWeekStr);
145                 daysOfWeekView.setVisibility(View.VISIBLE);
146             } else {
147                 daysOfWeekView.setVisibility(View.GONE);
148             }
149
150             // Display the label
151             TextView labelView =
152                     (TextView) view.findViewById(R.id.label);
153             if (alarm.label != null && alarm.label.length() != 0) {
154                 labelView.setText(alarm.label);
155                 labelView.setVisibility(View.VISIBLE);
156             } else {
157                 labelView.setVisibility(View.GONE);
158             }
159         }
160     };
161
162     @Override
163     public boolean onContextItemSelected(final MenuItem item) {
164         final AdapterContextMenuInfo info =
165                 (AdapterContextMenuInfo) item.getMenuInfo();
166         final int id = (int) info.id;
167         switch (item.getItemId()) {
168             case R.id.delete_alarm:
169                 // Confirm that the alarm will be deleted.
170                 new AlertDialog.Builder(this)
171                         .setTitle(getString(R.string.delete_alarm))
172                         .setMessage(getString(R.string.delete_alarm_confirm))
173                         .setPositiveButton(android.R.string.ok,
174                                 new DialogInterface.OnClickListener() {
175                                     public void onClick(DialogInterface d,
176                                             int w) {
177                                         Alarms.deleteAlarm(AlarmClock.this, id);
178                                     }
179                                 })
180                         .setNegativeButton(android.R.string.cancel, null)
181                         .show();
182                 return true;
183
184             case R.id.enable_alarm:
185                 final Cursor c = (Cursor) mAlarmsList.getAdapter()
186                         .getItem(info.position);
187                 final Alarm alarm = new Alarm(c);
188                 Alarms.enableAlarm(this, alarm.id, !alarm.enabled);
189                 if (!alarm.enabled) {
190                     SetAlarm.popAlarmSetToast(this, alarm.hour, alarm.minutes,
191                             alarm.daysOfWeek);
192                 }
193                 return true;
194
195             default:
196                 break;
197         }
198         return super.onContextItemSelected(item);
199     }
200
201     @Override
202     protected void onCreate(Bundle icicle) {
203         super.onCreate(icicle);
204
205         mFactory = LayoutInflater.from(this);
206         mPrefs = getSharedPreferences(PREFERENCES, 0);
207         mCursor = Alarms.getAlarmsCursor(getContentResolver());
208
209         updateLayout();
210     }
211
212     private void updateLayout() {
213         setContentView(R.layout.alarm_clock);
214         mAlarmsList = (ListView) findViewById(R.id.alarms_list);
215         AlarmTimeAdapter adapter = new AlarmTimeAdapter(this, mCursor);
216         mAlarmsList.setAdapter(adapter);
217         mAlarmsList.setVerticalScrollBarEnabled(true);
218         mAlarmsList.setOnItemClickListener(this);
219         mAlarmsList.setOnCreateContextMenuListener(this);
220
221         View addAlarm = findViewById(R.id.add_alarm);
222         addAlarm.setOnClickListener(new View.OnClickListener() {
223                 public void onClick(View v) {
224                     Uri uri = Alarms.addAlarm(getContentResolver());
225                     // FIXME: scroll to new item?
226                     String segment = uri.getPathSegments().get(1);
227                     int newId = Integer.parseInt(segment);
228                     if (Log.LOGV) {
229                         Log.v("In AlarmClock, new alarm id = " + newId);
230                     }
231                     Intent intent =
232                         new Intent(AlarmClock.this, SetAlarm.class);
233                     intent.putExtra(Alarms.ALARM_ID, newId);
234                     startActivity(intent);
235                 }
236             });
237
238         ImageButton deskClock =
239                 (ImageButton) findViewById(R.id.desk_clock_button);
240         deskClock.setOnClickListener(new View.OnClickListener() {
241                 public void onClick(View v) {
242                     startActivity(new Intent(AlarmClock.this, DeskClock.class));
243                 }
244         });
245     }
246
247     @Override
248     protected void onDestroy() {
249         super.onDestroy();
250         ToastMaster.cancelToast();
251         mCursor.deactivate();
252     }
253
254     @Override
255     public void onCreateContextMenu(ContextMenu menu, View view,
256             ContextMenuInfo menuInfo) {
257         // Inflate the menu from xml.
258         getMenuInflater().inflate(R.menu.context_menu, menu);
259
260         // Use the current item to create a custom view for the header.
261         final AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
262         final Cursor c =
263                 (Cursor) mAlarmsList.getAdapter().getItem((int) info.position);
264         final Alarm alarm = new Alarm(c);
265
266         // Construct the Calendar to compute the time.
267         final Calendar cal = Calendar.getInstance();
268         cal.set(Calendar.HOUR_OF_DAY, alarm.hour);
269         cal.set(Calendar.MINUTE, alarm.minutes);
270         final String time = Alarms.formatTime(this, cal);
271
272         // Inflate the custom view and set each TextView's text.
273         final View v = mFactory.inflate(R.layout.context_menu_header, null);
274         TextView textView = (TextView) v.findViewById(R.id.header_time);
275         textView.setText(time);
276         textView = (TextView) v.findViewById(R.id.header_label);
277         textView.setText(alarm.label);
278
279         // Set the custom view on the menu.
280         menu.setHeaderView(v);
281         // Change the text to "disable" if the alarm is already enabled.
282         if (alarm.enabled) {
283             menu.findItem(R.id.enable_alarm).setTitle(R.string.disable_alarm);
284         }
285     }
286
287     @Override
288     public boolean onOptionsItemSelected(MenuItem item) {
289         if (item.getItemId() == R.id.menu_item_settings) {
290             startActivity(new Intent(this, SettingsActivity.class));
291             return true;
292         }
293         return false;
294     }
295
296     @Override
297     public boolean onCreateOptionsMenu(Menu menu) {
298         getMenuInflater().inflate(R.menu.alarm_list_menu, menu);
299         return true;
300     }
301
302     public void onItemClick(AdapterView parent, View v, int pos, long id) {
303         Intent intent = new Intent(this, SetAlarm.class);
304         intent.putExtra(Alarms.ALARM_ID, (int) id);
305         startActivity(intent);
306     }
307 }