[skip] code style

This commit is contained in:
Connor Tumbleson 2014-09-23 08:24:45 -05:00
parent 49b6bf70e4
commit 7b0ba28a43
2 changed files with 47 additions and 123 deletions

View File

@ -445,11 +445,7 @@ public class Androlib {
apkFile.delete(); apkFile.delete();
} }
return true; return true;
} catch (IOException ex) { } catch (IOException | BrutException ex) {
throw new AndrolibException(ex);
} catch (DirectoryException ex) {
throw new AndrolibException(ex);
} catch (BrutException ex) {
throw new AndrolibException(ex); throw new AndrolibException(ex);
} }
} }
@ -504,9 +500,7 @@ public class Androlib {
} }
return true; return true;
} catch (IOException ex) { } catch (IOException | DirectoryException ex) {
throw new AndrolibException(ex);
} catch (DirectoryException ex) {
throw new AndrolibException(ex); throw new AndrolibException(ex);
} catch (AndrolibException ex) { } catch (AndrolibException ex) {
LOGGER.warning("Parse AndroidManifest.xml failed, treat it as raw file."); LOGGER.warning("Parse AndroidManifest.xml failed, treat it as raw file.");

View File

@ -71,8 +71,7 @@ final public class AndrolibResources {
public ResPackage loadMainPkg(ResTable resTable, ExtFile apkFile) public ResPackage loadMainPkg(ResTable resTable, ExtFile apkFile)
throws AndrolibException { throws AndrolibException {
LOGGER.info("Loading resource table..."); LOGGER.info("Loading resource table...");
ResPackage[] pkgs = getResPackagesFromApk(apkFile, resTable, ResPackage[] pkgs = getResPackagesFromApk(apkFile, resTable, sKeepBroken);
sKeepBroken);
ResPackage pkg = null; ResPackage pkg = null;
// @todo handle multiple packages using findPackageWithMostResSpecs() // @todo handle multiple packages using findPackageWithMostResSpecs()
@ -92,31 +91,27 @@ final public class AndrolibResources {
} }
if (pkg == null) { if (pkg == null) {
throw new AndrolibException( throw new AndrolibException("Arsc files with zero or multiple packages");
"Arsc files with zero or multiple packages");
} }
resTable.addPackage(pkg, true); resTable.addPackage(pkg, true);
return pkg; return pkg;
} }
public ResPackage loadFrameworkPkg(ResTable resTable, int id, public ResPackage loadFrameworkPkg(ResTable resTable, int id, String frameTag)
String frameTag) throws AndrolibException { throws AndrolibException {
File apk = getFrameworkApk(id, frameTag); File apk = getFrameworkApk(id, frameTag);
LOGGER.info("Loading resource table from file: " + apk); LOGGER.info("Loading resource table from file: " + apk);
ResPackage[] pkgs = getResPackagesFromApk(new ExtFile(apk), resTable, ResPackage[] pkgs = getResPackagesFromApk(new ExtFile(apk), resTable, true);
true);
if (pkgs.length != 1) { if (pkgs.length != 1) {
throw new AndrolibException( throw new AndrolibException("Arsc files with zero or multiple packages");
"Arsc files with zero or multiple packages");
} }
ResPackage pkg = pkgs[0]; ResPackage pkg = pkgs[0];
if (pkg.getId() != id) { if (pkg.getId() != id) {
throw new AndrolibException("Expected pkg of id: " throw new AndrolibException("Expected pkg of id: " + String.valueOf(id) + ", got: " + pkg.getId());
+ String.valueOf(id) + ", got: " + pkg.getId());
} }
resTable.addPackage(pkg, false); resTable.addPackage(pkg, false);
@ -142,8 +137,7 @@ final public class AndrolibResources {
out = new FileDirectory(outDir); out = new FileDirectory(outDir);
LOGGER.info("Decoding AndroidManifest.xml with only framework resources..."); LOGGER.info("Decoding AndroidManifest.xml with only framework resources...");
fileDecoder.decodeManifest(inApk, "AndroidManifest.xml", out, fileDecoder.decodeManifest(inApk, "AndroidManifest.xml", out, "AndroidManifest.xml");
"AndroidManifest.xml");
} catch (DirectoryException ex) { } catch (DirectoryException ex) {
throw new AndrolibException(ex); throw new AndrolibException(ex);
@ -244,8 +238,7 @@ final public class AndrolibResources {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filename); return docBuilder.parse(filename);
return doc;
} }
private void saveDocument(String filename, Document doc) private void saveDocument(String filename, Document doc)
@ -266,8 +259,7 @@ final public class AndrolibResources {
ResFileDecoder fileDecoder = duo.m1; ResFileDecoder fileDecoder = duo.m1;
ResAttrDecoder attrDecoder = duo.m2.getAttrDecoder(); ResAttrDecoder attrDecoder = duo.m2.getAttrDecoder();
attrDecoder.setCurrentPackage(resTable.listMainPackages().iterator() attrDecoder.setCurrentPackage(resTable.listMainPackages().iterator().next());
.next());
Directory inApk, in = null, out; Directory inApk, in = null, out;
try { try {
@ -276,8 +268,7 @@ final public class AndrolibResources {
LOGGER.info("Decoding AndroidManifest.xml with resources..."); LOGGER.info("Decoding AndroidManifest.xml with resources...");
fileDecoder.decodeManifest(inApk, "AndroidManifest.xml", out, fileDecoder.decodeManifest(inApk, "AndroidManifest.xml", out, "AndroidManifest.xml");
"AndroidManifest.xml");
// Remove versionName / versionCode (aapt API 16) // Remove versionName / versionCode (aapt API 16)
if (!resTable.getAnalysisMode()) { if (!resTable.getAnalysisMode()) {
@ -347,8 +338,7 @@ final public class AndrolibResources {
} }
} }
public void aaptPackage(File apkFile, File manifest, File resDir, public void aaptPackage(File apkFile, File manifest, File resDir, File rawDir, File assetDir, File[] include,
File rawDir, File assetDir, File[] include,
HashMap<String, Boolean> flags, String aaptPath) HashMap<String, Boolean> flags, String aaptPath)
throws AndrolibException { throws AndrolibException {
@ -356,7 +346,7 @@ final public class AndrolibResources {
List<String> cmd = new ArrayList<String>(); List<String> cmd = new ArrayList<String>();
// path for aapt binary // path for aapt binary
if (!aaptPath.isEmpty()) { if (! aaptPath.isEmpty()) {
File aaptFile = new File(aaptPath); File aaptFile = new File(aaptPath);
if (aaptFile.canRead() && aaptFile.exists()) { if (aaptFile.canRead() && aaptFile.exists()) {
aaptFile.setExecutable(true); aaptFile.setExecutable(true);
@ -477,7 +467,7 @@ final public class AndrolibResources {
public boolean detectWhetherAppIsFramework(File appDir) public boolean detectWhetherAppIsFramework(File appDir)
throws AndrolibException { throws AndrolibException {
File publicXml = new File(appDir, "res/values/public.xml"); File publicXml = new File(appDir, "res/values/public.xml");
if (!publicXml.exists()) { if (! publicXml.exists()) {
return false; return false;
} }
@ -511,11 +501,9 @@ final public class AndrolibResources {
AXmlResourceParser axmlParser = new AXmlResourceParser(); AXmlResourceParser axmlParser = new AXmlResourceParser();
axmlParser.setAttrDecoder(new ResAttrDecoder()); axmlParser.setAttrDecoder(new ResAttrDecoder());
decoders.setDecoder("xml", new XmlPullStreamDecoder(axmlParser, decoders.setDecoder("xml", new XmlPullStreamDecoder(axmlParser, getResXmlSerializer()));
getResXmlSerializer()));
return new Duo<ResFileDecoder, AXmlResourceParser>(new ResFileDecoder( return new Duo<ResFileDecoder, AXmlResourceParser>(new ResFileDecoder(decoders), axmlParser);
decoders), axmlParser);
} }
public Duo<ResFileDecoder, AXmlResourceParser> getManifestFileDecoder() { public Duo<ResFileDecoder, AXmlResourceParser> getManifestFileDecoder() {
@ -523,19 +511,15 @@ final public class AndrolibResources {
AXmlResourceParser axmlParser = new AXmlResourceParser(); AXmlResourceParser axmlParser = new AXmlResourceParser();
decoders.setDecoder("xml", new XmlPullStreamDecoder(axmlParser, decoders.setDecoder("xml", new XmlPullStreamDecoder(axmlParser,getResXmlSerializer()));
getResXmlSerializer()));
return new Duo<ResFileDecoder, AXmlResourceParser>(new ResFileDecoder( return new Duo<ResFileDecoder, AXmlResourceParser>(new ResFileDecoder(decoders), axmlParser);
decoders), axmlParser);
} }
public ExtMXSerializer getResXmlSerializer() { public ExtMXSerializer getResXmlSerializer() {
ExtMXSerializer serial = new ExtMXSerializer(); ExtMXSerializer serial = new ExtMXSerializer();
serial.setProperty(ExtXmlSerializer.PROPERTY_SERIALIZER_INDENTATION, serial.setProperty(ExtXmlSerializer.PROPERTY_SERIALIZER_INDENTATION, " ");
" "); serial.setProperty(ExtXmlSerializer.PROPERTY_SERIALIZER_LINE_SEPARATOR, System.getProperty("line.separator"));
serial.setProperty(ExtXmlSerializer.PROPERTY_SERIALIZER_LINE_SEPARATOR,
System.getProperty("line.separator"));
serial.setProperty(ExtXmlSerializer.PROPERTY_DEFAULT_ENCODING, "utf-8"); serial.setProperty(ExtXmlSerializer.PROPERTY_DEFAULT_ENCODING, "utf-8");
serial.setDisabledAttrEscape(true); serial.setDisabledAttrEscape(true);
return serial; return serial;
@ -553,8 +537,7 @@ final public class AndrolibResources {
if (valuesFile.isSynthesized(res)) { if (valuesFile.isSynthesized(res)) {
continue; continue;
} }
((ResValuesXmlSerializable) res.getValue()) ((ResValuesXmlSerializable) res.getValue()).serializeToResValuesXml(serial, res);
.serializeToResValuesXml(serial, res);
} }
serial.endTag(null, "resources"); serial.endTag(null, "resources");
@ -562,12 +545,8 @@ final public class AndrolibResources {
serial.endDocument(); serial.endDocument();
serial.flush(); serial.flush();
outStream.close(); outStream.close();
} catch (IOException ex) { } catch (IOException | DirectoryException ex) {
throw new AndrolibException("Could not generate: " throw new AndrolibException("Could not generate: " + valuesFile.getPath(), ex);
+ valuesFile.getPath(), ex);
} catch (DirectoryException ex) {
throw new AndrolibException("Could not generate: "
+ valuesFile.getPath(), ex);
} }
} }
@ -583,8 +562,7 @@ final public class AndrolibResources {
serial.startTag(null, "public"); serial.startTag(null, "public");
serial.attribute(null, "type", spec.getType().getName()); serial.attribute(null, "type", spec.getType().getName());
serial.attribute(null, "name", spec.getName()); serial.attribute(null, "name", spec.getName());
serial.attribute(null, "id", serial.attribute(null, "id", String.format("0x%08x", spec.getId().id));
String.format("0x%08x", spec.getId().id));
serial.endTag(null, "public"); serial.endTag(null, "public");
} }
@ -592,24 +570,18 @@ final public class AndrolibResources {
serial.endDocument(); serial.endDocument();
serial.flush(); serial.flush();
outStream.close(); outStream.close();
} catch (IOException ex) { } catch (IOException | DirectoryException ex) {
throw new AndrolibException("Could not generate public.xml file", throw new AndrolibException("Could not generate public.xml file", ex);
ex);
} catch (DirectoryException ex) {
throw new AndrolibException("Could not generate public.xml file",
ex);
} }
} }
private ResPackage[] getResPackagesFromApk(ExtFile apkFile, private ResPackage[] getResPackagesFromApk(ExtFile apkFile,ResTable resTable, boolean keepBroken)
ResTable resTable, boolean keepBroken) throws AndrolibException { throws AndrolibException {
try { try {
return ARSCDecoder.decode( return ARSCDecoder.decode(apkFile.getDirectory().getFileInput("resources.arsc"),false,
apkFile.getDirectory().getFileInput("resources.arsc"), keepBroken, resTable).getPackages();
false, keepBroken, resTable).getPackages();
} catch (DirectoryException ex) { } catch (DirectoryException ex) {
throw new AndrolibException( throw new AndrolibException("Could not load resources.arsc from file: " + apkFile, ex);
"Could not load resources.arsc from file: " + apkFile, ex);
} }
} }
@ -631,29 +603,12 @@ final public class AndrolibResources {
} }
if (id == 1) { if (id == 1) {
InputStream in = null; try (InputStream in = AndrolibResources.class.getResourceAsStream("/brut/androlib/android-framework.jar");
OutputStream out = null; OutputStream out = new FileOutputStream(apk)) {
try {
in = AndrolibResources.class
.getResourceAsStream("/brut/androlib/android-framework.jar");
out = new FileOutputStream(apk);
IOUtils.copy(in, out); IOUtils.copy(in, out);
return apk; return apk;
} catch (IOException ex) { } catch (IOException ex) {
throw new AndrolibException(ex); throw new AndrolibException(ex);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException ex) {
}
}
} }
} }
@ -675,8 +630,7 @@ final public class AndrolibResources {
in = zip.getInputStream(entry); in = zip.getInputStream(entry);
byte[] data = IOUtils.toByteArray(in); byte[] data = IOUtils.toByteArray(in);
ARSCData arsc = ARSCDecoder.decode(new ByteArrayInputStream(data), ARSCData arsc = ARSCDecoder.decode(new ByteArrayInputStream(data), true, true);
true, true);
publicizeResources(data, arsc.getFlagsOffsets()); publicizeResources(data, arsc.getFlagsOffsets());
File outFile = new File(getFrameworkDir(), String.valueOf(arsc File outFile = new File(getFrameworkDir(), String.valueOf(arsc
@ -720,38 +674,18 @@ final public class AndrolibResources {
public void publicizeResources(File arscFile) throws AndrolibException { public void publicizeResources(File arscFile) throws AndrolibException {
byte[] data = new byte[(int) arscFile.length()]; byte[] data = new byte[(int) arscFile.length()];
InputStream in = null; try(InputStream in = new FileInputStream(arscFile);
OutputStream out = null; OutputStream out = new FileOutputStream(arscFile)) {
try {
in = new FileInputStream(arscFile);
in.read(data); in.read(data);
publicizeResources(data); publicizeResources(data);
out = new FileOutputStream(arscFile);
out.write(data); out.write(data);
} catch (IOException ex) { } catch (IOException ex){
throw new AndrolibException(ex); throw new AndrolibException(ex);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException ex) {
}
}
} }
} }
public void publicizeResources(byte[] arsc) throws AndrolibException { public void publicizeResources(byte[] arsc) throws AndrolibException {
publicizeResources(arsc, publicizeResources(arsc, ARSCDecoder.decode(new ByteArrayInputStream(arsc), true, true).getFlagsOffsets());
ARSCDecoder.decode(new ByteArrayInputStream(arsc), true, true)
.getFlagsOffsets());
} }
public void publicizeResources(byte[] arsc, FlagsOffset[] flagsOffsets) public void publicizeResources(byte[] arsc, FlagsOffset[] flagsOffsets)
@ -773,7 +707,8 @@ final public class AndrolibResources {
if (sFrameworkFolder != null) { if (sFrameworkFolder != null) {
path = sFrameworkFolder; path = sFrameworkFolder;
} else if (OSDetection.isMacOSX()) { } else if (OSDetection.isMacOSX()) {
path = System.getProperty("user.home") + File.separatorChar + "Library" + File.separatorChar + "apktool" + File.separatorChar + "framework"; path = System.getProperty("user.home") + File.separatorChar + "Library" + File.separatorChar +
"apktool" + File.separatorChar + "framework";
} else { } else {
path = System.getProperty("user.home") + File.separatorChar + "apktool" + File.separatorChar + "framework"; path = System.getProperty("user.home") + File.separatorChar + "apktool" + File.separatorChar + "framework";
} }
@ -782,8 +717,7 @@ final public class AndrolibResources {
if (!dir.exists()) { if (!dir.exists()) {
if (!dir.mkdirs()) { if (!dir.mkdirs()) {
if (sFrameworkFolder != null) { if (sFrameworkFolder != null) {
System.err.println("Can't create Framework directory: " System.err.println("Can't create Framework directory: " + dir);
+ dir);
} }
throw new AndrolibException("Can't create directory: " + dir); throw new AndrolibException("Can't create directory: " + dir);
} }
@ -803,14 +737,11 @@ final public class AndrolibResources {
public File getAaptBinaryFile() throws AndrolibException { public File getAaptBinaryFile() throws AndrolibException {
try { try {
if (OSDetection.isMacOSX()) { if (OSDetection.isMacOSX()) {
mAaptBinary = Jar mAaptBinary = Jar.getResourceAsFile("/prebuilt/aapt/macosx/aapt");
.getResourceAsFile("/prebuilt/aapt/macosx/aapt");
} else if (OSDetection.isUnix()) { } else if (OSDetection.isUnix()) {
mAaptBinary = Jar mAaptBinary = Jar.getResourceAsFile("/prebuilt/aapt/linux/aapt");
.getResourceAsFile("/prebuilt/aapt/linux/aapt");
} else if (OSDetection.isWindows()) { } else if (OSDetection.isWindows()) {
mAaptBinary = Jar mAaptBinary = Jar.getResourceAsFile("/prebuilt/aapt/windows/aapt.exe");
.getResourceAsFile("/prebuilt/aapt/windows/aapt.exe");
} else { } else {
LOGGER.warning("Unknown Operating System: " + OSDetection.returnOS()); LOGGER.warning("Unknown Operating System: " + OSDetection.returnOS());
return null; return null;
@ -828,8 +759,7 @@ final public class AndrolibResources {
public File getAndroidResourcesFile() throws AndrolibException { public File getAndroidResourcesFile() throws AndrolibException {
try { try {
return Jar return Jar.getResourceAsFile("/brut/androlib/android-framework.jar");
.getResourceAsFile("/brut/androlib/android-framework.jar");
} catch (BrutException ex) { } catch (BrutException ex) {
throw new AndrolibException(ex); throw new AndrolibException(ex);
} }