Merge "Fix bug #2927288 (CalendarCache table is not setup correctly) - DO NOT MERGE" into froyo
diff --git a/src/com/android/providers/calendar/CalendarProvider2.java b/src/com/android/providers/calendar/CalendarProvider2.java
index a50f903..22b199b 100644
--- a/src/com/android/providers/calendar/CalendarProvider2.java
+++ b/src/com/android/providers/calendar/CalendarProvider2.java
@@ -398,9 +398,7 @@
             // helps to catch missed alarms when the Calendar process is
             // killed (because of low-memory conditions) and then restarted.
             rescheduleMissedAlarms();
-            return;
         }
-        regenerateInstancesTable();
     }
 
     protected void doProcessEventRawTimes(String timezone, String timeZoneDatabaseVersion) {
@@ -415,9 +413,7 @@
         try {
             updateEventsStartEndFromEventRawTimesLocked(timezone);
             updateTimezoneDatabaseVersion(timeZoneDatabaseVersion);
-            cleanInstancesTable();
             regenerateInstancesTable();
-
             mDb.setTransactionSuccessful();
         } finally {
             mDb.endTransaction();
@@ -482,10 +478,6 @@
         }
     }
 
-    private void cleanInstancesTable() {
-        mDb.delete("Instances", null /* where clause */, null /* where args */);
-    }
-
     private void updateTimezoneDatabaseVersion(String timeZoneDatabaseVersion) {
         try {
             mCalendarCache.writeTimezoneDatabaseVersion(timeZoneDatabaseVersion);
@@ -550,7 +542,9 @@
             cursor = handleInstanceQuery(new SQLiteQueryBuilder(),
                     begin, end,
                     new String[] { Instances._ID },
-                    null /* selection */, null /* sort */, false /* searchByDayInsteadOfMillis */);
+                    null /* selection */, null /* sort */,
+                    false /* searchByDayInsteadOfMillis */,
+                    true /* force Instances deletion and expansion */);
         } finally {
             if (cursor != null) {
                 cursor.close();
@@ -658,7 +652,8 @@
                             + uri.getPathSegments().get(3));
                 }
                 return handleInstanceQuery(qb, begin, end, projection,
-                        selection, sortOrder, match == INSTANCES_BY_DAY);
+                        selection, sortOrder, match == INSTANCES_BY_DAY,
+                        false /* do not force Instances deletion and expansion */);
             case EVENT_DAYS:
                 int startDay;
                 int endDay;
@@ -763,11 +758,12 @@
      * @param selection The selection
      * @param sort How to sort
      * @param searchByDay if true, range is in Julian days, if false, range is in ms
+     * @param forceExpansion force the Instance deletion and expansion if set to true
      * @return
      */
     private Cursor handleInstanceQuery(SQLiteQueryBuilder qb, long rangeBegin,
-            long rangeEnd, String[] projection,
-            String selection, String sort, boolean searchByDay) {
+            long rangeEnd, String[] projection, String selection, String sort,
+            boolean searchByDay, boolean forceExpansion) {
 
         qb.setTables("Instances INNER JOIN Events ON (Instances.event_id=Events._id) " +
                 "INNER JOIN Calendars ON (Events.calendar_id = Calendars._id)");
@@ -781,11 +777,15 @@
             // Julian day and we want to include all the events on the last day.
             long endMs = time.setJulianDay((int) rangeEnd + 1);
             // will lock the database.
-            acquireInstanceRange(beginMs, endMs, true /* use minimum expansion window */);
+            acquireInstanceRange(beginMs, endMs,
+                    true /* use minimum expansion window */, forceExpansion
+            );
             qb.appendWhere("startDay<=? AND endDay>=?");
         } else {
             // will lock the database.
-            acquireInstanceRange(rangeBegin, rangeEnd, true /* use minimum expansion window */);
+            acquireInstanceRange(rangeBegin, rangeEnd,
+                    true /* use minimum expansion window */, forceExpansion
+            );
             qb.appendWhere("begin<=? AND end>=?");
         }
         String selectionArgs[] = new String[] {String.valueOf(rangeEnd),
@@ -807,7 +807,7 @@
         // Julian day and we want to include all the events on the last day.
         long endMs = time.setJulianDay(end + 1);
 
-        acquireInstanceRange(beginMs, endMs, true);
+        acquireInstanceRange(beginMs, endMs, true, false /* do not force Instances expansion */);
         qb.appendWhere("startDay<=? AND endDay>=?");
         String selectionArgs[] = new String[] {String.valueOf(end), String.valueOf(begin)};
 
@@ -822,13 +822,13 @@
      * @param begin start of range (ms)
      * @param end end of range (ms)
      * @param useMinimumExpansionWindow expand by at least MINIMUM_EXPANSION_SPAN
+     * @param forceExpansion force the Instance deletion and expansion if set to true
      */
-    private void acquireInstanceRange(final long begin,
-            final long end,
-            final boolean useMinimumExpansionWindow) {
+    private void acquireInstanceRange(final long begin, final long end,
+            final boolean useMinimumExpansionWindow, final boolean forceExpansion) {
         mDb.beginTransaction();
         try {
-            acquireInstanceRangeLocked(begin, end, useMinimumExpansionWindow);
+            acquireInstanceRangeLocked(begin, end, useMinimumExpansionWindow, forceExpansion);
             mDb.setTransactionSuccessful();
         } finally {
             mDb.endTransaction();
@@ -844,7 +844,7 @@
      * @param useMinimumExpansionWindow expand by at least MINIMUM_EXPANSION_SPAN
      */
     private void acquireInstanceRangeLocked(long begin, long end,
-            boolean useMinimumExpansionWindow) {
+            boolean useMinimumExpansionWindow, boolean forceExpansion) {
         long expandBegin = begin;
         long expandEnd = end;
 
@@ -870,7 +870,7 @@
         String localTimezone = TimeZone.getDefault().getID();
         boolean timezoneChanged = (dbTimezone == null) || !dbTimezone.equals(localTimezone);
 
-        if (maxInstance == 0 || timezoneChanged) {
+        if (maxInstance == 0 || timezoneChanged || forceExpansion) {
             // Empty the Instances table and expand from scratch.
             mDb.execSQL("DELETE FROM Instances;");
             if (Config.LOGV) {
@@ -3057,7 +3057,10 @@
         String queryParams[] = new String[] {String.valueOf(start), String.valueOf(nextAlarmTime),
                 String.valueOf(currentMillis)};
 
-        acquireInstanceRangeLocked(start, end, false /* don't use minimum expansion windows */);
+        acquireInstanceRangeLocked(start,
+                end,
+                false /* don't use minimum expansion windows */,
+                false /* do not force Instances deletion and expansion */);
         Cursor cursor = null;
         try {
             cursor = db.rawQuery(query, queryParams);