From 1becdd849424999ea373f796672103fa7c96b925 Mon Sep 17 00:00:00 2001 From: Mama1emon Date: Mon, 23 Dec 2024 10:58:26 +0400 Subject: [PATCH] AND-9558 Fix GP review issue Signed-off-by: Mama1emon --- .../main/java/com/tangem/tap/MainActivity.kt | 44 ++---------- .../tangem/tap/WindowObscurationObserver.kt | 68 +++++++++++++++++++ .../tangem/tap/features/main/MainViewModel.kt | 1 + 3 files changed, 73 insertions(+), 40 deletions(-) create mode 100644 app/src/main/java/com/tangem/tap/WindowObscurationObserver.kt diff --git a/app/src/main/java/com/tangem/tap/MainActivity.kt b/app/src/main/java/com/tangem/tap/MainActivity.kt index b69d782652..a28dccd905 100644 --- a/app/src/main/java/com/tangem/tap/MainActivity.kt +++ b/app/src/main/java/com/tangem/tap/MainActivity.kt @@ -32,12 +32,9 @@ import com.google.android.material.snackbar.Snackbar import com.tangem.common.routing.AppRoute import com.tangem.common.routing.entity.SerializableIntent import com.tangem.core.analytics.api.AnalyticsEventHandler -import com.tangem.core.analytics.models.event.TechAnalyticsEvent -import com.tangem.core.analytics.models.event.TechAnalyticsEvent.WindowObscured.ObscuredState import com.tangem.core.decompose.context.AppComponentContext import com.tangem.core.decompose.di.RootAppComponentContext import com.tangem.core.deeplink.DeepLinksRegistry -import com.tangem.core.navigation.email.EmailSender import com.tangem.core.ui.event.StateEvent import com.tangem.core.ui.extensions.TextReference import com.tangem.core.ui.extensions.resolveReference @@ -224,9 +221,6 @@ class MainActivity : AppCompatActivity(), SnackbarHandler, ActivityResultCallbac private val onActivityResultCallbacks = mutableListOf() - private var isWindowPartiallyObscuredAlreadySent: Boolean = false - private var isWindowFullyObscuredAlreadySent: Boolean = false - override fun onCreate(savedInstanceState: Bundle?) { // We need to call it before onCreate to prevent unnecessary activity recreation installAppTheme() @@ -272,6 +266,8 @@ class MainActivity : AppCompatActivity(), SnackbarHandler, ActivityResultCallbac if (intent != null) { deepLinksRegistry.launch(intent) } + + lifecycle.addObserver(WindowObscurationObserver) } private fun installRouting() { @@ -529,41 +525,9 @@ class MainActivity : AppCompatActivity(), SnackbarHandler, ActivityResultCallbac } override fun dispatchTouchEvent(event: MotionEvent): Boolean { - val isPartiallyObscured = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - event.flags and MotionEvent.FLAG_WINDOW_IS_PARTIALLY_OBSCURED != 0 - } else { - false - } - - if (isPartiallyObscured) { - Timber.d("Window is partially obscured") - - if (!isWindowPartiallyObscuredAlreadySent) { - analyticsEventsHandler.send( - event = TechAnalyticsEvent.WindowObscured(state = ObscuredState.PARTIALLY), - ) - - isWindowPartiallyObscuredAlreadySent = true - } - } - - val isFullyObscured = event.flags and MotionEvent.FLAG_WINDOW_IS_OBSCURED != 0 - - if (isFullyObscured) { - Timber.d("Window is partially or fully obscured") - - if (!isWindowFullyObscuredAlreadySent) { - analyticsEventsHandler.send( - event = TechAnalyticsEvent.WindowObscured(state = ObscuredState.FULLY), - ) - - isWindowFullyObscuredAlreadySent = true - } - - return false - } + val result = WindowObscurationObserver.dispatchTouchEvent(event, analyticsEventsHandler) - return super.dispatchTouchEvent(event) + return if (result) super.dispatchTouchEvent(event) else false } private fun showSnackbar(text: String, length: Int, buttonTitle: String?, action: View.OnClickListener?) { diff --git a/app/src/main/java/com/tangem/tap/WindowObscurationObserver.kt b/app/src/main/java/com/tangem/tap/WindowObscurationObserver.kt new file mode 100644 index 0000000000..9e2545cba6 --- /dev/null +++ b/app/src/main/java/com/tangem/tap/WindowObscurationObserver.kt @@ -0,0 +1,68 @@ +package com.tangem.tap + +import android.os.Build +import android.view.MotionEvent +import androidx.lifecycle.DefaultLifecycleObserver +import androidx.lifecycle.LifecycleOwner +import com.tangem.core.analytics.api.AnalyticsEventHandler +import com.tangem.core.analytics.models.event.TechAnalyticsEvent +import com.tangem.core.analytics.models.event.TechAnalyticsEvent.WindowObscured.ObscuredState +import timber.log.Timber + +internal object WindowObscurationObserver : DefaultLifecycleObserver { + + private var isWindowPartiallyObscuredAlreadySent: Boolean = false + private var isWindowFullyObscuredAlreadySent: Boolean = false + + private var isReadyToProxy = false + + override fun onResume(owner: LifecycleOwner) { + super.onResume(owner) + isReadyToProxy = true + } + + override fun onPause(owner: LifecycleOwner) { + super.onPause(owner) + isReadyToProxy = false + } + + fun dispatchTouchEvent(event: MotionEvent, analyticsEventHandler: AnalyticsEventHandler): Boolean { + if (!isReadyToProxy) return true + + val isPartiallyObscured = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + event.flags and MotionEvent.FLAG_WINDOW_IS_PARTIALLY_OBSCURED != 0 + } else { + false + } + + if (isPartiallyObscured) { + Timber.d("Window is partially obscured") + + if (!isWindowPartiallyObscuredAlreadySent) { + analyticsEventHandler.send( + event = TechAnalyticsEvent.WindowObscured(state = ObscuredState.PARTIALLY), + ) + + isWindowPartiallyObscuredAlreadySent = true + } + } + + val isFullyObscured = event.flags and MotionEvent.FLAG_WINDOW_IS_OBSCURED != 0 + + if (isFullyObscured) { + Timber.d("Window is partially or fully obscured") + + if (!isWindowFullyObscuredAlreadySent) { + analyticsEventHandler.send( + event = TechAnalyticsEvent.WindowObscured(state = ObscuredState.FULLY), + ) + + isWindowFullyObscuredAlreadySent = true + } + + return false + } + + return true + } +} diff --git a/app/src/main/java/com/tangem/tap/features/main/MainViewModel.kt b/app/src/main/java/com/tangem/tap/features/main/MainViewModel.kt index a5178cfcf3..333c2267bc 100644 --- a/app/src/main/java/com/tangem/tap/features/main/MainViewModel.kt +++ b/app/src/main/java/com/tangem/tap/features/main/MainViewModel.kt @@ -212,6 +212,7 @@ internal class MainViewModel @Inject constructor( override fun onDismissBottomSheet() { listenToFlipsUseCase.changeUpdateEnabled(true) + router.pop() stateHolder.updateWithoutModalNotification() stateHolder.updateWithHiddenBalancesToast(true) }