Repository navigation
Expand file tree
/
Copy pathupdater.lua
More file actions
640 lines (564 loc) · 19.3 KB
/
Copy pathupdater.lua
File metadata and controls
640 lines (564 loc) · 19.3 KB
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
local updater = {}
local Dialog = luajava.bindClass("android.app.Dialog")
local WindowManager = luajava.bindClass("android.view.WindowManager")
local Build = luajava.bindClass("android.os.Build")
local activeDialog = nil
local function dismissActiveDialog()
if activeDialog then
pcall(function() activeDialog.dismiss() end)
activeDialog = nil
end
end
local function setScreen(view)
local ok = pcall(function()
service.setContentView(view)
end)
if not ok then
pcall(function()
dismissActiveDialog()
local dialog = Dialog(service)
dialog.requestWindowFeature(1)
dialog.setContentView(view)
local window = dialog.getWindow()
if window then
if Build.VERSION.SDK_INT >= 26 then
window.setType(WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY)
else
window.setType(2003)
end
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT)
end
dialog.show()
activeDialog = dialog
end)
end
end
local function closeExtension()
dismissActiveDialog()
pcall(function() service.finish() end)
end
local function enableBackKey(view, onBackFn)
pcall(function()
view.setFocusableInTouchMode(true)
view.requestFocus()
view.setOnKeyListener(luajava.createProxy("android.view.View$OnKeyListener", {
onKey = function(v, keyCode, event)
if event.getAction() == 0 and keyCode == 4 then
if onBackFn then onBackFn() end
return true
end
return false
end
}))
end)
end
updater.config = {
CURRENT_VERSION = "2026.10.04.07",
VERSION_URL = "https://raw.githubusercontent.com/Mahadeesh18/ComputerShortcuts/main/virgin.txt",
WHATSNEW_URL = "https://raw.githubusercontent.com/Mahadeesh18/ComputerShortcuts/main/what's%20new.txt",
ZIP_URL = "https://github.com/Mahadeesh18/ComputerShortcuts/archive/refs/heads/main.zip",
TARGET_PATH = "/storage/self/primary/解说/Plugins/Computer Shortcuts/",
MAIN_FILE = "main.lua",
UPDATER_FILE = "updater.lua",
EXCLUDE_FILES = {
["virgin.txt"] = true,
["what's new.txt"] = true,
}
}
local Handler = luajava.bindClass("android.os.Handler")
local Looper = luajava.bindClass("android.os.Looper")
local Toast = luajava.bindClass("android.widget.Toast")
local URL = luajava.bindClass("java.net.URL")
local BufferedReader = luajava.bindClass("java.io.BufferedReader")
local InputStreamReader = luajava.bindClass("java.io.InputStreamReader")
local File = luajava.bindClass("java.io.File")
local FileOutputStream = luajava.bindClass("java.io.FileOutputStream")
local FileInputStream = luajava.bindClass("java.io.FileInputStream")
local ZipInputStream = luajava.bindClass("java.util.zip.ZipInputStream")
local Executors = luajava.bindClass("java.util.concurrent.Executors")
local LinearLayout = luajava.bindClass("android.widget.LinearLayout")
local ScrollView = luajava.bindClass("android.widget.ScrollView")
local TextView = luajava.bindClass("android.widget.TextView")
local Button = luajava.bindClass("android.widget.Button")
local Color = luajava.bindClass("android.graphics.Color")
local View = luajava.bindClass("android.view.View")
local Byte = luajava.bindClass("java.lang.Byte")
local Thread = luajava.bindClass("java.lang.Thread")
local mainHandler = Handler(Looper.getMainLooper())
local executorService = Executors.newCachedThreadPool()
local function makeRunnable(fn)
return luajava.createProxy("java.lang.Runnable", {
run = function() fn() end
})
end
local function makeOnClickListener(fn)
return luajava.createProxy("android.view.View$OnClickListener", {
onClick = function(v) fn(v) end
})
end
local function runOnUI(fn)
mainHandler.post(makeRunnable(function() fn() end))
end
local function runInBackground(fn)
executorService.execute(makeRunnable(function() fn() end))
end
local function showToast(msg)
runOnUI(function()
Toast.makeText(service, msg, Toast.LENGTH_SHORT).show()
end)
end
local function cleanHtmlContent(rawHtml)
if not rawHtml then return nil end
if not (rawHtml:find("<html") or rawHtml:find("<!DOCTYPE")) then
return rawHtml
end
local extractedLines = {}
for rawLinesMatch in rawHtml:gmatch('"rawLines":%s*%[(.-)%]') do
for str in rawLinesMatch:gmatch('"([^"]-)"') do
table.insert(extractedLines, str)
end
end
if #extractedLines > 0 then
return table.concat(extractedLines, "\n")
end
for codeLine in rawHtml:gmatch('class="[^"]*blob%-code[^"]*"[^>]*>(.-)</td>') do
local cleanLine = codeLine:gsub("<[^>]+>", "")
table.insert(extractedLines, cleanLine)
end
if #extractedLines > 0 then
return table.concat(extractedLines, "\n")
end
return rawHtml:gsub("<script.-</script>", ""):gsub("<style.-</style>", ""):gsub("<[^>]+>", "")
end
local function fetchUrlText(urlString)
local currentUrl = urlString
pcall(function() math.randomseed(os.time() + math.floor(os.clock() * 1000)) end)
local cacheBuster = "cb=" .. tostring(os.time()) .. "_" .. tostring(math.random(100000, 999999))
if not currentUrl:find("%?") then
currentUrl = currentUrl .. "?" .. cacheBuster
else
currentUrl = currentUrl .. "&" .. cacheBuster
end
local lastErr = "Unknown connection error"
for i = 1, 5 do
local conn
local success, res = pcall(function()
local url = URL(currentUrl)
conn = url.openConnection()
conn.setRequestMethod("GET")
conn.setConnectTimeout(30000)
conn.setReadTimeout(30000)
conn.setInstanceFollowRedirects(true)
conn.setUseCaches(false)
pcall(function() conn.setDefaultUseCaches(false) end)
conn.setRequestProperty("User-Agent", "Mozilla/5.0")
conn.setRequestProperty("Cache-Control", "no-cache, no-store, must-revalidate, max-age=0")
conn.setRequestProperty("Pragma", "no-cache")
conn.setRequestProperty("Expires", "0")
local code = conn.getResponseCode()
if code == 301 or code == 302 or code == 303 or code == 307 or code == 308 then
local loc = conn.getHeaderField("Location")
conn.disconnect()
if loc then
currentUrl = loc
return "REDIRECT"
end
return nil
end
if code ~= 200 then return nil end
local reader = BufferedReader(InputStreamReader(conn.getInputStream(), "UTF-8"))
local lines = {}
local line = reader.readLine()
while line ~= nil do
table.insert(lines, line)
line = reader.readLine()
end
reader.close()
return table.concat(lines, "\n")
end)
if conn then pcall(function() conn.disconnect() end) end
if success then
if res ~= "REDIRECT" and res then
return cleanHtmlContent(res)
end
else
lastErr = tostring(res)
end
end
return nil, lastErr
end
local function downloadFile(urlString, destFile)
local currentUrl = urlString
pcall(function() math.randomseed(os.time() + math.floor(os.clock() * 1000)) end)
local cacheBuster = "cb=" .. tostring(os.time()) .. "_" .. tostring(math.random(100000, 999999))
if not currentUrl:find("%?") then
currentUrl = currentUrl .. "?" .. cacheBuster
else
currentUrl = currentUrl .. "&" .. cacheBuster
end
for i = 1, 5 do
local conn
local success, res = pcall(function()
local url = URL(currentUrl)
conn = url.openConnection()
conn.setRequestMethod("GET")
conn.setConnectTimeout(15000)
conn.setReadTimeout(15000)
conn.setInstanceFollowRedirects(true)
conn.setUseCaches(false)
pcall(function() conn.setDefaultUseCaches(false) end)
local code = conn.getResponseCode()
if code == 301 or code == 302 or code == 303 or code == 307 or code == 308 then
local loc = conn.getHeaderField("Location")
conn.disconnect()
if loc then
currentUrl = loc
return "REDIRECT"
end
return false
end
if code ~= 200 then return false end
local input = conn.getInputStream()
local output = FileOutputStream(destFile)
local buffer = luajava.newArray(Byte.TYPE, 4096)
local bytesRead = input.read(buffer)
while bytesRead ~= -1 do
output.write(buffer, 0, bytesRead)
bytesRead = input.read(buffer)
end
output.close()
input.close()
return true
end)
if conn then pcall(function() conn.disconnect() end) end
if success then
if res ~= "REDIRECT" then
return res == true
end
else
return false
end
end
return false
end
local function deleteDirectoryContents(dir)
if not dir or not dir.exists() then return end
local files = dir.listFiles()
if files ~= nil then
for i = 0, #files - 1 do
local f = files[i]
if f ~= nil then
if f.isDirectory() then
deleteDirectoryContents(f)
f.delete()
else
f.delete()
end
end
end
end
end
local function extractZipExcluding(zipFile, destDir, excludeMap)
local fis = FileInputStream(zipFile)
local zis = ZipInputStream(fis)
local entry = zis.getNextEntry()
while entry ~= nil do
local entryName = entry.getName()
local slashIdx = entryName:find("/")
if slashIdx then
local relativePath = entryName:sub(slashIdx + 1)
if relativePath ~= "" then
local filename = relativePath:match("^.+/(.+)$") or relativePath
local isExcluded = excludeMap[filename:lower()]
local outFile = File(destDir, relativePath)
if entry.isDirectory() then
outFile.mkdirs()
else
if not isExcluded then
local parent = outFile.getParentFile()
if parent and not parent.exists() then
parent.mkdirs()
end
local fos = FileOutputStream(outFile)
local buf = luajava.newArray(Byte.TYPE, 4096)
local len = zis.read(buf)
while len > 0 do
fos.write(buf, 0, len)
len = zis.read(buf)
end
fos.close()
end
end
end
end
zis.closeEntry()
entry = zis.getNextEntry()
end
zis.close()
fis.close()
end
local function updateVersionInFile(newVersion)
updater.config.CURRENT_VERSION = newVersion
local filePath = updater.config.TARGET_PATH .. updater.config.UPDATER_FILE
local rf = io.open(filePath, "r")
if rf then
local content = rf:read("*a")
rf:close()
if content then
local updatedContent = content:gsub('CURRENT_VERSION%s*=%s*["\']([^"\']+)["\']', 'CURRENT_VERSION = "' .. newVersion .. '"')
local wf = io.open(filePath, "w")
if wf then
wf:write(updatedContent)
wf:flush()
wf:close()
end
end
end
end
function updater.showLoadingDialog(msg)
local rootLayout = LinearLayout(service)
rootLayout.setOrientation(LinearLayout.VERTICAL)
rootLayout.setBackgroundColor(Color.BLACK)
local layoutInner = LinearLayout(service)
layoutInner.setOrientation(LinearLayout.VERTICAL)
layoutInner.setPadding(20, 20, 20, 20)
local tvMsg = TextView(service)
tvMsg.setText(msg or "Checking for update...")
tvMsg.setTextSize(18)
tvMsg.setTextColor(Color.WHITE)
tvMsg.setPadding(0, 20, 0, 20)
layoutInner.addView(tvMsg)
rootLayout.addView(layoutInner)
enableBackKey(rootLayout, function() end)
setScreen(rootLayout)
end
function updater.showNoUpdateDialog(onDismiss)
local rootLayout = LinearLayout(service)
rootLayout.setOrientation(LinearLayout.VERTICAL)
rootLayout.setBackgroundColor(Color.BLACK)
local layoutInner = LinearLayout(service)
layoutInner.setOrientation(LinearLayout.VERTICAL)
layoutInner.setPadding(20, 20, 20, 20)
local tvMsg = TextView(service)
tvMsg.setText("No updates available")
tvMsg.setTextSize(18)
tvMsg.setTextColor(Color.GREEN)
tvMsg.setPadding(0, 20, 0, 20)
layoutInner.addView(tvMsg)
rootLayout.addView(layoutInner)
enableBackKey(rootLayout, function() end)
setScreen(rootLayout)
mainHandler.postDelayed(makeRunnable(function()
dismissActiveDialog()
if onDismiss then onDismiss() end
end), 1000)
end
function updater.showErrorDialog(msg, onDismiss)
local rootLayout = LinearLayout(service)
rootLayout.setOrientation(LinearLayout.VERTICAL)
rootLayout.setBackgroundColor(Color.BLACK)
local scrollLayout = ScrollView(service)
local layoutInner = LinearLayout(service)
layoutInner.setOrientation(LinearLayout.VERTICAL)
layoutInner.setPadding(20, 20, 20, 20)
local tvMsg = TextView(service)
tvMsg.setText("Error Checking Update!\n\nDetails:\n" .. tostring(msg) .. "\n\nLocal Version: " .. tostring(updater.config.CURRENT_VERSION))
tvMsg.setTextSize(18)
tvMsg.setTextColor(Color.RED)
tvMsg.setPadding(0, 20, 0, 20)
layoutInner.addView(tvMsg)
local btnClose = Button(service)
btnClose.setText("Close")
btnClose.setOnClickListener(makeOnClickListener(function()
dismissActiveDialog()
if onDismiss then onDismiss() end
end))
layoutInner.addView(btnClose)
scrollLayout.addView(layoutInner)
rootLayout.addView(scrollLayout)
enableBackKey(rootLayout, function()
dismissActiveDialog()
if onDismiss then onDismiss() end
end)
setScreen(rootLayout)
end
function updater.showUpdateDialog(onlineVersion, whatsNewText, onDismiss)
local rootLayout = LinearLayout(service)
rootLayout.setOrientation(LinearLayout.VERTICAL)
rootLayout.setBackgroundColor(Color.BLACK)
local isDownloading = false
enableBackKey(rootLayout, function()
if isDownloading then
Toast.makeText(service, "Downloading in progress...", Toast.LENGTH_SHORT).show()
else
dismissActiveDialog()
if onDismiss then onDismiss() end
end
end)
local scrollLayout = ScrollView(service)
local layoutInner = LinearLayout(service)
layoutInner.setOrientation(LinearLayout.VERTICAL)
layoutInner.setPadding(20, 20, 20, 20)
local btnDismiss = Button(service)
btnDismiss.setText("Dismiss Update Dialog")
btnDismiss.setOnClickListener(makeOnClickListener(function()
if not isDownloading then
dismissActiveDialog()
if onDismiss then onDismiss() end
end
end))
layoutInner.addView(btnDismiss)
local titleView = TextView(service)
titleView.setText("Update Available: v" .. onlineVersion)
titleView.setTextSize(20)
titleView.setTextColor(Color.GREEN)
titleView.setPadding(0, 10, 0, 10)
layoutInner.addView(titleView)
local headerWhatsNew = TextView(service)
headerWhatsNew.setText("What's New:")
headerWhatsNew.setTextSize(18)
headerWhatsNew.setTextColor(Color.YELLOW)
headerWhatsNew.setPadding(0, 10, 0, 10)
layoutInner.addView(headerWhatsNew)
if whatsNewText and whatsNewText ~= "" then
for line in whatsNewText:gmatch("[^\r\n]+") do
local lineView = TextView(service)
lineView.setText(line)
lineView.setTextSize(16)
lineView.setTextColor(Color.WHITE)
lineView.setPadding(0, 5, 0, 5)
layoutInner.addView(lineView)
end
else
local lineView = TextView(service)
lineView.setText("No details provided.")
lineView.setTextSize(16)
lineView.setTextColor(Color.WHITE)
layoutInner.addView(lineView)
end
local btnUpdate = Button(service)
btnUpdate.setText("Update Now")
local btnCancel = Button(service)
btnCancel.setText("Cancel")
btnCancel.setVisibility(View.GONE)
btnUpdate.setOnClickListener(makeOnClickListener(function()
if isDownloading then return end
isDownloading = true
btnDismiss.setEnabled(false)
btnUpdate.setText("Downloading...")
btnUpdate.setEnabled(false)
btnCancel.setVisibility(View.VISIBLE)
runInBackground(function()
local tempZip = File(service.getCacheDir(), "update_temp.zip")
local success = downloadFile(updater.config.ZIP_URL, tempZip)
if not success then
isDownloading = false
runOnUI(function()
btnDismiss.setEnabled(true)
btnUpdate.setText("Update Now")
btnUpdate.setEnabled(true)
btnCancel.setVisibility(View.GONE)
Toast.makeText(service, "Download failed! Try again.", Toast.LENGTH_SHORT).show()
end)
return
end
local destDir = File(updater.config.TARGET_PATH)
deleteDirectoryContents(destDir)
pcall(function()
extractZipExcluding(tempZip, destDir, updater.config.EXCLUDE_FILES)
end)
if tempZip.exists() then tempZip.delete() end
updateVersionInFile(onlineVersion)
runOnUI(function()
updater.showRestartDialog()
end)
end)
end))
btnCancel.setOnClickListener(makeOnClickListener(function()
if isDownloading then
isDownloading = false
btnDismiss.setEnabled(true)
btnUpdate.setText("Update Now")
btnUpdate.setEnabled(true)
btnCancel.setVisibility(View.GONE)
Toast.makeText(service, "Update canceled.", Toast.LENGTH_SHORT).show()
end
end))
layoutInner.addView(btnUpdate)
layoutInner.addView(btnCancel)
scrollLayout.addView(layoutInner)
rootLayout.addView(scrollLayout)
setScreen(rootLayout)
end
function updater.showRestartDialog()
local rootLayout = LinearLayout(service)
rootLayout.setOrientation(LinearLayout.VERTICAL)
rootLayout.setBackgroundColor(Color.BLACK)
local scrollLayout = ScrollView(service)
local layoutInner = LinearLayout(service)
layoutInner.setOrientation(LinearLayout.VERTICAL)
layoutInner.setPadding(20, 20, 20, 20)
local tvMsg = TextView(service)
tvMsg.setText("Update completed successfully! Please restart the extension.")
tvMsg.setTextSize(18)
tvMsg.setTextColor(Color.GREEN)
tvMsg.setPadding(0, 20, 0, 20)
layoutInner.addView(tvMsg)
local btnRestart = Button(service)
btnRestart.setText("Restart Extension")
btnRestart.setOnClickListener(makeOnClickListener(function()
for k, _ in pairs(package.loaded) do
if k ~= "string" and k ~= "table" and k ~= "math" and k ~= "coroutine" and k ~= "package" and k ~= "io" and k ~= "os" and k ~= "debug" and k ~= "luajava" then
package.loaded[k] = nil
end
end
pcall(function() closeExtension() end)
mainHandler.postDelayed(makeRunnable(function()
pcall(function()
dofile(updater.config.TARGET_PATH .. updater.config.MAIN_FILE)
end)
end), 300)
end))
layoutInner.addView(btnRestart)
scrollLayout.addView(layoutInner)
rootLayout.addView(scrollLayout)
enableBackKey(rootLayout, function()
pcall(function() closeExtension() end)
end)
setScreen(rootLayout)
end
function updater.checkUpdate(onFinished)
local startTime = os.time()
runOnUI(function()
updater.showLoadingDialog("Checking for update...")
end)
runInBackground(function()
local onlineVersionRaw, errDetail = fetchUrlText(updater.config.VERSION_URL)
local elapsedTime = os.time() - startTime
if elapsedTime < 1 then
pcall(function() Thread.sleep(1000) end)
end
if not onlineVersionRaw then
runOnUI(function()
dismissActiveDialog()
showToast("Connection error")
if onFinished then onFinished() end
end)
return
end
local onlineVersion = onlineVersionRaw:match("^%s*(.-)%s*$")
if onlineVersion == updater.config.CURRENT_VERSION then
runOnUI(function()
dismissActiveDialog()
updater.showNoUpdateDialog(onFinished)
end)
else
local whatsNewText = fetchUrlText(updater.config.WHATSNEW_URL) or ""
runOnUI(function()
dismissActiveDialog()
updater.showUpdateDialog(onlineVersion, whatsNewText, onFinished)
end)
end
end)
end
return updater