mirror of
https://github.com/LittleTurtle2333/SimplicityTools.git
synced 2026-07-12 19:31:17 +08:00
增加 新控制中心天气 (不完美) (#180)
This commit is contained in:
@@ -23,6 +23,7 @@ object SystemUI: AppRegister() {
|
||||
RemoveTheLeftSideOfTheLockScreen, //移除锁屏负一屏功能
|
||||
RemoveLockScreenCamera, //移除锁屏相机功能
|
||||
NotificationWeather, //通知面板天气
|
||||
NewNotificationWeather, // 新控制中心天气
|
||||
OldNotificationWeather,
|
||||
ControlCenterWeather, //控制中心天气
|
||||
//StatusBarCurrent, //TODO:状态栏电流
|
||||
|
||||
@@ -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<TextView>("dateView")
|
||||
clockId = dateView.id
|
||||
weather = WeatherData(dateView.context, isDisplayCity)
|
||||
weather.callBacks = {
|
||||
dateView.invokeMethod("updateTime")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
107
app/src/main/java/com/lt2333/simplicitytools/view/WeatherData.kt
Normal file
107
app/src/main/java/com/lt2333/simplicitytools/view/WeatherData.kt
Normal file
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user