simplify OldNotificationWeather.kt

This commit is contained in:
QQ littleice
2022-03-10 22:48:40 +08:00
parent 3dc5170bb8
commit 8587984a88

View File

@@ -6,108 +6,55 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.TextView import android.widget.TextView
import android.widget.Toast import android.widget.Toast
import com.lt2333.simplicitytools.util.XSPUtils import com.github.kyuubiran.ezxhelper.utils.loadClass
import com.lt2333.simplicitytools.util.*
import com.lt2333.simplicitytools.view.WeatherView import com.lt2333.simplicitytools.view.WeatherView
import de.robv.android.xposed.IXposedHookLoadPackage import de.robv.android.xposed.IXposedHookLoadPackage
import de.robv.android.xposed.XC_MethodHook
import de.robv.android.xposed.XposedHelpers
import de.robv.android.xposed.callbacks.XC_LoadPackage import de.robv.android.xposed.callbacks.XC_LoadPackage
class OldNotificationWeather : IXposedHookLoadPackage { class OldNotificationWeather : IXposedHookLoadPackage {
override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam) { override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam) {
hasEnable("notification_weather") {
var mWeatherView: TextView? = null var mWeatherView: TextView? = null
if (!XSPUtils.getBoolean("notification_weather", false)) return
val isDisplayCity = XSPUtils.getBoolean("notification_weather_city", false) val isDisplayCity = XSPUtils.getBoolean("notification_weather_city", false)
"com.android.systemui.qs.MiuiQSHeaderView".hookAfterMethod(lpparam.classLoader, "onFinishInflate") {
val viewGroup = it.thisObject as ViewGroup
val context = viewGroup.context
val layoutParam = loadClass("androidx.constraintlayout.widget.ConstraintLayout\$LayoutParams")
.getConstructor(Int::class.java, Int::class.java)
.newInstance(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) as ViewGroup.MarginLayoutParams
val classIfExists = XposedHelpers.findClassIfExists( layoutParam.setObjectField("engToStart", context.resources.getIdentifier("notification_shade_shortcut", "id", context.packageName))
"com.android.systemui.qs.MiuiQSHeaderView", layoutParam.setObjectField("topToTop", context.resources.getIdentifier("notification_shade_shortcut", "id", context.packageName))
lpparam.classLoader layoutParam.setObjectField("bottomToBottom", context.resources.getIdentifier("notification_shade_shortcut", "id", context.packageName))
)
XposedHelpers.findAndHookMethod(classIfExists, "onFinishInflate", object : XC_MethodHook() { mWeatherView = WeatherView(context, isDisplayCity).apply {
override fun afterHookedMethod(param: MethodHookParam) { setTextAppearance(context.resources.getIdentifier("TextAppearance.StatusBar.Expanded.Clock.QuickSettingDate", "style", context.packageName))
val viewGroup = param.thisObject as ViewGroup layoutParams = layoutParam
val layout = XposedHelpers.findClass(
"androidx.constraintlayout.widget.ConstraintLayout\$LayoutParams",
lpparam.classLoader
).getConstructor(Int::class.java, Int::class.java).newInstance(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
) as ViewGroup.MarginLayoutParams
XposedHelpers.setObjectField(
layout,
"endToStart",
viewGroup.context.resources.getIdentifier(
"notification_shade_shortcut",
"id",
viewGroup.context.packageName
)
)
XposedHelpers.setObjectField(
layout,
"topToTop",
viewGroup.context.resources.getIdentifier(
"notification_shade_shortcut",
"id",
viewGroup.context.packageName
)
)
XposedHelpers.setObjectField(
layout,
"bottomToBottom",
viewGroup.context.resources.getIdentifier(
"notification_shade_shortcut",
"id",
viewGroup.context.packageName
)
)
mWeatherView = WeatherView(viewGroup.context,isDisplayCity).also {
it.setTextAppearance(
viewGroup.context.resources.getIdentifier(
"TextAppearance.StatusBar.Expanded.Clock.QuickSettingDate",
"style",
viewGroup.context.packageName
)
)
it.layoutParams = layout
} }
viewGroup.addView(mWeatherView) viewGroup.addView(mWeatherView)
(mWeatherView as WeatherView).setOnClickListener { (mWeatherView as WeatherView).setOnClickListener {
try { try {
val intent = Intent() val intent = Intent().apply {
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.component = ComponentName( component = ComponentName("com.miui.weather2", "com.miui.weather2.ActivityWeatherMain")
"com.miui.weather2", }
"com.miui.weather2.ActivityWeatherMain" context.startActivity(intent)
)
viewGroup.context.startActivity(intent)
} catch (e: Exception) { } catch (e: Exception) {
Toast.makeText(viewGroup.context, "启动失败,可能是不支持", Toast.LENGTH_LONG).show() Toast.makeText(context, "启动失败,可能是不支持", Toast.LENGTH_LONG).show()
} }
} }
} }
})
//解决横屏重叠 //解决横屏重叠
XposedHelpers.findAndHookMethod(classIfExists, "updateLayout", object : XC_MethodHook() { "com.android.systemui.qs.MiuiQSHeaderView".hookAfterMethod(lpparam.classLoader, "updateLayout") {
override fun afterHookedMethod(param: MethodHookParam) { val mOritation = it.thisObject.getObjectField("mOrientation") as Int
super.afterHookedMethod(param) if (mOritation == 1) {
val mOrientation =
XposedHelpers.getObjectField(param.thisObject, "mOrientation") as Int
if (mOrientation == 1) {
mWeatherView!!.visibility = View.VISIBLE mWeatherView!!.visibility = View.VISIBLE
} else { } else {
mWeatherView!!.visibility = View.GONE mWeatherView!!.visibility = View.GONE
} }
} }
}) }
} }
} }