Skip to content

Commit

Permalink
optimization: ChargingCVP adjusts the display effect
Browse files Browse the repository at this point in the history
Signed-off-by: mu7220 <[email protected]>
  • Loading branch information
lingqiqi5211 committed Mar 1, 2024
1 parent c0a0ee1 commit 2ac4cf2
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import com.sevtinge.hyperceiler.module.base.BaseHook
import com.sevtinge.hyperceiler.utils.api.IS_HYPER_OS
import com.sevtinge.hyperceiler.utils.devicesdk.getAndroidVersion
import com.sevtinge.hyperceiler.utils.devicesdk.isAndroidVersion
import de.robv.android.xposed.XC_MethodHook
import de.robv.android.xposed.XposedHelpers
import java.io.BufferedReader
import java.io.FileReader
Expand All @@ -55,6 +56,12 @@ object ChargingCVP : BaseHook() {
private val showSpacingValue by lazy {
mPrefsMap.getBoolean("system_ui_lock_screen_show_spacing_value")
}
private val isShowTemp by lazy {
mPrefsMap.getBoolean("system_ui_show_battery_temperature")
}
private val isShowMoreC by lazy {
mPrefsMap.getBoolean("system_ui_show_charging_c_more")
}

@SuppressLint("SetTextI18n")
override fun init() {
Expand All @@ -63,133 +70,26 @@ object ChargingCVP : BaseHook() {
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 { param ->
(param.thisObject as TextView).isSingleLine = false
(param.thisObject as TextView).let {
it.isSingleLine = false
it.textSize = 8.2f
}
if (showSpacingValue) {
val screenOnOffReceiver = object : BroadcastReceiver() {
val keyguardIndicationController = runCatching {
invokeStaticMethodBestMatch(
clazzDependency, "get", null, clazzKeyguardIndicationController
)!!
}.getOrElse {
val clazzMiuiStub = loadClass("miui.stub.MiuiStub")
val instanceMiuiStub =
getStaticObjectOrNull(clazzMiuiStub, "INSTANCE")!!
val mSysUIProvider =
getObjectOrNull(instanceMiuiStub, "mSysUIProvider")!!
val mKeyguardIndicationController =
getObjectOrNull(
mSysUIProvider,
"mKeyguardIndicationController"
)!!
invokeMethodBestMatch(mKeyguardIndicationController, "get")!!
}
val handler = Handler((param.thisObject as TextView).context.mainLooper)
val runnable = object : Runnable {
val clazzMiuiDependency =
loadClass("com.miui.systemui.MiuiDependency")
val clazzMiuiChargeController =
loadClass("com.miui.charge.MiuiChargeController")
val sDependency =
getStaticObjectOrNull(clazzMiuiDependency, "sDependency")!!
val mProviders =
getObjectOrNull(sDependency, "mProviders") as ArrayMap<*, *>
val mMiuiChargeControllerProvider =
mProviders[clazzMiuiChargeController]!!
val instanceMiuiChargeController = invokeMethodBestMatch(
mMiuiChargeControllerProvider, "createDependency"
)!!

override fun run() {
if (IS_HYPER_OS) {
doUpdateForHyperOS()
} else if (!IS_HYPER_OS && isAndroidVersion(34)) {
XposedHelpers.callStaticMethod(
loadClass("com.android.systemui.statusbar.KeyguardIndicationController"),
"updatePowerIndication"
)
} else {
invokeMethodBestMatch(
keyguardIndicationController,
"updatePowerIndication"
)
}
handler.postDelayed(
this,
mPrefsMap.getInt(
"system_ui_statusbar_lock_screen_show_spacing",
6
) / 2 * 1000L
)
}

fun doUpdateForHyperOS() {
val mBatteryStatus = getObjectOrNull(
instanceMiuiChargeController, "mBatteryStatus"
)!!
val level = getObjectOrNull(mBatteryStatus, "level")
val isPluggedIn =
invokeMethodBestMatch(mBatteryStatus, "isPluggedIn")
val mContext =
getObjectOrNull(instanceMiuiChargeController, "mContext")
val clazzChargeUtils = loadClass("com.miui.charge.ChargeUtils", lpparam.classLoader)
val chargingHintText =
invokeStaticMethodBestMatch(
clazzChargeUtils,
"getChargingHintText",
null,
level,
isPluggedIn,
mContext
)
setObject(
keyguardIndicationController,
"mComputePowerIndication",
chargingHintText
)
invokeMethodBestMatch(
keyguardIndicationController,
"updateDeviceEntryIndication",
null,
false
)
}
}

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
)
// 是否更改刷新频率
setShowSpacing(clazzDependency, clazzKeyguardIndicationController, param)
}
}
}
} else {
loadClassOrNull("com.android.systemui.statusbar.phone.KeyguardIndicationTextView")!!.constructors.createHooks {
after {
(it.thisObject as TextView).isSingleLine = false
after { param ->
(param.thisObject as TextView).let {
it.isSingleLine = false
it.textSize = 8.2f
}
}
}
}
Expand All @@ -200,10 +100,153 @@ object ChargingCVP : BaseHook() {
).methodFinder().filterByName("getChargingHintText").filterByParamCount(3).first()
.createHook {
after { param ->
param.result = param.result?.let { "$it${getCVP()}" }
param.result = param.result?.let {
"${getTemp()}$it\n${getCVP()}"
}
}
}

}

private fun setShowSpacing(
clazzDependency: Class<*>,
clazzKeyguardIndicationController: Class<*>,
param: XC_MethodHook.MethodHookParam
) {
val screenOnOffReceiver = object : BroadcastReceiver() {
val keyguardIndicationController = runCatching {
invokeStaticMethodBestMatch(
clazzDependency, "get", null, clazzKeyguardIndicationController
)!!
}.getOrElse {
val clazzMiuiStub = loadClass("miui.stub.MiuiStub")
val instanceMiuiStub =
getStaticObjectOrNull(clazzMiuiStub, "INSTANCE")!!
val mSysUIProvider =
getObjectOrNull(instanceMiuiStub, "mSysUIProvider")!!
val mKeyguardIndicationController =
getObjectOrNull(
mSysUIProvider,
"mKeyguardIndicationController"
)!!
invokeMethodBestMatch(mKeyguardIndicationController, "get")!!
}
val handler = Handler((param.thisObject as TextView).context.mainLooper)
val runnable = object : Runnable {
val clazzMiuiDependency =
loadClass("com.miui.systemui.MiuiDependency")
val clazzMiuiChargeController =
loadClass("com.miui.charge.MiuiChargeController")
val sDependency =
getStaticObjectOrNull(clazzMiuiDependency, "sDependency")!!
val mProviders =
getObjectOrNull(sDependency, "mProviders") as ArrayMap<*, *>
val mMiuiChargeControllerProvider =
mProviders[clazzMiuiChargeController]!!
val instanceMiuiChargeController = invokeMethodBestMatch(
mMiuiChargeControllerProvider, "createDependency"
)!!

override fun run() {
if (IS_HYPER_OS) {
doUpdateForHyperOS()
} else if (!IS_HYPER_OS && isAndroidVersion(34)) {
XposedHelpers.callStaticMethod(
loadClass("com.android.systemui.statusbar.KeyguardIndicationController"),
"updatePowerIndication"
)
} else {
invokeMethodBestMatch(
keyguardIndicationController,
"updatePowerIndication"
)
}
handler.postDelayed(
this,
mPrefsMap.getInt(
"system_ui_statusbar_lock_screen_show_spacing",
6
) / 2 * 1000L
)
}

fun doUpdateForHyperOS() {
val mBatteryStatus = getObjectOrNull(
instanceMiuiChargeController, "mBatteryStatus"
)!!
val level = getObjectOrNull(mBatteryStatus, "level")
val isPluggedIn =
invokeMethodBestMatch(mBatteryStatus, "isPluggedIn")
val mContext =
getObjectOrNull(instanceMiuiChargeController, "mContext")
val clazzChargeUtils =
loadClass("com.miui.charge.ChargeUtils", lpparam.classLoader)
val chargingHintText =
invokeStaticMethodBestMatch(
clazzChargeUtils,
"getChargingHintText",
null,
level,
isPluggedIn,
mContext
)
setObject(
keyguardIndicationController,
"mComputePowerIndication",
chargingHintText
)
invokeMethodBestMatch(
keyguardIndicationController,
"updateDeviceEntryIndication",
null,
false
)
}
}

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
)
}

private fun getTemp(): String {
var temp = 0.0

runCatching {
// 获取电池温度信息
val temNow =
BufferedReader(FileReader("/sys/class/power_supply/battery/temp"))
temp =
BigDecimal(temNow.readLine().toDouble() / 10.0).setScale(1, RoundingMode.HALF_UP).toDouble()
}

// 电池温度是否展示
val mTemp = if (isShowTemp) "$temp ℃ · " else ""

return mTemp
}

@SuppressLint("DefaultLocale")
Expand All @@ -215,40 +258,32 @@ object ChargingCVP : BaseHook() {
val current =
abs(batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW) / 1000)
var voltage = 0.0
var temp = 0.0

kotlin.runCatching {
runCatching {
// 获取电压信息
val voltageNow =
BufferedReader(FileReader("/sys/class/power_supply/battery/voltage_now"))
voltage =
BigDecimal(voltageNow.readLine().toDouble() / 1000.0).setScale(1, RoundingMode.HALF_UP).toDouble()
// 获取电池温度信息
val temNow =
BufferedReader(FileReader("/sys/class/power_supply/battery/temp"))
temp =
BigDecimal(temNow.readLine().toDouble() / 10.0).setScale(1, RoundingMode.HALF_UP).toDouble()
}

// 计算功率信息
val powerAll = abs((current * voltage) / 1000f / 1000f)
val power = String.format("%.2f", powerAll)

// 电流展示逻辑设置
val mCurrent = if (mPrefsMap.getBoolean("system_ui_show_charging_c_more")) {
val mCurrent = if (isShowMoreC) {
"$current mA"
} else {
"${String.format("%.1f", abs(current / 1000f))} A"
}
val mVoltage = "${String.format("%.1f", abs(voltage / 1000f))} V"
// 电池温度是否展示
val mTemp = if (mPrefsMap.getBoolean("system_ui_show_battery_temperature")) " $temp" else ""

// 判断充满信息是否归零
val showBattery = if (current == 0) {
mTemp
""
} else {
"$mTemp\n$mCurrent · $mVoltage · $power W"
"$mCurrent · $mVoltage · $power W"
}

// 输出展示信息
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-in-rID/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@
<string name="system_ui_status_bar_icon_big_hd">Ikon HD besar</string>
<string name="system_ui_status_bar_icon_mobile_network_signal_no_card">Tidak ada ikon kartu SIM</string>
<string name="system_ui_status_bar_icon_mobile_network_hide_card_1">Sembunyikan ikon sinyal kartu SIM 1</string>
<string name="system_ui_status_bar_icon_mobile_network_hide_card_2">Sembunyikan ikon sinyal kartu SIM 1</string>
<string name="system_ui_status_bar_icon_mobile_network_hide_card_2">Sembunyikan ikon sinyal kartu SIM 2</string>
<string name="system_ui_status_bar_icon_mobile_network_type">Logika tampilan</string>
<string name="system_ui_status_bar_big_mobile_network_type">Ikon Tipe Jaringan Besar</string>
<string name="system_ui_status_bar_icon_location">Lokasi</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@
<string name="system_ui_status_bar_icon_big_hd">Big HD icon</string>
<string name="system_ui_status_bar_icon_mobile_network_signal_no_card">No SIM card icon</string>
<string name="system_ui_status_bar_icon_mobile_network_hide_card_1">Hide SIM card 1 signal icon</string>
<string name="system_ui_status_bar_icon_mobile_network_hide_card_2">Hide SIM card 1 signal icon</string>
<string name="system_ui_status_bar_icon_mobile_network_hide_card_2">Hide SIM card 2 signal icon</string>
<string name="system_ui_status_bar_icon_mobile_network_type">Display logic</string>
<string name="system_ui_status_bar_big_mobile_network_type">Big Network Type Icon</string>
<string name="system_ui_status_bar_icon_location">Location</string>
Expand Down

0 comments on commit 2ac4cf2

Please sign in to comment.