michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.webapp; michael@0: michael@0: import java.io.BufferedReader; michael@0: import java.io.File; michael@0: import java.io.FileNotFoundException; michael@0: import java.io.IOException; michael@0: import java.io.InputStreamReader; michael@0: import android.content.Context; michael@0: import android.content.pm.ApplicationInfo; michael@0: import android.content.pm.PackageManager; michael@0: import android.content.pm.PackageManager.NameNotFoundException; michael@0: import android.graphics.drawable.Drawable; michael@0: import android.net.Uri; michael@0: import android.os.Bundle; michael@0: import android.os.Environment; michael@0: import android.util.Log; michael@0: michael@0: public class ApkResources { michael@0: private static final String LOGTAG = "GeckoWebappApkResources"; michael@0: private final String mPackageName; michael@0: private final ApplicationInfo mInfo; michael@0: private final Context mContext; michael@0: michael@0: public ApkResources(Context context, String packageName) throws NameNotFoundException { michael@0: mPackageName = packageName; michael@0: mInfo = context.getPackageManager().getApplicationInfo( michael@0: mPackageName, PackageManager.GET_META_DATA); michael@0: mContext = context; michael@0: } michael@0: michael@0: private ApplicationInfo info() { michael@0: return mInfo; michael@0: } michael@0: michael@0: public String getPackageName() { michael@0: return mPackageName; michael@0: } michael@0: michael@0: private Bundle metadata() { michael@0: return mInfo.metaData; michael@0: } michael@0: michael@0: public String getManifest(Context context) { michael@0: return readResource(context, "manifest"); michael@0: } michael@0: michael@0: public String getMiniManifest(Context context) { michael@0: return readResource(context, "mini"); michael@0: } michael@0: michael@0: public String getManifestUrl() { michael@0: return metadata().getString("manifestUrl"); michael@0: } michael@0: michael@0: public boolean isPackaged() { michael@0: return "packaged".equals(getWebappType()); michael@0: } michael@0: michael@0: public boolean isDebuggable() { michael@0: return (mInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; michael@0: } michael@0: michael@0: private String readResource(Context context, String resourceName) { michael@0: Uri resourceUri = Uri.parse("android.resource://" + mPackageName michael@0: + "/raw/" + resourceName); michael@0: StringBuilder fileContent = new StringBuilder(); michael@0: try { michael@0: BufferedReader r = new BufferedReader(new InputStreamReader(context michael@0: .getContentResolver().openInputStream(resourceUri))); michael@0: String line; michael@0: michael@0: while ((line = r.readLine()) != null) { michael@0: fileContent.append(line); michael@0: } michael@0: } catch (FileNotFoundException e) { michael@0: Log.e(LOGTAG, String.format("File not found: \"%s\"", resourceName)); michael@0: } catch (IOException e) { michael@0: Log.e(LOGTAG, String.format("Couldn't read file: \"%s\"", resourceName)); michael@0: } michael@0: michael@0: return fileContent.toString(); michael@0: } michael@0: michael@0: public Uri getAppIconUri() { michael@0: return Uri.parse("android.resource://" + mPackageName + "/" + info().icon); michael@0: } michael@0: michael@0: public Drawable getAppIcon() { michael@0: return info().loadIcon(mContext.getPackageManager()); michael@0: } michael@0: michael@0: public String getWebappType() { michael@0: return metadata().getString("webapp"); michael@0: } michael@0: michael@0: public String getAppName() { michael@0: return info().name; michael@0: } michael@0: michael@0: /** michael@0: * Which APK installer installed this APK. michael@0: * michael@0: * For OEM backed marketplaces, this will be non-null. Otherwise, null. michael@0: * michael@0: * TODO check that the G+ package installer gives us non-null results. michael@0: * michael@0: * @return the package name of the APK that installed this. michael@0: */ michael@0: public String getPackageInstallerName() { michael@0: return mContext.getPackageManager().getInstallerPackageName(mPackageName); michael@0: } michael@0: michael@0: public Uri getZipFileUri() { michael@0: return Uri.parse("android.resource://" + mPackageName + "/raw/application"); michael@0: } michael@0: michael@0: public File getFileDirectory() { michael@0: File dir = mContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS); michael@0: String path = dir.getAbsolutePath().replace(mContext.getPackageName(), mPackageName); michael@0: michael@0: dir = new File(path); michael@0: michael@0: if (!dir.exists()) { michael@0: dir.mkdirs(); michael@0: } michael@0: michael@0: return dir; michael@0: } michael@0: }