OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.webapk.lib.common; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.content.pm.ApplicationInfo; |
| 9 import android.content.pm.PackageManager; |
| 10 import android.content.pm.PackageManager.NameNotFoundException; |
| 11 import android.os.Bundle; |
| 12 |
| 13 /** |
| 14 * Contains utility methods for interacting with WebAPKs. |
| 15 */ |
| 16 public class WebApkUtils { |
| 17 |
| 18 /** |
| 19 * Caches the value read from Application Metadata which specifies the host
browser's package |
| 20 * name. |
| 21 */ |
| 22 private static String sHostPackage; |
| 23 |
| 24 /** |
| 25 * Returns a Context for the host browser that was specified when building t
he WebAPK. |
| 26 * @param context A context. |
| 27 * @return The remote context. Returns null on an error. |
| 28 */ |
| 29 public static Context getHostBrowserContext(Context context) { |
| 30 try { |
| 31 String hostPackage = getHostBrowserPackageName(context); |
| 32 return context.getApplicationContext().createPackageContext( |
| 33 hostPackage, |
| 34 Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_IN
CLUDE_CODE); |
| 35 } catch (NameNotFoundException e) { |
| 36 e.printStackTrace(); |
| 37 } |
| 38 return null; |
| 39 } |
| 40 |
| 41 /** |
| 42 * Returns the package name for the host browser that was specified when bui
lding the WebAPK. |
| 43 * @param context A context. |
| 44 * @return The package name. Returns null on an error. |
| 45 */ |
| 46 public static String getHostBrowserPackageName(Context context) { |
| 47 if (sHostPackage != null) return sHostPackage; |
| 48 String hostPackage = null; |
| 49 try { |
| 50 ApplicationInfo ai = context.getPackageManager().getApplicationInfo( |
| 51 context.getPackageName(), PackageManager.GET_META_DATA); |
| 52 Bundle bundle = ai.metaData; |
| 53 hostPackage = bundle.getString("runtimeHost"); |
| 54 } catch (NameNotFoundException e) { |
| 55 e.printStackTrace(); |
| 56 } |
| 57 // Set {@link sHostPackage} to a non-null value so that the value is com
puted only once. |
| 58 sHostPackage = hostPackage != null ? hostPackage : ""; |
| 59 return sHostPackage; |
| 60 } |
| 61 } |
OLD | NEW |