diff --git a/app/src/main/java/com/hnjc/wjtx/LaunchActivity.java b/app/src/main/java/com/hnjc/wjtx/LaunchActivity.java index 1e23c84..34583f0 100644 --- a/app/src/main/java/com/hnjc/wjtx/LaunchActivity.java +++ b/app/src/main/java/com/hnjc/wjtx/LaunchActivity.java @@ -40,18 +40,13 @@ 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}; @@ -103,22 +98,12 @@ 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); @@ -143,7 +128,7 @@ public class LaunchActivity extends Activity { */ private void getLocalVersionInfo() { String fileName = gameUrl.replace("index.html", "version.json"); - String realPath = getFileDirByUrl(fileName); + String realPath = preloadPath + "/" + getFileDirByUrl(fileName) + "version.json" ; File versionFile = new File(realPath); if (versionFile.exists()) { JSONObject data = AssetsUtil.readJsonFromFile(this, realPath); @@ -172,7 +157,6 @@ public class LaunchActivity extends Activity { */ private void getRemoteVersionInfo() { webApi.getVersionInfo((success, message, err) -> { - boolean needUpdate = false; if (success) { try { remoteVersion = message.getString("version"); @@ -181,6 +165,7 @@ 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; @@ -190,35 +175,37 @@ 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); + String dir = preloadPath + "/" + getFileDirByUrl(gameUrl); File dirFile = new File(dir); if (!dirFile.exists()) { dirFile.mkdirs(); } downloadGameRes(zipUrl, dir); } - // 下载资源 + private void downloadGameRes(final String zipUrl, String targetDir) { updateProgress(0); changeLoadTxt(downloadStr); @@ -230,7 +217,7 @@ public class LaunchActivity extends Activity { //开始Handler循环 handler.sendEmptyMessageDelayed(1, 200); } - // 解压资源 + private void unzip(File file) { updateProgress(0); changeLoadTxt(unzipStr); @@ -242,20 +229,12 @@ 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()) { @@ -265,11 +244,6 @@ public class LaunchActivity extends Activity { } } } - - /** - * 遍历删除文件夹和子文件夹 - * @param file 要删除的文件或文件夹路径 - */ private void deletePreloadFile(File file) { if (file.isDirectory()) { File[] files = file.listFiles(); @@ -281,16 +255,11 @@ 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(); } @@ -306,7 +275,6 @@ public class LaunchActivity extends Activity { @Override public void finish() { super.finish(); - // 移除activity的移出场动画 overridePendingTransition(0, 0); } diff --git a/app/src/main/java/com/hnjc/wjtx/util/AssetsUtil.java b/app/src/main/java/com/hnjc/wjtx/util/AssetsUtil.java index 73f1481..af52d0b 100644 --- a/app/src/main/java/com/hnjc/wjtx/util/AssetsUtil.java +++ b/app/src/main/java/com/hnjc/wjtx/util/AssetsUtil.java @@ -12,6 +12,7 @@ import org.json.JSONObject; import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -187,12 +188,15 @@ public class AssetsUtil { */ public static String readFromFile(Context context, String path) { try { - InputStreamReader inputReader = new InputStreamReader(context.openFileInput(path)); + FileInputStream fileInputStream = new FileInputStream(new File(path)); + InputStreamReader inputReader = new InputStreamReader(fileInputStream); BufferedReader bufReader = new BufferedReader(inputReader); + String line=""; StringBuilder result= new StringBuilder(); while((line = bufReader.readLine()) != null) result.append(line); + fileInputStream.close(); inputReader.close(); bufReader.close(); return result.toString();