From 6a70d0c20f31c761b718509d7793ae018284609a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E5=9D=97=E5=90=9B?= <86393520+577fkj@users.noreply.github.com> Date: Sun, 2 Oct 2022 16:09:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=E6=96=B0=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E4=B8=AD=E5=BF=83=E5=A4=A9=E6=B0=94=20(=E4=B8=8D?= =?UTF-8?q?=E5=AE=8C=E7=BE=8E)=20(#180)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../simplicitytools/hook/app/SystemUI.kt | 1 + .../app/systemui/NewNotificationWeather.kt | 53 +++++++++ .../simplicitytools/view/WeatherData.kt | 107 ++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 app/src/main/java/com/lt2333/simplicitytools/hook/app/systemui/NewNotificationWeather.kt create mode 100644 app/src/main/java/com/lt2333/simplicitytools/view/WeatherData.kt diff --git a/app/src/main/java/com/lt2333/simplicitytools/hook/app/SystemUI.kt b/app/src/main/java/com/lt2333/simplicitytools/hook/app/SystemUI.kt index 97af2c87..bcb4add9 100644 --- a/app/src/main/java/com/lt2333/simplicitytools/hook/app/SystemUI.kt +++ b/app/src/main/java/com/lt2333/simplicitytools/hook/app/SystemUI.kt @@ -23,6 +23,7 @@ object SystemUI: AppRegister() { RemoveTheLeftSideOfTheLockScreen, //移除锁屏负一屏功能 RemoveLockScreenCamera, //移除锁屏相机功能 NotificationWeather, //通知面板天气 + NewNotificationWeather, // 新控制中心天气 OldNotificationWeather, ControlCenterWeather, //控制中心天气 //StatusBarCurrent, //TODO:状态栏电流 diff --git a/app/src/main/java/com/lt2333/simplicitytools/hook/app/systemui/NewNotificationWeather.kt b/app/src/main/java/com/lt2333/simplicitytools/hook/app/systemui/NewNotificationWeather.kt new file mode 100644 index 00000000..4dbbfa8b --- /dev/null +++ b/app/src/main/java/com/lt2333/simplicitytools/hook/app/systemui/NewNotificationWeather.kt @@ -0,0 +1,53 @@ +package com.lt2333.simplicitytools.hook.app.systemui + +import android.annotation.SuppressLint +import android.content.pm.ApplicationInfo +import android.widget.TextView +import com.github.kyuubiran.ezxhelper.utils.* +import com.lt2333.simplicitytools.util.XSPUtils +import com.lt2333.simplicitytools.util.hasEnable +import com.lt2333.simplicitytools.util.xposed.base.HookRegister +import com.lt2333.simplicitytools.view.WeatherData + + +@SuppressLint("StaticFieldLeak") +object NewNotificationWeather : HookRegister() { + lateinit var weather: WeatherData + var clockId: Int = -2 + + @SuppressLint("DiscouragedApi", "ClickableViewAccessibility") + override fun init() = hasEnable("control_center_weather") { + val isDisplayCity = XSPUtils.getBoolean("notification_weather_city", false) + findMethod("com.android.systemui.controlcenter.phone.widget.ControlCenterDateView", findSuper = true) { name == "onDetachedFromWindow" }.hookBefore { + if ((it.thisObject as TextView).id == clockId && this::weather.isInitialized) { + weather.onDetachedFromWindow() + } + } + findMethod("com.android.systemui.controlcenter.phone.widget.ControlCenterDateView", findSuper = true) { name == "setText" }.hookBefore { + val time = it.args[0]?.toString() + val view = it.thisObject as TextView + if (view.id == clockId && time != null && this::weather.isInitialized) { + // TODO("调高天气位置") +// val layout = view.layoutParams as ViewGroup.MarginLayoutParams +// val y = view.height / 2 +// layout.topMargin = -y + it.args[0] = "${weather.weatherData}$time" + } + } + + findMethod("com.android.systemui.shared.plugins.PluginManagerImpl") { name == "getClassLoader" }.hookAfter { getClassLoader -> + val appInfo = getClassLoader.args[0] as ApplicationInfo + val classLoader = getClassLoader.result as ClassLoader + if (appInfo.packageName == "miui.systemui.plugin") { + findMethod("miui.systemui.controlcenter.windowview.MainPanelHeaderController", classLoader = classLoader) { name == "addClockViews" }.hookAfter { + val dateView = it.thisObject.getObjectAs("dateView") + clockId = dateView.id + weather = WeatherData(dateView.context, isDisplayCity) + weather.callBacks = { + dateView.invokeMethod("updateTime") + } + } + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/lt2333/simplicitytools/view/WeatherData.kt b/app/src/main/java/com/lt2333/simplicitytools/view/WeatherData.kt new file mode 100644 index 00000000..f3f18bc4 --- /dev/null +++ b/app/src/main/java/com/lt2333/simplicitytools/view/WeatherData.kt @@ -0,0 +1,107 @@ +package com.lt2333.simplicitytools.view + +import android.annotation.SuppressLint +import android.content.ComponentName +import android.content.Context +import android.content.Intent +import android.database.ContentObserver +import android.net.Uri +import android.os.Handler +import android.os.Looper +import android.os.Message +import android.text.TextUtils +import android.widget.TextClock +import android.widget.TextView +import android.widget.Toast +import com.github.kyuubiran.ezxhelper.utils.* +import de.robv.android.xposed.XposedBridge +import java.util.* + +@SuppressLint("ViewConstructor", "SetTextI18n") +class WeatherData(val context: Context?, private val showCity: Boolean) { + + private val mContext: Context + private val WEATHER_URI = Uri.parse("content://weather/weather") + private val mHandler: Handler + private val mWeatherObserver: ContentObserver? + private val mWeatherRunnable: WeatherRunnable + var weatherData: String = "\n" + var callBacks: () -> Unit = {} + + init { + mHandler = + object : Handler(Looper.getMainLooper()) { + override fun handleMessage(message: Message) { + val str = message.obj as String + weatherData = if (TextUtils.isEmpty(str)) "\n" else "$str\n" + callBacks() + } + } + mWeatherObserver = WeatherContentObserver(mHandler) + mContext = context!! + mWeatherRunnable = WeatherRunnable() + context.contentResolver.registerContentObserver(WEATHER_URI, true, mWeatherObserver) + updateWeatherInfo() + } + + private inner class WeatherContentObserver(handler: Handler?) : ContentObserver(handler) { + override fun onChange(z: Boolean) { + updateWeatherInfo() + } + } + + inner class WeatherRunnable : Runnable { + override fun run() { + var str = "" + try { + val query = mContext.contentResolver.query(WEATHER_URI, null, null, null, null) + if (query != null) { + if (query.moveToFirst()) { + str = if (showCity) { + query.getString(query.getColumnIndexOrThrow("city_name")) + " " + query.getString(query.getColumnIndexOrThrow("description")) + " " + query.getString(query.getColumnIndexOrThrow("temperature")) + } else { + query.getString(query.getColumnIndexOrThrow("description")) + " " + query.getString(query.getColumnIndexOrThrow("temperature")) + } + } + query.close() + } + } catch (e: Exception) { + + } + val obtainMessage2 = mHandler.obtainMessage() + obtainMessage2.what = 100 + obtainMessage2.obj = str + mHandler.sendMessage(obtainMessage2) + } + } + + private fun updateWeatherInfo() { + mHandler.removeCallbacks(mWeatherRunnable) + mHandler.postDelayed(mWeatherRunnable, 200) + } + + fun onDetachedFromWindow() { + if (mWeatherObserver != null) { + mContext.contentResolver.unregisterContentObserver(mWeatherObserver) + } + } + + fun startCalendarApp() { + mContext.classLoader.loadClass("com.miui.systemui.util.CommonUtil").invokeStaticMethod("startCalendarApp", args(context), argTypes(Context::class.java)) + } + + fun startWeatherApp() { + try { + val intent = Intent().apply { + flags = Intent.FLAG_ACTIVITY_NEW_TASK + component = ComponentName( + "com.miui.weather2", + "com.miui.weather2.ActivityWeatherMain" + ) + } + mContext.startActivity(intent) + } catch (e: Exception) { + Toast.makeText(context, "启动失败", Toast.LENGTH_LONG).show() + } + } +} \ No newline at end of file