|
2 | 2 |
|
3 | 3 | namespace AAPTForNet { |
4 | 4 | /// <summary> |
5 | | - /// Parse all messages from AAPTool |
| 5 | + /// Parse output messages from AAPTool |
6 | 6 | /// </summary> |
7 | 7 | internal class ApkParser { |
8 | 8 |
|
9 | | - private static readonly string SDKPattern = @"sdkVersion:'[0-9]{0,1}[^']"; |
10 | | - private static readonly string IconPattern = @"icon='[\w-.\\\/]*[^']"; |
11 | | - private static readonly string AppNamePattern = @"label='[\w-.() \\\/\[\]]*[^']"; |
12 | | - private static readonly string PackagePattern = @"name='[\w.]*[^']"; |
13 | | - private static readonly string VersionPattern = @"versionName='[\w-.() \/]*[^']"; |
14 | 9 | protected ApkParser() { } |
15 | 10 |
|
16 | 11 | public static ApkInfo Parse(DumpModel model) { |
17 | | - if(!model.isSuccess) |
18 | | - return new ApkInfo("", string.Join("\n", model.Messages), "", "", SDKInfo.Unknown, ""); |
| 12 | + if (!model.isSuccess) |
| 13 | + return ApkInfo.Empty; |
19 | 14 |
|
20 | | - // Extract info from apk |
21 | | - string name = "", package = "", ver = "", sdk = "", icon = ""; |
| 15 | + var permissions = new System.Collections.Generic.List<string>(); |
| 16 | + var supportScrs = new System.Collections.Generic.List<string>(); |
| 17 | + |
| 18 | + string ver = string.Empty, |
| 19 | + minSDK = string.Empty, |
| 20 | + targetSDK = string.Empty, |
| 21 | + icon = string.Empty, |
| 22 | + name = string.Empty, |
| 23 | + per = string.Empty, |
| 24 | + package = string.Empty; |
| 25 | + |
22 | 26 |
|
23 | 27 | foreach(string msg in model.Messages) { |
24 | 28 | if(msg.StartsWith("application:")) { |
25 | | - icon = splitValueFromString(msg, IconPattern); |
26 | | - name = splitValueFromString(msg, AppNamePattern); |
| 29 | + icon = splitValueFromString(msg, @"icon='[\w-.\\\/]*[^']"); |
| 30 | + name = splitValueFromString(msg, @"label='[\w-.() \\\/\[\]]*[^']"); |
| 31 | + continue; |
| 32 | + } |
| 33 | + |
| 34 | + if (msg.StartsWith("package:")) { |
| 35 | + ver = splitValueFromString(msg, @"versionName='[\w-.() \/]*[^']"); |
| 36 | + package = splitValueFromString(msg, @"name='[\w.]*[^']"); |
| 37 | + continue; |
27 | 38 | } |
28 | | - else if(msg.StartsWith("package:")) { |
29 | | - ver = splitValueFromString(msg, VersionPattern); |
30 | | - package = splitValueFromString(msg, PackagePattern); |
| 39 | + |
| 40 | + if (msg.StartsWith("sdkVersion:")) { |
| 41 | + minSDK = splitValueFromString(msg, @"sdkVersion:'[0-9]{0,1}[^']"); |
| 42 | + continue; |
31 | 43 | } |
32 | | - else if(msg.StartsWith("sdkVersion:")) { |
33 | | - sdk = splitValueFromString(msg, SDKPattern); |
| 44 | + |
| 45 | + if (msg.StartsWith("targetSdkVersion:")) { |
| 46 | + targetSDK = splitValueFromString(msg, @"targetSdkVersion:'[0-9]{0,1}[^']"); |
| 47 | + continue; |
| 48 | + } |
| 49 | + |
| 50 | + if (msg.StartsWith("uses-permission:")) { |
| 51 | + per = splitValueFromString(msg, @"'([A-Z0-9._])*"); |
| 52 | + permissions.Add(per); |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + if (msg.StartsWith("supports-screens:")) { |
| 57 | + if (msg.Contains(ApkInfo.SmallScreen)) { |
| 58 | + supportScrs.Add(ApkInfo.SmallScreen); |
| 59 | + } |
| 60 | + if (msg.Contains(ApkInfo.NormalScreen)) { |
| 61 | + supportScrs.Add(ApkInfo.NormalScreen); |
| 62 | + } |
| 63 | + if (msg.Contains(ApkInfo.LargeScreen)) { |
| 64 | + supportScrs.Add(ApkInfo.LargeScreen); |
| 65 | + } |
| 66 | + if (msg.Contains(ApkInfo.xLargeScreen)) { |
| 67 | + supportScrs.Add(ApkInfo.xLargeScreen); |
| 68 | + } |
| 69 | + continue; |
34 | 70 | } |
35 | | - else continue; |
36 | 71 | } |
37 | 72 |
|
38 | | - return new ApkInfo(model.FilePath, name, package, ver, SDKInfo.GetInfo(sdk), icon); |
| 73 | + return new ApkInfo() { |
| 74 | + FullPath = model.FilePath, |
| 75 | + AppName = name, |
| 76 | + PackageName = package, |
| 77 | + Version = ver, |
| 78 | + MinSDK = SDKInfo.GetInfo(minSDK), |
| 79 | + TargetSDK = SDKInfo.GetInfo(targetSDK), |
| 80 | + IconName = icon, |
| 81 | + Permissions = permissions, |
| 82 | + SupportScreens = supportScrs |
| 83 | + }; |
39 | 84 | } |
40 | 85 |
|
41 | 86 | private static string splitValueFromString(string source, string pattern) { |
42 | | - string temp = Regex.Match(source, pattern).Value; |
| 87 | + string temp = Regex.Match(source, pattern, RegexOptions.IgnoreCase).Value; |
43 | 88 | return temp.Substring(temp.IndexOf("'") + 1); |
44 | 89 | } |
45 | 90 | } |
46 | 91 | } |
| 92 | + |
0 commit comments