|
| 1 | +package com.elderdrivers.riru.edxp.util; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.File; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.jar.JarFile; |
| 10 | + |
| 11 | +import pxb.android.axml.AxmlReader; |
| 12 | +import pxb.android.axml.AxmlVisitor; |
| 13 | +import pxb.android.axml.NodeVisitor; |
| 14 | + |
| 15 | +public class MetaDataReader { |
| 16 | + private final HashMap<String, Object> metaData = new HashMap<>(); |
| 17 | + |
| 18 | + public static Map<String, Object> getMetaData(File apk) throws IOException { |
| 19 | + return new MetaDataReader(apk).metaData; |
| 20 | + } |
| 21 | + |
| 22 | + private MetaDataReader(File apk) throws IOException { |
| 23 | + JarFile zip = new JarFile(apk); |
| 24 | + InputStream is = zip.getInputStream(zip.getEntry("AndroidManifest.xml")); |
| 25 | + byte[] bytes = getBytesFromInputStream(is); |
| 26 | + AxmlReader reader = new AxmlReader(bytes); |
| 27 | + reader.accept(new AxmlVisitor() { |
| 28 | + @Override |
| 29 | + public NodeVisitor child(String ns, String name) { |
| 30 | + NodeVisitor child = super.child(ns, name); |
| 31 | + return new ManifestTagVisitor(child); |
| 32 | + } |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + public static byte[] getBytesFromInputStream(InputStream inputStream) throws IOException { |
| 37 | + try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { |
| 38 | + byte[] b = new byte[1024]; |
| 39 | + int n; |
| 40 | + while ((n = inputStream.read(b)) != -1) { |
| 41 | + bos.write(b, 0, n); |
| 42 | + } |
| 43 | + byte[] data = bos.toByteArray(); |
| 44 | + return data; |
| 45 | + } catch (Exception e) { |
| 46 | + e.printStackTrace(); |
| 47 | + } |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + private class ManifestTagVisitor extends NodeVisitor { |
| 52 | + public ManifestTagVisitor(NodeVisitor child) { |
| 53 | + super(child); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public NodeVisitor child(String ns, String name) { |
| 58 | + NodeVisitor child = super.child(ns, name); |
| 59 | + if ("application".equals(name)) { |
| 60 | + return new ApplicationTagVisitor(child); |
| 61 | + } |
| 62 | + return child; |
| 63 | + } |
| 64 | + |
| 65 | + private class ApplicationTagVisitor extends NodeVisitor { |
| 66 | + public ApplicationTagVisitor(NodeVisitor child) { |
| 67 | + super(child); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public NodeVisitor child(String ns, String name) { |
| 72 | + NodeVisitor child = super.child(ns, name); |
| 73 | + if("meta-data".equals(name)) { |
| 74 | + return new MetaDataVisitor(child); |
| 75 | + } |
| 76 | + return child; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private class MetaDataVisitor extends NodeVisitor { |
| 82 | + public String name = null; |
| 83 | + public Object value = null; |
| 84 | + public MetaDataVisitor(NodeVisitor child) { |
| 85 | + super(child); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void attr(String ns, String name, int resourceId, int type, Object obj) { |
| 90 | + if (type == 3 && "name".equals(name)) { |
| 91 | + this.name = (String)obj; |
| 92 | + } |
| 93 | + if ("value".equals(name) ) { |
| 94 | + value = obj; |
| 95 | + } |
| 96 | + super.attr(ns, name, resourceId, type, obj); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void end() { |
| 101 | + if(name != null && value != null) { |
| 102 | + metaData.put(name, value); |
| 103 | + } |
| 104 | + super.end(); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments