Skip to content

Commit

Permalink
Add Power mode(use shell cmd to delete file)
Browse files Browse the repository at this point in the history
Add File date limit(only clean out of date files)
Add user dir null-check
  • Loading branch information
KyuubiRan committed Mar 11, 2021
1 parent 7c3ac66 commit d6f8455
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 19 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "me.kyuubiran.qqcleaner"
minSdkVersion 21
targetSdkVersion 30
versionCode 27
versionName "1.4.4"
versionCode 28
versionName "1.5.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import me.kyuubiran.qqcleaner.R
import me.kyuubiran.qqcleaner.data.hostApp
import me.kyuubiran.qqcleaner.dialog.*
import me.kyuubiran.qqcleaner.dialog.CleanDialog.showConfirmDialog
import me.kyuubiran.qqcleaner.dialog.CleanDialog.showSetFileDateLimitDialog
import me.kyuubiran.qqcleaner.utils.*
import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_AUTO_CLEAN_ENABLED
import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_CLEAN_DELAY
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_DATE_LIMIT
import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_DATE_LIMIT_ENABLED
import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_TOTAL_CLEANED_SIZE
import me.kyuubiran.qqcleaner.utils.ConfigManager.checkCfg
import me.kyuubiran.qqcleaner.utils.ConfigManager.getConfig
Expand Down Expand Up @@ -44,10 +47,16 @@ class SettingsActivity : AppCompatTransferActivity() {
private lateinit var autoCleanMode: ListPreference
private lateinit var cleanedTime: Preference
private lateinit var cleanDelay: Preference

private lateinit var halfClean: Preference
private lateinit var fullClean: Preference
private lateinit var customerCleanList: MultiSelectListPreference
private lateinit var doCustomerClean: Preference

private lateinit var powerMode: SwitchPreferenceCompat
private lateinit var enableDateLimit: SwitchPreferenceCompat
private lateinit var setDateLimit: Preference

private lateinit var supportMe: Preference
private lateinit var gotoGithub: Preference
private lateinit var joinQQGroup: Preference
Expand All @@ -65,10 +74,16 @@ class SettingsActivity : AppCompatTransferActivity() {
autoCleanMode = findPreference("AutoCleanMode")!!
cleanedTime = findPreference("CleanedTime")!!
cleanDelay = findPreference("CleanDelay")!!

halfClean = findPreference("HalfClean")!!
fullClean = findPreference("FullClean")!!
customerCleanList = findPreference("CustomerClean")!!
doCustomerClean = findPreference("DoCustomerClean")!!

powerMode = findPreference("PowerMode")!!
enableDateLimit = findPreference("EnableDateLimit")!!
setDateLimit = findPreference("SetDateLimit")!!

gotoGithub = findPreference("GotoGithub")!!
supportMe = findPreference("SupportMe")!!
joinQQGroup = findPreference("JoinQQGroup")!!
Expand All @@ -80,7 +95,7 @@ class SettingsActivity : AppCompatTransferActivity() {
//初始化函数
private fun init() {
initSummary()
toggleCleanedTimeShow()
toggleSwitchItemCtrl()
setClickable()
setVersionName()
setCustomerCleanList()
Expand Down Expand Up @@ -123,6 +138,11 @@ class SettingsActivity : AppCompatTransferActivity() {
}
true
}
setDateLimit.setOnPreferenceClickListener {
showSetFileDateLimitDialog(this.requireContext(), it)
true
}

doCustomerClean.setOnPreferenceClickListener {
showConfirmDialog(CUSTOMER_MODE, this.requireContext())
true
Expand Down Expand Up @@ -177,8 +197,8 @@ class SettingsActivity : AppCompatTransferActivity() {
startActivity(intent)
}

//切换自动瘦身时间是否显示
private fun toggleCleanedTimeShow() {
private fun toggleSwitchItemCtrl() {
//自动瘦身是否显示
val currentCleanedTime = getLong(CFG_CURRENT_CLEANED_TIME)
setConfig(CFG_AUTO_CLEAN_ENABLED, autoClean.isChecked)
if (currentCleanedTime == 0L) {
Expand All @@ -201,24 +221,37 @@ class SettingsActivity : AppCompatTransferActivity() {
autoCleanMode.isVisible = newValue
cleanDelay.isVisible = newValue
setConfig(CFG_AUTO_CLEAN_ENABLED, newValue)
autoClean.summary =
if (newValue) "当前清理的间隔为${getInt(CFG_CLEAN_DELAY, 24)}小时" else "未开启"
true
}
//设置清理超过日期是否显示
setDateLimit.isVisible = enableDateLimit.isChecked
enableDateLimit.onPreferenceChangeListener =
Preference.OnPreferenceChangeListener { _, newValue ->
setDateLimit.isVisible = newValue as Boolean
setConfig(CFG_DATE_LIMIT_ENABLED, newValue)
true
}
}

private fun initSummary() {
//腾出空间
if (getConfig(CFG_TOTAL_CLEANED_SIZE) != 0) {
cleanedHistory.summary =
"总共为您腾出:${formatSize(getLong(CFG_TOTAL_CLEANED_SIZE))}空间"
} else {
cleanedHistory.setSummary(R.string.no_cleaned_his_hint)
}

val delayTmp = getInt(CFG_CLEAN_DELAY)
autoClean.summary = if (delayTmp == 0) "当前清理的间隔为24小时" else "当前清理的间隔为${delayTmp}小时"
//自动瘦身
autoClean.summary =
if (autoClean.isVisible) "当前清理的间隔为${getInt(CFG_CLEAN_DELAY, 24)}小时" else "未开启"
//设置清理超过日期
setDateLimit.summary = "当前会清理存在超过${getInt(CFG_DATE_LIMIT, 3)}天的文件"
}

private fun setVersionName() {
moduleInfo.summary = BuildConfig.VERSION_NAME
moduleInfo.summary = "${BuildConfig.VERSION_NAME}(${BuildConfig.VERSION_CODE})"
}
}
}
22 changes: 22 additions & 0 deletions app/src/main/java/me/kyuubiran/qqcleaner/dialog/CleanDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import android.text.InputFilter
import android.text.InputType
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import androidx.preference.Preference
import androidx.preference.SwitchPreferenceCompat
import me.kyuubiran.qqcleaner.utils.CleanManager
import me.kyuubiran.qqcleaner.utils.ConfigManager
import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_CLEAN_DELAY
import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_DATE_LIMIT
import me.kyuubiran.qqcleaner.utils.makeToast

const val HALF_MODE = 0
Expand Down Expand Up @@ -64,4 +66,24 @@ object CleanDialog {
.create()
.show()
}

fun showSetFileDateLimitDialog(context: Context, pf: Preference) {
val input = EditText(context)
input.setText(ConfigManager.getInt(CFG_DATE_LIMIT, 3).toString())
input.inputType = InputType.TYPE_CLASS_NUMBER
input.filters = arrayOf(InputFilter.LengthFilter(5))
AlertDialog.Builder(context)
.setView(input)
.setTitle("设置过期文件时间(天)")
.setPositiveButton("确定") { _, _ ->
var num = input.text.toString().toInt()
num = if (num < 1) 3 else num
ConfigManager.setConfig(CFG_DATE_LIMIT, num)
context.makeToast("保存成功!")
pf.summary = "当前会清理存在超过${num}天的文件"
}
.setNegativeButton("取消") { _, _ -> }
.create()
.show()
}
}
53 changes: 46 additions & 7 deletions app/src/main/java/me/kyuubiran/qqcleaner/utils/CleanManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_AUTO_CLEAN_ENABLED
import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_CLEAN_DELAY
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_DATE_LIMIT
import me.kyuubiran.qqcleaner.utils.ConfigManager.CFG_DATE_LIMIT_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.getBool
import me.kyuubiran.qqcleaner.utils.ConfigManager.getConfig
import me.kyuubiran.qqcleaner.utils.ConfigManager.getInt
import me.kyuubiran.qqcleaner.utils.ConfigManager.getLong
import me.kyuubiran.qqcleaner.utils.clean.CleanQQ
import me.kyuubiran.qqcleaner.utils.clean.CleanTIM
import me.kyuubiran.qqcleaner.utils.clean.CleanWeChat
import java.io.File
import kotlin.concurrent.thread
Expand All @@ -22,6 +26,8 @@ object CleanManager {
const val FULL_MODE = "full_mode"
const val CUSTOMER_MODE = "customer_mode"

private val isCleanedByShell = getBool(CFG_POWER_MODE_ENABLED)

//计算清理完毕后的释放的空间
private var size = 0L

Expand Down Expand Up @@ -89,9 +95,12 @@ object CleanManager {
size = 0L
if (showToast) appContext?.makeToast("好耶 开始清理了!")
try {
val ofd = getInt(CFG_DATE_LIMIT, 3)
val ts = System.currentTimeMillis()
val lmtEnable = getBool(CFG_DATE_LIMIT_ENABLED)
for (f in files) {
// logi("开始清理${f.path}")
delFiles(f)
delFiles(f, lmtEnable, ofd, ts)
}
appContext?.makeToast("好耶 清理完毕了!腾出了${formatSize(size)}空间!")
saveSize()
Expand All @@ -102,16 +111,47 @@ object CleanManager {
}
}

private fun File.deleteSingleByShell() {
runtimeProc.exec("rm -f ${this.path}")
}

/**
* @param file 文件/文件夹
* @param limitEnable 是否开启天数过滤
* @param outOfDate 文件过期时间(天)
* @param ts 当前时间戳
* 删除文件/文件夹的函数
*/
private fun delFiles(file: File) {
private fun delFiles(
file: File,
limitEnable: Boolean = false,
outOfDate: Int = 3,
ts: Long = System.currentTimeMillis()
) {
if (!file.exists()) return
//清理用
fun delete(f: File) {
size += f.length()
if (isCleanedByShell) {
f.deleteSingleByShell()
} else {
f.delete()
}
}
//文件
if (file.isFile) {
size += file.length()
file.delete()
//如果需要过滤文件
if (limitEnable) {
val ofdTime = outOfDate * 24L * 60L * 60L * 1000L
if (ts - file.lastModified() > ofdTime) {
delete(file)
}
} else {
//不需要直接删
delete(file)
}
} else {
//文件夹
val list = file.listFiles()
if (list == null || list.isEmpty()) {
file.delete()
Expand All @@ -132,8 +172,7 @@ object CleanManager {
init {
time = getLong(CFG_CURRENT_CLEANED_TIME)
//判断间隔
if ((getConfig(CFG_AUTO_CLEAN_ENABLED)
?: false) as Boolean && System.currentTimeMillis() - time > if (delay < 3600_000L) 24 * 3600L * 1000L else delay
if (getBool(CFG_AUTO_CLEAN_ENABLED) && System.currentTimeMillis() - time > if (delay < 3600_000L) 24 * 3600L * 1000L else delay
) {
mode = getConfig(CFG_CUSTOMER_CLEAN_MODE).toString()
autoClean()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ object ConfigManager {
const val CFG_CUSTOMER_CLEAN_MODE = "customerCleanMode"
const val CFG_TOTAL_CLEANED_SIZE = "totalCleanedSize"
const val CFG_CLEAN_DELAY = "cleanDelay"
const val CFG_POWER_MODE_ENABLED = "powerModeEnabled"
const val CFG_DATE_LIMIT_ENABLED = "dateLimitEnabled"
const val CFG_DATE_LIMIT = "dateLimit"

fun checkConfigIsExists() {
if (!config.exists()) {
Expand Down
2 changes: 2 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 @@ -19,6 +19,8 @@ lateinit var clzLoader: ClassLoader
//宿主全局Context
var appContext: Context? = null

val runtimeProc: Runtime = Runtime.getRuntime()

var sModulePath: String = ""

class Utils(classLoader: ClassLoader) {
Expand Down
18 changes: 15 additions & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@
<string name="do_clean_cus">执行自定义瘦身</string>
<string name="do_clean_cus_hint">芜狐~起飞!</string>

<string name="clean_settings">瘦身设置</string>

<string name="enable_power_mode">强力模式</string>
<string name="enable_power_mode_hint">使用Shell命令强制删除文件</string>

<string name="file_date_limit">不删除指定时间内的文件</string>
<string name="file_date_limit_hint">单位:天 默认为3天</string>

<string name="set_file_date_limit">设置时间</string>

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

<string name="enable_cleaned_history">清理统计(点我可刷新)</string>
Expand All @@ -33,14 +43,16 @@
<string name="about">公告</string>
<string name="about_hint">注意:本模块不会清理聊天记录、接收文件、保存的图片、表情收藏等重要东西\n
[更新日志]\n
-1.4.4(27)\n
1.修复微信彻底瘦身目录不全的问题\n
注意:微信瘦身包含根目录文件夹 请谨慎清理
-1.5.0(28)\n
1.新增强力模式(通过Shell命令强制删除文件 默认关闭)\n
2.新增天数过滤(不清理n天内的文件 默认关闭 默认3天)\n
3.微信获取用户文件夹时做空安全检测 获取失败则会跳过
</string>
<string name="join_qq_group">点击加入QQ群</string>
<string name="join_qq_group_hint">有问题可以来群里反馈哦~</string>
<string name="auto_clean_delay">自动瘦身间隔</string>
<string name="auto_clean_delay_hint">单位:小时 默认24小时(即一天) 设置为0时默认24小时</string>
<string name="join_tg_channel">点击加入Telegram频道</string>
<string name="join_tg_channel_hint">频道:@QQCleanerCh\n聊天群组:@QQCleanerChat</string>

</resources>
16 changes: 16 additions & 0 deletions app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@
app:title="@string/do_clean_cus"
app:summary="@string/do_clean_cus_hint" />
</PreferenceCategory>

<PreferenceCategory app:title="@string/clean_settings">
<SwitchPreferenceCompat
app:key="PowerMode"
app:title="@string/enable_power_mode"
app:summary="@string/enable_power_mode_hint" />
<SwitchPreferenceCompat
app:key="EnableDateLimit"
app:title="@string/file_date_limit"
app:summary="@string/file_date_limit_hint" />
<Preference
app:key="SetDateLimit"
app:title="@string/set_file_date_limit"
app:isPreferenceVisible="false" />
</PreferenceCategory>

<PreferenceCategory app:title="@string/other_title">
<Preference
app:key="CleanedHistory"
Expand Down

0 comments on commit d6f8455

Please sign in to comment.