Exemple #1
0
  /**
   * Get the nearest alarm from preference file.
   *
   * @param context
   * @return the nearest alarm object, if not set, return null.
   */
  public static Alarm getNearestAlarm(final Context context) {
    SharedPreferences prefs = context.getSharedPreferences(NEAREST_ALARM_PREFERENCES, 0);
    int alarmId = prefs.getInt(PREF_NEAREST_ALARM_ID, -1);

    if (alarmId == -1) {
      return null;
    }

    ContentResolver cr = context.getContentResolver();
    return Alarms.getAlarm(cr, alarmId);
  }
  /**
   * 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();
    }
  }