private static Recurring cursorToRecurring(Cursor c) {
   int i = 0;
   Calendar start;
   try {
     start = DateTimeHelper.parseDateTime(c.getString(8));
   } catch (ParseException e) {
     start = null;
     Log.d(TAG, "cannot parse Date");
   }
   Calendar end;
   try {
     end = DateTimeHelper.parseDateTime(c.getString(9));
   } catch (ParseException e) {
     Log.d(TAG, "cannot parse Date");
     end = null;
   }
   SparseBooleanArray weekdays = new SparseBooleanArray();
   weekdays.put(Calendar.MONDAY, c.getInt(12) == 1);
   weekdays.put(Calendar.TUESDAY, c.getInt(13) == 1);
   weekdays.put(Calendar.WEDNESDAY, c.getInt(14) == 1);
   weekdays.put(Calendar.THURSDAY, c.getInt(15) == 1);
   weekdays.put(Calendar.FRIDAY, c.getInt(16) == 1);
   weekdays.put(Calendar.SATURDAY, c.getInt(17) == 1);
   weekdays.put(Calendar.SUNDAY, c.getInt(18) == 1);
   Integer derivedFrom = c.isNull(19) ? null : c.getInt(19);
   return new Recurring(
       c.getInt(i++),
       c.getString(i++),
       c.getInt(i++),
       c.getInt(i++),
       c.getInt(i++),
       c.getInt(i++),
       c.getInt(i++),
       c.getInt(i++) == 1,
       start,
       end,
       c.getInt(10) == 1,
       c.getInt(11) == 1,
       weekdays,
       derivedFrom);
 }
 public static Recurring get(
     int minutes, int hours, int days, int month, int years, Calendar start, Calendar end) {
   String cal = "";
   if (start != null) {
     cal += " start_date=" + DateTimeHelper.formatDateTime(start);
   }
   if (end != null) {
     cal =
         cal + (cal.equals("") ? "" : " and") + " end_date=" + DateTimeHelper.formatDateTime(end);
   }
   Cursor cursor =
       database.query(
           TABLE,
           allColumns,
           "minutes="
               + minutes
               + " and hours="
               + hours
               + " and days="
               + days
               + " and months="
               + month
               + " and years="
               + years
               + cal,
           null,
           null,
           null,
           null);
   cursor.moveToFirst();
   if (cursor.getCount() != 0) {
     Recurring r = cursorToRecurring(cursor);
     cursor.close();
     return r;
   }
   cursor.close();
   return null;
 }
 private CharSequence getDateText(final @Nullable Calendar c) {
   return DateTimeHelper.formatDate(ctx, c);
 }
  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;
  }