commit b5a3b1317232e845a0fa0f43a05d04736efd9cb5
parent 5d35550cc5bff7320f11253259e1378b22fa9f2b
Author: rhunk <101876869+rhunk@users.noreply.github.com>
Date:   Sun, 17 Mar 2024 16:55:21 +0100

refactor: activity result event

Diffstat:
Mcore/src/main/kotlin/me/rhunk/snapenhance/core/event/EventDispatcher.kt | 27+++++++++++++++++++++++++++
Acore/src/main/kotlin/me/rhunk/snapenhance/core/event/events/impl/ActivityResultEvent.kt | 13+++++++++++++
Mcore/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/experiments/AccountSwitcher.kt | 66++++++++++++++++++++++++++++++------------------------------------
3 files changed, 70 insertions(+), 36 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 @@ -1,5 +1,6 @@ package me.rhunk.snapenhance.core.event +import android.app.Activity import android.content.Intent import android.view.View import android.view.ViewGroup @@ -222,6 +223,32 @@ class EventDispatcher( } } + arrayOf( + "com.snap.mushroom.MainActivity", + "com.snap.identity.loginsignup.ui.LoginSignupActivity" + ).forEach { + context.androidContext.classLoader.loadClass(it).hook("onActivityResult", HookStage.BEFORE) { param -> + val instance = param.thisObject<Activity>() + val requestCode = param.arg<Int>(0) + val resultCode = param.arg<Int>(1) + val intent = param.arg<Intent>(2) + + context.event.post( + ActivityResultEvent( + activity = instance, + requestCode = requestCode, + resultCode = resultCode, + intent = intent + ).apply { + adapter = param + } + ) { + if (canceled) param.setResult(null) + postHookEvent() + } + } + } + hookViewBinder() } } \ No newline at end of file diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/core/event/events/impl/ActivityResultEvent.kt b/core/src/main/kotlin/me/rhunk/snapenhance/core/event/events/impl/ActivityResultEvent.kt @@ -0,0 +1,12 @@ +package me.rhunk.snapenhance.core.event.events.impl + +import android.app.Activity +import android.content.Intent +import me.rhunk.snapenhance.core.event.events.AbstractHookEvent + +class ActivityResultEvent( + val activity: Activity, + val requestCode: Int, + val resultCode: Int, + val intent: Intent +): AbstractHookEvent()+ \ No newline at end of file diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/experiments/AccountSwitcher.kt b/core/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/experiments/AccountSwitcher.kt @@ -32,6 +32,7 @@ import me.rhunk.snapenhance.common.data.FileType import me.rhunk.snapenhance.common.ui.AppMaterialTheme import me.rhunk.snapenhance.common.ui.createComposeAlertDialog import me.rhunk.snapenhance.common.util.snap.MediaDownloaderHelper +import me.rhunk.snapenhance.core.event.events.impl.ActivityResultEvent import me.rhunk.snapenhance.core.event.events.impl.AddViewEvent import me.rhunk.snapenhance.core.features.Feature import me.rhunk.snapenhance.core.features.FeatureLoadParams @@ -425,29 +426,46 @@ class AccountSwitcher: Feature("Account Switcher", loadParams = FeatureLoadParam mainDbShmFile?.delete() } - private fun hookActivityResult(activityClass: Class<*>) { - activityClass.hook("onActivityResult", HookStage.BEFORE) { param -> - val requestCode = param.arg<Int>(0) - val resultCode = param.arg<Int>(1) - val intent = param.arg<Intent>(2) + override fun onActivityCreate() { + if (context.config.experimental.accountSwitcher.globalState != true) return + + activity = context.mainActivity!! + val hovaHeaderSearchIcon = activity!!.resources.getId("hova_header_search_icon") - if (importRequestCode == requestCode) { + context.event.subscribe(AddViewEvent::class) { event -> + if (event.view.id != hovaHeaderSearchIcon) return@subscribe + + event.view.setOnLongClickListener { + activity!!.vibrateLongPress() + showManagementPopup() + false + } + } + } + + @SuppressLint("SetTextI18n") + override fun init() { + if (context.config.experimental.accountSwitcher.globalState != true) return + + context.event.subscribe(ActivityResultEvent::class) { event -> + if (importRequestCode == event.requestCode) { importRequestCode = null - if (resultCode != Activity.RESULT_OK) return@hook - val uri = intent.data ?: return@hook + if (event.resultCode != Activity.RESULT_OK) return@subscribe + event.canceled = true + val uri = event.intent.data ?: return@subscribe context.coroutineScope.launch { importAccount(uri) } } - if (exportCallback?.first == requestCode) { + if (exportCallback?.first == event.requestCode) { val userId = exportCallback?.second exportCallback = null - param.setResult(null) - if (resultCode != Activity.RESULT_OK) return@hook + event.canceled = true + if (event.resultCode != Activity.RESULT_OK) return@subscribe context.coroutineScope.launch { runCatching { - intent.data?.let { uri -> + event.intent.data?.let { uri -> val accountDataPfd = context.bridgeClient.getAccountStorage().getAccountData(userId) ?: throw Exception("Account data not found") context.androidContext.contentResolver.openOutputStream(uri)?.use { outputStream -> ParcelFileDescriptor.AutoCloseInputStream(accountDataPfd).use { @@ -463,29 +481,6 @@ class AccountSwitcher: Feature("Account Switcher", loadParams = FeatureLoadParam } } } - } - - override fun onActivityCreate() { - if (context.config.experimental.accountSwitcher.globalState != true) return - - activity = context.mainActivity!! - hookActivityResult(activity!!::class.java) - val hovaHeaderSearchIcon = activity!!.resources.getId("hova_header_search_icon") - - context.event.subscribe(AddViewEvent::class) { event -> - if (event.view.id != hovaHeaderSearchIcon) return@subscribe - - event.view.setOnLongClickListener { - activity!!.vibrateLongPress() - showManagementPopup() - false - } - } - } - - @SuppressLint("SetTextI18n") - override fun init() { - if (context.config.experimental.accountSwitcher.globalState != true) return findClass("com.snap.identity.service.ForcedLogoutBroadcastReceiver").hook("onReceive", HookStage.BEFORE) { param -> val intent = param.arg<Intent>(1) @@ -508,7 +503,6 @@ class AccountSwitcher: Feature("Account Switcher", loadParams = FeatureLoadParam } findClass("com.snap.identity.loginsignup.ui.LoginSignupActivity").apply { - hookActivityResult(this) hook("onCreate", HookStage.AFTER) { param -> activity = param.thisObject() activity!!.findViewById<FrameLayout>(android.R.id.content).addView(Button(activity).apply {