Scope package manager queries for ephemeral apps

Ephemeral apps can only see their own components or those components
exposed via the "visibleToEphemeral" XML attribute.

Normal apps can only see other normal apps. There is no way to expose
ephemeral components to normal apps.

Bug: 33458220
Test: Manually install ephemeral/normal apps and ensure they can only see appropriate components
Change-Id: I6ae65fd2a6ddc9aa9691f02cd55d4953048966b0
diff --git a/services/core/java/com/android/server/IntentResolver.java b/services/core/java/com/android/server/IntentResolver.java
index 83d374c..14abb53 100644
--- a/services/core/java/com/android/server/IntentResolver.java
+++ b/services/core/java/com/android/server/IntentResolver.java
@@ -350,8 +350,8 @@
         return Collections.unmodifiableSet(mFilters);
     }
 
-    public List<R> queryIntentFromList(Intent intent, String resolvedType, 
-            boolean defaultOnly, ArrayList<F[]> listCut, int userId) {
+    public List<R> queryIntentFromList(Intent intent, String resolvedType, boolean defaultOnly,
+            boolean visibleToEphemeral, boolean isEphemeral, ArrayList<F[]> listCut, int userId) {
         ArrayList<R> resultList = new ArrayList<R>();
 
         final boolean debug = localLOGV ||
@@ -361,8 +361,8 @@
         final String scheme = intent.getScheme();
         int N = listCut.size();
         for (int i = 0; i < N; ++i) {
-            buildResolveList(intent, categories, debug, defaultOnly,
-                    resolvedType, scheme, listCut.get(i), resultList, userId);
+            buildResolveList(intent, categories, debug, defaultOnly, visibleToEphemeral,
+                    isEphemeral, resolvedType, scheme, listCut.get(i), resultList, userId);
         }
         filterResults(resultList);
         sortResults(resultList);
@@ -370,7 +370,7 @@
     }
 
     public List<R> queryIntent(Intent intent, String resolvedType, boolean defaultOnly,
-            int userId) {
+            boolean visibleToEphemeral, boolean isEphemeral, int userId) {
         String scheme = intent.getScheme();
 
         ArrayList<R> finalList = new ArrayList<R>();
@@ -443,20 +443,20 @@
 
         FastImmutableArraySet<String> categories = getFastIntentCategories(intent);
         if (firstTypeCut != null) {
-            buildResolveList(intent, categories, debug, defaultOnly,
-                    resolvedType, scheme, firstTypeCut, finalList, userId);
+            buildResolveList(intent, categories, debug, defaultOnly, visibleToEphemeral,
+                    isEphemeral, resolvedType, scheme, firstTypeCut, finalList, userId);
         }
         if (secondTypeCut != null) {
-            buildResolveList(intent, categories, debug, defaultOnly,
-                    resolvedType, scheme, secondTypeCut, finalList, userId);
+            buildResolveList(intent, categories, debug, defaultOnly, visibleToEphemeral,
+                    isEphemeral, resolvedType, scheme, secondTypeCut, finalList, userId);
         }
         if (thirdTypeCut != null) {
-            buildResolveList(intent, categories, debug, defaultOnly,
-                    resolvedType, scheme, thirdTypeCut, finalList, userId);
+            buildResolveList(intent, categories, debug, defaultOnly, visibleToEphemeral,
+                    isEphemeral, resolvedType, scheme, thirdTypeCut, finalList, userId);
         }
         if (schemeCut != null) {
-            buildResolveList(intent, categories, debug, defaultOnly,
-                    resolvedType, scheme, schemeCut, finalList, userId);
+            buildResolveList(intent, categories, debug, defaultOnly, visibleToEphemeral,
+                    isEphemeral, resolvedType, scheme, schemeCut, finalList, userId);
         }
         filterResults(finalList);
         sortResults(finalList);
@@ -694,7 +694,7 @@
     }
 
     private void buildResolveList(Intent intent, FastImmutableArraySet<String> categories,
-            boolean debug, boolean defaultOnly,
+            boolean debug, boolean defaultOnly, boolean visibleToEphemeral, boolean isEphemeral,
             String resolvedType, String scheme, F[] src, List<R> dest, int userId) {
         final String action = intent.getAction();
         final Uri data = intent.getData();
@@ -735,6 +735,15 @@
                 continue;
             }
 
+            // throw out filters that aren't visible to ephemeral apps
+            if (visibleToEphemeral && !filter.isVisibleToEphemeral()) {
+                continue;
+            }
+            // throw out ephemeral filters if we're not explicitly requesting them
+            if (!isEphemeral && filter.isEphemeral()) {
+                continue;
+            }
+
             // Are we verified ?
             if (filter.getAutoVerify()) {
                 if (localVerificationLOGV || debug) {