Skip to content

Commit

Permalink
add ProcessDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoXiCheng committed Nov 7, 2023
1 parent ec17fd8 commit 50b0e04
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
branches:
- main
env:
LATEST_VERSION: "1.4.2"
LATEST_VERSION: "1.4.3"

jobs:
build-and-deploy:
Expand Down
Binary file added apk/SKIP-v1.4.3.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
minSdk 24
targetSdk 32
versionCode 1
versionName "1.4.2"
versionName "1.4.3"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
59 changes: 51 additions & 8 deletions app/src/main/java/com/android/skip/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ import com.android.skip.ui.theme.OneClickTheme
import com.android.skip.ui.theme.green
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import okhttp3.Request
import org.yaml.snakeyaml.Yaml
import java.io.File
import java.util.*
import kotlin.concurrent.thread
import kotlin.math.roundToInt


var accessibilityState by mutableStateOf(false)
Expand Down Expand Up @@ -72,6 +71,11 @@ var latestVersionText by mutableStateOf("")
// 立即更新
var isUpdateAPKClicked by mutableStateOf(false)

// 更新进度对话框
var isProcessDialogVisible by mutableStateOf(false)

var downloadProgress by mutableStateOf(0f)


class MainActivity : ComponentActivity() {

Expand Down Expand Up @@ -159,6 +163,7 @@ class MainActivity : ComponentActivity() {
)
) {
MainSurface()
ProcessDialog()
}


Expand Down Expand Up @@ -220,7 +225,8 @@ class MainActivity : ComponentActivity() {
isCheckUpdateBtnClicked -> {
thread {
ToastManager.showToast(this, "开始检查更新")
val updateSkipConfigResult = if (HttpManager.updateSkipConfig()) "配置更新成功" else "配置更新失败"
val updateSkipConfigResult =
if (HttpManager.updateSkipConfig()) "配置更新成功" else "配置更新失败"
ToastManager.showToast(this, updateSkipConfigResult)

val latestVersion = HttpManager.getLatestVersion()
Expand All @@ -236,15 +242,24 @@ class MainActivity : ComponentActivity() {
}
isUpdateAPKClicked -> {
thread {
HttpManager.downLoadNewAPK(latestVersionText, this)
isProcessDialogVisible = true
HttpManager.downloadNewAPK(latestVersionText, this) { it ->
downloadProgress = it * 0.01f
if (it == 100) isProcessDialogVisible = false
}
val latestVersionAPK = "SKIP-v$latestVersionText.apk"
val apkFile = File(this.getExternalFilesDir(null), latestVersionAPK)
println(apkFile.name)
val apkUri = FileProvider.getUriForFile(this, this.applicationContext.packageName + ".provider", apkFile)

val apkUri = FileProvider.getUriForFile(
this,
this.applicationContext.packageName + ".provider",
apkFile
)

val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(apkUri, "application/vnd.android.package-archive")
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION
intent.flags =
Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION
this.startActivity(intent)

isUpdateAPKClicked = false
Expand Down Expand Up @@ -517,7 +532,7 @@ fun PageFooter() {
fun AlertDialog(
context: Context, title: CharSequence,
message: CharSequence?, negativeText: CharSequence,
positiveText: CharSequence, onPositiveButtonClick: ()->Unit
positiveText: CharSequence, onPositiveButtonClick: () -> Unit
) {
MaterialAlertDialogBuilder(context)
.setTitle(title)
Expand Down Expand Up @@ -560,5 +575,33 @@ fun ImageDialog() {
)
}

@Composable
fun ProcessDialog() {
if (isProcessDialogVisible) {
AlertDialog(
onDismissRequest = {
isProcessDialogVisible = false
},
title = {
Text(text = "正在下载")
},
text = {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
modifier = Modifier.padding(16.dp)
) {
LinearProgressIndicator(progress = downloadProgress)
Spacer(modifier = Modifier.height(16.dp))
Text(text = "下载进度:${(downloadProgress * 100).roundToInt()}%")
}
},
confirmButton = {
},
dismissButton = {
}
)
}
}


22 changes: 18 additions & 4 deletions app/src/main/java/com/android/skip/manager/HttpManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,28 @@ object HttpManager {
}
}

fun downLoadNewAPK(latestVersion: String, context: Context) {
fun downloadNewAPK(latestVersion: String, context: Context, onDownloadProcess: (process: Int) -> Unit) {
try {
val latestVersionAPK = "SKIP-v$latestVersion.apk"
val request = Request.Builder().url("$BASE_URL/$latestVersionAPK").build()

client.newCall(request).execute().use { response ->
val fos = FileOutputStream(File(context.getExternalFilesDir(null), latestVersionAPK))
fos.use {
fos.write(response.body()?.bytes())
val body = response.body()
val contentLength = body?.contentLength() ?: 0
body?.byteStream()?.apply {
val fos = FileOutputStream(File(context.getExternalFilesDir(null), latestVersionAPK))
val buffer = ByteArray(2048)
var len: Int
var downloaded = 0L

while (read(buffer).also { len = it } != -1) {
fos.write(buffer, 0, len)
downloaded += len
val progress = (downloaded.toFloat() / contentLength.toFloat()) * 100
onDownloadProcess(kotlin.math.floor(progress.toDouble()).toInt())
}
fos.flush()
fos.close()
}
}
} catch (e: Exception) {
Expand Down

0 comments on commit 50b0e04

Please sign in to comment.