diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 15458620..46629c42 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,7 +5,7 @@ on: branches: - main env: - LATEST_VERSION: "1.4.2" + LATEST_VERSION: "1.4.3" jobs: build-and-deploy: diff --git a/apk/SKIP-v1.4.3.apk b/apk/SKIP-v1.4.3.apk new file mode 100644 index 00000000..b07cbda9 Binary files /dev/null and b/apk/SKIP-v1.4.3.apk differ diff --git a/app/build.gradle b/app/build.gradle index e4aa527f..9c7b1e86 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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 { diff --git a/app/src/main/java/com/android/skip/MainActivity.kt b/app/src/main/java/com/android/skip/MainActivity.kt index 33554b10..884e9ae6 100644 --- a/app/src/main/java/com/android/skip/MainActivity.kt +++ b/app/src/main/java/com/android/skip/MainActivity.kt @@ -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) @@ -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() { @@ -159,6 +163,7 @@ class MainActivity : ComponentActivity() { ) ) { MainSurface() + ProcessDialog() } @@ -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() @@ -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 @@ -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) @@ -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 = { + } + ) + } +} diff --git a/app/src/main/java/com/android/skip/manager/HttpManager.kt b/app/src/main/java/com/android/skip/manager/HttpManager.kt index 09e3541d..e219fcef 100644 --- a/app/src/main/java/com/android/skip/manager/HttpManager.kt +++ b/app/src/main/java/com/android/skip/manager/HttpManager.kt @@ -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) {