Skip to content

Commit

Permalink
Support more Android package info
Browse files Browse the repository at this point in the history
  • Loading branch information
canheo136 committed Aug 5, 2020
1 parent 7ff5b66 commit ea2c925
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 68 deletions.
15 changes: 5 additions & 10 deletions AAPTForNet/ApkExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public static ApkInfo ExtractIcon(DumpModel model, ApkInfo apk) {

using(var archive = ZipFile.OpenRead(model.FilePath)) {
ZipArchiveEntry entry;
int max = archive.Entries.Count - 1;
int i = archive.Entries.Count - 1;

// Loop from bottom of the collection.
// The largest icon is usually position at the end of package
// (sorting by alphabet)
for(int i = max; i > 0; i--) {
for(; i > 0; i--) {
entry = archive.Entries[i];

if(entry.Name.Equals(apk.IconName)) {
Expand All @@ -34,14 +34,9 @@ public static ApkInfo ExtractIcon(DumpModel model, ApkInfo apk) {
}
}

return new ApkInfo(
model.FilePath,
apk.AppName,
apk.PackageName,
apk.Version,
apk.MinSDK,
hasIcon ? iconName : string.Empty
);
apk.IconName = hasIcon ? iconName : string.Empty;

return apk;
}
}
}
86 changes: 66 additions & 20 deletions AAPTForNet/ApkParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,91 @@

namespace AAPTForNet {
/// <summary>
/// Parse all messages from AAPTool
/// Parse output messages from AAPTool
/// </summary>
internal class ApkParser {

private static readonly string SDKPattern = @"sdkVersion:'[0-9]{0,1}[^']";
private static readonly string IconPattern = @"icon='[\w-.\\\/]*[^']";
private static readonly string AppNamePattern = @"label='[\w-.() \\\/\[\]]*[^']";
private static readonly string PackagePattern = @"name='[\w.]*[^']";
private static readonly string VersionPattern = @"versionName='[\w-.() \/]*[^']";
protected ApkParser() { }

public static ApkInfo Parse(DumpModel model) {
if(!model.isSuccess)
return new ApkInfo("", string.Join("\n", model.Messages), "", "", SDKInfo.Unknown, "");
if (!model.isSuccess)
return ApkInfo.Empty;

// Extract info from apk
string name = "", package = "", ver = "", sdk = "", icon = "";
var permissions = new System.Collections.Generic.List<string>();
var supportScrs = new System.Collections.Generic.List<string>();

string ver = string.Empty,
minSDK = string.Empty,
targetSDK = string.Empty,
icon = string.Empty,
name = string.Empty,
per = string.Empty,
package = string.Empty;


foreach(string msg in model.Messages) {
if(msg.StartsWith("application:")) {
icon = splitValueFromString(msg, IconPattern);
name = splitValueFromString(msg, AppNamePattern);
icon = splitValueFromString(msg, @"icon='[\w-.\\\/]*[^']");
name = splitValueFromString(msg, @"label='[\w-.() \\\/\[\]]*[^']");
continue;
}

if (msg.StartsWith("package:")) {
ver = splitValueFromString(msg, @"versionName='[\w-.() \/]*[^']");
package = splitValueFromString(msg, @"name='[\w.]*[^']");
continue;
}
else if(msg.StartsWith("package:")) {
ver = splitValueFromString(msg, VersionPattern);
package = splitValueFromString(msg, PackagePattern);

if (msg.StartsWith("sdkVersion:")) {
minSDK = splitValueFromString(msg, @"sdkVersion:'[0-9]{0,1}[^']");
continue;
}
else if(msg.StartsWith("sdkVersion:")) {
sdk = splitValueFromString(msg, SDKPattern);

if (msg.StartsWith("targetSdkVersion:")) {
targetSDK = splitValueFromString(msg, @"targetSdkVersion:'[0-9]{0,1}[^']");
continue;
}

if (msg.StartsWith("uses-permission:")) {
per = splitValueFromString(msg, @"'([A-Z0-9._])*");
permissions.Add(per);
continue;
}

if (msg.StartsWith("supports-screens:")) {
if (msg.Contains(ApkInfo.SmallScreen)) {
supportScrs.Add(ApkInfo.SmallScreen);
}
if (msg.Contains(ApkInfo.NormalScreen)) {
supportScrs.Add(ApkInfo.NormalScreen);
}
if (msg.Contains(ApkInfo.LargeScreen)) {
supportScrs.Add(ApkInfo.LargeScreen);
}
if (msg.Contains(ApkInfo.xLargeScreen)) {
supportScrs.Add(ApkInfo.xLargeScreen);
}
continue;
}
else continue;
}

return new ApkInfo(model.FilePath, name, package, ver, SDKInfo.GetInfo(sdk), icon);
return new ApkInfo() {
FullPath = model.FilePath,
AppName = name,
PackageName = package,
Version = ver,
MinSDK = SDKInfo.GetInfo(minSDK),
TargetSDK = SDKInfo.GetInfo(targetSDK),
IconName = icon,
Permissions = permissions,
SupportScreens = supportScrs
};
}

private static string splitValueFromString(string source, string pattern) {
string temp = Regex.Match(source, pattern).Value;
string temp = Regex.Match(source, pattern, RegexOptions.IgnoreCase).Value;
return temp.Substring(temp.IndexOf("'") + 1);
}
}
}

93 changes: 58 additions & 35 deletions AAPTForNet/Models/ApkInfo.cs
Original file line number Diff line number Diff line change
@@ -1,50 +1,73 @@
using System.IO;
using System.Collections.Generic;
using System.IO;

namespace AAPTForNet {
public class ApkInfo {
private readonly string unknownInfo = "<Empty>";
private readonly string defaultIconName = "ic_launcher.png";

private string _name;
private string _package;
private string _ver;
private string _icon;
private static readonly string unknownInfo = "<Empty>";
// Default icon name generated by Android Studio
private static readonly string defaultIconName = "ic_launcher.png";

private string _iconName = defaultIconName;

public static readonly string SmallScreen = "small";
public static readonly string NormalScreen = "normal";
public static readonly string LargeScreen = "large";
public static readonly string xLargeScreen = "xlarge";

public static readonly ApkInfo Empty = new ApkInfo();

public string AppName => _name.Length > 0 ? _name : unknownInfo;
public string PackageName => _package.Length > 0 ? _package : unknownInfo;
public string Version => _ver.Length > 0 ? _ver : unknownInfo;
public SDKInfo MinSDK { get; }
public string IconName => _icon.Length > 4 ? _icon : defaultIconName;
public string FullPath { get; }
public long PackageSize => File.Exists(FullPath) ?
new FileInfo(FullPath).Length : 0;
public string IconPath => (IconName != string.Empty && IconName != defaultIconName) ?
AAPTool.TempPath + @"\" + IconName : string.Empty;
public bool isEmpty { get; }
public string AppName { get; set; }
public string PackageName { get; set; }
public string Version { get; set; }
public string FullPath { get; set; }
public SDKInfo MinSDK { get; set; }
public SDKInfo TargetSDK { get; set; }
public List<string> Permissions { get; set; }
public List<string> SupportScreens { get; set; }

public string IconName {
// IconName include name and extension
get => _iconName.Length > 3 ? _iconName : defaultIconName;
set {
if (value == string.Empty || value.ToLower().EndsWith(".xml"))
// Exclude markup icon only. Many packages do not use .png icon
_iconName = defaultIconName;
else
_iconName = Path.GetFileName(value);
}
}
public string IconPath {
get {
if (IconName == defaultIconName) return string.Empty;

internal ApkInfo() {
isEmpty = true;
MinSDK = SDKInfo.Unknown;
_icon = FullPath = string.Empty;
_ver = _package = _name = unknownInfo;
return AAPTool.TempPath + @"\" + IconName;
}
}
public long PackageSize {
get {
if (!File.Exists(FullPath))
return 0;

internal ApkInfo(string path, string name, string package, string ver, SDKInfo sdk, string iconName) {
this.isEmpty = false;
this.MinSDK = sdk;
this.FullPath = path;
this._name = name;
this._package = package;
this._ver = ver;
this._icon = iconName.Length > 4 ? // Include name and extension
Path.GetFileNameWithoutExtension(iconName) + ".png" : defaultIconName;
return new FileInfo(FullPath).Length;
}
}
public bool IsEmpty {
get {
return (AppName == string.Empty || AppName == unknownInfo) &&
(PackageName == string.Empty || PackageName == unknownInfo);
}
}

public override string ToString() {
return string.Format("Name: {0};\nPackage: {1};\nVer: {2};\nMinSDK: {3};\nIcon: {4}\n",
this.AppName, this.PackageName, this.Version, this.MinSDK.ToString(), this.IconPath);
internal ApkInfo() {
AppName = unknownInfo;
PackageName = unknownInfo;
Version = unknownInfo;
FullPath = string.Empty;
MinSDK = SDKInfo.Unknown;
TargetSDK = SDKInfo.Unknown;
Permissions = new List<string>();
SupportScreens = new List<string>();
}
}
}
6 changes: 3 additions & 3 deletions Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Plugin : IViewer {

public void Prepare(string path, ContextObject context) {
context.Theme = Themes.Dark;
context.PreferredSize = new Size { Width = 650, Height = 200 };
context.PreferredSize = new Size { Width = 750, Height = 350 };
context.Title = path;
context.TitlebarOverlap = false;
context.TitlebarBlurVisibility = false;
Expand All @@ -22,9 +22,9 @@ public void Prepare(string path, ContextObject context) {

public void View(string path, ContextObject context) {
var apk = AAPTool.Decompile(path);
if (apk.isEmpty) {
if (apk.IsEmpty) {
context.ViewerContent = new System.Windows.Controls.Label() {
Content = "Can't not load package. Please change the file name and try again.",
Content = "Can't not load package.",
Foreground = System.Windows.Media.Brushes.White,
FontSize = 16,
VerticalAlignment = VerticalAlignment.Center,
Expand Down

0 comments on commit ea2c925

Please sign in to comment.