Skip to content

Commit

Permalink
Merge pull request #113 from GuoXiCheng/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
GuoXiCheng authored Jan 4, 2024
2 parents 84f28ef + 90a03d1 commit b5bfff0
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 6 deletions.
9 changes: 9 additions & 0 deletions app/src/main/java/com/android/skip/BaseActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,16 @@ abstract class BaseActivity : AppCompatActivity() {
ProvideContent()
}
}

RectManager.setMaxRect(this)

// 清理临时文件
val directory = this.getExternalFilesDir(null)
directory?.let {
it.listFiles()?.forEach { file ->
file.delete()
}
}
}

@Composable
Expand Down
82 changes: 81 additions & 1 deletion app/src/main/java/com/android/skip/NewMainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.android.skip

import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.compose.foundation.background
Expand All @@ -11,12 +12,20 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.content.FileProvider
import androidx.lifecycle.lifecycleScope
import com.android.skip.compose.AboutButton
import com.android.skip.compose.ConfirmDialog
import com.android.skip.compose.DownloadProcessDialog
import com.android.skip.compose.KeepAliveButton
import com.android.skip.compose.SettingsButton
import com.android.skip.compose.StartButton
Expand All @@ -28,8 +37,18 @@ import com.android.skip.manager.WhitelistManager
import com.android.skip.utils.DataStoreUtils
import com.android.skip.viewmodel.StartButtonViewModel
import org.yaml.snakeyaml.Yaml
import java.io.File
import kotlin.concurrent.thread

var showUpdateDialog by mutableStateOf(false)

var showDownloadDialog by mutableStateOf(false)

var showApkInstallDialog by mutableStateOf(false)

var apkDownloadProgress by mutableStateOf(0f)

var apkLatestVersionText by mutableStateOf(BuildConfig.VERSION_NAME.trim())

class NewMainActivity : BaseActivity() {
private val startButtonViewModel: StartButtonViewModel by viewModels()
Expand All @@ -43,10 +62,12 @@ class NewMainActivity : BaseActivity() {
} catch (e: Exception) {
e.printStackTrace()
}

}

@Composable
override fun ProvideContent() {
val context = LocalContext.current
Column(
modifier = Modifier
.fillMaxSize()
Expand All @@ -56,7 +77,7 @@ class NewMainActivity : BaseActivity() {
) {
Row {
Text(
text = "SKIP",
text = stringResource(id = R.string.app_name),
fontSize = 24.sp,
fontWeight = FontWeight.Bold,
color = MaterialTheme.colorScheme.onBackground
Expand All @@ -67,6 +88,55 @@ class NewMainActivity : BaseActivity() {
KeepAliveButton()
SettingsButton()
AboutButton()

if (showUpdateDialog) {
ConfirmDialog(
title = "发现新版本",
content = "是否立即下载更新?",
onDismiss = { showUpdateDialog = false },
onAllow = {
showUpdateDialog = false
showDownloadDialog = true
})
}

if (showDownloadDialog) {
DownloadProcessDialog()

thread {
HttpManager.downloadNewAPK(apkLatestVersionText, context) { it ->
apkDownloadProgress = it * 0.01f
if (it == 100) {
showDownloadDialog = false
showApkInstallDialog = true
}
}
}
}

if (showApkInstallDialog) {
ConfirmDialog(
title = "下载完成",
content = "是否立即安装新版本?",
onDismiss = { showApkInstallDialog = false },
onAllow = {
showApkInstallDialog = false

val filename = "SKIP-v$apkLatestVersionText.apk"
val apkFile = File(context.getExternalFilesDir(null), filename)
val apkUri = FileProvider.getUriForFile(
context,
context.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
context.startActivity(intent)
})
}
}
}

Expand All @@ -80,5 +150,15 @@ class NewMainActivity : BaseActivity() {
HttpManager.updateSkipConfigV2()
}
}

if (DataStoreUtils.getSyncData(SKIP_AUTO_CHECK_UPDATE, false)) {
thread {
val latestVersion = HttpManager.getLatestVersion()
if (latestVersion != BuildConfig.VERSION_NAME.trim()) {
apkLatestVersionText = latestVersion
showUpdateDialog = true
}
}
}
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/android/skip/SettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ fun SettingsActivityInterface(onBackClick: () -> Unit) {
val checkUpdateVersion = remember {
mutableStateOf(
DataStoreUtils.getSyncData(
SKIP_AUTO_CHECK_UPDATE, true
SKIP_AUTO_CHECK_UPDATE, false
)
)
}
val checkUpdateConfig = remember {
mutableStateOf(
DataStoreUtils.getSyncData(
SKIP_AUTO_SYNC_CONFIG, true
SKIP_AUTO_SYNC_CONFIG, false
)
)
}
Expand Down
13 changes: 10 additions & 3 deletions app/src/main/java/com/android/skip/compose/ConfirmDialog.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.android.skip.compose

import android.content.res.Configuration
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -10,6 +11,7 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
Expand All @@ -20,9 +22,12 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import com.android.skip.R
import com.android.skip.themeTypeState

@OptIn(ExperimentalMaterial3Api::class)
@Composable
Expand Down Expand Up @@ -65,10 +70,12 @@ fun ConfirmDialog(
Button(
onClick = onDismiss,
modifier = Modifier.weight(1f),
colors = ButtonDefaults.buttonColors(Color(0xFFE0E0E0))
colors = if (themeTypeState.value == Configuration.UI_MODE_NIGHT_NO) ButtonDefaults.buttonColors(
Color(0xFFF0F0F0)
) else ButtonDefaults.buttonColors(Color(0xFF454545))
) {
Text(
text = "拒绝",
text = stringResource(id = R.string.dialog_confirm_dismiss),
color = Color(0xFFc3c3c3),
fontSize = 16.sp
)
Expand All @@ -82,7 +89,7 @@ fun ConfirmDialog(
)
) {
Text(
text = "允许",
text = stringResource(id = R.string.dialog_confirm_allow),
color = Color.White,
fontSize = 16.sp
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.android.skip.compose

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.android.skip.apkDownloadProgress
import kotlin.math.roundToInt

@Composable
fun DownloadProcessDialog() {
AlertDialog(
onDismissRequest = {},
title = {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp)
) {
Text(text = "正在下载")
}
},
text = {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
modifier = Modifier.padding(16.dp)
) {
LinearProgressIndicator(progress = apkDownloadProgress)
Spacer(modifier = Modifier.height(16.dp))
Text(text = "下载进度:${(apkDownloadProgress * 100).roundToInt()}%")
}
},
confirmButton = {},
dismissButton = {}
)
}
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@
<string name="alive_warn_subtitle">以上操作方法可能仅适用于MIUI手机,其他品牌手机请自行查询相应的应用保活方法</string>

<string name="whitelist">应用白名单</string>

<string name="dialog_confirm_allow">允许</string>
<string name="dialog_confirm_dismiss">拒绝</string>
</resources>

0 comments on commit b5bfff0

Please sign in to comment.