1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/webapp/ApkResources.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,134 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.webapp; 1.10 + 1.11 +import java.io.BufferedReader; 1.12 +import java.io.File; 1.13 +import java.io.FileNotFoundException; 1.14 +import java.io.IOException; 1.15 +import java.io.InputStreamReader; 1.16 +import android.content.Context; 1.17 +import android.content.pm.ApplicationInfo; 1.18 +import android.content.pm.PackageManager; 1.19 +import android.content.pm.PackageManager.NameNotFoundException; 1.20 +import android.graphics.drawable.Drawable; 1.21 +import android.net.Uri; 1.22 +import android.os.Bundle; 1.23 +import android.os.Environment; 1.24 +import android.util.Log; 1.25 + 1.26 +public class ApkResources { 1.27 + private static final String LOGTAG = "GeckoWebappApkResources"; 1.28 + private final String mPackageName; 1.29 + private final ApplicationInfo mInfo; 1.30 + private final Context mContext; 1.31 + 1.32 + public ApkResources(Context context, String packageName) throws NameNotFoundException { 1.33 + mPackageName = packageName; 1.34 + mInfo = context.getPackageManager().getApplicationInfo( 1.35 + mPackageName, PackageManager.GET_META_DATA); 1.36 + mContext = context; 1.37 + } 1.38 + 1.39 + private ApplicationInfo info() { 1.40 + return mInfo; 1.41 + } 1.42 + 1.43 + public String getPackageName() { 1.44 + return mPackageName; 1.45 + } 1.46 + 1.47 + private Bundle metadata() { 1.48 + return mInfo.metaData; 1.49 + } 1.50 + 1.51 + public String getManifest(Context context) { 1.52 + return readResource(context, "manifest"); 1.53 + } 1.54 + 1.55 + public String getMiniManifest(Context context) { 1.56 + return readResource(context, "mini"); 1.57 + } 1.58 + 1.59 + public String getManifestUrl() { 1.60 + return metadata().getString("manifestUrl"); 1.61 + } 1.62 + 1.63 + public boolean isPackaged() { 1.64 + return "packaged".equals(getWebappType()); 1.65 + } 1.66 + 1.67 + public boolean isDebuggable() { 1.68 + return (mInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; 1.69 + } 1.70 + 1.71 + private String readResource(Context context, String resourceName) { 1.72 + Uri resourceUri = Uri.parse("android.resource://" + mPackageName 1.73 + + "/raw/" + resourceName); 1.74 + StringBuilder fileContent = new StringBuilder(); 1.75 + try { 1.76 + BufferedReader r = new BufferedReader(new InputStreamReader(context 1.77 + .getContentResolver().openInputStream(resourceUri))); 1.78 + String line; 1.79 + 1.80 + while ((line = r.readLine()) != null) { 1.81 + fileContent.append(line); 1.82 + } 1.83 + } catch (FileNotFoundException e) { 1.84 + Log.e(LOGTAG, String.format("File not found: \"%s\"", resourceName)); 1.85 + } catch (IOException e) { 1.86 + Log.e(LOGTAG, String.format("Couldn't read file: \"%s\"", resourceName)); 1.87 + } 1.88 + 1.89 + return fileContent.toString(); 1.90 + } 1.91 + 1.92 + public Uri getAppIconUri() { 1.93 + return Uri.parse("android.resource://" + mPackageName + "/" + info().icon); 1.94 + } 1.95 + 1.96 + public Drawable getAppIcon() { 1.97 + return info().loadIcon(mContext.getPackageManager()); 1.98 + } 1.99 + 1.100 + public String getWebappType() { 1.101 + return metadata().getString("webapp"); 1.102 + } 1.103 + 1.104 + public String getAppName() { 1.105 + return info().name; 1.106 + } 1.107 + 1.108 + /** 1.109 + * Which APK installer installed this APK. 1.110 + * 1.111 + * For OEM backed marketplaces, this will be non-<code>null</code>. Otherwise, <code>null</code>. 1.112 + * 1.113 + * TODO check that the G+ package installer gives us non-null results. 1.114 + * 1.115 + * @return the package name of the APK that installed this. 1.116 + */ 1.117 + public String getPackageInstallerName() { 1.118 + return mContext.getPackageManager().getInstallerPackageName(mPackageName); 1.119 + } 1.120 + 1.121 + public Uri getZipFileUri() { 1.122 + return Uri.parse("android.resource://" + mPackageName + "/raw/application"); 1.123 + } 1.124 + 1.125 + public File getFileDirectory() { 1.126 + File dir = mContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS); 1.127 + String path = dir.getAbsolutePath().replace(mContext.getPackageName(), mPackageName); 1.128 + 1.129 + dir = new File(path); 1.130 + 1.131 + if (!dir.exists()) { 1.132 + dir.mkdirs(); 1.133 + } 1.134 + 1.135 + return dir; 1.136 + } 1.137 +}