Skip to content

Commit

Permalink
充电功率信息现可以自定义亮屏时的刷新频率,并且息屏时不会刷新
Browse files Browse the repository at this point in the history
  • Loading branch information
hosizoraru committed Jun 15, 2023
1 parent 97e2f75 commit 58adaac
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import android.view.View
import cn.fkj233.ui.activity.MIUIActivity
import cn.fkj233.ui.activity.annotation.BMPage
import cn.fkj233.ui.activity.data.BasePage
import cn.fkj233.ui.activity.view.SeekBarWithTextV
import cn.fkj233.ui.activity.view.SwitchV
import cn.fkj233.ui.activity.view.TextSummaryV
import cn.fkj233.ui.activity.view.TextV
import star.sky.voyager.R

@BMPage("lock_screen", "Lock Screen", hideMenu = false)
Expand Down Expand Up @@ -78,6 +80,11 @@ class LockScreenPage : BasePage() {
textId = R.string.current_mA
), SwitchV("current_mA", false), dataBindingRecv = chargingInfo.binding.getRecv(1)
)
TextWithSeekBar(
TextV(textId = R.string.lockscreen_charging_info_refresh_frequency),
SeekBarWithTextV("lockscreen_charging_info_refresh_frequency", 1, 25, 10),
dataBindingRecv = chargingInfo.binding.getRecv(1)
)
TextSummaryWithSwitch(
TextSummaryV(
textId = R.string.double_tap_to_sleep,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,90 @@
package star.sky.voyager.hook.hooks.systemui

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Handler
import android.os.PowerManager
import android.widget.TextView
import com.github.kyuubiran.ezxhelper.ClassUtils
import com.github.kyuubiran.ezxhelper.ClassUtils.loadClass
import com.github.kyuubiran.ezxhelper.ClassUtils.loadClassOrNull
import com.github.kyuubiran.ezxhelper.EzXHelper
import com.github.kyuubiran.ezxhelper.HookFactory.`-Static`.createHook
import com.github.kyuubiran.ezxhelper.HookFactory.`-Static`.createHooks
import com.github.kyuubiran.ezxhelper.ObjectUtils
import com.github.kyuubiran.ezxhelper.finders.MethodFinder.`-Static`.methodFinder
import star.sky.voyager.R
import star.sky.voyager.utils.init.HookRegister
import star.sky.voyager.utils.key.XSPUtils.getBoolean
import star.sky.voyager.utils.key.XSPUtils.getInt
import star.sky.voyager.utils.key.hasEnable
import java.io.BufferedReader
import java.io.FileReader

object LockscreenChargingInfo : HookRegister() {
override fun init() = hasEnable("lockscreen_charging_info") {
val refreshFrequency =
getInt("lockscreen_charging_info_refresh_frequency", 10).toLong() * 100
val clazzDependency =
loadClass("com.android.systemui.Dependency")
val clazzKeyguardIndicationController =
loadClass("com.android.systemui.statusbar.KeyguardIndicationController")
loadClassOrNull("com.android.systemui.statusbar.phone.KeyguardIndicationTextView")?.constructors?.createHooks {
after {
(it.thisObject as TextView).isSingleLine = false
after { param ->
(param.thisObject as TextView).isSingleLine = false
val screenOnOffReceiver = object : BroadcastReceiver() {
val keyguardIndicationController = ClassUtils.invokeStaticMethodBestMatch(
clazzDependency, "get", null, clazzKeyguardIndicationController
)!!
val handler = Handler((param.thisObject as TextView).context.mainLooper)
val runnable = object : Runnable {
override fun run() {
ObjectUtils.invokeMethodBestMatch(
keyguardIndicationController,
"updatePowerIndication"
)
handler.postDelayed(this, refreshFrequency)
}
}

init {
if (((param.thisObject as TextView).context.getSystemService(Context.POWER_SERVICE) as PowerManager).isInteractive) {
handler.post(runnable)
}
}

override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
Intent.ACTION_SCREEN_ON -> {
handler.post(runnable)
}

Intent.ACTION_SCREEN_OFF -> {
handler.removeCallbacks(runnable)
}
}
}
}

val filter = IntentFilter().apply {
addAction(Intent.ACTION_SCREEN_ON)
addAction(Intent.ACTION_SCREEN_OFF)
}
(param.thisObject as TextView).context.registerReceiver(screenOnOffReceiver, filter)
}
}
loadClass("com.android.keyguard.charge.ChargeUtils").methodFinder()
.filterByName("getChargingHintText")
.filterByParamCount(3)
.filterByParamTypes(
Context::class.java,
Boolean::class.javaPrimitiveType,
Int::class.javaPrimitiveType
)
.first().createHook {
after {
if (it.result != null) {
it.result = it.result as String + "\n" + getChargingInfo()
}
after { param ->
param.result = param.result?.let { "$it\n${getChargingInfo()}" }
}
}
}
Expand All @@ -42,23 +98,21 @@ object LockscreenChargingInfo : HookRegister() {
current * voltage
}

current =
FileReader("/sys/class/power_supply/battery/current_now").use { fileReader ->
BufferedReader(fileReader).use { bufferedReader ->
-1.0 * bufferedReader.readLine().toDouble() / 1000000.0
}
current = FileReader("/sys/class/power_supply/battery/current_now").use { fileReader ->
BufferedReader(fileReader).use { bufferedReader ->
-1.0 * bufferedReader.readLine().toDouble() / 1000000.0
}
}
voltage = FileReader("/sys/class/power_supply/battery/voltage_now").use { fileReader ->
BufferedReader(fileReader).use { bufferedReader ->
bufferedReader.readLine().toDouble() / 1000000.0
}
}
temperature =
FileReader("/sys/class/power_supply/battery/temp").use { fileReader ->
BufferedReader(fileReader).use { bufferedReader ->
bufferedReader.readLine().toDouble() / 10.0
}
temperature = FileReader("/sys/class/power_supply/battery/temp").use { fileReader ->
BufferedReader(fileReader).use { bufferedReader ->
bufferedReader.readLine().toDouble() / 10.0
}
}

// val clazzMiuiChargeManager = loadClass("com.android.keyguard.charge.MiuiChargeManager")
// val plugState = loadClass("com.android.systemui.Dependency").classHelper()
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ja-rJP/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@


<string name="lockscreen_charging_info">充電パワーを表示</string>
<string name="lockscreen_charging_info_refresh_frequency">充電情報の更新頻度(単位:0.1秒)</string>
<string name="lockscreen_charging_info_not_supported">このデバイスはまだサポートされていません</string>

<string name="system_ui_show_status_bar_battery">ステータスバーにバッテリーについて表示</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@
<string name="blur_personal_assistant_radius">模糊程度</string>

<string name="lockscreen_charging_info">显示充电功率</string>
<string name="lockscreen_charging_info_refresh_frequency">充电功率的刷新频率 (单位:0.1/s)</string>
<string name="lockscreen_charging_info_not_supported">暂不支持此设备</string>

<string name="system_ui_show_status_bar_battery">状态栏显示关于电池</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@
<string name="blur_personal_assistant_radius">Blur Radius</string>

<string name="lockscreen_charging_info">Display charging power</string>
<string name="lockscreen_charging_info_refresh_frequency">Refresh frequency of power information (unit: 0.1/s)</string>
<string name="lockscreen_charging_info_not_supported">Device currently unsupported</string>

<string name="system_ui_show_status_bar_battery">StatusBar Show about Battery</string>
Expand Down

0 comments on commit 58adaac

Please sign in to comment.