[go: up one dir, main page]

Prefer launching WebApks for view intents in touchless mode.

(cherry picked from commit d9971f3089d2d6ef73494bd092d0c92d343a9019)

Bug: 968574
Change-Id: Ie952f411c10da45d7757bf235fca68c00fd6847a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1652205
Reviewed-by: Yaron Friedman <yfriedman@chromium.org>
Commit-Queue: Michael Thiessen <mthiesse@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#669307}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1664876
Reviewed-by: Michael Thiessen <mthiesse@chromium.org>
Cr-Commit-Position: refs/branch-heads/3809@{#418}
Cr-Branched-From: d82dec1a818f378c464ba307ddd9c92133eac355-refs/heads/master@{#665002}
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/LaunchIntentDispatcher.java b/chrome/android/java/src/org/chromium/chrome/browser/LaunchIntentDispatcher.java
index 6e84fa6..f7921f0 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/LaunchIntentDispatcher.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/LaunchIntentDispatcher.java
@@ -50,6 +50,7 @@
 import org.chromium.chrome.browser.webapps.WebappLauncherActivity;
 import org.chromium.content_public.browser.BrowserStartupController;
 import org.chromium.ui.widget.Toast;
+import org.chromium.webapk.lib.client.WebApkValidator;
 
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
@@ -194,6 +195,19 @@
             return Action.FINISH_ACTIVITY;
         }
 
+        // Check if we should launch a WebApk to handle the intent.
+        // For NoTouchMode, prefer to launch PWAs instead of the browser on view intents.
+        if (FeatureUtilities.isNoTouchModeEnabled() && url != null
+                && Intent.ACTION_VIEW.equals(mIntent.getAction())) {
+            String packageName = WebApkValidator.queryFirstWebApkPackage(
+                    ContextUtils.getApplicationContext(), url);
+            if (packageName != null) {
+                mActivity.startActivity(WebApkValidator.createWebApkIntentForUrlAndOptionalPackage(
+                        url, packageName));
+                return Action.FINISH_ACTIVITY;
+            }
+        }
+
         // Check if we should push the user through First Run.
         if (FirstRunFlowSequencer.launch(mActivity, mIntent, false /* requiresBroadcast */,
                     false /* preferLightweightFre */)) {
diff --git a/chrome/android/webapk/libs/client/src/org/chromium/webapk/lib/client/WebApkValidator.java b/chrome/android/webapk/libs/client/src/org/chromium/webapk/lib/client/WebApkValidator.java
index c74d78d..7882f23cd 100644
--- a/chrome/android/webapk/libs/client/src/org/chromium/webapk/lib/client/WebApkValidator.java
+++ b/chrome/android/webapk/libs/client/src/org/chromium/webapk/lib/client/WebApkValidator.java
@@ -130,24 +130,8 @@
      */
     private static List<ResolveInfo> resolveInfosForUrlAndOptionalPackage(
             Context context, String url, @Nullable String applicationPackage) {
-        Intent intent;
-        try {
-            intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
-        } catch (Exception e) {
-            return new LinkedList<>();
-        }
-
-        intent.addCategory(Intent.CATEGORY_BROWSABLE);
-        if (applicationPackage != null) {
-            intent.setPackage(applicationPackage);
-        } else {
-            intent.setComponent(null);
-        }
-        Intent selector = intent.getSelector();
-        if (selector != null) {
-            selector.addCategory(Intent.CATEGORY_BROWSABLE);
-            selector.setComponent(null);
-        }
+        Intent intent = createWebApkIntentForUrlAndOptionalPackage(url, applicationPackage);
+        if (intent == null) return new LinkedList<>();
 
         // StrictMode is relaxed due to https://crbug.com/843092.
         StrictMode.ThreadPolicy policy = StrictMode.allowThreadDiskReads();
@@ -228,6 +212,36 @@
         return verifyCommentSignedWebApk(packageInfo);
     }
 
+    /**
+     * @param url A Url that might launch a WebApk.
+     * @param applicationPackage The package of the WebApk to restrict the launch to.
+     * @return An intent that could launch a WebApk for the provided URL (and package), if such a
+     *         WebApk exists. If package isn't specified, the intent may create a disambiguation
+     *         dialog when started.
+     */
+    public static Intent createWebApkIntentForUrlAndOptionalPackage(
+            String url, @Nullable String applicationPackage) {
+        Intent intent;
+        try {
+            intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
+        } catch (Exception e) {
+            return null;
+        }
+
+        intent.addCategory(Intent.CATEGORY_BROWSABLE);
+        if (applicationPackage != null) {
+            intent.setPackage(applicationPackage);
+        } else {
+            intent.setComponent(null);
+        }
+        Intent selector = intent.getSelector();
+        if (selector != null) {
+            selector.addCategory(Intent.CATEGORY_BROWSABLE);
+            selector.setComponent(null);
+        }
+        return intent;
+    }
+
     /** Determine quickly whether this is definitely not a WebAPK */
     private static boolean isNotWebApkQuick(PackageInfo packageInfo) {
         if (packageInfo.applicationInfo == null || packageInfo.applicationInfo.metaData == null) {