Пример #1
0
  private void setSelectionFromIntent(
      Intent intent, int hour, int minutes, StringBuilder selection, List<String> args) {
    selection.append(Alarm.HOUR).append("=?");
    args.add(String.valueOf(hour));
    selection.append(" AND ").append(Alarm.MINUTES).append("=?");
    args.add(String.valueOf(minutes));

    if (intent.hasExtra(EXTRA_MESSAGE)) {
      selection.append(" AND ").append(Alarm.LABEL).append("=?");
      args.add(getMessageFromIntent(intent));
    }

    // Days is treated differently that other fields because if days is not specified, it
    // explicitly means "not recurring".
    selection.append(" AND ").append(Alarm.DAYS_OF_WEEK).append("=?");
    args.add(
        String.valueOf(
            intent.hasExtra(EXTRA_DAYS)
                ? getDaysFromIntent(intent).getBitSet()
                : DaysOfWeek.NO_DAYS_SET));

    if (intent.hasExtra(EXTRA_VIBRATE)) {
      selection.append(" AND ").append(Alarm.VIBRATE).append("=?");
      args.add(intent.getBooleanExtra(EXTRA_VIBRATE, false) ? "1" : "0");
    }

    if (intent.hasExtra(EXTRA_RINGTONE)) {
      selection.append(" AND ").append(Alarm.RINGTONE).append("=?");

      String ringTone = intent.getStringExtra(EXTRA_RINGTONE);
      if (ringTone == null) {
        // If the intent explicitly specified a NULL ringtone, treat it as the default
        // ringtone.
        ringTone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM).toString();
      } else if (VALUE_RINGTONE_SILENT.equals(ringTone) || ringTone.isEmpty()) {
        ringTone = Alarm.NO_RINGTONE;
      }
      args.add(ringTone);
    }
  }
Пример #2
0
  /**
   * * Processes the SET_ALARM intent
   *
   * @param intent
   */
  private void handleSetAlarm(Intent intent) {
    // If not provided or invalid, show UI
    final int hour = intent.getIntExtra(EXTRA_HOUR, -1);

    // If not provided, use zero. If it is provided, make sure it's valid, otherwise, show UI
    final int minutes;
    if (intent.hasExtra(EXTRA_MINUTES)) {
      minutes = intent.getIntExtra(EXTRA_MINUTES, -1);
    } else {
      minutes = 0;
    }
    if (hour < 0 || hour > 23 || minutes < 0 || minutes > 59) {
      // Intent has no time or an invalid time, open the alarm creation UI
      Intent createAlarm = Alarm.createIntent(this, DeskClock.class, Alarm.INVALID_ID);
      createAlarm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      createAlarm.putExtra(AlarmClockFragment.ALARM_CREATE_NEW_INTENT_EXTRA, true);
      createAlarm.putExtra(DeskClock.SELECT_TAB_INTENT_EXTRA, DeskClock.ALARM_TAB_INDEX);
      startActivity(createAlarm);
      finish();
      return;
    }

    final boolean skipUi = intent.getBooleanExtra(EXTRA_SKIP_UI, false);

    final StringBuilder selection = new StringBuilder();
    final List<String> args = new ArrayList<String>();
    setSelectionFromIntent(intent, hour, minutes, selection, args);

    // Check if the alarm already exists and handle it
    final ContentResolver cr = getContentResolver();
    final List<Alarm> alarms =
        Alarm.getAlarms(cr, selection.toString(), args.toArray(new String[args.size()]));
    if (!alarms.isEmpty()) {
      Alarm alarm = alarms.get(0);
      alarm.enabled = true;
      Alarm.updateAlarm(cr, alarm);

      // Delete all old instances and create a new one with updated values
      AlarmStateManager.deleteAllInstances(this, alarm.id);
      setupInstance(alarm.createInstanceAfter(Calendar.getInstance()), skipUi);
      finish();
      return;
    }

    // Otherwise insert it and handle it
    final String message = getMessageFromIntent(intent);
    final DaysOfWeek daysOfWeek = getDaysFromIntent(intent);
    final boolean vibrate = intent.getBooleanExtra(EXTRA_VIBRATE, false);
    final String alert = intent.getStringExtra(EXTRA_RINGTONE);

    Alarm alarm = new Alarm(hour, minutes);
    alarm.enabled = true;
    alarm.label = message;
    alarm.daysOfWeek = daysOfWeek;
    alarm.vibrate = vibrate;

    if (alert == null) {
      alarm.alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    } else if (VALUE_RINGTONE_SILENT.equals(alert) || alert.isEmpty()) {
      alarm.alert = Alarm.NO_RINGTONE_URI;
    } else {
      alarm.alert = Uri.parse(alert);
    }
    alarm.deleteAfterUse = !daysOfWeek.isRepeating() && skipUi;

    alarm = Alarm.addAlarm(cr, alarm);
    setupInstance(alarm.createInstanceAfter(Calendar.getInstance()), skipUi);
    finish();
  }