修正热更无法正常运行的bug

This commit is contained in:
zhl 2020-12-23 19:52:33 +08:00
parent b2e58d4462
commit 32096e8d74
2 changed files with 27 additions and 55 deletions

View File

@ -40,18 +40,13 @@ public class LaunchActivity extends Activity {
private String baseVersion = ZERO_VERSION; private String baseVersion = ZERO_VERSION;
// 远程的版本号 // 远程的版本号
private String remoteVersion = ZERO_VERSION; private String remoteVersion = ZERO_VERSION;
// 在检查版本阶段, 进度条下显示的文字
private String versionStr; private String versionStr;
// 在下载更新版本阶段, 进度条下显示的文字
private String downloadStr; private String downloadStr;
// 在解压版本阶段, 进度条下显示的文字
private String unzipStr; private String unzipStr;
//任务信息
private TaskInfo info; private TaskInfo info;//任务信息
//下载任务 private DownloadRunnable runnable;//下载任务
private DownloadRunnable runnable; private UnzipRunnable unzipRunnable; // 解压任务
// 解压任务
private UnzipRunnable unzipRunnable;
private static final String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE}; private static final String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
@ -103,22 +98,12 @@ public class LaunchActivity extends Activity {
compareVersions(); compareVersions();
} }
/**
* 更新进度条的进度
* @param val 进度 0 - 100
*/
private void updateProgress(int val) { private void updateProgress(int val) {
progressBar.setProgress(val); progressBar.setProgress(val);
} }
/**
* 更新进度条下面的文字
* @param title 要显示的内容
*/
private void changeLoadTxt(String title) { private void changeLoadTxt(String title) {
progressLabel.setText(title); progressLabel.setText(title);
} }
// 获取代码包, 预加载目录和远程的版本
private void compareVersions() { private void compareVersions() {
changeLoadTxt(versionStr); changeLoadTxt(versionStr);
updateProgress(10); updateProgress(10);
@ -143,7 +128,7 @@ public class LaunchActivity extends Activity {
*/ */
private void getLocalVersionInfo() { private void getLocalVersionInfo() {
String fileName = gameUrl.replace("index.html", "version.json"); String fileName = gameUrl.replace("index.html", "version.json");
String realPath = getFileDirByUrl(fileName); String realPath = preloadPath + "/" + getFileDirByUrl(fileName) + "version.json" ;
File versionFile = new File(realPath); File versionFile = new File(realPath);
if (versionFile.exists()) { if (versionFile.exists()) {
JSONObject data = AssetsUtil.readJsonFromFile(this, realPath); JSONObject data = AssetsUtil.readJsonFromFile(this, realPath);
@ -172,7 +157,6 @@ public class LaunchActivity extends Activity {
*/ */
private void getRemoteVersionInfo() { private void getRemoteVersionInfo() {
webApi.getVersionInfo((success, message, err) -> { webApi.getVersionInfo((success, message, err) -> {
boolean needUpdate = false;
if (success) { if (success) {
try { try {
remoteVersion = message.getString("version"); remoteVersion = message.getString("version");
@ -181,6 +165,7 @@ public class LaunchActivity extends Activity {
Log.i(TAG, "local version: " + localVersion); Log.i(TAG, "local version: " + localVersion);
Log.i(TAG, "remote version: " + remoteVersion); Log.i(TAG, "remote version: " + remoteVersion);
Log.i(TAG, "remote resource: " + zipUrl); Log.i(TAG, "remote resource: " + zipUrl);
boolean needUpdate = false;
if (StringUtil.compareAppVersion(baseVersion, localVersion) >= 0) { if (StringUtil.compareAppVersion(baseVersion, localVersion) >= 0) {
if (StringUtil.compareAppVersion(remoteVersion, baseVersion) > 0) { if (StringUtil.compareAppVersion(remoteVersion, baseVersion) > 0) {
needUpdate = true; needUpdate = true;
@ -190,35 +175,37 @@ public class LaunchActivity extends Activity {
needUpdate = true; needUpdate = true;
} }
} }
if (needUpdate) {
if (StringUtil.isBlank(zipUrl)) {
Log.e(TAG, "远程版本高于代码包版本和预加载版本, 需要更新, 但未获取到远程资源地址");
startMain();
} else {
Log.i(TAG, "远程版本高于代码包版本和预加载版本, 需要更新");
updateProgress(100);
preloadGame();
}
} else {
startMain();
}
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
}
}
if (needUpdate) {
if (StringUtil.isBlank(zipUrl)) {
Log.e(TAG, "远程版本高于代码包版本和预加载版本, 需要更新, 但未获取到远程资源地址");
startMain(); startMain();
} else {
Log.i(TAG, "远程版本高于代码包版本和预加载版本, 需要更新");
updateProgress(100);
preloadGame();
} }
} else { } else {
startMain(); startMain();
} }
}); });
} }
// 更新远程资源
private void preloadGame() { private void preloadGame() {
String dir = preloadPath + getFileDirByUrl(gameUrl); String dir = preloadPath + "/" + getFileDirByUrl(gameUrl);
File dirFile = new File(dir); File dirFile = new File(dir);
if (!dirFile.exists()) { if (!dirFile.exists()) {
dirFile.mkdirs(); dirFile.mkdirs();
} }
downloadGameRes(zipUrl, dir); downloadGameRes(zipUrl, dir);
} }
// 下载资源
private void downloadGameRes(final String zipUrl, String targetDir) { private void downloadGameRes(final String zipUrl, String targetDir) {
updateProgress(0); updateProgress(0);
changeLoadTxt(downloadStr); changeLoadTxt(downloadStr);
@ -230,7 +217,7 @@ public class LaunchActivity extends Activity {
//开始Handler循环 //开始Handler循环
handler.sendEmptyMessageDelayed(1, 200); handler.sendEmptyMessageDelayed(1, 200);
} }
// 解压资源
private void unzip(File file) { private void unzip(File file) {
updateProgress(0); updateProgress(0);
changeLoadTxt(unzipStr); changeLoadTxt(unzipStr);
@ -242,20 +229,12 @@ public class LaunchActivity extends Activity {
handler.sendEmptyMessageDelayed(1, 200); handler.sendEmptyMessageDelayed(1, 200);
} }
/**
* 根据url生成在预加载目录中的路径
* @param urlString 资源的url
* @return 资源对应的本地路径
*/
private static String getFileDirByUrl(String urlString) { private static String getFileDirByUrl(String urlString) {
int lastSlash = urlString.lastIndexOf('/'); int lastSlash = urlString.lastIndexOf('/');
String server = urlString.substring(0, lastSlash + 1); String server = urlString.substring(0, lastSlash + 1);
return server.replaceFirst("://", "/").replace(":", "#0A"); return server.replaceFirst("://", "/").replace(":", "#0A");
} }
/**
* 清空本地预加载目录
*/
private void emptyPreload() { private void emptyPreload() {
File dirFile = new File(preloadPath); File dirFile = new File(preloadPath);
if (dirFile.exists() && dirFile.isDirectory()) { if (dirFile.exists() && dirFile.isDirectory()) {
@ -265,11 +244,6 @@ public class LaunchActivity extends Activity {
} }
} }
} }
/**
* 遍历删除文件夹和子文件夹
* @param file 要删除的文件或文件夹路径
*/
private void deletePreloadFile(File file) { private void deletePreloadFile(File file) {
if (file.isDirectory()) { if (file.isDirectory()) {
File[] files = file.listFiles(); File[] files = file.listFiles();
@ -281,16 +255,11 @@ public class LaunchActivity extends Activity {
file.delete(); file.delete();
} }
} }
/**
* 切换至game activity
*/
private void startMain() { private void startMain() {
Intent intent = new Intent(LaunchActivity.this, MainActivity.class); Intent intent = new Intent(LaunchActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.putExtra("preloadPath", preloadPath); intent.putExtra("preloadPath", preloadPath);
startActivity(intent); startActivity(intent);
// 移除activity的进场动画
overridePendingTransition(0, 0); overridePendingTransition(0, 0);
finish(); finish();
} }
@ -306,7 +275,6 @@ public class LaunchActivity extends Activity {
@Override @Override
public void finish() { public void finish() {
super.finish(); super.finish();
// 移除activity的移出场动画
overridePendingTransition(0, 0); overridePendingTransition(0, 0);
} }

View File

@ -12,6 +12,7 @@ import org.json.JSONObject;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -187,12 +188,15 @@ public class AssetsUtil {
*/ */
public static String readFromFile(Context context, String path) { public static String readFromFile(Context context, String path) {
try { 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); BufferedReader bufReader = new BufferedReader(inputReader);
String line=""; String line="";
StringBuilder result= new StringBuilder(); StringBuilder result= new StringBuilder();
while((line = bufReader.readLine()) != null) while((line = bufReader.readLine()) != null)
result.append(line); result.append(line);
fileInputStream.close();
inputReader.close(); inputReader.close();
bufReader.close(); bufReader.close();
return result.toString(); return result.toString();