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
17fa954248b41b567dfa2ddd7bd063d3936a51758eDaniel Sandlerpackage com.android.deskclock;
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;
25cf7307401797b159ca32f85e7cf38d18cececd0dPatrick Scottimport android.content.res.Configuration;
265fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.database.Cursor;
274e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scottimport android.graphics.Typeface;
285fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.net.Uri;
295fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.os.Bundle;
305fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.os.Handler;
315fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.view.ContextMenu;
325fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.view.ContextMenu.ContextMenuInfo;
335fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.view.LayoutInflater;
345fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.view.Menu;
355fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.view.MenuItem;
365fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.view.View;
375fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.view.View.OnClickListener;
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;
42dce90517954050428bfaf20e1409c10d372a4759Patrick Scottimport android.widget.Button;
43bcfaaa6b567b95df64499c2a2e3c15780438a2dcPatrick Scottimport android.widget.CheckBox;
445fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.widget.CursorAdapter;
45e3f01aff06d4bddb09215ccad3d8502de9164966Patrick Scottimport android.widget.ImageButton;
464e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scottimport android.widget.ImageView;
475fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.widget.ListView;
485fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.widget.TextView;
495fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
50bcfaaa6b567b95df64499c2a2e3c15780438a2dcPatrick Scottimport java.util.Calendar;
515fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
525fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project/**
535fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project * AlarmClock application.
545fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project */
550dffdefe54299ee01b45574244c92d3a06507b87Patrick Scottpublic class AlarmClock extends Activity implements OnItemClickListener {
565fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
57bcfaaa6b567b95df64499c2a2e3c15780438a2dcPatrick Scott    static final String PREFERENCES = "AlarmClock";
585fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
595fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    /** This must be false for production.  If true, turns on logging,
605fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        test code, etc. */
61bcfaaa6b567b95df64499c2a2e3c15780438a2dcPatrick Scott    static final boolean DEBUG = false;
625fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
635fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    private SharedPreferences mPrefs;
645fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    private LayoutInflater mFactory;
655fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    private ListView mAlarmsList;
665fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    private Cursor mCursor;
675fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
684e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott    private void updateIndicatorAndAlarm(boolean enabled, ImageView bar,
694e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott            Alarm alarm) {
704e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott        bar.setImageResource(enabled ? R.drawable.ic_indicator_on
714e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott                : R.drawable.ic_indicator_off);
724e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott        Alarms.enableAlarm(this, alarm.id, enabled);
734e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott        if (enabled) {
744e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott            SetAlarm.popAlarmSetToast(this, alarm.hour, alarm.minutes,
754e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott                    alarm.daysOfWeek);
764e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott        }
774e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott    }
784e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott
795fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    private class AlarmTimeAdapter extends CursorAdapter {
805fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        public AlarmTimeAdapter(Context context, Cursor cursor) {
815fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            super(context, cursor);
825fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        }
835fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
845fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        public View newView(Context context, Cursor cursor, ViewGroup parent) {
855fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            View ret = mFactory.inflate(R.layout.alarm_time, parent, false);
86eb142ac59aa8039c409b5fe4f50a130f16960d41Eric Fischer
87bcfaaa6b567b95df64499c2a2e3c15780438a2dcPatrick Scott            DigitalClock digitalClock =
88bcfaaa6b567b95df64499c2a2e3c15780438a2dcPatrick Scott                    (DigitalClock) ret.findViewById(R.id.digitalClock);
895fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            digitalClock.setLive(false);
905fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            return ret;
915fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        }
925fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
935fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        public void bindView(View view, Context context, Cursor cursor) {
94d776e51cec79901a0d656bba7b1f3780cceac57dPatrick Scott            final Alarm alarm = new Alarm(cursor);
955fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
964e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott            View indicator = view.findViewById(R.id.indicator);
974e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott
984e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott            // Set the initial resource for the bar image.
994e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott            final ImageView barOnOff =
1004e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott                    (ImageView) indicator.findViewById(R.id.bar_onoff);
1014e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott            barOnOff.setImageResource(alarm.enabled ?
1024e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott                    R.drawable.ic_indicator_on : R.drawable.ic_indicator_off);
1034e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott
1044e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott            // Set the initial state of the clock "checkbox"
1054e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott            final CheckBox clockOnOff =
1064e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott                    (CheckBox) indicator.findViewById(R.id.clock_onoff);
1074e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott            clockOnOff.setChecked(alarm.enabled);
1084e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott
1094e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott            // Clicking outside the "checkbox" should also change the state.
1104e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott            indicator.setOnClickListener(new OnClickListener() {
1114e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott                    public void onClick(View v) {
1124e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott                        clockOnOff.toggle();
1134e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott                        updateIndicatorAndAlarm(clockOnOff.isChecked(),
1144e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott                                barOnOff, alarm);
1155fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                    }
1165fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            });
1175fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
118d776e51cec79901a0d656bba7b1f3780cceac57dPatrick Scott            DigitalClock digitalClock =
119d776e51cec79901a0d656bba7b1f3780cceac57dPatrick Scott                    (DigitalClock) view.findViewById(R.id.digitalClock);
1205fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
1215fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            // set the alarm text
1225fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            final Calendar c = Calendar.getInstance();
123d776e51cec79901a0d656bba7b1f3780cceac57dPatrick Scott            c.set(Calendar.HOUR_OF_DAY, alarm.hour);
124d776e51cec79901a0d656bba7b1f3780cceac57dPatrick Scott            c.set(Calendar.MINUTE, alarm.minutes);
1255fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            digitalClock.updateTime(c);
1264e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott            digitalClock.setTypeface(Typeface.DEFAULT);
127185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project
128185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project            // Set the repeat text or leave it blank if it does not repeat.
129d776e51cec79901a0d656bba7b1f3780cceac57dPatrick Scott            TextView daysOfWeekView =
130d776e51cec79901a0d656bba7b1f3780cceac57dPatrick Scott                    (TextView) digitalClock.findViewById(R.id.daysOfWeek);
131185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project            final String daysOfWeekStr =
132d776e51cec79901a0d656bba7b1f3780cceac57dPatrick Scott                    alarm.daysOfWeek.toString(AlarmClock.this, false);
133185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project            if (daysOfWeekStr != null && daysOfWeekStr.length() != 0) {
134185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project                daysOfWeekView.setText(daysOfWeekStr);
135185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project                daysOfWeekView.setVisibility(View.VISIBLE);
136185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project            } else {
137185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project                daysOfWeekView.setVisibility(View.GONE);
138185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project            }
139185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project
140185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project            // Display the label
141185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project            TextView labelView =
1424e1b06ebcf08dd57240a56bac6f923b2e72ea793Patrick Scott                    (TextView) view.findViewById(R.id.label);
143d776e51cec79901a0d656bba7b1f3780cceac57dPatrick Scott            if (alarm.label != null && alarm.label.length() != 0) {
144d776e51cec79901a0d656bba7b1f3780cceac57dPatrick Scott                labelView.setText(alarm.label);
1455d6f5fa51fa7fd13784a73199161deabec5bc898Patrick Scott                labelView.setVisibility(View.VISIBLE);
146185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project            } else {
1475d6f5fa51fa7fd13784a73199161deabec5bc898Patrick Scott                labelView.setVisibility(View.GONE);
148185d17974729a98cf48a71ab9f16adaab9d1e1e0The Android Open Source Project            }
1495fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        }
1505fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    };
1515fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
1525fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    @Override
1535fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    public boolean onContextItemSelected(final MenuItem item) {
1540dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        final AdapterContextMenuInfo info =
1550dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott                (AdapterContextMenuInfo) item.getMenuInfo();
1560dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        final int id = (int) info.id;
1570dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        switch (item.getItemId()) {
1580dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott            case R.id.delete_alarm:
1590dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott                // Confirm that the alarm will be deleted.
1600dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott                new AlertDialog.Builder(this)
1610dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott                        .setTitle(getString(R.string.delete_alarm))
1620dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott                        .setMessage(getString(R.string.delete_alarm_confirm))
1630dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott                        .setPositiveButton(android.R.string.ok,
1640dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott                                new DialogInterface.OnClickListener() {
1650dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott                                    public void onClick(DialogInterface d,
1660dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott                                            int w) {
1670dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott                                        Alarms.deleteAlarm(AlarmClock.this, id);
1680dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott                                    }
1690dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott                                })
1700dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott                        .setNegativeButton(android.R.string.cancel, null)
1710dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott                        .show();
1720dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott                return true;
1730dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott
1740dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott            case R.id.enable_alarm:
1750dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott                final Cursor c = (Cursor) mAlarmsList.getAdapter()
1760dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott                        .getItem(info.position);
177d776e51cec79901a0d656bba7b1f3780cceac57dPatrick Scott                final Alarm alarm = new Alarm(c);
178d776e51cec79901a0d656bba7b1f3780cceac57dPatrick Scott                Alarms.enableAlarm(this, alarm.id, !alarm.enabled);
179d776e51cec79901a0d656bba7b1f3780cceac57dPatrick Scott                if (!alarm.enabled) {
180d776e51cec79901a0d656bba7b1f3780cceac57dPatrick Scott                    SetAlarm.popAlarmSetToast(this, alarm.hour, alarm.minutes,
181d776e51cec79901a0d656bba7b1f3780cceac57dPatrick Scott                            alarm.daysOfWeek);
1820dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott                }
1830dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott                return true;
1840dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott
185f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott            case R.id.edit_alarm:
186f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott                Intent intent = new Intent(this, SetAlarm.class);
187f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott                intent.putExtra(Alarms.ALARM_ID, id);
188f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott                startActivity(intent);
189f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott                return true;
190f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott
1910dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott            default:
1920dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott                break;
1930dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        }
1940dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        return super.onContextItemSelected(item);
1955fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    }
1965fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
1975fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    @Override
1985fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    protected void onCreate(Bundle icicle) {
1995fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        super.onCreate(icicle);
2005fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
2015fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mFactory = LayoutInflater.from(this);
2025fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mPrefs = getSharedPreferences(PREFERENCES, 0);
2035fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mCursor = Alarms.getAlarmsCursor(getContentResolver());
204cf7307401797b159ca32f85e7cf38d18cececd0dPatrick Scott
205cf7307401797b159ca32f85e7cf38d18cececd0dPatrick Scott        updateLayout();
206cf7307401797b159ca32f85e7cf38d18cececd0dPatrick Scott    }
207cf7307401797b159ca32f85e7cf38d18cececd0dPatrick Scott
208cf7307401797b159ca32f85e7cf38d18cececd0dPatrick Scott    private void updateLayout() {
209cf7307401797b159ca32f85e7cf38d18cececd0dPatrick Scott        setContentView(R.layout.alarm_clock);
2105fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mAlarmsList = (ListView) findViewById(R.id.alarms_list);
211dce90517954050428bfaf20e1409c10d372a4759Patrick Scott        AlarmTimeAdapter adapter = new AlarmTimeAdapter(this, mCursor);
212dce90517954050428bfaf20e1409c10d372a4759Patrick Scott        mAlarmsList.setAdapter(adapter);
2135fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mAlarmsList.setVerticalScrollBarEnabled(true);
2140dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        mAlarmsList.setOnItemClickListener(this);
2150dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        mAlarmsList.setOnCreateContextMenuListener(this);
216dce90517954050428bfaf20e1409c10d372a4759Patrick Scott
217e3f01aff06d4bddb09215ccad3d8502de9164966Patrick Scott        View addAlarm = findViewById(R.id.add_alarm);
218dce90517954050428bfaf20e1409c10d372a4759Patrick Scott        addAlarm.setOnClickListener(new View.OnClickListener() {
219dce90517954050428bfaf20e1409c10d372a4759Patrick Scott                public void onClick(View v) {
220f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott                    addNewAlarm();
221dce90517954050428bfaf20e1409c10d372a4759Patrick Scott                }
222dce90517954050428bfaf20e1409c10d372a4759Patrick Scott            });
22390c4833f94f4775f747333f8bd67107364a1d9c1Patrick Scott        // Make the entire view selected when focused.
22490c4833f94f4775f747333f8bd67107364a1d9c1Patrick Scott        addAlarm.setOnFocusChangeListener(new View.OnFocusChangeListener() {
22590c4833f94f4775f747333f8bd67107364a1d9c1Patrick Scott                public void onFocusChange(View v, boolean hasFocus) {
22690c4833f94f4775f747333f8bd67107364a1d9c1Patrick Scott                    v.setSelected(hasFocus);
22790c4833f94f4775f747333f8bd67107364a1d9c1Patrick Scott                }
22890c4833f94f4775f747333f8bd67107364a1d9c1Patrick Scott        });
229dce90517954050428bfaf20e1409c10d372a4759Patrick Scott
230e3f01aff06d4bddb09215ccad3d8502de9164966Patrick Scott        ImageButton deskClock =
231e3f01aff06d4bddb09215ccad3d8502de9164966Patrick Scott                (ImageButton) findViewById(R.id.desk_clock_button);
232e3f01aff06d4bddb09215ccad3d8502de9164966Patrick Scott        deskClock.setOnClickListener(new View.OnClickListener() {
233dce90517954050428bfaf20e1409c10d372a4759Patrick Scott                public void onClick(View v) {
234e3f01aff06d4bddb09215ccad3d8502de9164966Patrick Scott                    startActivity(new Intent(AlarmClock.this, DeskClock.class));
235dce90517954050428bfaf20e1409c10d372a4759Patrick Scott                }
236e3f01aff06d4bddb09215ccad3d8502de9164966Patrick Scott        });
2375fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    }
2385fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
239ddacabc24b515db4fa6c7a289ef81d8050c8062cDaniel Sandler    private void addNewAlarm() {
240e46be904bd76dff5a13aeef12e9bfeddac7bbe81Patrick Scott        startActivity(new Intent(this, SetAlarm.class));
241f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott    }
242f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott
2435fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    @Override
2445fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    protected void onDestroy() {
2455fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        super.onDestroy();
2465fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        ToastMaster.cancelToast();
2475fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mCursor.deactivate();
2485fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    }
2495fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
2505fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    @Override
2510dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott    public void onCreateContextMenu(ContextMenu menu, View view,
2520dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott            ContextMenuInfo menuInfo) {
2530dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        // Inflate the menu from xml.
2540dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        getMenuInflater().inflate(R.menu.context_menu, menu);
2550dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott
2560dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        // Use the current item to create a custom view for the header.
2570dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        final AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
2580dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        final Cursor c =
2590dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott                (Cursor) mAlarmsList.getAdapter().getItem((int) info.position);
260d776e51cec79901a0d656bba7b1f3780cceac57dPatrick Scott        final Alarm alarm = new Alarm(c);
2610dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott
2620dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        // Construct the Calendar to compute the time.
2630dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        final Calendar cal = Calendar.getInstance();
264d776e51cec79901a0d656bba7b1f3780cceac57dPatrick Scott        cal.set(Calendar.HOUR_OF_DAY, alarm.hour);
265d776e51cec79901a0d656bba7b1f3780cceac57dPatrick Scott        cal.set(Calendar.MINUTE, alarm.minutes);
2660dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        final String time = Alarms.formatTime(this, cal);
2670dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott
2680dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        // Inflate the custom view and set each TextView's text.
2690dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        final View v = mFactory.inflate(R.layout.context_menu_header, null);
2700dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        TextView textView = (TextView) v.findViewById(R.id.header_time);
2710dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        textView.setText(time);
2720dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        textView = (TextView) v.findViewById(R.id.header_label);
273d776e51cec79901a0d656bba7b1f3780cceac57dPatrick Scott        textView.setText(alarm.label);
2740dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott
2750dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        // Set the custom view on the menu.
2760dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        menu.setHeaderView(v);
27790c4833f94f4775f747333f8bd67107364a1d9c1Patrick Scott        // Change the text based on the state of the alarm.
27890c4833f94f4775f747333f8bd67107364a1d9c1Patrick Scott        if (alarm.enabled) {
27990c4833f94f4775f747333f8bd67107364a1d9c1Patrick Scott            menu.findItem(R.id.enable_alarm).setTitle(R.string.disable_alarm);
28090c4833f94f4775f747333f8bd67107364a1d9c1Patrick Scott        }
2810dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott    }
2820dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott
283e3f01aff06d4bddb09215ccad3d8502de9164966Patrick Scott    @Override
284e3f01aff06d4bddb09215ccad3d8502de9164966Patrick Scott    public boolean onOptionsItemSelected(MenuItem item) {
285f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott        switch (item.getItemId()) {
286f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott            case R.id.menu_item_settings:
287f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott                startActivity(new Intent(this, SettingsActivity.class));
288f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott                return true;
289f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott            case R.id.menu_item_desk_clock:
290f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott                startActivity(new Intent(this, DeskClock.class));
291f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott                return true;
292f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott            case R.id.menu_item_add_alarm:
293f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott                addNewAlarm();
294f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott                return true;
295f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott            default:
296f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott                break;
297e3f01aff06d4bddb09215ccad3d8502de9164966Patrick Scott        }
298f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott        return super.onOptionsItemSelected(item);
299e3f01aff06d4bddb09215ccad3d8502de9164966Patrick Scott    }
300e3f01aff06d4bddb09215ccad3d8502de9164966Patrick Scott
301e3f01aff06d4bddb09215ccad3d8502de9164966Patrick Scott    @Override
302e3f01aff06d4bddb09215ccad3d8502de9164966Patrick Scott    public boolean onCreateOptionsMenu(Menu menu) {
303e3f01aff06d4bddb09215ccad3d8502de9164966Patrick Scott        getMenuInflater().inflate(R.menu.alarm_list_menu, menu);
304f1e8069bcd1755d9ef2322b705d8d9d6b83728f0Patrick Scott        return super.onCreateOptionsMenu(menu);
305e3f01aff06d4bddb09215ccad3d8502de9164966Patrick Scott    }
306e3f01aff06d4bddb09215ccad3d8502de9164966Patrick Scott
3070dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott    public void onItemClick(AdapterView parent, View v, int pos, long id) {
3080dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        Intent intent = new Intent(this, SetAlarm.class);
309d776e51cec79901a0d656bba7b1f3780cceac57dPatrick Scott        intent.putExtra(Alarms.ALARM_ID, (int) id);
3100dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott        startActivity(intent);
3110dffdefe54299ee01b45574244c92d3a06507b87Patrick Scott    }
3125fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project}
313