Skip to content

Commit 66fe0dd

Browse files
author
lucas
committed
lua桥接的webView 支持 webViewCallBack 和setInitData 两个方法
1 parent 86e2e3c commit 66fe0dd

File tree

7 files changed

+178
-5
lines changed

7 files changed

+178
-5
lines changed

VideoOS/LuaViewSDK/src/cn/com/venvy/lua/maper/LVWebViewMethodMapper.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
import com.taobao.luaview.fun.mapper.LuaViewLib;
77
import com.taobao.luaview.fun.mapper.ui.UIViewMethodMapper;
88
import com.taobao.luaview.global.VmVersion;
9+
import com.taobao.luaview.util.JsonUtil;
910
import com.taobao.luaview.util.LuaUtil;
1011

1112
import org.luaj.vm2.LuaFunction;
13+
import org.luaj.vm2.LuaTable;
1214
import org.luaj.vm2.LuaValue;
1315
import org.luaj.vm2.Varargs;
1416

@@ -35,7 +37,9 @@ public class LVWebViewMethodMapper<U extends VenvyUDWebView> extends UIViewMetho
3537
"stopLoading",//8
3638
"url",//9
3739
"pullRefreshEnable",//10
38-
"callJS"//11
40+
"callJS",//11
41+
"webViewCallback", // 12
42+
"setInitData" // 13
3943
};
4044

4145
@Override
@@ -71,6 +75,10 @@ public Varargs invoke(int code, U target, Varargs varargs) {
7175
return pullRefreshEnable(target, varargs);
7276
case 11:
7377
return callJS(target, varargs);
78+
case 12:
79+
return webViewCallback(target, varargs);
80+
case 13:
81+
return setInitData(target, varargs);
7482
}
7583
return super.invoke(code, target, varargs);
7684
}
@@ -147,4 +155,23 @@ public LuaValue callJS(U view, Varargs varargs) {
147155
final LuaFunction callback = LuaUtil.getFunction(varargs, 3);
148156
return LuaValue.valueOf(view.callJS(jsMethod, callback));
149157
}
158+
159+
public LuaValue webViewCallback(U view, Varargs varargs) {
160+
final LuaTable callback = varargs.opttable(2, null);
161+
if (callback != null) {
162+
LuaValue onClose = LuaUtil.getFunction(callback, "onClose", "onClose");
163+
return view.webViewCallback(onClose);
164+
}
165+
166+
return LuaValue.valueOf("");
167+
}
168+
169+
public LuaValue setInitData(U view, Varargs varargs) {
170+
final LuaTable data = varargs.opttable(2, null);
171+
if (data != null) {
172+
String jsData = JsonUtil.toString(data);
173+
return view.setInitData(jsData);
174+
}
175+
return LuaValue.valueOf("");
176+
}
150177
}

VideoOS/LuaViewSDK/src/cn/com/venvy/lua/ud/VenvyUDWebView.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,25 @@
1313
import org.luaj.vm2.Varargs;
1414

1515
import cn.com.venvy.common.webview.IVenvyWebView;
16+
import cn.com.venvy.common.webview.JsBridge;
1617
import cn.com.venvy.lua.view.VenvyLVWebView;
1718

1819
/**
1920
* Created by Arthur on 2017/9/4.
2021
*/
2122

2223
public class VenvyUDWebView extends UDView<VenvyLVWebView> {
24+
25+
private JsBridge jsBridge;
26+
2327
public VenvyUDWebView(VenvyLVWebView view, Globals globals, LuaValue metatable, Varargs initParams) {
2428
super(view, globals, metatable, initParams);
2529
}
2630

31+
public void setJsBridge(JsBridge jsBridge) {
32+
this.jsBridge = jsBridge;
33+
}
34+
2735
/**
2836
* Loads the given URL.
2937
*/
@@ -176,5 +184,24 @@ public void onReceiveValue(String value) {
176184

177185
return "";
178186
}
187+
188+
public VenvyUDWebView webViewCallback(final LuaValue callback){
189+
jsBridge.setWebViewCloseListener(new JsBridge.WebViewCloseListener() {
190+
@Override
191+
public void onClose(CloseType actionType) {
192+
LuaUtil.callFunction(callback);
193+
}
194+
});
195+
return this;
196+
}
197+
198+
public VenvyUDWebView setInitData(String data){
199+
final VenvyLVWebView view = this.getView();
200+
if (view != null) {
201+
view.setJsData(data);
202+
}
203+
return this;
204+
}
205+
179206
}
180207

VideoOS/LuaViewSDK/src/cn/com/venvy/lua/view/VenvyLVWebView.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
public class VenvyLVWebView extends FrameLayout implements ILVNativeViewProvider, ILVView {
3838

39-
protected UDView mLuaUserdata;
39+
protected VenvyUDWebView mLuaUserdata;
4040
protected IVenvyWebView mWebView;
4141
protected JsBridge mJsBridge;
4242
protected boolean mIsLoading;
@@ -52,6 +52,7 @@ public void init(Context context, Globals globals) {
5252
this.mWebView = WebViewFactory.createWebView(context);
5353
mJsBridge = new JsBridge(context, mWebView, platform);
5454
mJsBridge.setDeveloperUserId(getDeveloperUserId(globals));
55+
this.mLuaUserdata.setJsBridge(mJsBridge);
5556
if (mWebView instanceof VenvyWebView) {
5657
((VenvyWebView) mWebView).setJsBridge(mJsBridge);
5758
}
@@ -94,6 +95,9 @@ public boolean getLoadingState() {
9495
}
9596

9697

98+
public void setJsData(String jsData){
99+
mJsBridge.setJsData(jsData);
100+
}
97101
@Override
98102
public View getNativeView() {
99103
if (mWebView instanceof View) {
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
-- 商品
2+
require "os_config"
3+
require "os_string"
4+
require "os_constant"
5+
require "os_util"
6+
require "os_track"
7+
shopping = object:new()
8+
local adTypeName = "shopping"
9+
local scale = getScale()
10+
11+
local function updateLuaViewSize(luaView, isPortrait)
12+
if (luaView == nil) then
13+
return
14+
end
15+
local screenWidth, screenHeight = Native:getVideoSize(2)
16+
if (isPortrait) then
17+
luaView:frame(0, y, math.min(screenWidth, screenHeight), math.max(screenWidth, screenHeight))
18+
else
19+
luaView:frame(0, 0, math.max(screenWidth, screenHeight), math.min(screenWidth, screenHeight))
20+
end
21+
end
22+
23+
local function updateWebViewSize(webView, isPortrait)
24+
if (webView == nil) then
25+
return
26+
end
27+
local w, h = shopping.luaView:size()
28+
if (isPortrait) then
29+
local _, videoHeight, y = Native:getVideoSize(0)
30+
if System.android() then
31+
y = 0.0
32+
end
33+
webView:frame(0, y + videoHeight, w, h - y - videoHeight)
34+
else
35+
local webViewWidth = h / 375 * 220
36+
webView:frame(w - webViewWidth, 0, webViewWidth, h)
37+
end
38+
end
39+
40+
local function createParent(isPortrait)
41+
local luaView
42+
if System.android() then
43+
luaView = View()
44+
else
45+
luaView = ThroughView()
46+
end
47+
updateLuaViewSize(luaView, isPortrait)
48+
return luaView
49+
end
50+
51+
local function createWebView(isPortrait)
52+
local webView = WebView()
53+
webView:setInitData(shopping.data)
54+
webView:webViewCallback({
55+
onClose = function()
56+
Native:destroyView()
57+
end
58+
})
59+
shopping.luaView:addView(webView)
60+
61+
updateWebViewSize(webView, isPortrait)
62+
return webView
63+
end
64+
65+
local function onScreenChange(isPortrait)
66+
-- body
67+
updateLuaViewSize(shopping.luaView, isPortrait)
68+
updateWebViewSize(shopping.webView, isPortrait)
69+
end
70+
71+
local function registerMedia()
72+
local media = Media()
73+
-- body
74+
-- 注册window callback通知
75+
local callbackTable = {
76+
--0: 竖屏小屏幕,1 竖屏全凭,2 横屏全屏
77+
onPlayerSize = function(type)
78+
if (type == 0) then
79+
onScreenChange(true)
80+
elseif (type == 1) then
81+
onScreenChange(true)
82+
elseif (type == 2) then
83+
onScreenChange(false)
84+
end
85+
end
86+
}
87+
media:mediaCallback(callbackTable)
88+
return media
89+
end
90+
91+
local function onCreate()
92+
-- body
93+
local isPortrait = Native:isPortraitScreen()
94+
shopping.luaView = createParent(isPortrait)
95+
shopping.webView = createWebView(isPortrait)
96+
shopping.media = registerMedia()
97+
98+
end
99+
100+
--入口Native调用--
101+
function show(args)
102+
103+
local dataTable = args.data
104+
if (dataTable == nil) then
105+
return
106+
end
107+
108+
shopping.data = dataTable
109+
110+
onCreate()
111+
shopping.webView:loadUrl(shopping.data.data.url)
112+
113+
end

VideoOS/venvy_pub/src/main/java/cn/com/videopls/pub/VideoPlusController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,8 @@ public void downComplete(String entranceLua, String miniAppInfo) {
486486
JSONObject paramsJson = new JSONObject();
487487

488488
try {
489-
paramsJson.put("data", params.get("data"));
490-
paramsJson.put("miniAppInfo", miniAppInfo);
489+
paramsJson.put("data", new JSONObject(params.get("data")));
490+
paramsJson.put("miniAppInfo", new JSONObject(miniAppInfo));
491491
} catch (JSONException e) {
492492
e.printStackTrace();
493493
}

VideoOS/venvy_pub/src/main/java/cn/com/videopls/pub/VideoProgramTypeBView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public void start(@NonNull String appletId, String data, int orientationType, bo
198198
*/
199199
public void startH5(String url, String developerUserId) {
200200
currentH5Program.addDeveloperUserIdToJsBridge(developerUserId);
201-
// url = "http://cytroncdn.videojj.com/pages/jijiMall/dev/index.html#debug";
201+
url = "http://cytroncdn.videojj.com/pages/jijiMall/dev/index.html#debug";
202202
currentH5Program.openLink(url);
203203
}
204204

VideoOS/venvy_pub/src/main/java/cn/com/videopls/pub/VideoWebToolBarView.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import cn.com.venvy.common.webview.JsBridge;
3131
import cn.com.venvy.common.webview.VenvyWebView;
3232

33+
import static cn.com.venvy.common.webview.JsBridge.WebViewCloseListener.CloseType.*;
34+
3335
/**
3436
* Created by Lucas on 2019/8/30.
3537
*/

0 commit comments

Comments
 (0)