2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.deskclock;
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;
48 import java.text.DateFormatSymbols;
49 import java.util.Calendar;
52 * AlarmClock application.
54 public class AlarmClock extends Activity implements OnItemClickListener {
56 static final String PREFERENCES = "AlarmClock";
58 /** Cap alarm count at this number */
59 static final int MAX_ALARM_COUNT = 12;
61 /** This must be false for production. If true, turns on logging,
63 static final boolean DEBUG = false;
65 private SharedPreferences mPrefs;
66 private LayoutInflater mFactory;
67 private ListView mAlarmsList;
68 private Cursor mCursor;
70 private String mAm, mPm;
72 private class AlarmTimeAdapter extends CursorAdapter {
73 public AlarmTimeAdapter(Context context, Cursor cursor) {
74 super(context, cursor);
77 public View newView(Context context, Cursor cursor, ViewGroup parent) {
78 View ret = mFactory.inflate(R.layout.alarm_time, parent, false);
80 ((TextView) ret.findViewById(R.id.am)).setText(mAm);
81 ((TextView) ret.findViewById(R.id.pm)).setText(mPm);
83 DigitalClock digitalClock =
84 (DigitalClock) ret.findViewById(R.id.digitalClock);
85 digitalClock.setLive(false);
89 public void bindView(View view, Context context, Cursor cursor) {
90 final Alarm alarm = new Alarm(cursor);
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,
100 SetAlarm.popAlarmSetToast(AlarmClock.this,
101 alarm.hour, alarm.minutes, alarm.daysOfWeek);
106 DigitalClock digitalClock =
107 (DigitalClock) view.findViewById(R.id.digitalClock);
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);
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);
124 daysOfWeekView.setVisibility(View.GONE);
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);
134 labelView.setVisibility(View.GONE);
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,
154 Alarms.deleteAlarm(AlarmClock.this, id);
157 .setNegativeButton(android.R.string.cancel, null)
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,
175 return super.onContextItemSelected(item);
179 protected void onCreate(Bundle icicle) {
180 super.onCreate(icicle);
182 String[] ampm = new DateFormatSymbols().getAmPmStrings();
186 mFactory = LayoutInflater.from(this);
187 mPrefs = getSharedPreferences(PREFERENCES, 0);
188 mCursor = Alarms.getAlarmsCursor(getContentResolver());
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);
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);
210 Log.v("In AlarmClock, new alarm id = " + newId);
213 new Intent(AlarmClock.this, SetAlarm.class);
214 intent.putExtra(Alarms.ALARM_ID, newId);
215 startActivity(intent);
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);
224 public void onInvalidate() {
225 updateAddAlarmButton(addAlarm);
229 Button settings = (Button) findViewById(R.id.settings);
230 settings.setOnClickListener(new View.OnClickListener() {
231 public void onClick(View v) {
233 new Intent(AlarmClock.this, SettingsActivity.class));
238 private void updateAddAlarmButton(Button b) {
239 b.setEnabled(mAlarmsList.getAdapter().getCount() < MAX_ALARM_COUNT);
243 protected void onDestroy() {
245 ToastMaster.cancelToast();
246 mCursor.deactivate();
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);
255 // Use the current item to create a custom view for the header.
256 final AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
258 (Cursor) mAlarmsList.getAdapter().getItem((int) info.position);
259 final Alarm alarm = new Alarm(c);
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);
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);
274 // Set the custom view on the menu.
275 menu.setHeaderView(v);
276 // Change the text to "disable" if the alarm is already enabled.
278 menu.findItem(R.id.enable_alarm).setTitle(R.string.disable_alarm);
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);