11package com .taobao .luaview .userdata .kit ;
22
33import android .os .Bundle ;
4+ import android .text .TextUtils ;
45import android .util .Pair ;
56
67import com .taobao .luaview .fun .mapper .LuaViewLib ;
78import com .taobao .luaview .userdata .base .BaseLuaTable ;
9+ import com .taobao .luaview .util .JsonUtil ;
10+ import com .taobao .luaview .util .LuaUtil ;
811import com .taobao .luaview .util .VisionUtil ;
912
13+ import org .json .JSONObject ;
1014import org .luaj .vm2 .Globals ;
15+ import org .luaj .vm2 .LuaTable ;
1116import org .luaj .vm2 .LuaValue ;
1217import org .luaj .vm2 .Varargs ;
1318import org .luaj .vm2 .lib .VarArgFunction ;
1419
20+ import cn .com .venvy .CacheConstants ;
1521import cn .com .venvy .Platform ;
22+ import cn .com .venvy .common .bean .WidgetInfo ;
1623import cn .com .venvy .common .observer .ObservableManager ;
1724import cn .com .venvy .common .observer .VenvyObservableTarget ;
1825import cn .com .venvy .lua .binder .VenvyLVLibBinder ;
@@ -37,6 +44,9 @@ public UDApplet(Globals globals, LuaValue metatable, Platform platform) {
3744 set ("canGoBack" , new CanGoBack (platform ));// 是否能够返回上一页
3845 set ("goBack" , new GoBack (platform ));// 返回上一页
3946 set ("closeView" , new CloseView (platform ));// 关闭当前容器
47+ set ("setStorageData" , new SetStorageData (platform ));// 存储本地数据
48+ set ("getStorageData" , new GetStorageData (platform ));// 获取本地储存的数据
49+ set ("openAds" , new OpenAds (platform ));// 获取本地储存的数据
4050 }
4151
4252 class AppletSize extends VarArgFunction {
@@ -145,4 +155,110 @@ public Varargs invoke(Varargs args) {
145155 }
146156 }
147157
158+
159+ class SetStorageData extends VarArgFunction {
160+ private Platform platform ;
161+
162+ public SetStorageData (Platform platform ) {
163+ super ();
164+ this .platform = platform ;
165+ }
166+
167+ @ Override
168+ public Varargs invoke (Varargs args ) {
169+ int fixIndex = VenvyLVLibBinder .fixIndex (args );
170+ LuaValue key = args .arg (fixIndex + 2 ); // key
171+ LuaValue value = args .arg (fixIndex + 3 ); // value
172+ LuaValue fileName = args .arg (fixIndex + 4 ); // fileName
173+ CacheConstants .putCacheByFileName (platform .getContentViewGroup ().getContext (), luaValueToString (fileName ),
174+ luaValueToString (key ), luaValueToString (value ));
175+ return LuaValue .NIL ;
176+ }
177+
178+ }
179+
180+ class GetStorageData extends VarArgFunction {
181+
182+ private Platform platform ;
183+
184+ public GetStorageData (Platform platform ) {
185+ super ();
186+ this .platform = platform ;
187+ }
188+
189+ @ Override
190+ public Varargs invoke (Varargs args ) {
191+ int fixIndex = VenvyLVLibBinder .fixIndex (args );
192+ LuaValue key = args .arg (fixIndex + 2 ); // key
193+ LuaValue fileName = args .arg (fixIndex + 3 ); // file sp 文件名
194+ String fileNameStr = luaValueToString (fileName );
195+ if (TextUtils .isEmpty (fileNameStr )) {
196+ // 没有传文件名,默认查询当前开发者ID对应的缓存数据
197+ String data = CacheConstants .getCacheByDeveloperId (platform .getContentViewGroup ().getContext (), luaValueToString (key ));
198+ return LuaValue .valueOf (data );
199+ } else {
200+ String jsonStr = CacheConstants .getVisionProgramId (platform .getContentViewGroup ().getContext (), fileNameStr );
201+ return LuaValue .valueOf (jsonStr );
202+ }
203+ }
204+ }
205+
206+ class OpenAds extends VarArgFunction {
207+ private Platform platform ;
208+
209+ public OpenAds (Platform platform ) {
210+ super ();
211+ this .platform = platform ;
212+ }
213+
214+ @ Override
215+ public Varargs invoke (Varargs args ) {
216+ int fixIndex = VenvyLVLibBinder .fixIndex (args );
217+ if (args .narg () > fixIndex ) {
218+ final LuaTable table = LuaUtil .getTable (args , fixIndex + 2 );
219+ try {
220+ String str = JsonUtil .toString (table );
221+ JSONObject jsonObject = new JSONObject (str );
222+ // VenvyLog.d("openAds : " + jsonObject.toString());
223+ if (jsonObject .has ("targetType" )) {
224+ String targetType = jsonObject .optString ("targetType" );
225+ JSONObject linkData = jsonObject .optJSONObject ("linkData" );
226+ String downAPI = linkData .optString ("linkUrl" );
227+ String deepLink = linkData .optString ("deepLink" );
228+ // targetType 1 落地页 2 deepLink 3 下载
229+ if (targetType .equalsIgnoreCase ("3" )) {
230+ JSONObject downloadTrackLink = jsonObject .optJSONObject ("downloadTrackLink" );
231+ Bundle trackData = new Bundle ();
232+ trackData .putString (VenvyObservableTarget .Constant .CONSTANT_DOWNLOAD_API , downAPI );
233+ trackData .putStringArray ("isTrackLinks" , JsonUtil .toStringArray (downloadTrackLink .optJSONArray ("isTrackLinks" )));
234+ trackData .putStringArray ("dsTrackLinks" , JsonUtil .toStringArray (downloadTrackLink .optJSONArray ("dsTrackLinks" )));
235+ trackData .putStringArray ("dfTrackLinks" , JsonUtil .toStringArray (downloadTrackLink .optJSONArray ("dfTrackLinks" )));
236+ trackData .putStringArray ("instTrackLinks" , JsonUtil .toStringArray (downloadTrackLink .optJSONArray ("instTrackLinks" )));
237+ ObservableManager .getDefaultObserable ().sendToTarget (VenvyObservableTarget .TAG_DOWNLOAD_TASK , trackData );
238+
239+ } else {
240+ // 走Native:widgetNotify() 逻辑
241+ WidgetInfo .Builder builder = new WidgetInfo .Builder ()
242+ .setWidgetActionType (WidgetInfo .WidgetActionType .ACTION_OPEN_URL );
243+ if (targetType .equalsIgnoreCase ("1" )) {
244+ builder .setLinkUrl (downAPI );
245+ } else if (targetType .equalsIgnoreCase ("2" )){
246+ builder .setDeepLink (deepLink );
247+ }
248+ WidgetInfo widgetInfo = builder .build ();
249+ if (platform .getWidgetClickListener () != null ) {
250+ platform .getWidgetClickListener ().onClick (widgetInfo );
251+ }
252+ }
253+ }
254+ } catch (Exception e ) {
255+ e .printStackTrace ();
256+ }
257+ }
258+ return LuaValue .NIL ;
259+ }
260+
261+
262+ }
263+
148264}
0 commit comments