Search in sources :

Example 21 with AndrolibException

use of brut.androlib.AndrolibException in project Apktool by iBotPeaches.

the class Res9patchStreamDecoder method decode.

@Override
public void decode(InputStream in, OutputStream out) throws AndrolibException {
    try {
        byte[] data = IOUtils.toByteArray(in);
        BufferedImage im = ImageIO.read(new ByteArrayInputStream(data));
        int w = im.getWidth(), h = im.getHeight();
        BufferedImage im2 = new BufferedImage(w + 2, h + 2, BufferedImage.TYPE_INT_ARGB);
        im2.createGraphics().drawImage(im, 1, 1, w, h, null);
        NinePatch np = getNinePatch(data);
        drawHLine(im2, h + 1, np.padLeft + 1, w - np.padRight);
        drawVLine(im2, w + 1, np.padTop + 1, h - np.padBottom);
        int[] xDivs = np.xDivs;
        for (int i = 0; i < xDivs.length; i += 2) {
            drawHLine(im2, 0, xDivs[i] + 1, xDivs[i + 1]);
        }
        int[] yDivs = np.yDivs;
        for (int i = 0; i < yDivs.length; i += 2) {
            drawVLine(im2, 0, yDivs[i] + 1, yDivs[i + 1]);
        }
        ImageIO.write(im2, "png", out);
    } catch (IOException ex) {
        throw new AndrolibException(ex);
    } catch (NullPointerException ex) {
        // This could be more verbose and try to MIME ?
        throw new AndrolibException(ex);
    }
}
Also used : AndrolibException(brut.androlib.AndrolibException) BufferedImage(java.awt.image.BufferedImage)

Example 22 with AndrolibException

use of brut.androlib.AndrolibException in project Apktool by iBotPeaches.

the class XmlPullStreamDecoder method decode.

@Override
public void decode(InputStream in, OutputStream out) throws AndrolibException {
    try {
        XmlPullWrapperFactory factory = XmlPullWrapperFactory.newInstance();
        XmlPullParserWrapper par = factory.newPullParserWrapper(mParser);
        final ResTable resTable = ((AXmlResourceParser) mParser).getAttrDecoder().getCurrentPackage().getResTable();
        XmlSerializerWrapper ser = new StaticXmlSerializerWrapper(mSerial, factory) {

            boolean hideSdkInfo = false;

            boolean hidePackageInfo = false;

            @Override
            public void event(XmlPullParser pp) throws XmlPullParserException, IOException {
                int type = pp.getEventType();
                if (type == XmlPullParser.START_TAG) {
                    if ("manifest".equalsIgnoreCase(pp.getName())) {
                        try {
                            hidePackageInfo = parseManifest(pp);
                        } catch (AndrolibException ignored) {
                        }
                    } else if ("uses-sdk".equalsIgnoreCase(pp.getName())) {
                        try {
                            hideSdkInfo = parseAttr(pp);
                            if (hideSdkInfo) {
                                return;
                            }
                        } catch (AndrolibException ignored) {
                        }
                    }
                } else if (hideSdkInfo && type == XmlPullParser.END_TAG && "uses-sdk".equalsIgnoreCase(pp.getName())) {
                    return;
                } else if (hidePackageInfo && type == XmlPullParser.END_TAG && "manifest".equalsIgnoreCase(pp.getName())) {
                    super.event(pp);
                    return;
                }
                super.event(pp);
            }

            private boolean parseManifest(XmlPullParser pp) throws AndrolibException {
                String attr_name;
                // read <manifest> for package:
                for (int i = 0; i < pp.getAttributeCount(); i++) {
                    attr_name = pp.getAttributeName(i);
                    if (attr_name.equalsIgnoreCase(("package"))) {
                        resTable.setPackageRenamed(pp.getAttributeValue(i));
                    } else if (attr_name.equalsIgnoreCase("versionCode")) {
                        resTable.setVersionCode(pp.getAttributeValue(i));
                    } else if (attr_name.equalsIgnoreCase("versionName")) {
                        resTable.setVersionName(pp.getAttributeValue(i));
                    }
                }
                return true;
            }

            private boolean parseAttr(XmlPullParser pp) throws AndrolibException {
                for (int i = 0; i < pp.getAttributeCount(); i++) {
                    final String a_ns = "http://schemas.android.com/apk/res/android";
                    String ns = pp.getAttributeNamespace(i);
                    if (a_ns.equalsIgnoreCase(ns)) {
                        String name = pp.getAttributeName(i);
                        String value = pp.getAttributeValue(i);
                        if (name != null && value != null) {
                            if (name.equalsIgnoreCase("minSdkVersion") || name.equalsIgnoreCase("targetSdkVersion") || name.equalsIgnoreCase("maxSdkVersion")) {
                                resTable.addSdkInfo(name, value);
                            } else {
                                resTable.clearSdkInfo();
                                // Found unknown flags
                                return false;
                            }
                        }
                    } else {
                        resTable.clearSdkInfo();
                        if (i >= pp.getAttributeCount()) {
                            // Found unknown flags
                            return false;
                        }
                    }
                }
                return !resTable.getAnalysisMode();
            }
        };
        par.setInput(in, null);
        ser.setOutput(out, null);
        while (par.nextToken() != XmlPullParser.END_DOCUMENT) {
            ser.event(par);
        }
        ser.flush();
    } catch (XmlPullParserException ex) {
        throw new AndrolibException("Could not decode XML", ex);
    } catch (IOException ex) {
        throw new AndrolibException("Could not decode XML", ex);
    }
}
Also used : XmlPullWrapperFactory(org.xmlpull.v1.wrapper.XmlPullWrapperFactory) XmlPullParserWrapper(org.xmlpull.v1.wrapper.XmlPullParserWrapper) StaticXmlSerializerWrapper(org.xmlpull.v1.wrapper.classic.StaticXmlSerializerWrapper) XmlPullParser(org.xmlpull.v1.XmlPullParser) AndrolibException(brut.androlib.AndrolibException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) XmlSerializerWrapper(org.xmlpull.v1.wrapper.XmlSerializerWrapper) StaticXmlSerializerWrapper(org.xmlpull.v1.wrapper.classic.StaticXmlSerializerWrapper) IOException(java.io.IOException) ResTable(brut.androlib.res.data.ResTable)

Example 23 with AndrolibException

use of brut.androlib.AndrolibException in project Apktool by iBotPeaches.

the class SmaliBuilder method build.

private void build() throws AndrolibException {
    try {
        DexBuilder dexBuilder;
        if (mApiLevel > 0) {
            dexBuilder = DexBuilder.makeDexBuilder(Opcodes.forApi(mApiLevel));
        } else {
            dexBuilder = DexBuilder.makeDexBuilder();
        }
        for (String fileName : mSmaliDir.getDirectory().getFiles(true)) {
            buildFile(fileName, dexBuilder);
        }
        dexBuilder.writeTo(new FileDataStore(new File(mDexFile.getAbsolutePath())));
    } catch (IOException | DirectoryException ex) {
        throw new AndrolibException(ex);
    }
}
Also used : AndrolibException(brut.androlib.AndrolibException) FileDataStore(org.jf.dexlib2.writer.io.FileDataStore) ExtFile(brut.directory.ExtFile) DirectoryException(brut.directory.DirectoryException) DexBuilder(org.jf.dexlib2.writer.builder.DexBuilder)

Aggregations

AndrolibException (brut.androlib.AndrolibException)23 ExtFile (brut.directory.ExtFile)11 ZipFile (java.util.zip.ZipFile)9 DirectoryException (brut.directory.DirectoryException)7 ZipOutputStream (java.util.zip.ZipOutputStream)5 Directory (brut.directory.Directory)4 FileDirectory (brut.directory.FileDirectory)4 IOException (java.io.IOException)3 BrutException (brut.common.BrutException)2 CantFind9PatchChunk (brut.androlib.err.CantFind9PatchChunk)1 CantFindFrameworkResException (brut.androlib.err.CantFindFrameworkResException)1 ResTable (brut.androlib.res.data.ResTable)1 ResBoolValue (brut.androlib.res.data.value.ResBoolValue)1 ResFileValue (brut.androlib.res.data.value.ResFileValue)1 ARSCData (brut.androlib.res.decoder.ARSCDecoder.ARSCData)1 ExtMXSerializer (brut.androlib.res.util.ExtMXSerializer)1 ResValuesXmlSerializable (brut.androlib.res.xml.ResValuesXmlSerializable)1 BufferedImage (java.awt.image.BufferedImage)1 PrintWriter (java.io.PrintWriter)1 BigInteger (java.math.BigInteger)1