1 /* //device/content/providers/pim/RecurrenceProcessor.java 2 ** 3 ** Copyright 2006, The Android Open Source Project 4 ** 5 ** Licensed under the Apache License, Version 2.0 (the "License"); 6 ** you may not use this file except in compliance with the License. 7 ** You may obtain a copy of the License at 8 ** 9 ** http://www.apache.org/licenses/LICENSE-2.0 10 ** 11 ** Unless required by applicable law or agreed to in writing, software 12 ** distributed under the License is distributed on an "AS IS" BASIS, 13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 ** See the License for the specific language governing permissions and 15 ** limitations under the License. 16 */ 17 18 package com.android.calendarcommon; 19 20 import android.text.format.Time; 21 import android.util.Log; 22 23 import java.util.TreeSet; 24 25 public class RecurrenceProcessor 26 { 27 // these are created once and reused. 28 private Time mIterator = new Time(Time.TIMEZONE_UTC); 29 private Time mUntil = new Time(Time.TIMEZONE_UTC); 30 private StringBuilder mStringBuilder = new StringBuilder(); 31 private Time mGenerated = new Time(Time.TIMEZONE_UTC); 32 private DaySet mDays = new DaySet(false); 33 // Give up after this many loops. This is roughly 1 second of expansion. 34 private static final int MAX_ALLOWED_ITERATIONS = 2000; 35 36 public RecurrenceProcessor() 37 { 38 } 39 40 private static final String TAG = "RecurrenceProcessor"; 41 42 private static final boolean SPEW = false; 43 44 /** 45 * Returns the time (millis since epoch) of the last occurrence, 46 * or -1 if the event repeats forever. If there are no occurrences 47 * (because the exrule or exdates cancel all the occurrences) and the 48 * event does not repeat forever, then 0 is returned. 49 * 50 * This computes a conservative estimate of the last occurrence. That is, 51 * the time of the actual last occurrence might be earlier than the time 52 * returned by this method. 53 * 54 * @param dtstart the time of the first occurrence 55 * @param recur the recurrence 56 * @return an estimate of the time (in UTC milliseconds) of the last 57 * occurrence, which may be greater than the actual last occurrence 58 * @throws DateException 59 */ 60 public long getLastOccurence(Time dtstart, 61 RecurrenceSet recur) throws DateException { 62 return getLastOccurence(dtstart, null /* no limit */, recur); 63 } 64 65 /** 66 * Returns the time (millis since epoch) of the last occurrence, 67 * or -1 if the event repeats forever. If there are no occurrences 68 * (because the exrule or exdates cancel all the occurrences) and the 69 * event does not repeat forever, then 0 is returned. 70 * 71 * This computes a conservative estimate of the last occurrence. That is, 72 * the time of the actual last occurrence might be earlier than the time 73 * returned by this method. 74 * 75 * @param dtstart the time of the first occurrence 76 * @param maxtime the max possible time of the last occurrence. null means no limit 77 * @param recur the recurrence 78 * @return an estimate of the time (in UTC milliseconds) of the last 79 * occurrence, which may be greater than the actual last occurrence 80 * @throws DateException 81 */ 82 public long getLastOccurence(Time dtstart, Time maxtime, 83 RecurrenceSet recur) throws DateException { 84 long lastTime = -1; 85 boolean hasCount = false; 86 87 // first see if there are any "until"s specified. if so, use the latest 88 // until / rdate. 89 if (recur.rrules != null) { 90 for (EventRecurrence rrule : recur.rrules) { 91 if (rrule.count != 0) { 92 hasCount = true; 93 } else if (rrule.until != null) { 94 // according to RFC 2445, until must be in UTC. 95 mIterator.parse(rrule.until); 96 long untilTime = mIterator.toMillis(false /* use isDst */); 97 if (untilTime > lastTime) { 98 lastTime = untilTime; 99 } 100 } 101 } 102 if (lastTime != -1 && recur.rdates != null) { 103 for (long dt : recur.rdates) { 104 if (dt > lastTime) { 105 lastTime = dt; 106 } 107 } 108 } 109 110 // If there were only "until"s and no "count"s, then return the 111 // last "until" date or "rdate". 112 if (lastTime != -1 && !hasCount) { 113 return lastTime; 114 } 115 } else if (recur.rdates != null && 116 recur.exrules == null && recur.exdates == null) { 117 // if there are only rdates, we can just pick the last one. 118 for (long dt : recur.rdates) { 119 if (dt > lastTime) { 120 lastTime = dt; 121 } 122 } 123 return lastTime; 124 } 125 126 // Expand the complete recurrence if there were any counts specified, 127 // or if there were rdates specified. 128 if (hasCount || recur.rdates != null || maxtime != null) { 129 // The expansion might not contain any dates if the exrule or 130 // exdates cancel all the generated dates. 131 long[] dates = expand(dtstart, recur, 132 dtstart.toMillis(false /* use isDst */) /* range start */, 133 (maxtime != null) ? 134 maxtime.toMillis(false /* use isDst */) : -1 /* range end */); 135 136 // The expansion might not contain any dates if exrule or exdates 137 // cancel all the generated dates. 138 if (dates.length == 0) { 139 return 0; 140 } 141 return dates[dates.length - 1]; 142 } 143 return -1; 144 } 145 146 /** 147 * a -- list of values 148 * N -- number of values to use in a 149 * v -- value to check for 150 */ 151 private static boolean listContains(int[] a, int N, int v) 152 { 153 for (int i=0; i<N; i++) { 154 if (a[i] == v) { 155 return true; 156 } 157 } 158 return false; 159 } 160 161 /** 162 * a -- list of values 163 * N -- number of values to use in a 164 * v -- value to check for 165 * max -- if a value in a is negative, add that negative value 166 * to max and compare that instead; this is how we deal with 167 * negative numbers being offsets from the end value 168 */ 169 private static boolean listContains(int[] a, int N, int v, int max) 170 { 171 for (int i=0; i<N; i++) { 172 int w = a[i]; 173 if (w > 0) { 174 if (w == v) { 175 return true; 176 } 177 } else { 178 max += w; // w is negative 179 if (max == v) { 180 return true; 181 } 182 } 183 } 184 return false; 185 } 186 187 /** 188 * Filter out the ones for events whose BYxxx rule is for 189 * a period greater than or equal to the period of the FREQ. 190 * 191 * Returns 0 if the event should not be filtered out 192 * Returns something else (a rule number which is useful for debugging) 193 * if the event should not be returned 194 */ 195 private static int filter(EventRecurrence r, Time iterator) 196 { 197 boolean found; 198 int freq = r.freq; 199 200 if (EventRecurrence.MONTHLY >= freq) { 201 // BYMONTH 202 if (r.bymonthCount > 0) { 203 found = listContains(r.bymonth, r.bymonthCount, 204 iterator.month + 1); 205 if (!found) { 206 return 1; 207 } 208 } 209 } 210 if (EventRecurrence.WEEKLY >= freq) { 211 // BYWEEK -- this is just a guess. I wonder how many events 212 // acutally use BYWEEKNO. 213 if (r.byweeknoCount > 0) { 214 found = listContains(r.byweekno, r.byweeknoCount, 215 iterator.getWeekNumber(), 216 iterator.getActualMaximum(Time.WEEK_NUM)); 217 if (!found) { 218 return 2; 219 } 220 } 221 } 222 if (EventRecurrence.DAILY >= freq) { 223 // BYYEARDAY 224 if (r.byyeardayCount > 0) { 225 found = listContains(r.byyearday, r.byyeardayCount, 226 iterator.yearDay, iterator.getActualMaximum(Time.YEAR_DAY)); 227 if (!found) { 228 return 3; 229 } 230 } 231 // BYMONTHDAY 232 if (r.bymonthdayCount > 0 ) { 233 found = listContains(r.bymonthday, r.bymonthdayCount, 234 iterator.monthDay, 235 iterator.getActualMaximum(Time.MONTH_DAY)); 236 if (!found) { 237 return 4; 238 } 239 } 240 // BYDAY -- when filtering, we ignore the number field, because it 241 // only is meaningful when creating more events. 242 byday: 243 if (r.bydayCount > 0) { 244 int a[] = r.byday; 245 int N = r.bydayCount; 246 int v = EventRecurrence.timeDay2Day(iterator.weekDay); 247 for (int i=0; i<N; i++) { 248 if (a[i] == v) { 249 break byday; 250 } 251 } 252 return 5; 253 } 254 } 255 if (EventRecurrence.HOURLY >= freq) { 256 // BYHOUR 257 found = listContains(r.byhour, r.byhourCount, 258 iterator.hour, 259 iterator.getActualMaximum(Time.HOUR)); 260 if (!found) { 261 return 6; 262 } 263 } 264 if (EventRecurrence.MINUTELY >= freq) { 265 // BYMINUTE 266 found = listContains(r.byminute, r.byminuteCount, 267 iterator.minute, 268 iterator.getActualMaximum(Time.MINUTE)); 269 if (!found) { 270 return 7; 271 } 272 } 273 if (EventRecurrence.SECONDLY >= freq) { 274 // BYSECOND 275 found = listContains(r.bysecond, r.bysecondCount, 276 iterator.second, 277 iterator.getActualMaximum(Time.SECOND)); 278 if (!found) { 279 return 8; 280 } 281 } 282 283 if (r.bysetposCount > 0) { 284 bysetpos: 285 // BYSETPOS - we only handle rules like FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1 286 if (freq == EventRecurrence.MONTHLY && r.bydayCount > 0) { 287 // Check for stuff like BYDAY=1TU 288 for (int i = r.bydayCount - 1; i >= 0; i--) { 289 if (r.bydayNum[i] != 0) { 290 if (Log.isLoggable(TAG, Log.VERBOSE)) { 291 Log.v(TAG, "BYSETPOS not supported with these rules: " + r); 292 } 293 break bysetpos; 294 } 295 } 296 if (!filterMonthlySetPos(r, iterator)) { 297 // Not allowed, filter it out. 298 return 9; 299 } 300 } else { 301 if (Log.isLoggable(TAG, Log.VERBOSE)) { 302 Log.v(TAG, "BYSETPOS not supported with these rules: " + r); 303 } 304 } 305 // BYSETPOS was defined but we don't know how to handle it. Do no filtering based 306 // on it. 307 } 308 309 // if we got to here, we didn't filter it out 310 return 0; 311 } 312 313 /** 314 * Filters out instances that don't match the BYSETPOS clause of a monthly recurrence rule. 315 * This is an awkward and inefficient way to go about it. 316 * 317 * @returns true if this instance should be kept 318 */ 319 private static boolean filterMonthlySetPos(EventRecurrence r, Time instance) { 320 /* 321 * Compute the day of the week for the first day of the month. "instance" has a 322 * day number and a DotW, so we compute the DotW of the 1st from that. Note DotW 323 * is 0-6, where 0=SUNDAY. 324 * 325 * The basic calculation is to take the instance's "day of the week" number, subtract 326 * (day of the month - 1) mod 7, and then make sure it's positive. We can simplify 327 * that with some algebra. 328 */ 329 int dotw = (instance.weekDay - instance.monthDay + 36) % 7; 330 331 /* 332 * The byday[] values are specified as bits, so we can just OR them all 333 * together. 334 */ 335 int bydayMask = 0; 336 for (int i = 0; i < r.bydayCount; i++) { 337 bydayMask |= r.byday[i]; 338 } 339 340 /* 341 * Generate a set according to the BYDAY rules. For each day of the month, determine 342 * if its day of the week is included. If so, append it to the day set. 343 */ 344 int maxDay = instance.getActualMaximum(Time.MONTH_DAY); 345 int daySet[] = new int[maxDay]; 346 int daySetLength = 0; 347 348 for (int md = 1; md <= maxDay; md++) { 349 // For each month day, see if it's part of the set. (This makes some assumptions 350 // about the exact form of the DotW constants.) 351 int dayBit = EventRecurrence.SU << dotw; 352 if ((bydayMask & dayBit) != 0) { 353 daySet[daySetLength++] = md; 354 } 355 356 dotw++; 357 if (dotw == 7) 358 dotw = 0; 359 } 360 361 /* 362 * Now walk through the BYSETPOS list and see if the instance is equal to any of the 363 * specified daySet entries. 364 */ 365 for (int i = r.bysetposCount - 1; i >= 0; i--) { 366 int index = r.bysetpos[i]; 367 if (index > 0) { 368 if (index > daySetLength) { 369 continue; // out of range 370 } 371 if (daySet[index-1] == instance.monthDay) { 372 return true; 373 } 374 } else if (index < 0) { 375 if (daySetLength + index < 0) { 376 continue; // out of range 377 } 378 if (daySet[daySetLength + index] == instance.monthDay) { 379 return true; 380 } 381 } else { 382 // should have been caught by parser 383 throw new RuntimeException("invalid bysetpos value"); 384 } 385 } 386 387 return false; 388 } 389 390 391 private static final int USE_ITERATOR = 0; 392 private static final int USE_BYLIST = 1; 393 394 /** 395 * Return whether we should make this list from the BYxxx list or 396 * from the component of the iterator. 397 */ 398 int generateByList(int count, int freq, int byFreq) 399 { 400 if (byFreq >= freq) { 401 return USE_ITERATOR; 402 } else { 403 if (count == 0) { 404 return USE_ITERATOR; 405 } else { 406 return USE_BYLIST; 407 } 408 } 409 } 410 411 private static boolean useBYX(int freq, int freqConstant, int count) 412 { 413 return freq > freqConstant && count > 0; 414 } 415 416 public static class DaySet 417 { 418 public DaySet(boolean zulu) 419 { 420 mTime = new Time(Time.TIMEZONE_UTC); 421 } 422 423 void setRecurrence(EventRecurrence r) 424 { 425 mYear = 0; 426 mMonth = -1; 427 mR = r; 428 } 429 430 boolean get(Time iterator, int day) 431 { 432 int realYear = iterator.year; 433 int realMonth = iterator.month; 434 435 Time t = null; 436 437 if (SPEW) { 438 Log.i(TAG, "get called with iterator=" + iterator 439 + " " + iterator.month 440 + "/" + iterator.monthDay 441 + "/" + iterator.year + " day=" + day); 442 } 443 if (day < 1 || day > 28) { 444 // if might be past the end of the month, we need to normalize it 445 t = mTime; 446 t.set(day, realMonth, realYear); 447 unsafeNormalize(t); 448 realYear = t.year; 449 realMonth = t.month; 450 day = t.monthDay; 451 if (SPEW) { 452 Log.i(TAG, "normalized t=" + t + " " + t.month 453 + "/" + t.monthDay 454 + "/" + t.year); 455 } 456 } 457 458 /* 459 if (true || SPEW) { 460 Log.i(TAG, "set t=" + t + " " + realMonth + "/" + day + "/" + realYear); 461 } 462 */ 463 if (realYear != mYear || realMonth != mMonth) { 464 if (t == null) { 465 t = mTime; 466 t.set(day, realMonth, realYear); 467 unsafeNormalize(t); 468 if (SPEW) { 469 Log.i(TAG, "set t=" + t + " " + t.month 470 + "/" + t.monthDay 471 + "/" + t.year 472 + " realMonth=" + realMonth + " mMonth=" + mMonth); 473 } 474 } 475 mYear = realYear; 476 mMonth = realMonth; 477 mDays = generateDaysList(t, mR); 478 if (SPEW) { 479 Log.i(TAG, "generated days list"); 480 } 481 } 482 return (mDays & (1<<day)) != 0; 483 } 484 485 /** 486 * Fill in a bit set containing the days of the month on which this 487 * will occur. 488 * 489 * Only call this if the r.freq > DAILY. Otherwise, we should be 490 * processing the BYDAY, BYMONTHDAY, etc. as filters instead. 491 * 492 * monthOffset may be -1, 0 or 1 493 */ 494 private static int generateDaysList(Time generated, EventRecurrence r) 495 { 496 int days = 0; 497 498 int i, count, v; 499 int[] byday, bydayNum, bymonthday; 500 int j, lastDayThisMonth; 501 int first; // Time.SUNDAY, etc 502 int k; 503 504 lastDayThisMonth = generated.getActualMaximum(Time.MONTH_DAY); 505 506 // BYDAY 507 count = r.bydayCount; 508 if (count > 0) { 509 // calculate the day of week for the first of this month (first) 510 j = generated.monthDay; 511 while (j >= 8) { 512 j -= 7; 513 } 514 first = generated.weekDay; 515 if (first >= j) { 516 first = first - j + 1; 517 } else { 518 first = first - j + 8; 519 } 520 521 // What to do if the event is weekly: 522 // This isn't ideal, but we'll generate a month's worth of events 523 // and the code that calls this will only use the ones that matter 524 // for the current week. 525 byday = r.byday; 526 bydayNum = r.bydayNum; 527 for (i=0; i<count; i++) { 528 v = bydayNum[i]; 529 j = EventRecurrence.day2TimeDay(byday[i]) - first + 1; 530 if (j <= 0) { 531 j += 7; 532 } 533 if (v == 0) { 534 // v is 0, each day in the month/week 535 for (; j<=lastDayThisMonth; j+=7) { 536 if (SPEW) Log.i(TAG, "setting " + j + " for rule " 537 + v + "/" + EventRecurrence.day2TimeDay(byday[i])); 538 days |= 1 << j; 539 } 540 } 541 else if (v > 0) { 542 // v is positive, count from the beginning of the month 543 // -1 b/c the first one should add 0 544 j += 7*(v-1); 545 if (j <= lastDayThisMonth) { 546 if (SPEW) Log.i(TAG, "setting " + j + " for rule " 547 + v + "/" + EventRecurrence.day2TimeDay(byday[i])); 548 // if it's impossible, we drop it 549 days |= 1 << j; 550 } 551 } 552 else { 553 // v is negative, count from the end of the month 554 // find the last one 555 for (; j<=lastDayThisMonth; j+=7) { 556 } 557 // v is negative 558 // should do +1 b/c the last one should add 0, but we also 559 // skipped the j -= 7 b/c the loop to find the last one 560 // overshot by one week 561 j += 7*v; 562 if (j >= 1) { 563 if (SPEW) Log.i(TAG, "setting " + j + " for rule " 564 + v + "/" + EventRecurrence.day2TimeDay(byday[i])); 565 days |= 1 << j; 566 } 567 } 568 } 569 } 570 571 // BYMONTHDAY 572 // Q: What happens if we have BYMONTHDAY and BYDAY? 573 // A: I didn't see it in the spec, so in lieu of that, we'll 574 // intersect the two. That seems reasonable to me. 575 if (r.freq > EventRecurrence.WEEKLY) { 576 count = r.bymonthdayCount; 577 if (count != 0) { 578 bymonthday = r.bymonthday; 579 if (r.bydayCount == 0) { 580 for (i=0; i<count; i++) { 581 v = bymonthday[i]; 582 if (v >= 0) { 583 days |= 1 << v; 584 } else { 585 j = lastDayThisMonth + v + 1; // v is negative 586 if (j >= 1 && j <= lastDayThisMonth) { 587 days |= 1 << j; 588 } 589 } 590 } 591 } else { 592 // This is O(lastDayThisMonth*count), which is really 593 // O(count) with a decent sized constant. 594 for (j=1; j<=lastDayThisMonth; j++) { 595 next_day : { 596 if ((days&(1<<j)) != 0) { 597 for (i=0; i<count; i++) { 598 if (bymonthday[i] == j) { 599 break next_day; 600 } 601 } 602 days &= ~(1<<j); 603 } 604 } 605 } 606 } 607 } 608 } 609 return days; 610 } 611 612 private EventRecurrence mR; 613 private int mDays; 614 private Time mTime; 615 private int mYear; 616 private int mMonth; 617 } 618 619 /** 620 * Expands the recurrence within the given range using the given dtstart 621 * value. Returns an array of longs where each element is a date in UTC 622 * milliseconds. The return value is never null. If there are no dates 623 * then an array of length zero is returned. 624 * 625 * @param dtstart a Time object representing the first occurrence 626 * @param recur the recurrence rules, including RRULE, RDATES, EXRULE, and 627 * EXDATES 628 * @param rangeStartMillis the beginning of the range to expand, in UTC 629 * milliseconds 630 * @param rangeEndMillis the non-inclusive end of the range to expand, in 631 * UTC milliseconds; use -1 for the entire range. 632 * @return an array of dates, each date is in UTC milliseconds 633 * @throws DateException 634 * @throws android.util.TimeFormatException if recur cannot be parsed 635 */ 636 public long[] expand(Time dtstart, 637 RecurrenceSet recur, 638 long rangeStartMillis, 639 long rangeEndMillis) throws DateException { 640 String timezone = dtstart.timezone; 641 mIterator.clear(timezone); 642 mGenerated.clear(timezone); 643 644 // We don't need to clear the mUntil (and it wouldn't do any good to 645 // do so) because the "until" date string is specified in UTC and that 646 // sets the timezone in the mUntil Time object. 647 648 mIterator.set(rangeStartMillis); 649 long rangeStartDateValue = normDateTimeComparisonValue(mIterator); 650 651 long rangeEndDateValue; 652 if (rangeEndMillis != -1) { 653 mIterator.set(rangeEndMillis); 654 rangeEndDateValue = normDateTimeComparisonValue(mIterator); 655 } else { 656 rangeEndDateValue = Long.MAX_VALUE; 657 } 658 659 TreeSet<Long> dtSet = new TreeSet<Long>(); 660 661 if (recur.rrules != null) { 662 for (EventRecurrence rrule : recur.rrules) { 663 expand(dtstart, rrule, rangeStartDateValue, 664 rangeEndDateValue, true /* add */, dtSet); 665 } 666 } 667 if (recur.rdates != null) { 668 for (long dt : recur.rdates) { 669 // The dates are stored as milliseconds. We need to convert 670 // them to year/month/day values in the local timezone. 671 mIterator.set(dt); 672 long dtvalue = normDateTimeComparisonValue(mIterator); 673 dtSet.add(dtvalue); 674 } 675 } 676 if (recur.exrules != null) { 677 for (EventRecurrence exrule : recur.exrules) { 678 expand(dtstart, exrule, rangeStartDateValue, 679 rangeEndDateValue, false /* remove */, dtSet); 680 } 681 } 682 if (recur.exdates != null) { 683 for (long dt : recur.exdates) { 684 // The dates are stored as milliseconds. We need to convert 685 // them to year/month/day values in the local timezone. 686 mIterator.set(dt); 687 long dtvalue = normDateTimeComparisonValue(mIterator); 688 dtSet.remove(dtvalue); 689 } 690 } 691 if (dtSet.isEmpty()) { 692 // this can happen if the recurrence does not occur within the 693 // expansion window. 694 return new long[0]; 695 } 696 697 // The values in dtSet are represented in a special form that is useful 698 // for fast comparisons and that is easy to generate from year/month/day 699 // values. We need to convert these to UTC milliseconds and also to 700 // ensure that the dates are valid. 701 int len = dtSet.size(); 702 long[] dates = new long[len]; 703 int i = 0; 704 for (Long val: dtSet) { 705 setTimeFromLongValue(mIterator, val); 706 dates[i++] = mIterator.toMillis(true /* ignore isDst */); 707 } 708 return dates; 709 } 710 711 /** 712 * Run the recurrence algorithm. Processes events defined in the local 713 * timezone of the event. Return a list of iCalendar DATETIME 714 * strings containing the start date/times of the occurrences; the output 715 * times are defined in the local timezone of the event. 716 * 717 * If you want all of the events, pass Long.MAX_VALUE for rangeEndDateValue. If you pass 718 * Long.MAX_VALUE for rangeEnd, and the event doesn't have a COUNT or UNTIL field, 719 * you'll get a DateException. 720 * 721 * @param dtstart the dtstart date as defined in RFC2445. This 722 * {@link Time} should be in the timezone of the event. 723 * @param r the parsed recurrence, as defiend in RFC2445 724 * @param rangeStartDateValue the first date-time you care about, inclusive 725 * @param rangeEndDateValue the last date-time you care about, not inclusive (so 726 * if you care about everything up through and including 727 * Dec 22 1995, set last to Dec 23, 1995 00:00:00 728 * @param add Whether or not we should add to out, or remove from out. 729 * @param out the TreeSet you'd like to fill with the events 730 * @throws DateException 731 * @throws android.util.TimeFormatException if r cannot be parsed. 732 */ 733 public void expand(Time dtstart, 734 EventRecurrence r, 735 long rangeStartDateValue, 736 long rangeEndDateValue, 737 boolean add, 738 TreeSet<Long> out) throws DateException { 739 unsafeNormalize(dtstart); 740 long dtstartDateValue = normDateTimeComparisonValue(dtstart); 741 int count = 0; 742 743 // add the dtstart instance to the recurrence, if within range. 744 // For example, if dtstart is Mar 1, 2010 and the range is Jan 1 - Apr 1, 745 // then add it here and increment count. If the range is earlier or later, 746 // then don't add it here. In that case, count will be incremented later 747 // inside the loop. It is important that count gets incremented exactly 748 // once here or in the loop for dtstart. 749 // 750 // NOTE: if DTSTART is not synchronized with the recurrence rule, the first instance 751 // we return will not fit the RRULE pattern. 752 if (add && dtstartDateValue >= rangeStartDateValue 753 && dtstartDateValue < rangeEndDateValue) { 754 out.add(dtstartDateValue); 755 ++count; 756 } 757 758 Time iterator = mIterator; 759 Time until = mUntil; 760 StringBuilder sb = mStringBuilder; 761 Time generated = mGenerated; 762 DaySet days = mDays; 763 764 try { 765 766 days.setRecurrence(r); 767 if (rangeEndDateValue == Long.MAX_VALUE && r.until == null && r.count == 0) { 768 throw new DateException( 769 "No range end provided for a recurrence that has no UNTIL or COUNT."); 770 } 771 772 // the top-level frequency 773 int freqField; 774 int freqAmount = r.interval; 775 int freq = r.freq; 776 switch (freq) 777 { 778 case EventRecurrence.SECONDLY: 779 freqField = Time.SECOND; 780 break; 781 case EventRecurrence.MINUTELY: 782 freqField = Time.MINUTE; 783 break; 784 case EventRecurrence.HOURLY: 785 freqField = Time.HOUR; 786 break; 787 case EventRecurrence.DAILY: 788 freqField = Time.MONTH_DAY; 789 break; 790 case EventRecurrence.WEEKLY: 791 freqField = Time.MONTH_DAY; 792 freqAmount = 7 * r.interval; 793 if (freqAmount <= 0) { 794 freqAmount = 7; 795 } 796 break; 797 case EventRecurrence.MONTHLY: 798 freqField = Time.MONTH; 799 break; 800 case EventRecurrence.YEARLY: 801 freqField = Time.YEAR; 802 break; 803 default: 804 throw new DateException("bad freq=" + freq); 805 } 806 if (freqAmount <= 0) { 807 freqAmount = 1; 808 } 809 810 int bymonthCount = r.bymonthCount; 811 boolean usebymonth = useBYX(freq, EventRecurrence.MONTHLY, bymonthCount); 812 boolean useDays = freq >= EventRecurrence.WEEKLY && 813 (r.bydayCount > 0 || r.bymonthdayCount > 0); 814 int byhourCount = r.byhourCount; 815 boolean usebyhour = useBYX(freq, EventRecurrence.HOURLY, byhourCount); 816 int byminuteCount = r.byminuteCount; 817 boolean usebyminute = useBYX(freq, EventRecurrence.MINUTELY, byminuteCount); 818 int bysecondCount = r.bysecondCount; 819 boolean usebysecond = useBYX(freq, EventRecurrence.SECONDLY, bysecondCount); 820 821 // initialize the iterator 822 iterator.set(dtstart); 823 if (freqField == Time.MONTH) { 824 if (useDays) { 825 // if it's monthly, and we're going to be generating 826 // days, set the iterator day field to 1 because sometimes 827 // we'll skip months if it's greater than 28. 828 // XXX Do we generate days for MONTHLY w/ BYHOUR? If so, 829 // we need to do this then too. 830 iterator.monthDay = 1; 831 } 832 } 833 834 long untilDateValue; 835 if (r.until != null) { 836 // Ensure that the "until" date string is specified in UTC. 837 String untilStr = r.until; 838 // 15 is length of date-time without trailing Z e.g. "20090204T075959" 839 // A string such as 20090204 is a valid UNTIL (see RFC 2445) and the 840 // Z should not be added. 841 if (untilStr.length() == 15) { 842 untilStr = untilStr + 'Z'; 843 } 844 // The parse() method will set the timezone to UTC 845 until.parse(untilStr); 846 847 // We need the "until" year/month/day values to be in the same 848 // timezone as all the generated dates so that we can compare them 849 // using the values returned by normDateTimeComparisonValue(). 850 until.switchTimezone(dtstart.timezone); 851 untilDateValue = normDateTimeComparisonValue(until); 852 } else { 853 untilDateValue = Long.MAX_VALUE; 854 } 855 856 sb.ensureCapacity(15); 857 sb.setLength(15); // TODO: pay attention to whether or not the event 858 // is an all-day one. 859 860 if (SPEW) { 861 Log.i(TAG, "expand called w/ rangeStart=" + rangeStartDateValue 862 + " rangeEnd=" + rangeEndDateValue); 863 } 864 865 // go until the end of the range or we're done with this event 866 boolean eventEnded = false; 867 int failsafe = 0; // Avoid infinite loops 868 events: { 869 while (true) { 870 int monthIndex = 0; 871 if (failsafe++ > MAX_ALLOWED_ITERATIONS) { // Give up after about 1 second of processing 872 throw new DateException("Recurrence processing stuck: " + r.toString()); 873 } 874 875 unsafeNormalize(iterator); 876 877 int iteratorYear = iterator.year; 878 int iteratorMonth = iterator.month + 1; 879 int iteratorDay = iterator.monthDay; 880 int iteratorHour = iterator.hour; 881 int iteratorMinute = iterator.minute; 882 int iteratorSecond = iterator.second; 883 884 // year is never expanded -- there is no BYYEAR 885 generated.set(iterator); 886 887 if (SPEW) Log.i(TAG, "year=" + generated.year); 888 889 do { // month 890 int month = usebymonth 891 ? r.bymonth[monthIndex] 892 : iteratorMonth; 893 month--; 894 if (SPEW) Log.i(TAG, " month=" + month); 895 896 int dayIndex = 1; 897 int lastDayToExamine = 0; 898 899 // Use this to handle weeks that overlap the end of the month. 900 // Keep the year and month that days is for, and generate it 901 // when needed in the loop 902 if (useDays) { 903 // Determine where to start and end, don't worry if this happens 904 // to be before dtstart or after the end, because that will be 905 // filtered in the inner loop 906 if (freq == EventRecurrence.WEEKLY) { 907 int dow = iterator.weekDay; 908 dayIndex = iterator.monthDay - dow; 909 lastDayToExamine = dayIndex + 6; 910 } else { 911 lastDayToExamine = generated 912 .getActualMaximum(Time.MONTH_DAY); 913 } 914 if (SPEW) Log.i(TAG, "dayIndex=" + dayIndex 915 + " lastDayToExamine=" + lastDayToExamine 916 + " days=" + days); 917 } 918 919 do { // day 920 int day; 921 if (useDays) { 922 if (!days.get(iterator, dayIndex)) { 923 dayIndex++; 924 continue; 925 } else { 926 day = dayIndex; 927 } 928 } else { 929 day = iteratorDay; 930 } 931 if (SPEW) Log.i(TAG, " day=" + day); 932 933 // hour 934 int hourIndex = 0; 935 do { 936 int hour = usebyhour 937 ? r.byhour[hourIndex] 938 : iteratorHour; 939 if (SPEW) Log.i(TAG, " hour=" + hour + " usebyhour=" + usebyhour); 940 941 // minute 942 int minuteIndex = 0; 943 do { 944 int minute = usebyminute 945 ? r.byminute[minuteIndex] 946 : iteratorMinute; 947 if (SPEW) Log.i(TAG, " minute=" + minute); 948 949 // second 950 int secondIndex = 0; 951 do { 952 int second = usebysecond 953 ? r.bysecond[secondIndex] 954 : iteratorSecond; 955 if (SPEW) Log.i(TAG, " second=" + second); 956 957 // we do this here each time, because if we distribute it, we find the 958 // month advancing extra times, as we set the month to the 32nd, 33rd, etc. 959 // days. 960 generated.set(second, minute, hour, day, month, iteratorYear); 961 unsafeNormalize(generated); 962 963 long genDateValue = normDateTimeComparisonValue(generated); 964 // sometimes events get generated (BYDAY, BYHOUR, etc.) that 965 // are before dtstart. Filter these. I believe this is correct, 966 // but Google Calendar doesn't seem to always do this. 967 if (genDateValue >= dtstartDateValue) { 968 // filter and then add 969 // TODO: we don't check for stop conditions (like 970 // passing the "end" date) unless the filter 971 // allows the event. Could stop sooner. 972 int filtered = filter(r, generated); 973 if (0 == filtered) { 974 975 // increase the count as long 976 // as this isn't the same 977 // as the first instance 978 // specified by the DTSTART 979 // (for RRULEs -- additive). 980 // This condition must be the complement of the 981 // condition for incrementing count at the 982 // beginning of the method, so if we don't 983 // increment count there, we increment it here. 984 // For example, if add is set and dtstartDateValue 985 // is inside the start/end range, then it was added 986 // and count was incremented at the beginning. 987 // If dtstartDateValue is outside the range or add 988 // is not set, then we must increment count here. 989 if (!(dtstartDateValue == genDateValue 990 && add 991 && dtstartDateValue >= rangeStartDateValue 992 && dtstartDateValue < rangeEndDateValue)) { 993 ++count; 994 } 995 // one reason we can stop is that 996 // we're past the until date 997 if (genDateValue > untilDateValue) { 998 if (SPEW) { 999 Log.i(TAG, "stopping b/c until=" 1000 + untilDateValue 1001 + " generated=" 1002 + genDateValue); 1003 } 1004 break events; 1005 } 1006 // or we're past rangeEnd 1007 if (genDateValue >= rangeEndDateValue) { 1008 if (SPEW) { 1009 Log.i(TAG, "stopping b/c rangeEnd=" 1010 + rangeEndDateValue 1011 + " generated=" + generated); 1012 } 1013 break events; 1014 } 1015 1016 if (genDateValue >= rangeStartDateValue) { 1017 if (SPEW) { 1018 Log.i(TAG, "adding date=" + generated + " filtered=" + filtered); 1019 } 1020 if (add) { 1021 out.add(genDateValue); 1022 } else { 1023 out.remove(genDateValue); 1024 } 1025 } 1026 // another is that count is high enough 1027 if (r.count > 0 && r.count == count) { 1028 //Log.i(TAG, "stopping b/c count=" + count); 1029 break events; 1030 } 1031 } 1032 } 1033 secondIndex++; 1034 } while (usebysecond && secondIndex < bysecondCount); 1035 minuteIndex++; 1036 } while (usebyminute && minuteIndex < byminuteCount); 1037 hourIndex++; 1038 } while (usebyhour && hourIndex < byhourCount); 1039 dayIndex++; 1040 } while (useDays && dayIndex <= lastDayToExamine); 1041 monthIndex++; 1042 } while (usebymonth && monthIndex < bymonthCount); 1043 1044 // Add freqAmount to freqField until we get another date that we want. 1045 // We don't want to "generate" dates with the iterator. 1046 // XXX: We do this for days, because there is a varying number of days 1047 // per month 1048 int oldDay = iterator.monthDay; 1049 generated.set(iterator); // just using generated as a temporary. 1050 int n = 1; 1051 while (true) { 1052 int value = freqAmount * n; 1053 switch (freqField) { 1054 case Time.SECOND: 1055 iterator.second += value; 1056 break; 1057 case Time.MINUTE: 1058 iterator.minute += value; 1059 break; 1060 case Time.HOUR: 1061 iterator.hour += value; 1062 break; 1063 case Time.MONTH_DAY: 1064 iterator.monthDay += value; 1065 break; 1066 case Time.MONTH: 1067 iterator.month += value; 1068 break; 1069 case Time.YEAR: 1070 iterator.year += value; 1071 break; 1072 case Time.WEEK_DAY: 1073 iterator.monthDay += value; 1074 break; 1075 case Time.YEAR_DAY: 1076 iterator.monthDay += value; 1077 break; 1078 default: 1079 throw new RuntimeException("bad field=" + freqField); 1080 } 1081 1082 unsafeNormalize(iterator); 1083 if (freqField != Time.YEAR && freqField != Time.MONTH) { 1084 break; 1085 } 1086 if (iterator.monthDay == oldDay) { 1087 break; 1088 } 1089 n++; 1090 iterator.set(generated); 1091 } 1092 } 1093 } 1094 } 1095 catch (DateException e) { 1096 Log.w(TAG, "DateException with r=" + r + " rangeStart=" + rangeStartDateValue 1097 + " rangeEnd=" + rangeEndDateValue); 1098 throw e; 1099 } 1100 catch (RuntimeException t) { 1101 Log.w(TAG, "RuntimeException with r=" + r + " rangeStart=" + rangeStartDateValue 1102 + " rangeEnd=" + rangeEndDateValue); 1103 throw t; 1104 } 1105 } 1106 1107 /** 1108 * Normalizes the date fields to give a valid date, but if the time falls 1109 * in the invalid window during a transition out of Daylight Saving Time 1110 * when time jumps forward an hour, then the "normalized" value will be 1111 * invalid. 1112 * <p> 1113 * This method also computes the weekDay and yearDay fields. 1114 * 1115 * <p> 1116 * This method does not modify the fields isDst, or gmtOff. 1117 */ 1118 static void unsafeNormalize(Time date) { 1119 int second = date.second; 1120 int minute = date.minute; 1121 int hour = date.hour; 1122 int monthDay = date.monthDay; 1123 int month = date.month; 1124 int year = date.year; 1125 1126 int addMinutes = ((second < 0) ? (second - 59) : second) / 60; 1127 second -= addMinutes * 60; 1128 minute += addMinutes; 1129 int addHours = ((minute < 0) ? (minute - 59) : minute) / 60; 1130 minute -= addHours * 60; 1131 hour += addHours; 1132 int addDays = ((hour < 0) ? (hour - 23) : hour) / 24; 1133 hour -= addDays * 24; 1134 monthDay += addDays; 1135 1136 // We want to make "monthDay" positive. We do this by subtracting one 1137 // from the year and adding a year's worth of days to "monthDay" in 1138 // the following loop while "monthDay" <= 0. 1139 while (monthDay <= 0) { 1140 // If month is after Feb, then add this year's length so that we 1141 // include this year's leap day, if any. 1142 // Otherwise (the month is Feb or earlier), add last year's length. 1143 // Subtract one from the year in either case. This gives the same 1144 // effective date but makes monthDay (the day of the month) much 1145 // larger. Eventually (usually in one iteration) monthDay will 1146 // be positive. 1147 int days = month > 1 ? yearLength(year) : yearLength(year - 1); 1148 monthDay += days; 1149 year -= 1; 1150 } 1151 // At this point, monthDay >= 1. Normalize the month to the range [0,11]. 1152 if (month < 0) { 1153 int years = (month + 1) / 12 - 1; 1154 year += years; 1155 month -= 12 * years; 1156 } else if (month >= 12) { 1157 int years = month / 12; 1158 year += years; 1159 month -= 12 * years; 1160 } 1161 // At this point, month is in the range [0,11] and monthDay >= 1. 1162 // Now loop until the monthDay is in the correct range for the month. 1163 while (true) { 1164 // On January, check if we can jump forward a whole year. 1165 if (month == 0) { 1166 int yearLength = yearLength(year); 1167 if (monthDay > yearLength) { 1168 year++; 1169 monthDay -= yearLength; 1170 } 1171 } 1172 int monthLength = monthLength(year, month); 1173 if (monthDay > monthLength) { 1174 monthDay -= monthLength; 1175 month++; 1176 if (month >= 12) { 1177 month -= 12; 1178 year++; 1179 } 1180 } else break; 1181 } 1182 // At this point, monthDay <= the length of the current month and is 1183 // in the range [1,31]. 1184 1185 date.second = second; 1186 date.minute = minute; 1187 date.hour = hour; 1188 date.monthDay = monthDay; 1189 date.month = month; 1190 date.year = year; 1191 date.weekDay = weekDay(year, month, monthDay); 1192 date.yearDay = yearDay(year, month, monthDay); 1193 } 1194 1195 /** 1196 * Returns true if the given year is a leap year. 1197 * 1198 * @param year the given year to test 1199 * @return true if the given year is a leap year. 1200 */ 1201 static boolean isLeapYear(int year) { 1202 return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)); 1203 } 1204 1205 /** 1206 * Returns the number of days in the given year. 1207 * 1208 * @param year the given year 1209 * @return the number of days in the given year. 1210 */ 1211 static int yearLength(int year) { 1212 return isLeapYear(year) ? 366 : 365; 1213 } 1214 1215 private static final int[] DAYS_PER_MONTH = { 31, 28, 31, 30, 31, 30, 31, 1216 31, 30, 31, 30, 31 }; 1217 private static final int[] DAYS_IN_YEAR_PRECEDING_MONTH = { 0, 31, 59, 90, 1218 120, 151, 180, 212, 243, 273, 304, 334 }; 1219 1220 /** 1221 * Returns the number of days in the given month of the given year. 1222 * 1223 * @param year the given year. 1224 * @param month the given month in the range [0,11] 1225 * @return the number of days in the given month of the given year. 1226 */ 1227 static int monthLength(int year, int month) { 1228 int n = DAYS_PER_MONTH[month]; 1229 if (n != 28) { 1230 return n; 1231 } 1232 return isLeapYear(year) ? 29 : 28; 1233 } 1234 1235 /** 1236 * Computes the weekday, a number in the range [0,6] where Sunday=0, from 1237 * the given year, month, and day. 1238 * 1239 * @param year the year 1240 * @param month the 0-based month in the range [0,11] 1241 * @param day the 1-based day of the month in the range [1,31] 1242 * @return the weekday, a number in the range [0,6] where Sunday=0 1243 */ 1244 static int weekDay(int year, int month, int day) { 1245 if (month <= 1) { 1246 month += 12; 1247 year -= 1; 1248 } 1249 return (day + (13 * month - 14) / 5 + year + year/4 - year/100 + year/400) % 7; 1250 } 1251 1252 /** 1253 * Computes the 0-based "year day", given the year, month, and day. 1254 * 1255 * @param year the year 1256 * @param month the 0-based month in the range [0,11] 1257 * @param day the 1-based day in the range [1,31] 1258 * @return the 0-based "year day", the number of days into the year 1259 */ 1260 static int yearDay(int year, int month, int day) { 1261 int yearDay = DAYS_IN_YEAR_PRECEDING_MONTH[month] + day - 1; 1262 if (month >= 2 && isLeapYear(year)) { 1263 yearDay += 1; 1264 } 1265 return yearDay; 1266 } 1267 1268 /** 1269 * Converts a normalized Time value to a 64-bit long. The mapping of Time 1270 * values to longs provides a total ordering on the Time values so that 1271 * two Time values can be compared efficiently by comparing their 64-bit 1272 * long values. This is faster than converting the Time values to UTC 1273 * millliseconds. 1274 * 1275 * @param normalized a Time object whose date and time fields have been 1276 * normalized 1277 * @return a 64-bit long value that can be used for comparing and ordering 1278 * dates and times represented by Time objects 1279 */ 1280 private static final long normDateTimeComparisonValue(Time normalized) { 1281 // 37 bits for the year, 4 bits for the month, 5 bits for the monthDay, 1282 // 5 bits for the hour, 6 bits for the minute, 6 bits for the second. 1283 return ((long)normalized.year << 26) + (normalized.month << 22) 1284 + (normalized.monthDay << 17) + (normalized.hour << 12) 1285 + (normalized.minute << 6) + normalized.second; 1286 } 1287 1288 private static final void setTimeFromLongValue(Time date, long val) { 1289 date.year = (int) (val >> 26); 1290 date.month = (int) (val >> 22) & 0xf; 1291 date.monthDay = (int) (val >> 17) & 0x1f; 1292 date.hour = (int) (val >> 12) & 0x1f; 1293 date.minute = (int) (val >> 6) & 0x3f; 1294 date.second = (int) (val & 0x3f); 1295 } 1296 } 1297