增加一些注释

This commit is contained in:
zhl 2020-11-19 19:21:48 +08:00
parent e29648f328
commit c0e90edc9e

View File

@ -40,13 +40,18 @@ 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 UnzipRunnable unzipRunnable; // 解压任务 private DownloadRunnable runnable;
// 解压任务
private UnzipRunnable unzipRunnable;
private static final String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE}; private static final String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
@ -98,12 +103,22 @@ 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);
@ -157,6 +172,7 @@ 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");
@ -165,7 +181,6 @@ 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;
@ -175,6 +190,11 @@ public class LaunchActivity extends Activity {
needUpdate = true; needUpdate = true;
} }
} }
} catch (JSONException e) {
e.printStackTrace();
}
}
if (needUpdate) { if (needUpdate) {
if (StringUtil.isBlank(zipUrl)) { if (StringUtil.isBlank(zipUrl)) {
Log.e(TAG, "远程版本高于代码包版本和预加载版本, 需要更新, 但未获取到远程资源地址"); Log.e(TAG, "远程版本高于代码包版本和预加载版本, 需要更新, 但未获取到远程资源地址");
@ -187,16 +207,9 @@ public class LaunchActivity extends Activity {
} else { } else {
startMain(); startMain();
} }
} catch (JSONException e) {
e.printStackTrace();
startMain();
}
} else {
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);
@ -205,7 +218,7 @@ public class LaunchActivity extends Activity {
} }
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);
@ -217,7 +230,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);
@ -229,12 +242,20 @@ 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()) {
@ -244,6 +265,11 @@ 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();
@ -255,11 +281,16 @@ 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();
} }
@ -275,6 +306,7 @@ public class LaunchActivity extends Activity {
@Override @Override
public void finish() { public void finish() {
super.finish(); super.finish();
// 移除activity的移出场动画
overridePendingTransition(0, 0); overridePendingTransition(0, 0);
} }