From daa687984ebd63497fef4f3ff91bfe14d6cac201 Mon Sep 17 00:00:00 2001 From: KyuubiRan Date: Mon, 26 Apr 2021 22:44:48 +0800 Subject: [PATCH] Bug fix: sometimes not sync settings with cfg file. --- .../qqcleaner/activity/SettingsActivity.kt | 35 ++++++++++++------ .../kyuubiran/qqcleaner/utils/CleanManager.kt | 4 +-- .../qqcleaner/utils/ConfigManager.kt | 36 ++++++++++++++++--- app/src/main/res/values/strings.xml | 9 ++--- 4 files changed, 62 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/me/kyuubiran/qqcleaner/activity/SettingsActivity.kt b/app/src/main/java/me/kyuubiran/qqcleaner/activity/SettingsActivity.kt index eb1031c..58092a2 100644 --- a/app/src/main/java/me/kyuubiran/qqcleaner/activity/SettingsActivity.kt +++ b/app/src/main/java/me/kyuubiran/qqcleaner/activity/SettingsActivity.kt @@ -31,10 +31,14 @@ import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_DO_NOT_DISTURB_ENABLED import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_POWER_MODE_ENABLED import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_TOTAL_CLEANED_SIZE import me.kyuubiran.qqcleaner.utils.ConfigManager.checkCfg +import me.kyuubiran.qqcleaner.utils.ConfigManager.getBool import me.kyuubiran.qqcleaner.utils.ConfigManager.getInt +import me.kyuubiran.qqcleaner.utils.ConfigManager.getJsonArray import me.kyuubiran.qqcleaner.utils.ConfigManager.getLong -import me.kyuubiran.qqcleaner.utils.ConfigManager.setArray +import me.kyuubiran.qqcleaner.utils.ConfigManager.getString import me.kyuubiran.qqcleaner.utils.ConfigManager.setConfig +import me.kyuubiran.qqcleaner.utils.ConfigManager.setJsonArray +import me.kyuubiran.qqcleaner.utils.ConfigManager.toHashSet import me.kyuubiran.qqcleaner.utils.HostApp import me.kyuubiran.qqcleaner.utils.formatSize import me.kyuubiran.qqcleaner.utils.isInNightMode @@ -112,12 +116,12 @@ class SettingsActivity : Activity() { //初始化函数 private fun init() { - initSummary() + setCustomerCleanList() + refreshCleanedSize() + initItemStatus() toggleSwitchItemCtrl() setClickable() - setVersionName() - setCustomerCleanList() - setArray(CFG_CUSTOMER_CLEAN_LIST, customerCleanList.values as HashSet) + setSummary() } private fun setCustomerCleanList() { @@ -137,6 +141,15 @@ class SettingsActivity : Activity() { } } + private fun initItemStatus() { + autoClean.isChecked = getBool(CFG_AUTO_CLEAN_ENABLED) + powerMode.isChecked = getBool(CFG_POWER_MODE_ENABLED) + enableDateLimit.isChecked = getBool(CFG_DATE_LIMIT_ENABLED) + doNotDisturb.isChecked = getBool(CFG_DO_NOT_DISTURB_ENABLED) + autoCleanMode.value = getString(CFG_AUTO_CLEAN_MODE, HALF_MODE) + customerCleanList.values = getJsonArray(CFG_CUSTOMER_CLEAN_LIST)?.toHashSet() + } + //设置Item点击事件 private fun setClickable() { halfClean.setOnPreferenceClickListener { @@ -150,7 +163,7 @@ class SettingsActivity : Activity() { customerCleanList.setOnPreferenceChangeListener { _, newValue -> try { @Suppress("UNCHECKED_CAST") - setArray(CFG_CUSTOMER_CLEAN_LIST, newValue as HashSet) + setJsonArray(CFG_CUSTOMER_CLEAN_LIST, newValue as HashSet) appContext.show("好耶 保存自定义瘦身列表成功了!") } catch (e: Exception) { Log.e(e) @@ -201,7 +214,7 @@ class SettingsActivity : Activity() { } cleanedHistory.setOnPreferenceClickListener { appContext.show("已刷新统计信息") - initSummary() + refreshCleanedSize() true } cleanDelay.setOnPreferenceClickListener { @@ -286,7 +299,7 @@ class SettingsActivity : Activity() { } } - private fun initSummary() { + private fun refreshCleanedSize() { //腾出空间 if (getInt(CFG_TOTAL_CLEANED_SIZE) != 0) { cleanedHistory.summary = @@ -294,14 +307,14 @@ class SettingsActivity : Activity() { } else { cleanedHistory.setSummary(R.string.no_cleaned_his_hint) } + } + + private fun setSummary() { //自动瘦身 autoClean.summary = if (autoClean.isEnabled) "当前清理的间隔为${getInt(CFG_CLEAN_DELAY, 24)}小时" else "未开启" //设置清理超过日期 setDateLimit.summary = "当前会清理存在超过${getInt(CFG_DATE_LIMIT, 3)}天的文件" - } - - private fun setVersionName() { moduleInfo.summary = "${BuildConfig.VERSION_NAME}(${BuildConfig.VERSION_CODE})" } } diff --git a/app/src/main/java/me/kyuubiran/qqcleaner/utils/CleanManager.kt b/app/src/main/java/me/kyuubiran/qqcleaner/utils/CleanManager.kt index 8f652d9..d7ba8ee 100644 --- a/app/src/main/java/me/kyuubiran/qqcleaner/utils/CleanManager.kt +++ b/app/src/main/java/me/kyuubiran/qqcleaner/utils/CleanManager.kt @@ -13,9 +13,9 @@ import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_DATE_LIMIT_ENABLED import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_DO_NOT_DISTURB_ENABLED import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_POWER_MODE_ENABLED import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_TOTAL_CLEANED_SIZE -import me.kyuubiran.qqcleaner.utils.ConfigManager.getArray import me.kyuubiran.qqcleaner.utils.ConfigManager.getBool import me.kyuubiran.qqcleaner.utils.ConfigManager.getInt +import me.kyuubiran.qqcleaner.utils.ConfigManager.getJsonArray import me.kyuubiran.qqcleaner.utils.ConfigManager.getLong import me.kyuubiran.qqcleaner.utils.ConfigManager.getString import me.kyuubiran.qqcleaner.utils.clean.CleanQQ @@ -44,7 +44,7 @@ object CleanManager { * @return 获取用户自定义的瘦身列表 */ private fun getCustomerList(): ArrayList { - val customerList = getArray(ConfigManager.CFG_CUSTOMER_CLEAN_LIST) + val customerList = getJsonArray(ConfigManager.CFG_CUSTOMER_CLEAN_LIST) val arr = ArrayList() customerList?.forEach { s -> when (hostApp) { diff --git a/app/src/main/java/me/kyuubiran/qqcleaner/utils/ConfigManager.kt b/app/src/main/java/me/kyuubiran/qqcleaner/utils/ConfigManager.kt index 93138ba..02ffc3d 100644 --- a/app/src/main/java/me/kyuubiran/qqcleaner/utils/ConfigManager.kt +++ b/app/src/main/java/me/kyuubiran/qqcleaner/utils/ConfigManager.kt @@ -45,7 +45,9 @@ object ConfigManager { } private fun checkArrayHasValue(key: String, defValue: ArrayList) { - setArray(key, defValue) + if (getJsonArray(key).isNullOrEmpty()) { + setJsonArray(key, defValue) + } } private fun getConfig(): JSONObject? { @@ -129,7 +131,7 @@ object ConfigManager { } } - fun setArray(key: String, arr: ArrayList) { + fun setJsonArray(key: String, arr: ArrayList) { val jsonArray = JSONArray() for (v in arr) { jsonArray.put(v) @@ -137,7 +139,7 @@ object ConfigManager { setConfig(key, jsonArray) } - fun setArray(key: String, hs: HashSet) { + fun setJsonArray(key: String, hs: HashSet) { val jsonArray = JSONArray() for (v in hs) { jsonArray.put(v) @@ -145,7 +147,7 @@ object ConfigManager { setConfig(key, jsonArray) } - fun setArray(key: String, arr: Array) { + fun setJsonArray(key: String, arr: Array) { val jsonArray = JSONArray() for (v in arr) { jsonArray.put(v) @@ -153,10 +155,34 @@ object ConfigManager { setConfig(key, jsonArray) } - fun getArray(key: String): JSONArray? { + fun getJsonArray(key: String): JSONArray? { return getConfig()?.getJSONArray(key) } + fun JSONArray.toArrayList(): ArrayList { + val arr = ArrayList() + this.forEach { arr.add(it) } + return arr + } + + inline fun JSONArray.toArray(): Array { + return this.toArrayList().toTypedArray() + } + + fun JSONArray.toHashSet(): HashSet { + val hs = HashSet() + this.forEach { hs.add(it) } + return hs + } + + fun JSONArray?.isNotNullOrEmpty(): Boolean { + return this != null || this?.length() != 0 + } + + fun JSONArray?.isNullOrEmpty(): Boolean { + return !this.isNotNullOrEmpty() + } + private fun save(jsonObject: JSONObject) { save(jsonObject.toString()) } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0b5892a..a0b62f3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -43,10 +43,11 @@ 更新日志 -1.7.0(34)\n 1.极限压缩APK体积\n - 2.修复强力模式开关无效的问题(如果你已经开启了强力模式 则需要再手动开关一次)\n - 3.[QQ/TIM]修改部分瘦身内容 新增一些路径\n - 4.新增静默模式(不弹出清理Toast)\n - 5.修改Toast样式 + 2.修复强力模式开关无效的问题\n + 3.现在瘦身设置完全由配置文件控制 避免一些设置与配置文件不同步的BUG\n + 4.[QQ/TIM]修改部分瘦身内容 新增一些路径\n + 5.新增静默模式(不弹出清理Toast)\n + 6.修改Toast样式 点击加入QQ群 有问题可以来群里反馈哦~