private void InitState() { // title String sSubTitle = utils.GetResStr(R.string.titleDefaultAppointment); // date values dateStart = Calendar.getInstance(); dateEndOn = Calendar.getInstance(); dataRow.SetDuration(prefs.iMinutesDuration); // INSERT MODE if (GetStartMode() == StartMode.NEW) { sSubTitle = utils.GetResStr(R.string.titleNewAppointment); btnDelete.setVisibility(View.INVISIBLE); // initialize data SetStartDateByAgendaView(dateStart); updateStartDateTimeForNewAppointment(dateStart); SetStartTimeForDayAgendaView(dateStart); chkAllDay.setChecked(false); chkAlarm.setChecked(true); // repeat data iRepeatType = 0; // none iRepeatEvery = 1; dateEndOn.setTimeInMillis(0); // no end date } // EDIT MODE if (GetStartMode() == StartMode.EDIT) { sSubTitle = utils.GetResStr(R.string.titleEditAppointment); dateStart.setTimeInMillis(dataRow.GetStartDate().getTimeInMillis()); btnDelete.setVisibility(View.VISIBLE); edSubject.setText(dataRow.GetSubject()); chkAllDay.setChecked(dataRow.GetAllDay()); chkAlarm.setChecked(dataRow.GetAlarm()); // repeat data iRepeatType = dataRow.GetRepeat().GetRepeatTypeAsInt(); iRepeatEvery = dataRow.GetRepeat().GetEvery(); dateEndOn.setTimeInMillis(dataRow.GetRepeat().GetEndOnDate().getTimeInMillis()); } restoreStateFromFreezeIfRequired(); SetActivityTitle(sSubTitle); UpdateStartDateTimeView(); UpdateRepeatInfo(); // set focus to subject edSubject.requestFocus(); if (GetStartMode() == StartMode.EDIT) edSubject.setSelection(edSubject.length()); }
@Override protected void restoreStateFromFreeze() { edSubject.setText(freeze.getString("subject")); chkAlarm.setChecked(freeze.getBoolean("alarm")); dateStart.setTimeInMillis(freeze.getLong("dateStart")); chkAllDay.setChecked(freeze.getBoolean("allday")); iRepeatType = freeze.getInt("repeatType"); iRepeatEvery = freeze.getInt("repeatEvery"); dateEndOn.setTimeInMillis(freeze.getLong("dateEndOn")); }
@Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // save controls state outState.putString("subject", edSubject.getText().toString()); outState.putBoolean("alarm", chkAlarm.isChecked()); outState.putLong("dateStart", dateStart.getTimeInMillis()); outState.putBoolean("allday", chkAllDay.isChecked()); outState.putInt("repeatType", iRepeatType); outState.putInt("repeatEvery", iRepeatEvery); outState.putLong("dateEndOn", dateEndOn.getTimeInMillis()); }
private void validateDate(Date date) { if (date == null) { throw new IllegalArgumentException("Selected date must be non-null."); } if (date.getTime() == 0) { throw new IllegalArgumentException("Selected date must be non-zero. " + date); } if (date.before(minCal.getTime()) || date.after(maxCal.getTime())) { throw new IllegalArgumentException( "selectedDate must be between minDate and maxDate. " + date); } }
/** ----------------------- */ public CalendarPickerView(Context context, AttributeSet attrs) { super(context, attrs); adapter = new MonthAdapter(); setDivider(null); setDividerHeight(100); setVerticalScrollBarEnabled(false); final int bg = getResources().getColor(R.color.calendar_bg); // setBackgroundColor(bg); setCacheColorHint(bg); locale = Locale.getDefault(); today = Calendar.getInstance(locale); minCal = Calendar.getInstance(locale); maxCal = Calendar.getInstance(locale); monthCounter = Calendar.getInstance(locale); monthNameFormat = new SimpleDateFormat(context.getString(R.string.month_name_format), locale); weekdayNameFormat = new SimpleDateFormat(context.getString(R.string.day_name_format), locale); fullDateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); if (isInEditMode()) { Calendar nextYear = Calendar.getInstance(locale); nextYear.add(Calendar.YEAR, 1); init(new Date(), nextYear.getTime()) // .withSelectedDate(new Date()); } }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Bundle extras = CommonActivity.getIntentExtras(data); if (extras != null) { // check for repeat update if (ActivityAppointmentRepeat.GetActivityResult(requestCode, resultCode, extras)) { iRepeatType = ActivityAppointmentRepeat.getExtraRepeatType(extras); iRepeatEvery = ActivityAppointmentRepeat.getExtraRepeatEvery(extras); dateEndOn.setTimeInMillis(ActivityAppointmentRepeat.getExtraRepeatEndOnDate(extras)); UpdateRepeatInfo(); return; } // check for date widget edit request code if (requestCode == DateWidget.SELECT_DATE_REQUEST) { final long lDate = DateWidget.GetSelectedDateOnActivityResult(requestCode, resultCode, extras, dateStart); if (lDate != -1) { UpdateStartDateTimeView(); return; } } // check for time widget edit request code if ((requestCode == TimeWidget.SELECT_TIME_REQUEST) && (resultCode == RESULT_OK)) { final int iHour = TimeWidget.GetSelectedTimeHourOnActivityResult(requestCode, resultCode, extras); final int iMinute = TimeWidget.GetSelectedTimeMinuteOnActivityResult(requestCode, resultCode, extras); dateStart.set(Calendar.HOUR_OF_DAY, iHour); dateStart.set(Calendar.MINUTE, iMinute); chkAllDay.setChecked(false); UpdateStartDateTimeView(); return; } // get KeyboardWidget result if ((requestCode == KeyboardWidget.EDIT_TEXT_REQUEST) && (resultCode == RESULT_OK)) { String sText = KeyboardWidget.GetTextOnActivityResult(requestCode, resultCode, extras); edSubject.setText(sText); return; } } }
List<List<MonthCellDescriptor>> getMonthCells(MonthDescriptor month, Calendar startCal) { Calendar cal = Calendar.getInstance(locale); cal.setTime(startCal.getTime()); List<List<MonthCellDescriptor>> cells = new ArrayList<List<MonthCellDescriptor>>(); cal.set(DAY_OF_MONTH, 1); int firstDayOfWeek = cal.get(DAY_OF_WEEK); int offset = cal.getFirstDayOfWeek() - firstDayOfWeek; if (offset > 0) { offset -= 7; } cal.add(Calendar.DATE, offset); Calendar minSelectedCal = minDate(selectedCals); Calendar maxSelectedCal = maxDate(selectedCals); while ((cal.get(MONTH) < month.getMonth() + 1 || cal.get(YEAR) < month.getYear()) // && cal.get(YEAR) <= month.getYear()) { Logr.d("Building week row starting at %s", cal.getTime()); List<MonthCellDescriptor> weekCells = new ArrayList<MonthCellDescriptor>(); cells.add(weekCells); for (int c = 0; c < 7; c++) { Date date = cal.getTime(); boolean isCurrentMonth = cal.get(MONTH) == month.getMonth(); boolean isSelected = isCurrentMonth && containsDate(selectedCals, cal); boolean isSelectable = isCurrentMonth && betweenDates(cal, minCal, maxCal) && isDateSelectable(date); boolean isToday = sameDate(cal, today); boolean isHighlighted = containsDate(highlightedCals, cal); int value = cal.get(DAY_OF_MONTH); MonthCellDescriptor.RangeState rangeState = MonthCellDescriptor.RangeState.NONE; if (selectedCals.size() > 1) { if (sameDate(minSelectedCal, cal)) { rangeState = MonthCellDescriptor.RangeState.FIRST; } else if (sameDate(maxDate(selectedCals), cal)) { rangeState = MonthCellDescriptor.RangeState.LAST; } else if (betweenDates(cal, minSelectedCal, maxSelectedCal)) { rangeState = MonthCellDescriptor.RangeState.MIDDLE; } } weekCells.add( new MonthCellDescriptor( date, isCurrentMonth, isSelectable, isSelected, isToday, isHighlighted, value, rangeState)); cal.add(DATE, 1); } } return cells; }
protected void onPrepareDialog(int id, Dialog dialog) { super.onPrepareDialog(id, dialog); switch (id) { case SampleDialog: break; case QuestionDialog: Calendar calendar = Calendar.getInstance(); String stime = String.format( "%d시 %d분 %d초", calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND)); dialog.setTitle(stime); break; } }
@Override public boolean DateValuesChanged(Bundle data) { super.DateValuesChanged(data); if (GetStartMode() == StartMode.EDIT) { if (dateStart.getTimeInMillis() != data.getLong("dateStart")) return true; } return false; }
private void SetStartTimeForDayAgendaView(Calendar calDate) { if (getIntent() != null) { Bundle extras = getIntent().getExtras(); if (extras != null) { if (extras.containsKey(CommonActivity.bundleHourOfDay)) { int iView = extras.getInt(CommonActivity.bundleAgendaView); if (iView == 1) // day { int iHourOfDay = extras.getInt(CommonActivity.bundleHourOfDay); int iMinutes = extras.getInt(CommonActivity.bundleMinutes); calDate.set(Calendar.HOUR_OF_DAY, iHourOfDay); calDate.set(Calendar.MINUTE, iMinutes); } } } } }
public void highlightDates(Collection<Date> dates) { for (Date date : dates) { validateDate(date); MonthCellWithMonthIndex monthCellWithMonthIndex = getMonthCellWithIndexByDate(date); if (monthCellWithMonthIndex != null) { Calendar newlyHighlightedCal = Calendar.getInstance(); newlyHighlightedCal.setTime(date); MonthCellDescriptor cell = monthCellWithMonthIndex.cell; highlightedCells.add(cell); highlightedCals.add(newlyHighlightedCal); cell.setHighlighted(true); } } adapter.notifyDataSetChanged(); setAdapter(adapter); }
private void SetStartDateByAgendaView(Calendar calDate) { if (getIntent() != null) { Bundle extras = getIntent().getExtras(); if (extras != null) { if (extras.containsKey(CommonActivity.bundleAgendaView)) { int iView = extras.getInt(CommonActivity.bundleAgendaView); if (iView == 1) // day { long lStartDate = extras.getLong(CommonActivity.bundleAgendaViewStartDate); calDate.setTimeInMillis(lStartDate); } } } } }
public void SaveData() { // check date if no repeat if ((iRepeatType == 0) && (DateBeforeNow(dateStart))) return; dataRow.SetSubject(edSubject.getText().toString()); dataRow.SetStartDate(dateStart); dataRow.SetAllDay(chkAllDay.isChecked()); dataRow.SetAlarm(chkAlarm.isChecked()); // set repeat type RepeatData rd = dataRow.GetRepeat(); rd.SetRepeatTypeAsInt(iRepeatType); rd.SetEvery(iRepeatEvery); rd.SetEndOnDate(dateEndOn.getTimeInMillis()); if (SaveDataToTable(dataTable)) CloseActivity(dataTable); }
private void updateStartDateTimeForNewAppointment(Calendar calDate) { int iHour = calDate.get(Calendar.HOUR_OF_DAY); int iMinute = calDate.get(Calendar.MINUTE); if (iHour < 23) iHour += 1; iMinute = 0; calDate.set(Calendar.HOUR_OF_DAY, iHour); calDate.set(Calendar.MINUTE, iMinute); calDate.set(Calendar.SECOND, 0); calDate.set(Calendar.MILLISECOND, 0); }
private String GetRepeatInfo() { String s = utils.GetResStr(R.string.strRepeatTypeNone); String sUntil = utils.GetResStr(R.string.strRepeatInfoUntil); String sEndDate = (dateEndOn.getTimeInMillis() == 0) ? "" : " " + sUntil + " " + utils.GetLongDate(dateEndOn); // daily if (iRepeatType == 1) s = String.format(utils.GetResStr(R.string.strRepeatInfoDaily), iRepeatEvery, sEndDate); // weekly if (iRepeatType == 2) s = String.format(utils.GetResStr(R.string.strRepeatInfoWeekly), iRepeatEvery, sEndDate); // monthly if (iRepeatType == 3) s = String.format(utils.GetResStr(R.string.strRepeatInfoMonthly), iRepeatEvery, sEndDate); // monthly if (iRepeatType == 4) s = String.format(utils.GetResStr(R.string.strRepeatInfoYearly), sEndDate); return s; }
private void scrollToSelectedDates() { Integer selectedIndex = null; Integer todayIndex = null; Calendar today = Calendar.getInstance(locale); for (int c = 0; c < months.size(); c++) { MonthDescriptor month = months.get(c); if (selectedIndex == null) { for (Calendar selectedCal : selectedCals) { if (sameMonth(selectedCal, month)) { selectedIndex = c; break; } } if (selectedIndex == null && todayIndex == null && sameMonth(today, month)) { todayIndex = c; } } } if (selectedIndex != null) { scrollToSelectedMonth(selectedIndex); } else if (todayIndex != null) { scrollToSelectedMonth(todayIndex); } }
/** Return cell and month-index (for scrolling) for a given Date. */ private MonthCellWithMonthIndex getMonthCellWithIndexByDate(Date date) { int index = 0; Calendar searchCal = Calendar.getInstance(locale); searchCal.setTime(date); Calendar actCal = Calendar.getInstance(locale); for (List<List<MonthCellDescriptor>> monthCells : cells) { for (List<MonthCellDescriptor> weekCells : monthCells) { for (MonthCellDescriptor actCell : weekCells) { actCal.setTime(actCell.getDate()); if (sameDate(actCal, searchCal) && actCell.isSelectable()) { return new MonthCellWithMonthIndex(actCell, index); } } } index++; } return null; }
/** * Both date parameters must be non-null and their {@link Date#getTime()} must not return 0. Time * of day will be ignored. For instance, if you pass in {@code minDate} as 11/16/2012 5:15pm and * {@code maxDate} as 11/16/2013 4:30am, 11/16/2012 will be the first selectable date and * 11/15/2013 will be the last selectable date ({@code maxDate} is exclusive). * * <p>This will implicitly set the {@link SelectionMode} to {@link SelectionMode#SINGLE}. If you * want a different selection mode, use {@link FluentInitializer#inMode(SelectionMode)} on the * {@link FluentInitializer} this method returns. * * <p>The calendar will be constructed using the given locale. This means that all names (months, * days) will be in the language of the locale and the weeks start with the day specified by the * locale. * * @param minDate Earliest selectable date, inclusive. Must be earlier than {@code maxDate}. * @param maxDate Latest selectable date, exclusive. Must be later than {@code minDate}. */ public FluentInitializer init(Date minDate, Date maxDate, Locale locale) { if (minDate == null || maxDate == null) { throw new IllegalArgumentException( "minDate and maxDate must be non-null. " + dbg(minDate, maxDate)); } if (minDate.after(maxDate)) { throw new IllegalArgumentException( "minDate must be before maxDate. " + dbg(minDate, maxDate)); } if (minDate.getTime() == 0 || maxDate.getTime() == 0) { throw new IllegalArgumentException( "minDate and maxDate must be non-zero. " + dbg(minDate, maxDate)); } if (locale == null) { throw new IllegalArgumentException("Locale is null."); } // Make sure that all calendar instances use the same locale. this.locale = locale; today = Calendar.getInstance(locale); minCal = Calendar.getInstance(locale); maxCal = Calendar.getInstance(locale); monthCounter = Calendar.getInstance(locale); monthNameFormat = new SimpleDateFormat(getContext().getString(R.string.month_name_format), locale); for (MonthDescriptor month : months) { month.setLabel(monthNameFormat.format(month.getDate())); } weekdayNameFormat = new SimpleDateFormat(getContext().getString(R.string.day_name_format), locale); fullDateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); this.selectionMode = SelectionMode.SINGLE; // Clear out any previously-selected dates/cells. selectedCals.clear(); selectedCells.clear(); highlightedCells.clear(); // Clear previous state. cells.clear(); months.clear(); minCal.setTime(minDate); maxCal.setTime(maxDate); setMidnight(minCal); setMidnight(maxCal); displayOnly = false; // maxDate is exclusive: bump back to the previous day so if maxDate is // the first of a month, // we don't accidentally include that month in the view. maxCal.add(MINUTE, -1); // Now iterate between minCal and maxCal and build up our list of months // to show. monthCounter.setTime(minCal.getTime()); final int maxMonth = maxCal.get(MONTH); final int maxYear = maxCal.get(YEAR); while ((monthCounter.get(MONTH) <= maxMonth // Up to, including the // month. || monthCounter.get(YEAR) < maxYear) // Up to the year. && monthCounter.get(YEAR) < maxYear + 1) { // But not > next yr. Date date = monthCounter.getTime(); MonthDescriptor month = new MonthDescriptor( monthCounter.get(MONTH), monthCounter.get(YEAR), date, monthNameFormat.format(date)); cells.add(getMonthCells(month, monthCounter)); // Logr.d("Adding month %s", month); months.add(month); monthCounter.add(MONTH, 1); } validateAndUpdate(); return new FluentInitializer(); }
private static boolean sameMonth(Calendar cal, MonthDescriptor month) { return (cal.get(MONTH) == month.getMonth() && cal.get(YEAR) == month.getYear()); }
static boolean betweenDates(Date date, Calendar minCal, Calendar maxCal) { final Date min = minCal.getTime(); return (date.equals(min) || date.after(min)) // >= minCal && date.before(maxCal.getTime()); // && < maxCal }
private static boolean betweenDates(Calendar cal, Calendar minCal, Calendar maxCal) { final Date date = cal.getTime(); return betweenDates(date, minCal, maxCal); }
private static boolean sameDate(Calendar cal, Calendar selectedDate) { return cal.get(MONTH) == selectedDate.get(MONTH) && cal.get(YEAR) == selectedDate.get(YEAR) && cal.get(DAY_OF_MONTH) == selectedDate.get(DAY_OF_MONTH); }
public void OpenRepeatDialog() { ActivityAppointmentRepeat.OpenRepeatForEdit( this, bundleOtherDataStartup, iRepeatType, iRepeatEvery, dateEndOn.getTimeInMillis()); }
private boolean doSelectDate(Date date, MonthCellDescriptor cell) { Calendar newlySelectedCal = Calendar.getInstance(locale); newlySelectedCal.setTime(date); // Sanitize input: clear out the hours/minutes/seconds/millis. setMidnight(newlySelectedCal); // Clear any remaining range state. for (MonthCellDescriptor selectedCell : selectedCells) { selectedCell.setRangeState(RangeState.NONE); } switch (selectionMode) { case RANGE: if (selectedCals.size() > 1) { // We've already got a range selected: clear the old one. clearOldSelections(); } else if (selectedCals.size() == 1 && newlySelectedCal.before(selectedCals.get(0))) { // We're moving the start of the range back in time: clear the // old start date. clearOldSelections(); } break; case MULTIPLE: date = applyMultiSelect(date, newlySelectedCal); break; case SINGLE: clearOldSelections(); break; default: throw new IllegalStateException("Unknown selectionMode " + selectionMode); } if (date != null) { // Select a new cell. if (selectedCells.size() == 0 || !selectedCells.get(0).equals(cell)) { selectedCells.add(cell); cell.setSelected(true); } selectedCals.add(newlySelectedCal); if (selectionMode == SelectionMode.RANGE && selectedCells.size() > 1) { // Select all days in between start and end. Date start = selectedCells.get(0).getDate(); Date end = selectedCells.get(1).getDate(); selectedCells.get(0).setRangeState(MonthCellDescriptor.RangeState.FIRST); selectedCells.get(1).setRangeState(MonthCellDescriptor.RangeState.LAST); for (List<List<MonthCellDescriptor>> month : cells) { for (List<MonthCellDescriptor> week : month) { for (MonthCellDescriptor singleCell : week) { if (singleCell.getDate().after(start) && singleCell.getDate().before(end) && singleCell.isSelectable()) { singleCell.setSelected(true); singleCell.setRangeState(MonthCellDescriptor.RangeState.MIDDLE); selectedCells.add(singleCell); } } } } } } // Update the adapter. validateAndUpdate(); return date != null; }
/** Clears out the hours/minutes/seconds/millis of a Calendar. */ static void setMidnight(Calendar cal) { cal.set(HOUR_OF_DAY, 0); cal.set(MINUTE, 0); cal.set(SECOND, 0); cal.set(MILLISECOND, 0); }