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

Unified Diff: chrome/android/webapk/shell_apk/src/org/chromium/webapk/shell_apk/WebApkUtils.java

Issue 2858563004: Add support for webapk without runtimeHost (Closed)
Patch Set: Fix test failure. Created 3 years, 7 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/shell_apk/src/org/chromium/webapk/shell_apk/WebApkUtils.java
diff --git a/chrome/android/webapk/shell_apk/src/org/chromium/webapk/shell_apk/WebApkUtils.java b/chrome/android/webapk/shell_apk/src/org/chromium/webapk/shell_apk/WebApkUtils.java
index a9b5424f22948798560bdbc803c658000c769658..3e8c8e2b83d4b28c50925663047dac735de0d43d 100644
--- a/chrome/android/webapk/shell_apk/src/org/chromium/webapk/shell_apk/WebApkUtils.java
+++ b/chrome/android/webapk/shell_apk/src/org/chromium/webapk/shell_apk/WebApkUtils.java
@@ -5,24 +5,50 @@
package org.chromium.webapk.shell_apk;
import android.content.Context;
+import android.content.Intent;
+import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
+import android.content.pm.ResolveInfo;
+import android.net.Uri;
import android.os.Bundle;
+import android.text.TextUtils;
+import org.chromium.webapk.lib.common.WebApkConstants;
import org.chromium.webapk.lib.common.WebApkMetaDataKeys;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
/**
* Contains utility methods for interacting with WebAPKs.
*/
public class WebApkUtils {
+ public static final String SHARED_PREF_RUNTIME_HOST = "runtime_host";
+
+ // The package names of the channels of Chrome that support WebAPKs. The most preferred one
+ // comes first.
+ private static List<String> sBrowsersSupportingWebApk = new ArrayList<String>(
+ Arrays.asList("com.google.android.apps.chrome", "com.android.chrome", "com.chrome.beta",
+ "com.chrome.dev", "com.chrome.canary"));
/**
- * Caches the value read from Application Metadata which specifies the host browser's package
- * name.
+ * Caches the package name of the host browser. {@link sHostPackage} might refer to a browser
+ * which has been uninstalled. A notification can keep the WebAPK process alive after the host
+ * browser has been uninstalled.
*/
private static String sHostPackage;
+ public static void resetCachedHostPackageForTesting() {
Yaron 2017/05/15 18:41:37 Can you confirm whether these are removed for rele
Xi Han 2017/05/16 13:50:39 I have confirmed that these functions aren't in th
+ sHostPackage = null;
+ }
+
+ public static void setBrowsersSupportingWebApkForTesting(List<String> browsers) {
+ sBrowsersSupportingWebApk = browsers;
+ }
+
/**
* Returns a Context for the host browser that was specified when building the WebAPK.
* @param context A context.
@@ -41,26 +67,66 @@ public class WebApkUtils {
}
/**
- * Returns the package name for the host browser that was specified when building the WebAPK.
+ * Returns the package name of the host browser to launch the WebAPK. Also caches the package
+ * name in the SharedPreference if it is not null.
* @param context A context.
* @return The package name. Returns null on an error.
*/
public static String getHostBrowserPackageName(Context context) {
- if (sHostPackage != null) return sHostPackage;
- String hostPackage = null;
- try {
- ApplicationInfo ai = context.getPackageManager().getApplicationInfo(
- context.getPackageName(), PackageManager.GET_META_DATA);
- Bundle bundle = ai.metaData;
- hostPackage = bundle.getString(WebApkMetaDataKeys.RUNTIME_HOST);
- } catch (NameNotFoundException e) {
- e.printStackTrace();
+ if (sHostPackage == null) {
+ sHostPackage = getHostBrowserPackageNameInternal(context);
+ if (sHostPackage != null) {
+ writeHostBrowserToSharedPref(context, sHostPackage);
+ }
}
- // Set {@link sHostPackage} to a non-null value so that the value is computed only once.
- sHostPackage = hostPackage != null ? hostPackage : "";
+
return sHostPackage;
}
+ /**
+ * Returns the package name of the host browser to launch the WebAPK, or null if not find one.
+ */
+ private static String getHostBrowserPackageNameInternal(Context context) {
+ // Gets all installed browsers that support WebAPKs.
+ List<String> installedBrowsers = getInstalledBrowsers(context.getPackageManager());
+ if (installedBrowsers.isEmpty()) {
+ return null;
+ }
+
+ // Gets the package name of the host browser if it is specified in AndroidManifest.xml.
+ String hostBrowserFromManifest = getHostBrowserFromAndroidManifest(
+ context.getPackageManager(), context.getPackageName());
+ if (!TextUtils.isEmpty(hostBrowserFromManifest)
+ && installedBrowsers.contains(hostBrowserFromManifest)) {
+ return hostBrowserFromManifest;
+ }
+
+ // Gets the package name of the host browser if it is stored in the SharedPreference.
+ String cachedHostBrowser = getHostBrowserFromSharedPreference(context);
+ if (!TextUtils.isEmpty(cachedHostBrowser)
+ && installedBrowsers.contains(cachedHostBrowser)) {
+ return cachedHostBrowser;
+ }
+
+ // Gets the package name of the default browser on the Android device.
+ // TODO(hanxi): Investigate the best way to know which browser supports WebAPKs.
+ String defaultBrowser = getDefaultBrowserPackageName(context.getPackageManager());
+ if (!TextUtils.isEmpty(defaultBrowser) && installedBrowsers.contains(defaultBrowser)
+ && sBrowsersSupportingWebApk.contains(defaultBrowser)) {
+ return defaultBrowser;
+ }
+
+ // TODO(hanxi): adds a new dialog to ask users to select a host browser when the default
+ // browser doesn't support WebAPKs.
+ for (String name : sBrowsersSupportingWebApk) {
+ if (installedBrowsers.contains(name)) {
+ return name;
+ }
+ }
+
+ return null;
+ }
+
/**
* Returns the uid for the host browser that was specified when building the WebAPK.
* @param context A context.
@@ -81,4 +147,59 @@ public class WebApkUtils {
}
return -1;
}
+
+ /** Returns the package name of the host browser cached in the SharedPreferences. */
+ private static String getHostBrowserFromSharedPreference(Context context) {
+ SharedPreferences sharedPref =
+ context.getSharedPreferences(WebApkConstants.PREF_PACKAGE, Context.MODE_PRIVATE);
+ return sharedPref.getString(SHARED_PREF_RUNTIME_HOST, null);
+ }
+
+ /** Returns a list of package names of all the installed browsers on the device. */
+ private static List<String> getInstalledBrowsers(PackageManager packageManager) {
Yaron 2017/05/15 18:41:38 Ok, so Peter was right that ordering matters for t
Xi Han 2017/05/16 13:50:39 You are right, updated to set.
+ Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"));
+ List<ResolveInfo> resolvedActivityList = packageManager.queryIntentActivities(
+ browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
+
+ List<String> packagesSupportingWebApks = new ArrayList<>();
+ for (ResolveInfo info : resolvedActivityList) {
+ packagesSupportingWebApks.add(info.activityInfo.packageName);
+ }
+ return packagesSupportingWebApks;
+ }
+
+ /** Returns the package name of the "runtime host" in the AndroidManifest.xml. */
+ private static String getHostBrowserFromAndroidManifest(
+ PackageManager packageManager, String webApkPackageName) {
+ try {
+ ApplicationInfo ai = packageManager.getApplicationInfo(
+ webApkPackageName, PackageManager.GET_META_DATA);
+ Bundle bundle = ai.metaData;
+ return bundle.getString(WebApkMetaDataKeys.RUNTIME_HOST);
+ } catch (NameNotFoundException e) {
+ return null;
+ }
+ }
+
+ /** Returns the package name of the default browser on the Android device. */
+ private static String getDefaultBrowserPackageName(PackageManager packageManager) {
+ Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"));
+ ResolveInfo resolveInfo =
+ packageManager.resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
+ if (resolveInfo == null || resolveInfo.activityInfo == null) return null;
+
+ return resolveInfo.activityInfo.packageName;
+ }
+
+ /** Writes the package name of the host browser to the SharedPreferences. */
+ private static void writeHostBrowserToSharedPref(
+ final Context context, final String hostPackage) {
+ if (TextUtils.isEmpty(hostPackage)) return;
+
+ SharedPreferences sharedPref =
+ context.getSharedPreferences(WebApkConstants.PREF_PACKAGE, Context.MODE_PRIVATE);
+ SharedPreferences.Editor editor = sharedPref.edit();
+ editor.putString(SHARED_PREF_RUNTIME_HOST, hostPackage);
+ editor.apply();
+ }
}

Powered by Google App Engine
This is Rietveld 408576698