Skip to content

Commit 9ba8da9

Browse files
authored
Merge 460ec03 into 6ccfe1e
2 parents 6ccfe1e + 460ec03 commit 9ba8da9

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ object NotificationFeedbackTrigger : Application.ActivityLifecycleCallbacks {
3535
private var isEnabled = false
3636
private var hasRequestedPermission = false
3737
private var currentActivity: Activity? = null
38-
private var requestPermissionLauncher: ActivityResultLauncher<String>? = null
38+
// TODO(lkellogg): this is getting too complex - simplify it by, for example, only enabling this
39+
// on a per-activity basis instead of app-wide
40+
private var requestPermissionLaunchers: MutableMap<Activity, ActivityResultLauncher<String>?> = mutableMapOf()
3941

4042
fun initialize(application: Application) {
4143
// Create the NotificationChannel, but only on API 26+ because
@@ -108,7 +110,7 @@ object NotificationFeedbackTrigger : Application.ActivityLifecycleCallbacks {
108110
}
109111

110112
private fun requestPermission(activity: Activity) {
111-
var launcher = requestPermissionLauncher
113+
var launcher = requestPermissionLaunchers[activity]
112114
if (launcher == null) {
113115
Log.i(TAG, "Not requesting permission, because of inability to register for result.")
114116
} else {
@@ -155,14 +157,14 @@ object NotificationFeedbackTrigger : Application.ActivityLifecycleCallbacks {
155157
override fun onActivityDestroyed(activity: Activity) {
156158
if (activity == currentActivity) {
157159
Log.d(TAG, "clearing current activity")
158-
requestPermissionLauncher = null
159160
currentActivity = null
160161
}
162+
requestPermissionLaunchers[activity] = null
161163
}
162164

163165
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
164166
if (activity is ActivityResultCaller && !hasRequestedPermission) {
165-
requestPermissionLauncher =
167+
requestPermissionLaunchers[activity] =
166168
activity.registerForActivityResult(ActivityResultContracts.RequestPermission()) {
167169
isGranted: Boolean ->
168170
if (!isEnabled) {

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

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ private constructor(private val infoTextResourceId: Int, handler: Handler) :
3131
ContentObserver(handler), Application.ActivityLifecycleCallbacks {
3232

3333
private val seenImages = HashSet<Uri>()
34-
private var requestPermissionLauncher: ActivityResultLauncher<String>? = null
34+
// TODO(lkellogg): this is getting too complex - simplify it by, for example, only enabling this
35+
// on a per-activity basis instead of app-wide
36+
private var requestPermissionLaunchers: MutableMap<Activity, ActivityResultLauncher<String>?> = mutableMapOf()
3537
private var currentActivity: Activity? = null
3638
private var currentUri: Uri? = null
3739
private var isEnabled = false
@@ -46,7 +48,7 @@ private constructor(private val infoTextResourceId: Int, handler: Handler) :
4648

4749
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
4850
if (activity is ActivityResultCaller) {
49-
requestPermissionLauncher =
51+
requestPermissionLaunchers[activity] =
5052
activity.registerForActivityResult(ActivityResultContracts.RequestPermission()) {
5153
isGranted: Boolean ->
5254
if (!isEnabled) {
@@ -62,10 +64,17 @@ private constructor(private val infoTextResourceId: Int, handler: Handler) :
6264
}
6365
}
6466
} else {
65-
Log.w(TAG, "Not listening for screenshots because this activity can't register for permission request results: $activity")
67+
Log.w(
68+
TAG,
69+
"Not listening for screenshots because this activity can't register for permission request results: $activity"
70+
)
6671
}
6772
}
6873

74+
override fun onActivityDestroyed(activity: Activity) {
75+
requestPermissionLaunchers[activity] = null
76+
}
77+
6978
override fun onActivityResumed(activity: Activity) {
7079
currentActivity = activity
7180
if (isEnabled) {
@@ -85,7 +94,10 @@ private constructor(private val infoTextResourceId: Int, handler: Handler) :
8594
if (isPermissionGranted(activity)) {
8695
maybeStartFeedbackForScreenshot(activity, uri)
8796
} else if (hasRequestedPermission) {
88-
Log.i(TAG, "We've already request permission. Not requesting again for the life of the activity.")
97+
Log.i(
98+
TAG,
99+
"We've already request permission. Not requesting again for the life of the activity."
100+
)
89101
} else {
90102
// Set an in memory flag so we don't ask them again right away
91103
hasRequestedPermission = true
@@ -95,22 +107,25 @@ private constructor(private val infoTextResourceId: Int, handler: Handler) :
95107
}
96108

97109
private fun requestReadPermission(activity: Activity, uri: Uri) {
98-
if (activity.shouldShowRequestPermissionRationale(permissionToRequest)) {
99-
Log.i(TAG, "Showing customer rationale for requesting permission.")
100-
AlertDialog.Builder(activity)
101-
.setMessage(
102-
"Taking a screenshot of the app can initiate feedback to the developer. To enable this feature, allow the app access to device storage."
103-
)
104-
.setPositiveButton("OK") { _, _ ->
105-
Log.i(TAG, "Launching request for permission.")
106-
currentUri = uri
107-
requestPermissionLauncher!!.launch(permissionToRequest)
108-
}
109-
.show()
110-
} else {
111-
Log.i(TAG, "Launching request for permission without rationale.")
112-
currentUri = uri
113-
requestPermissionLauncher!!.launch(permissionToRequest)
110+
var launcher = requestPermissionLaunchers[activity]
111+
if (launcher != null) {
112+
if (activity.shouldShowRequestPermissionRationale(permissionToRequest)) {
113+
Log.i(TAG, "Showing customer rationale for requesting permission.")
114+
AlertDialog.Builder(activity)
115+
.setMessage(
116+
"Taking a screenshot of the app can initiate feedback to the developer. To enable this feature, allow the app access to device storage."
117+
)
118+
.setPositiveButton("OK") { _, _ ->
119+
Log.i(TAG, "Launching request for permission.")
120+
currentUri = uri
121+
launcher.launch(permissionToRequest)
122+
}
123+
.show()
124+
} else {
125+
Log.i(TAG, "Launching request for permission without rationale.")
126+
currentUri = uri
127+
launcher.launch(permissionToRequest)
128+
}
114129
}
115130
}
116131

@@ -161,7 +176,6 @@ private constructor(private val infoTextResourceId: Int, handler: Handler) :
161176
}
162177

163178
// Other lifecycle methods
164-
override fun onActivityDestroyed(activity: Activity) {}
165179
override fun onActivityStarted(activity: Activity) {}
166180
override fun onActivityStopped(activity: Activity) {}
167181
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}

0 commit comments

Comments
 (0)