Norway


-  - The Android Arsenal – Permissions -  - The Android Arsenal – Permissions

in

Permissions with Kotlin Coroutines

No more callbacks, builders, listeners or verbose code for requesting Android permissions. Request permissions with one function call, thanks to Kotlin Coroutines.


Installation

Add jcenter repository

compile 'com.markodevcic.peko:peko:0.1'

Example in Android Activity:

launch (UI) {
    val permissionResultDeferred = Peko.requestPermissionsAsync(this, Manifest.permission.BLUETOOTH)
    val permissionResult = permissionResultDeferred.await()
    
    if (permissionResult.grantedPermissions.contains(Manifest.permission.BLUETOOTH)) {
        //we have permission
    } else {
        //can't continue
    }
}

If you want to show a permission rationale to the , you can use the built in AlertDialogPermissionRationale. This will show an Alert Dialog with your message and title, explaining to user why this rationale is needed. It will be shown only once and only if user denies the permission for the first .

val rationale = AlertDialogPermissionRationale(this@MainActivity) {
    this.setTitle("Need permissions")
    this.setMessage("Please give permissions to use this feature") 
}

launch (UI) {
    val permissionResult = Peko.requestPermissionsAsync(this, Manifest.permission.BLUETOOTH, rationale = rationale).await()
}

There is also a SnackBarRationale class that shows a SnackBar when permission rationale is required.

val snackBar = Snackbar.make(rootView, "Permissions needed to continue", Snackbar.LENGTH_LONG)
val snackBarRationale = SnackBarRationale(snackBar, "Request again")

launch(UI) {
    val permissionResult = Peko.requestPermissionsAsync(this@MainActivity, *permissions, rationale = snackBarRationale).await()
}

You can also show your own implementation of Permission Rationale to the user. Just implement the interface PermissionRationale. If true is returned from suspend function shouldRequestAfterRationaleShownAsync, Permission Request will be repeated, otherwise the permission request completes and returns the current permission result.

License

Copyright  Marko Devcic

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here