-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfrmApkInstaller.pas
447 lines (396 loc) · 12.6 KB
/
frmApkInstaller.pas
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
unit frmApkInstaller;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
Vcl.TitleBarCtrls, UWP.Form, UWP.QuickButton, UWP.Caption, UWP.Button,
Vcl.WinXCtrls, DosCommand, SynEditHighlighter, SynHighlighterUNIXShellScript,
SynEdit, ES.BaseControls, ES.Images,
JclCompression, JclStrings;
type
TApkDetails = record
DisplayName: string;
PackageName: string;
DisplayVersion: string;
Icon: string;
end;
TPanel = class(Vcl.ExtCtrls.TPanel)
private
const
DEFAULT_BORDER_COLOR = clActiveBorder;//$0033CCFF;
DEFAULT_CLIENT_COLOR = clWindow;
DEFAULT_BORDER_RADIUS = 16;
private
{ Private Declarations }
FBorderColor: TColor;
FClientColor: TColor;
FBorderRadius: Integer;
FRounded: Boolean;
procedure SetStyle(const Value: Boolean);
protected
procedure Paint; override;
public
constructor Create(AOwner: TComponent); override;
property Rounded: Boolean read FRounded write SetStyle default False;
end;
TfrmInstaller = class(TUWPForm)
btnReUnInstall: TButton;
btnLaunch: TButton;
lbAPKDisplayName: TLabel;
lbPublisher: TLabel;
lbVersion: TLabel;
lbCertificate: TLabel;
lbCapabilities: TLabel;
apkInstallerMemo: TMemo;
UWPQuickButton1: TUWPQuickButton;
UWPQuickButton2: TUWPQuickButton;
UWPQuickButton3: TUWPQuickButton;
pnlAbout: TPanel;
pnlCaption: TPanel;
ActivityIndicator1: TActivityIndicator;
lbAbout: TLabel;
lbInsVersion: TLabel;
Label1: TLabel;
lnkWebSite: TLinkLabel;
lnkRepository: TLinkLabel;
Button1: TButton;
Shape1: TShape;
DCAapt: TDosCommand;
SynEdit1: TSynEdit;
SynUNIXShellScriptSyn1: TSynUNIXShellScriptSyn;
eApkImage: TEsImage;
btnLog: TButton;
DCPKCS7: TDosCommand;
procedure pnlCaptionMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormCreate(Sender: TObject);
procedure UWPButton2Click(Sender: TObject);
procedure FormClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure lnkWebSiteLinkClick(Sender: TObject; const Link: string;
LinkType: TSysLinkType);
procedure lnkRepositoryLinkClick(Sender: TObject; const Link: string;
LinkType: TSysLinkType);
procedure DCAaptExecuteError(ASender: TObject; AE: Exception;
var AHandled: Boolean);
procedure DCAaptNewLine(ASender: TObject; const ANewLine: string;
AOutputType: TOutputType);
procedure DCAaptTerminateProcess(ASender: TObject;
var ACanTerminate: Boolean);
procedure btnLogClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure DCAaptTerminated(Sender: TObject);
procedure lbCertificateClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
FArchive: TJclDecompressArchive;
FZipContents: TStringList;
public
{ Public declarations }
FApkFile: string;
FApkInfo: TApkDetails;
FApkPermissions: TStringList;
procedure GetAPKInfoWithAndroidAssetPackagingTool;
procedure GetXAPKInfo;
end;
var
frmInstaller: TfrmInstaller;
implementation
uses
Winapi.ShellAPI, WSAManager, UWP.ColorManager, RegularExpressions,
System.JSON, Rest.Json, System.Zip;
{$R *.dfm}
procedure TfrmInstaller.btnLogClick(Sender: TObject);
begin
SynEdit1.Visible := not SynEdit1.Visible;
end;
procedure TfrmInstaller.Button1Click(Sender: TObject);
begin
pnlAbout.Visible := not pnlAbout.Visible;
end;
procedure TfrmInstaller.DCAaptExecuteError(ASender: TObject; AE: Exception;
var AHandled: Boolean);
begin
if AHandled then
ShowMessage(AE.ToString);
end;
procedure TfrmInstaller.DCAaptNewLine(ASender: TObject;
const ANewLine: string; AOutputType: TOutputType);
begin
AOutputType := otEntireLine;
if SynEdit1.Lines.Count > 1000 then
SynEdit1.Lines.Clear;
SynEdit1.Lines.Add(ANewLine);
SynEdit1.GotoLineAndCenter(SynEdit1.Lines.Count);
if FApkInfo.PackageName = '' then
if Pos('package: ', ANewLine) = 1 then
begin
FApkInfo.PackageName := TRegEx.Match(ANewLine, '(?<=name='')[^'']*').Value;
FApkInfo.DisplayVersion := TRegEx.Match(ANewLine, '(?<=versionName='')[^'']*').Value;
end;
if FApkInfo.DisplayName = '' then
if Pos('launchable-activity: ', ANewLine) = 1 then
begin
FApkInfo.DisplayName := TRegEx.Match(ANewLine, '(?<=label='')[^'']*').Value;
// FApkInfo.Icon := TRegEx.Match(ANewLine, '(?<=icon='')[^'']*').Value;
end
else if Pos('application-label', ANewLine) = 1 then
FApkInfo.DisplayName := TRegEx.Match(ANewLine, '(?<=:'')[^'']*').Value;
if Pos('uses-permission: ', ANewLine) = 1 then
FApkPermissions.Add(TRegEx.Match(ANewLine, '(?<=name='')[^'']*').Value);
if FApkInfo.Icon = '' then
if Pos('application-icon-', ANewLine) = 1 then
FApkInfo.Icon := TRegEx.Match(ANewLine, '(?<=:'')[^'']*').Value;
end;
procedure TfrmInstaller.DCAaptTerminated(Sender: TObject);
var
zip: TZipFile;
I: Integer;
zipHeader: TZipHeader;
picBuff: TStream;
vArchive: TJclDecompressArchive;
ArchiveClass: TJclDecompressArchiveClass;
begin
lbAPKDisplayName.Caption := FApkInfo.DisplayName;
lbCapabilities.Caption := 'Capabilities';
lbVersion.Caption := 'Version: ' + FApkInfo.DisplayVersion;
// lbPublisher.Caption := 'Icon: ' + FApkInfo.Icon;
apkInstallerMemo.Lines := FApkPermissions;
// just a dummy ficticious path to use extractfilename, extractfileext, etc.
// since zip files path starts with no c:\ neither backslashes
var dummypath := LowerCase(StringReplace('c:/'+fapkinfo.icon, '/', '\', [rfReplaceAll]));
if ExtractFileExt(dummypath) = '.png' then
// open it and show
begin
zip := TZipFile.Create;
try
if TZipFile.IsValid(FApkFile) then
begin
zip.Open(FApkFile, zmRead);
//load icon
picBuff := TStream.Create;
try
zip.Read(FApkInfo.Icon, picBuff, zipHeader);
eApkImage.Picture.LoadFromStream(picBuff);
finally
picBuff.Free;
end;
end;
finally
zip.Free;
end;
end
else // Open .APK file as ZipFile, list contents and try to find an icon that match some brute force search
if (FApkInfo.Icon <> '') and (FZipContents.Count = 0) then
begin
zip := TZipFile.Create;
try
if TZipFile.IsValid(FApkFile) then
begin
zip.Open(FApkFile, zmRead);
var pngName := ExtractFileName(ChangeFileExt(dummypath, '.png'));
for var filename in zip.FileNames do
begin
if filename.Contains(pngName) then
begin
//load icon
picBuff := TStream.Create;
try
zip.Read(filename, picBuff, zipHeader);
eApkImage.Picture.LoadFromStream(picBuff);
finally
picBuff.Free;
end;
end;
end;
end;
finally
zip.Free;
end;// replaced with 7zip, since TZipFile is too slow
{ vArchive := TJclZipDecompressArchive.Create(FApkFile, 0, False);
try
// if e.g. icon_launcher.xml most likely we would like to find icon_launcher.png instead
var pngName := ExtractFileName(ChangeFileExt(dummypath, '.png'));
vArchive.ListFiles;
for I := 0 to vArchive.ItemCount - 1 do
begin
if not vArchive.Items[I].Directory then
begin
picBuff := TMemoryStream.Create;
try
if string(vArchive.Items[I].PackedName).Contains(pngName) then
begin
vArchive.Items[I].Stream := picBuff;
vArchive.Items[I].OwnsStream := False;
vArchive.Items[I].Selected := True;
vArchive.ExtractSelected();
vArchive.Items[I].Selected := False;
picBuff.Position := 0;
eApkImage.Picture.LoadFromStream(picBuff);
//Break; // that's it. TODO : search other ones, extract them and pick the highest quality one
end;
finally
picBuff.Free;
end;
end;
end;
finally
FreeAndNil(vArchive);
end;}
end;
end;
procedure TfrmInstaller.DCAaptTerminateProcess(ASender: TObject;
var ACanTerminate: Boolean);
begin
ACanTerminate := True;
end;
procedure TfrmInstaller.FormClick(Sender: TObject);
begin
pnlAbout.Visible := False;
end;
procedure TfrmInstaller.FormClose(Sender: TObject; var Action: TCloseAction);
begin
// clear labels and picture on close
lbAPKDisplayName.Caption := '';
lbPublisher.Caption := 'Publisher: ';
lbVersion.Caption := 'Version: ';
lbCertificate.Caption := '';
apkInstallerMemo.Lines.Clear;
eApkImage.Picture := nil;
end;
procedure TfrmInstaller.FormCreate(Sender: TObject);
begin
FApkPermissions := TStringList.Create;
FZipContents := TStringList.Create;
// pnlCaption.Height := GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CXBORDER);
// pnlAbout.Rounded := True;
ColorizationManager.ColorizationType := TUWPColorizationType.ctLight;
end;
procedure TfrmInstaller.FormDestroy(Sender: TObject);
begin
DCAapt.Stop;
FZipContents.Free;
FApkPermissions.Free;
end;
procedure TfrmInstaller.GetAPKInfoWithAndroidAssetPackagingTool;
var
cmdline: string;
begin
eApkImage.Picture := nil;
if FileExists(ExtractFilePath(ParamStr(0))+ 'aapt.exe') then
begin
cmdline := 'aapt.exe d badging "' + FApkFile + '"';
if DCAapt.IsRunning then
DCAapt.Stop;
FZipContents.Clear;
DCAapt.InputToOutput := False;
DCAapt.CommandLine := cmdline;
DCAapt.Execute;
end;
end;
procedure TfrmInstaller.GetXAPKInfo;
var
json: TJSONObject;
zip: TZipFile;
I: Integer;
zipHeader: TZipHeader;
buff: TBytes;
picBuff: TStream;
begin
eApkImage.Picture := nil;
//extract manifest.json from *.xapk to read its info
zip := TZipFile.Create;
json := TJsonObject.Create;
try
if TZipFile.IsValid(FApkFile) then
begin
try
zip.Open(FApkFile, zmRead);
zip.Read('manifest.json', buff);
if json.Parse(buff, 0) > 0 then
begin
//let's find its details from json
SynEdit1.Text := json.ToString;//TEncoding.UTF8.GetString(buff);
lbAPKDisplayName.Caption := json.Values['name'].Value;
lbVersion.Caption := json.Values['version_name'].Value;
//
var icon := json.Values['icon'].Value;
apkInstallerMemo.Text := json.Values['permissions'].ToString;
//load icon
picBuff := TStream.Create;
try
zip.Read(icon, picBuff, zipHeader);
eApkImage.Picture.LoadFromStream(picBuff);
finally
picBuff.Free;
end;
end;
finally
zip.Close;
end;
end;
finally
json.Free;
zip.Free;
end;
end;
procedure TfrmInstaller.lbCertificateClick(Sender: TObject);
begin
// DCPKCS7.CommandLine := 'openssl pkcs7 -in '+GetRSAFile()+' -inform DER -print_certs | openssl x509 -text -noout'
end;
procedure TfrmInstaller.lnkRepositoryLinkClick(Sender: TObject;
const Link: string; LinkType: TSysLinkType);
begin
ShellExecute(0, 'OPEN', PChar(Link), nil, nil, SW_SHOWNORMAL);
end;
procedure TfrmInstaller.lnkWebSiteLinkClick(Sender: TObject; const Link: string;
LinkType: TSysLinkType);
begin
ShellExecute(0, 'OPEN', PChar(Link), nil, nil, SW_SHOWNORMAL);
end;
procedure TfrmInstaller.pnlCaptionMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
ReleaseCapture;
Perform(WM_SYSCOMMAND, $F012, 0);
end;
procedure TfrmInstaller.UWPButton2Click(Sender: TObject);
begin
end;
{ TPanel }
constructor TPanel.Create(AOwner: TComponent);
begin
inherited;
FBorderColor := DEFAULT_BORDER_COLOR;
FClientColor := DEFAULT_CLIENT_COLOR;
FBorderRadius := DEFAULT_BORDER_RADIUS;
end;
procedure TPanel.Paint;
var
r: TRect;
begin
inherited;
if Rounded then
begin
//BevelOuter := bvNone;
Canvas.Pen.Color := FBorderColor;
Canvas.Brush.Color := FBorderColor;
Canvas.Brush.Style := bsSolid;
Canvas.FillRect(Rect(FBorderRadius,
0, ClientWidth - FBorderRadius, FBorderRadius));
Canvas.Ellipse(Rect(0, 0, 2 * FBorderRadius, 2 * FBorderRadius));
Canvas.Ellipse(Rect(ClientWidth - 2 * FBorderRadius, 0,
ClientWidth, 2 * FBorderRadius));
// Canvas.Brush.Color := FClientColor;
//// Canvas.Rectangle(Rect(0, FBorderRadius, ClientWidth, ClientHeight));
// Canvas.Font.Assign(Self.Font);
// r := Rect(FBorderRadius, 0, ClientWidth - FBorderRadius, FBorderRadius);
// Canvas.Brush.Style := bsClear;
end;
end;
procedure TPanel.SetStyle(const Value: Boolean);
begin
FRounded := Value;
end;
end.