Skip to content

Commit

Permalink
opt:StatuBar clock hook
Browse files Browse the repository at this point in the history
  • Loading branch information
lingqiqi5211 committed Mar 26, 2024
1 parent f147e7c commit 3d780c5
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import android.util.*
import android.view.*
import android.widget.*
import com.github.kyuubiran.ezxhelper.ClassUtils.loadClass
import com.github.kyuubiran.ezxhelper.HookFactory.`-Static`.createHook
import com.github.kyuubiran.ezxhelper.HookFactory.`-Static`.createAfterHook
import com.github.kyuubiran.ezxhelper.HookFactory.`-Static`.createBeforeHook
import com.github.kyuubiran.ezxhelper.finders.ConstructorFinder.`-Static`.constructorFinder
import com.github.kyuubiran.ezxhelper.finders.MethodFinder.`-Static`.methodFinder
import com.sevtinge.hyperceiler.module.base.*
Expand All @@ -50,13 +51,13 @@ object StatusBarClockNew : BaseHook() {
mPrefsMap.getBoolean("system_ui_disable_clock_synch")
}
private val clockSizeS by lazy {
mPrefsMap.getInt("system_ui_statusbar_clock_size_1", 5)
mPrefsMap.getInt("system_ui_statusbar_clock_size_1", 12)
}
private val clockSizeB by lazy {
mPrefsMap.getInt("system_ui_statusbar_clock_size_2", 20)
mPrefsMap.getInt("system_ui_statusbar_clock_size_2", 54)
}
private val clockSizeN by lazy {
mPrefsMap.getInt("system_ui_statusbar_clock_size_3", 9)
mPrefsMap.getInt("system_ui_statusbar_clock_size_3", 12)
}
private val clockTextSpacing by lazy {
mPrefsMap.getInt("system_ui_statusbar_clock_double_spacing_margin_1", 16)
Expand Down Expand Up @@ -105,82 +106,79 @@ object StatusBarClockNew : BaseHook() {
override fun init() {
statusBarClass.constructorFinder()
.filterByParamCount(3)
.first().createHook {
after {
try {
val miuiClock = it.thisObject as TextView
val miuiClockName = miuiClock.resources.getResourceEntryName(miuiClock.id) ?: return@after

setMiuiClockStyle(miuiClockName, miuiClock)

val isSec =
miuiClockName in setOf("clock", "big_time", "horizontal_time", "date_time")
// miuiClockName 内部标签分类如下
// clock 竖屏状态栏时钟
// big_time 通知中心时钟
// horizontal_time 横屏通知中心时钟
// date_time 通知中心日期时钟

if (getClockStyle != 0 && miuiClockName == "clock")
miuiClock.isSingleLine = false

if (isSec) {
val d: Method = miuiClock.javaClass.getDeclaredMethod("updateTime")
val r = Runnable {
d.isAccessible = true
d.invoke(miuiClock)
}
.first().createAfterHook {
try {
val miuiClock = it.thisObject as TextView
val miuiClockName = miuiClock.resources.getResourceEntryName(miuiClock.id)
?: return@createAfterHook

setMiuiClockStyle(miuiClockName, miuiClock)

val isSec =
miuiClockName in setOf("clock", "big_time", "horizontal_time", "date_time")
// miuiClockName 内部标签分类如下
// clock 竖屏状态栏时钟
// big_time 通知中心时钟
// horizontal_time 横屏通知中心时钟
// date_time 通知中心日期时钟

if (getClockStyle != 0 && miuiClockName == "clock")
miuiClock.isSingleLine = false

if (isSec) {
val d: Method = miuiClock.javaClass.getDeclaredMethod("updateTime")
val r = Runnable {
d.isAccessible = true
d.invoke(miuiClock)
}

class T : TimerTask() {
override fun run() {
Handler(miuiClock.context.mainLooper).post(r)
}
class T : TimerTask() {
override fun run() {
Handler(miuiClock.context.mainLooper).post(r)
}
Timer().schedule(
T(), 1000 - System.currentTimeMillis() % 1000, 1000
)
}
} catch (_: Exception) {
Timer().schedule(
T(), 1000 - System.currentTimeMillis() % 1000, 1000
)
}
} catch (_: Exception) {
}
}

// 设置格式
statusBarClass.methodFinder()
.filterByName("updateTime")
.single().createHook {
before {
try {
val textV = it.thisObject as TextView
val context = textV.context
val miuiClockName = textV.resources.getResourceEntryName(textV.id) ?: return@before
if (miuiClockName in setOf("clock", "big_time", "horizontal_time", "date_time")) {
setMiuiClockStyle(miuiClockName, textV)

if ((isSync && miuiClockName == "big_time") || (getFormatN.isEmpty() && miuiClockName in setOf("date_time", "horizontal_time"))) return@before
setMiuiClockFormat(context, miuiClockName, textV)
it.result = null
}
} catch (_: Exception) {
.single().createBeforeHook {
try {
val textV = it.thisObject as TextView
val context = textV.context
val miuiClockName =
textV.resources.getResourceEntryName(textV.id) ?: return@createBeforeHook
if (miuiClockName in setOf("clock", "big_time", "horizontal_time", "date_time")) {
setMiuiClockStyle(miuiClockName, textV)

if ((isSync && miuiClockName == "big_time") || (getFormatN.isEmpty() && miuiClockName in setOf("date_time", "horizontal_time"))) return@createBeforeHook
setMiuiClockFormat(context, miuiClockName, textV)
it.result = null
}
} catch (_: Exception) {
}
}

mNewClockClass.methodFinder()
.filterByName("updateTime")
.single().createHook {
before {
try {
val textV = it.thisObject as TextView
val context = textV.context
val miuiClockName = textV.resources.getResourceEntryName(textV.id) ?: return@before
setMiuiClockStyle(miuiClockName, textV)

if ((isSync && miuiClockName == "big_time") || (getFormatN.isEmpty() && miuiClockName in setOf("date_time", "horizontal_time"))) return@before
setMiuiClockFormat(context, miuiClockName, textV)
it.result = null
} catch (_: Exception) {
}
.single().createBeforeHook {
try {
val textV = it.thisObject as TextView
val context = textV.context
val miuiClockName =
textV.resources.getResourceEntryName(textV.id) ?: return@createBeforeHook
setMiuiClockStyle(miuiClockName, textV)

if ((isSync && miuiClockName == "big_time") || (getFormatN.isEmpty() && miuiClockName in setOf("date_time", "horizontal_time"))) return@createBeforeHook
setMiuiClockFormat(context, miuiClockName, textV)
it.result = null
} catch (_: Exception) {
}
}
}
Expand Down Expand Up @@ -223,15 +221,15 @@ object StatusBarClockNew : BaseHook() {

private fun setStatusBarClock(name: String, text: TextView) {
when {
clockSizeS != 5 && name == "clock" -> {
clockSizeS != 12 && name == "clock" -> {
text.setTextSize(TypedValue.COMPLEX_UNIT_DIP, clockSizeS.toFloat())
}

clockSizeB != 20 && name == "big_time" -> {
clockSizeB != 54 && name == "big_time" -> {
text.setTextSize(TypedValue.COMPLEX_UNIT_DIP, clockSizeB.toFloat())
}

clockSizeN != 9 && name in setOf("date_time", "horizontal_time") -> {
clockSizeN != 12 && name in setOf("date_time", "horizontal_time") -> {
text.setTextSize(TypedValue.COMPLEX_UNIT_DIP, clockSizeN.toFloat())
}
}
Expand All @@ -253,6 +251,9 @@ object StatusBarClockNew : BaseHook() {
}

private fun setMiuiClockFormat(context: Context?, name: String, textV: TextView) {
val textSb: StringBuilder
val formatSb: StringBuilder

// 因为输入对话框限制,所以里面部分内容会比较抽象
val sClockName = if (getFormatN.isEmpty()) {
when (getClockStyle) {
Expand Down Expand Up @@ -280,33 +281,27 @@ object StatusBarClockNew : BaseHook() {

when (name) {
"clock" -> {
val textSb = StringBuilder()
val formatSb = StringBuilder(sClockName)
mCalendar?.callMethod("format", context, textSb, formatSb)
textV.text = textSb.toString()
textSb = StringBuilder()
formatSb = StringBuilder(sClockName)
}

"big_time" -> {
val textSb = StringBuilder()
val formatSb = StringBuilder(getFormatS.split("\n")[0])
mCalendar?.callMethod("format", context, textSb, formatSb)
textV.text = textSb.toString()
textSb = StringBuilder()
formatSb = StringBuilder(getFormatS.split("\n")[0])
}

"horizontal_time" -> {
val textSb = StringBuilder()
val formatSb =
textSb = StringBuilder()
formatSb =
StringBuilder("${getFormatN.split("\n")[0]} ${getFormatS.split("\n")[0]}")
mCalendar?.callMethod("format", context, textSb, formatSb)
textV.text = textSb.toString()
}

else -> {
val textSb = StringBuilder()
val formatSb = StringBuilder(getFormatN.split("\n")[0])
mCalendar?.callMethod("format", context, textSb, formatSb)
textV.text = textSb.toString()
textSb = StringBuilder()
formatSb = StringBuilder(getFormatN.split("\n")[0])
}
}
mCalendar?.callMethod("format", context, textSb, formatSb)
textV.text = textSb.toString()
}
}
6 changes: 3 additions & 3 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,9 @@
<string name="system_ui_statusbar_network_speed_style_3">上下行网速单行显示</string>
<string name="system_ui_statusbar_network_speed_style_4">上下行网速双排显示</string>

<string name="system_ui_statusbar_clock_style_1">单行显示 (仅状态栏时钟)</string>
<string name="system_ui_statusbar_clock_style_2">双行显示 (状态栏时钟在上, 通知中心迷你时钟在下)</string>
<string name="system_ui_statusbar_clock_style_3">双行显示 (通知中心迷你时钟在上, 状态栏时钟在下)</string>
<string name="system_ui_statusbar_clock_style_1">单行显示\n\n仅状态栏时钟</string>
<string name="system_ui_statusbar_clock_style_2">双行显示\n\n状态栏时钟在上\n通知中心迷你时钟在下</string>
<string name="system_ui_statusbar_clock_style_3">双行显示\n\n通知中心迷你时钟在上\n状态栏时钟在下</string>

<string name="mediaeditor_mickey">米老鼠</string>
<string name="mediaeditor_strawberry_bear">草莓熊</string>
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,9 @@
<string name="system_ui_statusbar_network_speed_style_2">Values and units are displayed in two rows</string>
<string name="system_ui_statusbar_network_speed_style_3">The uplink and downlink network speed is displayed in a single line</string>
<string name="system_ui_statusbar_network_speed_style_4">Dual-row display of uplink and downlink network speeds</string>
<string name="system_ui_statusbar_clock_style_1">Single line status bar clock</string>
<string name="system_ui_statusbar_clock_style_2">Status bar clock\nNotification bar mini clock</string>
<string name="system_ui_statusbar_clock_style_3">Notification bar mini clock\nStatus bar clock</string>
<string name="system_ui_statusbar_clock_style_1">Single line display\n\nStatus bar clock only</string>
<string name="system_ui_statusbar_clock_style_2">Two-line display\n\nStatus bar clock on top\nNotification bar mini clock on bottom</string>
<string name="system_ui_statusbar_clock_style_3">Two-line display\n\nNotification bar mini clock on top\nStatus bar clock on bottom</string>
<string name="mediaeditor_mickey">Mickey Mouse</string>
<string name="mediaeditor_strawberry_bear">Strawberry Bear</string>
<!--System Framework-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
app:entryValues="@array/system_ui_status_bar_clock_double_mode_value" />

<SeekBarPreferenceEx
android:defaultValue="5"
android:defaultValue="12"
android:key="prefs_key_system_ui_statusbar_clock_size_1"
android:title="@string/system_ui_statusbar_clock_size"
app:defaultValueText="@string/array_default"
app:format="%d dp"
app:maxValue="20"
app:minValue="5"
app:minValue="7"
app:showSeekBarValue="true"
app:stepValue="1" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory>
<SeekBarPreferenceEx
android:defaultValue="20"
android:defaultValue="54"
android:key="prefs_key_system_ui_statusbar_clock_size_2"
android:title="@string/system_ui_statusbar_clock_size"
app:defaultValueText="@string/array_default"
app:format="%d dp"
app:maxValue="60"
app:maxValue="66"
app:minValue="20"
app:showSeekBarValue="true"
app:stepValue="1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<SeekBarPreferenceEx
android:key="prefs_key_system_ui_statusbar_clock_size_3"
android:title="@string/system_ui_statusbar_clock_size"
android:defaultValue="9"
android:defaultValue="12"
app:defaultValueText="@string/array_default"
app:maxValue="32"
app:minValue="9"
Expand Down

0 comments on commit 3d780c5

Please sign in to comment.