private void updatePrefs(Alarm alarm) {
   mId = alarm.id;
   mEnabledPref.setChecked(alarm.enabled);
   mLabel.setText(alarm.label);
   mLabel.setSummary(alarm.label);
   mHour = alarm.hour;
   mMinutes = alarm.minutes;
   mRepeatPref.setDaysOfWeek(alarm.daysOfWeek);
   mVibratePref.setChecked(alarm.vibrate);
   // Give the alert uri to the preference.
   mAlarmPref.setAlert(alarm.alert);
   updateTime();
 }
  private long saveAlarm() {
    Alarm alarm = new Alarm();
    alarm.id = mId;
    alarm.enabled = mEnabledPref.isChecked();
    alarm.hour = mHour;
    alarm.minutes = mMinutes;
    alarm.daysOfWeek = mRepeatPref.getDaysOfWeek();
    alarm.vibrate = mVibratePref.isChecked();
    alarm.label = mLabel.getText();
    alarm.alert = mAlarmPref.getAlert();

    long time;
    if (alarm.id == -1) {
      time = Alarms.addAlarm(this, alarm);
      // addAlarm populates the alarm with the new id. Update mId so that
      // changes to other preferences update the new alarm.
      mId = alarm.id;
    } else {
      time = Alarms.setAlarm(this, alarm);
    }
    return time;
  }
  /**
   * Set an alarm. Requires an Alarms.ALARM_ID to be passed in as an extra. FIXME: Pass an Alarm
   * object like every other Activity.
   */
  @Override
  protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    // Override the default content view.
    setContentView(R.layout.set_alarm);

    addPreferencesFromResource(R.xml.alarm_prefs);

    // Get each preference so we can retrieve the value later.
    mLabel = (EditTextPreference) findPreference("label");
    mLabel.setOnPreferenceChangeListener(
        new Preference.OnPreferenceChangeListener() {
          public boolean onPreferenceChange(Preference p, Object newValue) {
            String val = (String) newValue;
            // Set the summary based on the new label.
            p.setSummary(val);
            if (val != null && !val.equals(mLabel.getText())) {
              // Call through to the generic listener.
              return SetAlarm.this.onPreferenceChange(p, newValue);
            }
            return true;
          }
        });
    mEnabledPref = (CheckBoxPreference) findPreference("enabled");
    mEnabledPref.setOnPreferenceChangeListener(
        new Preference.OnPreferenceChangeListener() {
          public boolean onPreferenceChange(Preference p, Object newValue) {
            // Pop a toast when enabling alarms.
            if (!mEnabledPref.isChecked()) {
              popAlarmSetToast(SetAlarm.this, mHour, mMinutes, mRepeatPref.getDaysOfWeek());
            }
            return SetAlarm.this.onPreferenceChange(p, newValue);
          }
        });
    mTimePref = findPreference("time");
    mAlarmPref = (AlarmPreference) findPreference("alarm");
    mAlarmPref.setOnPreferenceChangeListener(this);
    mVibratePref = (CheckBoxPreference) findPreference("vibrate");
    mVibratePref.setOnPreferenceChangeListener(this);
    mRepeatPref = (RepeatPreference) findPreference("setRepeat");
    mRepeatPref.setOnPreferenceChangeListener(this);

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

    Alarm alarm = null;
    if (mId == -1) {
      // No alarm id means create a new alarm.
      alarm = new Alarm();
    } else {
      /* load alarm details from database */
      alarm = Alarms.getAlarm(getContentResolver(), mId);
      // Bad alarm, bail to avoid a NPE.
      if (alarm == null) {
        finish();
        return;
      }
    }
    mOriginalAlarm = alarm;

    updatePrefs(mOriginalAlarm);

    // We have to do this to get the save/cancel buttons to highlight on
    // their own.
    getListView().setItemsCanFocus(true);

    // Attach actions to each button.
    Button b = (Button) findViewById(R.id.alarm_save);
    b.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View v) {
            saveAlarm();
            finish();
          }
        });
    final Button revert = (Button) findViewById(R.id.alarm_revert);
    revert.setEnabled(false);
    revert.setOnClickListener(
        new View.OnClickListener() {
          public void onClick(View v) {
            int newId = mId;
            updatePrefs(mOriginalAlarm);
            // "Revert" on a newly created alarm should delete it.
            if (mOriginalAlarm.id == -1) {
              Alarms.deleteAlarm(SetAlarm.this, newId);
            } else {
              saveAlarm();
            }
            revert.setEnabled(false);
          }
        });
    b = (Button) findViewById(R.id.alarm_delete);
    if (mId == -1) {
      b.setEnabled(false);
    } else {
      b.setOnClickListener(
          new View.OnClickListener() {
            public void onClick(View v) {
              deleteAlarm();
            }
          });
    }

    // The last thing we do is pop the time picker if this is a new alarm.
    if (mId == -1) {
      // Assume the user hit cancel
      mTimePickerCancelled = true;
      showTimePicker();
    }
  }
 private void updateTime() {
   if (Log.LOGV) {
     Log.v("updateTime " + mId);
   }
   mTimePref.setSummary(Alarms.formatTime(this, mHour, mMinutes, mRepeatPref.getDaysOfWeek()));
 }