Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a crash which happens when user shakes the phone #ANDROID-15102 #43

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions library/src/enabled/java/com/telefonica/tweaks/Tweaks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import android.os.Build
import android.os.VibrationEffect
import android.os.Vibrator
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.DisposableEffect
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.core.content.ContextCompat
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
Expand All @@ -28,6 +29,7 @@ import com.telefonica.tweaks.domain.TweaksBusinessLogic
import com.telefonica.tweaks.domain.TweaksGraph
import com.telefonica.tweaks.ui.TweaksCategoryScreen
import com.telefonica.tweaks.ui.TweaksScreen
import com.telefonica.tweaks.utils.onStart
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.map
Expand Down Expand Up @@ -94,14 +96,18 @@ open class Tweaks : TweaksContract {
@Composable
fun NavController.navigateToTweaksOnShake() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feature depends on a NavController, although in some cases we might not have a NavController.

In B2P we have this launched effect that does not depend on it, and also allows a custom action to launch the tweaks:

https://github.com/Telefonica/b2p-germany-android/blob/4a9f90436777833358e891f4864d75ad9308309e/app/src/debug/java/com/telefonica/germany/b2p/core/tweaks/ShakeDetectorWrapper.kt#L18

I think this approach would be very helpful to open tweaks from the command line into a different activity (same as SmartWifi does)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created another function which does not depends on navcontroller

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! Thanks!

val context = LocalContext.current
val lifeCycleOwner = LocalLifecycleOwner.current
val sensorManager: SensorManager =
context.getSystemService(Context.SENSOR_SERVICE) as SensorManager
LaunchedEffect(true) {
val shakeDetector = ShakeDetector {
vibrateIfAble(context)
navigate(TWEAKS_NAVIGATION_ENTRYPOINT)

DisposableEffect(true) {
this.onStart(lifeCycleOwner) {
val shakeDetector = ShakeDetector {
vibrateIfAble(context)
navigate(TWEAKS_NAVIGATION_ENTRYPOINT)
}
shakeDetector.start(sensorManager, SENSOR_DELAY_NORMAL)
}
shakeDetector.start(sensorManager, SENSOR_DELAY_NORMAL)
}
}

Expand Down Expand Up @@ -171,4 +177,4 @@ fun NavGraphBuilder.addTweakGraph(
}
}

private fun TweakCategory.navigationRoute(): String = "${this.title.replace(" ", "")}-tweak-screen"
private fun TweakCategory.navigationRoute(): String = "${this.title.replace(" ", "")}-tweak-screen"
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.telefonica.tweaks.utils

import androidx.compose.runtime.DisposableEffectResult
import androidx.compose.runtime.DisposableEffectScope
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner


fun DisposableEffectScope.onStart(
lifeCycleOwner: LifecycleOwner,
onStart: () -> Unit
): DisposableEffectResult {
val observer = LifecycleEventObserver { _, event ->
if (event == Lifecycle.Event.ON_START) {
onStart()
}
}

lifeCycleOwner.lifecycle.addObserver(observer)

return onDispose {
lifeCycleOwner.lifecycle.removeObserver(observer)
}
}
Loading