diff --git a/app/src/main/java/com/lt2333/simplicitytools/hook/app/systemui/ControlCenterWeather.kt b/app/src/main/java/com/lt2333/simplicitytools/hook/app/systemui/ControlCenterWeather.kt index e5dc8bf3..b102bb80 100644 --- a/app/src/main/java/com/lt2333/simplicitytools/hook/app/systemui/ControlCenterWeather.kt +++ b/app/src/main/java/com/lt2333/simplicitytools/hook/app/systemui/ControlCenterWeather.kt @@ -4,8 +4,11 @@ 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.github.kyuubiran.ezxhelper.utils.loadClass import com.lt2333.simplicitytools.util.* import com.lt2333.simplicitytools.view.WeatherView @@ -14,9 +17,9 @@ import de.robv.android.xposed.callbacks.XC_LoadPackage class ControlCenterWeather : IXposedHookLoadPackage { override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam) { - hasEnable("control_center_weather") { + hasEnable("") { var mWeatherView: TextView? = null - val isDisplayCity = XSPUtils.getBoolean("control_center_weather_city", false) + val isDisplayCity = XSPUtils.getBoolean("", false) "com.android.systemui.controlcenter.phone.widget.QSControlCenterHeaderView".hookAfterMethod( lpparam.classLoader, "onFinishInflate" @@ -86,5 +89,183 @@ class ControlCenterWeather : IXposedHookLoadPackage { } } } + hasEnable("control_center_weather") { + var mWeatherView: TextView? = null + var mConstraintLayout: ConstraintLayout? = null + val isDisplayCity = XSPUtils.getBoolean("control_center_weather_city", false) + "com.android.systemui.controlcenter.phone.widget.QSControlCenterHeaderView".hookAfterMethod( + lpparam.classLoader, + "onFinishInflate" + ) { + val viewGroup = it.thisObject as ViewGroup + val context = viewGroup.context + + // MIUI编译时间大于 2022-03-12 00:00:00 + if (SystemProperties.get(context, "ro.build.date.utc")!!.toInt() >= 1647014400) { + //获取原组件 + val big_time_ID = + context.resources.getIdentifier("big_time", "id", context.packageName) + val big_time: TextView = viewGroup.findViewById(big_time_ID) + + val date_time_ID = + context.resources.getIdentifier("date_time", "id", context.packageName) + val date_time: TextView = viewGroup.findViewById(date_time_ID) + + //创建新布局 + 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 } + + (big_time.parent as ViewGroup).addView(mConstraintLayout, 0) + + + //从原布局中删除组件 + (big_time.parent as ViewGroup).removeView(big_time) + (date_time.parent as ViewGroup).removeView(date_time) + + + //添加组件至新布局 + mConstraintLayout!!.addView(big_time) + mConstraintLayout!!.addView(date_time) + + //组件属性 + + val date_time_LP = ConstraintLayout.LayoutParams( + ConstraintLayout.LayoutParams.WRAP_CONTENT, + ConstraintLayout.LayoutParams.WRAP_CONTENT + ).also { + it.startToEnd = big_time_ID + it.bottomToBottom = 0 + it.marginStart = context.resources.getDimensionPixelSize( + context.resources.getIdentifier( + "notification_panel_time_date_space", + "dimen", + context.packageName + ) + ) + it.bottomMargin = dp2px(context, 5f) + } + date_time.layoutParams = date_time_LP + + + //创建天气组件 + mWeatherView = WeatherView(context, isDisplayCity).apply { + setTextAppearance( + context.resources.getIdentifier( + "TextAppearance.QSControl.Date", + "style", + context.packageName + ) + ) + + } + mConstraintLayout!!.addView(mWeatherView) + + val mWeatherView_LP = ConstraintLayout.LayoutParams( + ConstraintLayout.LayoutParams.WRAP_CONTENT, + ConstraintLayout.LayoutParams.WRAP_CONTENT + ).also { + it.startToEnd = big_time_ID + it.bottomToTop = date_time_ID + it.marginStart = context.resources.getDimensionPixelSize( + context.resources.getIdentifier( + "notification_panel_time_date_space", + "dimen", + context.packageName + ) + ) + } + + (mWeatherView as WeatherView).layoutParams = mWeatherView_LP + + } else { + 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 + layoutParam.setObjectField( + "bottomToTop", + context.resources.getIdentifier("date_time", "id", context.packageName) + ) + layoutParam.setObjectField( + "startToEnd", + 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() + } + } + + } + //解决横屏重叠 + "com.android.systemui.controlcenter.phone.widget.QSControlCenterHeaderView".hookAfterMethod( + lpparam.classLoader, + "updateLayout" + ) { + val viewGroup = it.thisObject as ViewGroup + val context = viewGroup.context + val mOrientation = viewGroup.getObjectField("mOrientation") as Int + // MIUI编译时间大于 2022-03-12 00:00:00 + if (SystemProperties.get(context, "ro.build.date.utc")!!.toInt() >= 1647014400) { + 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 + } + } + } + } } } diff --git a/app/src/main/java/com/lt2333/simplicitytools/hook/app/systemui/NotificationWeather.kt b/app/src/main/java/com/lt2333/simplicitytools/hook/app/systemui/NotificationWeather.kt index 9b585f12..db9b5eb3 100644 --- a/app/src/main/java/com/lt2333/simplicitytools/hook/app/systemui/NotificationWeather.kt +++ b/app/src/main/java/com/lt2333/simplicitytools/hook/app/systemui/NotificationWeather.kt @@ -4,8 +4,11 @@ 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.github.kyuubiran.ezxhelper.utils.loadClass import com.lt2333.simplicitytools.util.* import com.lt2333.simplicitytools.view.WeatherView @@ -13,41 +16,184 @@ import de.robv.android.xposed.IXposedHookLoadPackage import de.robv.android.xposed.callbacks.XC_LoadPackage class NotificationWeather : IXposedHookLoadPackage { + + override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam) { hasEnable("notification_weather") { var mWeatherView: TextView? = null + var mConstraintLayout: ConstraintLayout? = null val isDisplayCity = XSPUtils.getBoolean("notification_weather_city", false) - "com.android.systemui.qs.MiuiNotificationHeaderView".hookAfterMethod(lpparam.classLoader, "onFinishInflate") { + "com.android.systemui.qs.MiuiNotificationHeaderView".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 - layoutParam.setObjectField("bottomToTop", context.resources.getIdentifier("date_time", "id", context.packageName)) - layoutParam.setObjectField("startToEnd", 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 + + // MIUI编译时间大于 2022-03-12 00:00:00 + if (SystemProperties.get(context, "ro.build.date.utc")!!.toInt() >= 1647014400) { + //获取原组件 + val big_time_ID = + context.resources.getIdentifier("big_time", "id", context.packageName) + val big_time: TextView = viewGroup.findViewById(big_time_ID) + + val date_time_ID = + context.resources.getIdentifier("date_time", "id", context.packageName) + val date_time: TextView = viewGroup.findViewById(date_time_ID) + + //创建新布局 + 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 } + + (big_time.parent as ViewGroup).addView(mConstraintLayout, 0) + + + //从原布局中删除组件 + (big_time.parent as ViewGroup).removeView(big_time) + (date_time.parent as ViewGroup).removeView(date_time) + + + //添加组件至新布局 + mConstraintLayout!!.addView(big_time) + mConstraintLayout!!.addView(date_time) + + //组件属性 + + val date_time_LP = ConstraintLayout.LayoutParams( + ConstraintLayout.LayoutParams.WRAP_CONTENT, + ConstraintLayout.LayoutParams.WRAP_CONTENT + ).also { + it.startToEnd = big_time_ID + it.bottomToBottom = 0 + it.marginStart = context.resources.getDimensionPixelSize( + context.resources.getIdentifier( + "notification_panel_time_date_space", + "dimen", + context.packageName + ) + ) + it.bottomMargin = dp2px(context, 5f) + } + date_time.layoutParams = date_time_LP + + + //创建天气组件 + mWeatherView = WeatherView(context, isDisplayCity).apply { + setTextAppearance( + context.resources.getIdentifier( + "TextAppearance.QSControl.Date", + "style", + context.packageName + ) + ) + + } + mConstraintLayout!!.addView(mWeatherView) + + val mWeatherView_LP = ConstraintLayout.LayoutParams( + ConstraintLayout.LayoutParams.WRAP_CONTENT, + ConstraintLayout.LayoutParams.WRAP_CONTENT + ).also { + it.startToEnd = big_time_ID + it.bottomToTop = date_time_ID + it.marginStart = context.resources.getDimensionPixelSize( + context.resources.getIdentifier( + "notification_panel_time_date_space", + "dimen", + context.packageName + ) + ) + } + + (mWeatherView as WeatherView).layoutParams = mWeatherView_LP + + } else { + 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 + layoutParam.setObjectField( + "bottomToTop", + context.resources.getIdentifier("date_time", "id", context.packageName) + ) + layoutParam.setObjectField( + "startToEnd", + 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) } - 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") + component = ComponentName( + "com.miui.weather2", + "com.miui.weather2.ActivityWeatherMain" + ) } context.startActivity(intent) } catch (e: Exception) { Toast.makeText(context, "启动失败", Toast.LENGTH_LONG).show() } } + } //解决横屏重叠 - "com.android.systemui.qs.MiuiNotificationHeaderView".hookAfterMethod(lpparam.classLoader, "updateLayout") { - val mOrientation = it.thisObject.getObjectField("mOrientation") as Int - if (mOrientation == 1) { - mWeatherView!!.visibility = View.VISIBLE + "com.android.systemui.qs.MiuiNotificationHeaderView".hookAfterMethod( + lpparam.classLoader, + "updateLayout" + ) { + val viewGroup = it.thisObject as ViewGroup + val context = viewGroup.context + val mOrientation = viewGroup.getObjectField("mOrientation") as Int + // MIUI编译时间大于 2022-03-12 00:00:00 + if (SystemProperties.get(context, "ro.build.date.utc")!!.toInt() >= 1647014400) { + if (mOrientation == 1) { + mConstraintLayout!!.visibility = View.VISIBLE + } else { + mConstraintLayout!!.visibility = View.GONE + } } else { - mWeatherView!!.visibility = View.GONE + if (mOrientation == 1) { + mWeatherView!!.visibility = View.VISIBLE + } else { + mWeatherView!!.visibility = View.GONE + } } } } diff --git a/app/src/main/java/com/lt2333/simplicitytools/util/SystemProperties.kt b/app/src/main/java/com/lt2333/simplicitytools/util/SystemProperties.kt new file mode 100644 index 00000000..da6aca44 --- /dev/null +++ b/app/src/main/java/com/lt2333/simplicitytools/util/SystemProperties.kt @@ -0,0 +1,26 @@ +package com.lt2333.simplicitytools.util + +import android.content.Context + +object SystemProperties { + operator fun get(context: Context, key: String?): String? { + var ret = "" + try { + val cl = context.classLoader + val SystemProperties = cl.loadClass("android.os.SystemProperties") + //参数类型 + val paramTypes: Array?> = arrayOfNulls(1) + paramTypes[0] = String::class.java + val get = SystemProperties.getMethod("get", *paramTypes) + //参数 + val params = arrayOfNulls(1) + params[0] = key + ret = get.invoke(SystemProperties, *params) as String + } catch (iAE: IllegalArgumentException) { + throw iAE + } catch (e: Exception) { + ret = "" + } + return ret + } +} \ No newline at end of file