Use Default Browser App for IntentResolution when needed

- add MATCH_ALL as a new flag for telling that all results need to
be returned (even if there is some sort of filtering done).
- take into account the default Browser App for Intent resolution
- also, dont do any domain verification priming for non system app

See bug #20144393

Change-Id: Iddd1f2029e3bbf3b99ebc5f416dc7f17e5bad10c
diff --git a/api/current.txt b/api/current.txt
index 035b372..6c3be70 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -9402,6 +9402,7 @@
     field public static final int GET_SIGNATURES = 64; // 0x40
     field public static final int GET_UNINSTALLED_PACKAGES = 8192; // 0x2000
     field public static final int GET_URI_PERMISSION_PATTERNS = 2048; // 0x800
+    field public static final int MATCH_ALL = 131072; // 0x20000
     field public static final int MATCH_DEFAULT_ONLY = 65536; // 0x10000
     field public static final long MAXIMUM_VERIFICATION_TIMEOUT = 3600000L; // 0x36ee80L
     field public static final int PERMISSION_DENIED = -1; // 0xffffffff
@@ -30433,7 +30434,6 @@
   public static abstract class InCallService.VideoCall {
     ctor public InCallService.VideoCall();
     method public abstract void registerCallback(android.telecom.InCallService.VideoCall.Callback);
-    method public abstract void unregisterCallback();
     method public abstract void requestCallDataUsage();
     method public abstract void requestCameraCapabilities();
     method public abstract void sendSessionModifyRequest(android.telecom.VideoProfile);
@@ -30444,6 +30444,7 @@
     method public abstract void setPauseImage(java.lang.String);
     method public abstract void setPreviewSurface(android.view.Surface);
     method public abstract void setZoom(float);
+    method public abstract void unregisterCallback();
   }
 
   public static abstract class InCallService.VideoCall.Callback {
diff --git a/api/system-current.txt b/api/system-current.txt
index e49ef9d..44bd836 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -9697,6 +9697,7 @@
     field public static final int INSTALL_PARSE_FAILED_NO_CERTIFICATES = -103; // 0xffffff99
     field public static final int INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION = -102; // 0xffffff9a
     field public static final int INSTALL_SUCCEEDED = 1; // 0x1
+    field public static final int MATCH_ALL = 131072; // 0x20000
     field public static final int MATCH_DEFAULT_ONLY = 65536; // 0x10000
     field public static final long MAXIMUM_VERIFICATION_TIMEOUT = 3600000L; // 0x36ee80L
     field public static final int PERMISSION_DENIED = -1; // 0xffffffff
@@ -32550,7 +32551,6 @@
   public static abstract class InCallService.VideoCall {
     ctor public InCallService.VideoCall();
     method public abstract void registerCallback(android.telecom.InCallService.VideoCall.Callback);
-    method public abstract void unregisterCallback();
     method public abstract void requestCallDataUsage();
     method public abstract void requestCameraCapabilities();
     method public abstract void sendSessionModifyRequest(android.telecom.VideoProfile);
@@ -32561,6 +32561,7 @@
     method public abstract void setPauseImage(java.lang.String);
     method public abstract void setPreviewSurface(android.view.Surface);
     method public abstract void setZoom(float);
+    method public abstract void unregisterCallback();
   }
 
   public static abstract class InCallService.VideoCall.Callback {
diff --git a/core/java/android/content/pm/PackageManager.java b/core/java/android/content/pm/PackageManager.java
index e1c271d..f01ca09 100644
--- a/core/java/android/content/pm/PackageManager.java
+++ b/core/java/android/content/pm/PackageManager.java
@@ -209,7 +209,14 @@
      * matching.  This is a synonym for including the CATEGORY_DEFAULT in your
      * supplied Intent.
      */
-    public static final int MATCH_DEFAULT_ONLY   = 0x00010000;
+    public static final int MATCH_DEFAULT_ONLY  = 0x00010000;
+
+    /**
+     * Querying flag: if set and if the platform is doing any filtering of the results, then
+     * the filtering will not happen. This is a synonym for saying that all results should
+     * be returned.
+     */
+    public static final int MATCH_ALL = 0x00020000;
 
     /**
      * Flag for {@link addCrossProfileIntentFilter}: if this flag is set:
@@ -2637,6 +2644,8 @@
      * {@link #MATCH_DEFAULT_ONLY}, to limit the resolution to only
      * those activities that support the {@link android.content.Intent#CATEGORY_DEFAULT}.
      *
+     * You can also set {@link #MATCH_ALL} for preventing the filtering of the results.
+     *
      * @return A List<ResolveInfo> containing one entry for each matching
      *         Activity. These are ordered from best to worst match -- that
      *         is, the first item in the list is what is returned by
@@ -2658,6 +2667,8 @@
      * {@link #MATCH_DEFAULT_ONLY}, to limit the resolution to only
      * those activities that support the {@link android.content.Intent#CATEGORY_DEFAULT}.
      *
+     * You can also set {@link #MATCH_ALL} for preventing the filtering of the results.
+     *
      * @return A List<ResolveInfo> containing one entry for each matching
      *         Activity. These are ordered from best to worst match -- that
      *         is, the first item in the list is what is returned by
diff --git a/services/core/java/com/android/server/pm/PackageManagerService.java b/services/core/java/com/android/server/pm/PackageManagerService.java
index f087c33..6c18e25 100644
--- a/services/core/java/com/android/server/pm/PackageManagerService.java
+++ b/services/core/java/com/android/server/pm/PackageManagerService.java
@@ -49,6 +49,7 @@
 import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ASK;
 import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_NEVER;
 import static android.content.pm.PackageManager.INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED;
+import static android.content.pm.PackageManager.MATCH_ALL;
 import static android.content.pm.PackageManager.MOVE_FAILED_DOESNT_EXIST;
 import static android.content.pm.PackageManager.MOVE_FAILED_INTERNAL_ERROR;
 import static android.content.pm.PackageManager.MOVE_FAILED_OPERATION_PENDING;
@@ -2274,6 +2275,13 @@
                 }
                 continue;
             }
+            if (!pkg.isSystemApp()) {
+                if (logging) {
+                    Slog.d(TAG, "No priming domain verifications for a non system package : " +
+                            packageName);
+                }
+                continue;
+            }
             for (PackageParser.Activity a : pkg.activities) {
                 for (ActivityIntentInfo filter : a.intents) {
                     if (hasValidDomains(filter, false)) {
@@ -2281,7 +2289,7 @@
                     }
                 }
             }
-            if (allHosts.size() > 0) {
+            if (allHosts.size() == 0) {
                 allHosts.add("*");
             }
             IntentFilterVerificationInfo ivi =
@@ -3939,7 +3947,7 @@
                 }
                 result = filterIfNotPrimaryUser(result, userId);
                 if (result.size() > 1 && hasWebURI(intent)) {
-                    return filterCandidatesWithDomainPreferedActivitiesLPr(result);
+                    return filterCandidatesWithDomainPreferedActivitiesLPr(flags, result);
                 }
                 return result;
             }
@@ -3984,7 +3992,7 @@
     }
 
     private List<ResolveInfo> filterCandidatesWithDomainPreferedActivitiesLPr(
-            List<ResolveInfo> candidates) {
+            int flags, List<ResolveInfo> candidates) {
         if (DEBUG_PREFERRED) {
             Slog.v("TAG", "Filtering results with prefered activities. Candidates count: " +
                     candidates.size());
@@ -4004,6 +4012,11 @@
                 String packageName = info.activityInfo.packageName;
                 PackageSetting ps = mSettings.mPackages.get(packageName);
                 if (ps != null) {
+                    // Add to the special match all list (Browser use case)
+                    if (info.handleAllWebDataURI) {
+                        matchAllList.add(info);
+                        continue;
+                    }
                     // Try to get the status from User settings first
                     int status = getDomainVerificationStatusLPr(ps, userId);
                     if (status == INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS) {
@@ -4013,10 +4026,6 @@
                     } else if (status == INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_UNDEFINED) {
                         undefinedList.add(info);
                     }
-                    // Add to the special match all list (Browser use case)
-                    if (info.handleAllWebDataURI) {
-                        matchAllList.add(info);
-                    }
                 }
             }
             // If there is nothing selected, add all candidates and remove the ones that the User
@@ -4031,7 +4040,30 @@
             result.removeAll(matchAllList);
             if (result.size() == 0) {
                 result.addAll(undefinedList);
-                result.addAll(matchAllList);
+                if ((flags & MATCH_ALL) != 0) {
+                    result.addAll(matchAllList);
+                } else {
+                    // Try to add the Default Browser if we can
+                    final String defaultBrowserPackageName = getDefaultBrowserPackageName(
+                            UserHandle.myUserId());
+                    if (!TextUtils.isEmpty(defaultBrowserPackageName)) {
+                        boolean defaultBrowserFound = false;
+                        final int browserCount = matchAllList.size();
+                        for (int n=0; n<browserCount; n++) {
+                            ResolveInfo browser = matchAllList.get(n);
+                            if (browser.activityInfo.packageName.equals(defaultBrowserPackageName)) {
+                                result.add(browser);
+                                defaultBrowserFound = true;
+                                break;
+                            }
+                        }
+                        if (!defaultBrowserFound) {
+                            result.addAll(matchAllList);
+                        }
+                    } else {
+                        result.addAll(matchAllList);
+                    }
+                }
             }
         }
         if (DEBUG_PREFERRED) {
@@ -11331,10 +11363,16 @@
                                 verifierUid, userId, verificationId, filter, packageName);
                         count++;
                     } else if (!needsFilterVerification) {
-                        Slog.d(TAG, "No verification needed for IntentFilter:"
-                                + filter.toString());
+                        Slog.d(TAG, "No verification needed for IntentFilter:" + filter.toString());
                         if (hasValidDomains(filter)) {
-                            allHosts.addAll(filter.getHostsList());
+                            ArrayList<String> hosts = filter.getHostsList();
+                            if (hosts.size() > 0) {
+                                allHosts.addAll(hosts);
+                            } else {
+                                if (allHosts.isEmpty()) {
+                                    allHosts.add("*");
+                                }
+                            }
                         }
                     } else {
                         Slog.d(TAG, "Verification already done for IntentFilter:"