Chromium Code Reviews| Index: chrome/android/webapk/libs/common/src/org/chromium/webapk/lib/common/WebApkUtils.java |
| diff --git a/chrome/android/webapk/libs/common/src/org/chromium/webapk/lib/common/WebApkUtils.java b/chrome/android/webapk/libs/common/src/org/chromium/webapk/lib/common/WebApkUtils.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..395c02ab1b26ee1a65c9f18ac3433d292c90670b |
| --- /dev/null |
| +++ b/chrome/android/webapk/libs/common/src/org/chromium/webapk/lib/common/WebApkUtils.java |
| @@ -0,0 +1,62 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.webapk.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"; |
|
gone
2016/05/25 21:11:02
nit: Kind of weird to hard-code this. Is there an
pkotwicz
2016/05/26 01:11:41
Ok, I have removed the default.
|
| + |
| + /** |
| + * 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( |
|
gone
2016/05/25 21:11:02
ContextUtils.getApplicationContext()?
pkotwicz
2016/05/26 01:11:41
Unfortunately the code in chrome/android/webapk ca
|
| + 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; |
| + } |
| +} |