2012-03-22 11:11:49 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2012-08-01 18:58:23 -07:00
|
|
|
package org.mozilla.gecko.util;
|
2012-03-22 11:11:49 -07:00
|
|
|
|
2013-03-08 10:20:13 -08:00
|
|
|
import org.mozilla.gecko.mozglue.NativeZip;
|
|
|
|
|
2012-07-27 09:52:57 -07:00
|
|
|
import android.content.res.Resources;
|
2013-03-05 04:08:43 -08:00
|
|
|
import android.graphics.Bitmap;
|
2012-07-27 17:53:54 -07:00
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
|
|
|
import android.util.Log;
|
2013-11-18 21:09:00 -08:00
|
|
|
import org.mozilla.gecko.mozglue.RobocopTarget;
|
2012-07-27 17:53:54 -07:00
|
|
|
|
2012-09-10 10:51:18 -07:00
|
|
|
import java.io.BufferedReader;
|
2012-07-27 17:53:54 -07:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2012-09-10 10:51:18 -07:00
|
|
|
import java.io.InputStreamReader;
|
2012-03-22 11:11:49 -07:00
|
|
|
import java.net.URL;
|
|
|
|
import java.util.Stack;
|
|
|
|
|
|
|
|
/* Reads out of a multiple level deep jar file such as
|
|
|
|
* jar:jar:file:///data/app/org.mozilla.fennec.apk!/omni.ja!/chrome/chrome/content/branding/favicon32.png
|
|
|
|
*/
|
2012-08-01 18:58:23 -07:00
|
|
|
public final class GeckoJarReader {
|
2013-03-05 04:08:43 -08:00
|
|
|
private static final String LOGTAG = "GeckoJarReader";
|
2012-03-22 11:11:49 -07:00
|
|
|
|
2012-08-01 18:58:23 -07:00
|
|
|
private GeckoJarReader() {}
|
|
|
|
|
2013-03-05 04:08:43 -08:00
|
|
|
public static Bitmap getBitmap(Resources resources, String url) {
|
|
|
|
BitmapDrawable drawable = getBitmapDrawable(resources, url);
|
|
|
|
return (drawable != null) ? drawable.getBitmap() : null;
|
|
|
|
}
|
|
|
|
|
2012-07-27 09:52:57 -07:00
|
|
|
public static BitmapDrawable getBitmapDrawable(Resources resources, String url) {
|
2012-03-22 11:11:49 -07:00
|
|
|
Stack<String> jarUrls = parseUrl(url);
|
2012-03-23 17:07:10 -07:00
|
|
|
InputStream inputStream = null;
|
|
|
|
BitmapDrawable bitmap = null;
|
2012-03-22 11:11:49 -07:00
|
|
|
|
2013-03-08 10:20:13 -08:00
|
|
|
NativeZip zip = null;
|
2012-03-22 11:11:49 -07:00
|
|
|
try {
|
|
|
|
// Load the initial jar file as a zip
|
2012-03-23 17:07:10 -07:00
|
|
|
zip = getZipFile(jarUrls.pop());
|
2013-05-25 12:03:42 -07:00
|
|
|
inputStream = getStream(zip, jarUrls, url);
|
2012-03-23 17:07:10 -07:00
|
|
|
if (inputStream != null) {
|
2012-07-27 09:52:57 -07:00
|
|
|
bitmap = new BitmapDrawable(resources, inputStream);
|
2012-03-23 17:07:10 -07:00
|
|
|
}
|
|
|
|
} catch (IOException ex) {
|
|
|
|
Log.e(LOGTAG, "Exception ", ex);
|
|
|
|
} finally {
|
|
|
|
if (inputStream != null) {
|
|
|
|
try {
|
|
|
|
inputStream.close();
|
|
|
|
} catch(IOException ex) {
|
|
|
|
Log.e(LOGTAG, "Error closing stream", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (zip != null) {
|
2013-03-08 10:20:13 -08:00
|
|
|
zip.close();
|
2012-03-23 17:07:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
2012-09-10 10:51:18 -07:00
|
|
|
public static String getText(String url) {
|
|
|
|
Stack<String> jarUrls = parseUrl(url);
|
|
|
|
|
2013-03-08 10:20:13 -08:00
|
|
|
NativeZip zip = null;
|
2012-09-10 10:51:18 -07:00
|
|
|
BufferedReader reader = null;
|
|
|
|
String text = null;
|
|
|
|
try {
|
|
|
|
zip = getZipFile(jarUrls.pop());
|
2013-05-25 12:03:42 -07:00
|
|
|
InputStream input = getStream(zip, jarUrls, url);
|
2012-09-10 10:51:18 -07:00
|
|
|
if (input != null) {
|
|
|
|
reader = new BufferedReader(new InputStreamReader(input));
|
|
|
|
text = reader.readLine();
|
|
|
|
}
|
|
|
|
} catch (IOException ex) {
|
|
|
|
Log.e(LOGTAG, "Exception ", ex);
|
|
|
|
} finally {
|
|
|
|
if (reader != null) {
|
|
|
|
try {
|
|
|
|
reader.close();
|
|
|
|
} catch(IOException ex) {
|
|
|
|
Log.e(LOGTAG, "Error closing reader", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (zip != null) {
|
2013-03-08 10:20:13 -08:00
|
|
|
zip.close();
|
2012-09-10 10:51:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2013-03-08 10:20:13 -08:00
|
|
|
private static NativeZip getZipFile(String url) throws IOException {
|
2012-03-23 17:07:10 -07:00
|
|
|
URL fileUrl = new URL(url);
|
2013-03-08 10:20:13 -08:00
|
|
|
return new NativeZip(fileUrl.getPath());
|
2012-03-23 17:07:10 -07:00
|
|
|
}
|
2012-03-22 11:11:49 -07:00
|
|
|
|
2013-11-18 21:09:00 -08:00
|
|
|
@RobocopTarget
|
2013-06-21 14:00:39 -07:00
|
|
|
public static InputStream getStream(String url) {
|
|
|
|
Stack<String> jarUrls = parseUrl(url);
|
|
|
|
try {
|
|
|
|
NativeZip zip = getZipFile(jarUrls.pop());
|
|
|
|
return getStream(zip, jarUrls, url);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
// Some JNI code throws IllegalArgumentException on a bad file name;
|
|
|
|
// swallow the error and return null. We could also see legitimate
|
|
|
|
// IOExceptions here.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-25 12:03:42 -07:00
|
|
|
private static InputStream getStream(NativeZip zip, Stack<String> jarUrls, String origUrl) {
|
2013-03-08 10:20:13 -08:00
|
|
|
InputStream inputStream = null;
|
2012-03-22 11:11:49 -07:00
|
|
|
|
2013-04-23 16:00:44 -07:00
|
|
|
// loop through children jar files until we reach the innermost one
|
|
|
|
while (!jarUrls.empty()) {
|
|
|
|
String fileName = jarUrls.pop();
|
2012-03-22 11:11:49 -07:00
|
|
|
|
2013-04-23 16:00:44 -07:00
|
|
|
if (inputStream != null) {
|
|
|
|
// intermediate NativeZips and InputStreams will be garbage collected.
|
2013-05-25 12:03:42 -07:00
|
|
|
try {
|
|
|
|
zip = new NativeZip(inputStream);
|
|
|
|
} catch (IllegalArgumentException e) {
|
2013-05-25 12:03:42 -07:00
|
|
|
String description = "!!! BUG 849589 !!! origUrl=" + origUrl;
|
|
|
|
Log.e(LOGTAG, description, e);
|
|
|
|
throw new IllegalArgumentException(description);
|
2013-05-25 12:03:42 -07:00
|
|
|
}
|
2013-04-23 16:00:44 -07:00
|
|
|
}
|
2012-12-13 20:32:25 -08:00
|
|
|
|
2013-04-23 16:00:44 -07:00
|
|
|
inputStream = zip.getInputStream(fileName);
|
|
|
|
if (inputStream == null) {
|
|
|
|
Log.d(LOGTAG, "No Entry for " + fileName);
|
|
|
|
return null;
|
2012-03-22 11:11:49 -07:00
|
|
|
}
|
|
|
|
}
|
2013-04-23 16:00:44 -07:00
|
|
|
|
2012-03-22 11:11:49 -07:00
|
|
|
return inputStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns a stack of strings breaking the url up into pieces. Each piece
|
|
|
|
* is assumed to point to a jar file except for the final one. Callers should
|
|
|
|
* pass in the url to parse, and null for the parent parameter (used for recursion)
|
|
|
|
* For example, jar:jar:file:///data/app/org.mozilla.fennec.apk!/omni.ja!/chrome/chrome/content/branding/favicon32.png
|
|
|
|
* will return:
|
|
|
|
* file:///data/app/org.mozilla.fennec.apk
|
|
|
|
* omni.ja
|
|
|
|
* chrome/chrome/content/branding/favicon32.png
|
|
|
|
*/
|
2012-03-23 17:07:10 -07:00
|
|
|
private static Stack<String> parseUrl(String url) {
|
2012-03-22 11:11:49 -07:00
|
|
|
return parseUrl(url, null);
|
|
|
|
}
|
|
|
|
|
2012-03-23 17:07:10 -07:00
|
|
|
private static Stack<String> parseUrl(String url, Stack<String> results) {
|
2012-03-22 11:11:49 -07:00
|
|
|
if (results == null) {
|
|
|
|
results = new Stack<String>();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url.startsWith("jar:")) {
|
|
|
|
int jarEnd = url.lastIndexOf("!");
|
|
|
|
String subStr = url.substring(4, jarEnd);
|
|
|
|
results.push(url.substring(jarEnd+2)); // remove the !/ characters
|
|
|
|
return parseUrl(subStr, results);
|
|
|
|
} else {
|
|
|
|
results.push(url);
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|