Skip to content

Commit 8265d2f

Browse files
committed
PreloadZipUpdate 优化
1 parent ac3144a commit 8265d2f

File tree

1 file changed

+83
-60
lines changed

1 file changed

+83
-60
lines changed

VideoOS/VenvyLibrary/src/main/java/cn/com/venvy/PreloadZipUpdate.java

Lines changed: 83 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
import java.io.File;
1111
import java.util.ArrayList;
12+
import java.util.HashMap;
1213
import java.util.List;
14+
import java.util.Map;
1315

1416
import cn.com.venvy.common.download.DownloadTask;
1517
import cn.com.venvy.common.download.DownloadTaskRunner;
@@ -28,11 +30,12 @@
2830

2931
public class PreloadZipUpdate {
3032
private final static String TAG = PreloadZipUpdate.class.getName();
31-
private static final String LUA_CACHE_PATH = "/lua/os/cache/demo";
32-
private static final String LUA_ZIP = "/lua/os/chain.zip";
33-
private static final String LUA_ZIP_CACHE = "/lua/os/cache/demo/zip";
33+
private static final String DOWN_ZIP_PATH = "/lua/os/down/zip";
34+
private static final String UN_ZIP_PATH = "/lua/os/cache/unzip";
3435
private final String PARSE_LOCAL_ZIP = "parse_local_zip";
3536
private final String PARSE_UNZIP = "unzip";
37+
private static String NEED_DOWN_URL_KEY = "need_urls";
38+
private static String ALL_DOWN_URL_KEY = "all_urls";
3639
private DownloadTaskRunner mDownloadTaskRunner;
3740
private CacheZipUpdateCallback mUpdateCallback;
3841
private Platform mPlatform;
@@ -82,13 +85,15 @@ public void startDownloadZipFile(JSONArray luaUrls) {
8285
*/
8386
private void checkDownZipUrls(final JSONArray zipUrls) {
8487
VenvyAsyncTaskUtil.doAsyncTask(PARSE_LOCAL_ZIP, new VenvyAsyncTaskUtil.IDoAsyncTask<JSONArray,
85-
List<String>>() {
88+
Map<String, List<String>>>() {
8689
@Override
87-
public List<String> doAsyncTask(JSONArray... zips) throws Exception {
90+
public Map<String, List<String>> doAsyncTask(JSONArray... zips) throws Exception {
8891
if (zips == null || zips.length == 0) {
8992
return null;
9093
}
91-
List<String> needDownUrls = new ArrayList<>();
94+
Map<String, List<String>> mapOfUrls = new HashMap<>();
95+
List<String> listOfNeedUrls = new ArrayList<>();
96+
List<String> listOfAllUrls = new ArrayList<>();
9297
try {
9398
JSONArray jsonArray = zips[0];
9499
int len = jsonArray.length();
@@ -102,44 +107,46 @@ public List<String> doAsyncTask(JSONArray... zips) throws Exception {
102107
if (TextUtils.isEmpty(url)) {
103108
continue;
104109
}
110+
listOfAllUrls.add(url);
105111
String cacheMd5 = getFileZipEncoderByMd5(Uri.parse(url).getLastPathSegment());
106112
if (!TextUtils.equals(md5, cacheMd5)) {
107-
needDownUrls.add(url);
113+
listOfNeedUrls.add(url);
108114
}
109115
}
110116
} catch (Exception e) {
111117
e.printStackTrace();
112118
VenvyLog.i(TAG, "VideoPlusZipUpdate ——> checkDownZipUrls error:" + e.getMessage());
113119
}
114-
return needDownUrls;
120+
mapOfUrls.put(NEED_DOWN_URL_KEY, listOfNeedUrls);
121+
mapOfUrls.put(ALL_DOWN_URL_KEY, listOfAllUrls);
122+
return mapOfUrls;
115123
}
116-
}, new VenvyAsyncTaskUtil.IAsyncCallback<List<String>>() {
124+
}, new VenvyAsyncTaskUtil.IAsyncCallback<Map<String, List<String>>>() {
117125
@Override
118126
public void onPreExecute() {
119127
}
120128

121129
@Override
122-
public void onPostExecute(List<String> urls) {
130+
public void onPostExecute(Map<String, List<String>> urls) {
123131
CacheZipUpdateCallback callback = getCacheLuaUpdateCallback();
124132
if (urls == null) {
125133
if (callback != null) {
126134
callback.updateError(new Exception("update zip error,发生未知错误"));
127135
}
128136
return;
129137
}
130-
List<String> zipUrlArray = getAllZipUrls(zipUrls);
131-
if (urls.size() == 0) {
132-
List<File> zipFiles = getZipFilesWithUrl(zipUrlArray);
138+
List<String> listOfNeedUrls = urls.get(NEED_DOWN_URL_KEY);
139+
List<String> listOfAllUrls = urls.get(ALL_DOWN_URL_KEY);
140+
if (listOfNeedUrls.size() == 0) {
141+
List<File> zipFiles = getZipFilesWithUrl(listOfAllUrls);
133142
if (zipFiles == null || zipFiles.size() <= 0) {
134-
if (callback != null) {
135-
callback.updateError(new Exception("update zip error,because down urls is failed"));
136-
}
137-
return;
143+
startDownloadZipFile(listOfAllUrls, listOfAllUrls);
144+
} else {
145+
unZipAndReadData(zipFiles);
138146
}
139-
unZipAndReadData(zipFiles);
140147
return;
141148
}
142-
startDownloadZipFile(zipUrlArray, urls);
149+
startDownloadZipFile(listOfAllUrls, listOfNeedUrls);
143150
}
144151

145152
@Override
@@ -165,58 +172,58 @@ private void unZipAndReadData(final List<File> zipFiles) {
165172
}
166173
return;
167174
}
168-
VenvyAsyncTaskUtil.doAsyncTask(PARSE_UNZIP, new VenvyAsyncTaskUtil.IDoAsyncTask<File, List<String>>() {
175+
VenvyAsyncTaskUtil.doAsyncTask(PARSE_UNZIP, new VenvyAsyncTaskUtil.IDoAsyncTask<File, JSONArray>() {
169176

170177
@Override
171-
public List<String> doAsyncTask(File... files) throws Exception {
172-
List<String> cacheUrls = new ArrayList<>();
173-
VenvyFileUtil.createDir(VenvyFileUtil.getCachePath(App.getContext()) + LUA_ZIP_CACHE);
178+
public JSONArray doAsyncTask(File... files) throws Exception {
179+
final JSONArray queryArray = new JSONArray();
180+
final String unZipPath = getUnZipAbsolutePath();
181+
VenvyFileUtil.createDir(unZipPath);
174182
for (File file : files) {
175183
final String cacheUrlPath = file.getAbsolutePath();
176-
VenvyGzipUtil.unzipFile(cacheUrlPath, VenvyFileUtil.getCachePath(App.getContext()) + LUA_ZIP_CACHE, false);
184+
VenvyGzipUtil.unzipFile(cacheUrlPath, unZipPath, false);
185+
}
186+
List<String> listOfUnZipJson = VenvyFileUtil.getFileName(unZipPath);
187+
for (String cacheUrlPath : listOfUnZipJson) {
188+
String fileName = Uri.parse(cacheUrlPath).getLastPathSegment();
189+
if (TextUtils.isEmpty(fileName)) {
190+
continue;
191+
}
192+
File file = new File(unZipPath, fileName);
193+
if (!file.exists() || !file.isFile()) {
194+
continue;
195+
}
196+
String queryChainData = VenvyFileUtil.readFormFile(App.getContext(), unZipPath + File.separator + fileName);
197+
if (TextUtils.isEmpty(queryChainData)) {
198+
continue;
199+
}
200+
try {
201+
queryArray.put(new JSONObject(queryChainData));
202+
} catch (Exception e) {
203+
e.printStackTrace();
204+
}
177205
}
178-
cacheUrls.addAll(VenvyFileUtil.getFileName(VenvyFileUtil.getCachePath(App.getContext()) + LUA_ZIP_CACHE));
179-
VenvyFileUtil.copyDir(VenvyFileUtil.getCachePath(App.getContext()) + LUA_ZIP_CACHE, VenvyFileUtil.getCachePath(App.getContext()) + LUA_CACHE_PATH);
180-
return cacheUrls;
206+
VenvyFileUtil.delFolder(unZipPath);
207+
return queryArray;
181208

182209
}
183-
}, new VenvyAsyncTaskUtil.IAsyncCallback<List<String>>() {
210+
}, new VenvyAsyncTaskUtil.IAsyncCallback<JSONArray>() {
184211
@Override
185212
public void onPreExecute() {
186213

187214
}
188215

189216
@Override
190-
public void onPostExecute(List<String> cacheUrls) {
191-
VenvyFileUtil.delFolder(VenvyFileUtil.getCachePath(App.getContext()) + LUA_ZIP_CACHE);
192-
final JSONArray queryArray = new JSONArray();
217+
public void onPostExecute(JSONArray queryData) {
193218
CacheZipUpdateCallback callback = getCacheLuaUpdateCallback();
194-
for (String cacheUrlPath : cacheUrls) {
195-
if (callback != null) {
196-
String fileName = Uri.parse(cacheUrlPath).getLastPathSegment();
197-
if (TextUtils.isEmpty(fileName)) {
198-
callback.updateError(new Exception(""));
199-
return;
200-
}
201-
File file = new File(VenvyFileUtil.getCachePath(App.getContext()) + LUA_CACHE_PATH, fileName);
202-
if (!file.exists() || !file.isFile()) {
203-
callback.updateError(new Exception(""));
204-
return;
205-
}
206-
String queryChainData = VenvyFileUtil.readFormFile(App.getContext(), VenvyFileUtil.getCachePath(App.getContext()) + LUA_CACHE_PATH + File.separator + fileName);
207-
if (TextUtils.isEmpty(queryChainData)) {
208-
callback.updateError(new Exception(""));
209-
return;
210-
}
211-
try {
212-
queryArray.put(new JSONObject(queryChainData));
213-
} catch (Exception e) {
214-
e.printStackTrace();
215-
}
216-
}
219+
if (callback == null) {
220+
VenvyLog.i(TAG, "unZip call is Null ");
221+
return;
217222
}
218-
if (callback != null) {
219-
callback.updateComplete(queryArray);
223+
if (queryData == null) {
224+
callback.updateError(new NullPointerException("read unZip data is error,because read json is null"));
225+
} else {
226+
callback.updateComplete(queryData);
220227
}
221228
}
222229

@@ -246,7 +253,7 @@ private void startDownloadZipFile(final List<String> downZipUrls, final List<Str
246253
}
247254
final ArrayList<DownloadTask> arrayList = new ArrayList<>();
248255
for (String string : needDownZipUrls) {
249-
DownloadTask task = new DownloadTask(App.getContext(), string, VenvyFileUtil.getCachePath(App.getContext()) + LUA_ZIP + File.separator + Uri.parse(string).getLastPathSegment(), true);
256+
DownloadTask task = new DownloadTask(App.getContext(), string, getZipAbsolutePath() + File.separator + Uri.parse(string).getLastPathSegment(), true);
250257
arrayList.add(task);
251258
}
252259
mDownloadTaskRunner.startTasks(arrayList, new TaskListener<DownloadTask, Boolean>() {
@@ -336,9 +343,9 @@ private List<File> getZipFilesWithUrl(List<String> urls) {
336343
return zipFlies;
337344
}
338345
for (String url : urls) {
339-
String path = VenvyFileUtil.getCachePath(App.getContext()) + LUA_ZIP + File.separator + Uri.parse(url).getLastPathSegment();
346+
String path = getZipAbsolutePath() + File.separator + Uri.parse(url).getLastPathSegment();
340347
File hasDownFile = new File(path);
341-
if (!hasDownFile.exists() || !TextUtils.equals("zip", VenvyFileUtil.getExtension(path))) {
348+
if (!hasDownFile.exists() || !TextUtils.equals("zip", VenvyFileUtil.getExtension(path).toLowerCase())) {
342349
continue;
343350
}
344351
zipFlies.add(hasDownFile);
@@ -352,6 +359,22 @@ private List<File> getZipFilesWithUrl(List<String> urls) {
352359
* @return
353360
*/
354361
private String getFileZipEncoderByMd5(String fileName) {
355-
return VenvyMD5Util.EncoderByMd5(new File(VenvyFileUtil.getCachePath(App.getContext()) + LUA_ZIP + File.separator + fileName));
362+
return VenvyMD5Util.EncoderByMd5(new File(getZipAbsolutePath() + File.separator + fileName));
363+
}
364+
365+
/***
366+
* 获取下载Zip的绝对路径
367+
* @return
368+
*/
369+
private String getZipAbsolutePath() {
370+
return VenvyFileUtil.getCachePath(App.getContext()) + DOWN_ZIP_PATH;
371+
}
372+
373+
/***
374+
* 获取解压Zip的绝对路径
375+
* @return
376+
*/
377+
private String getUnZipAbsolutePath() {
378+
return VenvyFileUtil.getCachePath(App.getContext()) + UN_ZIP_PATH;
356379
}
357380
}

0 commit comments

Comments
 (0)