Skip to content

Commit

Permalink
vaenow#131 Make download filename unique with timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
JEreth committed Dec 7, 2022
1 parent 2fe6973 commit 37afef2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/android/DownloadApkThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.lang.*;

import java.nio.charset.StandardCharsets;

Expand All @@ -39,6 +40,7 @@ public class DownloadApkThread implements Runnable {
private DownloadHandler downloadHandler;
private Handler mHandler;
private AuthenticationOptions authentication;
private long uniqueVersionId;

public DownloadApkThread(Context mContext, Handler mHandler, ProgressBar mProgress, AlertDialog mDownloadDialog, HashMap<String, String> mHashMap, JSONObject options) {
this.mDownloadDialog = mDownloadDialog;
Expand All @@ -47,7 +49,8 @@ public DownloadApkThread(Context mContext, Handler mHandler, ProgressBar mProgre
this.authentication = new AuthenticationOptions(options);

this.mSavePath = Environment.getExternalStorageDirectory() + "/" + "download"; // SD Path
this.downloadHandler = new DownloadHandler(mContext, mProgress, mDownloadDialog, this.mSavePath, mHashMap);
this.uniqueVersionId = System.currentTimeMillis();
this.downloadHandler = new DownloadHandler(mContext, mProgress, mDownloadDialog, this.mSavePath, mHashMap, this.uniqueVersionId);
}


Expand Down Expand Up @@ -86,7 +89,7 @@ private void downloadAndInstall() {
if (!file.exists()) {
file.mkdir();
}
File apkFile = new File(mSavePath, mHashMap.get("name")+".apk");
File apkFile = new File(mSavePath, mHashMap.get("name")+this.uniqueVersionId+".apk");
FileOutputStream fos = new FileOutputStream(apkFile);
int count = 0;
// 缓存
Expand Down Expand Up @@ -120,4 +123,4 @@ private void downloadAndInstall() {
}

}
}
}
11 changes: 7 additions & 4 deletions src/android/DownloadHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ProgressBar;
import android.support.v4.content.FileProvider;
import androidx.core.content.FileProvider;
import java.io.File;
import java.util.HashMap;

Expand All @@ -37,14 +37,16 @@ public class DownloadHandler extends Handler {
private HashMap<String, String> mHashMap;
private MsgHelper msgHelper;
private AlertDialog mDownloadDialog;
private long uniqueVersionId;

public DownloadHandler(Context mContext, ProgressBar mProgress, AlertDialog mDownloadDialog, String mSavePath, HashMap<String, String> mHashMap) {
public DownloadHandler(Context mContext, ProgressBar mProgress, AlertDialog mDownloadDialog, String mSavePath, HashMap<String, String> mHashMap, long uniqueVersionId) {
this.msgHelper = new MsgHelper(mContext.getPackageName(), mContext.getResources());
this.mDownloadDialog = mDownloadDialog;
this.mContext = mContext;
this.mProgress = mProgress;
this.mSavePath = mSavePath;
this.mHashMap = mHashMap;
this.uniqueVersionId = uniqueVersionId;
}

public void handleMessage(Message msg) {
Expand Down Expand Up @@ -92,7 +94,7 @@ public void onClick(View view) {
private void installApk() {
LOG.d(TAG, "Installing APK");

File apkFile = new File(mSavePath, mHashMap.get("name")+".apk");
File apkFile = new File(mSavePath, mHashMap.get("name")+this.uniqueVersionId+".apk");
if (!apkFile.exists()) {
LOG.e(TAG, "Could not find APK: " + mHashMap.get("name"));
return;
Expand All @@ -101,13 +103,14 @@ private void installApk() {
LOG.d(TAG, "APK Filename: " + apkFile.toString());

// 通过Intent安装APK文件
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
LOG.d(TAG, "Build SDK Greater than or equal to Nougat");
String applicationId = (String) BuildHelper.getBuildConfigValue((Activity) mContext, "APPLICATION_ID");
Uri apkUri = FileProvider.getUriForFile(mContext, applicationId + ".appupdate.provider", apkFile);
Intent i = new Intent(Intent.ACTION_INSTALL_PACKAGE);
i.setData(apkUri);
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
mContext.startActivity(i);
}else{
LOG.d(TAG, "Build SDK less than Nougat");
Expand Down
2 changes: 1 addition & 1 deletion src/android/GenericFileProvider.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.vaenow.appupdate.android;

import android.support.v4.content.FileProvider;
import androidx.core.content.FileProvider;

/**
* Created by kalea on 2017/8/23.
Expand Down

0 comments on commit 37afef2

Please sign in to comment.