Skip to content

Commit 9c3549d

Browse files
committed
[WIP] Add Lua IO helpers and WebView bindings
Add Lua-side IO utilities and integrate WebView handler helpers. Introduce webui/src/main/assets/lua/nio.lua to provide SuFile binding and byte-stream copy/read helpers. Update std.lua to expose common Java classes and context/domain globals to Lua. Update str.lua to require std, add stringToStream and export helpers to _G. Refactor webview.lua to require std/str, expose WebView-related Java types, register path/event handlers via java.proxy, and use stringToStream for responses. Update WXView.kt to reference SuFile (ensure the class is available to the Lua bridge). These changes unify stream handling and simplify registering resource handlers and event listeners from Lua.
1 parent 7cc2c61 commit 9c3549d

4 files changed

Lines changed: 90 additions & 26 deletions

File tree

webui/src/main/assets/lua/nio.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require("wx:std")
2+
3+
local sufile_import = java.import("com.dergoogler.mmrl.platform.file.SuFile")
4+
5+
local DEFAULT_BUFFER_SIZE = 8 * 1024
6+
7+
local M = {}
8+
9+
function _G.SuFile(...)
10+
local args = {...}
11+
local jarray = java.array(Object, #args)
12+
for i = 1, #args do
13+
jarray[i] = String(args[i])
14+
end
15+
return java.new(sufile_import, jarray)
16+
end
17+
18+
function M.copyTo(input, output, bufferSize)
19+
bufferSize = bufferSize or DEFAULT_BUFFER_SIZE
20+
local bytesCopied = 0
21+
local chunk
22+
local buf = BufferedInputStream(input, bufferSize)
23+
-- read byte by byte as fallback
24+
local b = buf:read()
25+
while b ~= -1 do
26+
output:write(b)
27+
bytesCopied = bytesCopied + 1
28+
b = buf:read()
29+
end
30+
return bytesCopied
31+
end
32+
33+
function M.readBytes(input)
34+
local available = input:available()
35+
local size = math.max(DEFAULT_BUFFER_SIZE, available)
36+
local buffer = ByteArrayOutputStream(size)
37+
M.copyTo(input, buffer)
38+
return buffer:toByteArray()
39+
end
40+
41+
return M

webui/src/main/assets/lua/std.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
-- std.lua
22

3+
_G.String = java.import("java.lang.String")
4+
_G.ByteArrayInputStream = java.import("java.io.ByteArrayInputStream")
5+
_G.ByteArrayOutputStream = java.import("java.io.ByteArrayOutputStream")
6+
_G.BufferedInputStream = java.import("java.io.BufferedInputStream")
7+
_G.Array = java.import("java.lang.reflect.Array")
8+
_G.Byte = java.import("java.lang.Byte")
9+
_G.Object = java.import("java.lang.Object")
10+
11+
_G.context = _WX_OPTIONS:getContext()
12+
-- android.net.Uri
13+
_G.domain = _WX_OPTIONS:getDomain()
14+
315
local M = {}
416

517
local String = java.import("java.lang.String")

webui/src/main/assets/lua/str.lua

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
-- str.lua
1+
require("wx:std")
22

33
local M = {}
44

55
function M.startsWith(s, prefix) return s:sub(1, #prefix) == prefix end
66
function M.endsWith(s, suffix) return suffix == "" or s:sub(-#suffix) == suffix end
77
function M.trim(s) return s:match("^%s*(.-)%s*$") end
88
function M.jsonString(s) return '"' .. tostring(s):gsub('"', '\\"') .. '"' end
9+
function M.stringToStream(s) return ByteArrayInputStream(String(s):getBytes("UTF-8")) end
910

10-
_G.startsWith = M.startsWith
11-
_G.endsWith = M.endsWith
12-
_G.trim = M.trim
13-
_G.jsonString = M.jsonString
11+
_G.startsWith = M.startsWith
12+
_G.endsWith = M.endsWith
13+
_G.trim = M.trim
14+
_G.jsonString = M.jsonString
15+
_G.stringToStream = M.stringToStream
1416

1517
return M
Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,53 @@
1-
-- webview.lua
1+
require("wx:std")
2+
require("wx:str")
23

3-
local String = java.import("java.lang.String")
4-
local ByteArrayInputStream = java.import("java.io.ByteArrayInputStream")
5-
local WebResourceResponse = java.import("android.webkit.WebResourceResponse")
4+
local R = java.import("android.R")
5+
local WX_R = java.import("com.dergoogler.mmrl.webui.R")
6+
local HWUI_R = java.import("com.dergoogler.mmrl.hybridwebui.R")
7+
local Uri = java.import("android.net.Uri")
8+
9+
_G.WebResourceResponse = java.import("android.webkit.WebResourceResponse")
10+
_G.WebViewFeature = java.import("androidx.webkit.WebViewFeature")
11+
_G.WebMessageCompat = java.import("androidx.webkit.WebMessageCompat")
12+
13+
local rootView = context:getWindow():getDecorView():getRootView()
14+
local wxView = rootView:findViewById(WX_R.id.wxview)
615

716
local M = {}
817

9-
-- Stream helper
18+
-- M.console = wxView:console
19+
20+
function M.registerPathHandler(path, handler, authority)
21+
if authority == nil then
22+
authority = domain:toString()
23+
end
24+
25+
local handlerProxy = java.proxy("com.dergoogler.mmrl.hybridwebui.HybridWebUI$PathHandler", handler)
26+
wxView:addPathHandler(path, handlerProxy, Uri:parse(authority))
27+
end
1028

11-
function M.stringToStream(s)
12-
str = String(s):getBytes("UTF-8")
13-
return ByteArrayInputStream(str)
29+
function M.registerEventListener(objectName, listener)
30+
local listenerProxy = java.proxy("com.dergoogler.mmrl.hybridwebui.HybridWebUI$EventListener", listener)
31+
wxView:addEventListener(objectName, listenerProxy)
1432
end
1533

1634
-- Response helpers
1735

1836
function M.htmlResponse(body, status)
19-
return WebResourceResponse("text/html", "UTF-8", status or 200, "OK", {}, M.stringToStream(body))
37+
return WebResourceResponse("text/html", "UTF-8", status or 200, "OK", {}, stringToStream(body))
2038
end
2139

2240
function M.jsonResponse(body, status)
23-
return WebResourceResponse("application/json", "UTF-8", status or 200, "OK", {}, M.stringToStream(body))
41+
return WebResourceResponse("application/json", "UTF-8", status or 200, "OK", {}, stringToStream(body))
2442
end
2543

2644
function M.textResponse(body, status)
27-
return WebResourceResponse("text/plain", "UTF-8", status or 200, "OK", {}, M.stringToStream(body))
45+
return WebResourceResponse("text/plain", "UTF-8", status or 200, "OK", {}, stringToStream(body))
2846
end
2947

3048
function M.errorResponse(code, message)
31-
return WebResourceResponse("text/plain", "UTF-8", code, message, {}, M.stringToStream(message))
49+
return WebResourceResponse("text/plain", "UTF-8", code, message, {} , stringToStream(message))
3250
end
3351

34-
-- Handler registration
35-
36-
function M.registerPathHandler(path, handler, authority)
37-
_registerPathHandler:invoke(path, handler, authority)
38-
end
39-
40-
function M.registerEventListener(objectName, event)
41-
_registerEventListener:invoke(objectName, event)
42-
end
4352

4453
return M

0 commit comments

Comments
 (0)