AlarmClock.java revision 0dffdefe54299ee01b45574244c92d3a06507b87
15fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project/* 25fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project 35fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project * 45fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License"); 55fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project * you may not use this file except in compliance with the License. 65fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project * You may obtain a copy of the License at 75fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project * 85fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 95fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project * 105fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project * Unless required by applicable law or agreed to in writing, software 115fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 125fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 135fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project * See the License for the specific language governing permissions and 145fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project * limitations under the License. 155fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project */ 165fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 175fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectpackage com.android.alarmclock; 185fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 195fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.app.Activity; 205fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.app.AlertDialog; 215fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.content.Context; 225fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.content.DialogInterface; 235fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.content.Intent; 245fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.content.SharedPreferences; 255fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.database.Cursor; 265fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.net.Uri; 275fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.os.Bundle; 285fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.os.Handler; 295fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.provider.Settings; 305fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.view.ContextMenu; 315fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.view.ContextMenu.ContextMenuInfo; 325fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.view.LayoutInflater; 335fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.view.Menu; 345fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.view.MenuItem; 355fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.view.View; 365fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.view.View.OnClickListener; 370dffdefe54299ee01b45574244c92d3a06507b87Patrick Scottimport android.view.View.OnCreateContextMenuListener; 385fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.view.ViewGroup; 390dffdefe54299ee01b45574244c92d3a06507b87Patrick Scottimport android.widget.AdapterView; 400dffdefe54299ee01b45574244c92d3a06507b87Patrick Scottimport android.widget.AdapterView.AdapterContextMenuInfo; 410dffdefe54299ee01b45574244c92d3a06507b87Patrick Scottimport android.widget.AdapterView.OnItemClickListener; 425fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.widget.CursorAdapter; 435fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.widget.ListView; 445fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.widget.TextView; 455fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.widget.CheckBox; 465fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 475fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport java.util.Calendar; 485fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 495fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project/** 505fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project * AlarmClock application. 515fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project */ 520dffdefe54299ee01b45574244c92d3a06507b87Patrick Scottpublic class AlarmClock extends Activity implements OnItemClickListener { 535fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 545fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project final static String PREFERENCES = "AlarmClock"; 555fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project final static String PREF_CLOCK_FACE = "face"; 565fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project final static String PREF_SHOW_CLOCK = "show_clock"; 575fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 585fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project /** Cap alarm count at this number */ 595fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project final static int MAX_ALARM_COUNT = 12; 605fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 615fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project /** This must be false for production. If true, turns on logging, 625fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project test code, etc. */ 635fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project final static boolean DEBUG = false; 645fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 655fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project private SharedPreferences mPrefs; 665fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project private LayoutInflater mFactory; 675fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project private ViewGroup mClockLayout; 685fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project private View mClock = null; 695fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project private ListView mAlarmsList; 705fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project private Cursor mCursor; 715fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 725fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project /** 735fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project * Which clock face to show 745fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project */ 755fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project private int mFace = -1; 765fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 775fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project /* 785fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project * FIXME: it would be nice for this to live in an xml config file. 795fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project */ 805fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project final static int[] CLOCKS = { 815fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project R.layout.clock_basic_bw, 825fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project R.layout.clock_googly, 835fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project R.layout.clock_droid2, 845fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project R.layout.clock_droids, 855fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project R.layout.digital_clock 865fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project }; 875fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 885fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project private class AlarmTimeAdapter extends CursorAdapter { 895fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project public AlarmTimeAdapter(Context context, Cursor cursor) { 905fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project super(context, cursor); 915fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project } 925fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 935fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project public View newView(Context context, Cursor cursor, ViewGroup parent) { 945fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project View ret = mFactory.inflate(R.layout.alarm_time, parent, false); 955fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project DigitalClock digitalClock = (DigitalClock)ret.findViewById(R.id.digitalClock); 965fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project digitalClock.setLive(false); 975fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project if (Log.LOGV) Log.v("newView " + cursor.getPosition()); 985fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project return ret; 995fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project } 1005fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 1015fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project public void bindView(View view, Context context, Cursor cursor) { 1025fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project final int id = cursor.getInt(Alarms.AlarmColumns.ALARM_ID_INDEX); 1035fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project final int hour = cursor.getInt(Alarms.AlarmColumns.ALARM_HOUR_INDEX); 1045fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project final int minutes = cursor.getInt(Alarms.AlarmColumns.ALARM_MINUTES_INDEX); 1055fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project final Alarms.DaysOfWeek daysOfWeek = new Alarms.DaysOfWeek( 1065fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project cursor.getInt(Alarms.AlarmColumns.ALARM_DAYS_OF_WEEK_INDEX)); 1075fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project final boolean enabled = cursor.getInt(Alarms.AlarmColumns.ALARM_ENABLED_INDEX) == 1; 108185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project final String label = 109185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project cursor.getString(Alarms.AlarmColumns.ALARM_MESSAGE_INDEX); 1105fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 1115fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project CheckBox onButton = (CheckBox)view.findViewById(R.id.alarmButton); 1125fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project onButton.setChecked(enabled); 1135fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project onButton.setOnClickListener(new OnClickListener() { 1145fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project public void onClick(View v) { 1155fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project boolean isChecked = ((CheckBox) v).isChecked(); 1165fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project Alarms.enableAlarm(AlarmClock.this, id, isChecked); 1175fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project if (isChecked) { 1185fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project SetAlarm.popAlarmSetToast( 1195fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project AlarmClock.this, hour, minutes, daysOfWeek); 1205fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project } 1215fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project } 1225fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project }); 1235fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 1245fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project DigitalClock digitalClock = (DigitalClock)view.findViewById(R.id.digitalClock); 1255fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project if (Log.LOGV) Log.v("bindView " + cursor.getPosition() + " " + id + " " + hour + 1265fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project ":" + minutes + " " + daysOfWeek.toString(context, true) + " dc " + digitalClock); 1275fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 1285fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project // set the alarm text 1295fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project final Calendar c = Calendar.getInstance(); 1305fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project c.set(Calendar.HOUR_OF_DAY, hour); 1315fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project c.set(Calendar.MINUTE, minutes); 1325fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project digitalClock.updateTime(c); 133185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project 134185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project // Set the repeat text or leave it blank if it does not repeat. 1355fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project TextView daysOfWeekView = (TextView) digitalClock.findViewById(R.id.daysOfWeek); 136185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project final String daysOfWeekStr = 137185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project daysOfWeek.toString(AlarmClock.this, false); 138185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project if (daysOfWeekStr != null && daysOfWeekStr.length() != 0) { 139185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project daysOfWeekView.setText(daysOfWeekStr); 140185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project daysOfWeekView.setVisibility(View.VISIBLE); 141185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project } else { 142185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project daysOfWeekView.setVisibility(View.GONE); 143185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project } 144185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project 145185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project // Display the label 146185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project TextView labelView = 147185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project (TextView) digitalClock.findViewById(R.id.label); 148185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project if (label != null && label.length() != 0) { 149185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project labelView.setText(label); 1505d6f5fa51fa7fd13784a73199161deabec5bc898Patrick Scott labelView.setVisibility(View.VISIBLE); 151185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project } else { 1525d6f5fa51fa7fd13784a73199161deabec5bc898Patrick Scott labelView.setVisibility(View.GONE); 153185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project } 1545fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project } 1555fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project }; 1565fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 1570dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott private boolean isAlarmEnabled(final Cursor c) { 1580dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott return c.getInt(Alarms.AlarmColumns.ALARM_ENABLED_INDEX) == 1; 1590dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott } 1600dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott 1615fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project @Override 1625fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project public boolean onContextItemSelected(final MenuItem item) { 1630dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott final AdapterContextMenuInfo info = 1640dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott (AdapterContextMenuInfo) item.getMenuInfo(); 1650dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott final int id = (int) info.id; 1660dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott switch (item.getItemId()) { 1670dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott case R.id.delete_alarm: 1680dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott // Confirm that the alarm will be deleted. 1690dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott new AlertDialog.Builder(this) 1700dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott .setTitle(getString(R.string.delete_alarm)) 1710dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott .setMessage(getString(R.string.delete_alarm_confirm)) 1720dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott .setPositiveButton(android.R.string.ok, 1730dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott new DialogInterface.OnClickListener() { 1740dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott public void onClick(DialogInterface d, 1750dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott int w) { 1760dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott Alarms.deleteAlarm(AlarmClock.this, id); 1770dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott } 1780dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott }) 1790dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott .setNegativeButton(android.R.string.cancel, null) 1800dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott .show(); 1810dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott return true; 1820dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott 1830dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott case R.id.enable_alarm: 1840dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott final Cursor c = (Cursor) mAlarmsList.getAdapter() 1850dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott .getItem(info.position); 1860dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott boolean enabled = isAlarmEnabled(c); 1870dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott Alarms.enableAlarm(this, id, !enabled); 1880dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott if (!enabled) { 1890dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott final int hour = 1900dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott c.getInt(Alarms.AlarmColumns.ALARM_HOUR_INDEX); 1910dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott final int minutes = 1920dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott c.getInt(Alarms.AlarmColumns.ALARM_MINUTES_INDEX); 1930dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott final Alarms.DaysOfWeek daysOfWeek = new Alarms.DaysOfWeek( 1940dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott c.getInt(Alarms.AlarmColumns.ALARM_DAYS_OF_WEEK_INDEX)); 1950dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott SetAlarm.popAlarmSetToast(this, hour, minutes, daysOfWeek); 1960dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott } 1970dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott return true; 1980dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott 1990dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott default: 2000dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott break; 2010dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott } 2020dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott return super.onContextItemSelected(item); 2035fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project } 2045fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 2055fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project @Override 2065fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project protected void onCreate(Bundle icicle) { 2075fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project super.onCreate(icicle); 2085fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 2095fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project // sanity check -- no database, no clock 2105fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project if (getContentResolver() == null) { 2115fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project new AlertDialog.Builder(this) 2125fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project .setTitle(getString(R.string.error)) 2135fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project .setMessage(getString(R.string.dberror)) 2145fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project .setPositiveButton( 2155fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project android.R.string.ok, 2165fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project new DialogInterface.OnClickListener() { 2175fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project public void onClick(DialogInterface dialog, int which) { 2185fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project finish(); 2195fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project } 2205fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project }) 2215fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project .setOnCancelListener( 2225fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project new DialogInterface.OnCancelListener() { 2235fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project public void onCancel(DialogInterface dialog) { 2245fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project finish(); 2255fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project }}) 2265fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project .setIcon(android.R.drawable.ic_dialog_alert) 2275fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project .create().show(); 2285fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project return; 2295fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project } 2305fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 2315fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project setContentView(R.layout.alarm_clock); 2325fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project mFactory = LayoutInflater.from(this); 2335fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project mPrefs = getSharedPreferences(PREFERENCES, 0); 2345fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 2355fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project mCursor = Alarms.getAlarmsCursor(getContentResolver()); 2365fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project mAlarmsList = (ListView) findViewById(R.id.alarms_list); 2375fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project mAlarmsList.setAdapter(new AlarmTimeAdapter(this, mCursor)); 2385fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project mAlarmsList.setVerticalScrollBarEnabled(true); 2390dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott mAlarmsList.setOnItemClickListener(this); 2400dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott mAlarmsList.setOnCreateContextMenuListener(this); 2415fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 2425fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project mClockLayout = (ViewGroup) findViewById(R.id.clock_layout); 2435fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project mClockLayout.setOnClickListener(new View.OnClickListener() { 2445fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project public void onClick(View v) { 2455fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project final Intent intent = new Intent(AlarmClock.this, ClockPicker.class); 2465fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 2475fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project startActivity(intent); 2485fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project } 2495fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project }); 2505fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 2515fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project setClockVisibility(mPrefs.getBoolean(PREF_SHOW_CLOCK, true)); 2525fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project } 2535fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 2545fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project @Override 2555fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project protected void onResume() { 2565fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project super.onResume(); 2575fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 2585fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project int face = mPrefs.getInt(PREF_CLOCK_FACE, 0); 2595fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project if (mFace != face) { 2605fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project if (face < 0 || face >= AlarmClock.CLOCKS.length) 2615fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project mFace = 0; 2625fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project else 2635fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project mFace = face; 2645fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project inflateClock(); 2655fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project } 2665fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project } 2675fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 2685fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project @Override 2695fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project protected void onDestroy() { 2705fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project super.onDestroy(); 2715fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project ToastMaster.cancelToast(); 2725fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project mCursor.deactivate(); 2735fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project } 2745fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 2755fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project protected void inflateClock() { 2765fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project if (mClock != null) { 2775fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project mClockLayout.removeView(mClock); 2785fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project } 2795fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project mClock = mFactory.inflate(CLOCKS[mFace], null); 2805fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project mClockLayout.addView(mClock, 0); 2815fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project } 2825fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 2835fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project @Override 2845fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project public boolean onCreateOptionsMenu(Menu menu) { 28506522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott // Inflate our menu. 28606522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott getMenuInflater().inflate(R.menu.main_menu, menu); 2875fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 28806522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott return super.onCreateOptionsMenu(menu); 2895fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project } 2905fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 2910dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott @Override 2920dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott public void onCreateContextMenu(ContextMenu menu, View view, 2930dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott ContextMenuInfo menuInfo) { 2940dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott // Inflate the menu from xml. 2950dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott getMenuInflater().inflate(R.menu.context_menu, menu); 2960dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott 2970dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott // Use the current item to create a custom view for the header. 2980dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott final AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo; 2990dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott final Cursor c = 3000dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott (Cursor) mAlarmsList.getAdapter().getItem((int) info.position); 3010dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott final int hour = c.getInt(Alarms.AlarmColumns.ALARM_HOUR_INDEX); 3020dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott final int minutes = c.getInt(Alarms.AlarmColumns.ALARM_MINUTES_INDEX); 3030dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott final String label = 3040dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott c.getString(Alarms.AlarmColumns.ALARM_MESSAGE_INDEX); 3050dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott 3060dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott // Construct the Calendar to compute the time. 3070dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott final Calendar cal = Calendar.getInstance(); 3080dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott cal.set(Calendar.HOUR_OF_DAY, hour); 3090dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott cal.set(Calendar.MINUTE, minutes); 3100dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott final String time = Alarms.formatTime(this, cal); 3110dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott 3120dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott // Inflate the custom view and set each TextView's text. 3130dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott final View v = mFactory.inflate(R.layout.context_menu_header, null); 3140dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott TextView textView = (TextView) v.findViewById(R.id.header_time); 3150dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott textView.setText(time); 3160dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott textView = (TextView) v.findViewById(R.id.header_label); 3170dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott textView.setText(label); 3180dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott 3190dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott // Set the custom view on the menu. 3200dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott menu.setHeaderView(v); 3210dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott // Change the text to "disable" if the alarm is already enabled. 3220dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott if (isAlarmEnabled(c)) { 3230dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott menu.findItem(R.id.enable_alarm).setTitle(R.string.disable_alarm); 3240dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott } 3250dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott } 3260dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott 3270dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott public void onItemClick(AdapterView parent, View v, int pos, long id) { 3280dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott Intent intent = new Intent(this, SetAlarm.class); 3290dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott intent.putExtra(Alarms.ID, (int) id); 3300dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott startActivity(intent); 3310dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott } 3320dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott 3335fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project /** 3345fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project * Only allow user to add a new alarm if there are fewer than 3355fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project * MAX_ALARM_COUNT 3365fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project */ 3375fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project @Override 3385fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project public boolean onPrepareOptionsMenu(Menu menu) { 33906522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott menu.findItem(R.id.menu_add_alarm).setVisible( 34006522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott mAlarmsList.getAdapter().getCount() < MAX_ALARM_COUNT); 34106522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott menu.findItem(R.id.menu_toggle_clock).setTitle( 34206522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott getClockVisibility() ? R.string.hide_clock 34306522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott : R.string.show_clock); 34406522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott return super.onPrepareOptionsMenu(menu); 3455fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project } 3465fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 3475fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project @Override 3485fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project public boolean onOptionsItemSelected(MenuItem item) { 34906522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott switch (item.getItemId()) { 35006522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott case R.id.menu_add_alarm: 35106522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott Uri uri = Alarms.addAlarm(getContentResolver()); 35206522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott // FIXME: scroll to new item? 35306522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott String segment = uri.getPathSegments().get(1); 35406522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott int newId = Integer.parseInt(segment); 35506522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott if (Log.LOGV) { 35606522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott Log.v("In AlarmClock, new alarm id = " + newId); 35706522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott } 35806522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott Intent intent = new Intent(this, SetAlarm.class); 35906522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott intent.putExtra(Alarms.ID, newId); 36006522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott startActivity(intent); 36106522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott return true; 36206522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott 36306522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott case R.id.menu_toggle_clock: 36406522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott setClockVisibility(!getClockVisibility()); 36506522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott saveClockVisibility(); 36606522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott return true; 36706522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott 36806522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott case R.id.menu_settings: 36906522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott startActivity(new Intent(this, SettingsActivity.class)); 37006522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott return true; 3715fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project } 3725fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 37306522bf515aa117255e1e1d7ad0d6ae56a55ebfcPatrick Scott return super.onOptionsItemSelected(item); 3745fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project } 3755fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 3765fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 3775fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project private boolean getClockVisibility() { 3785fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project return mClockLayout.getVisibility() == View.VISIBLE; 3795fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project } 3805fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 3815fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project private void setClockVisibility(boolean visible) { 3825fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project mClockLayout.setVisibility(visible ? View.VISIBLE : View.GONE); 3835fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project } 3845fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project 3855fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project private void saveClockVisibility() { 3865fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project mPrefs.edit().putBoolean(PREF_SHOW_CLOCK, getClockVisibility()).commit(); 3875fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project } 3885fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project} 389