优化锁屏电流显示

This commit is contained in:
乌堆小透明
2022-03-14 00:43:00 +08:00
parent b71fa8a15e
commit cd0aaef385
2 changed files with 13 additions and 57 deletions

View File

@@ -1,59 +1,30 @@
package com.lt2333.simplicitytools.hook.app.systemui
import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
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 {
class LockScreenCurrent : 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 = getBatteryTemperature(viewGroup.context).toString()
val textView = TextView(viewGroup.context).also {
it.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
it.setTextSize(android.util.TypedValue.COMPLEX_UNIT_SP, 14f)
it.gravity = Gravity.CENTER or Gravity.BOTTOM
it.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)
"com.android.keyguard.charge.ChargeUtils".findClass(lpparam.classLoader)
.hookAfterMethod(
"getChargingHintText",
Context::class.java,
Boolean::class.java,
Int::class.java
) {
it.result = getCurrent() + "\n" + it.result
}
}
}
@@ -72,19 +43,12 @@ class LockScreenCurrent(private var battery: String = "") : IXposedHookLoadPacka
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"
val current = (getMeanCurrentVal(filePath, 5, 0) / 1000.0f).roundToInt()
result = "${InitFields.moduleRes.getString(R.string.current_current)} ${current}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.temp
)
}$battery".replace("-", "").replace("${InitFields.moduleRes.getString(R.string.current_current)}0 mA", "")
} else {
"${InitFields.moduleRes.getString(R.string.current_current)}-$current mA"
}
result = "${InitFields.moduleRes.getString(R.string.current_current)} ${current}mA"
}
} catch (e: Exception) {
e.printStackTrace()
@@ -130,11 +94,4 @@ class LockScreenCurrent(private var battery: String = "") : IXposedHookLoadPacka
}
return defaultValue
}
private fun getBatteryTemperature(context: Context): Int {
return context.registerReceiver(
null as BroadcastReceiver?,
IntentFilter("android.intent.action.BATTERY_CHANGED")
)!!.getIntExtra("temperature", 0) / 10
}
}

View File

@@ -157,6 +157,5 @@
<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="temp">temp.</string>
<string name="current_current">Current</string>
</resources>