From d47aa371ab6c69cfc0c0e4ed65c2347cf1d42dc6 Mon Sep 17 00:00:00 2001 From: LittleTurtle2333 Date: Mon, 27 Feb 2023 17:43:16 +0800 Subject: [PATCH] Update NotificationWeatherForT.kt --- .../t/systemui/NotificationWeatherForT.kt | 202 ++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100644 app/src/main/java/com/lt2333/simplicitytools/hooks/rules/t/systemui/NotificationWeatherForT.kt diff --git a/app/src/main/java/com/lt2333/simplicitytools/hooks/rules/t/systemui/NotificationWeatherForT.kt b/app/src/main/java/com/lt2333/simplicitytools/hooks/rules/t/systemui/NotificationWeatherForT.kt new file mode 100644 index 00000000..f033f03a --- /dev/null +++ b/app/src/main/java/com/lt2333/simplicitytools/hooks/rules/t/systemui/NotificationWeatherForT.kt @@ -0,0 +1,202 @@ +package com.lt2333.simplicitytools.hooks.rules.t.systemui + +import android.content.ComponentName +import android.content.Intent +import android.view.View +import android.view.ViewGroup +import android.widget.LinearLayout +import android.widget.TextView +import android.widget.Toast +import androidx.constraintlayout.widget.ConstraintLayout +import cn.fkj233.ui.activity.dp2px +import com.highcapable.yukihookapi.hook.entity.YukiBaseHooker +import com.highcapable.yukihookapi.hook.factory.constructor +import com.highcapable.yukihookapi.hook.factory.current +import com.highcapable.yukihookapi.hook.factory.toClass +import com.lt2333.simplicitytools.utils.SystemProperties +import com.lt2333.simplicitytools.utils.XSPUtils +import com.lt2333.simplicitytools.utils.hasEnable +import com.lt2333.simplicitytools.views.WeatherView + +object NotificationWeatherForT : YukiBaseHooker() { + override fun onHook() = hasEnable("notification_weather") { + var mWeatherView: TextView? = null + var mConstraintLayout: ConstraintLayout? = null + val isDisplayCity = XSPUtils.getBoolean("notification_weather_city", false) + "com.android.systemui.qs.MiuiNotificationHeaderView".hook { + injectMember { + method { name = "onFinishInflate" } + afterHook { + val viewGroup = instance() + val context = viewGroup.context + + // MIUI编译时间大于 2022-03-12 00:00:00 且为内测版 + if (SystemProperties[context, "ro.build.date.utc"].toInt() >= 1647014400 && + !SystemProperties[context, "ro.build.version.incremental"].endsWith("XM") + ) { + //获取原组件 + val bigTimeId = + context.resources.getIdentifier("big_time", "id", context.packageName) + val bigTime: TextView = viewGroup.findViewById(bigTimeId) + + val dateTimeId = + context.resources.getIdentifier("date_time", "id", context.packageName) + val dateTime: TextView = viewGroup.findViewById(dateTimeId) + + //创建新布局 + val mConstraintLayoutLp = LinearLayout.LayoutParams( + LinearLayout.LayoutParams.MATCH_PARENT, + LinearLayout.LayoutParams.WRAP_CONTENT + ).also { + it.topMargin = context.resources.getDimensionPixelSize( + context.resources.getIdentifier( + "qs_control_header_tiles_margin_top", + "dimen", + context.packageName + ) + ) + } + + mConstraintLayout = + ConstraintLayout(context).also { it.layoutParams = mConstraintLayoutLp } + + (bigTime.parent as ViewGroup).addView(mConstraintLayout, 0) + + + //从原布局中删除组件 + (bigTime.parent as ViewGroup).removeView(bigTime) + (dateTime.parent as ViewGroup).removeView(dateTime) + + + //添加组件至新布局 + mConstraintLayout!!.addView(bigTime) + mConstraintLayout!!.addView(dateTime) + + //组件属性 + + val dateTimeLp = ConstraintLayout.LayoutParams( + ConstraintLayout.LayoutParams.WRAP_CONTENT, + ConstraintLayout.LayoutParams.WRAP_CONTENT + ).also { + it.startToEnd = bigTimeId + it.bottomToBottom = 0 + it.marginStart = context.resources.getDimensionPixelSize( + context.resources.getIdentifier( + "notification_panel_time_date_space", + "dimen", + context.packageName + ) + ) + it.bottomMargin = dp2px(context, 5f) + } + dateTime.layoutParams = dateTimeLp + + + //创建天气组件 + mWeatherView = WeatherView(context, isDisplayCity).apply { + setTextAppearance( + context.resources.getIdentifier( + "TextAppearance.QSControl.Date", + "style", + context.packageName + ) + ) + + } + mConstraintLayout!!.addView(mWeatherView) + + val mweatherviewLp = ConstraintLayout.LayoutParams( + ConstraintLayout.LayoutParams.WRAP_CONTENT, + ConstraintLayout.LayoutParams.WRAP_CONTENT + ).also { + it.startToEnd = bigTimeId + it.bottomToTop = dateTimeId + it.marginStart = context.resources.getDimensionPixelSize( + context.resources.getIdentifier( + "notification_panel_time_date_space", + "dimen", + context.packageName + ) + ) + } + + (mWeatherView as WeatherView).layoutParams = mweatherviewLp + + } else { + + val layoutParam = "androidx.constraintlayout.widget.ConstraintLayout\$LayoutParams".toClass() + .constructor { + paramCount = 2 + }.get().newInstance( + ViewGroup.LayoutParams.WRAP_CONTENT, + ViewGroup.LayoutParams.WRAP_CONTENT + ) + + + layoutParam?.current()?.field { name = "bottomToTop" }?.set(context.resources.getIdentifier("date_time", "id", context.packageName)) + layoutParam?.current()?.field { name = "startToEnd" }?.set(context.resources.getIdentifier("big_time", "id", context.packageName)) + + layoutParam?.marginStart = context.resources.getDimensionPixelSize( + context.resources.getIdentifier( + "notification_panel_time_date_space", + "dimen", + context.packageName + ) + ) + mWeatherView = WeatherView(context, isDisplayCity).apply { + setTextAppearance( + context.resources.getIdentifier( + "TextAppearance.QSControl.Date", + "style", + context.packageName + ) + ) + layoutParams = layoutParam + } + viewGroup.addView(mWeatherView) + } + + (mWeatherView as WeatherView).setOnClickListener { + try { + val intent = Intent().apply { + flags = Intent.FLAG_ACTIVITY_NEW_TASK + component = ComponentName( + "com.miui.weather2", + "com.miui.weather2.ActivityWeatherMain" + ) + } + context.startActivity(intent) + } catch (e: Exception) { + Toast.makeText(context, "启动失败", Toast.LENGTH_LONG).show() + } + } + } + } + injectMember { + method { name = "updateLayout" } + afterHook { + val viewGroup = instance() + val context = viewGroup.context + val mOrientation = viewGroup.current().field { name = "mOrientation" }.int() + // MIUI编译时间大于 2022-03-12 00:00:00 且为内测版 + if (SystemProperties[context, "ro.build.date.utc"].toInt() >= 1647014400 && !SystemProperties[context, "ro.build.version.incremental"].endsWith( + "DEV" + ) && !SystemProperties[context, "ro.build.version.incremental"].endsWith("XM") + ) { + if (mOrientation == 1) { + mConstraintLayout!!.visibility = View.VISIBLE + } else { + mConstraintLayout!!.visibility = View.GONE + } + } else { + if (mOrientation == 1) { + mWeatherView!!.visibility = View.VISIBLE + } else { + mWeatherView!!.visibility = View.GONE + } + } + } + } + } + } +} \ No newline at end of file