Throw exception when work profile doesn't exist or disabled or calendar app is not whitelisted or setting for calendar integration is disabled.

Test: atest ManagedProfileTest#testCrossProfileCalendar
      atest CalendarProvider2Test
Bug: 130284827

Change-Id: I3f5d2794c801b7a379893962eb815e89cc7da6b4
diff --git a/src/com/android/providers/calendar/CalendarProvider2.java b/src/com/android/providers/calendar/CalendarProvider2.java
index e6cf42b..4a6bd22 100644
--- a/src/com/android/providers/calendar/CalendarProvider2.java
+++ b/src/com/android/providers/calendar/CalendarProvider2.java
@@ -929,6 +929,12 @@
                 : selection + " AND (" +  SELECTION_PRIMARY_CALENDAR + ")";
     }
 
+    /*
+     * Throw UnsupportedOperationException if
+     * <p>1. Work profile doesn't exits or disabled.
+     * <p>2. Calling package is not allowed to access cross profile calendar.
+     * <p>3. CROSS_PROFILE_CALENDAR_ENABLED is turned off in Settings.
+     */
     private Cursor queryWorkProfileProvider(Uri localUri, String[] projection,
             String selection, String[] selectionArgs, String sortOrder,
             List<String> additionalPathSegments) {
@@ -936,10 +942,10 @@
         // allowed columns.
         projection = mCrossProfileCalendarHelper.getCalibratedProjection(
                 projection, localUri);
-        // Return empty cursor if cross profile calendar is currently not available.
+        // Throw exception if cross profile calendar is currently not available.
         final int workProfileUserId = getWorkProfileUserId();
         if (!canAccessCrossProfileCalendar(workProfileUserId)) {
-            return createEmptyCursor(projection);
+            throw new UnsupportedOperationException("Can't access cross profile for " + localUri);
         }
 
         Uri remoteUri = maybeAddUserId(
diff --git a/tests/src/com/android/providers/calendar/CalendarProvider2Test.java b/tests/src/com/android/providers/calendar/CalendarProvider2Test.java
index f420622..e0a4edf 100644
--- a/tests/src/com/android/providers/calendar/CalendarProvider2Test.java
+++ b/tests/src/com/android/providers/calendar/CalendarProvider2Test.java
@@ -3600,14 +3600,15 @@
         // Assume cross profile uri access is not allowed by policy or disabled in settings.
         MockCrossProfileCalendarHelper.setPackageAllowedToAccessCalendar(false);
 
-        // Test empty cursor is returned if cross profile calendar is disabled in settings.
-        final Cursor cursor = mResolver.query(
-                Calendars.ENTERPRISE_CONTENT_URI,
-                new String[]{}, null, null, null);
-        assertTrue(cursor != null);
-        assertTrue(cursor.getCount() == 0);
-        cursor.close();
-
+        // Throw exception if cross profile calendar is disabled in settings.
+        try {
+            final Cursor cursor = mResolver.query(
+                    Calendars.ENTERPRISE_CONTENT_URI,
+                    new String[]{}, null, null, null);
+            fail("Unsupported operation exception should have been raised.");
+        } catch (UnsupportedOperationException e) {
+            // Exception expected.
+        }
         cleanupEnterpriseTestForCalendars(1);
     }