Skip to content

Add shake for feedback to test app #4138

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

Merged
merged 1 commit into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppDistributionTestAppSample">
android:theme="@style/Theme.AppDistributionTestAppSample"
android:name="com.googletest.firebase.appdistribution.testapp.AppDistroTestApplication">
<activity android:name="com.googletest.firebase.appdistribution.testapp.MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.googletest.firebase.appdistribution.testapp

import android.app.Application

class AppDistroTestApplication : Application() {
override fun onCreate() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Unrelated to this PR, but should we move more initialization code from MainActiviry.onCreate() to here?

Also, I'm curious: Would this also work if we added it to MainActivity.onCreate()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unrelated to this PR, but should we move more initialization code from MainActiviry.onCreate() to here?

Yes I think so. I'm interested in trying to make the screenshot trigger work this way: you enable it in the Application class, and then it listens to lifecycle callbacks and attached to each Activity as it starts.

Also, I'm curious: Would this also work if we added it to MainActivity.onCreate()?

If we just picked this up and moved it there, then you run the risk of calling enable() multiple times if there are other activities in the app. I believe that would register multiple lifecycle callbacks on the Application. We could prevent that from happening by only allowing it to be enabled once. But I think it makes sense to initialize this in the Application since it enables application-wide functionality.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, makes sense. Let's explore moving the initialization code here in a separate PR once this one is merged.

Also, I think we should restore the button to launch the second activity so we can test those scenarios. Maybe you can do that when adding the radio buttons.

super.onCreate()
ShakeForFeedback.enable(this)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.googletest.firebase.appdistribution.testapp

import android.app.Activity
import android.app.Application
import android.hardware.SensorManager
import android.os.Bundle
import android.util.Log
import com.google.firebase.appdistribution.ktx.appDistribution
import com.google.firebase.ktx.Firebase
import com.squareup.seismic.ShakeDetector

class ShakeForFeedback private constructor() : ShakeDetector.Listener,
Application.ActivityLifecycleCallbacks {
private val shakeDetector = ShakeDetector(this)

override fun hearShake() {
Log.i(TAG, "Shake detected")
Firebase.appDistribution.startFeedback(R.string.terms_and_conditions)
}

override fun onActivityResumed(activity: Activity) {
Log.i(TAG, "Shake detection started")
val sensorManager = activity.getSystemService(Activity.SENSOR_SERVICE) as SensorManager
shakeDetector.start(sensorManager, SensorManager.SENSOR_DELAY_NORMAL)
}

override fun onActivityPaused(activity: Activity) {
Log.i(TAG, "Shake detection stopped")
shakeDetector.stop()
}

// Other lifecycle methods
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {}
override fun onActivityStarted(activity: Activity) {}
override fun onActivityStopped(activity: Activity) {}
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
override fun onActivityDestroyed(activity: Activity) {}

companion object {
const val TAG: String = "ShakeForFeedback"

fun enable(application: Application) {
application.registerActivityLifecycleCallbacks(ShakeForFeedback())
Log.i(TAG, "Shake detector registered")
}
}
}
3 changes: 3 additions & 0 deletions firebase-appdistribution/test-app/test-app.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

testImplementation 'junit:junit:4.+'

// Shake detection
implementation 'com.squareup:seismic:1.0.3'
}

// This allows the app to connect to Firebase on the CI.
Expand Down