// TODO don't lose data when getting data that our UI can't handle
  private static void copyEventRecurrenceToModel(final EventRecurrence er, RecurrenceModel model) {
    // Freq:
    switch (er.freq) {
      case EventRecurrence.HOURLY:
        model.freq = RecurrenceModel.FREQ_HOURLY;
        break;
      case EventRecurrence.DAILY:
        model.freq = RecurrenceModel.FREQ_DAILY;
        break;
      case EventRecurrence.MONTHLY:
        model.freq = RecurrenceModel.FREQ_MONTHLY;
        break;
      case EventRecurrence.YEARLY:
        model.freq = RecurrenceModel.FREQ_YEARLY;
        break;
      case EventRecurrence.WEEKLY:
        model.freq = RecurrenceModel.FREQ_WEEKLY;
        break;
      default:
        throw new IllegalStateException("freq=" + er.freq);
    }

    // Interval:
    if (er.interval > 0) {
      model.interval = er.interval;
    }

    // End:
    // End by count:
    model.endCount = er.count;
    if (model.endCount > 0) {
      model.end = RecurrenceModel.END_BY_COUNT;
    }

    // End by date:
    if (!TextUtils.isEmpty(er.until)) {
      if (model.endDate == null) {
        model.endDate = new Time();
      }

      try {
        model.endDate.parse(er.until);
      } catch (TimeFormatException e) {
        model.endDate = null;
      }

      // LIMITATION: The UI can only handle END_BY_DATE or END_BY_COUNT
      if (model.end == RecurrenceModel.END_BY_COUNT && model.endDate != null) {
        throw new IllegalStateException("freq=" + er.freq);
      }

      model.end = RecurrenceModel.END_BY_DATE;
    }

    // Weekly: repeat by day of week or Monthly: repeat by nth day of week
    // in the month
    Arrays.fill(model.weeklyByDayOfWeek, false);
    if (er.bydayCount > 0) {
      int count = 0;
      for (int i = 0; i < er.bydayCount; i++) {
        int dayOfWeek = EventRecurrence.day2TimeDay(er.byday[i]);
        model.weeklyByDayOfWeek[dayOfWeek] = true;

        if (model.freq == RecurrenceModel.FREQ_MONTHLY
            && isSupportedMonthlyByNthDayOfWeek(er.bydayNum[i])) {
          // LIMITATION: Can handle only (one) weekDayNum in nth or last and only
          // when
          // monthly
          model.monthlyByDayOfWeek = dayOfWeek;
          model.monthlyByNthDayOfWeek = er.bydayNum[i];
          model.monthlyRepeat = RecurrenceModel.MONTHLY_BY_NTH_DAY_OF_WEEK;
          count++;
        }
      }

      if (model.freq == RecurrenceModel.FREQ_MONTHLY) {
        if (er.bydayCount != 1) {
          // Can't handle 1st Monday and 2nd Wed
          throw new IllegalStateException("Can handle only 1 byDayOfWeek in monthly");
        }
        if (count != 1) {
          throw new IllegalStateException(
              "Didn't specify which nth day of week to repeat for a monthly");
        }
      }
    }

    // Monthly by day of month
    if (model.freq == RecurrenceModel.FREQ_MONTHLY) {
      if (er.bymonthdayCount == 1) {
        if (model.monthlyRepeat == RecurrenceModel.MONTHLY_BY_NTH_DAY_OF_WEEK) {
          throw new IllegalStateException(
              "Can handle only by monthday or by nth day of week, not both");
        }
        model.monthlyByMonthDay = er.bymonthday[0];
        model.monthlyRepeat = RecurrenceModel.MONTHLY_BY_DATE;
      } else if (er.bymonthCount > 1) {
        // LIMITATION: Can handle only one month day
        throw new IllegalStateException("Can handle only one bymonthday");
      }
    }
  }
  public void updateDialog() {
    // Interval
    // Checking before setting because this causes infinite recursion
    // in afterTextWatcher
    final String intervalStr = Integer.toString(mModel.interval);
    if (!intervalStr.equals(mInterval.getText().toString())) {
      mInterval.setText(intervalStr);
    }

    mFreqSpinner.setSelection(mModel.freq);
    mWeekGroup.setVisibility(mModel.freq == RecurrenceModel.FREQ_WEEKLY ? View.VISIBLE : View.GONE);
    mWeekGroup2.setVisibility(
        mModel.freq == RecurrenceModel.FREQ_WEEKLY ? View.VISIBLE : View.GONE);
    mMonthGroup.setVisibility(
        mModel.freq == RecurrenceModel.FREQ_MONTHLY ? View.VISIBLE : View.GONE);

    switch (mModel.freq) {
      case RecurrenceModel.FREQ_HOURLY:
        mIntervalResId = R.plurals.recurrence_interval_hourly;
        break;
      case RecurrenceModel.FREQ_DAILY:
        mIntervalResId = R.plurals.recurrence_interval_daily;
        break;

      case RecurrenceModel.FREQ_WEEKLY:
        mIntervalResId = R.plurals.recurrence_interval_weekly;
        for (int i = 0; i < 7; i++) {
          mWeekByDayButtons[i].setChecked(mModel.weeklyByDayOfWeek[i]);
        }
        break;

      case RecurrenceModel.FREQ_MONTHLY:
        mIntervalResId = R.plurals.recurrence_interval_monthly;

        if (mModel.monthlyRepeat == RecurrenceModel.MONTHLY_BY_DATE) {
          mMonthRepeatByRadioGroup.check(R.id.repeatMonthlyByNthDayOfMonth);
        } else if (mModel.monthlyRepeat == RecurrenceModel.MONTHLY_BY_NTH_DAY_OF_WEEK) {
          mMonthRepeatByRadioGroup.check(R.id.repeatMonthlyByNthDayOfTheWeek);
        }

        if (mMonthRepeatByDayOfWeekStr == null) {
          if (mModel.monthlyByNthDayOfWeek == 0) {
            mModel.monthlyByNthDayOfWeek = (mTime.monthDay + 6) / 7;
            // Since not all months have 5 weeks, we convert 5th NthDayOfWeek to
            // -1 for last monthly day of the week
            if (mModel.monthlyByNthDayOfWeek >= FIFTH_WEEK_IN_A_MONTH) {
              mModel.monthlyByNthDayOfWeek = LAST_NTH_DAY_OF_WEEK;
            }
            mModel.monthlyByDayOfWeek = mTime.weekDay;
          }

          String[] monthlyByNthDayOfWeekStrs =
              mMonthRepeatByDayOfWeekStrs[mModel.monthlyByDayOfWeek];

          // TODO(psliwowski): Find a better way handle -1 indexes
          int msgIndex =
              mModel.monthlyByNthDayOfWeek < 0
                  ? FIFTH_WEEK_IN_A_MONTH
                  : mModel.monthlyByNthDayOfWeek;
          mMonthRepeatByDayOfWeekStr = monthlyByNthDayOfWeekStrs[msgIndex - 1];
          mRepeatMonthlyByNthDayOfWeek.setText(mMonthRepeatByDayOfWeekStr);
        }
        break;

      case RecurrenceModel.FREQ_YEARLY:
        mIntervalResId = R.plurals.recurrence_interval_yearly;
        break;
    }
    updateIntervalText();
    updateDoneButtonState();

    mEndSpinner.setSelection(mModel.end);
    if (mModel.end == RecurrenceModel.END_BY_DATE) {
      final String dateStr =
          DateUtils.formatDateTime(
              getActivity(), mModel.endDate.toMillis(false), DateUtils.FORMAT_NUMERIC_DATE);
      mEndDateTextView.setText(dateStr);
    } else {
      if (mModel.end == RecurrenceModel.END_BY_COUNT) {
        // Checking before setting because this causes infinite
        // recursion
        // in afterTextWatcher
        final String countStr = Integer.toString(mModel.endCount);
        if (!countStr.equals(mEndCount.getText().toString())) {
          mEndCount.setText(countStr);
        }
      }
    }
  }