From c0e90edc9e33c8c379b4a49d1e36646cfea2fa2d Mon Sep 17 00:00:00 2001 From: zhl Date: Thu, 19 Nov 2020 19:21:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=BA=9B=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/hnjc/wjtx/LaunchActivity.java | 72 +++++++++++++------ 1 file changed, 52 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/hnjc/wjtx/LaunchActivity.java b/app/src/main/java/com/hnjc/wjtx/LaunchActivity.java index e6bee53..1e23c84 100644 --- a/app/src/main/java/com/hnjc/wjtx/LaunchActivity.java +++ b/app/src/main/java/com/hnjc/wjtx/LaunchActivity.java @@ -40,13 +40,18 @@ public class LaunchActivity extends Activity { private String baseVersion = ZERO_VERSION; // 远程的版本号 private String remoteVersion = ZERO_VERSION; + // 在检查版本阶段, 进度条下显示的文字 private String versionStr; + // 在下载更新版本阶段, 进度条下显示的文字 private String downloadStr; + // 在解压版本阶段, 进度条下显示的文字 private String unzipStr; - - private TaskInfo info;//任务信息 - private DownloadRunnable runnable;//下载任务 - private UnzipRunnable unzipRunnable; // 解压任务 + //任务信息 + private TaskInfo info; + //下载任务 + private DownloadRunnable runnable; + // 解压任务 + private UnzipRunnable unzipRunnable; private static final String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE}; @@ -98,12 +103,22 @@ public class LaunchActivity extends Activity { compareVersions(); } + /** + * 更新进度条的进度 + * @param val 进度 0 - 100 + */ private void updateProgress(int val) { progressBar.setProgress(val); } + + /** + * 更新进度条下面的文字 + * @param title 要显示的内容 + */ private void changeLoadTxt(String title) { progressLabel.setText(title); } + // 获取代码包, 预加载目录和远程的版本 private void compareVersions() { changeLoadTxt(versionStr); updateProgress(10); @@ -157,6 +172,7 @@ public class LaunchActivity extends Activity { */ private void getRemoteVersionInfo() { webApi.getVersionInfo((success, message, err) -> { + boolean needUpdate = false; if (success) { try { remoteVersion = message.getString("version"); @@ -165,7 +181,6 @@ public class LaunchActivity extends Activity { Log.i(TAG, "local version: " + localVersion); Log.i(TAG, "remote version: " + remoteVersion); Log.i(TAG, "remote resource: " + zipUrl); - boolean needUpdate = false; if (StringUtil.compareAppVersion(baseVersion, localVersion) >= 0) { if (StringUtil.compareAppVersion(remoteVersion, baseVersion) > 0) { needUpdate = true; @@ -175,28 +190,26 @@ public class LaunchActivity extends Activity { needUpdate = true; } } - if (needUpdate) { - if (StringUtil.isBlank(zipUrl)) { - Log.e(TAG, "远程版本高于代码包版本和预加载版本, 需要更新, 但未获取到远程资源地址"); - startMain(); - } else { - Log.i(TAG, "远程版本高于代码包版本和预加载版本, 需要更新"); - updateProgress(100); - preloadGame(); - } - } else { - startMain(); - } } catch (JSONException e) { e.printStackTrace(); + } + } + if (needUpdate) { + if (StringUtil.isBlank(zipUrl)) { + Log.e(TAG, "远程版本高于代码包版本和预加载版本, 需要更新, 但未获取到远程资源地址"); startMain(); + } else { + Log.i(TAG, "远程版本高于代码包版本和预加载版本, 需要更新"); + updateProgress(100); + preloadGame(); } } else { startMain(); } }); } + // 更新远程资源 private void preloadGame() { String dir = preloadPath + getFileDirByUrl(gameUrl); File dirFile = new File(dir); @@ -205,7 +218,7 @@ public class LaunchActivity extends Activity { } downloadGameRes(zipUrl, dir); } - + // 下载资源 private void downloadGameRes(final String zipUrl, String targetDir) { updateProgress(0); changeLoadTxt(downloadStr); @@ -217,7 +230,7 @@ public class LaunchActivity extends Activity { //开始Handler循环 handler.sendEmptyMessageDelayed(1, 200); } - + // 解压资源 private void unzip(File file) { updateProgress(0); changeLoadTxt(unzipStr); @@ -229,12 +242,20 @@ public class LaunchActivity extends Activity { handler.sendEmptyMessageDelayed(1, 200); } - + /** + * 根据url生成在预加载目录中的路径 + * @param urlString 资源的url + * @return 资源对应的本地路径 + */ private static String getFileDirByUrl(String urlString) { int lastSlash = urlString.lastIndexOf('/'); String server = urlString.substring(0, lastSlash + 1); return server.replaceFirst("://", "/").replace(":", "#0A"); } + + /** + * 清空本地预加载目录 + */ private void emptyPreload() { File dirFile = new File(preloadPath); if (dirFile.exists() && dirFile.isDirectory()) { @@ -244,6 +265,11 @@ public class LaunchActivity extends Activity { } } } + + /** + * 遍历删除文件夹和子文件夹 + * @param file 要删除的文件或文件夹路径 + */ private void deletePreloadFile(File file) { if (file.isDirectory()) { File[] files = file.listFiles(); @@ -255,11 +281,16 @@ public class LaunchActivity extends Activity { file.delete(); } } + + /** + * 切换至game activity + */ private void startMain() { Intent intent = new Intent(LaunchActivity.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); intent.putExtra("preloadPath", preloadPath); startActivity(intent); + // 移除activity的进场动画 overridePendingTransition(0, 0); finish(); } @@ -275,6 +306,7 @@ public class LaunchActivity extends Activity { @Override public void finish() { super.finish(); + // 移除activity的移出场动画 overridePendingTransition(0, 0); }