mobile/android/base/webapp/ApkResources.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko.webapp;
michael@0 7
michael@0 8 import java.io.BufferedReader;
michael@0 9 import java.io.File;
michael@0 10 import java.io.FileNotFoundException;
michael@0 11 import java.io.IOException;
michael@0 12 import java.io.InputStreamReader;
michael@0 13 import android.content.Context;
michael@0 14 import android.content.pm.ApplicationInfo;
michael@0 15 import android.content.pm.PackageManager;
michael@0 16 import android.content.pm.PackageManager.NameNotFoundException;
michael@0 17 import android.graphics.drawable.Drawable;
michael@0 18 import android.net.Uri;
michael@0 19 import android.os.Bundle;
michael@0 20 import android.os.Environment;
michael@0 21 import android.util.Log;
michael@0 22
michael@0 23 public class ApkResources {
michael@0 24 private static final String LOGTAG = "GeckoWebappApkResources";
michael@0 25 private final String mPackageName;
michael@0 26 private final ApplicationInfo mInfo;
michael@0 27 private final Context mContext;
michael@0 28
michael@0 29 public ApkResources(Context context, String packageName) throws NameNotFoundException {
michael@0 30 mPackageName = packageName;
michael@0 31 mInfo = context.getPackageManager().getApplicationInfo(
michael@0 32 mPackageName, PackageManager.GET_META_DATA);
michael@0 33 mContext = context;
michael@0 34 }
michael@0 35
michael@0 36 private ApplicationInfo info() {
michael@0 37 return mInfo;
michael@0 38 }
michael@0 39
michael@0 40 public String getPackageName() {
michael@0 41 return mPackageName;
michael@0 42 }
michael@0 43
michael@0 44 private Bundle metadata() {
michael@0 45 return mInfo.metaData;
michael@0 46 }
michael@0 47
michael@0 48 public String getManifest(Context context) {
michael@0 49 return readResource(context, "manifest");
michael@0 50 }
michael@0 51
michael@0 52 public String getMiniManifest(Context context) {
michael@0 53 return readResource(context, "mini");
michael@0 54 }
michael@0 55
michael@0 56 public String getManifestUrl() {
michael@0 57 return metadata().getString("manifestUrl");
michael@0 58 }
michael@0 59
michael@0 60 public boolean isPackaged() {
michael@0 61 return "packaged".equals(getWebappType());
michael@0 62 }
michael@0 63
michael@0 64 public boolean isDebuggable() {
michael@0 65 return (mInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
michael@0 66 }
michael@0 67
michael@0 68 private String readResource(Context context, String resourceName) {
michael@0 69 Uri resourceUri = Uri.parse("android.resource://" + mPackageName
michael@0 70 + "/raw/" + resourceName);
michael@0 71 StringBuilder fileContent = new StringBuilder();
michael@0 72 try {
michael@0 73 BufferedReader r = new BufferedReader(new InputStreamReader(context
michael@0 74 .getContentResolver().openInputStream(resourceUri)));
michael@0 75 String line;
michael@0 76
michael@0 77 while ((line = r.readLine()) != null) {
michael@0 78 fileContent.append(line);
michael@0 79 }
michael@0 80 } catch (FileNotFoundException e) {
michael@0 81 Log.e(LOGTAG, String.format("File not found: \"%s\"", resourceName));
michael@0 82 } catch (IOException e) {
michael@0 83 Log.e(LOGTAG, String.format("Couldn't read file: \"%s\"", resourceName));
michael@0 84 }
michael@0 85
michael@0 86 return fileContent.toString();
michael@0 87 }
michael@0 88
michael@0 89 public Uri getAppIconUri() {
michael@0 90 return Uri.parse("android.resource://" + mPackageName + "/" + info().icon);
michael@0 91 }
michael@0 92
michael@0 93 public Drawable getAppIcon() {
michael@0 94 return info().loadIcon(mContext.getPackageManager());
michael@0 95 }
michael@0 96
michael@0 97 public String getWebappType() {
michael@0 98 return metadata().getString("webapp");
michael@0 99 }
michael@0 100
michael@0 101 public String getAppName() {
michael@0 102 return info().name;
michael@0 103 }
michael@0 104
michael@0 105 /**
michael@0 106 * Which APK installer installed this APK.
michael@0 107 *
michael@0 108 * For OEM backed marketplaces, this will be non-<code>null</code>. Otherwise, <code>null</code>.
michael@0 109 *
michael@0 110 * TODO check that the G+ package installer gives us non-null results.
michael@0 111 *
michael@0 112 * @return the package name of the APK that installed this.
michael@0 113 */
michael@0 114 public String getPackageInstallerName() {
michael@0 115 return mContext.getPackageManager().getInstallerPackageName(mPackageName);
michael@0 116 }
michael@0 117
michael@0 118 public Uri getZipFileUri() {
michael@0 119 return Uri.parse("android.resource://" + mPackageName + "/raw/application");
michael@0 120 }
michael@0 121
michael@0 122 public File getFileDirectory() {
michael@0 123 File dir = mContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
michael@0 124 String path = dir.getAbsolutePath().replace(mContext.getPackageName(), mPackageName);
michael@0 125
michael@0 126 dir = new File(path);
michael@0 127
michael@0 128 if (!dir.exists()) {
michael@0 129 dir.mkdirs();
michael@0 130 }
michael@0 131
michael@0 132 return dir;
michael@0 133 }
michael@0 134 }

mercurial