Index: web_apks/minting_example/libs/common/org/chromium/minting/lib/common/WebAPKUtils.java |
diff --git a/web_apks/minting_example/libs/common/org/chromium/minting/lib/common/WebAPKUtils.java b/web_apks/minting_example/libs/common/org/chromium/minting/lib/common/WebAPKUtils.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c8b407954b7d62206d9fd9488bb98dfb9c9a5cc0 |
--- /dev/null |
+++ b/web_apks/minting_example/libs/common/org/chromium/minting/lib/common/WebAPKUtils.java |
@@ -0,0 +1,58 @@ |
+package org.chromium.minting.lib.common; |
+ |
+import android.content.Context; |
+import android.content.pm.ApplicationInfo; |
+import android.content.pm.PackageManager; |
+import android.content.pm.PackageManager.NameNotFoundException; |
+import android.os.Bundle; |
+ |
+/** |
+ * Contains utility methods for interacting with WebApks. |
+ */ |
+public class WebAPKUtils { |
+ |
+ private static final String DEFAULT_CHROME_PACKAGE_NAME = "com.google.android.apps.chrome"; |
+ |
+ /** |
+ * Caches the value read from Application Metadata which specifies the host browser's package |
+ * name. |
+ */ |
+ private static String sHostPackage; |
+ |
+ /** |
+ * Returns a Context for the host browser that was specified when building the WebAPK. |
+ * @param context A context. |
+ * @return The remote context. Returns null on an error. |
+ */ |
+ public static Context getHostBrowserContext(Context context) { |
+ try { |
+ String hostPackage = getHostBrowserPackageName(context); |
+ return context.getApplicationContext().createPackageContext( |
+ hostPackage, |
+ Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE); |
+ } catch (NameNotFoundException e) { |
+ e.printStackTrace(); |
+ } |
+ return null; |
+ } |
+ |
+ /** |
+ * Returns the package name for the host browser that was specified when building the WebAPK. |
+ * @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("runtimeHost"); |
+ } catch (NameNotFoundException e) { |
+ e.printStackTrace(); |
+ } |
+ sHostPackage = hostPackage != null ? hostPackage : DEFAULT_CHROME_PACKAGE_NAME; |
+ return sHostPackage; |
+ } |
+} |