package net.erdfelt.android.apk;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

import net.erdfelt.android.apk.io.IO;
import net.erdfelt.android.apk.xml.Attribute;
import net.erdfelt.android.apk.xml.BinaryXmlListener;
import net.erdfelt.android.apk.xml.BinaryXmlParser;

public class AndroidApk {
    private String appVersion;
    private String appVersionCode;
    private String packageName;

    private class ManifestListener implements BinaryXmlListener {
        public void onXmlEntry(String path, String name, Attribute... attrs) {
            if ("//".equals(path) && "manifest".equals(name)) {
                for (Attribute attrib : attrs) {
                    if ("package".equals(attrib.getName())) {
                        packageName = attrib.getValue();
                    } else if ("versionName".equals(attrib.getName())) {
                        appVersion = attrib.getValue();
                    } else if ("versionCode".equals(attrib.getName())) {
                        appVersionCode = attrib.getValue();
                    }
                }
            }
        }
    }

    public AndroidApk(File apkfile) throws ZipException, IOException {
        ZipFile zip = new ZipFile(apkfile);
        ZipEntry manifestEntry = zip.getEntry("AndroidManifest.xml");

        InputStream in = null;
        try {
            in = zip.getInputStream(manifestEntry);
            BinaryXmlParser parser = new BinaryXmlParser();
            // parser.addListener(new BinaryXmlDump());
            parser.addListener(new ManifestListener());
            parser.parse(in);
        } finally {
            IO.close(in);
            try {
                if(zip != null) {
                    zip.close();
                }
            } catch(IOException ignore) {
                /* ignore */
            }
        }
    }

    public String getAppVersion() {
        return appVersion;
    }

    public String getAppVersionCode() {
        return appVersionCode;
    }

    public String getPackageName() {
        return packageName;
    }
}

@@ -516,17 +516,123 @@
 geName;%0A
+    private String minSdkVersion;%0A    private String targetSdkVersion;%0A    private String maxSdkVersion;%0A%09
 %0A
-
     priv
@@ -1308,32 +1308,448 @@
 %7D%0A            %7D%0A
+%09%09%09%0A%09%09%09if (%22uses-sdk%22.equals(name)) %7B%0A%09%09%09%09for (Attribute attrib : attrs) %7B%0A%09%09%09%09%09if (%22minSdkVersion%22.equals(attrib.getName())) %7B%0A%09%09%09%09%09%09minSdkVersion = attrib.getValue();%0A%09%09%09%09%09%7D%0A%09%09%09%09%09%0A%09%09%09%09%09if (%22targetSdkVersion%22.equals(attrib.getName())) %7B%0A%09%09%09%09%09%09targetSdkVersion = attrib.getValue();%0A%09%09%09%09%09%7D%0A%09%09%09%09%09%0A%09%09%09%09%09if (%22maxSdkVersion%22.equals(attrib.getName())) %7B%0A%09%09%09%09%09%09maxSdkVersion = attrib.getValue();%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A            %7D%0A
         %7D%0A    %7D%0A
@@ -2698,16 +2698,219 @@
 geName;%0A
-
     %7D%0A
+%09%0A%09public String getMinSdkVersion() %7B%0A%09%09return minSdkVersion;%0A%09%7D%0A%09%0A%09public String getTargetSdkVersion() %7B%0A%09%09return targetSdkVersion;%0A%09%7D%0A%09%0A%09public String getMaxSdkVersion() %7B%0A%09%09return maxSdkVersion;%0A%09%7D%0A%09%0A
 %7D%0A

version related methods added