Skip to content

Commit 31f8300

Browse files
committed
添加AcrClould Lua插件
1 parent 2595126 commit 31f8300

File tree

5 files changed

+259
-0
lines changed

5 files changed

+259
-0
lines changed

VideoOS/LuaViewSDK/src/cn/com/venvy/lua/LuaHelper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import cn.com.venvy.Platform;
1212
import cn.com.venvy.lua.binder.UIGradientViewBinder;
13+
import cn.com.venvy.lua.binder.VenvyAcrCloudBinder;
1314
import cn.com.venvy.lua.binder.VenvyActivityLifeCycleBinder;
1415
import cn.com.venvy.lua.binder.VenvyAppletBinder;
1516
import cn.com.venvy.lua.binder.VenvyHttpRequestBinder;
@@ -34,6 +35,7 @@ public class LuaHelper {
3435
private static UIGradientViewBinder uiGradientViewBinder;
3536
private static VenvyMediaLifeCycleBinder venvyMediaLifeCycleBinder;
3637
private static VenvyMqttBinder venvyMqttBinder;
38+
private static VenvyAcrCloudBinder acrCloudBinder;
3739
private static VenvyWebViewBinder venvyWebViewBinder;
3840
private static VenvyActivityLifeCycleBinder venvyActivityLifeCycleBinder;
3941
private static VenvyKeyboardBinder venvyKeyboardBinder;
@@ -76,6 +78,7 @@ private static void registerNativeLibs(@NonNull LuaView luaView, Platform platfo
7678
luaView.registerLibs(getUiGradientViewBinder(),
7779
getMediaLifeCycleBinder(platform),
7880
getMqttBinder(),
81+
getAcrCloudBinder(),
7982
getAppletBinder(platform),
8083
getSvgaBinder(),
8184
getWebViewBinder(platform),
@@ -123,6 +126,10 @@ private static VenvyMqttBinder getMqttBinder() {
123126
return venvyMqttBinder == null ? venvyMqttBinder = new VenvyMqttBinder() : venvyMqttBinder;
124127
}
125128

129+
private static VenvyAcrCloudBinder getAcrCloudBinder() {
130+
return acrCloudBinder == null ? acrCloudBinder = new VenvyAcrCloudBinder() : acrCloudBinder;
131+
}
132+
126133
private static VenvyAppletBinder getAppletBinder(Platform platform) {
127134
VenvyAppletBinder appletBinder = venvyAppletBinder == null ? venvyAppletBinder = new VenvyAppletBinder() : venvyAppletBinder;
128135
appletBinder.setPlatform(platform);
@@ -156,6 +163,7 @@ public static void destroy() {
156163
venvyWebViewBinder = null;
157164
venvyMediaLifeCycleBinder = null;
158165
venvyMqttBinder = null;
166+
acrCloudBinder = null;
159167
venvyAppletBinder = null;
160168
venvyActivityLifeCycleBinder = null;
161169
venvyKeyboardBinder = null;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package cn.com.venvy.lua.binder;
2+
3+
import com.taobao.luaview.fun.base.BaseFunctionBinder;
4+
import com.taobao.luaview.fun.base.BaseVarArgUICreator;
5+
import com.taobao.luaview.view.interfaces.ILVView;
6+
7+
import org.luaj.vm2.Globals;
8+
import org.luaj.vm2.LuaValue;
9+
import org.luaj.vm2.Varargs;
10+
import org.luaj.vm2.lib.LibFunction;
11+
12+
import cn.com.venvy.lua.maper.VenvyAcrCloudMapper;
13+
import cn.com.venvy.lua.view.VenvyLVAcrClouldCallback;
14+
15+
/**
16+
* Created by mac on 18/3/29.
17+
*/
18+
19+
public class VenvyAcrCloudBinder extends BaseFunctionBinder {
20+
21+
public VenvyAcrCloudBinder() {
22+
super("AcrCloud");
23+
}
24+
25+
@Override
26+
public Class<? extends LibFunction> getMapperClass() {
27+
return VenvyAcrCloudMapper.class;
28+
}
29+
30+
@Override
31+
public LuaValue createCreator(LuaValue env, LuaValue metaTable) {
32+
return new BaseVarArgUICreator(env.checkglobals(), metaTable, getMapperClass()) {
33+
@Override
34+
public ILVView createView(Globals globals, LuaValue metaTable, Varargs varargs) {
35+
return new VenvyLVAcrClouldCallback(globals, metaTable, varargs);
36+
}
37+
};
38+
}
39+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package cn.com.venvy.lua.maper;
2+
3+
import android.text.TextUtils;
4+
5+
import com.taobao.luaview.fun.mapper.LuaViewLib;
6+
import com.taobao.luaview.fun.mapper.ui.UIViewMethodMapper;
7+
import com.taobao.luaview.util.LuaUtil;
8+
9+
import org.luaj.vm2.LuaFunction;
10+
import org.luaj.vm2.LuaValue;
11+
import org.luaj.vm2.Varargs;
12+
13+
import java.io.File;
14+
import java.util.List;
15+
16+
import cn.com.venvy.common.utils.VenvyFileUtil;
17+
import cn.com.venvy.common.utils.VenvyLog;
18+
import cn.com.venvy.lua.ud.VenvyUDAcrClouldCallback;
19+
20+
/**
21+
* Created by mac on 18/3/29.
22+
*/
23+
@LuaViewLib(revisions = {"20200226已对标"})
24+
public class VenvyAcrCloudMapper<U extends VenvyUDAcrClouldCallback> extends UIViewMethodMapper<U> {
25+
private static final String TAG = "VenvyAcrCloudMapper";
26+
private static final String[] sMethods = new String[]{
27+
"acrRecognizeCallback",
28+
"startAcrRecognize",
29+
"stopAcrRecognize",
30+
};
31+
32+
@Override
33+
public List<String> getAllFunctionNames() {
34+
return mergeFunctionNames(TAG, super.getAllFunctionNames(), sMethods);
35+
}
36+
37+
@Override
38+
public Varargs invoke(int code, U target, Varargs varargs) {
39+
final int optcode = code - super.getAllFunctionNames().size();
40+
switch (optcode) {
41+
case 0:
42+
return acrRecognizeCallback(target, varargs);
43+
case 1:
44+
return startAcrRecognize(target, varargs);
45+
case 2:
46+
return stopAcrRecognize(target, varargs);
47+
}
48+
return super.invoke(code, target, varargs);
49+
}
50+
51+
public LuaValue acrRecognizeCallback(U target, Varargs varargs) {
52+
final LuaFunction callback = varargs.optfunction(2, null);
53+
if (callback != null && callback.isfunction()) {
54+
return target.setMqttCallback(callback);
55+
}
56+
return LuaValue.NIL;
57+
}
58+
59+
public LuaValue startAcrRecognize(U target, Varargs args) {
60+
try {
61+
if (args.narg() > 0) {
62+
String filePath = LuaUtil.getString(args, 2);
63+
if (TextUtils.isEmpty(filePath)) {
64+
VenvyLog.e(VenvyAcrCloudMapper.class.getName(), new NullPointerException("startAcrRecognize is error,because recognize url is null"));
65+
return LuaValue.NIL;
66+
}
67+
File file=new File(filePath);
68+
if(!file.exists()||!file.isFile()){
69+
VenvyLog.e(VenvyAcrCloudMapper.class.getName(), new NullPointerException("startAcrRecognize is error,because recognize url is null"));
70+
return LuaValue.NIL;
71+
}
72+
target.startRecognize(VenvyFileUtil.readBytes(file));
73+
}
74+
} catch (Exception e) {
75+
VenvyLog.e(VenvyAcrCloudMapper.class.getName(), e);
76+
}
77+
return LuaValue.NIL;
78+
}
79+
80+
public LuaValue stopAcrRecognize(U target, Varargs args) {
81+
try {
82+
target.stopRecognize();
83+
} catch (Exception e) {
84+
VenvyLog.e(VenvyAcrCloudMapper.class.getName(), e);
85+
}
86+
return LuaValue.NIL;
87+
}
88+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package cn.com.venvy.lua.ud;
2+
3+
import android.os.Bundle;
4+
import android.text.TextUtils;
5+
6+
import com.taobao.luaview.userdata.ui.UDView;
7+
import com.taobao.luaview.util.JsonUtil;
8+
import com.taobao.luaview.util.LuaUtil;
9+
10+
import org.luaj.vm2.Globals;
11+
import org.luaj.vm2.LuaValue;
12+
import org.luaj.vm2.Varargs;
13+
14+
import cn.com.venvy.common.acr.VenvyACRCloudFactory;
15+
import cn.com.venvy.common.interf.IACRCloud;
16+
import cn.com.venvy.lua.view.VenvyLVAcrClouldCallback;
17+
18+
/**
19+
* Created by lgf on 2020/2/26.
20+
*/
21+
22+
public class VenvyUDAcrClouldCallback extends UDView<VenvyLVAcrClouldCallback> {
23+
private IACRCloud mAcrCloud;
24+
LuaValue mAcrCloudCallback;
25+
26+
public VenvyUDAcrClouldCallback(VenvyLVAcrClouldCallback view, Globals globals, LuaValue metatable, Varargs initParams) {
27+
super(view, globals, metatable, initParams);
28+
mAcrCloud = VenvyACRCloudFactory.getACRCloud();
29+
}
30+
31+
public VenvyUDAcrClouldCallback setAcrCloudCallback(LuaValue callbacks) {
32+
if (callbacks != null) {
33+
mAcrCloudCallback = callbacks;
34+
}
35+
return this;
36+
}
37+
38+
public void handleAcrMessageBundle(Bundle bundle) {
39+
if (bundle == null) {
40+
return;
41+
}
42+
String message = bundle.getString("data");
43+
if (TextUtils.isEmpty(message)) {
44+
return;
45+
}
46+
LuaUtil.callFunction(mMqttCallback, JsonUtil.toLuaTable(message));
47+
}
48+
49+
public void startRecognize(byte[] buffer) {
50+
if (mAcrCloud != null) {
51+
mAcrCloud.startRecognize(buffer);
52+
}
53+
}
54+
55+
public void stopRecognize() {
56+
if (mAcrCloud != null) {
57+
mAcrCloud.stopRecognize();
58+
}
59+
}
60+
61+
public void destroyRecognize() {
62+
if (mAcrCloud != null) {
63+
mAcrCloud.destroyRecognize();
64+
}
65+
}
66+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package cn.com.venvy.lua.view;
2+
3+
import android.os.Bundle;
4+
import android.view.View;
5+
6+
import com.taobao.luaview.userdata.ui.UDView;
7+
import com.taobao.luaview.view.interfaces.ILVView;
8+
9+
import org.luaj.vm2.Globals;
10+
import org.luaj.vm2.LuaValue;
11+
import org.luaj.vm2.Varargs;
12+
13+
import cn.com.venvy.common.observer.ObservableManager;
14+
import cn.com.venvy.common.observer.VenvyObservable;
15+
import cn.com.venvy.common.observer.VenvyObserver;
16+
import cn.com.venvy.lua.ud.VenvyUDAcrClouldCallback;
17+
18+
import static cn.com.venvy.common.observer.VenvyObservableTarget.TAG_ACR_DATA_MESSAGE;
19+
20+
/**
21+
* Created by lgf on 2020/2/26.
22+
*/
23+
24+
public class VenvyLVAcrClouldCallback extends View implements ILVView, VenvyObserver {
25+
private VenvyUDAcrClouldCallback mLuaUserdata;
26+
27+
public VenvyLVAcrClouldCallback(Globals globals, LuaValue metaTable, Varargs varargs) {
28+
super(globals.getContext());
29+
this.mLuaUserdata = new VenvyUDAcrClouldCallback(this, globals, metaTable, varargs);
30+
}
31+
32+
@Override
33+
protected void onAttachedToWindow() {
34+
super.onAttachedToWindow();
35+
ObservableManager.getDefaultObserable().addObserver(TAG_ACR_DATA_MESSAGE, this);
36+
}
37+
38+
@Override
39+
public UDView getUserdata() {
40+
return mLuaUserdata;
41+
}
42+
43+
@Override
44+
protected void onDetachedFromWindow() {
45+
super.onDetachedFromWindow();
46+
if (mLuaUserdata != null) {
47+
mLuaUserdata.destroyRecognize();
48+
}
49+
ObservableManager.getDefaultObserable().removeObserver(TAG_ACR_DATA_MESSAGE, this);
50+
}
51+
52+
@Override
53+
public void notifyChanged(VenvyObservable observable, String tag, Bundle bundle) {
54+
if(mLuaUserdata!=null){
55+
mLuaUserdata.handleAcrMessageBundle(bundle);
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)