|
@@ -10,6 +10,7 @@ import android.content.pm.PackageManager
|
|
|
import android.net.Uri
|
|
|
import android.os.Build
|
|
|
import android.os.Environment
|
|
|
+import androidx.annotation.RequiresApi
|
|
|
import androidx.core.app.ActivityCompat
|
|
|
import androidx.core.app.NotificationCompat
|
|
|
import androidx.core.app.NotificationManagerCompat
|
|
@@ -56,8 +57,8 @@ class UpdateManager {
|
|
|
|
|
|
fun startDownload(updateInfo: UpdateInfo) {
|
|
|
val request = DownloadManager.Request(Uri.parse(updateInfo.apkUrl)).apply {
|
|
|
- setTitle("${mContext?.packageName}")
|
|
|
- setDescription("Downloading new version...")
|
|
|
+ setTitle(mContext?.getString(R.string.app_name))
|
|
|
+ setDescription(mContext?.getString(R.string.update_app_downloading))
|
|
|
setDestinationInExternalFilesDir(mContext, Environment.DIRECTORY_DOWNLOADS, DOWNLOADED_APK_FILENAME)
|
|
|
setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
|
|
|
}
|
|
@@ -118,6 +119,7 @@ class UpdateManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @RequiresApi(Build.VERSION_CODES.TIRAMISU)
|
|
|
private fun registerDownloadReceiver(context: Context) {
|
|
|
val receiver = object : BroadcastReceiver() {
|
|
|
override fun onReceive(context: Context, intent: Intent) {
|
|
@@ -130,20 +132,26 @@ class UpdateManager {
|
|
|
val status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
|
|
|
when (status) {
|
|
|
DownloadManager.STATUS_SUCCESSFUL -> {
|
|
|
- Logger.d(TAG, "======== DownloadManager.STATUS_SUCCESSFUL")
|
|
|
+ Logger.d(TAG, "======== DownloadManager.STATUS_SUCCESSFUL2")
|
|
|
NotificationManagerCompat.from(context).cancel(notificationId)
|
|
|
val apkFile = File(mContext?.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), DOWNLOADED_APK_FILENAME)
|
|
|
- val md5 = calculateMD5(apkFile)
|
|
|
- Logger.d(TAG, "======== DownloadManager downloadInfo.md5:${downloadInfo.md5}, md5:$md5")
|
|
|
- if (apkFile.exists() && downloadInfo.md5.equals(md5, ignoreCase = true)) {
|
|
|
- Logger.d(TAG, "======== DownloadManager showInstallDialog")
|
|
|
- showInstallDialog()
|
|
|
- } else {
|
|
|
- if (apkFile.exists()) {
|
|
|
+ if (apkFile.exists()) {
|
|
|
+ val md5 = calculateMD5(apkFile)
|
|
|
+ Logger.d(TAG, "======== DownloadManager downloadInfo.md5:${downloadInfo.md5}, md5:$md5")
|
|
|
+ if (downloadInfo.md5.equals(md5, ignoreCase = true)) {
|
|
|
+ Logger.d(TAG, "======== DownloadManager showInstallDialog")
|
|
|
+ showInstallDialog()
|
|
|
+ } else {
|
|
|
apkFile.delete()
|
|
|
+ if (downloadInfo.retry < 2) {
|
|
|
+ Logger.d(TAG, "======== DownloadManager downloadInfo.retry:${downloadInfo.retry}")
|
|
|
+ downloadInfo.retry ++
|
|
|
+ startDownload(UpdateInfo("", "", downloadInfo.apkUrl, downloadInfo.md5))
|
|
|
+ }
|
|
|
}
|
|
|
+ } else {
|
|
|
if (downloadInfo.retry < 2) {
|
|
|
- Logger.d(TAG, "======== DownloadManager downloadInfo.retry:${downloadInfo.retry}")
|
|
|
+ Logger.d(TAG, "======== DownloadManager file not exist, retry:${downloadInfo.retry}")
|
|
|
downloadInfo.retry ++
|
|
|
startDownload(UpdateInfo("", "", downloadInfo.apkUrl, downloadInfo.md5))
|
|
|
}
|
|
@@ -254,6 +262,7 @@ class UpdateManager {
|
|
|
|
|
|
override fun onFailure(call: Call<UpdateAppRepo>, t: Throwable) {
|
|
|
Logger.e(TAG, "Network error: ${t.message}")
|
|
|
+ ToastUtils.showLongToast(R.string.network_status_0)
|
|
|
}
|
|
|
})
|
|
|
}
|