Skip to content

Commit

Permalink
Fix crash when opening in-app Camera for the very first time (#6139)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rohit9625 authored Jan 17, 2025
1 parent 2d6583f commit 9f1fe87
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions app/src/main/java/fr/free/nrw/commons/utils/PermissionUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import com.karumi.dexter.listener.PermissionRequest
import com.karumi.dexter.listener.multi.MultiplePermissionsListener
import fr.free.nrw.commons.R
import fr.free.nrw.commons.upload.UploadActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


object PermissionUtils {
Expand Down Expand Up @@ -130,7 +133,7 @@ object PermissionUtils {
vararg permissions: String
) {
if (hasPartialAccess(activity)) {
Thread(onPermissionGranted).start()
CoroutineScope(Dispatchers.Main).launch { onPermissionGranted.run() }
return
}
checkPermissionsAndPerformAction(
Expand Down Expand Up @@ -166,13 +169,15 @@ object PermissionUtils {
rationaleMessage: Int,
vararg permissions: String
) {
val scope = CoroutineScope(Dispatchers.Main)

Dexter.withActivity(activity)
.withPermissions(*permissions)
.withListener(object : MultiplePermissionsListener {
override fun onPermissionsChecked(report: MultiplePermissionsReport) {
when {
report.areAllPermissionsGranted() || hasPartialAccess(activity) ->
Thread(onPermissionGranted).start()
scope.launch { onPermissionGranted.run() }
report.isAnyPermissionPermanentlyDenied -> {
DialogUtil.showAlertDialog(
activity,
Expand All @@ -189,7 +194,7 @@ object PermissionUtils {
null, null
)
}
else -> Thread(onPermissionDenied).start()
else -> scope.launch { onPermissionDenied?.run() }
}
}

Expand Down

0 comments on commit 9f1fe87

Please sign in to comment.