新增状态栏日期功能

This commit is contained in:
LittleTurtle2333
2022-02-22 22:52:03 +08:00
parent bfe67dc5b4
commit 58bb21884f
3 changed files with 161 additions and 58 deletions

View File

@@ -82,12 +82,6 @@ class SettingsActivity : MIUIActivity() {
)
add(LineV())
add(TitleTextV(resId = R.string.statusbar))
add(
TextWithSwitchV(
TextV(resId = R.string.status_bar_time_seconds),
SwitchV("status_bar_time_seconds")
)
)
add(
TextWithSwitchV(
TextV(resId = R.string.status_bar_network_speed_refresh_speed),
@@ -113,6 +107,44 @@ class SettingsActivity : MIUIActivity() {
)
)
add(LineV())
add(TitleTextV(resId = R.string.status_bar_clock_format))
add(
TextWithSwitchV(
TextV(resId = R.string.status_bar_time_year),
SwitchV("status_bar_time_year")
)
)
add(
TextWithSwitchV(
TextV(resId = R.string.status_bar_time_month),
SwitchV("status_bar_time_month")
)
)
add(
TextWithSwitchV(
TextV(resId = R.string.status_bar_time_day),
SwitchV("status_bar_time_day")
)
)
add(
TextWithSwitchV(
TextV(resId = R.string.status_bar_time_week),
SwitchV("status_bar_time_week")
)
)
add(
TextWithSwitchV(
TextV(resId = R.string.status_bar_time_seconds),
SwitchV("status_bar_time_seconds")
)
)
add(
TextWithSwitchV(
TextV(resId = R.string.status_bar_time_hide_space),
SwitchV("status_bar_time_hide_space")
)
)
add(LineV())
add(TitleTextV(resId = R.string.status_bar_icon))
add(
TextWithSwitchV(

View File

@@ -15,62 +15,127 @@ import java.lang.reflect.Method
import java.text.SimpleDateFormat
import java.util.*
class StatusBarTimeSeconds :IXposedHookLoadPackage {
class StatusBarTimeSeconds : IXposedHookLoadPackage {
override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam) {
if (XSPUtils.getBoolean("status_bar_time_seconds", false)) {
var c: Context? = null
val classIfExists = XposedHelpers.findClassIfExists(
"com.android.systemui.statusbar.views.MiuiClock",
lpparam.classLoader
)
XposedHelpers.findAndHookConstructor(classIfExists,
Context::class.java,
AttributeSet::class.java,
Integer.TYPE,
object : XC_MethodHook() {
override fun afterHookedMethod(param: MethodHookParam) {
c = param.args[0] as Context
val textV = param.thisObject as TextView
val d: Method = textV.javaClass.getDeclaredMethod("updateTime")
val r = Runnable {
d.isAccessible = true
d.invoke(textV)
}
class T : TimerTask() {
override fun run() {
Handler(textV.context.mainLooper).post(r)
}
var c: Context? = null
val classIfExists = XposedHelpers.findClassIfExists(
"com.android.systemui.statusbar.views.MiuiClock",
lpparam.classLoader
)
XposedHelpers.findAndHookConstructor(classIfExists,
Context::class.java,
AttributeSet::class.java,
Integer.TYPE,
object : XC_MethodHook() {
override fun afterHookedMethod(param: MethodHookParam) {
c = param.args[0] as Context
val textV = param.thisObject as TextView
val d: Method = textV.javaClass.getDeclaredMethod("updateTime")
val r = Runnable {
d.isAccessible = true
d.invoke(textV)
}
class T : TimerTask() {
override fun run() {
Handler(textV.context.mainLooper).post(r)
}
if (textV.resources.getResourceEntryName(textV.id) == "clock")
Timer().scheduleAtFixedRate(
T(),
1000 - System.currentTimeMillis() % 1000,
1000
)
}
if (textV.resources.getResourceEntryName(textV.id) == "clock")
Timer().scheduleAtFixedRate(
T(),
1000 - System.currentTimeMillis() % 1000,
1000
)
}
}
)
XposedHelpers.findAndHookMethod(classIfExists, "updateTime",
object : XC_MethodHook() {
@SuppressLint("SetTextI18n", "SimpleDateFormat")
override fun afterHookedMethod(param: MethodHookParam) {
val textV = param.thisObject as TextView
if (textV.resources.getResourceEntryName(textV.id) == "clock") {
val t = Settings.System.getString(
c!!.contentResolver,
Settings.System.TIME_12_24
)
if (t == "24")
textV.text =
getYear() + getMonth() + getDay() + getDateSpace() + getWeek() + SimpleDateFormat(
"HH:mm"
).format(Calendar.getInstance().time)+getSecond()
else
textV.text =
getYear() + getMonth() + getDay() + getDateSpace() + getWeek() +textV.text.toString() + getSecond()
}
}
)
XposedHelpers.findAndHookMethod(classIfExists, "updateTime",
object : XC_MethodHook() {
@SuppressLint("SetTextI18n", "SimpleDateFormat")
override fun afterHookedMethod(param: MethodHookParam) {
val textV = param.thisObject as TextView
if (textV.resources.getResourceEntryName(textV.id) == "clock") {
val t = Settings.System.getString(
c!!.contentResolver,
Settings.System.TIME_12_24
)
if (t == "24")
textV.text =
SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().time)
else
textV.text =
textV.text.toString() + SimpleDateFormat(":ss").format(Calendar.getInstance().time)
}
}
})
})
}
}
@SuppressLint("SimpleDateFormat")
private fun getYear(): String {
if (XSPUtils.getBoolean("status_bar_time_year", false)) {
return SimpleDateFormat("YY年").format(Calendar.getInstance().time)
}
return ""
}
@SuppressLint("SimpleDateFormat")
private fun getMonth(): String {
if (XSPUtils.getBoolean("status_bar_time_month", false)) {
return SimpleDateFormat("M月").format(Calendar.getInstance().time)
}
return ""
}
@SuppressLint("SimpleDateFormat")
private fun getDay(): String {
if (XSPUtils.getBoolean("status_bar_time_day", false)) {
return SimpleDateFormat("d日").format(Calendar.getInstance().time)
}
return ""
}
@SuppressLint("SimpleDateFormat")
private fun getSecond(): String {
if (XSPUtils.getBoolean("status_bar_time_seconds", false)) {
return SimpleDateFormat(":ss").format(Calendar.getInstance().time)
}
return ""
}
@SuppressLint("SimpleDateFormat")
private fun getWeek(): String {
var week = ""
if (XSPUtils.getBoolean("status_bar_time_week", false)) {
week = SimpleDateFormat("E").format(Calendar.getInstance().time)
if (XSPUtils.getBoolean("status_bar_time_hide_space", false)) {
week += ""
} else {
week += " "
}
}
return week
}
private fun getDateSpace(): String {
if (XSPUtils.getBoolean("status_bar_time_year", false) ||
XSPUtils.getBoolean("status_bar_time_month", false) ||
XSPUtils.getBoolean("status_bar_time_day", false)
) {
if (XSPUtils.getBoolean("status_bar_time_hide_space", false)) {
return ""
} else {
return " "
}
}
return ""
}
}