From 22d7eeb2dfcca4bd7ccad3200dd19f7424437b8b Mon Sep 17 00:00:00 2001 From: HuanCheng65 <22636177+HuanCheng65@users.noreply.github.com> Date: Wed, 15 Feb 2023 22:20:38 +0800 Subject: [PATCH] chore: Dev 13 --- .../java/com/huanchengfly/tieba/post/App.kt | 29 ++++++- .../post/fragments/PreferencesFragment.kt | 2 +- .../ui/page/settings/more/MoreSettingsPage.kt | 6 +- .../tieba/post/utils/AppIconUtil.kt | 6 +- .../tieba/post/utils/DeviceUtils.kt | 85 +++++++++++++++++++ application.properties | 4 +- 6 files changed, 123 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/com/huanchengfly/tieba/post/utils/DeviceUtils.kt diff --git a/app/src/main/java/com/huanchengfly/tieba/post/App.kt b/app/src/main/java/com/huanchengfly/tieba/post/App.kt index cb20a44b..2e8cb578 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/App.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/App.kt @@ -5,6 +5,7 @@ import android.app.Activity import android.app.ActivityManager import android.app.Application import android.app.Dialog +import android.content.ComponentName import android.content.Context import android.content.res.Configuration import android.content.res.Resources @@ -36,6 +37,8 @@ import com.huanchengfly.tieba.post.ui.common.theme.interfaces.ThemeSwitcher import com.huanchengfly.tieba.post.ui.common.theme.utils.ThemeUtils import com.huanchengfly.tieba.post.utils.AccountUtil import com.huanchengfly.tieba.post.utils.AppIconUtil +import com.huanchengfly.tieba.post.utils.AppIconUtil.disableComponent +import com.huanchengfly.tieba.post.utils.AppIconUtil.enableComponent import com.huanchengfly.tieba.post.utils.ClientUtils import com.huanchengfly.tieba.post.utils.EmoticonManager import com.huanchengfly.tieba.post.utils.Icons @@ -94,7 +97,29 @@ class App : Application(), IApp, IGetter, SketchFactory { return null } - fun refreshIcon(enableNewUi: Boolean = applicationMetaData.getBoolean("enable_new_ui") || appPreferences.enableNewUi) { + private fun setOldMainActivityEnabled(enabled: Boolean) { + if (enabled) { + packageManager.enableComponent( + ComponentName( + this, + "com.huanchengfly.tieba.post.activities.MainActivity" + ) + ) + } else { + packageManager.disableComponent( + ComponentName( + this, + "com.huanchengfly.tieba.post.activities.MainActivity" + ) + ) + } + } + + fun setIcon( + enableNewUi: Boolean = applicationMetaData.getBoolean("enable_new_ui") || appPreferences.enableNewUi, + keepOld: Boolean = BuildConfig.DEBUG + ) { + setOldMainActivityEnabled(!enableNewUi && !keepOld) if (enableNewUi) AppIconUtil.setIcon() else AppIconUtil.setIcon(Icons.DISABLE) } @@ -117,7 +142,7 @@ class App : Application(), IApp, IGetter, SketchFactory { Analytics::class.java, Crashes::class.java, Distribute::class.java ) } - refreshIcon() + setIcon(keepOld = !BuildConfig.DEBUG && !isSelfBuild) AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) ThemeUtils.init(ThemeDelegate) registerActivityLifecycleCallbacks(ClipBoardLinkDetector) diff --git a/app/src/main/java/com/huanchengfly/tieba/post/fragments/PreferencesFragment.kt b/app/src/main/java/com/huanchengfly/tieba/post/fragments/PreferencesFragment.kt index c58dfd6d..44028237 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/fragments/PreferencesFragment.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/fragments/PreferencesFragment.kt @@ -259,7 +259,7 @@ class PreferencesFragment : PreferencesFragment() { getString(R.string.tip_about, BuildConfig.VERSION_NAME) refresh() findPreference("enableNewUi")?.setOnPreferenceChangeListener { _, newValue -> - App.INSTANCE.refreshIcon(newValue == true) + App.INSTANCE.setIcon(newValue == true) true } /* diff --git a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/settings/more/MoreSettingsPage.kt b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/settings/more/MoreSettingsPage.kt index 9a7b8fb8..b8ee1410 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/ui/page/settings/more/MoreSettingsPage.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/ui/page/settings/more/MoreSettingsPage.kt @@ -16,6 +16,7 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.vectorResource import androidx.compose.ui.unit.dp +import com.huanchengfly.tieba.post.App import com.huanchengfly.tieba.post.BuildConfig import com.huanchengfly.tieba.post.R import com.huanchengfly.tieba.post.dataStore @@ -93,7 +94,10 @@ fun MoreSettingsPage( key = "enableNewUi", title = stringResource(id = R.string.title_enable_new_ui), defaultChecked = false, - summary = stringResource(id = R.string.summary_enable_new_ui) + summary = stringResource(id = R.string.summary_enable_new_ui), + onCheckedChange = { + App.INSTANCE.setIcon(it) + } ) } prefsItem { diff --git a/app/src/main/java/com/huanchengfly/tieba/post/utils/AppIconUtil.kt b/app/src/main/java/com/huanchengfly/tieba/post/utils/AppIconUtil.kt index 0f13ac4c..8aea70aa 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/utils/AppIconUtil.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/utils/AppIconUtil.kt @@ -27,7 +27,7 @@ object AppIconUtil { get() = context.appPreferences fun setIcon(icon: String = appPreferences.appIcon ?: Icons.NEW_ICON) { - val newIcon = if (Icons.ICONS.contains(icon)) { + val newIcon = if (Icons.ICONS.contains(icon) || icon == Icons.DISABLE) { icon } else Icons.DEFAULT_ICON Icons.ICONS.forEach { @@ -44,7 +44,7 @@ object AppIconUtil { * * @param componentName 组件名 */ - private fun PackageManager.enableComponent(componentName: ComponentName) { + fun PackageManager.enableComponent(componentName: ComponentName) { setComponentEnabledSetting( componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, @@ -57,7 +57,7 @@ object AppIconUtil { * * @param componentName 组件名 */ - private fun PackageManager.disableComponent(componentName: ComponentName) { + fun PackageManager.disableComponent(componentName: ComponentName) { setComponentEnabledSetting( componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, diff --git a/app/src/main/java/com/huanchengfly/tieba/post/utils/DeviceUtils.kt b/app/src/main/java/com/huanchengfly/tieba/post/utils/DeviceUtils.kt new file mode 100644 index 00000000..4fb935cb --- /dev/null +++ b/app/src/main/java/com/huanchengfly/tieba/post/utils/DeviceUtils.kt @@ -0,0 +1,85 @@ +package com.huanchengfly.tieba.post.utils + +import android.os.Environment +import android.os.StatFs +import java.io.File +import java.io.IOException +import java.util.Locale +import java.util.regex.Pattern + +object DeviceUtils { + var coreNum = -1 + private const val CPU_MAX_INFO_FORMAT = "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq" + private const val MEM_INFO_FILE = "/proc/meminfo" + + fun getDeviceScore(): Float { + val cpuCores = getDeviceCpuCore().takeIf { it > 0 } ?: 6.9822063f + val cpuAverageFrequency = getDeviceCpuAverageFrequency().takeIf { it > 0 } ?: 1.7859616f + val totalMemory = getTotalMemory().takeIf { it > 0 } ?: 3.5425532f + val totalSDCardSize = getTotalSDCardSize().takeIf { it >= 0 } ?: 51.957294f +// val deviceScore = round(totalMemory)*0.0572301f + roundUpRom + return 0f + } + + fun getTotalSDCardSize(): Float { + return runCatching { + if (Environment.getExternalStorageState().equals("mounted")) { + val path = Environment.getExternalStorageDirectory().path + val stat = StatFs(path) + val blockSize = stat.blockSizeLong + val totalBlocks = stat.blockCountLong + val totalSize = totalBlocks * blockSize + totalSize / 1024f / 1024 / 1024 + } else -1f + }.getOrDefault(-1f) + } + + fun getTotalMemory(): Float { + val memory = runCatching { + File(MEM_INFO_FILE).bufferedReader(bufferSize = 8192).use { reader -> + reader.readLine().split("\\s+".toRegex()).takeIf { it.size >= 2 }?.get(1) + ?.toLongOrNull() ?: 0 + } + }.getOrDefault(0L) + return if (memory > 0) memory.toFloat() / 1024 / 1024 + else -1f + } + + // 获取手机 CPU 平均核心频率 + fun getDeviceCpuAverageFrequency(): Float { + var totalFrequency = 0f + val coreNum = getDeviceCpuCore() + for (i in 0 until coreNum) { + totalFrequency += getDeviceCpuFrequency(i) + } + return totalFrequency / coreNum + } + + // 获取手机 CPU 某个核心的频率 + fun getDeviceCpuFrequency(core: Int): Float { + return getContentFromFileInfo( + CPU_MAX_INFO_FORMAT.format( + Locale.ENGLISH, + core + ) + ).toFloatOrNull() ?: -1f + } + + // 获取手机 CPU 核心数 + fun getDeviceCpuCore(): Int { + return coreNum.takeIf { it > 0 } ?: runCatching { + File("/sys/devices/system/cpu").listFiles { file -> + Pattern.matches("cpu[0-9]", file.name) + }?.size ?: -1 + }.getOrDefault(-1).also { coreNum = it } + } + + private fun getContentFromFileInfo(filePath: String): String { + return try { + File(filePath).bufferedReader().use { it.readLine() } + } catch (e: IOException) { + e.printStackTrace() + "" + } + } +} \ No newline at end of file diff --git a/application.properties b/application.properties index 393d5eb0..6dec95a0 100644 --- a/application.properties +++ b/application.properties @@ -1,5 +1,5 @@ -versionCode=39012 +versionCode=39013 versionName=4.0.0 isPerRelease=true preReleaseName=dev -preReleaseVer=12 \ No newline at end of file +preReleaseVer=13 \ No newline at end of file