Skip to content

Commit

Permalink
Add total cleaned size
Browse files Browse the repository at this point in the history
  • Loading branch information
KyuubiRan committed Nov 16, 2020
1 parent c5d9f1e commit 58ff793
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 27 deletions.
3 changes: 2 additions & 1 deletion app/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/build
/build
/release/
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "me.kyuubiran.qqcleaner"
minSdkVersion 21
targetSdkVersion 30
versionCode 13
versionName "1.2.1"
versionCode 14
versionName "1.2.2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import me.kyuubiran.qqcleaner.utils.*
import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_AUTO_CLEAN_ENABLED
import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_CURRENT_CLEANED_TIME
import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_CUSTOMER_CLEAN_LIST
import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_TOTAL_CLEANED_SIZE
import me.kyuubiran.qqcleaner.utils.ConfigManager.checkCfg
import me.kyuubiran.qqcleaner.utils.ConfigManager.getConfig
import me.kyuubiran.qqcleaner.utils.ConfigManager.getLong
import me.kyuubiran.qqcleaner.utils.ConfigManager.setConfig
import java.lang.Exception
import java.text.SimpleDateFormat

Expand All @@ -28,10 +33,12 @@ class SettingsActivity : AppCompatTransferActivity() {
.replace(R.id.settings, SettingsFragment())
.commit()
supportActionBar?.setDisplayHomeAsUpEnabled(true)
checkCfg()
}

class SettingsFragment : PreferenceFragmentCompat() {
private lateinit var autoClean: SwitchPreferenceCompat
private lateinit var cleanedHistory: Preference
private lateinit var autoCleanMode: ListPreference
private lateinit var cleanedTime: Preference
private lateinit var halfClean: Preference
Expand All @@ -46,6 +53,7 @@ class SettingsActivity : AppCompatTransferActivity() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)
autoClean = findPreference("AutoClean")!!
cleanedHistory = findPreference("CleanedHistory")!!
autoCleanMode = findPreference("AutoCleanMode")!!
cleanedTime = findPreference("CleanedTime")!!
halfClean = findPreference("HalfClean")!!
Expand All @@ -58,6 +66,7 @@ class SettingsActivity : AppCompatTransferActivity() {
}

private fun init() {
setHistorySummary()
toggleCleanedTimeShow()
setClickable()
}
Expand All @@ -73,7 +82,7 @@ class SettingsActivity : AppCompatTransferActivity() {
}
customerCleanList.setOnPreferenceChangeListener { _, newValue ->
try {
ConfigManager.setConfig(CFG_CUSTOMER_CLEAN_LIST, newValue)
setConfig(CFG_CUSTOMER_CLEAN_LIST, newValue)
qqContext?.showToastBySystem("好耶 保存自定义瘦身列表成功了!")
} catch (e: Exception) {
loge(e)
Expand Down Expand Up @@ -105,12 +114,17 @@ class SettingsActivity : AppCompatTransferActivity() {
}
} else {
clicked = 0
ConfigManager.setConfig(CFG_CURRENT_CLEANED_TIME, 0)
cleanedTime.setSummary(R.string.auto_clean_time_hint)
setConfig(CFG_CURRENT_CLEANED_TIME, 0)
cleanedTime.setSummary(R.string.no_cleaned_his_hint)
qqContext?.showToastBySystem("已重置清理时间")
}
true
}
cleanedHistory.setOnPreferenceClickListener {
qqContext?.showToastBySystem("已刷新统计信息")
setHistorySummary()
true
}
}

private fun onClickCleanHalf() {
Expand All @@ -122,23 +136,33 @@ class SettingsActivity : AppCompatTransferActivity() {
}

private fun toggleCleanedTimeShow() {
if (ConfigManager.getLong(CFG_CURRENT_CLEANED_TIME).toString() == "null" ||
ConfigManager.getLong(CFG_CURRENT_CLEANED_TIME) == 0L
setConfig(CFG_AUTO_CLEAN_ENABLED, autoClean.isChecked)
if (getLong(CFG_CURRENT_CLEANED_TIME).toString() == "null" ||
getLong(CFG_CURRENT_CLEANED_TIME) == 0L
) {
cleanedTime.setSummary(R.string.auto_clean_time_hint)
cleanedTime.setSummary(R.string.no_cleaned_his_hint)
} else {
val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
cleanedTime.summary = format.format(ConfigManager.getLong(CFG_CURRENT_CLEANED_TIME))
cleanedTime.summary = format.format(getLong(CFG_CURRENT_CLEANED_TIME))
}
cleanedTime.isVisible = autoClean.isChecked
autoCleanMode.isVisible = autoClean.isChecked
autoClean.onPreferenceChangeListener =
Preference.OnPreferenceChangeListener { _, newValue ->
cleanedTime.isVisible = newValue as Boolean
autoCleanMode.isVisible = newValue
ConfigManager.setConfig(CFG_AUTO_CLEAN_ENABLED, newValue)
setConfig(CFG_AUTO_CLEAN_ENABLED, newValue)
true
}
}

private fun setHistorySummary() {
if (getConfig(CFG_TOTAL_CLEANED_SIZE) != 0) {
cleanedHistory.summary =
"总共为您腾出:${getLong(CFG_TOTAL_CLEANED_SIZE)?.let { it2 -> formatSize(it2) }}空间"
} else {
cleanedHistory.setSummary(R.string.no_cleaned_his_hint)
}
}
}
}
23 changes: 18 additions & 5 deletions app/src/main/java/me/kyuubiran/qqcleaner/utils/CleanManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import com.alibaba.fastjson.JSONArray
import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_AUTO_CLEAN_ENABLED
import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_CURRENT_CLEANED_TIME
import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_CUSTOMER_CLEAN_MODE
import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_TOTAL_CLEANED_SIZE
import me.kyuubiran.qqcleaner.utils.ConfigManager.getConfig
import me.kyuubiran.qqcleaner.utils.ConfigManager.getLong
import java.io.File
import kotlin.concurrent.thread

Expand Down Expand Up @@ -35,6 +38,8 @@ object CleanManager {
const val RECEIVE_FILE_CACHE = "receive_file_cache"
const val OTHERS = "others"

private var size = 0L

private fun getFiles(item: String): ArrayList<File> {
val arr = ArrayList<File>()
when (item) {
Expand Down Expand Up @@ -155,8 +160,13 @@ object CleanManager {
return arr
}

private fun saveSize() {
val totalSize = getLong(CFG_TOTAL_CLEANED_SIZE)?.plus(size) ?: 0
ConfigManager.setConfig(CFG_TOTAL_CLEANED_SIZE, totalSize)
}

private fun getCustomerList(): ArrayList<File> {
val customerList = ConfigManager.getConfig(CFG_CUSTOMER_CLEAN_LIST) as JSONArray
val customerList = getConfig(CFG_CUSTOMER_CLEAN_LIST) as JSONArray
val arr = ArrayList<File>()
for (s in customerList) {
arr.addAll(getFiles(s.toString()))
Expand All @@ -178,12 +188,14 @@ object CleanManager {

private fun doClean(files: ArrayList<File>, showToast: Boolean = true) {
thread {
size = 0L
if (showToast) qqContext?.showToastBySystem("好耶 开始清理了!")
try {
for (f in files) {
deleteAllFiles(f)
}
qqContext?.showToastBySystem("好耶 清理完毕了!")
qqContext?.showToastBySystem("好耶 清理完毕了!腾出了${formatSize(size)}空间!")
saveSize()
} catch (e: Exception) {
loge(e)
qqContext?.showToastBySystem("坏耶 清理失败了!")
Expand All @@ -193,6 +205,7 @@ object CleanManager {

private fun deleteAllFiles(file: File) {
if (file.isFile) {
size += file.length()
file.delete()
return
}
Expand All @@ -214,9 +227,9 @@ object CleanManager {
private var mode = ""

init {
time = ConfigManager.getLong(CFG_CURRENT_CLEANED_TIME) ?: 0L
if (ConfigManager.getConfig(CFG_AUTO_CLEAN_ENABLED) as Boolean && System.currentTimeMillis() - time > 86400000) {
mode = ConfigManager.getConfig(CFG_CUSTOMER_CLEAN_MODE).toString()
time = getLong(CFG_CURRENT_CLEANED_TIME) ?: 0L
if (getConfig(CFG_AUTO_CLEAN_ENABLED) as Boolean && System.currentTimeMillis() - time > 86400000) {
mode = getConfig(CFG_CUSTOMER_CLEAN_MODE).toString()
autoClean()
time = System.currentTimeMillis()
ConfigManager.setConfig(CFG_CURRENT_CLEANED_TIME, time)
Expand Down
24 changes: 22 additions & 2 deletions app/src/main/java/me/kyuubiran/qqcleaner/utils/ConfigManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,38 @@ object ConfigManager {
const val CFG_CURRENT_CLEANED_TIME = "cleanedTime"
const val CFG_CUSTOMER_CLEAN_LIST = "customerList"
const val CFG_CUSTOMER_CLEAN_MODE = "customerCleanMode"
const val CFG_TOTAL_CLEANED_SIZE = "totalCleanedSize"

fun checkConfigIsExists() {
if (!config.exists()) {
config.createNewFile()
save("{}")
setConfig(CFG_CURRENT_CLEANED_TIME, 0)
}
}

fun checkCfg() {
checkConfigIsExists()
checkConfigKeyHasValue(CFG_CURRENT_CLEANED_TIME, 0)
checkConfigKeyHasValue(CFG_TOTAL_CLEANED_SIZE, 0)
}

private fun checkConfigKeyHasValue(key: String, defValue: Any): Boolean {
return if (getConfig(key) == null || getConfig(key).toString() == "null") {
setConfig(key, defValue)
false
} else true
}

private fun getConfig(): JSONObject? {
checkConfigIsExists()
return JSONObject.parseObject(config.readText(Charsets.UTF_8))
return try {
JSONObject.parseObject(config.readText(Charsets.UTF_8))
} catch (e: Exception) {
loge(e)
config.delete()
checkCfg()
JSONObject.parseObject(config.readText(Charsets.UTF_8))
}
}

fun getConfig(key: String): Any? {
Expand Down
39 changes: 39 additions & 0 deletions app/src/main/java/me/kyuubiran/qqcleaner/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import java.lang.reflect.Constructor
import java.lang.reflect.Field
import java.lang.reflect.Method
import java.lang.reflect.Modifier
import java.math.BigDecimal

private lateinit var mHandler: Handler
lateinit var clzLoader: ClassLoader
Expand Down Expand Up @@ -164,6 +165,44 @@ fun newInstance(clazz: Class<*>, vararg argsAndTypes: Any?): Any? {
}
}

fun formatSize(size: Long): String {
return formatSize(size.toString())
}

fun formatSize(size: String): String {
val sl = BigDecimal(size)
val b: BigDecimal
val result: Double
return when {
size.length in 0..3 -> {
" $sl Byte "
}
size.length in 4..6 -> {
b = sl.divide(BigDecimal(1_024.0))
result = b.setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()
" $result KB "
}
size.length in 7..9 -> {
b = sl.divide(BigDecimal(1_048_576.0))
result = b.setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()
" $result MB "
}
size.length in 10..12 -> {
b = sl.divide(BigDecimal(1_073_741_824.0))
result = b.setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()
" $result GB "
}
size.length > 12 -> {
b = sl.divide(BigDecimal(1_099_511_627_776.0))
result = b.setScale(2, BigDecimal.ROUND_HALF_UP).toDouble()
" $result TB "
}
else -> {
""
}
}
}

fun Method.isStatic(): Boolean {
return Modifier.isStatic(this.modifiers)
}
Expand Down
13 changes: 5 additions & 8 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<resources>
<string name="app_name">QQ瘦身</string>
<string name="version_name">1.2.2</string>
<string name="title_activity_settings">QQ瘦身</string>
<string name="res_inject_success">注入资源成功</string>

Expand All @@ -9,7 +10,7 @@
<string name="auto_clean_switch_key">启用自动瘦身</string>
<string name="auto_clean_time">上次自动瘦身时间</string>
<string name="auto_clean_mode">瘦身方案</string>
<string name="auto_clean_time_hint">还没有记录哦~</string>
<string name="no_cleaned_his_hint">还没有记录哦~</string>
<string name="auto_clean_mode_hint">三选一哦</string>

<string name="clean_title">清理缓存</string>
Expand All @@ -25,20 +26,16 @@

<string name="other_title">其他</string>

<string name="enable_cleaned_history">清理统计(点我可刷新)</string>
<string name="module_version">模块版本</string>
<string name="version_name">1.2.1</string>
<string name="goto_github">点我前往项目地址</string>
<string name="author">模块作者:KyuubiRan</string>
<string name="support_me">点我扶贫</string>
<string name="support_me_hint">本模块完全免费开源 一切开发旨在学习 请勿用于非法用途 喜欢本模块的可以捐赠支持我 谢谢~</string>
<string name="about">公告</string>
<string name="about_hint">注意:本模块不会清理聊天记录以及接收文件等重要东西\n
[更新日志]\n
-1.2.1\n
修改提示\n
-1.2\n
修改UI 增加自动瘦身\n
-1.1\n
增加自定义瘦身
-1.2.2\n
增加瘦身统计
</string>
</resources>
6 changes: 5 additions & 1 deletion app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<Preference
app:key="CleanedTime"
app:title="@string/auto_clean_time"
app:summary="@string/auto_clean_time_hint"
app:summary="@string/no_cleaned_his_hint"
app:isPreferenceVisible="false"/>
</PreferenceCategory>

Expand All @@ -44,6 +44,10 @@
</PreferenceCategory>
<PreferenceCategory
app:title="@string/other_title">
<Preference
app:key="CleanedHistory"
app:title="@string/enable_cleaned_history"
app:summary="@string/no_cleaned_his_hint"/>
<Preference
app:key="ModuleInfo"
app:title="@string/module_version"
Expand Down

0 comments on commit 58ff793

Please sign in to comment.