Skip to content

Commit

Permalink
fix(core): SuspendLocationUpdates
Browse files Browse the repository at this point in the history
  • Loading branch information
rhunk committed Dec 29, 2023
1 parent d7d3834 commit ffd42de
Showing 1 changed file with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,47 @@ import me.rhunk.snapenhance.core.ui.ViewAppearanceHelper
import me.rhunk.snapenhance.core.util.hook.HookStage
import me.rhunk.snapenhance.core.util.hook.hook
import me.rhunk.snapenhance.core.util.ktx.getId
import java.util.WeakHashMap

//TODO: bridge shared preferences
class SuspendLocationUpdates : BridgeFileFeature(
"Suspend Location Updates",
loadParams = FeatureLoadParams.INIT_SYNC or FeatureLoadParams.ACTIVITY_CREATE_SYNC,
bridgeFileType = BridgeFileType.SUSPEND_LOCATION_STATE
) {
private val streamSendHandlerInstanceMap = WeakHashMap<Any, () -> Unit>()
private val isEnabled get() = context.config.global.suspendLocationUpdates.get()

override fun init() {
if (!isEnabled) return
reload()

context.classCache.unifiedGrpcService.hook("bidiStreamingCall", HookStage.BEFORE) { param ->
val uri = param.arg<String>(0)
if (uri == "/snapchat.valis.Valis/Communicate" && exists("true")) {
param.setResult(null)
findClass("com.snapchat.client.grpc.ClientStreamSendHandler\$CppProxy").hook("send", HookStage.BEFORE) { param ->
if (param.nullableThisObject<Any>() !in streamSendHandlerInstanceMap) return@hook
if (!exists("true")) return@hook
param.setResult(null)
}

context.classCache.unifiedGrpcService.apply {
hook("unaryCall", HookStage.BEFORE) { param ->
val uri = param.arg<String>(0)
if (exists("true") && uri == "/snapchat.valis.Valis/SendClientUpdate") {
param.setResult(null)
}
}

hook("bidiStreamingCall", HookStage.AFTER) { param ->
val uri = param.arg<String>(0)
if (uri != "/snapchat.valis.Valis/Communicate") return@hook
param.getResult()?.let { instance ->
streamSendHandlerInstanceMap[instance] = {
runCatching {
instance::class.java.methods.first { it.name == "closeStream" }.invoke(instance)
}.onFailure {
context.log.error("Failed to close stream send handler instance", it)
}
}
}
}
}
}
Expand All @@ -47,6 +72,12 @@ class SuspendLocationUpdates : BridgeFileFeature(
ViewGroup.LayoutParams.WRAP_CONTENT
)
setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
streamSendHandlerInstanceMap.entries.removeIf { (_, closeStream) ->
closeStream()
true
}
}
setState("true", isChecked)
}
})
Expand Down

0 comments on commit ffd42de

Please sign in to comment.