|
| 1 | +package com.googletest.firebase.appdistribution.testapp |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import android.app.Application |
| 5 | +import android.hardware.SensorManager |
| 6 | +import android.os.Bundle |
| 7 | +import android.util.Log |
| 8 | +import com.google.firebase.appdistribution.ktx.appDistribution |
| 9 | +import com.google.firebase.ktx.Firebase |
| 10 | +import com.squareup.seismic.ShakeDetector |
| 11 | + |
| 12 | +/** |
| 13 | + * Listens to the device accelerometer, and starts App Distribution in-app feedback when the user |
| 14 | + * shakes their device. |
| 15 | + */ |
| 16 | +object ShakeDetectionFeedbackTrigger : ShakeDetector.Listener, Application.ActivityLifecycleCallbacks { |
| 17 | + private const val TAG: String = "ShakeDetectionFeedbackTrigger" |
| 18 | + |
| 19 | + private val shakeDetector = |
| 20 | + ShakeDetector(this).also { it.setSensitivity(ShakeDetector.SENSITIVITY_LIGHT) } |
| 21 | + private var isEnabled = false |
| 22 | + |
| 23 | + /** Start listening for device shakes. */ |
| 24 | + fun enable(application: Application, currentActivity: Activity? = null) { |
| 25 | + synchronized(this) { |
| 26 | + if (!isEnabled) { |
| 27 | + application.registerActivityLifecycleCallbacks(this) |
| 28 | + Log.i(TAG, "Shake detector registered") |
| 29 | + if (currentActivity != null) { |
| 30 | + listenForShakes(currentActivity) |
| 31 | + } |
| 32 | + isEnabled = true |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /** Stop listening for device shakes. */ |
| 38 | + fun disable(application: Application) { |
| 39 | + synchronized(this) { |
| 40 | + if (isEnabled) { |
| 41 | + stopListeningForShakes() |
| 42 | + application.unregisterActivityLifecycleCallbacks(this) |
| 43 | + Log.i(TAG, "Shake detector unregistered") |
| 44 | + isEnabled = false |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private fun listenForShakes(activity: Activity) { |
| 50 | + val sensorManager = activity.getSystemService(Activity.SENSOR_SERVICE) as SensorManager |
| 51 | + shakeDetector.start(sensorManager, SensorManager.SENSOR_DELAY_NORMAL) |
| 52 | + } |
| 53 | + |
| 54 | + private fun stopListeningForShakes() { |
| 55 | + shakeDetector.stop() |
| 56 | + } |
| 57 | + |
| 58 | + override fun hearShake() { |
| 59 | + Log.i(TAG, "Shake detected") |
| 60 | + Firebase.appDistribution.startFeedback(R.string.termsAndConditions) |
| 61 | + } |
| 62 | + |
| 63 | + override fun onActivityResumed(activity: Activity) { |
| 64 | + Log.i(TAG, "Shake detection started") |
| 65 | + listenForShakes(activity) |
| 66 | + } |
| 67 | + |
| 68 | + override fun onActivityPaused(activity: Activity) { |
| 69 | + Log.i(TAG, "Shake detection stopped") |
| 70 | + stopListeningForShakes() |
| 71 | + } |
| 72 | + |
| 73 | + // Other lifecycle methods |
| 74 | + override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {} |
| 75 | + override fun onActivityStarted(activity: Activity) {} |
| 76 | + override fun onActivityStopped(activity: Activity) {} |
| 77 | + override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {} |
| 78 | + override fun onActivityDestroyed(activity: Activity) {} |
| 79 | +} |
0 commit comments