Merge "Call getEntryData with correct user context." into udc-qpr-dev
diff --git a/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/TileUtils.java b/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/TileUtils.java
index e46db75..33907ec 100644
--- a/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/TileUtils.java
+++ b/packages/SettingsLib/Tile/src/com/android/settingslib/drawer/TileUtils.java
@@ -344,7 +344,9 @@
                 continue;
             }
             final ProviderInfo providerInfo = resolved.providerInfo;
-            final List<Bundle> entryData = getEntryDataFromProvider(context,
+            final List<Bundle> entryData = getEntryDataFromProvider(
+                    // Build new context so the entry data is retrieved for the queried user.
+                    context.createContextAsUser(user, 0 /* flags */),
                     providerInfo.authority);
             if (entryData == null || entryData.isEmpty()) {
                 continue;
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileUtilsTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileUtilsTest.java
index 2086466..a8063e8 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileUtilsTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/drawer/TileUtilsTest.java
@@ -35,6 +35,7 @@
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.ArgumentMatchers.isNull;
 import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
@@ -52,6 +53,7 @@
 import android.content.pm.ProviderInfo;
 import android.content.pm.ResolveInfo;
 import android.content.res.Resources;
+import android.net.Uri;
 import android.os.Bundle;
 import android.os.UserHandle;
 import android.os.UserManager;
@@ -69,6 +71,8 @@
 import org.robolectric.annotation.Config;
 import org.robolectric.annotation.Implementation;
 import org.robolectric.annotation.Implements;
+import org.robolectric.shadow.api.Shadow;
+import org.robolectric.util.ReflectionHelpers;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -88,6 +92,10 @@
     private UserManager mUserManager;
     @Mock
     private ContentResolver mContentResolver;
+    @Mock
+    private Context mUserContext;
+    @Mock
+    private ContentResolver mUserContentResolver;
 
     private static final String URI_GET_SUMMARY = "content://authority/text/summary";
     private static final String URI_GET_ICON = "content://authority/icon/my_icon";
@@ -104,6 +112,8 @@
         mContentResolver = spy(application.getContentResolver());
         when(mContext.getContentResolver()).thenReturn(mContentResolver);
         when(mContext.getPackageName()).thenReturn("com.android.settings");
+        when(mUserContext.getContentResolver()).thenReturn(mUserContentResolver);
+        ShadowTileUtils.sCallRealEntryDataFromProvider = false;
     }
 
     @Test
@@ -375,6 +385,30 @@
     }
 
     @Test
+    public void loadTilesForAction_forUserProvider_getEntryDataFromProvider_inContextOfGivenUser() {
+        ShadowTileUtils.sCallRealEntryDataFromProvider = true;
+        UserHandle userHandle = new UserHandle(10);
+
+        doReturn(mUserContext).when(mContext).createContextAsUser(eq(userHandle), anyInt());
+
+        Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>();
+        List<Tile> outTiles = new ArrayList<>();
+        List<ResolveInfo> info = new ArrayList<>();
+        ResolveInfo resolveInfo = newInfo(true, null /* category */, null, URI_GET_ICON,
+                URI_GET_SUMMARY, null, 123, PROFILE_ALL);
+        info.add(resolveInfo);
+
+        when(mPackageManager.queryIntentContentProvidersAsUser(any(Intent.class), anyInt(),
+            anyInt())).thenReturn(info);
+
+        TileUtils.loadTilesForAction(mContext, userHandle, IA_SETTINGS_ACTION,
+                addedCache, null /* defaultCategory */, outTiles, false /* requiresSettings */);
+
+        verify(mUserContentResolver, atLeastOnce())
+            .acquireUnstableProvider(any(Uri.class));
+    }
+
+    @Test
     public void loadTilesForAction_withPendingIntent_updatesPendingIntentMap() {
         Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>();
         List<Tile> outTiles = new ArrayList<>();
@@ -472,8 +506,17 @@
 
         private static Bundle sMetaData;
 
+        private static boolean sCallRealEntryDataFromProvider;
+
         @Implementation
         protected static List<Bundle> getEntryDataFromProvider(Context context, String authority) {
+            if (sCallRealEntryDataFromProvider) {
+                return Shadow.directlyOn(
+                    TileUtils.class,
+                    "getEntryDataFromProvider",
+                    ReflectionHelpers.ClassParameter.from(Context.class, context),
+                    ReflectionHelpers.ClassParameter.from(String.class, authority));
+            }
             return Arrays.asList(sMetaData);
         }