@@ -31,7 +31,9 @@ private constructor(private val infoTextResourceId: Int, handler: Handler) :
31
31
ContentObserver (handler), Application .ActivityLifecycleCallbacks {
32
32
33
33
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 ()
35
37
private var currentActivity: Activity ? = null
36
38
private var currentUri: Uri ? = null
37
39
private var isEnabled = false
@@ -46,7 +48,7 @@ private constructor(private val infoTextResourceId: Int, handler: Handler) :
46
48
47
49
override fun onActivityCreated (activity : Activity , savedInstanceState : Bundle ? ) {
48
50
if (activity is ActivityResultCaller ) {
49
- requestPermissionLauncher =
51
+ requestPermissionLaunchers[activity] =
50
52
activity.registerForActivityResult(ActivityResultContracts .RequestPermission ()) {
51
53
isGranted: Boolean ->
52
54
if (! isEnabled) {
@@ -62,10 +64,17 @@ private constructor(private val infoTextResourceId: Int, handler: Handler) :
62
64
}
63
65
}
64
66
} 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
+ )
66
71
}
67
72
}
68
73
74
+ override fun onActivityDestroyed (activity : Activity ) {
75
+ requestPermissionLaunchers[activity] = null
76
+ }
77
+
69
78
override fun onActivityResumed (activity : Activity ) {
70
79
currentActivity = activity
71
80
if (isEnabled) {
@@ -85,7 +94,10 @@ private constructor(private val infoTextResourceId: Int, handler: Handler) :
85
94
if (isPermissionGranted(activity)) {
86
95
maybeStartFeedbackForScreenshot(activity, uri)
87
96
} 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
+ )
89
101
} else {
90
102
// Set an in memory flag so we don't ask them again right away
91
103
hasRequestedPermission = true
@@ -95,22 +107,25 @@ private constructor(private val infoTextResourceId: Int, handler: Handler) :
95
107
}
96
108
97
109
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
+ }
114
129
}
115
130
}
116
131
@@ -161,7 +176,6 @@ private constructor(private val infoTextResourceId: Int, handler: Handler) :
161
176
}
162
177
163
178
// Other lifecycle methods
164
- override fun onActivityDestroyed (activity : Activity ) {}
165
179
override fun onActivityStarted (activity : Activity ) {}
166
180
override fun onActivityStopped (activity : Activity ) {}
167
181
override fun onActivitySaveInstanceState (activity : Activity , outState : Bundle ) {}
0 commit comments