Skip to content

Commit

Permalink
Update permission manager docs
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanWang committed May 23, 2019
1 parent 2051605 commit 8591040
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -43,13 +43,20 @@ internal object PermissionManager {
*/
private val manifestPermission = lazyContext<Set<String>> {
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<out String>,
Expand All @@ -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<out String>) {
permissions.forEach {
if (!manifestPermission(context).contains(it)) {
Expand All @@ -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<out String>, grantResults: IntArray) {
KL.i { "On permission result: pending ${pendingResults.size}" }
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ class PermissionResult(permissions: Array<out String>, val callback: (granted: B
return true
}
permissions.remove(permission)
if (permissions.isNotEmpty()) return false
if (permissions.isNotEmpty()) {
return false
}
callback(true, null)
return true
}
Expand Down

0 comments on commit 8591040

Please sign in to comment.