8000 request update: LuaView for 插件首页 · pythonzz/LuaViewSDK@defacaa · GitHub
[go: up one dir, main page]

Skip to content

Commit defacaa

Browse files
author
野松
committed
request update: LuaView for 插件首页
1 parent 81dee1b commit defacaa

File tree

8 files changed

+74
-26
lines changed

8 files changed

+74
-26
lines changed
622 Bytes
Binary file not shown.

Android/LuaViewDemo/assets/test/UI_CollectionView.lua

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,28 @@ local cellData = function(_, key)
5555
return {
5656
Size = function(section, row)
5757
print("Size", section, row, 10 + row * 2)
58-
return 10 + row * 2
58+
-- return 10 + row * 2
59+
return s_width, 200
5960
end,
6061
Init = function(cell, section, row) -- 初始化cell
6162
print("Init", section, row, 10 + row * 2)
6263
cell.title = Label();
6364
cell.title.backgroundColor(section == 1 and 0xff0000 or 0x00ff00)
65+
cell.title.lines(10)
66+
cell.title.maxLine(10)
6467
end,
6568
Layout = function(cell, section, row) -- cell复用时调用
6669
print("Layout", section, row, 10 + row * 2)
67-
cell.title.frame(0, 0, s_width, 10 + row * 2)
68-
cell.title.text(section .. '--' .. row)
70+
-- cell.title.frame(0, 0, s_width, 10 + row * 2)
71+
72+
cell.title.text("sfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfssfadfs")
73+
74+
print("cell-size 11", cell.title.height())
75+
76+
cell.title.adjustSize()
77+
78+
print("cell-size 22", cell.title.height())
79+
6980
end,
7081
Callback = function(cell, section, row) -- 用户点击了section和row
7182
Toast("Section " .. section .. ", Row " .. row);

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,28 @@ private void extendsLuaView(final LuaView luaView) {
109109
* 加载数据
110110
*/
111111
public void loadScript(final LuaView luaView) {
112-
luaView.load(getLuaUri(), new LuaScriptLoader.ScriptExecuteCallback() {
112+
// luaView.load(getLuaUri(), new LuaScriptLoader.ScriptExecuteCallback() {
113+
// @Override
114+
// public boolean onScriptPrepared(ScriptBundle bundle) {
115+
// return false;
116+
// }
117+
//
118+
// @Override
119+
// public boolean onScriptCompiled(LuaValue value, LuaValue context, LuaValue view) {
120+
// return false;
121+
// }
122+
//
123+
// @Override
124+
// public void onScriptExecuted(String uri, boolean executedSuccess) {
125+
// //测试调用 lua function
126+
// LogUtil.d("call-lua-function return:", luaView.callLuaFunction("global_fun_test1", 1, "a", 0.1));
127+
// LogUtil.d("call-lua-function return:", JsonUtil.toString(luaView.callLuaFunction("global_fun_test2", 2, "b", 0.2)));
128+
// LogUtil.d("call-window-function return:", luaView.callWindowFunction("window_fun1", 3, "c", 0.3));
129+
// LogUtil.d("call-window-function return:", luaView.callWindowFunction("window_fun2", 4, "d", 0.4));
130+
// }
131+
// });
132+
133+
luaView.load("file:///android_asset/luaview/test.szip", new LuaScriptLoader.ScriptExecuteCallback() {
113134
@Override
114135
public boolean onScriptPrepared(ScriptBundle bundle) {
115136
return false;
@@ -122,11 +143,7 @@ public boolean onScriptCompiled(LuaValue value, LuaValue context, LuaValue view)
122143

123144
@Override
124145
public void onScriptExecuted(String uri, boolean executedSuccess) {
125-
//测试调用 lua function
126-
LogUtil.d("call-lua-function return:", luaView.callLuaFunction("global_fun_test1", 1, "a", 0.1));
127-
LogUtil.d("call-lua-function return:", JsonUtil.toString(luaView.callLuaFunction("global_fun_test2", 2, "b", 0.2)));
128-
LogUtil.d("call-window-function return:", luaView.callWindowFunction("window_fun1", 3, "c", 0.3));
129-
LogUtil.d("call-window-function return:", luaView.callWindowFunction("window_fun2", 4, "d", 0.4));
146+
130147
}
131148
});
132149
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public class Constants {
2424
public static final String PARAM_URI = "uri";
2525
public static final String PAGE_PARAMS = "page_params";
2626

27+
// asset file prefix for loading luaview.load(ASSET_FILE_PREFIX + "foler/name")
28+
public static final String ASSET_FILE_PREFIX = "file:///android_asset/";
29+
2730
//Bundle encrypt and decrypt
2831
public static final String PUBLIC_KEY_PATH = "luaview/luaview_rsa_public_key.der";
2932
public static final String PUBLIC_KEY_PATH_MD5 = "luaview/luaview_rsa_public_key.der-md5";

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ public void load(final String url, final ScriptLoaderCallback callback) {
154154
public void load(final String url, final String sha256, final ScriptLoaderCallback callback) {
155155
new ScriptBundleUltimateLoadTask(mContext, callback).load(url, sha256);
156156
}
157+
157158

158159
//--------------------------------preload script------------------------------------------------
159160

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public LuaViewCore load(final String urlOrFileOrScript, final String sha256) {
159159

160160
public LuaViewCore load(final String urlOrFileOrScript, final String sha256, final LuaScriptLoader.ScriptExecuteCallback callback) {
161161
if (!TextUtils.isEmpty(urlOrFileOrScript)) {
162-
if (URLUtil.isNetworkUrl(urlOrFileOrScript)) {//url, http:// or https://
162+
if (URLUtil.isNetworkUrl(urlOrFileOrScript) || URLUtil.isAssetUrl(urlOrFileOrScript)) {//url, http:// or https:// or asset://
163163
loadUrl(urlOrFileOrScript, sha256, callback);
164164
} else {
165165
loadFile(urlOrFileOrScript, callback);

Android/LuaViewSDK/src/com/taobao/luaview/scriptbundle/asynctask/ScriptBundleUltimateLoadTask.java

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

1111
import android.content.Context;
1212
import android.support.annotation.Nullable;
13+
import android.webkit.URLUtil;
1314

1415
import com.taobao.luaview.cache.AppCache;
1516
import com.taobao.luaview.global.LuaScriptLoader;
@@ -18,6 +19,7 @@
1819
import com.taobao.luaview.scriptbundle.asynctask.delegate.ScriptBundleDownloadDelegate;
1920
import com.taobao.luaview.scriptbundle.asynctask.delegate.ScriptBundleLoadDelegate;
2021
import com.taobao.luaview.scriptbundle.asynctask.delegate.ScriptBundleUnpackDelegate;
22+
import com.taobao.luaview.util.AssetUtil;
2123
import com.taobao.luaview.util.DebugUtil;
2224
import com.taobao.luaview.util.FileUtil;
2325

@@ -64,37 +66,41 @@ public void loadAsset(String... params) {
6466
public ScriptBundle doInBackground(String... params) {
6567
DebugUtil.tsi("luaviewp-ScriptBundleUltimateLoadTask");
6668

67-
final String url = params[0];
68-
final String destFolderPath = LuaScriptManager.buildScriptBundleFolderPath(url);
69+
final String urlOrAssetPath = params[0];
70+
final String destFolderPath = LuaScriptManager.buildScriptBundleFolderPath(urlOrAssetPath);
6971
final String sha256 = params.length > 1 ? params[1] : null;
7072

7173
ScriptBundle scriptBundle = null;
7274

73-
if (LuaScriptManager.existsScriptBundle(url)) {//读取并加载,之前的脚本
74-
scriptBundle = AppCache.getCache(AppCache.CACHE_SCRIPTS).getLru(url);
75+
if (LuaScriptManager.existsScriptBundle(urlOrAssetPath)) {//读取并加载,之前的脚本
76+
scriptBundle = AppCache.getCache(AppCache.CACHE_SCRIPTS).getLru(urlOrAssetPath);
7577
if (scriptBundle != null) {
7678
return scriptBundle;
7779
} else {
7880
DebugUtil.tsi("luaviewp-loadBundle");
7981

80-
scriptBundle = ScriptBundleUnpackDelegate.loadBundle(LuaScriptManager.isLuaBytecodeUrl(url), url, destFolderPath);//TODO 性能瓶颈
82+
scriptBundle = ScriptBundleUnpackDelegate.loadBundle(LuaScriptManager.isLuaBytecodeUrl(urlOrAssetPath), urlOrAssetPath, destFolderPath);//TODO 性能瓶颈
8183

8284
DebugUtil.tei("luaviewp-loadBundle");
8385
}
8486

85-
} else if (LuaScriptManager.existsPredownloadBundle(url)) {//预先加载的地址有脚本,则尝试解压并加载 xxx.zip
86-
scriptBundle = AppCache.getCache(AppCache.CACHE_SCRIPTS).getLru(url);
87+
} else if (LuaScriptManager.existsPredownloadBundle(urlOrAssetPath)) {//预先加载的地址有脚本,则尝试解压并加载 xxx.zip
88+
scriptBundle = AppCache.getCache(AppCache.CACHE_SCRIPTS).getLru(urlOrAssetPath);
8789
if (scriptBundle != null) {
8890
return scriptBundle;
8991
} else {
9092
DebugUtil.tsi("luaviewp-loadPredownloadBundle");
9193

92-
String uri = LuaScriptManager.buildPredownloadScriptBundleFilePath(url);
94+
String uri = LuaScriptManager.buildPredownloadScriptBundleFilePath(urlOrAssetPath);
9395
InputStream inputStream = FileUtil.open(uri);
94-
scriptBundle = ScriptBundleUnpackDelegate.unpack(url, inputStream);
96+
scriptBundle = ScriptBundleUnpackDelegate.unpack(urlOrAssetPath, inputStream);
9597

9698
DebugUtil.tei("luaviewp-loadPredownloadBundle");
9799
}
100+
} else if (URLUtil.isAssetUrl(urlOrAssetPath) && AssetUtil.exists(mContext, urlOrAssetPath)) {//asset file exists
101+
if (LuaScriptManager.isLuaScriptZip(urlOrAssetPath)) {//asset下的包加载
102+
scriptBundle = ScriptBundleUnpackDelegate.unpack(mContext, FileUtil.removePostfix(urlOrAssetPath), urlOrAssetPath);
103+
}
98104
} else {//下载解压加载
99105

100106
// Assert 预置包
@@ -104,12 +110,12 @@ public ScriptBundle doInBackground(String... params) {
104110
callLoaderDownloadStart();
105111

106112
// download
107-
ScriptBundleDownloadDelegate downloadDelegate = new ScriptBundleDownloadDelegate(url, sha256);
113+
ScriptBundleDownloadDelegate downloadDelegate = new ScriptBundleDownloadDelegate(urlOrAssetPath, sha256);
108114
HttpURLConnection connection = downloadDelegate.createHttpUrlConnection();
109115
InputStream inputStream = downloadDelegate.downloadAsStream(connection);
110116

111117
if (inputStream != null) {
112-
scriptBundle = ScriptBundleUnpackDelegate.unpack(url, inputStream);//unpack
118+
scriptBundle = ScriptBundleUnpackDelegate.unpack(urlOrAssetPath, inputStream);//unpack
113119
}
114120

115121
if (connection != null) {
@@ -124,13 +130,13 @@ public ScriptBundle doInBackground(String... params) {
124130

125131
if (scriptBundle != null) {
126132

127-
if (url != null) {
128-
scriptBundle.setUrl(url);
133+
if (urlOrAssetPath != null) {
134+
scriptBundle.setUrl(urlOrAssetPath);
129135
scriptBundle.setBaseFilePath(destFolderPath);
130136
}
131137

132138
//cache
133-
AppCache.getCache(AppCache.CACHE_SCRIPTS).putLru(url != null ? url : scriptBundle.getUrl(), scriptBundle);
139+
AppCache.getCache(AppCache.CACHE_SCRIPTS).putLru(urlOrAssetPath != null ? urlOrAssetPath : scriptBundle.getUrl(), scriptBundle);
134140
}
135141

136142
return scriptBundle;

Android/LuaViewSDK/src/com/taobao/luaview/util/AssetUtil.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
package com.taobao.luaview.util;
1010

1111
import android.content.Context;
12+
import android.webkit.URLUtil;
13+
14+
import com.taobao.luaview.global.Constants;
1215

1316
import java.io.IOException;
1417
import java.io.InputStream;
@@ -20,6 +23,13 @@
2023
*/
2124
public class AssetUtil {
2225

26+
private static String getAssetFilePathWithoutPrefix(final String assetFilePath){
27+
if(assetFilePath != null && assetFilePath.startsWith(Constants.ASSET_FILE_PREFIX)){//如果带前缀,则剔除前缀
28+
return assetFilePath.substring(Constants.ASSET_FILE_PREFIX.length());
29+
}
30+
return assetFilePath;
31+
}
32+
2333
/**
2434
* check asset exists
2535
*
@@ -30,7 +40,7 @@ public class AssetUtil {
3040
public static boolean exists(final Context context, final String assetFilePath) {
3141
boolean bAssetOk = false;
3242
try {
33-
InputStream stream = context.getAssets().open(assetFilePath);
43+
InputStream stream = context.getAssets().open(getAssetFilePathWithoutPrefix(assetFilePath));
3444
stream.close();
3545
bAssetOk = true;
3646
} catch (Exception e) {
@@ -47,7 +57,7 @@ public static boolean exists(final Context context, final String assetFilePath)
4757
*/
4858
public static InputStream open(final Context context, final String assetFilePath) {
4959
try {
50-
return context.getAssets().open(assetFilePath);
60+
return context.getAssets().open(getAssetFilePathWithoutPrefix(assetFilePath));
5161
} catch (IOException e) {
5262
return null;
5363
}

0 commit comments

Comments
 (0)
0