Feature: 锁屏电流信息展示 (#65)

This commit is contained in:
YuKongA
2022-03-13 20:50:07 +08:00
committed by GitHub
parent 780db8dfa5
commit d2ff9d61e7
9 changed files with 173 additions and 1 deletions

View File

@@ -942,6 +942,14 @@ class SettingsActivity : MIUIActivity() {
SwitchV("enable_wave_charge_animation")
)
)
add(
TextSummaryWithSwitchV(
TextSummaryV(
textId = R.string.lock_screen_current,
),
SwitchV("lock_screen_current")
)
)
}
}

View File

@@ -4,11 +4,17 @@ import com.github.kyuubiran.ezxhelper.init.EzXHelperInit
import com.lt2333.simplicitytools.BuildConfig
import com.lt2333.simplicitytools.hook.app.*
import de.robv.android.xposed.IXposedHookLoadPackage
import de.robv.android.xposed.IXposedHookZygoteInit
import de.robv.android.xposed.XSharedPreferences
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam
class MainHook : IXposedHookLoadPackage {
class MainHook : IXposedHookLoadPackage,IXposedHookZygoteInit {
private var prefs = XSharedPreferences(BuildConfig.APPLICATION_ID, "config")
override fun initZygote(startupParam: IXposedHookZygoteInit.StartupParam) {
EzXHelperInit.initZygote(startupParam)
}
override fun handleLoadPackage(lpparam: LoadPackageParam) {
if (prefs.getBoolean("main_switch", true)) {
EzXHelperInit.initHandleLoadPackage(lpparam)

View File

@@ -70,6 +70,8 @@ class SystemUI : IXposedHookLoadPackage {
HideNetworkSpeedSplitter().handleLoadPackage(lpparam)
//Alpha充电动画
WaveCharge().handleLoadPackage(lpparam)
//锁屏电流
LockScreenCurrent().handleLoadPackage(lpparam)
}
}

View File

@@ -0,0 +1,140 @@
package com.lt2333.simplicitytools.hook.app.systemui
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Typeface
import android.view.Gravity
import android.view.ViewGroup
import android.widget.TextView
import cn.fkj233.ui.activity.dp2px
import com.github.kyuubiran.ezxhelper.init.InitFields
import com.lt2333.simplicitytools.R
import com.lt2333.simplicitytools.util.findClass
import com.lt2333.simplicitytools.util.hasEnable
import com.lt2333.simplicitytools.util.hookAfterAllMethods
import com.lt2333.simplicitytools.util.hookAfterMethod
import com.microsoft.appcenter.utils.HandlerUtils.runOnUiThread
import de.robv.android.xposed.IXposedHookLoadPackage
import de.robv.android.xposed.callbacks.XC_LoadPackage
import java.io.BufferedReader
import java.io.FileReader
import java.lang.reflect.Method
import java.util.*
import kotlin.math.roundToInt
class LockScreenCurrent(private var battery: String = "") : IXposedHookLoadPackage {
override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam) {
hasEnable("lock_screen_current") {
val miuiPhoneStatusBarViewClass =
"com.android.systemui.statusbar.phone.KeyguardBottomAreaView".findClass(lpparam.classLoader)
miuiPhoneStatusBarViewClass.hookAfterMethod("onFinishInflate") { param ->
val viewGroup = param.thisObject as ViewGroup
battery = getSystemBattery(viewGroup.context)
val textView = TextView(viewGroup.context).also {
it.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
it.textSize = dp2px(viewGroup.context, 5f).toFloat()
}
textView.gravity = Gravity.CENTER or Gravity.BOTTOM
textView.setPadding(0, 0, 0, dp2px(viewGroup.context, 5f))
Timer().schedule(object : TimerTask() {
override fun run() {
runOnUiThread {
textView.text = getCurrent()
}
}
}, 0, 1000)
val darkIconDispatcherClass =
"com.android.systemui.plugins.DarkIconDispatcher".findClass(lpparam.classLoader)
darkIconDispatcherClass.hookAfterAllMethods("getTint") {
val areaTint = it.args[2] as Int
textView.setTextColor(areaTint)
}
viewGroup.addView(textView)
}
}
}
/**
* 原始代码来自 CSDN
* https://blog.csdn.net/zhangyongfeiyong/article/details/53641809
*/
@SuppressLint("PrivateApi")
private fun getCurrent(): String {
var result = ""
try {
val systemProperties = Class.forName("android.os.SystemProperties")
val get = systemProperties.getDeclaredMethod("get", String::class.java) as Method
val platName = get.invoke(null, "ro.hardware") as String
if (platName.startsWith("mt") || platName.startsWith("MT")) {
val filePath =
"/sys/class/power_supply/battery/device/FG_Battery_CurrentConsumption"
result = "${(getMeanCurrentVal(filePath, 5, 0) / 1000.0f).roundToInt()} mA"
} else if (platName.startsWith("qcom")) {
val filePath = "/sys/class/power_supply/battery/current_now"
val current = (getMeanCurrentVal(filePath, 5, 0) / 1000.0f).roundToInt()
result = if (current < 0) {
"${InitFields.moduleRes.getString(R.string.current_current)}$current mA${InitFields.moduleRes.getString(R.string.charging)}$battery%".replace("-", "")
} else {
"${InitFields.moduleRes.getString(R.string.current_current)}-$current mA"
}
}
} catch (e: Exception) {
e.printStackTrace()
}
return result
}
/**
* 获取平均电流值
* 获取 filePath 文件 totalCount 次数的平均值,每次采样间隔 intervalMs 时间
*/
private fun getMeanCurrentVal(filePath: String, totalCount: Int, intervalMs: Int): Float {
var meanVal = 0.0f
if (totalCount <= 0) {
return 0.0f
}
for (i in 0 until totalCount) {
try {
val f: Float = readFile(filePath, 0).toFloat()
meanVal += f / totalCount
} catch (e: Exception) {
e.printStackTrace()
}
if (intervalMs <= 0) {
continue
}
try {
Thread.sleep(intervalMs.toLong())
} catch (e: Exception) {
e.printStackTrace()
}
}
return meanVal
}
private fun readFile(path: String, defaultValue: Int): Int {
try {
val bufferedReader = BufferedReader(FileReader(path))
val i: Int = bufferedReader.readLine().toInt(10)
bufferedReader.close()
return i
} catch (localException: java.lang.Exception) {
}
return defaultValue
}
private fun getSystemBattery(context: Context): String {
val batteryInfoIntent: Intent? = context.applicationContext.registerReceiver(
null,
IntentFilter(Intent.ACTION_BATTERY_CHANGED)
)
val level: Int = batteryInfoIntent!!.getIntExtra("level", 0)
val batterySum = batteryInfoIntent.getIntExtra("scale", 100)
val percentBattery = 100 * level / batterySum
return percentBattery.toString()
}
}

View File

@@ -152,4 +152,7 @@
<string name="current">当前值:</string>
<string name="participate_in_translation">参与翻译</string>
<string name="participate_in_translation_summary">帮助我们将应用翻译成您的语言</string>
<string name="lock_screen_current">锁屏显示电流信息</string>
<string name="current_current">当前电流:</string>
<string name="charging">充电中:</string>
</resources>

View File

@@ -152,4 +152,7 @@
<string name="current">當前值:</string>
<string name="participate_in_translation">參與翻譯</string>
<string name="participate_in_translation_summary">幫助我們將這個APP翻譯成您的語言</string>
<string name="lock_screen_current">锁屏显示电流信息</string>
<string name="current_current">當前電流:</string>
<string name="charging">充電中:</string>
</resources>

View File

@@ -152,4 +152,7 @@
<string name="current">當前值:</string>
<string name="participate_in_translation">參與評估</string>
<string name="participate_in_translation_summary">幫助我們將這個APP翻譯成您的語言</string>
<string name="lock_screen_current">锁屏显示电流信息</string>
<string name="current_current">當前電流:</string>
<string name="charging">充電中:</string>
</resources>

View File

@@ -156,4 +156,7 @@
<string name="current">Current:</string>
<string name="participate_in_translation">Participate in translation</string>
<string name="participate_in_translation_summary">Help us translate the app into your language</string>
<string name="lock_screen_current">Lock screen displays current information</string>
<string name="current_current">current:</string>
<string name="charging">charging:</string>
</resources>