From 30456e9db5c2249a2e170662c9cef7d562c2f14c Mon Sep 17 00:00:00 2001 From: "JI,XIAOYONG" Date: Fri, 28 Jun 2024 10:01:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E5=87=BA=E9=94=99=E5=88=99=E5=B1=95=E7=A4=BA?= =?UTF-8?q?=E8=AF=A6=E7=BB=86=E5=87=BA=E9=94=99=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/jixiaoyong/ApkSigner/issues/17 --- .../pages/settings/SettingInfoViewModel.kt | 5 +- .../jixiaoyong/pages/settings/Settings.kt | 75 +++++++++++++++++-- .../io/github/jixiaoyong/utils/ApkSigner.kt | 12 +-- 3 files changed, 77 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/io/github/jixiaoyong/pages/settings/SettingInfoViewModel.kt b/src/main/kotlin/io/github/jixiaoyong/pages/settings/SettingInfoViewModel.kt index f50984d..2686779 100644 --- a/src/main/kotlin/io/github/jixiaoyong/pages/settings/SettingInfoViewModel.kt +++ b/src/main/kotlin/io/github/jixiaoyong/pages/settings/SettingInfoViewModel.kt @@ -4,7 +4,6 @@ import ApkSigner import Logger import io.github.jixiaoyong.base.BaseViewModel import io.github.jixiaoyong.data.SettingPreferencesRepository -import io.github.jixiaoyong.utils.showToast import kotlinx.coroutines.flow.* import kotlinx.coroutines.launch import org.jetbrains.skiko.OS.* @@ -105,12 +104,12 @@ class SettingInfoViewModel(private val repository: SettingPreferencesRepository) } } - fun setupBuildToolsConfig(buildToolDir: String) { + fun setupBuildToolsConfig(buildToolDir: String): String? { val result = ApkSigner.init(buildToolDir) saveApkSigner(ApkSigner.apkSignerPath) saveZipAlign(ApkSigner.zipAlignPath) saveAapt(ApkSigner.aaptPath) - showToast(result ?: "修改成功") + return result } fun saveApkSigner(apkSigner: String?) { diff --git a/src/main/kotlin/io/github/jixiaoyong/pages/settings/Settings.kt b/src/main/kotlin/io/github/jixiaoyong/pages/settings/Settings.kt index b893a24..a00ce0a 100644 --- a/src/main/kotlin/io/github/jixiaoyong/pages/settings/Settings.kt +++ b/src/main/kotlin/io/github/jixiaoyong/pages/settings/Settings.kt @@ -15,7 +15,10 @@ import androidx.compose.material.* import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.ClipboardManager +import androidx.compose.ui.platform.LocalClipboardManager import androidx.compose.ui.platform.LocalUriHandler +import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.text.SpanStyle import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.buildAnnotatedString @@ -60,6 +63,7 @@ fun PageSettingInfo() { val i18nString = strings val lyricist = LocalLyricist.current val repository = LocalDatastore.current + val clipboard = LocalClipboardManager.current val viewModel = viewModel { SettingInfoViewModel(repository) } val uiState by viewModel.uiState.collectAsState() @@ -169,12 +173,30 @@ fun PageSettingInfo() { if (chooseFileName.isNullOrBlank()) { showToast(i18nString.chooseBuildTools, ToastConfig.DURATION.Long) } else { - viewModel.setupBuildToolsConfig(chooseFileName) + val result = viewModel.setupBuildToolsConfig(chooseFileName) + showErrorMsg( + scaffoldState, + result, + i18nString.changeSuccess, + i18nString.copyErrorMsg, + clipboard + ) } } }, component = JPanel(), - onFileDrop = { scope.launch { viewModel.setupBuildToolsConfig(it.first()) } }) { + onFileDrop = { + scope.launch { + val result = viewModel.setupBuildToolsConfig(it.first()) + showErrorMsg( + scaffoldState, + result, + i18nString.changeSuccess, + i18nString.copyErrorMsg, + clipboard + ) + } + }) { Text( text = i18nString.chooseBuildToolsTips, color = MaterialTheme.colors.onSurface, @@ -195,7 +217,13 @@ fun PageSettingInfo() { } else { val result = ApkSigner.setupApkSigner(chooseFileName) viewModel.saveApkSigner(ApkSigner.apkSignerPath) - showToast(result ?: i18nString.changeSuccess) + showErrorMsg( + scaffoldState, + result, + i18nString.changeSuccess, + i18nString.copyErrorMsg, + clipboard + ) } } @@ -212,7 +240,13 @@ fun PageSettingInfo() { } else { val result = ApkSigner.setupZipAlign(chooseFileName) viewModel.saveZipAlign(ApkSigner.zipAlignPath) - showToast(result ?: i18nString.changeSuccess) + showErrorMsg( + scaffoldState, + result, + i18nString.changeSuccess, + i18nString.copyErrorMsg, + clipboard + ) } } }) @@ -259,7 +293,13 @@ fun PageSettingInfo() { } else { val result = ApkSigner.setupAapt(chooseFileName) viewModel.saveAapt(ApkSigner.aaptPath) - showToast(result ?: i18nString.changeSuccess) + showErrorMsg( + scaffoldState, + result, + i18nString.changeSuccess, + i18nString.copyErrorMsg, + clipboard + ) } } }) @@ -292,7 +332,7 @@ fun PageSettingInfo() { val result = viewModel.openLogDirectory() if (!result) { showToast(i18nString.openLogDirectoryFailed) - }else{ + } else { showToast(i18nString.openLogDirectorySucceed) } }) {} @@ -339,3 +379,26 @@ fun PageSettingInfo() { } } + +private suspend inline fun showErrorMsg( + scaffoldState: ScaffoldState, + msg: String?, + successMsg: String, + actionLabel: String, + clipboard: ClipboardManager +) { + if (msg.isNullOrBlank()) { + showToast(successMsg, ToastConfig.DURATION.Long) + return + } + + val result = scaffoldState.snackbarHostState.showSnackbar( + msg, + actionLabel = actionLabel, + duration = SnackbarDuration.Long + ) + + if (SnackbarResult.ActionPerformed == result) { + clipboard.setText(AnnotatedString(msg)) + } +} \ No newline at end of file diff --git a/src/main/kotlin/io/github/jixiaoyong/utils/ApkSigner.kt b/src/main/kotlin/io/github/jixiaoyong/utils/ApkSigner.kt index 3f7a179..b4b96c1 100644 --- a/src/main/kotlin/io/github/jixiaoyong/utils/ApkSigner.kt +++ b/src/main/kotlin/io/github/jixiaoyong/utils/ApkSigner.kt @@ -44,7 +44,7 @@ object ApkSigner { val buildTools = if (androidBuildToolsDir.isNullOrBlank()) return "" else File(androidBuildToolsDir) if (!buildTools.exists() || !buildTools.isDirectory) { - return "指定的Android SDK中build-tools目录无效或不存在,请重新选择。\n该目录一般为${ANDROID_BUILD_TOOLS_DIR_EXAMPLE}" + return "指定的Android SDK中build-tools目录无效或不存在($androidBuildToolsDir),请重新选择。\n该目录一般为${ANDROID_BUILD_TOOLS_DIR_EXAMPLE}" } val apkSignerPath = "$androidBuildToolsDir${File.separator}apksigner" @@ -89,15 +89,15 @@ object ApkSigner { // 校验文件是否为AOSP提供的apk signer,而非本APP避免误操作导致无限循环启动 val apkSignerFile = File(apkSignerPath) if (!apkSignerFile.exists()) { - return "apkSigner命令不存在,请重新选择。" + return "apkSigner命令(${apkSignerPath})不存在,请重新选择。" } else if (!apkSignerFile.readText().contains(AOSP_NAME)) { - return "apkSigner命令不是${AOSP_NAME}官方提供的,请重新选择。" + return "apkSigner命令(${apkSignerPath})不是${AOSP_NAME}官方提供的,请重新选择。" } val result = RunCommandUtil.runCommand("$apkSignerPath version", "apk signer") return if (result != null) { - "apkSigner命令检查失败,请重试(${result.message})" + "apkSigner命令(${apkSignerPath})检查失败,请重试(${result.message})" } else { apkSignerCmdPath = apkSignerPath null @@ -114,7 +114,7 @@ object ApkSigner { } return if (null != result) { - "zipAlign命令检查失败,请重试(${result.message})" + "zipAlign命令(${zipAlignPath})检查失败,请重试(${result.message})" } else { zipAlignCmdPath = zipAlignPath null @@ -131,7 +131,7 @@ object ApkSigner { } return if (null != result) { - "aapt命令检查失败,请重试(${result.message})" + "aapt命令(${aaptPath})检查失败,请重试(${result.message})" } else { aaptCmdPath = aaptPath null