FileDocCategorySizeDatePackage
SetAlarm.javaAPI DocAndroid 1.5 API14516Wed May 06 22:42:42 BST 2009com.android.alarmclock

SetAlarm

public class SetAlarm extends android.preference.PreferenceActivity implements Alarms.AlarmSettings, TimePickerDialog.OnTimeSetListener
Manages each alarm

Fields Summary
private android.preference.EditTextPreference
mLabel
private android.preference.CheckBoxPreference
mAlarmOnPref
private android.preference.Preference
mTimePref
private AlarmPreference
mAlarmPref
private android.preference.CheckBoxPreference
mVibratePref
private RepeatPreference
mRepeatPref
private android.database.ContentObserver
mAlarmsChangeObserver
private android.view.MenuItem
mDeleteAlarmItem
private android.view.MenuItem
mTestAlarmItem
private int
mId
private int
mHour
private int
mMinutes
private Alarms.DaysOfWeek
mDaysOfWeek
private boolean
mReportAlarmCalled
private static final int
DIALOG_TIMEPICKER
Constructors Summary
Methods Summary
static java.lang.StringformatToast(android.content.Context context, int hour, int minute, Alarms.DaysOfWeek daysOfWeek)
format "Alarm set for 2 days 7 hours and 53 minutes from now"

        long alarm = Alarms.calculateAlarm(hour, minute,
                                           daysOfWeek).getTimeInMillis();
        long delta = alarm - System.currentTimeMillis();;
        long hours = delta / (1000 * 60 * 60);
        long minutes = delta / (1000 * 60) % 60;
        long days = hours / 24;
        hours = hours % 24;

        String daySeq = (days == 0) ? "" :
                (days == 1) ? context.getString(R.string.day) :
                context.getString(R.string.days, Long.toString(days));

        String minSeq = (minutes == 0) ? "" :
                (minutes == 1) ? context.getString(R.string.minute) :
                context.getString(R.string.minutes, Long.toString(minutes));

        String hourSeq = (hours == 0) ? "" :
                (hours == 1) ? context.getString(R.string.hour) :
                context.getString(R.string.hours, Long.toString(hours));

        boolean dispDays = days > 0;
        boolean dispHour = hours > 0;
        boolean dispMinute = minutes > 0;

        String ret;
        if (!(dispDays || dispHour || dispMinute)) {
            ret = context.getString(R.string.subminute);
        } else {
            String parts[] = new String[5];
            parts[0] = daySeq;
            parts[1] = !dispDays ? "" :
                    dispHour && dispMinute ? context.getString(R.string.space) :
                    !dispHour && !dispMinute ? "" :
                    context.getString(R.string.and);
            parts[2] = dispHour ? hourSeq : "";
            parts[3] = dispHour && dispMinute ? context.getString(R.string.and) : "";
            parts[4] = dispMinute ? minSeq : "";
            ret = context.getString(R.string.combiner, (Object[])parts);
        }

        ret = context.getString(R.string.alarm_set, ret);
        /* if (Log.LOGV) Log.v("** TOAST daySeq " + daySeq + " hourSeq " + hourSeq +
           " minSeq " + minSeq + " ret " + ret); */
        return ret;
    
private android.net.UrigetDefaultAlarm()
picks the first alarm available

        RingtoneManager ringtoneManager = new RingtoneManager(this);
        ringtoneManager.setType(RingtoneManager.TYPE_ALARM);
        return ringtoneManager.getRingtoneUri(0);
    
protected voidonCreate(android.os.Bundle icicle)
Set an alarm. Requires an Alarms.ID to be passed in as an extra

        super.onCreate(icicle);

        addPreferencesFromResource(R.xml.alarm_prefs);
        mLabel = (EditTextPreference) findPreference("label");
        mLabel.setOnPreferenceChangeListener(
                new Preference.OnPreferenceChangeListener() {
                    public boolean onPreferenceChange(Preference p,
                            Object newValue) {
                        p.setSummary((String) newValue);
                        saveAlarm(false, (String) newValue);
                        return true;
                    }
                });
        mAlarmOnPref = (CheckBoxPreference)findPreference("on");
        mTimePref = findPreference("time");
        mAlarmPref = (AlarmPreference) findPreference("alarm");
        mVibratePref = (CheckBoxPreference) findPreference("vibrate");
        mRepeatPref = (RepeatPreference) findPreference("setRepeat");

        Intent i = getIntent();
        mId = i.getIntExtra(Alarms.ID, -1);
        if (Log.LOGV) Log.v("In SetAlarm, alarm id = " + mId);

        mReportAlarmCalled = false;
        /* load alarm details from database */
        Alarms.getAlarm(getContentResolver(), this, mId);
        /* This should never happen, but does occasionally with the monkey.
         * I believe it's a race condition where a deleted alarm is opened
         * before the alarm list is refreshed. */
        if (!mReportAlarmCalled) {
            Log.e("reportAlarm never called!");
            finish();
        }

        mAlarmsChangeObserver = new AlarmsChangeObserver();
        getContentResolver().registerContentObserver(
                Alarms.AlarmColumns.CONTENT_URI, true, mAlarmsChangeObserver);

        mAlarmPref.setRingtoneChangedListener(new RingtoneChangedListener());
        mRepeatPref.setOnRepeatChangedObserver(new OnRepeatChangedObserver());
    
protected android.app.DialogonCreateDialog(int id)

        Dialog d;

        switch (id) {
        case DIALOG_TIMEPICKER:
            d = new TimePickerDialog(
                    SetAlarm.this,
                    this,
                    0,
                    0,
                    DateFormat.is24HourFormat(SetAlarm.this));
            d.setTitle(getResources().getString(R.string.time));
            break;
        default:
            d = null;
        }

        return d;
    
public booleanonCreateOptionsMenu(android.view.Menu menu)

        super.onCreateOptionsMenu(menu);

        mDeleteAlarmItem = menu.add(0, 0, 0, R.string.delete_alarm);
        mDeleteAlarmItem.setIcon(android.R.drawable.ic_menu_delete);

        if (AlarmClock.DEBUG) {
            mTestAlarmItem = menu.add(0, 0, 0, "test alarm");
        }


        return true;
    
protected voidonDestroy()

        super.onDestroy();
        getContentResolver().unregisterContentObserver(mAlarmsChangeObserver);
    
public booleanonOptionsItemSelected(android.view.MenuItem item)

        if (item == mDeleteAlarmItem) {
            Alarms.deleteAlarm(this, mId);
            finish();
            return true;
        }
        if (AlarmClock.DEBUG) {
            if (item == mTestAlarmItem) {
                setTestAlarm();
                return true;
            }
        }

        return false;
    
public booleanonPreferenceTreeClick(android.preference.PreferenceScreen preferenceScreen, android.preference.Preference preference)


        if (preference == mTimePref) {
            showDialog(DIALOG_TIMEPICKER);
        } else if (preference == mAlarmOnPref) {
            saveAlarm(true);
        } else if (preference == mVibratePref) {
            saveAlarm(false);
        }

        return super.onPreferenceTreeClick(preferenceScreen, preference);
    
protected voidonPrepareDialog(int id, android.app.Dialog dialog)

        super.onPrepareDialog(id, dialog);

        switch (id) {
        case DIALOG_TIMEPICKER:
            TimePickerDialog timePicker = (TimePickerDialog)dialog;
            timePicker.updateTime(mHour, mMinutes);
            break;
        }
    
public voidonTimeSet(android.widget.TimePicker view, int hourOfDay, int minute)

        mHour = hourOfDay;
        mMinutes = minute;
        mAlarmOnPref.setChecked(true);
        saveAlarm(true);
    
static voidpopAlarmSetToast(android.content.Context context, int hour, int minute, Alarms.DaysOfWeek daysOfWeek)
Display a toast that tells the user how long until the alarm goes off. This helps prevent "am/pm" mistakes.


        String toastText = formatToast(context, hour, minute, daysOfWeek);
        Toast toast = Toast.makeText(context, toastText, Toast.LENGTH_LONG);
        ToastMaster.setToast(toast);
        toast.show();
    
public voidreportAlarm(int idx, boolean enabled, int hour, int minutes, Alarms.DaysOfWeek daysOfWeek, boolean vibrate, java.lang.String label, java.lang.String alert)
Alarms.AlarmSettings implementation. Database feeds current settings in through this call


        if (label == null || label.length() == 0) {
            label = getString(R.string.default_label);
        }
        mLabel.setText(label);
        mLabel.setSummary(label);
        mHour = hour;
        mMinutes = minutes;
        mAlarmOnPref.setChecked(enabled);
        mDaysOfWeek.set(daysOfWeek);
        mVibratePref.setChecked(vibrate);

        if (alert == null || alert.length() == 0) {
            if (Log.LOGV) Log.v("****** reportAlarm null or 0-length alert");
            mAlarmPref.mAlert = getDefaultAlarm();
            if (mAlarmPref.mAlert == null) {
                Log.e("****** Default Alarm null");
            }
        } else {
            mAlarmPref.mAlert = Uri.parse(alert);
            if (mAlarmPref.mAlert == null) {
                Log.e("****** Parsed null alarm. URI: " + alert);
            }
        }
        if (Log.LOGV) Log.v("****** reportAlarm uri " + alert + " alert " +
                            mAlarmPref.mAlert);
        updateTime();
        updateRepeat();
        updateAlarm(mAlarmPref.mAlert);

        mReportAlarmCalled = true;
    
private voidsaveAlarm(boolean popToast)

        saveAlarm(popToast, mLabel.getText());
    
private voidsaveAlarm(boolean popToast, java.lang.String label)
This version of saveAlarm uses the passed in label since mLabel may contain the old value (i.e. during the preference value change).

        if (mReportAlarmCalled && mAlarmPref.mAlert != null) {
            String alertString = mAlarmPref.mAlert.toString();
            saveAlarm(this, mId, mAlarmOnPref.isChecked(), mHour, mMinutes,
                      mDaysOfWeek, mVibratePref.isChecked(), label, alertString,
                      popToast);
        }
    
private static voidsaveAlarm(android.content.Context context, int id, boolean enabled, int hour, int minute, Alarms.DaysOfWeek daysOfWeek, boolean vibrate, java.lang.String label, java.lang.String alert, boolean popToast)
Write alarm out to persistent store and pops toast if alarm enabled

        if (Log.LOGV) Log.v("** saveAlarm " + id + " " + label + " " + enabled
                + " " + hour + " " + minute + " vibe " + vibrate);

        // Fix alert string first
        Alarms.setAlarm(context, id, enabled, hour, minute, daysOfWeek, vibrate,
                label, alert);

        if (enabled && popToast) {
            popAlarmSetToast(context, hour, minute, daysOfWeek);
        }
    
voidsetTestAlarm()
Test code: this is disabled for production build. Sets this alarm to go off on the next minute


        // start with now
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTimeInMillis(System.currentTimeMillis());

        int nowHour = c.get(java.util.Calendar.HOUR_OF_DAY);
        int nowMinute = c.get(java.util.Calendar.MINUTE);

        int minutes = (nowMinute + 1) % 60;
        int hour = nowHour + (nowMinute == 0? 1 : 0);

        saveAlarm(this, mId, true, hour, minutes, mDaysOfWeek, true,
                mLabel.getText(), mAlarmPref.mAlert.toString(), true);
    
private voidupdateAlarm(android.net.Uri ringtoneUri)

        if (Log.LOGV) Log.v("updateAlarm " + mId);
        Ringtone ringtone = RingtoneManager.getRingtone(SetAlarm.this, ringtoneUri);
        if (ringtone != null) {
            mAlarmPref.setSummary(ringtone.getTitle(SetAlarm.this));
        }
    
private voidupdateRepeat()

        if (Log.LOGV) Log.v("updateRepeat " + mId);
        mRepeatPref.setSummary(mDaysOfWeek.toString(this, true));
    
private voidupdateTime()

        if (Log.LOGV) Log.v("updateTime " + mId);
        mTimePref.setSummary(Alarms.formatTime(this, mHour, mMinutes, mDaysOfWeek));