|
|
@@ -1,10 +1,14 @@ |
|
|
|
package com.nsgk.ruralWeb.ui; |
|
|
|
|
|
|
|
import android.app.Notification; |
|
|
|
import android.app.NotificationChannel; |
|
|
|
import android.app.NotificationManager; |
|
|
|
import android.content.Context; |
|
|
|
import android.content.DialogInterface; |
|
|
|
import android.content.Intent; |
|
|
|
import android.content.SharedPreferences; |
|
|
|
import android.net.Uri; |
|
|
|
import android.os.Build; |
|
|
|
import android.os.Bundle; |
|
|
|
import android.os.Handler; |
|
|
|
import android.os.HandlerThread; |
|
|
@@ -15,6 +19,7 @@ import android.util.Log; |
|
|
|
import android.widget.Toast; |
|
|
|
|
|
|
|
import androidx.appcompat.app.AlertDialog; |
|
|
|
import androidx.core.app.NotificationCompat; |
|
|
|
import androidx.preference.Preference; |
|
|
|
import androidx.preference.PreferenceFragmentCompat; |
|
|
|
import androidx.preference.PreferenceManager; |
|
|
@@ -33,6 +38,7 @@ import java.io.File; |
|
|
|
import java.util.Objects; |
|
|
|
|
|
|
|
import cn.hutool.core.io.FileUtil; |
|
|
|
import cn.hutool.core.io.StreamProgress; |
|
|
|
import cn.hutool.core.util.NumberUtil; |
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
import cn.hutool.http.HttpUtil; |
|
|
@@ -250,29 +256,94 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Prefer |
|
|
|
Log.d(ID_TAG, "下载apk地址: " + updateUrl); |
|
|
|
Toast.makeText(context, "开始下载......", Toast.LENGTH_SHORT).show(); |
|
|
|
|
|
|
|
int icon = context.getResources().getIdentifier("ic_launcher_" + NSConstants.AppIcon()/* + "_round"*/, "mipmap", context.getApplicationContext().getPackageName()); |
|
|
|
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
|
|
|
|
|
|
|
String channelId = "nsgk"; |
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
|
|
|
NotificationChannel notificationChannel = notificationManager.getNotificationChannel(channelId); |
|
|
|
if(null == notificationChannel) |
|
|
|
{ |
|
|
|
NotificationChannel channel = new NotificationChannel(channelId, "下载更新", NotificationManager.IMPORTANCE_DEFAULT); |
|
|
|
channel.setDescription("下载更新"); |
|
|
|
notificationManager.createNotificationChannel(channel); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId); |
|
|
|
builder.setProgress(0, 0, true) |
|
|
|
.setSmallIcon(icon) |
|
|
|
.setTicker("下载更新") |
|
|
|
.setOngoing(true) |
|
|
|
.setWhen(System.currentTimeMillis()) |
|
|
|
.setContentTitle("下载更新") |
|
|
|
.setContentText("开始下载......") |
|
|
|
; |
|
|
|
int notifyId = 0x1003; |
|
|
|
Notification notification = builder.build(); |
|
|
|
notificationManager.notify(notifyId, notification); |
|
|
|
|
|
|
|
// 创建文件夹并删除缓存文件 |
|
|
|
File externalFilesDir = context.getExternalFilesDir(null); |
|
|
|
String dir = externalFilesDir.getAbsolutePath() + File.separator + NSConstants.APK_UPDATE_DOWNLOAD_DIR; |
|
|
|
Log.d(ID_TAG, "创建下载缓存目录: " + dir); |
|
|
|
notificationManager.notify(notifyId, builder.setContentText("创建下载缓存目录").build()); |
|
|
|
FileUtil.mkdir(dir); |
|
|
|
|
|
|
|
String file = dir + File.separator + NSConstants.APK_UPDATE_DOWNLOAD_FILE; |
|
|
|
Log.d(ID_TAG, "删除文件: " + file); |
|
|
|
Log.d(ID_TAG, "删除历史缓存文件: " + file); |
|
|
|
notificationManager.notify(notifyId, builder.setContentText("删除历史缓存文件").build()); |
|
|
|
FileUtil.del(file); |
|
|
|
|
|
|
|
new Thread(() -> { |
|
|
|
long bytes = HttpUtil.downloadFile(updateUrl, file); |
|
|
|
long bytes = HttpUtil.downloadFile(updateUrl, new File(file), new StreamProgress() { |
|
|
|
@Override |
|
|
|
public void start() |
|
|
|
{ |
|
|
|
handler.post(() -> { |
|
|
|
notificationManager.notify(notifyId, builder.setContentText("开始下载......").setProgress(100, 0, false).build()); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void progress(long progressSize) |
|
|
|
{ |
|
|
|
handler.post(() -> { |
|
|
|
notificationManager.notify(notifyId, builder.setContentText("下载: " + FileUtil.readableFileSize(progressSize)).setProgress(0, 0, true).build()); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void finish() |
|
|
|
{ |
|
|
|
handler.post(() -> { |
|
|
|
notificationManager.notify(notifyId, builder.setContentText("下载完成").setProgress(100, 100, false).setOngoing(false).build()); |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
Log.d(ID_TAG, "下载apk文件: " + FileUtil.readableFileSize(bytes)); |
|
|
|
if(bytes > 0) |
|
|
|
{ |
|
|
|
handler.post(() -> { |
|
|
|
handler.postDelayed(() -> { |
|
|
|
notificationManager.notify(notifyId, builder.setContentText("准备安装更新").setProgress(0, 0, false).setOngoing(false).build()); |
|
|
|
Toast.makeText(context, "准备安装......", Toast.LENGTH_SHORT).show(); |
|
|
|
NSContextUtils.InstallApk(context, file); |
|
|
|
}); |
|
|
|
|
|
|
|
// handler.postDelayed(() -> { |
|
|
|
// notificationManager.cancel(notifyId); |
|
|
|
// }, 2000); |
|
|
|
}, 1000); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
handler.post(() -> { |
|
|
|
handler.postDelayed(() -> { |
|
|
|
notificationManager.notify(notifyId, builder.setContentText("下载更新失败").setProgress(0, 0, false).setOngoing(false).build()); |
|
|
|
Toast.makeText(context, "下载更新失败", Toast.LENGTH_LONG).show(); |
|
|
|
}); |
|
|
|
|
|
|
|
// handler.postDelayed(() -> { |
|
|
|
// notificationManager.cancel(notifyId); |
|
|
|
// }, 2000); |
|
|
|
}, 1000); |
|
|
|
} |
|
|
|
}).start(); |
|
|
|
} |
|
|
|