|
| 1 | +#include "apkmetainfo.h" |
| 2 | + |
| 3 | +#include <QDebug> |
| 4 | +#include <QJsonArray> |
| 5 | +#include <QTextCodec> |
| 6 | + |
| 7 | +///////////////////////////////////////////////////////// |
| 8 | +/// run exe |
| 9 | +ApkMetaInfo::ApkMetaInfo(QObject *parent) : QObject(parent) |
| 10 | +{ |
| 11 | + connect(&m_exe, &QProcess::readyReadStandardOutput, this, |
| 12 | + [=]() { m_output.append(m_exe.readAllStandardOutput()); }); |
| 13 | + connect(&m_exe, &QProcess::errorOccurred, this, |
| 14 | + [=]() { emit sigFinished({}); }); |
| 15 | + connect(&m_exe, |
| 16 | + QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, |
| 17 | + [=]() { |
| 18 | + m_output.append(m_exe.readAllStandardOutput()); |
| 19 | + |
| 20 | + if (QTextCodec *codec = QTextCodec::codecForName(m_output)) { |
| 21 | + parse(codec->toUnicode(m_output)); |
| 22 | + } |
| 23 | + else { |
| 24 | + parse(m_output); |
| 25 | + } |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +void ApkMetaInfo::run(const QString &p) |
| 30 | +{ |
| 31 | + m_exe.start("aapt.exe", QStringList{"dump", "badging", p}); |
| 32 | + if (!m_exe.waitForStarted()) { |
| 33 | + qDebug() << m_exe.errorString(); |
| 34 | + m_exe.kill(); |
| 35 | + emit sigFinished({}); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +////////////////////////////////////////////////////////////////////// |
| 40 | +/// parse data |
| 41 | +void ApkMetaInfo::parseLine(const QString &pre, |
| 42 | + const QString &str, |
| 43 | + QJsonObject &obj) |
| 44 | +{ |
| 45 | + // str |
| 46 | + // "package: name='com.github.kr328.clash' versionCode='203022' |
| 47 | + // versionName='2.3.22' compileSdkVersion='30' |
| 48 | + // compileSdkVersionCodename='11'" |
| 49 | + |
| 50 | + const auto parts |
| 51 | + = QString(str).remove(pre + ":").split(" ", Qt::SkipEmptyParts); |
| 52 | + for (const auto &j : parts) { |
| 53 | + auto pair = j.split("="); |
| 54 | + if (pair.size() == 2) { |
| 55 | + obj[pair[0]] = pair[1].remove("'"); |
| 56 | + } |
| 57 | + else { |
| 58 | + qDebug() << "err" << pair << str; |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +void ApkMetaInfo::parseLine(const QString &pre, |
| 64 | + const QString &str, |
| 65 | + QJsonArray &array) |
| 66 | +{ |
| 67 | + const auto parts |
| 68 | + = QString(str).remove(pre + ":").split(" ", Qt::SkipEmptyParts); |
| 69 | + for (const auto &j : parts) { |
| 70 | + auto pair = j.split("="); |
| 71 | + if (pair.size() == 2) { |
| 72 | + array.append(pair[1].remove("'")); |
| 73 | + } |
| 74 | + else { |
| 75 | + qDebug() << "err" << pair << str; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +void ApkMetaInfo::parseLineShort(const QString & /*pre*/, |
| 81 | + const QString &str, |
| 82 | + QJsonObject &obj) |
| 83 | +{ |
| 84 | + // str |
| 85 | + // ""application-icon-160:'r/n_.xml'" |
| 86 | + const auto parts = str.split(":", Qt::SkipEmptyParts); |
| 87 | + if (parts.size() == 2) { |
| 88 | + obj[parts.value(0)] = parts.value(1).remove("'").trimmed(); |
| 89 | + } |
| 90 | + else { |
| 91 | + qDebug() << "err" << parts; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +void ApkMetaInfo::parse(const QString &data) |
| 96 | +{ |
| 97 | + const QString pre_package = "package"; |
| 98 | + const QString pre_sdk_v = "sdkVersion"; |
| 99 | + const QString pre_sdk_t_v = "targetSdkVersion"; |
| 100 | + const QString pre_up = "uses-permission"; |
| 101 | + const QString pre_il = "install-location"; |
| 102 | + const QString pre_al = "application-label"; |
| 103 | + const QString pre_nc = "native-code"; |
| 104 | + |
| 105 | + QJsonObject obj_package; |
| 106 | + QJsonArray obj_up; |
| 107 | + QJsonArray obj_other; |
| 108 | + QString line; |
| 109 | + |
| 110 | + const auto list = data.split('\n', Qt::SkipEmptyParts); |
| 111 | + for (const auto &i : list) { |
| 112 | + line = i.trimmed(); |
| 113 | + |
| 114 | + if (line.startsWith(pre_package)) { |
| 115 | + parseLine(pre_package, line, obj_package); |
| 116 | + } |
| 117 | + else if (line.startsWith(pre_sdk_v)) { |
| 118 | + parseLineShort(pre_sdk_v, line, obj_package); |
| 119 | + } |
| 120 | + else if (line.startsWith(pre_sdk_t_v)) { |
| 121 | + parseLineShort(pre_sdk_t_v, line, obj_package); |
| 122 | + } |
| 123 | + else if (line.startsWith(pre_il)) { |
| 124 | + parseLineShort(pre_il, line, obj_package); |
| 125 | + } |
| 126 | + else if (line.startsWith(pre_al + "-") || line.startsWith(pre_al)) { |
| 127 | + if (line.startsWith(pre_al + "-")) { |
| 128 | + continue; |
| 129 | + } |
| 130 | + parseLineShort(pre_al, line, obj_package); |
| 131 | + } |
| 132 | + else if (line.startsWith(pre_nc)) { |
| 133 | + parseLineShort(pre_nc, line, obj_package); |
| 134 | + } |
| 135 | + else if (line.startsWith(pre_up)) { |
| 136 | + parseLine(pre_up, line, obj_up); |
| 137 | + } |
| 138 | + else { |
| 139 | + obj_other.append(line); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + QJsonObject obj_root; |
| 144 | + obj_root["Package"] = obj_package; |
| 145 | + obj_root["Uses Permission"] = obj_up; |
| 146 | + // should label it any character after "U" |
| 147 | + obj_root["X"] = obj_other; |
| 148 | + emit sigFinished(obj_root); |
| 149 | +} |
0 commit comments