Skip to content

Commit

Permalink
功能[自动获取更新]: 检查本地是否有更新包,并作出相应的处理
Browse files Browse the repository at this point in the history
  • Loading branch information
Vera-Firefly committed Sep 7, 2024
1 parent 7d53270 commit a1baa38
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

public class UpdateLauncher {
private static final String GITHUB_API = "https://api.github.com/repos/Vera-Firefly/Pojav-Glow-Worm/releases/latest";
Expand All @@ -46,7 +48,35 @@ public UpdateLauncher(Context context) {
}
}

public void checkForUpdates() {
public void checkCachedApk() {
File dir = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
File apkFile = new File(dir, "cache.apk");
File apkVersionFile = new File(dir, "apk_version");

if (apkFile.exists() && apkVersionFile.exists()) {
try {
String savedTagName = new String(java.nio.file.Files.readAllBytes(apkVersionFile.toPath()));
int savedVersionCode = Integer.parseInt(savedTagName.replaceAll("[^\\d]", ""));
if (savedVersionCode > localVersionCode) {
installApk(apkFile);
} else {
apkFile.delete();
apkVersionFile.delete();
checkForUpdates();
}
} catch (IOException | NumberFormatException e) {
e.printStackTrace();
}
} else if (apkFile.exists() || apkVersionFile.exists()) {
if (apkFile.exists()) apkFile.delete();
if (apkVersionFile.exists()) apkVersionFile.delete();
checkForUpdates();
} else {
checkForUpdates();
}
}

private void checkForUpdates() {
new GetLatestReleaseTask().execute(GITHUB_API);
}

Expand Down Expand Up @@ -126,20 +156,25 @@ private void showDownloadSourceDialog(String tagName, String versionName, String

if (apkUrl != null) {
dialog.dismiss();
startDownload(apkUrl);
startDownload(apkUrl, tagName);
}
})
.setNegativeButton(R.string.alertdialog_cancel, (dialog, id) -> dialog.cancel())
.show();
}

private void startDownload(String apkUrl) {
new DownloadApkTask().execute(apkUrl);
private void startDownload(String apkUrl, String tagName) {
new DownloadApkTask(tagName).execute(apkUrl);
}

private class DownloadApkTask extends AsyncTask<String, Integer, File> {
private String tagName;
ProgressDialog progressDialog;

public DownloadApkTask(String tagName) {
this.tagName = tagName;
}

@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(context);
Expand All @@ -159,7 +194,10 @@ protected File doInBackground(String... urls) {
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
apkFile = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "cache.apk");
File dir = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
apkFile = new File(dir, "cache.apk");
File apkVersionFile = new File(dir, "apk_version");

InputStream inputStream = response.body().byteStream();
FileOutputStream outputStream = new FileOutputStream(apkFile);
byte[] buffer = new byte[1024];
Expand All @@ -173,6 +211,11 @@ protected File doInBackground(String... urls) {
publishProgress((int) ((totalBytesRead * 100) / totalBytes));
}

FileOutputStream versionOutputStream = new FileOutputStream(apkVersionFile);
versionOutputStream.write(tagName.getBytes());
versionOutputStream.flush();
versionOutputStream.close();

outputStream.flush();
outputStream.close();
inputStream.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ protected void onCreate(Bundle savedInstanceState) {
mProgressLayout.observe(ProgressLayout.DOWNLOAD_VERSION_LIST);
// 初始化并调用 UpdateLauncher 进行更新检查
UpdateLauncher updateLauncher = new UpdateLauncher(this);
updateLauncher.checkForUpdates();
updateLauncher.checkCachedApk();
}

@Override
Expand Down

0 comments on commit a1baa38

Please sign in to comment.