mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-28 13:23:58 +01:00
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.
This commit is contained in:
parent
2d6583fea6
commit
557b76da67
1 changed files with 7 additions and 2 deletions
|
|
@ -16,6 +16,9 @@ import com.karumi.dexter.listener.PermissionRequest
|
||||||
import com.karumi.dexter.listener.multi.MultiplePermissionsListener
|
import com.karumi.dexter.listener.multi.MultiplePermissionsListener
|
||||||
import fr.free.nrw.commons.R
|
import fr.free.nrw.commons.R
|
||||||
import fr.free.nrw.commons.upload.UploadActivity
|
import fr.free.nrw.commons.upload.UploadActivity
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
|
|
||||||
object PermissionUtils {
|
object PermissionUtils {
|
||||||
|
|
@ -166,13 +169,15 @@ object PermissionUtils {
|
||||||
rationaleMessage: Int,
|
rationaleMessage: Int,
|
||||||
vararg permissions: String
|
vararg permissions: String
|
||||||
) {
|
) {
|
||||||
|
val scope = CoroutineScope(Dispatchers.Main)
|
||||||
|
|
||||||
Dexter.withActivity(activity)
|
Dexter.withActivity(activity)
|
||||||
.withPermissions(*permissions)
|
.withPermissions(*permissions)
|
||||||
.withListener(object : MultiplePermissionsListener {
|
.withListener(object : MultiplePermissionsListener {
|
||||||
override fun onPermissionsChecked(report: MultiplePermissionsReport) {
|
override fun onPermissionsChecked(report: MultiplePermissionsReport) {
|
||||||
when {
|
when {
|
||||||
report.areAllPermissionsGranted() || hasPartialAccess(activity) ->
|
report.areAllPermissionsGranted() || hasPartialAccess(activity) ->
|
||||||
Thread(onPermissionGranted).start()
|
scope.launch { onPermissionGranted.run() }
|
||||||
report.isAnyPermissionPermanentlyDenied -> {
|
report.isAnyPermissionPermanentlyDenied -> {
|
||||||
DialogUtil.showAlertDialog(
|
DialogUtil.showAlertDialog(
|
||||||
activity,
|
activity,
|
||||||
|
|
@ -189,7 +194,7 @@ object PermissionUtils {
|
||||||
null, null
|
null, null
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
else -> Thread(onPermissionDenied).start()
|
else -> scope.launch { onPermissionDenied?.run() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue