private DaysOfWeek getDaysFromIntent(Intent intent) { final DaysOfWeek daysOfWeek = new DaysOfWeek(0); final ArrayList<Integer> days = intent.getIntegerArrayListExtra(EXTRA_DAYS); if (days != null) { final int[] daysArray = new int[days.size()]; for (int i = 0; i < days.size(); i++) { daysArray[i] = days.get(i); } daysOfWeek.setDaysOfWeek(true, daysArray); } else { // API says to use an ArrayList<Integer> but we allow the user to use a int[] too. final int[] daysArray = intent.getIntArrayExtra(EXTRA_DAYS); if (daysArray != null) { daysOfWeek.setDaysOfWeek(true, daysArray); } } return daysOfWeek; }
/** * * 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(); }