Skip to content

Commit d87df66

Browse files
authored
Simplify screenshot trigger example and move to its own activity (#4252)
1 parent b5bab24 commit d87df66

File tree

11 files changed

+127
-201
lines changed

11 files changed

+127
-201
lines changed

firebase-appdistribution/test-app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
</intent-filter>
1919
</activity>
2020
<activity android:name="com.googletest.firebase.appdistribution.testapp.SecondActivity" />
21+
<activity android:name="com.googletest.firebase.appdistribution.testapp.ScreenshotDetectionActivity" />
2122
<activity android:name="com.googletest.firebase.appdistribution.testapp.TakeScreenshotAndTriggerFeedbackActivity" />
2223
</application>
2324
</manifest>

firebase-appdistribution/test-app/src/main/java/com/googletest/firebase/appdistribution/testapp/AppDistroTestApplication.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class AppDistroTestApplication : Application() {
77
super.onCreate()
88

99
// Perform any required trigger initialization here
10-
ScreenshotDetectionFeedbackTrigger.initialize(this, R.string.termsAndConditions)
1110
CustomNotificationFeedbackTrigger.initialize(this);
1211

1312
// Default feedback triggers can optionally be enabled application-wide here

firebase-appdistribution/test-app/src/main/java/com/googletest/firebase/appdistribution/testapp/CustomNotificationFeedbackTrigger.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ class TakeScreenshotAndTriggerFeedbackActivity : Activity() {
209209
override fun onResume() {
210210
super.onResume()
211211
val screenshotUri = Uri.fromFile(getFileStreamPath(SCREENSHOT_FILE_NAME))
212-
Firebase.appDistribution.startFeedback(R.string.termsAndConditions, screenshotUri)
212+
Firebase.appDistribution.startFeedback(R.string.feedbackInfoText, screenshotUri)
213213
finish()
214214
}
215215

firebase-appdistribution/test-app/src/main/java/com/googletest/firebase/appdistribution/testapp/MainActivity.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class MainActivity : AppCompatActivity() {
9393
disableAllFeedbackTriggers()
9494
Log.i(TAG, "Enabling notification trigger (SDK)")
9595
firebaseAppDistribution.showFeedbackNotification(
96-
R.string.termsAndConditions, InterruptionLevel.HIGH)
96+
R.string.feedbackInfoText, InterruptionLevel.HIGH)
9797
}
9898
FeedbackTrigger.CUSTOM_NOTIFICATION.label -> {
9999
disableAllFeedbackTriggers()
@@ -107,8 +107,10 @@ class MainActivity : AppCompatActivity() {
107107
}
108108
FeedbackTrigger.SCREENSHOT.label -> {
109109
disableAllFeedbackTriggers()
110-
Log.i(TAG, "Enabling screenshot detection trigger")
111-
ScreenshotDetectionFeedbackTrigger.enable()
110+
startActivity(Intent(this, ScreenshotDetectionActivity::class.java))
111+
// Set the selection back to None since once we're back to this activity the
112+
// trigger will be disabled
113+
autoCompleteTextView.setText(FeedbackTrigger.NONE.label, false)
112114
}
113115
}
114116
}
@@ -124,7 +126,6 @@ class MainActivity : AppCompatActivity() {
124126
firebaseAppDistribution.cancelFeedbackNotification()
125127
CustomNotificationFeedbackTrigger.disable()
126128
ShakeForFeedback.disable(application)
127-
ScreenshotDetectionFeedbackTrigger.disable()
128129
}
129130

130131
override fun onCreateOptionsMenu(menu: Menu): Boolean {
@@ -136,7 +137,7 @@ class MainActivity : AppCompatActivity() {
136137
override fun onOptionsItemSelected(item: MenuItem): Boolean {
137138
return when (item.itemId) {
138139
R.id.startFeedbackMenuItem -> {
139-
Firebase.appDistribution.startFeedback(R.string.termsAndConditions)
140+
Firebase.appDistribution.startFeedback(R.string.feedbackInfoText)
140141
true
141142
}
142143
else -> super.onOptionsItemSelected(item)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.googletest.firebase.appdistribution.testapp
2+
3+
import android.app.AlertDialog
4+
import android.os.Bundle
5+
import android.os.Handler
6+
import android.os.HandlerThread
7+
import android.util.Log
8+
import androidx.activity.result.contract.ActivityResultContracts
9+
import androidx.appcompat.app.AppCompatActivity
10+
import com.googletest.firebase.appdistribution.testapp.ScreenshotDetectionFeedbackTrigger.Companion.screenshotReadPermission
11+
import java.util.*
12+
13+
class ScreenshotDetectionActivity : AppCompatActivity() {
14+
private val handlerThread = HandlerThread("AppDistroFeedbackTrigger").also { it.start() }
15+
private var screenshotDetectionFeedbackTrigger =
16+
ScreenshotDetectionFeedbackTrigger(
17+
this,
18+
R.string.feedbackInfoText,
19+
Handler(handlerThread.looper)
20+
)
21+
22+
override fun onCreate(savedInstanceState: Bundle?) {
23+
super.onCreate(savedInstanceState)
24+
setContentView(R.layout.activity_screenshot_detection)
25+
26+
val launcher =
27+
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean ->
28+
if (!isGranted) {
29+
Log.i(TAG, "Permission not granted")
30+
AlertDialog.Builder(this)
31+
.setMessage(
32+
"Because storage permission has been denied, screenshots taken will not trigger feedback to the developer."
33+
)
34+
.setPositiveButton("OK") { _, _ -> }
35+
.show()
36+
}
37+
}
38+
39+
if (shouldShowRequestPermissionRationale(screenshotReadPermission)) {
40+
Log.i(TAG, "Showing customer rationale for requesting permission.")
41+
AlertDialog.Builder(this)
42+
.setMessage(
43+
"Taking a screenshot of the app can initiate feedback to the developer. To enable this feature, allow the app access to device storage."
44+
)
45+
.setPositiveButton("OK") { _, _ ->
46+
Log.i(TAG, "Launching request for permission.")
47+
launcher.launch(screenshotReadPermission)
48+
}
49+
.setNegativeButton("No thanks") { _, _ -> Log.i(TAG, "User denied permission request.") }
50+
.show()
51+
} else {
52+
Log.i(TAG, "Launching request for permission without rationale.")
53+
launcher.launch(screenshotReadPermission)
54+
}
55+
56+
screenshotDetectionFeedbackTrigger.listenForScreenshots()
57+
}
58+
59+
override fun onDestroy() {
60+
screenshotDetectionFeedbackTrigger.stopListeningForScreenshots()
61+
super.onDestroy()
62+
}
63+
64+
companion object {
65+
private const val TAG = "ScreenshotDetectionActivity"
66+
}
67+
}

0 commit comments

Comments
 (0)