Merge branch 'hotfix-v5.8.0-511-setting_crash' into 'release'

修复快速进出设置界面可能的闪退问题

See merge request halo/android/assistant-android!244
This commit is contained in:
陈君陶
2022-05-11 15:07:29 +08:00

View File

@ -1,13 +1,17 @@
package com.halo.assistant.fragment
import android.annotation.SuppressLint
import android.app.Application
import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.view.View
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModelProvider
import com.airbnb.lottie.LottieAnimationView
import com.gh.common.constant.Config
@ -51,6 +55,8 @@ class SettingsFragment : NormalFragment() {
private var loadingDialog: Dialog? = null
private var mUsageStatus = false
private val mViewModel: SettingViewModel by lazy { viewModelProvider() }
override fun getLayoutId() = 0
override fun getInflatedLayout() = FragmentSettingBinding.inflate(layoutInflater).apply { mBinding = this }.root
@ -94,13 +100,12 @@ class SettingsFragment : NormalFragment() {
//判断隐私政策是否有更新
mBinding.privacyPolicyItem.redDot.visibility = if (checkPrivacyIsSame()) View.GONE else View.VISIBLE
runOnIoThread {
val cacheSize = getCacheSize()
runOnUiThread {
mBinding.cacheItem.contentTv.text = cacheSize
}
mViewModel.cacheSizeLiveData.observe(this) {
mBinding.cacheItem.contentTv.text = it
}
mViewModel.getCacheSize()
mBinding.personalRecommendItem.switchLottie.setSwitchAnimation(SPUtils.getBoolean(PERSONAL_RECOMMEND_SP_KEY, true))
checkSizeIndex = SPUtils.getInt(FONT_SIZE_SP_KEY, 1)
@ -377,7 +382,7 @@ class SettingsFragment : NormalFragment() {
if (loadingDialog != null) {
loadingDialog!!.dismiss()
}
mBinding.cacheItem.contentTv.text = getCacheSize()
mViewModel.getCacheSize()
Utils.toast(context, "缓存清除成功")
}
@ -446,45 +451,6 @@ class SettingsFragment : NormalFragment() {
return currentMd5 == privacyMd5
}
// 获取缓存大小
private fun getCacheSize(): String {
val ecDir = requireContext().externalCacheDir
var cacheLength: Long = getFolderSize(requireContext().cacheDir)
if (ecDir != null) {
cacheLength += getFolderSize(ecDir)
}
return long2Size(cacheLength)
}
private fun getFolderSize(folder: File): Long {
var size: Long = 0
//忽略视频缓存
if (folder.name == "video-cache" || folder.name == "exo") return size
size += folder.length()
if (folder.isDirectory) {
val files = folder.listFiles()
if (files == null || files.isEmpty()) return size
for (file in files) {
size += if (file.isDirectory) {
getFolderSize(file)
} else {
file.length()
}
}
}
return size
}
private fun long2Size(length: Long): String {
val m = length / 1024f / 1024f
var str = m.toString()
val index = str.lastIndexOf(".")
if (index != -1 && str.length > index + 3) {
str = str.substring(0, index + 3)
}
return str + "M"
}
companion object {
const val AUTO_INSTALL_SP_KEY = "autoinstall"
const val CONCERN_GAME_SP_KEY = "concerngame"
@ -494,4 +460,51 @@ class SettingsFragment : NormalFragment() {
const val INSERT_MOBILE_CODE = 411
}
class SettingViewModel(application: Application): AndroidViewModel(application) {
val cacheSizeLiveData = MutableLiveData<String>()
// 获取缓存大小
fun getCacheSize() {
runOnIoThread {
val ecDir = getApplication<Application>().externalCacheDir
var cacheLength: Long = getFolderSize(getApplication<Application>().cacheDir)
if (ecDir != null) {
cacheLength += getFolderSize(ecDir)
}
cacheSizeLiveData.postValue(long2Size(cacheLength))
}
}
private fun long2Size(length: Long): String {
val m = length / 1024f / 1024f
var str = m.toString()
val index = str.lastIndexOf(".")
if (index != -1 && str.length > index + 3) {
str = str.substring(0, index + 3)
}
return str + "M"
}
private fun getFolderSize(folder: File): Long {
var size: Long = 0
//忽略视频缓存
if (folder.name == "video-cache" || folder.name == "exo") return size
size += folder.length()
if (folder.isDirectory) {
val files = folder.listFiles()
if (files == null || files.isEmpty()) return size
for (file in files) {
size += if (file.isDirectory) {
getFolderSize(file)
} else {
file.length()
}
}
}
return size
}
}
}