Block non-primary calendars from being shared to personal profile.

Fixes: 125764413
Test: atest CalendarProvider2Test
Test: atest ManagedProfileTest#testCrossProfileCalendar
Change-Id: I4a4d65e5065010b4ebbf846ee1aa6897dbc0eddf
diff --git a/src/com/android/providers/calendar/CalendarProvider2.java b/src/com/android/providers/calendar/CalendarProvider2.java
index 1c5e8e9..c856f9d 100644
--- a/src/com/android/providers/calendar/CalendarProvider2.java
+++ b/src/com/android/providers/calendar/CalendarProvider2.java
@@ -450,6 +450,10 @@
 
     /** set to 'true' to enable debug logging for recurrence exception code */
     private static final boolean DEBUG_EXCEPTION = false;
+    
+    private static final String SELECTION_PRIMARY_CALENDAR =
+            Calendars.IS_PRIMARY + "= 1"
+                    + " OR " + Calendars.ACCOUNT_NAME + "=" + Calendars.OWNER_ACCOUNT;
 
     private final ThreadLocal<Boolean> mCallingPackageErrorLogged = new ThreadLocal<Boolean>();
 
@@ -919,6 +923,12 @@
                         getCallingPackageName(), workProfileUserId);
     }
 
+    private String appendPrimaryOnlyToSelection(String selection) {
+        return TextUtils.isEmpty(selection)
+                ? SELECTION_PRIMARY_CALENDAR
+                : selection + " AND (" +  SELECTION_PRIMARY_CALENDAR + ")";
+    }
+
     private Cursor queryWorkProfileProvider(Uri localUri, String[] projection,
             String selection, String[] selectionArgs, String sortOrder,
             List<String> additionalPathSegments) {
@@ -939,6 +949,9 @@
                 remoteUri = Uri.withAppendedPath(remoteUri, segment);
             }
         }
+
+        selection = appendPrimaryOnlyToSelection(selection);
+
         final Cursor cursor = getContext().getContentResolver().query(remoteUri, projection,
                 selection, selectionArgs, sortOrder);
         return cursor == null ? createEmptyCursor(projection) : cursor;
diff --git a/tests/src/com/android/providers/calendar/CalendarProvider2Test.java b/tests/src/com/android/providers/calendar/CalendarProvider2Test.java
index f9180ca..f420622 100644
--- a/tests/src/com/android/providers/calendar/CalendarProvider2Test.java
+++ b/tests/src/com/android/providers/calendar/CalendarProvider2Test.java
@@ -3223,9 +3223,10 @@
         String[] projection = new String[] {
                 Calendars.IS_PRIMARY
         };
-        String selection = "((" + Calendars.ACCOUNT_NAME + " = ? ))";
+        String selection =
+                "((" + Calendars.IS_PRIMARY + " = ? OR " + Calendars.ACCOUNT_NAME + " = ?))";
         String[] selectionArgs = new String[] {
-                DEFAULT_ACCOUNT
+                "1", DEFAULT_ACCOUNT
         };
         Cursor cursor = mResolver.query(Calendars.CONTENT_URI, projection, selection, selectionArgs,
                 null);
@@ -3320,6 +3321,7 @@
         assertEquals(calendarId, cursor.getLong(1));
         assertEquals(WORK_EVENT_DTSTART, cursor.getLong(2));
         assertEquals(WORK_CALENDAR_COLOR, cursor.getInt(3));
+        cursor.close();
 
         cleanupEnterpriseTestForEvents(calendarId, 2);
         cleanupEnterpriseTestForCalendars(1);
@@ -3355,6 +3357,7 @@
         // There are two events that meet the search criteria.
         assertNotNull(cursor);
         assertEquals(2, cursor.getCount());
+        cursor.close();
 
         cleanupEnterpriseTestForEvents(calendarId, 2);
         cleanupEnterpriseTestForCalendars(1);
@@ -3393,6 +3396,7 @@
         assertEquals(calendarId, cursor.getLong(2));
         assertEquals(WORK_EVENT_DTSTART, cursor.getLong(3));
         assertEquals(WORK_CALENDAR_COLOR, cursor.getInt(4));
+        cursor.close();
 
         cleanupEnterpriseTestForEvents(calendarId, 2);
         cleanupEnterpriseTestForCalendars(1);
@@ -3427,6 +3431,8 @@
         assertEquals(calendarId, cursor.getLong(2));
         assertEquals(WORK_EVENT_DTSTART, cursor.getLong(3));
         assertEquals(WORK_CALENDAR_COLOR, cursor.getInt(4));
+        assertEquals(1, cursor.getInt(2));
+        cursor.close();
 
         cleanupEnterpriseTestForEvents(calendarId, 2);
         cleanupEnterpriseTestForCalendars(1);
@@ -3465,6 +3471,7 @@
                 cursor.getColumnIndex(Calendars.CALENDAR_COLOR)));
         assertEquals(1, cursor.getInt(
                 cursor.getColumnIndex(Calendars.IS_PRIMARY)));
+        cursor.close();
 
         cleanupEnterpriseTestForEvents(calendarId, 2);
         cleanupEnterpriseTestForCalendars(1);
@@ -3540,6 +3547,7 @@
         cursor.moveToFirst();
         assertEquals(idToTest, cursor.getLong(0));
         assertEquals(WORK_CALENDAR_COLOR, cursor.getInt(1));
+        cursor.close();
 
         cleanupEnterpriseTestForCalendars(2);
     }
@@ -3565,6 +3573,7 @@
                 cursor.getColumnIndex(Calendars._ID)));
         assertEquals(WORK_CALENDAR_COLOR, cursor.getInt(
                 cursor.getColumnIndex(Calendars.CALENDAR_COLOR)));
+        cursor.close();
 
         cleanupEnterpriseTestForCalendars(1);
     }
@@ -3597,6 +3606,7 @@
                 new String[]{}, null, null, null);
         assertTrue(cursor != null);
         assertTrue(cursor.getCount() == 0);
+        cursor.close();
 
         cleanupEnterpriseTestForCalendars(1);
     }