AlarmClock.java revision 5fedae0fb8cc29db88719329d52bdd62aad14277
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;
375fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.view.ViewGroup;
385fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.widget.CursorAdapter;
395fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.widget.ListView;
405fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.widget.TextView;
415fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport android.widget.CheckBox;
425fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
435fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectimport java.util.Calendar;
445fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
455fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project/**
465fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project * AlarmClock application.
475fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project */
485fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Projectpublic class AlarmClock extends Activity {
495fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
505fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    final static String PREFERENCES = "AlarmClock";
515fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    final static int SET_ALARM = 1;
525fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    final static String PREF_CLOCK_FACE = "face";
535fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    final static String PREF_SHOW_CLOCK = "show_clock";
545fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
555fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    /** Cap alarm count at this number */
565fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    final static int MAX_ALARM_COUNT = 12;
575fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
585fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    /** This must be false for production.  If true, turns on logging,
595fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        test code, etc. */
605fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    final static boolean DEBUG = false;
615fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
625fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    private SharedPreferences mPrefs;
635fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    private LayoutInflater mFactory;
645fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    private ViewGroup mClockLayout;
655fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    private View mClock = null;
665fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    private MenuItem mAddAlarmItem;
675fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    private MenuItem mToggleClockItem;
685fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    private ListView mAlarmsList;
695fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    private Cursor mCursor;
705fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
715fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    /**
725fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project     * Which clock face to show
735fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project     */
745fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    private int mFace = -1;
755fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
765fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    /*
775fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project     * FIXME: it would be nice for this to live in an xml config file.
785fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project     */
795fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    final static int[] CLOCKS = {
805fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        R.layout.clock_basic_bw,
815fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        R.layout.clock_googly,
825fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        R.layout.clock_droid2,
835fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        R.layout.clock_droids,
845fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        R.layout.digital_clock
855fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    };
865fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
875fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    private class AlarmTimeAdapter extends CursorAdapter {
885fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        public AlarmTimeAdapter(Context context, Cursor cursor) {
895fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            super(context, cursor);
905fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        }
915fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
925fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        public View newView(Context context, Cursor cursor, ViewGroup parent) {
935fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            View ret = mFactory.inflate(R.layout.alarm_time, parent, false);
945fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            DigitalClock digitalClock = (DigitalClock)ret.findViewById(R.id.digitalClock);
955fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            digitalClock.setLive(false);
965fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            if (Log.LOGV) Log.v("newView " + cursor.getPosition());
975fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            return ret;
985fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        }
995fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
1005fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        public void bindView(View view, Context context, Cursor cursor) {
1015fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            final int id = cursor.getInt(Alarms.AlarmColumns.ALARM_ID_INDEX);
1025fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            final int hour = cursor.getInt(Alarms.AlarmColumns.ALARM_HOUR_INDEX);
1035fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            final int minutes = cursor.getInt(Alarms.AlarmColumns.ALARM_MINUTES_INDEX);
1045fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            final Alarms.DaysOfWeek daysOfWeek = new Alarms.DaysOfWeek(
1055fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                    cursor.getInt(Alarms.AlarmColumns.ALARM_DAYS_OF_WEEK_INDEX));
1065fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            final boolean enabled = cursor.getInt(Alarms.AlarmColumns.ALARM_ENABLED_INDEX) == 1;
1075fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
1085fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            CheckBox onButton = (CheckBox)view.findViewById(R.id.alarmButton);
1095fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            onButton.setChecked(enabled);
1105fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            onButton.setOnClickListener(new OnClickListener() {
1115fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                    public void onClick(View v) {
1125fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                        boolean isChecked = ((CheckBox) v).isChecked();
1135fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                        Alarms.enableAlarm(AlarmClock.this, id, isChecked);
1145fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                        if (isChecked) {
1155fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                            SetAlarm.popAlarmSetToast(
1165fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                                    AlarmClock.this, hour, minutes, daysOfWeek);
1175fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                        }
1185fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                    }
1195fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            });
1205fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
1215fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            DigitalClock digitalClock = (DigitalClock)view.findViewById(R.id.digitalClock);
1225fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            if (Log.LOGV) Log.v("bindView " + cursor.getPosition() + " " + id + " " + hour +
1235fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                                ":" + minutes + " " + daysOfWeek.toString(context, true) + " dc " + digitalClock);
1245fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
1255fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            digitalClock.setOnClickListener(new OnClickListener() {
1265fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                    public void onClick(View v) {
1275fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                        if (true) {
1285fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                            Intent intent = new Intent(AlarmClock.this, SetAlarm.class);
1295fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                            intent.putExtra(Alarms.ID, id);
1305fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                            startActivityForResult(intent, SET_ALARM);
1315fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                        } else {
1325fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                            // TESTING: immediately pop alarm
1335fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                            Intent fireAlarm = new Intent(AlarmClock.this, AlarmAlert.class);
1345fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                            fireAlarm.putExtra(Alarms.ID, id);
1355fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                            fireAlarm.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1365fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                            startActivity(fireAlarm);
1375fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                        }
1385fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                    }
1395fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                });
1405fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
1415fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            // set the alarm text
1425fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            final Calendar c = Calendar.getInstance();
1435fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            c.set(Calendar.HOUR_OF_DAY, hour);
1445fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            c.set(Calendar.MINUTE, minutes);
1455fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            digitalClock.updateTime(c);
1465fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            TextView daysOfWeekView = (TextView) digitalClock.findViewById(R.id.daysOfWeek);
1475fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            daysOfWeekView.setText(daysOfWeek.toString(AlarmClock.this, false));
1485fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
1495fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            // Build context menu
1505fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            digitalClock.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
1515fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                    public void onCreateContextMenu(ContextMenu menu, View view,
1525fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                                                    ContextMenuInfo menuInfo) {
1535fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                        menu.setHeaderTitle(Alarms.formatTime(AlarmClock.this, c));
1545fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                        MenuItem deleteAlarmItem = menu.add(0, id, 0, R.string.delete_alarm);
1555fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                    }
1565fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                });
1575fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        }
1585fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    };
1595fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
1605fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    @Override
1615fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    public boolean onContextItemSelected(final MenuItem item) {
1625fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        // Confirm that the alarm will be deleted.
1635fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        new AlertDialog.Builder(this)
1645fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                .setTitle(getString(R.string.delete_alarm))
1655fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                .setMessage(getString(R.string.delete_alarm_confirm))
1665fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                .setPositiveButton(android.R.string.ok,
1675fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                        new DialogInterface.OnClickListener() {
1685fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                            public void onClick(DialogInterface d, int w) {
1695fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                                Alarms.deleteAlarm(AlarmClock.this,
1705fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                                        item.getItemId());
1715fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                            }
1725fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                        })
1735fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                .setNegativeButton(android.R.string.cancel, null)
1745fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                .show();
1755fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        return true;
1765fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    }
1775fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
1785fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    @Override
1795fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    protected void onCreate(Bundle icicle) {
1805fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        super.onCreate(icicle);
1815fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
1825fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        // sanity check -- no database, no clock
1835fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        if (getContentResolver() == null) {
1845fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            new AlertDialog.Builder(this)
1855fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                    .setTitle(getString(R.string.error))
1865fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                    .setMessage(getString(R.string.dberror))
1875fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                    .setPositiveButton(
1885fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                            android.R.string.ok,
1895fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                            new DialogInterface.OnClickListener() {
1905fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                                public void onClick(DialogInterface dialog, int which) {
1915fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                                    finish();
1925fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                                }
1935fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                            })
1945fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                    .setOnCancelListener(
1955fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                            new DialogInterface.OnCancelListener() {
1965fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                                public void onCancel(DialogInterface dialog) {
1975fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                                    finish();
1985fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                                }})
1995fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                    .setIcon(android.R.drawable.ic_dialog_alert)
2005fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                    .create().show();
2015fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            return;
2025fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        }
2035fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
2045fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        setContentView(R.layout.alarm_clock);
2055fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mFactory = LayoutInflater.from(this);
2065fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mPrefs = getSharedPreferences(PREFERENCES, 0);
2075fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
2085fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mCursor = Alarms.getAlarmsCursor(getContentResolver());
2095fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mAlarmsList = (ListView) findViewById(R.id.alarms_list);
2105fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mAlarmsList.setAdapter(new AlarmTimeAdapter(this, mCursor));
2115fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mAlarmsList.setVerticalScrollBarEnabled(true);
2125fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mAlarmsList.setItemsCanFocus(true);
2135fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
2145fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mClockLayout = (ViewGroup) findViewById(R.id.clock_layout);
2155fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mClockLayout.setOnClickListener(new View.OnClickListener() {
2165fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                public void onClick(View v) {
2175fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                    final Intent intent = new Intent(AlarmClock.this, ClockPicker.class);
2185fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2195fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                    startActivity(intent);
2205fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                }
2215fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            });
2225fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
2235fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        setClockVisibility(mPrefs.getBoolean(PREF_SHOW_CLOCK, true));
2245fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    }
2255fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
2265fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    @Override
2275fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    protected void onResume() {
2285fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        super.onResume();
2295fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
2305fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        int face = mPrefs.getInt(PREF_CLOCK_FACE, 0);
2315fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        if (mFace != face) {
2325fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            if (face < 0 || face >= AlarmClock.CLOCKS.length)
2335fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                mFace = 0;
2345fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            else
2355fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                mFace = face;
2365fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            inflateClock();
2375fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        }
2385fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    }
2395fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
2405fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    @Override
2415fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    protected void onDestroy() {
2425fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        super.onDestroy();
2435fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        ToastMaster.cancelToast();
2445fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mCursor.deactivate();
2455fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    }
2465fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
2475fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    protected void inflateClock() {
2485fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        if (mClock != null) {
2495fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            mClockLayout.removeView(mClock);
2505fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        }
2515fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mClock = mFactory.inflate(CLOCKS[mFace], null);
2525fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mClockLayout.addView(mClock, 0);
2535fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    }
2545fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
2555fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    @Override
2565fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    public boolean onCreateOptionsMenu(Menu menu) {
2575fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        super.onCreateOptionsMenu(menu);
2585fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
2595fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mAddAlarmItem = menu.add(0, 0, 0, R.string.add_alarm);
2605fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mAddAlarmItem.setIcon(android.R.drawable.ic_menu_add);
2615fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
2625fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mToggleClockItem = menu.add(0, 0, 0, R.string.hide_clock);
2635fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mToggleClockItem.setIcon(R.drawable.ic_menu_clock_face);
2645fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
2655fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        return true;
2665fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    }
2675fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
2685fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    /**
2695fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project     * Only allow user to add a new alarm if there are fewer than
2705fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project     * MAX_ALARM_COUNT
2715fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project     */
2725fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    @Override
2735fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    public boolean onPrepareOptionsMenu(Menu menu) {
2745fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        super.onPrepareOptionsMenu(menu);
2755fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mAddAlarmItem.setVisible(mAlarmsList.getChildCount() < MAX_ALARM_COUNT);
2765fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mToggleClockItem.setTitle(getClockVisibility() ? R.string.hide_clock :
2775fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project                                  R.string.show_clock);
2785fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        return true;
2795fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    }
2805fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
2815fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    @Override
2825fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    public boolean onOptionsItemSelected(MenuItem item) {
2835fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        if (item == mAddAlarmItem) {
2845fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            Uri uri = Alarms.addAlarm(getContentResolver());
2855fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            // FIXME: scroll to new item.  mAlarmsList.requestChildRectangleOnScreen() ?
2865fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            String segment = uri.getPathSegments().get(1);
2875fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            int newId = Integer.parseInt(segment);
2885fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            if (Log.LOGV) Log.v("In AlarmClock, new alarm id = " + newId);
2895fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            Intent intent = new Intent(AlarmClock.this, SetAlarm.class);
2905fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            intent.putExtra(Alarms.ID, newId);
2915fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            startActivityForResult(intent, SET_ALARM);
2925fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            return true;
2935fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        } else if (item == mToggleClockItem) {
2945fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            setClockVisibility(!getClockVisibility());
2955fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            saveClockVisibility();
2965fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project            return true;
2975fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        }
2985fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
2995fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        return false;
3005fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    }
3015fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
3025fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
3035fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    private boolean getClockVisibility() {
3045fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        return mClockLayout.getVisibility() == View.VISIBLE;
3055fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    }
3065fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
3075fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    private void setClockVisibility(boolean visible) {
3085fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mClockLayout.setVisibility(visible ? View.VISIBLE : View.GONE);
3095fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    }
3105fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project
3115fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    private void saveClockVisibility() {
3125fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project        mPrefs.edit().putBoolean(PREF_SHOW_CLOCK, getClockVisibility()).commit();
3135fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project    }
3145fedae0fb8cc29db88719329d52bdd62aad14277The Android Open Source Project}
315