Skip to content

Commit 9f1fe87

Browse files
authored
Fix crash when opening in-app Camera for the very first time (commons-app#6139)
* fix: correctly handle permission callbacks on Main thread The PermissionUtils was incorrectly executing permission callbacks on a background thread, leading to a Handler error. Now, it is using Main dispatcher. * fix crash when opening camera while having partial storage access
1 parent 2d6583f commit 9f1fe87

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

app/src/main/java/fr/free/nrw/commons/utils/PermissionUtils.kt

+8-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import com.karumi.dexter.listener.PermissionRequest
1616
import com.karumi.dexter.listener.multi.MultiplePermissionsListener
1717
import fr.free.nrw.commons.R
1818
import fr.free.nrw.commons.upload.UploadActivity
19+
import kotlinx.coroutines.CoroutineScope
20+
import kotlinx.coroutines.Dispatchers
21+
import kotlinx.coroutines.launch
1922

2023

2124
object PermissionUtils {
@@ -130,7 +133,7 @@ object PermissionUtils {
130133
vararg permissions: String
131134
) {
132135
if (hasPartialAccess(activity)) {
133-
Thread(onPermissionGranted).start()
136+
CoroutineScope(Dispatchers.Main).launch { onPermissionGranted.run() }
134137
return
135138
}
136139
checkPermissionsAndPerformAction(
@@ -166,13 +169,15 @@ object PermissionUtils {
166169
rationaleMessage: Int,
167170
vararg permissions: String
168171
) {
172+
val scope = CoroutineScope(Dispatchers.Main)
173+
169174
Dexter.withActivity(activity)
170175
.withPermissions(*permissions)
171176
.withListener(object : MultiplePermissionsListener {
172177
override fun onPermissionsChecked(report: MultiplePermissionsReport) {
173178
when {
174179
report.areAllPermissionsGranted() || hasPartialAccess(activity) ->
175-
Thread(onPermissionGranted).start()
180+
scope.launch { onPermissionGranted.run() }
176181
report.isAnyPermissionPermanentlyDenied -> {
177182
DialogUtil.showAlertDialog(
178183
activity,
@@ -189,7 +194,7 @@ object PermissionUtils {
189194
null, null
190195
)
191196
}
192-
else -> Thread(onPermissionDenied).start()
197+
else -> scope.launch { onPermissionDenied?.run() }
193198
}
194199
}
195200

0 commit comments

Comments
 (0)