public Recurring create() {
   database.beginTransaction();
   ContentValues values = getContentValues();
   values.remove("_id");
   int insertId = (int) database.insertOrThrow(TABLE, null, values);
   database.setTransactionSuccessful();
   database.endTransaction();
   return Recurring.get(insertId);
 }
 public static Recurring newRecurring(
     String label,
     int minutes,
     int hours,
     int days,
     int months,
     int years,
     boolean forDue,
     Calendar startDate,
     Calendar endDate,
     boolean temporary,
     boolean isExact,
     SparseBooleanArray weekdays) {
   Recurring r =
       new Recurring(
           0, label, minutes, hours, days, months, years, forDue, startDate, endDate, temporary,
           isExact, weekdays, null);
   return r.create();
 }
  @NonNull
  private Recurring getRecurring() {
    final SparseBooleanArray checked = new SparseBooleanArray();
    final int type = (mForDue ? 2 : 0) + mIntervalType.getSelectedItemPosition();
    for (int i = 0; i < mWeekByDayButtons.length; i++) {
      if (mWeekByDayButtons[i].isChecked()) {
        checked.put(TIME_DAY_TO_CALENDAR_DAY[i], (type == 2) && mWeekByDayButtons[i].isChecked());
      }
    }

    Log.d(TAG, "TYPE: " + type);
    int intervalMonths = 0;
    int intervalYears = 0;
    int intervalDays = 0;
    int intervalHours = 0;
    int intervalMinutes = 0;
    switch (type) {
      case 0:
        intervalMinutes = mIntervalValue;
        break;
      case 1:
        intervalHours = mIntervalValue;
        break;
      case 2:
        // weekdays, handled above
        break;
      case 3:
        intervalDays = mIntervalValue;
        break;
      case 4:
        intervalMonths = mIntervalValue;
        break;
      case 5:
        intervalYears = mIntervalValue;
        break;
      default:
        throw new IllegalStateException("Implement another case of intervall type");
    }
    return Recurring.newRecurring(
        "",
        intervalMinutes,
        intervalHours,
        intervalDays,
        intervalMonths,
        intervalYears,
        mForDue,
        mStartDate,
        mEndDate,
        true,
        mUseExact.isChecked(),
        checked);
  }
  public View createView(final LayoutInflater inflater) {
    this.ctx = new ContextThemeWrapper(getActivity(), R.style.MirakelBaseTheme);
    final List<Pair<Integer, String>> recurring = Recurring.getForDialog(this.mForDue);
    this.extraItems = 1;
    final CharSequence[] items = new String[recurring.size() + this.extraItems];
    // there is a button...
    items[0] = this.ctx.getString(R.string.recurrence_custom);
    this.mPosition = 0;
    for (int i = this.extraItems; i < (recurring.size() + this.extraItems); i++) {
      items[i] = recurring.get(i - this.extraItems).second;
      if (this.mRecurring.isPresent() && items[i].equals(this.mRecurring.get().getLabel())) {
        this.mPosition = i;
      }
    }
    final View view = inflater.inflate(R.layout.recurrencepicker, null);
    boolean isNotTwoRows = view.getMeasuredWidth() > MIN_SCREEN_WIDTH_FOR_SINGLE_ROW_WEEK;

    this.mWeekGroup = (LinearLayout) view.findViewById(R.id.weekGroup);
    this.mWeekGroup2 = (LinearLayout) view.findViewById(R.id.weekGroup2);

    final String[] dayOfWeekString = new DateFormatSymbols().getShortWeekdays();
    if (isNotTwoRows) {
      this.numOfButtonsInRow1 = 7;
      this.mWeekGroup2.setVisibility(View.GONE);
    } else {
      this.numOfButtonsInRow1 = 4;
      this.mWeekGroup2.setVisibility(View.VISIBLE);
    }
    List<Integer> weekdays = new ArrayList<>();
    this.mWeekGroup.removeAllViews();
    this.mWeekGroup2.removeAllViews();
    if (this.mRecurring.isPresent()) {
      weekdays = this.mRecurring.get().getWeekdays();
    }
    final int startDay = DateTimeHelper.getFirstDayOfWeek();
    for (int i = startDay; i < (startDay + 7); i++) {
      final int day = i % 7;
      // Create Button
      final WeekButton item = new WeekButton(this.ctx);
      item.setTextAppearance(this.ctx, R.style.TextAppearance_Bold);
      item.setTextOff(
          dayOfWeekString[TIME_DAY_TO_CALENDAR_DAY[day]].toUpperCase(Helpers.getLocale(this.ctx)));
      item.setTextOn(
          dayOfWeekString[TIME_DAY_TO_CALENDAR_DAY[day]].toUpperCase(Helpers.getLocale(this.ctx)));
      item.setChecked(weekdays.contains(day + 1));
      item.setTextColor(
          new ColorStateList(
              new int[][] {new int[] {android.R.attr.state_checked}, new int[] {}},
              new int[] {
                ThemeManager.getPrimaryThemeColor(), ThemeManager.getColor(R.attr.colorTextGrey)
              }));
      // Add to view
      final ViewGroup root;
      if ((i - startDay) >= this.numOfButtonsInRow1) {
        root = this.mWeekGroup2;
      } else {
        root = this.mWeekGroup;
      }
      final LinearLayout.LayoutParams lp =
          new LinearLayout.LayoutParams(
              LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
      lp.setMargins(
          ViewHelper.dpToPx(ctx, 5),
          ViewHelper.dpToPx(ctx, 5),
          ViewHelper.dpToPx(ctx, 5),
          ViewHelper.dpToPx(ctx, 5));
      item.setLayoutParams(lp);
      root.addView(item);
      this.mWeekByDayButtons[day] = item;
    }
    this.mUseExact = (CheckBox) view.findViewById(R.id.recurrence_is_exact);
    this.mUseExact.setChecked(this.mInitialExact);
    mUseExact.setVisibility(mForDue ? View.GONE : View.VISIBLE);

    this.mIntervalType = (Spinner) view.findViewById(R.id.interval_type);
    this.mIntervalCount = (EditText) view.findViewById(R.id.interval_count);
    this.mIntervalCount.setText("1");
    this.mIntervalValue = 1;
    updateIntervalType();
    this.mIntervalCount.addTextChangedListener(
        new TextWatcher() {
          @Override
          public void onTextChanged(
              final CharSequence s, final int start, final int before, final int count) {
            int newValue;
            try {
              newValue = Integer.parseInt(s.toString());
            } catch (final NumberFormatException e) {
              Log.wtf(TAG, e);
              newValue = 0;
            }
            if (newValue == 0) {
              mIntervalCount.setText(String.valueOf(mIntervalValue));
            }
            mIntervalValue = newValue;
            updateIntervalType();
          }

          @Override
          public void beforeTextChanged(
              final CharSequence s, final int start, final int count, final int after) {}

          @Override
          public void afterTextChanged(final Editable s) {}
        });
    final int weekdayPosition = this.mForDue ? 0 : 2;
    this.mIntervalType.setOnItemSelectedListener(
        new OnItemSelectedListener() {
          @Override
          public void onItemSelected(
              final AdapterView<?> parent, final View v, final int position, final long id) {
            if (position == weekdayPosition) {
              view.findViewById(R.id.weekGroup).setVisibility(View.VISIBLE);
              view.findViewById(R.id.weekGroup2).setVisibility(View.VISIBLE);
              mIntervalCount.setVisibility(View.GONE);
            } else {
              view.findViewById(R.id.weekGroup).setVisibility(View.GONE);
              view.findViewById(R.id.weekGroup2).setVisibility(View.GONE);
              mIntervalCount.setVisibility(View.VISIBLE);
            }
          }

          @Override
          public void onNothingSelected(final AdapterView<?> parent) {
            // nothing
          }
        });
    this.mRadioGroup = (RadioGroup) view.findViewById(R.id.monthGroup);
    this.mRadioGroup.setVisibility(View.GONE); // Don't support this for
    // now...
    this.mEndButton = (TextView) view.findViewById(R.id.endButton);
    view.findViewById(R.id.endGroup).setOnClickListener(onEndClicked);
    this.mStartButton = (TextView) view.findViewById(R.id.startButton);
    view.findViewById(R.id.startGroup).setOnClickListener(onStartClicked);

    if (!this.mForDue) {
      this.mUseExact.setVisibility(View.GONE);
    }
    if (this.mRecurring.isPresent() && this.mRecurring.get().isTemporary()) {
      final Recurring recurringRaw = mRecurring.get();
      if (!recurringRaw.getWeekdays().isEmpty()) {
        this.mIntervalType.setSelection(this.mForDue ? 0 : 2);
        this.mIntervalCount.setVisibility(View.VISIBLE);
      } else if (recurringRaw.getMinutes() != 0) {
        this.mIntervalType.setSelection(0);
        this.mIntervalCount.setText(String.valueOf(recurringRaw.getMinutes()));
        this.mIntervalValue = recurringRaw.getMinutes();
      } else if (recurringRaw.getHours() != 0) {
        this.mIntervalType.setSelection(1);
        this.mIntervalCount.setText(String.valueOf(recurringRaw.getHours()));
        this.mIntervalValue = recurringRaw.getHours();
      } else if (recurringRaw.getDays() != 0) {
        this.mIntervalType.setSelection(this.mForDue ? 1 : 3);
        this.mIntervalCount.setText(String.valueOf(recurringRaw.getDays()));
        this.mIntervalValue = recurringRaw.getDays();
      } else if (recurringRaw.getMonths() != 0) {
        this.mIntervalType.setSelection(this.mForDue ? 2 : 4);
        this.mIntervalCount.setText(String.valueOf(recurringRaw.getMonths()));
        this.mIntervalValue = recurringRaw.getMonths();
      } else if (recurringRaw.getYears() != 0) {
        this.mIntervalType.setSelection(this.mForDue ? 3 : 5);
        this.mIntervalCount.setText(String.valueOf(recurringRaw.getYears()));
        this.mIntervalValue = recurringRaw.getYears();
      }
      this.mStartDate = recurringRaw.getStartDate();
      this.mEndDate = recurringRaw.getEndDate();
    }
    mStartButton.setText(getDateText(mStartDate.or(new GregorianCalendar())));
    if (this.mEndDate.isPresent()) {
      mEndButton.setText(getDateText(mEndDate.get()));
    } else {
      mEndButton.setText(getDateText());
    }
    return view;
  }
 public static Recurring createTemporayCopy(Recurring r) {
   Recurring newR =
       new Recurring(
           0,
           r.getLabel(),
           r.getMinutes(),
           r.getHours(),
           r.getDays(),
           r.getMonths(),
           r.getYears(),
           r.isForDue(),
           r.getStartDate(),
           r.getEndDate(),
           true,
           r.isExact(),
           r.getWeekdaysRaw(),
           r.getId());
   return newR.create();
 }