From 1af8e0605b0d4e58ee8c71f68571973826190460 Mon Sep 17 00:00:00 2001 From: LittleTurtle2333 Date: Mon, 6 Mar 2023 15:44:23 +0800 Subject: [PATCH] Update StatusBarLayoutForT.kt --- .../rules/t/systemui/StatusBarLayoutForT.kt | 521 ++++++++++++++++++ 1 file changed, 521 insertions(+) create mode 100644 app/src/main/java/com/lt2333/simplicitytools/hooks/rules/t/systemui/StatusBarLayoutForT.kt diff --git a/app/src/main/java/com/lt2333/simplicitytools/hooks/rules/t/systemui/StatusBarLayoutForT.kt b/app/src/main/java/com/lt2333/simplicitytools/hooks/rules/t/systemui/StatusBarLayoutForT.kt new file mode 100644 index 00000000..60c4763f --- /dev/null +++ b/app/src/main/java/com/lt2333/simplicitytools/hooks/rules/t/systemui/StatusBarLayoutForT.kt @@ -0,0 +1,521 @@ +package com.lt2333.simplicitytools.hooks.rules.t.systemui + +import android.app.KeyguardManager +import android.content.Context +import android.content.res.Configuration +import android.content.res.Resources +import android.view.Gravity +import android.view.View +import android.view.ViewGroup +import android.widget.LinearLayout +import android.widget.TextView +import androidx.constraintlayout.widget.ConstraintLayout +import cn.fkj233.ui.activity.dp2px +import com.highcapable.yukihookapi.hook.entity.YukiBaseHooker +import com.highcapable.yukihookapi.hook.factory.current +import com.lt2333.simplicitytools.utils.XSPUtils +import com.lt2333.simplicitytools.utils.hasEnable + +object StatusBarLayoutForT : YukiBaseHooker() { + + private val getMode = XSPUtils.getInt("status_bar_layout_mode", 0) + private val isCompatibilityMode = XSPUtils.getBoolean("layout_compatibility_mode", false) + + private var statusBarLeft = 0 + private var statusBarTop = 0 + private var statusBarRight = 0 + private var statusBarBottom = 0 + + override fun onHook() { + var mLeftLayout: LinearLayout? = null + var mRightLayout: LinearLayout? = null + var mCenterLayout: LinearLayout? + var statusBar: ViewGroup? = null + + //判断屏幕状态更新布局 mode: 1正常布局 2居中布局 + fun updateLayout(context: Context, mode: Int) { + runCatching { + when (mode) { + 1 -> { + val mConfiguration: Configuration = context.resources.configuration + if (mConfiguration.orientation == Configuration.ORIENTATION_PORTRAIT) { //横屏 + statusBar!!.setPadding(statusBarLeft, statusBarTop, statusBarRight, statusBarBottom) + } + } + + 2 -> { + val mConfiguration: Configuration = context.resources.configuration + if (mConfiguration.orientation == Configuration.ORIENTATION_PORTRAIT) { //横屏 + mLeftLayout!!.setPadding(statusBarLeft, 0, 0, 0) + mRightLayout!!.setPadding(0, 0, statusBarRight, 0) + statusBar!!.setPadding(0, statusBarTop, 0, statusBarBottom) + } else { //竖屏 + mLeftLayout!!.setPadding(0, 0, 0, 0) + mRightLayout!!.setPadding(0, 0, 0, 0) + } + } + } + }.getOrNull() + + } + + //判断是否开启挖孔兼容模式 + if (isCompatibilityMode) { + "com.android.systemui.ScreenDecorations".hook { + injectMember { + method { + name = "boundsFromDirection" + paramCount = 3 + } + beforeHook { + args[1] = 0 + } + } + } + } + + //修改对应布局 + when (getMode) { + //默认 + 0 -> { + "com.android.systemui.statusbar.phone.fragment.CollapsedStatusBarFragment".hook { + injectMember { + method { + name = "onViewCreated" + paramCount = 2 + } + afterHook { + val miuiPhoneStatusBarView =args(0).cast()!! + val context: Context = miuiPhoneStatusBarView.context + val res: Resources = miuiPhoneStatusBarView.resources + val statusBarId: Int = res.getIdentifier("status_bar", "id", "com.android.systemui") + statusBar = miuiPhoneStatusBarView.findViewById(statusBarId) + if (statusBar == null) return@afterHook + + statusBarLeft = statusBar!!.paddingLeft + statusBarTop = statusBar!!.paddingTop + statusBarRight = statusBar!!.paddingRight + statusBarBottom = statusBar!!.paddingBottom + + if (isCompatibilityMode) { + val customLeftMargin = XSPUtils.getInt("status_bar_left_margin", 0) + if (customLeftMargin != 0) { + statusBarLeft = customLeftMargin + } + + val customRightMargin = XSPUtils.getInt("status_bar_right_margin", 0) + if (customRightMargin != 0) { + statusBarRight = customRightMargin + } + updateLayout(context, 1) + } + } + } + } + //兼容模式 + "com.android.systemui.statusbar.phone.PhoneStatusBarView".hook { + injectMember { + method { + name = "updateLayoutForCutout" + } + afterHook { + if (isCompatibilityMode) { + val context = instance().context + updateLayout(context, 1) + } + } + } + } + } + //时钟居中 + 1 -> { + "com.android.systemui.statusbar.phone.fragment.CollapsedStatusBarFragment".hook { + injectMember { + method { + name = "onViewCreated" + paramCount = 2 + } + afterHook { + val miuiPhoneStatusBarView = args(0).cast()!! + val context: Context = miuiPhoneStatusBarView.context + val res: Resources = miuiPhoneStatusBarView.resources + val statusBarId: Int = res.getIdentifier("status_bar", "id", "com.android.systemui") + val statusBarContentsId: Int = res.getIdentifier( + "status_bar_contents", "id", "com.android.systemui" + ) + val systemIconAreaId: Int = res.getIdentifier("system_icon_area", "id", "com.android.systemui") + val clockId: Int = res.getIdentifier("clock", "id", "com.android.systemui") + val phoneStatusBarLeftContainerId: Int = res.getIdentifier( + "phone_status_bar_left_container", "id", "com.android.systemui" + ) + val notificationIconAreaInnerId: Int = res.getIdentifier( + "notification_icon_area_inner", "id", "com.android.systemui" + ) + statusBar = miuiPhoneStatusBarView.findViewById(statusBarId) + val statusBarContents: ViewGroup = miuiPhoneStatusBarView.findViewById(statusBarContentsId) + if (statusBar == null) return@afterHook + val clock: TextView = miuiPhoneStatusBarView.findViewById(clockId) + val phoneStatusBarLeftContainer: ViewGroup = miuiPhoneStatusBarView.findViewById( + phoneStatusBarLeftContainerId + ) + val notificationIconAreaInner: ViewGroup = miuiPhoneStatusBarView.findViewById( + notificationIconAreaInnerId + ) + val systemIconArea: ViewGroup = miuiPhoneStatusBarView.findViewById(systemIconAreaId) + + (clock.parent as ViewGroup).removeView(clock) + (phoneStatusBarLeftContainer.parent as ViewGroup).removeView( + phoneStatusBarLeftContainer + ) + (notificationIconAreaInner.parent as ViewGroup).removeView( + notificationIconAreaInner + ) + (systemIconArea.parent as ViewGroup).removeView(systemIconArea) + + val mConstraintLayout = ConstraintLayout(context).also { + it.layoutParams = ConstraintLayout.LayoutParams( + ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT + ) + } + + mConstraintLayout.addView(notificationIconAreaInner) + + val fullscreenNotificationIconAreaLp = LinearLayout.LayoutParams( + ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT + ) + + notificationIconAreaInner.layoutParams = fullscreenNotificationIconAreaLp + + //增加一个左对齐布局 + mLeftLayout = LinearLayout(context) + val leftLp: LinearLayout.LayoutParams = LinearLayout.LayoutParams( + 0, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f + ) + mLeftLayout!!.layoutParams = leftLp + mLeftLayout!!.gravity = Gravity.START or Gravity.CENTER_VERTICAL + + //增加一个居中布局 + mCenterLayout = LinearLayout(context) + val centerLp: LinearLayout.LayoutParams = LinearLayout.LayoutParams( + LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT + ) + mCenterLayout!!.layoutParams = centerLp + mCenterLayout!!.gravity = Gravity.CENTER or Gravity.CENTER_VERTICAL + mRightLayout = LinearLayout(context) + val rightLp: LinearLayout.LayoutParams = LinearLayout.LayoutParams( + 0, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f + ) + mRightLayout!!.layoutParams = rightLp + mRightLayout!!.gravity = Gravity.END or Gravity.CENTER_VERTICAL + mLeftLayout!!.addView(phoneStatusBarLeftContainer) + mLeftLayout!!.addView(mConstraintLayout) + + mCenterLayout!!.addView(clock) + mRightLayout!!.addView(systemIconArea) + statusBarContents.addView(mLeftLayout, 0) + statusBarContents.addView(mCenterLayout) + statusBarContents.addView(mRightLayout) + + statusBarLeft = statusBar!!.paddingLeft + statusBarTop = statusBar!!.paddingTop + statusBarRight = statusBar!!.paddingRight + statusBarBottom = statusBar!!.paddingBottom + + + if (isCompatibilityMode) { + val customLeftMargin = XSPUtils.getInt("status_bar_left_margin", 0) + if (customLeftMargin != 0) { + statusBarLeft = customLeftMargin + } + + val customRightMargin = XSPUtils.getInt("status_bar_right_margin", 0) + if (customRightMargin != 0) { + statusBarRight = customRightMargin + } + updateLayout(context, 2) + } + } + } + } + //兼容模式 + "com.android.systemui.statusbar.phone.PhoneStatusBarView".hook { + injectMember { + method { + name = "updateLayoutForCutout" + } + afterHook { + hasEnable("layout_compatibility_mode") { + val context = instance().context + updateLayout(context, 2) + } + } + } + } + } + //时钟居右 + 2 -> { + "com.android.systemui.statusbar.phone.fragment.CollapsedStatusBarFragment".hook { + injectMember { + method { + name = "onViewCreated" + paramCount = 2 + } + afterHook { + val miuiPhoneStatusBarView = args(0).cast()!! + val context: Context = miuiPhoneStatusBarView.context + val res: Resources = miuiPhoneStatusBarView.resources + + //组件ID + val statusBarId: Int = res.getIdentifier("status_bar", "id", "com.android.systemui") + val clockId: Int = res.getIdentifier("clock", "id", "com.android.systemui") + val batteryId: Int = res.getIdentifier("battery", "id", "com.android.systemui") + + //查找组件 + statusBar = miuiPhoneStatusBarView.findViewById(statusBarId) + if (statusBar == null) return@afterHook + val clock: TextView = miuiPhoneStatusBarView.findViewById(clockId) + val battery: ViewGroup = miuiPhoneStatusBarView.findViewById(batteryId) + + //新建布局 + val rightLp = LinearLayout.LayoutParams( + LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT + ).also { + it.marginStart = dp2px(context, 5f) + } + mRightLayout = LinearLayout(context).also { + it.layoutParams = rightLp + } + + //添加布局与组件 + battery.addView(mRightLayout) + (clock.parent as ViewGroup).removeView(clock) + mRightLayout!!.addView(clock) + + + statusBarLeft = statusBar!!.paddingLeft + statusBarTop = statusBar!!.paddingTop + statusBarRight = statusBar!!.paddingRight + statusBarBottom = statusBar!!.paddingBottom + + + if (isCompatibilityMode) { + val customLeftMargin = XSPUtils.getInt("status_bar_left_margin", 0) + if (customLeftMargin != 0) { + statusBarLeft = customLeftMargin + } + + val customRightMargin = XSPUtils.getInt("status_bar_right_margin", 0) + if (customRightMargin != 0) { + statusBarRight = customRightMargin + } + updateLayout(context, 1) + } + } + } + } + //兼容模式 + "com.android.systemui.statusbar.phone.PhoneStatusBarView".hook { + injectMember { + method { + name = "updateLayoutForCutout" + } + afterHook { + if (isCompatibilityMode) { + val context = instance().context + updateLayout(context, 1) + } + } + } + } + } + //时钟居中+图标居左 + 3 -> { + "com.android.systemui.statusbar.phone.fragment.CollapsedStatusBarFragment".hook { + injectMember { + method { + name = "onViewCreated" + paramCount = 2 + } + afterHook { + val miuiPhoneStatusBarView = args(0).cast()!! + val context: Context = miuiPhoneStatusBarView.context + val res: Resources = miuiPhoneStatusBarView.resources + val statusBarId: Int = res.getIdentifier("status_bar", "id", "com.android.systemui") + val statusBarContentsId: Int = res.getIdentifier( + "status_bar_contents", "id", "com.android.systemui" + ) + val systemIconAreaId: Int = res.getIdentifier("system_icon_area", "id", "com.android.systemui") + val clockId: Int = res.getIdentifier("clock", "id", "com.android.systemui") + val phoneStatusBarLeftContainerId: Int = res.getIdentifier( + "phone_status_bar_left_container", "id", "com.android.systemui" + ) + val fullscreenNotificationIconAreaId: Int = res.getIdentifier( + "fullscreen_notification_icon_area", "id", "com.android.systemui" + ) + val statusIconsId: Int = res.getIdentifier( + "statusIcons", "id", "com.android.systemui" + ) + val systemIconsId: Int = res.getIdentifier( + "system_icons", "id", "com.android.systemui" + ) + val batteryId: Int = res.getIdentifier( + "battery", "id", "com.android.systemui" + ) + + statusBar = miuiPhoneStatusBarView.findViewById(statusBarId) + val statusBarContents: ViewGroup = miuiPhoneStatusBarView.findViewById(statusBarContentsId) + if (statusBar == null) return@afterHook + val clock: TextView = miuiPhoneStatusBarView.findViewById(clockId) + val phoneStatusBarLeftContainer: ViewGroup = miuiPhoneStatusBarView.findViewById( + phoneStatusBarLeftContainerId + ) + val fullscreenNotificationIconArea: ViewGroup = miuiPhoneStatusBarView.findViewById( + fullscreenNotificationIconAreaId + ) + val systemIconArea: ViewGroup = miuiPhoneStatusBarView.findViewById(systemIconAreaId) + val statusIcons: ViewGroup = miuiPhoneStatusBarView.findViewById(statusIconsId) + val systemIcons: ViewGroup = miuiPhoneStatusBarView.findViewById(systemIconsId) + val battery: ViewGroup = miuiPhoneStatusBarView.findViewById(batteryId) + + (clock.parent as ViewGroup).removeView(clock) + (phoneStatusBarLeftContainer.parent as ViewGroup).removeView( + phoneStatusBarLeftContainer + ) + (systemIconArea.parent as ViewGroup).removeView(systemIconArea) + (statusIcons.parent as ViewGroup).removeView(statusIcons) + (systemIcons.parent as ViewGroup).removeView(systemIcons) + (battery.parent as ViewGroup).removeView(battery) + (fullscreenNotificationIconArea.parent as ViewGroup).removeView( + fullscreenNotificationIconArea + ) + + val mConstraintLayout = ConstraintLayout(context).also { + it.layoutParams = ConstraintLayout.LayoutParams( + ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT + ) + } + + mConstraintLayout.addView(fullscreenNotificationIconArea) + mConstraintLayout.addView(battery) + + + battery.layoutParams = ConstraintLayout.LayoutParams( + ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.MATCH_PARENT + ).also { + it.endToEnd = 0 + } + + + fullscreenNotificationIconArea.layoutParams = ConstraintLayout.LayoutParams( + 0, ConstraintLayout.LayoutParams.MATCH_PARENT + ).also { + it.startToEnd = batteryId + it.endToEnd = 0 + } + fullscreenNotificationIconArea.layoutDirection = View.LAYOUT_DIRECTION_RTL + + + //增加一个左对齐布局 + mLeftLayout = LinearLayout(context) + val leftLp: LinearLayout.LayoutParams = LinearLayout.LayoutParams( + 0, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f + ) + mLeftLayout!!.layoutParams = leftLp + mLeftLayout!!.gravity = Gravity.START or Gravity.CENTER_VERTICAL + + //增加一个居中布局 + mCenterLayout = LinearLayout(context) + val centerLp: LinearLayout.LayoutParams = LinearLayout.LayoutParams( + LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT + ) + mCenterLayout!!.layoutParams = centerLp + mCenterLayout!!.gravity = Gravity.CENTER or Gravity.CENTER_VERTICAL + + //增加一个右布局 + mRightLayout = LinearLayout(context) + val rightLp: LinearLayout.LayoutParams = LinearLayout.LayoutParams( + 0, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f + ) + mRightLayout!!.layoutParams = rightLp + mRightLayout!!.gravity = Gravity.END or Gravity.CENTER_VERTICAL + + + mLeftLayout!!.addView(phoneStatusBarLeftContainer) + mLeftLayout!!.addView(statusIcons) + statusIcons.layoutDirection = View.LAYOUT_DIRECTION_RTL + + mCenterLayout!!.addView(clock) + + mRightLayout!!.addView(mConstraintLayout) + fullscreenNotificationIconArea.layoutDirection = View.LAYOUT_DIRECTION_RTL + + + statusBarContents.addView(mLeftLayout, 0) + statusBarContents.addView(mCenterLayout) + statusBarContents.addView(mRightLayout) + + + statusBarLeft = statusBar!!.paddingLeft + statusBarTop = statusBar!!.paddingTop + statusBarRight = statusBar!!.paddingRight + statusBarBottom = statusBar!!.paddingBottom + + + if (isCompatibilityMode) { + val customLeftMargin = XSPUtils.getInt("status_bar_left_margin", 0) + if (customLeftMargin != 0) { + statusBarLeft = customLeftMargin + } + + val customRightMargin = XSPUtils.getInt("status_bar_right_margin", 0) + if (customRightMargin != 0) { + statusBarRight = customRightMargin + } + updateLayout(context, 2) + } + } + } + } + //兼容模式 + "com.android.systemui.statusbar.phone.PhoneStatusBarView".hook { + injectMember { + method { + name = "updateLayoutForCutout" + } + afterHook { + if (isCompatibilityMode) { + val context = instance().context + updateLayout(context, 2) + } + } + } + } + //解决重叠 + "com.android.systemui.statusbar.phone.MiuiCollapsedStatusBarFragment".hook { + injectMember { + method { + name = "showClock" + param { it[0] == Boolean::class.java } + } + afterHook { + val miuiPhoneStatusBarView = instance.current().field { + name = "mStatusBar" + superClass(true) + }.cast()!! + val res = miuiPhoneStatusBarView.resources + val statusBarId = res.getIdentifier("status_bar", "id", "com.android.systemui") + val statusBar1 = miuiPhoneStatusBarView.findViewById(statusBarId) + //非锁屏下整个状态栏布局 + val keyguardMgr = statusBar1.context.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager + if (keyguardMgr.isKeyguardLocked) { + statusBar1!!.visibility = View.GONE + } else { + statusBar1!!.visibility = View.VISIBLE + } + } + } + } + } + } + } +} \ No newline at end of file