Skip to content

Commit 0a5cf55

Browse files
author
野松
committed
request update: LuaView for tao首页
1 parent 2dc3e6b commit 0a5cf55

File tree

10 files changed

+306
-14
lines changed

10 files changed

+306
-14
lines changed

Android/LuaViewDemo/LuaViewDemo.iml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,5 @@
101101
<orderEntry type="library" exported="" name="support-v4-23.0.1" level="project" />
102102
<orderEntry type="library" exported="" name="glide-3.7.0" level="project" />
103103
<orderEntry type="module" module-name="LuaViewSDK" exported="" />
104-
<orderEntry type="library" exported="" name="support-annotations-23.0.1" level="project" />
105104
</component>
106105
</module>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
-- Created by LuaView.
2+
-- Copyright (c) 2017, Alibaba Group. All rights reserved.
3+
--
4+
-- This source code is licensed under the MIT.
5+
-- For the full copyright and license information,please view the LICENSE file in the root directory of this source tree.
6+
7+
-- Created by LuaView.
8+
-- Copyright (c) 2017, Alibaba Group. All rights reserved.
9+
--
10+
-- This source code is licensed under the MIT.
11+
-- For the full copyright and license information,please view the LICENSE file in the root directory of this source tree.
12+
13+
code = [[
14+
a = {}
15+
a.x = function()
16+
local btn = Button()
17+
btn:frame(0, 0, 100, 100)
18+
btn:text("button")
19+
-- btn:callback(function
20+
-- Toast("btn clicked")
21+
-- end)
22+
end
23+
24+
a.x()
25+
]]
26+
27+
28+
--hi = load(code)
29+
--hi()
30+
31+
32+
33+
load("local btn = Button(); btn:text('yyy'); btn:callback(function Toast('x') end)")()
34+
35+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
-- 活动指示器
2+
local SCREEN_WIDTH = System:screenSize()
3+
4+
local scrollView = HScrollView()
5+
6+
7+
for i = 0, 10, 1 do
8+
local btn = Label()
9+
btn:text("BTN" .. i)
10+
btn:frame(i * 60, 10, 59, 80)
11+
btn:backgroundColor(0xff00ff00)
12+
scrollView:addView(btn)
13+
end
14+
15+
scrollView:xy(0, 300)
16+
scrollView:size(SCREEN_WIDTH, 100)
17+
scrollView:backgroundColor(0xffcccccc)
18+
scrollView:showScrollIndicator(false)
19+
20+
21+
scrollView:callback({
22+
Scrolling = function(x, y)
23+
print("Scrolling", x, y)
24+
print(scrollView:offset())
25+
end
26+
})

Android/LuaViewDemo/src/com/taobao/luaview/demo/activity/DemoActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
public class DemoActivity extends ListActivity {
2727
//测试标准语法
28-
public static boolean useStandardSyntax = false;
28+
public static boolean useStandardSyntax = true;
2929

3030
//非标准语法代码路径
3131
private static final String FOLDER_NAME = "test";

Android/LuaViewSDK/src/com/taobao/luaview/global/LuaScriptLoader.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ public interface ScriptLoaderCallback {
4545
void onScriptLoaded(final ScriptBundle bundle);//脚本加载
4646
}
4747

48+
public interface ScriptLoaderCallback2 extends ScriptLoaderCallback {
49+
void onScriptDownloadStart();//下载开始
50+
51+
void onScriptDownloadEnd(final ScriptBundle bundle);//包内设置的脚本加载
52+
}
53+
4854
/**
4955
* 脚本运行回调
5056
*/
@@ -74,6 +80,26 @@ public interface ScriptExecuteCallback {
7480
void onScriptExecuted(String uri, boolean executedSuccess);
7581
}
7682

83+
/**
84+
* 脚本运行回调
85+
*/
86+
public interface ScriptExecuteCallback2 extends ScriptExecuteCallback {
87+
88+
/**
89+
* 脚本下载开始
90+
*/
91+
void onScriptDownloadStart();
92+
93+
/**
94+
* 脚本下载完毕
95+
*
96+
* @param bundle
97+
* @return
98+
*/
99+
void onScriptDownloadEnd(ScriptBundle bundle);
100+
101+
}
102+
77103
//-----------------------------------------static methods---------------------------------------
78104

79105
/**
@@ -129,7 +155,6 @@ public void load(final String url, final String sha256, final ScriptLoaderCallba
129155
new ScriptBundleUltimateLoadTask(mContext, callback).load(url, sha256);
130156
}
131157

132-
133158
//--------------------------------preload script------------------------------------------------
134159

135160
/**

Android/LuaViewSDK/src/com/taobao/luaview/global/LuaViewCore.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,35 @@ public LuaViewCore loadUrl(final String url, final String sha256) {
181181
return loadUrl(url, sha256, null);
182182
}
183183

184+
185+
/**
186+
* load url
187+
*
188+
* @param url
189+
* @param sha256
190+
* @param callback
191+
* @return
192+
*/
184193
public LuaViewCore loadUrl(final String url, final String sha256, final LuaScriptLoader.ScriptExecuteCallback callback) {
185194
updateUri(url);
186195
if (!TextUtils.isEmpty(url)) {
187-
new LuaScriptLoader(mContext).load(url, sha256, new LuaScriptLoader.ScriptLoaderCallback() {
196+
new LuaScriptLoader(mContext).load(url, sha256, new LuaScriptLoader.ScriptLoaderCallback2() {
197+
@Override
198+
public void onScriptDownloadStart() {//下载开始,不一定会调用,存在调用失败的情况
199+
if (callback instanceof LuaScriptLoader.ScriptExecuteCallback2) {
200+
((LuaScriptLoader.ScriptExecuteCallback2) callback).onScriptDownloadStart();
201+
}
202+
}
203+
204+
@Override
205+
public void onScriptDownloadEnd(ScriptBundle bundle) {//下载完成,不一定会调用,如果调用的话,且packageName不为空的话,则onScriptLoaded加载的是Asset预置脚本
206+
if (callback instanceof LuaScriptLoader.ScriptExecuteCallback2) {
207+
((LuaScriptLoader.ScriptExecuteCallback2) callback).onScriptDownloadEnd(bundle);
208+
}
209+
}
210+
188211
@Override
189-
public void onScriptLoaded(ScriptBundle bundle) {
212+
public void onScriptLoaded(ScriptBundle bundle) {//脚本本地 or Asset or 网络 准备成功
190213
if (callback == null || callback.onScriptPrepared(bundle) == false) {//脚本准备完成,且不第三方自己执行
191214
loadScriptBundle(bundle, callback);
192215
} else if (callback != null) {

Android/LuaViewSDK/src/com/taobao/luaview/scriptbundle/LuaScriptManager.java

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class LuaScriptManager {
3030
//folders
3131
private static final String PACKAGE_NAME_DEFAULT = "luaview";
3232
public static final String FOLDER_SCRIPT = "script";
33+
public static final String FOLDER_PRE_DOWNLOAD = "predownload";
3334

3435
//默认缓存文件的后缀
3536
public static final String POSTFIX_SCRIPT_BUNDLE = ".lvraw";
@@ -56,7 +57,8 @@ public static void init(final Context context) {
5657
initInternalFilePath(context);
5758
} else {//测试环境优先使用sd卡路径
5859
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
59-
PACKAGE_NAME = context.getPackageName();
60+
// PACKAGE_NAME = context.getPackageName();//改成luaview
61+
PACKAGE_NAME = PACKAGE_NAME_DEFAULT;
6062
BASE_FILECACHE_PATH = context.getExternalCacheDir() + File.separator;
6163
} else {
6264
initInternalFilePath(context);
@@ -84,7 +86,7 @@ private static void initInternalFilePath(Context context) {
8486
//--------------------------------static methods for get file path------------------------------
8587

8688
/**
87-
* 获取基础文件路劲
89+
* 获取基础文件路径
8890
*
8991
* @return
9092
*/
@@ -101,6 +103,15 @@ public static String getBaseScriptFolderPath() {
101103
return BASE_FILECACHE_PATH + PACKAGE_NAME + File.separator + FOLDER_SCRIPT + File.separator;
102104
}
103105

106+
/**
107+
* get predownload folder path
108+
*
109+
* @return
110+
*/
111+
public static String getBasePredownloadFolderPath() {
112+
return BASE_FILECACHE_PATH + PACKAGE_NAME + File.separator + FOLDER_PRE_DOWNLOAD + File.separator;
113+
}
114+
104115
/**
105116
* get path of given folder
106117
*
@@ -129,6 +140,19 @@ public static String getFilePath(final String subFolderName, final String fileNa
129140
.toString();
130141
}
131142

143+
/**
144+
* get pre downloaded file path
145+
*
146+
* @param fileNameWithPostfix
147+
* @return
148+
*/
149+
public static String getPredownloadFilePath(String fileNameWithPostfix) {
150+
return new StringBuffer()
151+
.append(getBasePredownloadFolderPath())
152+
.append(fileNameWithPostfix).toString();
153+
}
154+
155+
132156
/**
133157
* 构建文件名称
134158
*
@@ -178,6 +202,22 @@ public static String buildScriptBundleFilePath(final String uri) {
178202
return getFilePath(folderName, fileName);
179203
}
180204

205+
206+
/**
207+
* 构建预下载脚本文件名(这里直接使用脚本地址,不做任何修改)
208+
*
209+
* @param uri
210+
* @return
211+
*/
212+
public static String buildPredownloadScriptBundleFilePath(final String uri) {
213+
if(uri != null){
214+
String fileName = uri.substring(uri.lastIndexOf('/') + 1);//只取最后的文件名
215+
return getPredownloadFilePath(fileName);
216+
} else {
217+
return null;
218+
}
219+
}
220+
181221
//------------------------------------------exists----------------------------------------------
182222

183223
/**
@@ -193,6 +233,19 @@ public static boolean existsScriptBundle(final String uri) {
193233
return false;
194234
}
195235

236+
/**
237+
* 预下载目录下的某个文件是否存在
238+
*
239+
* @param uri
240+
* @return
241+
*/
242+
public static boolean existsPredownloadBundle(final String uri) {
243+
if (!TextUtils.isEmpty(uri)) {
244+
return FileUtil.exists(buildPredownloadScriptBundleFilePath(uri));
245+
}
246+
return false;
247+
}
248+
196249
//--------------------------------------------判断函数-------------------------------------------
197250

198251
/**
@@ -241,6 +294,17 @@ public static boolean isLuaStandardSyntaxUrl(final String url) {
241294
return FileUtil.isSuffix(url, LuaScriptManager.POSTFIX_LV_STANDARD_SYNTAX_ZIP);
242295
}
243296

297+
/**
298+
* 是否是非标准语法的 zip包
299+
*
300+
* @param url
301+
* @return
302+
*/
303+
public static boolean isLuaUnStandardSyntaxUrl(final String url) {
304+
return FileUtil.isSuffix(url, LuaScriptManager.POSTFIX_LV_ZIP);//只有zip包是非标准包,bzip、szip都认为是标准,bzip有问题么?
305+
}
306+
307+
244308
/**
245309
* 是否是lua加密脚本
246310
*

0 commit comments

Comments
 (0)