commit 40c26627397488cf4ccc4cb4830606ffa3cfc903
parent 1f54a6295f160c0288b713e6b25656484073c438
Author: rhunk <101876869+rhunk@users.noreply.github.com>
Date: Tue, 16 Apr 2024 22:57:49 +0200
fix: suspend location updates switch
Diffstat:
5 files changed, 55 insertions(+), 14 deletions(-)
diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/core/event/EventDispatcher.kt b/core/src/main/kotlin/me/rhunk/snapenhance/core/event/EventDispatcher.kt
@@ -2,6 +2,7 @@ package me.rhunk.snapenhance.core.event
import android.app.Activity
import android.content.Intent
+import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams
@@ -132,6 +133,30 @@ class EventDispatcher(
}
}
+ LayoutInflater::class.java.getMethod(
+ "inflate",
+ Int::class.java,
+ ViewGroup::class.java,
+ Boolean::class.javaPrimitiveType
+ ).hook(HookStage.AFTER) { param ->
+ val layoutId = param.argNullable<Int>(0) ?: return@hook
+ val parent = param.argNullable<ViewGroup>(1)
+ val result = param.getResult() as? View
+
+ context.event.post(
+ LayoutInflateEvent(
+ layoutId = layoutId,
+ parent = parent,
+ view = result
+ ).apply {
+ adapter = param
+ }
+ ) {
+ if (canceled) param.setResult(null)
+ postHookEvent()
+ }
+ }
+
context.classCache.networkApi.hook("submit", HookStage.BEFORE) { param ->
val request = param.arg<Any>(0)
diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/core/event/events/impl/LayoutInflateEvent.kt b/core/src/main/kotlin/me/rhunk/snapenhance/core/event/events/impl/LayoutInflateEvent.kt
@@ -0,0 +1,11 @@
+package me.rhunk.snapenhance.core.event.events.impl
+
+import android.view.View
+import android.view.ViewGroup
+import me.rhunk.snapenhance.core.event.events.AbstractHookEvent
+
+class LayoutInflateEvent(
+ val layoutId: Int,
+ val parent: ViewGroup?,
+ val view: View?
+) : AbstractHookEvent()+
\ No newline at end of file
diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/global/SuspendLocationUpdates.kt b/core/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/global/SuspendLocationUpdates.kt
@@ -3,11 +3,12 @@ package me.rhunk.snapenhance.core.features.impl.global
import android.view.ViewGroup
import android.widget.Switch
import me.rhunk.snapenhance.common.bridge.types.BridgeFileType
-import me.rhunk.snapenhance.core.event.events.impl.AddViewEvent
+import me.rhunk.snapenhance.core.event.events.impl.LayoutInflateEvent
import me.rhunk.snapenhance.core.features.BridgeFileFeature
import me.rhunk.snapenhance.core.features.FeatureLoadParams
import me.rhunk.snapenhance.core.ui.ViewAppearanceHelper
import me.rhunk.snapenhance.core.util.ktx.getId
+import me.rhunk.snapenhance.core.util.ktx.getLayoutId
class SuspendLocationUpdates : BridgeFileFeature(
"Suspend Location Updates",
@@ -19,12 +20,15 @@ class SuspendLocationUpdates : BridgeFileFeature(
if (context.config.global.betterLocation.takeIf { it.globalState == true }?.suspendLocationUpdates?.get() != true) return
reload()
- val locationSharingSettingsContainerId = context.resources.getId("location_sharing_settings_container")
+ val locationSharingSettingsContainerId = context.resources.getLayoutId("v3_screen_location_sharing_settings")
val recyclerViewContainerId = context.resources.getId("recycler_view_container")
- context.event.subscribe(AddViewEvent::class) { event ->
- if (event.parent.id == locationSharingSettingsContainerId && event.view.id == recyclerViewContainerId) {
- (event.view as ViewGroup).addView(Switch(event.view.context).apply {
+ context.event.subscribe(LayoutInflateEvent::class) { event ->
+ if (event.layoutId != locationSharingSettingsContainerId) return@subscribe
+ val viewGroup = event.view as? ViewGroup ?: return@subscribe
+ viewGroup.post {
+ val container = viewGroup.findViewById<ViewGroup>(recyclerViewContainerId)
+ container.addView(Switch(event.view.context).apply {
isChecked = isSuspended()
ViewAppearanceHelper.applyTheme(this)
text = this@SuspendLocationUpdates.context.translation["suspend_location_updates.switch_text"]
diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/ui/UITweaks.kt b/core/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/ui/UITweaks.kt
@@ -2,13 +2,13 @@ package me.rhunk.snapenhance.core.features.impl.ui
import android.content.res.Resources
import android.util.Size
-import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup.MarginLayoutParams
import android.widget.FrameLayout
import me.rhunk.snapenhance.common.util.ktx.findFieldsToString
import me.rhunk.snapenhance.core.event.events.impl.AddViewEvent
import me.rhunk.snapenhance.core.event.events.impl.BindViewEvent
+import me.rhunk.snapenhance.core.event.events.impl.LayoutInflateEvent
import me.rhunk.snapenhance.core.features.Feature
import me.rhunk.snapenhance.core.features.FeatureLoadParams
import me.rhunk.snapenhance.core.util.hook.HookStage
@@ -72,12 +72,9 @@ class UITweaks : Feature("UITweaks", loadParams = FeatureLoadParams.ACTIVITY_CRE
}
}
- LayoutInflater::class.java.hook("inflate", HookStage.AFTER) { param ->
- val id = param.args().firstOrNull() as? Int ?: return@hook
- val result = param.getResult() as? View ?: return@hook
-
- if (id == getId("chat_input_bar_sharing_drawer_button", "layout") && hiddenElements.contains("hide_live_location_share_button")) {
- hideView(result)
+ context.event.subscribe(LayoutInflateEvent::class) { event ->
+ if (event.layoutId == getId("chat_input_bar_sharing_drawer_button", "layout") && hiddenElements.contains("hide_live_location_share_button")) {
+ hideView(event.view ?: return@subscribe)
}
}
diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/core/util/ktx/AndroidExt.kt b/core/src/main/kotlin/me/rhunk/snapenhance/core/util/ktx/AndroidExt.kt
@@ -22,9 +22,12 @@ fun Resources.getIdentifier(name: String, type: String): Int {
return getIdentifier(name, type, Constants.SNAPCHAT_PACKAGE_NAME)
}
-@SuppressLint("DiscouragedApi")
fun Resources.getId(name: String): Int {
- return getIdentifier(name, "id", Constants.SNAPCHAT_PACKAGE_NAME)
+ return getIdentifier(name, "id")
+}
+
+fun Resources.getLayoutId(name: String): Int {
+ return getIdentifier(name, "layout")
}
fun Resources.getDimens(name: String): Int {