From a9abc039c4684b3923c9162d86551e280ab342da Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 21 May 2019 20:05:25 -0700 Subject: Remove lazy result collector --- core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt index 57a6f42..669f32e 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt @@ -36,7 +36,7 @@ import java.lang.ref.WeakReference internal object PermissionManager { private var requestInProgress = false - private val pendingResults: MutableList> by lazy { mutableListOf>() } + private val pendingResults = mutableListOf>() /** * Retrieve permissions requested in our manifest -- cgit v1.2.3 From b787e49a5f8f97a2b330d4503ceb66056ef9ccd7 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 21 May 2019 20:26:17 -0700 Subject: Remove pending flag --- .../main/kotlin/ca/allanwang/kau/kotlin/Streams.kt | 1 - .../allanwang/kau/permissions/PermissionManager.kt | 34 +++++++++------------- 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/core/src/main/kotlin/ca/allanwang/kau/kotlin/Streams.kt b/core/src/main/kotlin/ca/allanwang/kau/kotlin/Streams.kt index bfa73fd..65d15a7 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kotlin/Streams.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kotlin/Streams.kt @@ -24,7 +24,6 @@ package ca.allanwang.kau.kotlin * Since we don't have access to the internals of our extended class, * We will simply iterate and remove when the filter returns {@code false} */ -@Synchronized inline fun > C.kauRemoveIf(filter: (item: T) -> Boolean): C { val iter = iterator() while (iter.hasNext()) diff --git a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt index 669f32e..769a9fb 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt @@ -19,6 +19,7 @@ import android.app.Activity import android.content.Context import android.content.pm.PackageManager import androidx.core.app.ActivityCompat +import ca.allanwang.kau.kotlin.kauRemoveIf import ca.allanwang.kau.kotlin.lazyContext import ca.allanwang.kau.logging.KL import ca.allanwang.kau.utils.KauException @@ -35,7 +36,6 @@ import java.lang.ref.WeakReference */ internal object PermissionManager { - private var requestInProgress = false private val pendingResults = mutableListOf>() /** @@ -60,13 +60,13 @@ internal object PermissionManager { val missingPermissions = permissions.filter { !context.hasPermission(it) } if (missingPermissions.isEmpty()) return callback(true, null) pendingResults.add(WeakReference(PermissionResult(permissions, callback = callback))) - if (!requestInProgress) { - requestInProgress = true + if (pendingResults.size == 1) { requestPermissions(context, missingPermissions.toTypedArray()) - } else KL.d { "Request is postponed since another one is still in progress; did you remember to override onRequestPermissionsResult?" } + } else { + KL.d { "Request is postponed since another one is still in progress; did you remember to override onRequestPermissionsResult?" } + } } - @Synchronized private fun requestPermissions(context: Context, permissions: Array) { permissions.forEach { if (!manifestPermission(context).contains(it)) { @@ -88,23 +88,15 @@ internal object PermissionManager { fun onRequestPermissionsResult(context: Context, permissions: Array, grantResults: IntArray) { KL.i { "On permission result: pending ${pendingResults.size}" } val count = Math.min(permissions.size, grantResults.size) - val iter = pendingResults.iterator() - while (iter.hasNext()) { - val action = iter.next().get() - if ((0 until count).any { action?.onResult(permissions[it], grantResults[it]) != false }) - iter.remove() + pendingResults.kauRemoveIf { + val action = it.get() + action == null || (0 until count).any { i -> action.onResult(permissions[i], grantResults[i]) } } - if (pendingResults.isEmpty()) - requestInProgress = false - else { - val action = pendingResults.map { it.get() }.firstOrNull { it != null } - if (action == null) { //actions have been unlinked from their weak references - pendingResults.clear() - requestInProgress = false - return - } - requestPermissions(context, action.permissions.toTypedArray()) + val action = pendingResults.asSequence().map { it.get() }.firstOrNull { it != null } + if (action == null) { //actions have been unlinked from their weak references + pendingResults.clear() + return } - KL.i { "Post on permission result: pending ${pendingResults.size}" } + requestPermissions(context, action.permissions.toTypedArray()) } } -- cgit v1.2.3 From f8b58a39ab65a7097f731ab4cf38516d1c57f220 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 21 May 2019 20:29:20 -0700 Subject: Use set for manifest permissions --- .../main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt index 769a9fb..4d97070 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt @@ -41,12 +41,12 @@ internal object PermissionManager { /** * Retrieve permissions requested in our manifest */ - private val manifestPermission = lazyContext> { + private val manifestPermission = lazyContext> { try { - it.packageManager.getPackageInfo(it.packageName, PackageManager.GET_PERMISSIONS)?.requestedPermissions - ?: emptyArray() + it.packageManager.getPackageInfo(it.packageName, PackageManager.GET_PERMISSIONS)?.requestedPermissions?.toSet() + ?: emptySet() } catch (e: Exception) { - emptyArray() + emptySet() } } -- cgit v1.2.3 From 8591040162b6147344aa82a2e4a308319e62c2d4 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Wed, 22 May 2019 18:02:32 -0700 Subject: Update permission manager docs --- .../allanwang/kau/permissions/PermissionManager.kt | 22 ++++++++++++++++------ .../allanwang/kau/permissions/PermissionResult.kt | 4 +++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt index 4d97070..96adfa3 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt @@ -31,8 +31,8 @@ import java.lang.ref.WeakReference /** * Created by Allan Wang on 2017-07-03. * - * Permission manager that is decoupled from activities - * Keeps track of pending requests, and warns about invalid requests + * Permission manager that is decoupled from activities. + * Keeps track of pending requests, and warns about invalid requests. */ internal object PermissionManager { @@ -43,13 +43,20 @@ internal object PermissionManager { */ private val manifestPermission = lazyContext> { try { - it.packageManager.getPackageInfo(it.packageName, PackageManager.GET_PERMISSIONS)?.requestedPermissions?.toSet() + it.packageManager.getPackageInfo( + it.packageName, + PackageManager.GET_PERMISSIONS + )?.requestedPermissions?.toSet() ?: emptySet() } catch (e: Exception) { emptySet() } } + /** + * Registers a new permission request. + * It is expected that the callback will be called eventually, unless the parent activity is destroyed. + */ operator fun invoke( context: Context, permissions: Array, @@ -67,6 +74,9 @@ internal object PermissionManager { } } + /** + * Checks that the listed permissions can be requested, and submits the request to the provided activity + */ private fun requestPermissions(context: Context, permissions: Array) { permissions.forEach { if (!manifestPermission(context).contains(it)) { @@ -82,8 +92,8 @@ internal object PermissionManager { } /** - * Handles permission result by allowing accepted permissions for all pending requests - * Also cleans up destroyed or completed pending requests + * Handles permission result by allowing accepted permissions for all pending requests. + * Also cleans up destroyed or completed pending requests. */ fun onRequestPermissionsResult(context: Context, permissions: Array, grantResults: IntArray) { KL.i { "On permission result: pending ${pendingResults.size}" } @@ -93,7 +103,7 @@ internal object PermissionManager { action == null || (0 until count).any { i -> action.onResult(permissions[i], grantResults[i]) } } val action = pendingResults.asSequence().map { it.get() }.firstOrNull { it != null } - if (action == null) { //actions have been unlinked from their weak references + if (action == null) { // actions have been unlinked from their weak references pendingResults.clear() return } diff --git a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionResult.kt b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionResult.kt index 8888c91..599f6e8 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionResult.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionResult.kt @@ -36,7 +36,9 @@ class PermissionResult(permissions: Array, val callback: (granted: B return true } permissions.remove(permission) - if (permissions.isNotEmpty()) return false + if (permissions.isNotEmpty()) { + return false + } callback(true, null) return true } -- cgit v1.2.3 From efae24a43ba6f861442a485ddff49afd65559eb4 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Wed, 22 May 2019 18:04:26 -0700 Subject: Update changelog --- docs/Changelog.md | 1 + sample/src/main/res/xml/kau_changelog.xml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/Changelog.md b/docs/Changelog.md index 0100e8e..a2c38e1 100644 --- a/docs/Changelog.md +++ b/docs/Changelog.md @@ -2,6 +2,7 @@ ## v4.1.0 * :core: Deprecate NetworkUtils, as the underlying functions are deprecated +* :core: Permission manager no longer synchronized, as all actions should occur in the main thread * :kpref-activity: Getter and setter now have action context, with the option to reload self ## v4.0.0 diff --git a/sample/src/main/res/xml/kau_changelog.xml b/sample/src/main/res/xml/kau_changelog.xml index dbaa1ef..0b13dd4 100644 --- a/sample/src/main/res/xml/kau_changelog.xml +++ b/sample/src/main/res/xml/kau_changelog.xml @@ -8,9 +8,9 @@ + - -- cgit v1.2.3