Skip to content

Commit f955d43

Browse files
committed
优化细节 (progress,total 变更 int -> long)
1 parent 8f38e0d commit f955d43

File tree

12 files changed

+63
-24
lines changed

12 files changed

+63
-24
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,10 @@ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
419419

420420
## 版本日志
421421

422+
#### 待发布版本([提前体验](test.md)
423+
* 优化细节 (progress,total 变更 int -> long)
424+
* 优化进度显示([#44](https://github.com/jenly1314/AppUpdater/issues/44)
425+
422426
#### v2.0.1:2025-8-31
423427
* 优化AppUpdater中属性的访问权限
424428

app-updater/src/main/java/com/king/app/updater/http/HttpManager.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,16 @@ class HttpManager @JvmOverloads constructor(
7070
HttpURLConnection.HTTP_OK -> {
7171

7272
val length = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
73-
connection.contentLengthLong.toInt()
73+
connection.contentLengthLong
7474
} else {
75-
connection.contentLength
75+
connection.contentLength.toLong()
7676
}
7777
val outputFile = File(filepath)
7878
try {
7979
connection.inputStream.use { inputStream ->
8080
FileOutputStream(outputFile).use { outputStream ->
8181
val buffer = ByteArray(8192)
82-
var progress = 0
82+
var progress = 0L
8383
while (true) {
8484
val bytesRead = inputStream.read(buffer)
8585
if (bytesRead == -1 || isCancel) break

app-updater/src/main/java/com/king/app/updater/http/IHttpManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ sealed class DownloadState {
4545
* 下载进度
4646
*/
4747
data class Progress(
48-
val progress: Int,
49-
val total: Int,
48+
val progress: Long,
49+
val total: Long,
5050
) : DownloadState()
5151

5252
/**

app-updater/src/main/java/com/king/app/updater/http/OkHttpManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ class OkHttpManager(private val okHttpClient: OkHttpClient) : IHttpManager {
8080
try {
8181
val body = response.body ?: throw IllegalStateException("Response body is null.")
8282

83-
val length = body.contentLength().toInt()
83+
val length = body.contentLength()
8484
val outputFile = File(filepath)
8585

8686
body.byteStream().use { inputStream ->
8787
FileOutputStream(outputFile).use { outputStream ->
8888
val buffer = ByteArray(8192)
89-
var progress = 0
89+
var progress = 0L
9090
while (true) {
9191
val bytesRead = inputStream.read(buffer)
9292
if (bytesRead == -1 || isCancel) break

app-updater/src/main/java/com/king/app/updater/listener/DownloadListener.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface DownloadListener {
2222
* @param progress 当前进度大小
2323
* @param total 总文件大小
2424
*/
25-
fun onProgress(progress: Int, total: Int)
25+
fun onProgress(progress: Long, total: Long)
2626

2727
/**
2828
* 下载成功

app-updater/src/main/java/com/king/app/updater/listener/SimpleDownloadListener.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ open class SimpleDownloadListener : DownloadListener {
1515

1616
}
1717

18-
override fun onProgress(progress: Int, total: Int) {
18+
override fun onProgress(progress: Long, total: Long) {
1919

2020
}
2121

app-updater/src/main/java/com/king/app/updater/notification/INotificationHandler.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ interface INotificationHandler {
6161
@DrawableRes smallIcon: Int,
6262
title: CharSequence,
6363
content: CharSequence,
64-
progress: Int,
65-
total: Int,
64+
progress: Long,
65+
total: Long,
6666
supportCancelDownload: Boolean
6767
)
6868

app-updater/src/main/java/com/king/app/updater/notification/NotificationHandler.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ open class NotificationHandler : INotificationHandler {
4646
smallIcon: Int,
4747
title: CharSequence,
4848
content: CharSequence,
49-
progress: Int,
50-
total: Int,
49+
progress: Long,
50+
total: Long,
5151
supportCancelDownload: Boolean
5252
) {
5353
NotificationUtils.showProgressNotification(

app-updater/src/main/java/com/king/app/updater/service/DownloadService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class DownloadService : Service() {
199199
lastTime = currentTime
200200
var progressPercentage = 0
201201
if (it.total > 0) {
202-
progressPercentage = it.progress * 100 / it.total
202+
progressPercentage = (it.progress * 100L / it.total).toInt()
203203
LogX.format(LogFormat.PLAIN)
204204
.d(
205205
"%-4s | %d/%d",

app-updater/src/main/java/com/king/app/updater/util/NotificationUtils.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ object NotificationUtils {
106106
@DrawableRes smallIcon: Int,
107107
title: CharSequence,
108108
content: CharSequence,
109-
progress: Int,
110-
total: Int,
109+
progress: Long,
110+
total: Long,
111111
supportCancelDownload: Boolean
112112
) {
113113
val builder = buildNotification(
@@ -308,16 +308,21 @@ object NotificationUtils {
308308
@DrawableRes smallIcon: Int,
309309
title: CharSequence,
310310
content: CharSequence,
311-
progress: Int = Constants.NONE,
312-
total: Int = Constants.NONE,
311+
progress: Long = 0,
312+
total: Long = 0,
313313
): NotificationCompat.Builder {
314314
return NotificationCompat.Builder(context, channelId).apply {
315315
setSmallIcon(smallIcon)
316316
setContentTitle(title)
317317
setContentText(content)
318318
setOngoing(true)
319-
if (progress != Constants.NONE) {
320-
setProgress(total, progress, total <= 0)
319+
if (progress > 0) {
320+
if(total > 0) {
321+
val progressPercentage = (progress * 100L / total).toInt()
322+
setProgress(100, progressPercentage, false)
323+
} else {
324+
setProgress(0,0, true)
325+
}
321326
}
322327
}
323328
}

0 commit comments

Comments
 (0)