-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPlugin.cs
82 lines (68 loc) · 2.4 KB
/
Plugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.IO;
using System.Windows;
using AAPTForNet;
using QuickLook.Common.Helpers;
using QuickLook.Common.Plugin;
namespace QuickLook.Plugin.ApkViewer {
public class Plugin : IViewer {
private string tempApk = string.Empty;
public int Priority => 0;
public void Init() { }
public bool CanHandle(string path) => path.ToLower().EndsWith(".apk");
public void Prepare(string path, ContextObject context) {
context.Title = Path.GetFileName(path);
context.PreferredSize = new Size { Width = 750, Height = 450 };
tempApk = createTempApk(path);
}
public void View(string path, ContextObject context) {
try {
var apk = AAPTool.Decompile(tempApk);
if (apk.IsEmpty)
context.ViewerContent = new ErrorContent();
else
context.ViewerContent = new ViewerPane(context) { ApkInfo = apk };
}
catch (Exception e) {
ProcessHelper.WriteLog($"{path}\r\n{e}");
context.ViewerContent = new ErrorContent();
}
context.IsBusy = false;
}
public void Cleanup() {
if (!tempApk.ToLower().EndsWith(".tmp"))
return;
try {
File.Delete(tempApk);
}
catch (Exception e) {
ProcessHelper.WriteLog(e.ToString());
}
}
private string createTempApk(string sourceFile) {
string tempFile = string.Empty;
try {
tempFile = Path.GetTempFileName();
}
catch (IOException) {
tempFile = Path.Combine(Path.GetTempPath(), $"{nameof(ApkViewer)}.tmp");
}
try {
File.Copy(sourceFile, tempFile, true);
return tempFile;
}
catch {
return sourceFile;
}
}
private class ErrorContent : System.Windows.Controls.Label {
public ErrorContent() {
FontSize = 16;
Content = "Can not load package.";
Foreground = System.Windows.Media.Brushes.White;
VerticalAlignment = VerticalAlignment.Center;
HorizontalAlignment = HorizontalAlignment.Center;
}
}
}
}