// Implements OnItemSelectedListener interface
  // Freq spinner
  // End spinner
  @Override
  public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    if (parent == mFreqSpinner) {
      mModel.freq = position;
    } else if (parent == mEndSpinner) {
      switch (position) {
        case RecurrenceModel.END_NEVER:
          mModel.end = RecurrenceModel.END_NEVER;
          break;
        case RecurrenceModel.END_BY_DATE:
          mModel.end = RecurrenceModel.END_BY_DATE;
          break;
        case RecurrenceModel.END_BY_COUNT:
          mModel.end = RecurrenceModel.END_BY_COUNT;

          if (mModel.endCount <= 1) {
            mModel.endCount = 1;
          } else if (mModel.endCount > COUNT_MAX) {
            mModel.endCount = COUNT_MAX;
          }
          updateEndCountText();
          break;
      }
      mEndCount.setVisibility(
          mModel.end == RecurrenceModel.END_BY_COUNT ? View.VISIBLE : View.GONE);
      mEndDateTextView.setVisibility(
          mModel.end == RecurrenceModel.END_BY_DATE ? View.VISIBLE : View.GONE);
      mPostEndCount.setVisibility(
          mModel.end == RecurrenceModel.END_BY_COUNT && !mHidePostEndCount
              ? View.VISIBLE
              : View.GONE);
    }
    updateDialog();
  }
  // 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");
      }
    }
  }