Add some columns

isPrimary to Calendars
isOrganizer to Events

Bug: 6888477

Change-Id: I6ea7d69eec4a7226bf3e9fac216a31b5cd170782
diff --git a/src/com/android/providers/calendar/CalendarDatabaseHelper.java b/src/com/android/providers/calendar/CalendarDatabaseHelper.java
index 01bd6f8..1548a67 100644
--- a/src/com/android/providers/calendar/CalendarDatabaseHelper.java
+++ b/src/com/android/providers/calendar/CalendarDatabaseHelper.java
@@ -16,10 +16,6 @@
 
 package com.android.providers.calendar;
 
-import com.google.common.annotations.VisibleForTesting;
-
-import com.android.common.content.SyncStateContentProviderHelper;
-
 import android.accounts.Account;
 import android.content.ContentResolver;
 import android.content.ContentValues;
@@ -42,6 +38,9 @@
 import android.text.format.Time;
 import android.util.Log;
 
+import com.android.common.content.SyncStateContentProviderHelper;
+import com.google.common.annotations.VisibleForTesting;
+
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
 import java.util.TimeZone;
@@ -71,9 +70,10 @@
     // 2xx for Honeycomb
     // 3xx for ICS
     // 4xx for JB
-    // 5xx for K
+    // 5xx for JB MR1
+    // 6xx for K
     // Bump this to the next hundred at each major release.
-    static final int DATABASE_VERSION = 403;
+    static final int DATABASE_VERSION = 501;
 
     private static final int PRE_FROYO_SYNC_STATE_VERSION = 3;
 
@@ -112,6 +112,7 @@
             Events.GUESTS_CAN_INVITE_OTHERS + "," +
             Events.GUESTS_CAN_SEE_GUESTS + "," +
             Events.ORGANIZER + "," +
+            Events.IS_ORGANIZER + "," +
             Events.CUSTOM_APP_PACKAGE + "," +
             Events.CUSTOM_APP_URI;
 
@@ -558,6 +559,7 @@
                 CalendarContract.Events.GUESTS_CAN_INVITE_OTHERS + " INTEGER NOT NULL DEFAULT 1," +
                 CalendarContract.Events.GUESTS_CAN_SEE_GUESTS + " INTEGER NOT NULL DEFAULT 1," +
                 CalendarContract.Events.ORGANIZER + " STRING," +
+                CalendarContract.Events.IS_ORGANIZER + " INTEGER," +
                 CalendarContract.Events.DELETED + " INTEGER NOT NULL DEFAULT 0," +
                 // timezone for event with allDay events are in local timezone
                 CalendarContract.Events.EVENT_END_TIMEZONE + " TEXT," +
@@ -773,6 +775,7 @@
                 Calendars.CALENDAR_LOCATION + " TEXT," +
                 Calendars.CALENDAR_TIME_ZONE + " TEXT," +
                 Calendars.OWNER_ACCOUNT + " TEXT, " +
+                Calendars.IS_PRIMARY + " INTEGER, " +
                 Calendars.CAN_ORGANIZER_RESPOND + " INTEGER NOT NULL DEFAULT 1," +
                 Calendars.CAN_MODIFY_TIME_ZONE + " INTEGER DEFAULT 1," +
                 Calendars.CAN_PARTIALLY_UPDATE + " INTEGER DEFAULT 0," +
@@ -1390,6 +1393,12 @@
                 oldVersion = 403;
             }
 
+            if (oldVersion == 403) {
+                upgradeToVersion501(db);
+                createEventsView = true; // This is needed if the calendars or events schema changed
+                oldVersion = 501;
+            }
+
             if (createEventsView) {
                 createEventsView(db);
             }
@@ -1462,10 +1471,24 @@
     /**********************************************************/
 
     /**********************************************************/
-    /* 5xx db version is for K release
+    /* 6xx db version is for K release
     /**********************************************************/
 
     /**********************************************************/
+    /* 5xx db version is for JB MR1 release
+    /**********************************************************/
+
+    private void upgradeToVersion501(SQLiteDatabase db) {
+        /*
+         * Changes from version 403 to 501:
+         * - add isOrganizer column to Events table
+         * - add isPrimary column to Calendars table
+         */
+        db.execSQL("ALTER TABLE Events ADD COLUMN isOrganizer INTEGER;");
+        db.execSQL("ALTER TABLE Calendars ADD COLUMN isPrimary INTEGER;");
+    }
+
+    /**********************************************************/
     /* 4xx db version is for J release
     /**********************************************************/
 
@@ -3139,6 +3162,9 @@
                 + CalendarContract.Events.GUESTS_CAN_MODIFY + ","
                 + CalendarContract.Events.GUESTS_CAN_SEE_GUESTS + ","
                 + CalendarContract.Events.ORGANIZER + ","
+                + "COALESCE("
+                + Events.IS_ORGANIZER + ", " + Events.ORGANIZER + " = " + Calendars.OWNER_ACCOUNT
+                + ") AS " + Events.IS_ORGANIZER + ","
                 + CalendarContract.Events.CUSTOM_APP_PACKAGE + ","
                 + CalendarContract.Events.CUSTOM_APP_URI + ","
                 + CalendarContract.Events.SYNC_DATA1 + ","
diff --git a/src/com/android/providers/calendar/CalendarProvider2.java b/src/com/android/providers/calendar/CalendarProvider2.java
index 3b11e20..cf96f26 100644
--- a/src/com/android/providers/calendar/CalendarProvider2.java
+++ b/src/com/android/providers/calendar/CalendarProvider2.java
@@ -876,12 +876,14 @@
             case CALENDARS:
             case CALENDAR_ENTITIES:
                 qb.setTables(Tables.CALENDARS);
+                qb.setProjectionMap(sCalendarsProjectionMap);
                 selection = appendAccountToSelection(uri, selection, Calendars.ACCOUNT_NAME,
                         Calendars.ACCOUNT_TYPE);
                 break;
             case CALENDARS_ID:
             case CALENDAR_ENTITIES_ID:
                 qb.setTables(Tables.CALENDARS);
+                qb.setProjectionMap(sCalendarsProjectionMap);
                 selectionArgs = insertSelectionArg(selectionArgs, uri.getPathSegments().get(1));
                 qb.appendWhere(SQL_WHERE_ID);
                 break;
@@ -4553,6 +4555,7 @@
     private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
     private static final HashMap<String, String> sInstancesProjectionMap;
     private static final HashMap<String, String> sColorsProjectionMap;
+    protected static final HashMap<String, String> sCalendarsProjectionMap;
     protected static final HashMap<String, String> sEventsProjectionMap;
     private static final HashMap<String, String> sEventEntitiesProjectionMap;
     private static final HashMap<String, String> sAttendeesProjectionMap;
@@ -4614,6 +4617,48 @@
         sColorsProjectionMap.put(Colors.COLOR_TYPE, Colors.COLOR_TYPE);
         sColorsProjectionMap.put(Colors.COLOR, Colors.COLOR);
 
+        sCalendarsProjectionMap = new HashMap<String, String>();
+        sCalendarsProjectionMap.put(Calendars._ID, Calendars._ID);
+        sCalendarsProjectionMap.put(Calendars.ACCOUNT_NAME, Calendars.ACCOUNT_NAME);
+        sCalendarsProjectionMap.put(Calendars.ACCOUNT_TYPE, Calendars.ACCOUNT_TYPE);
+        sCalendarsProjectionMap.put(Calendars._SYNC_ID, Calendars._SYNC_ID);
+        sCalendarsProjectionMap.put(Calendars.DIRTY, Calendars.DIRTY);
+        sCalendarsProjectionMap.put(Calendars.NAME, Calendars.NAME);
+        sCalendarsProjectionMap.put(
+                Calendars.CALENDAR_DISPLAY_NAME, Calendars.CALENDAR_DISPLAY_NAME);
+        sCalendarsProjectionMap.put(Calendars.CALENDAR_COLOR, Calendars.CALENDAR_COLOR);
+        sCalendarsProjectionMap.put(Calendars.CALENDAR_COLOR_KEY, Calendars.CALENDAR_COLOR_KEY);
+        sCalendarsProjectionMap.put(Calendars.CALENDAR_ACCESS_LEVEL,
+                Calendars.CALENDAR_ACCESS_LEVEL);
+        sCalendarsProjectionMap.put(Calendars.VISIBLE, Calendars.VISIBLE);
+        sCalendarsProjectionMap.put(Calendars.SYNC_EVENTS, Calendars.SYNC_EVENTS);
+        sCalendarsProjectionMap.put(Calendars.CALENDAR_LOCATION, Calendars.CALENDAR_LOCATION);
+        sCalendarsProjectionMap.put(Calendars.CALENDAR_TIME_ZONE, Calendars.CALENDAR_TIME_ZONE);
+        sCalendarsProjectionMap.put(Calendars.OWNER_ACCOUNT, Calendars.OWNER_ACCOUNT);
+        sCalendarsProjectionMap.put(Calendars.IS_PRIMARY,
+                "COALESCE(" + Events.IS_PRIMARY + ", "
+                        + Calendars.OWNER_ACCOUNT + " = " + Calendars.ACCOUNT_NAME + ")");
+        sCalendarsProjectionMap.put(Calendars.CAN_ORGANIZER_RESPOND,
+                Calendars.CAN_ORGANIZER_RESPOND);
+        sCalendarsProjectionMap.put(Calendars.CAN_MODIFY_TIME_ZONE, Calendars.CAN_MODIFY_TIME_ZONE);
+        sCalendarsProjectionMap.put(Calendars.CAN_PARTIALLY_UPDATE, Calendars.CAN_PARTIALLY_UPDATE);
+        sCalendarsProjectionMap.put(Calendars.MAX_REMINDERS, Calendars.MAX_REMINDERS);
+        sCalendarsProjectionMap.put(Calendars.ALLOWED_REMINDERS, Calendars.ALLOWED_REMINDERS);
+        sCalendarsProjectionMap.put(Calendars.ALLOWED_AVAILABILITY, Calendars.ALLOWED_AVAILABILITY);
+        sCalendarsProjectionMap.put(Calendars.ALLOWED_ATTENDEE_TYPES,
+                Calendars.ALLOWED_ATTENDEE_TYPES);
+        sCalendarsProjectionMap.put(Calendars.DELETED, Calendars.DELETED);
+        sCalendarsProjectionMap.put(Calendars.CAL_SYNC1, Calendars.CAL_SYNC1);
+        sCalendarsProjectionMap.put(Calendars.CAL_SYNC2, Calendars.CAL_SYNC2);
+        sCalendarsProjectionMap.put(Calendars.CAL_SYNC3, Calendars.CAL_SYNC3);
+        sCalendarsProjectionMap.put(Calendars.CAL_SYNC4, Calendars.CAL_SYNC4);
+        sCalendarsProjectionMap.put(Calendars.CAL_SYNC5, Calendars.CAL_SYNC5);
+        sCalendarsProjectionMap.put(Calendars.CAL_SYNC6, Calendars.CAL_SYNC6);
+        sCalendarsProjectionMap.put(Calendars.CAL_SYNC7, Calendars.CAL_SYNC7);
+        sCalendarsProjectionMap.put(Calendars.CAL_SYNC8, Calendars.CAL_SYNC8);
+        sCalendarsProjectionMap.put(Calendars.CAL_SYNC9, Calendars.CAL_SYNC9);
+        sCalendarsProjectionMap.put(Calendars.CAL_SYNC10, Calendars.CAL_SYNC10);
+
         sEventsProjectionMap = new HashMap<String, String>();
         // Events columns
         sEventsProjectionMap.put(Events.ACCOUNT_NAME, Events.ACCOUNT_NAME);
@@ -4650,6 +4695,7 @@
         sEventsProjectionMap.put(Events.GUESTS_CAN_MODIFY, Events.GUESTS_CAN_MODIFY);
         sEventsProjectionMap.put(Events.GUESTS_CAN_SEE_GUESTS, Events.GUESTS_CAN_SEE_GUESTS);
         sEventsProjectionMap.put(Events.ORGANIZER, Events.ORGANIZER);
+        sEventsProjectionMap.put(Events.IS_ORGANIZER, Events.IS_ORGANIZER);
         sEventsProjectionMap.put(Events.CUSTOM_APP_PACKAGE, Events.CUSTOM_APP_PACKAGE);
         sEventsProjectionMap.put(Events.CUSTOM_APP_URI, Events.CUSTOM_APP_URI);
         sEventsProjectionMap.put(Events.DELETED, Events.DELETED);
@@ -4741,6 +4787,7 @@
         sEventEntitiesProjectionMap.put(Events.GUESTS_CAN_MODIFY, Events.GUESTS_CAN_MODIFY);
         sEventEntitiesProjectionMap.put(Events.GUESTS_CAN_SEE_GUESTS, Events.GUESTS_CAN_SEE_GUESTS);
         sEventEntitiesProjectionMap.put(Events.ORGANIZER, Events.ORGANIZER);
+        sEventEntitiesProjectionMap.put(Events.IS_ORGANIZER, Events.IS_ORGANIZER);
         sEventEntitiesProjectionMap.put(Events.CUSTOM_APP_PACKAGE, Events.CUSTOM_APP_PACKAGE);
         sEventEntitiesProjectionMap.put(Events.CUSTOM_APP_URI, Events.CUSTOM_APP_URI);
         sEventEntitiesProjectionMap.put(Events.DELETED, Events.DELETED);