From 8587984a88934f46ca0d6463623b88868a850a04 Mon Sep 17 00:00:00 2001 From: QQ littleice <1767523953@qq.com> Date: Thu, 10 Mar 2022 22:48:40 +0800 Subject: [PATCH] simplify OldNotificationWeather.kt --- .../app/systemui/OldNotificationWeather.kt | 109 +++++------------- 1 file changed, 28 insertions(+), 81 deletions(-) diff --git a/app/src/main/java/com/lt2333/simplicitytools/hook/app/systemui/OldNotificationWeather.kt b/app/src/main/java/com/lt2333/simplicitytools/hook/app/systemui/OldNotificationWeather.kt index ab0d49af..22c7f0f5 100644 --- a/app/src/main/java/com/lt2333/simplicitytools/hook/app/systemui/OldNotificationWeather.kt +++ b/app/src/main/java/com/lt2333/simplicitytools/hook/app/systemui/OldNotificationWeather.kt @@ -6,108 +6,55 @@ import android.view.View import android.view.ViewGroup import android.widget.TextView 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 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 class OldNotificationWeather : IXposedHookLoadPackage { override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam) { + hasEnable("notification_weather") { + var mWeatherView: TextView? = null + 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 - var mWeatherView: TextView? = null + layoutParam.setObjectField("engToStart", context.resources.getIdentifier("notification_shade_shortcut", "id", context.packageName)) + layoutParam.setObjectField("topToTop", context.resources.getIdentifier("notification_shade_shortcut", "id", context.packageName)) + layoutParam.setObjectField("bottomToBottom", context.resources.getIdentifier("notification_shade_shortcut", "id", context.packageName)) - if (!XSPUtils.getBoolean("notification_weather", false)) return - - val isDisplayCity = XSPUtils.getBoolean("notification_weather_city", false) - - val classIfExists = XposedHelpers.findClassIfExists( - "com.android.systemui.qs.MiuiQSHeaderView", - lpparam.classLoader - ) - - XposedHelpers.findAndHookMethod(classIfExists, "onFinishInflate", object : XC_MethodHook() { - override fun afterHookedMethod(param: MethodHookParam) { - val viewGroup = param.thisObject as ViewGroup - - 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 + mWeatherView = WeatherView(context, isDisplayCity).apply { + setTextAppearance(context.resources.getIdentifier("TextAppearance.StatusBar.Expanded.Clock.QuickSettingDate", "style", context.packageName)) + layoutParams = layoutParam } viewGroup.addView(mWeatherView) (mWeatherView as WeatherView).setOnClickListener { try { - val intent = Intent() - intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK - intent.component = ComponentName( - "com.miui.weather2", - "com.miui.weather2.ActivityWeatherMain" - ) - viewGroup.context.startActivity(intent) + 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(viewGroup.context, "启动失败,可能是不支持", Toast.LENGTH_LONG).show() + Toast.makeText(context, "启动失败,可能是不支持", Toast.LENGTH_LONG).show() } } } - }) - //解决横屏重叠 - XposedHelpers.findAndHookMethod(classIfExists, "updateLayout", object : XC_MethodHook() { - override fun afterHookedMethod(param: MethodHookParam) { - super.afterHookedMethod(param) - val mOrientation = - XposedHelpers.getObjectField(param.thisObject, "mOrientation") as Int - if (mOrientation == 1) { + //解决横屏重叠 + "com.android.systemui.qs.MiuiQSHeaderView".hookAfterMethod(lpparam.classLoader, "updateLayout") { + val mOritation = it.thisObject.getObjectField("mOrientation") as Int + if (mOritation == 1) { mWeatherView!!.visibility = View.VISIBLE } else { mWeatherView!!.visibility = View.GONE } } - }) + } } }