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.

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

mercurial