|
| 1 | +package com.rey.material.app; |
| 2 | + |
| 3 | +import java.util.Calendar; |
| 4 | + |
| 5 | +/** |
| 6 | + * Created by Rey on 2/4/2015. |
| 7 | + */ |
| 8 | +public class Recurring { |
| 9 | + |
| 10 | + public static final int REPEAT_NONE = 0; |
| 11 | + public static final int REPEAT_DAILY = 1; |
| 12 | + public static final int REPEAT_WEEKLY = 2; |
| 13 | + public static final int REPEAT_MONTHLY = 3; |
| 14 | + public static final int REPEAT_YEARLY = 4; |
| 15 | + |
| 16 | + public static final int END_FOREVER = 0; |
| 17 | + public static final int END_UNTIL_DATE = 1; |
| 18 | + public static final int END_FOR_EVENT = 2; |
| 19 | + |
| 20 | + public static final int MONTH_SAME_DAY = 0; |
| 21 | + public static final int MONTH_SAME_WEEKDAY = 1; |
| 22 | + |
| 23 | + private static final int[] WEEKDAY_MASK = { |
| 24 | + 0x01,0x02, 0x04, 0x08, 0x10, 0x20, 0x40 |
| 25 | + }; |
| 26 | + |
| 27 | + private static final int DAY_TIME = 86400000; |
| 28 | + |
| 29 | + private int mRepeatMode; |
| 30 | + private int mPeriod = 1; |
| 31 | + private int mRepeatSetting; |
| 32 | + |
| 33 | + private int mEndMode; |
| 34 | + private long mEndSetting; |
| 35 | + |
| 36 | + public Recurring(){} |
| 37 | + |
| 38 | + /** |
| 39 | + * Set repeat mode of this recurring obj. |
| 40 | + */ |
| 41 | + public void setRepeatMode(int mode){ |
| 42 | + mRepeatMode = mode; |
| 43 | + } |
| 44 | + |
| 45 | + public int getRepeatMode(){ |
| 46 | + return mRepeatMode; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Set the period of this recurring obj. Depend on repeat mode, the unit can be days, weeks, months or years. |
| 51 | + */ |
| 52 | + public void setPeriod(int period){ |
| 53 | + mPeriod = period; |
| 54 | + } |
| 55 | + |
| 56 | + public int getPeriod(){ |
| 57 | + return mPeriod; |
| 58 | + } |
| 59 | + |
| 60 | + public void clearWeekdaySetting(){ |
| 61 | + if(mRepeatMode != REPEAT_WEEKLY) |
| 62 | + return; |
| 63 | + |
| 64 | + mRepeatSetting = 0; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Enable repeat on a weekday. Only apply it repeat mode is REPEAT_WEEKLY. |
| 69 | + * @param weekday value of weekday, take from Calendar obj. |
| 70 | + * @param enable Enable this weekday or not. |
| 71 | + */ |
| 72 | + public void setEnabledWeekday(int weekday, boolean enable){ |
| 73 | + if(mRepeatMode != REPEAT_WEEKLY) |
| 74 | + return; |
| 75 | + |
| 76 | + if(enable) |
| 77 | + mRepeatSetting = mRepeatSetting | WEEKDAY_MASK[weekday - 1]; |
| 78 | + else |
| 79 | + mRepeatSetting = mRepeatSetting & (~WEEKDAY_MASK[weekday - 1]); |
| 80 | + } |
| 81 | + |
| 82 | + public boolean isEnabledWeekday(int weekday){ |
| 83 | + if(mRepeatMode != REPEAT_WEEKLY) |
| 84 | + return false; |
| 85 | + |
| 86 | + return (mRepeatSetting & WEEKDAY_MASK[weekday - 1]) != 0; |
| 87 | + } |
| 88 | + |
| 89 | + public void setMonthRepeatType(int type){ |
| 90 | + if(mRepeatMode != REPEAT_MONTHLY) |
| 91 | + return; |
| 92 | + |
| 93 | + mRepeatSetting = type; |
| 94 | + } |
| 95 | + |
| 96 | + public int getMonthRepeatType(){ |
| 97 | + return mRepeatSetting; |
| 98 | + } |
| 99 | + |
| 100 | + public void setEndMode(int mode){ |
| 101 | + mEndMode = mode; |
| 102 | + } |
| 103 | + |
| 104 | + public int getEndMode(){ |
| 105 | + return mEndMode; |
| 106 | + } |
| 107 | + |
| 108 | + public void setEndDate(long date){ |
| 109 | + if(mEndMode != END_UNTIL_DATE) |
| 110 | + return; |
| 111 | + mEndSetting = date; |
| 112 | + } |
| 113 | + |
| 114 | + public long getEndDate(){ |
| 115 | + if(mEndMode != END_UNTIL_DATE) |
| 116 | + return 0; |
| 117 | + return mEndSetting; |
| 118 | + } |
| 119 | + |
| 120 | + public void setEventNumber(int number){ |
| 121 | + if(mEndMode != END_FOR_EVENT) |
| 122 | + return; |
| 123 | + mEndSetting = number; |
| 124 | + } |
| 125 | + |
| 126 | + public int getEventNumber(){ |
| 127 | + if(mEndMode != END_FOR_EVENT) |
| 128 | + return 0; |
| 129 | + return (int)mEndSetting; |
| 130 | + } |
| 131 | + |
| 132 | + public long getNextEventTime(long start){ |
| 133 | + return getNextEventTime(start, System.currentTimeMillis()); |
| 134 | + } |
| 135 | + |
| 136 | + public long getNextEventTime(long start, long now){ |
| 137 | + if(start >= now) |
| 138 | + return start; |
| 139 | + |
| 140 | + Calendar cal = Calendar.getInstance(); |
| 141 | + |
| 142 | + switch (mRepeatMode){ |
| 143 | + case REPEAT_DAILY: |
| 144 | + return getNextDailyEventTime(cal, start, now); |
| 145 | + case REPEAT_WEEKLY: |
| 146 | + return getNextWeeklyEventTime(cal, start, now); |
| 147 | + case REPEAT_MONTHLY: |
| 148 | + return getNextMonthlyEventTime(cal, start, now); |
| 149 | + case REPEAT_YEARLY: |
| 150 | + return getNextYearlyEventTime(cal, start, now); |
| 151 | + default: |
| 152 | + return 0; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private long getNextDailyEventTime(Calendar cal, long start, long now){ |
| 157 | + long period = mPeriod * DAY_TIME; |
| 158 | + long time = start + ((now - start) / period) * period; |
| 159 | + |
| 160 | + do{ |
| 161 | + if(time >= now) |
| 162 | + return time; |
| 163 | + time += period; |
| 164 | + } |
| 165 | + while(true); |
| 166 | + } |
| 167 | + |
| 168 | + private long gotoFirstDayOfWeek(Calendar cal){ |
| 169 | + int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); |
| 170 | + int firstDayOfWeek = cal.getFirstDayOfWeek(); |
| 171 | + int shift = dayOfWeek >= firstDayOfWeek ? (dayOfWeek - firstDayOfWeek) : (dayOfWeek + 7 - firstDayOfWeek); |
| 172 | + |
| 173 | + cal.add(Calendar.DAY_OF_MONTH, -shift); |
| 174 | + return cal.getTimeInMillis(); |
| 175 | + } |
| 176 | + |
| 177 | + private long getNextWeeklyEventTime(Calendar cal, long start, long now){ |
| 178 | + if(mRepeatSetting == 0) |
| 179 | + return 0; |
| 180 | + |
| 181 | + //daily case |
| 182 | + if(mRepeatSetting == 0x7F && mPeriod == 1){ |
| 183 | + long period = DAY_TIME; |
| 184 | + long time = start + ((now - start) / period) * period; |
| 185 | + |
| 186 | + do{ |
| 187 | + if(time >= now) |
| 188 | + return time; |
| 189 | + time += period; |
| 190 | + } |
| 191 | + while(true); |
| 192 | + } |
| 193 | + |
| 194 | + long period = mPeriod * 7 * DAY_TIME; |
| 195 | + |
| 196 | + cal.setTimeInMillis(now); |
| 197 | + long nowFirstDayTime = gotoFirstDayOfWeek(cal); |
| 198 | + |
| 199 | + cal.setTimeInMillis(start); |
| 200 | + long startFirstDayTime = gotoFirstDayOfWeek(cal); |
| 201 | + |
| 202 | + long time = startFirstDayTime + ((nowFirstDayTime - startFirstDayTime) / period) * period; |
| 203 | + do{ |
| 204 | + cal.setTimeInMillis(time); |
| 205 | + int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); |
| 206 | + |
| 207 | + for(int i = 0; i < 7; i++){ |
| 208 | + int nextDayOfWeek = dayOfWeek + i; |
| 209 | + if(nextDayOfWeek > Calendar.SATURDAY) |
| 210 | + nextDayOfWeek = Calendar.SUNDAY; |
| 211 | + |
| 212 | + if(isEnabledWeekday(nextDayOfWeek)){ |
| 213 | + long nextTime = time + i * DAY_TIME; |
| 214 | + if(nextTime >= now) |
| 215 | + return nextTime; |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + time += period; |
| 220 | + } |
| 221 | + while(true); |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * Get the order number of weekday. 0 mean the first, -1 mean the last. |
| 226 | + */ |
| 227 | + private int getWeekDayOrderNum(Calendar cal){ |
| 228 | + return cal.get(Calendar.DAY_OF_MONTH) + 7 > cal.getActualMaximum(Calendar.DAY_OF_MONTH) ? - 1 : (cal.get(Calendar.DAY_OF_MONTH) - 1) / 7; |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Get the day in month of the current month of Calendar. |
| 233 | + * @param cal |
| 234 | + * @param dayOfWeek The day of week. |
| 235 | + * @param orderNum The order number, 0 mean the first, -1 mean the last. |
| 236 | + * @return The day int month |
| 237 | + */ |
| 238 | + private int getDay(Calendar cal, int dayOfWeek, int orderNum){ |
| 239 | + int day = cal.getActualMaximum(Calendar.DAY_OF_MONTH); |
| 240 | + |
| 241 | + cal.set(Calendar.DAY_OF_MONTH, day); |
| 242 | + int lastWeekday = cal.get(Calendar.DAY_OF_WEEK); |
| 243 | + int shift = lastWeekday >= dayOfWeek ? (lastWeekday - dayOfWeek) : (lastWeekday + 7 - dayOfWeek); |
| 244 | + |
| 245 | + //find last dayOfWeek of this month |
| 246 | + day -= shift; |
| 247 | + |
| 248 | + if(orderNum < 0) |
| 249 | + return day; |
| 250 | + |
| 251 | + cal.set(Calendar.DAY_OF_MONTH, day); |
| 252 | + int lastOrderNum = (cal.get(Calendar.DAY_OF_MONTH) - 1) / 7; |
| 253 | + |
| 254 | + if(orderNum >= lastOrderNum) |
| 255 | + return day; |
| 256 | + |
| 257 | + return day - (lastOrderNum - orderNum) * 7; |
| 258 | + } |
| 259 | + |
| 260 | + private long getNextMonthlyEventTime(Calendar cal, long start, long now){ |
| 261 | + if(mRepeatSetting == MONTH_SAME_DAY){ |
| 262 | + cal.setTimeInMillis(now); |
| 263 | + int nowMonthYear = cal.get(Calendar.MONTH) + cal.get(Calendar.YEAR) * 12; |
| 264 | + |
| 265 | + cal.setTimeInMillis(start); |
| 266 | + int startMonthYear = cal.get(Calendar.MONTH) + cal.get(Calendar.YEAR) * 12; |
| 267 | + int startDay = cal.get(Calendar.DAY_OF_MONTH); |
| 268 | + |
| 269 | + int monthYear = startMonthYear + ((nowMonthYear - startMonthYear) / mPeriod) * mPeriod; |
| 270 | + do{ |
| 271 | + cal.set(Calendar.DAY_OF_MONTH, 1); |
| 272 | + cal.set(Calendar.YEAR, monthYear / 12); |
| 273 | + cal.set(Calendar.MONTH, monthYear % 12); |
| 274 | + cal.set(Calendar.DAY_OF_MONTH, Math.min(startDay, cal.getActualMaximum(Calendar.DAY_OF_MONTH))); |
| 275 | + |
| 276 | + if(cal.getTimeInMillis() >= now) |
| 277 | + return cal.getTimeInMillis(); |
| 278 | + |
| 279 | + monthYear += mPeriod; |
| 280 | + } |
| 281 | + while(true); |
| 282 | + } |
| 283 | + else{ |
| 284 | + cal.setTimeInMillis(now); |
| 285 | + int nowMonthYear = cal.get(Calendar.MONTH) + cal.get(Calendar.YEAR) * 12; |
| 286 | + |
| 287 | + cal.setTimeInMillis(start); |
| 288 | + int startMonthYear = cal.get(Calendar.MONTH) + cal.get(Calendar.YEAR) * 12; |
| 289 | + int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); |
| 290 | + int orderNum = getWeekDayOrderNum(cal); |
| 291 | + |
| 292 | + int monthYear = startMonthYear + ((nowMonthYear - startMonthYear) / mPeriod) * mPeriod; |
| 293 | + do{ |
| 294 | + cal.set(Calendar.DAY_OF_MONTH, 1); |
| 295 | + cal.set(Calendar.YEAR, monthYear / 12); |
| 296 | + cal.set(Calendar.MONTH, monthYear % 12); |
| 297 | + cal.set(Calendar.DAY_OF_MONTH, getDay(cal, dayOfWeek, orderNum)); |
| 298 | + |
| 299 | + if(cal.getTimeInMillis() >= now) |
| 300 | + return cal.getTimeInMillis(); |
| 301 | + |
| 302 | + monthYear += mPeriod; |
| 303 | + } |
| 304 | + while(true); |
| 305 | + } |
| 306 | + } |
| 307 | + |
| 308 | + private long getNextYearlyEventTime(Calendar cal, long start, long now){ |
| 309 | + cal.setTimeInMillis(now); |
| 310 | + int nowYear = cal.get(Calendar.YEAR); |
| 311 | + |
| 312 | + cal.setTimeInMillis(start); |
| 313 | + int startYear = cal.get(Calendar.YEAR); |
| 314 | + |
| 315 | + int year = startYear + ((nowYear - startYear) / mPeriod) * mPeriod; |
| 316 | + do{ |
| 317 | + cal.setTimeInMillis(start); |
| 318 | + cal.set(Calendar.YEAR, year); |
| 319 | + if(cal.getTimeInMillis() >= now) |
| 320 | + return cal.getTimeInMillis(); |
| 321 | + |
| 322 | + year += mPeriod; |
| 323 | + } |
| 324 | + while(true); |
| 325 | + } |
| 326 | +} |
0 commit comments