Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1987)

Unified Diff: chrome/android/webapk/libs/client/src/org/chromium/webapk/lib/client/WebApkValidator.java

Issue 2707993003: [Android]: Hide add-to-homescreen app menu item when WebAPK is installed (Closed)
Patch Set: Add new state for 'Add to Homescreen' Menu item Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/webapk/libs/client/src/org/chromium/webapk/lib/client/WebApkValidator.java
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 bb1e2ec37140302172b5b7efc06dda697d881154..505dc2bf659b29f394c9b428503c704476c6749b 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
@@ -13,9 +13,11 @@ import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.content.pm.Signature;
+import android.support.annotation.NonNull;
import android.util.Log;
import java.util.Arrays;
+import java.util.LinkedList;
import java.util.List;
/**
@@ -32,19 +34,49 @@ public class WebApkValidator {
* whether the user has selected a default handler for the URL and whether the default
* handler is the WebAPK.
*
- * NOTE(yfriedman): This can fail if multiple WebAPKs can match the supplied url.
+ * NOTE(yfriedman): This can fail if multiple WebAPKs can match the supplied |url| (the first
+ * one found will be returned).
*
* @param context The application context.
- * @param url The url to check.
- * @return Package name of WebAPK which can handle the URL. Null if the url should not be
+ * @param url The URL to check.
+ * @return Package name of WebAPK which can handle the URL. Null if the URL should not be
* handled by a WebAPK.
*/
public static String queryWebApkPackage(Context context, String url) {
+ return findWebApkPackage(context, resolveInfosForUrl(context, url));
+ }
+
+ /**
+ * Queries the PackageManager to determine whether a WebAPK can handle the URL. Ignores
+ * whether the user has selected a default handler for the URL and whether the default
+ * handler is the WebAPK.
+ *
+ * NOTE: This can fail if multiple WebAPKs can match the supplied |url| (the first one found
+ * will be returned).
+ *
+ * @param context The application context.
+ * @param url The URL to check.
+ * @return Resolve Info of a WebAPK which can handle the URL. Null if the URL should not be
+ * handled by a WebAPK.
+ */
+ public static ResolveInfo queryResolveInfo(Context context, String url) {
+ return findResolveInfo(context, resolveInfosForUrl(context, url));
+ }
+
+ /**
+ * Fetches the list of {@link ResolveInfo}s from the PackageManager that can handle the URL.
+ *
+ * @param context The application context.
+ * @param url The URL to check.
+ * @return The list of resolve infos found that can handle the URL.
+ */
+ @NonNull
+ private static List<ResolveInfo> resolveInfosForUrl(Context context, String url) {
Intent intent;
try {
intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
} catch (Exception e) {
- return null;
+ return new LinkedList<>();
}
intent.addCategory(Intent.CATEGORY_BROWSABLE);
@@ -54,10 +86,8 @@ public class WebApkValidator {
selector.addCategory(Intent.CATEGORY_BROWSABLE);
selector.setComponent(null);
}
-
- List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentActivities(
+ return context.getPackageManager().queryIntentActivities(
intent, PackageManager.GET_RESOLVED_FILTER);
- return findWebApkPackage(context, resolveInfos);
}
/**
@@ -67,10 +97,24 @@ public class WebApkValidator {
* ResolveInfos corresponds to a WebAPK.
*/
public static String findWebApkPackage(Context context, List<ResolveInfo> infos) {
+ ResolveInfo resolveInfo = findResolveInfo(context, infos);
+ if (resolveInfo != null) {
+ return resolveInfo.activityInfo.packageName;
+ }
+ return null;
+ }
+
+ /**
+ * @param context The context to use to check whether WebAPK is valid.
+ * @param infos The ResolveInfos to search.
+ * @return ResolveInfo which corresponds to a WebAPK. Null if none of the ResolveInfos
+ * corresponds to a WebAPK.
+ */
+ private static ResolveInfo findResolveInfo(Context context, List<ResolveInfo> infos) {
for (ResolveInfo info : infos) {
if (info.activityInfo != null
&& isValidWebApk(context, info.activityInfo.packageName)) {
- return info.activityInfo.packageName;
+ return info;
}
}
return null;
@@ -87,7 +131,7 @@ public class WebApkValidator {
Log.wtf(TAG, "WebApk validation failure - expected signature not set."
+ "missing call to WebApkValidator.initWithBrowserHostSignature");
}
- if (!webappPackageName.startsWith(WEBAPK_PACKAGE_PREFIX)) {
+ if (webappPackageName == null || !webappPackageName.startsWith(WEBAPK_PACKAGE_PREFIX)) {
return false;
}
// check signature
@@ -96,8 +140,7 @@ public class WebApkValidator {
packageInfo = context.getPackageManager().getPackageInfo(webappPackageName,
PackageManager.GET_SIGNATURES);
} catch (NameNotFoundException e) {
- e.printStackTrace();
- Log.d(TAG, "WebApk not found");
+ Log.d(TAG, "WebApk not found", e);
return false;
}

Powered by Google App Engine
This is Rietveld 408576698