Android permissions as suspend functions

Overview

Warden

Android permissions as suspend functions

Permission handling in Android can be complicated. It requires boilerplate code to setup the permission request, receive the result and then forward the result to the place of your codebase where the actual handling logic takes place.

Warden removes all this boilerplate code and enables requesting permissions and receiving the result from any coroutine in your codebase (such as your ViewModels).

Quick Start

Install Warden via Gradle by adding the dependency in your app/build.gradle file:

repositories {
  ...
  mavenCentral()
}

dependencies {
    implementation 'com.alexstyl:warden:1.0.0-alpha2'   
}

The following example showcases how to use Warden to request the CALL_PHONE permission.

coroutineScope.launch {
    val result = Warden.with(anyContext).requestPermission(Manifest.permission.CALL_PHONE)
    when (result) {
        is PermissionState.Denied -> dialNumber(phoneNumber)
        PermissionState.Granted -> startCall(phoneNumber)
    }
}

As soon as requestPermission() is called, Warden will ask Android for the requested permission and will suspend the coroutine until the permission dialog has been dismissed.

You can use the requestPermissions() suspend function to request multiple permissions at once.

The Denied state contains a shouldShowRationale property. When true, it is the right time to show an educational message towards why your app requires the said permission.

Limitations

Requesting permissions on the background

The way Warden works is via launching an activity which handles the permission requests for you. This is what enables Warden to request permission and receive the results from any part of your app.

If you need to request permissions while no activity is visible (such as from a service), you might be limited by the restrictions on starting activities from the background.

Orientation changes and process death

Warden assumes that the coroutine context you are requesting the permission from lives long enough until the permission dialog is dismissed. For the cases the coroutine context doesn't exist, no one will be there to receive the result.

Sample app

The repository comes with a sample app for an example of usage.

Getting Help

To report a problem or request a feature, open a new issue on Github.

License

Apache 2.0. See the LICENSE file for details.

Author

Made by Alex Styl. Follow @alexstyl on Twitter for future updates.

You might also like...
OpenPGP for Android

APG (Android Privacy Guard) APG originally brought email encryption to the Android platform. In recent years the project has fallen asleep, but a lot

Analyze any Android/Java based app or game
Analyze any Android/Java based app or game

ClassyShark Introduction ClassyShark is a standalone binary inspection tool for Android developers. It can reliably browse any Android executable and

Android virtual machine and deobfuscator
Android virtual machine and deobfuscator

Simplify Generic Android Deobfuscator Simplify virtually executes an app to understand its behavior and then tries to optimize the code so that it beh

enjarify 8.6 0.0 L5 Python Enjarify is a tool for translating Dalvik bytecode to equivalent Java bytecode. This allows Java analysis tools to analyze Android applications.

Note: This repository may be out of date. Future development will occur at https://github.com/Storyyeller/enjarify. Introduction Enjarify is a tool fo

CLI tool for decompiling Android apps to Java. It does resources! It does Java! Its real easy!

Easy-as-pie Android Decompiler Why One stop shop I got pretty tired of decompiling Android apps with a bunch of steps that I had to remember all the t

A android app for encrypting apk
A android app for encrypting apk

A android app for encrypting apk

Unlock an Android phone (or device) by bruteforcing the lockscreen PIN.
Unlock an Android phone (or device) by bruteforcing the lockscreen PIN.

Unlock an Android phone (or device) by bruteforcing the lockscreen PIN. Turn your Kali Nethunter phone into a bruteforce PIN cracker for Android devices! (no root, no adb)

A Java ePub reader and parser framework for Android.
A Java ePub reader and parser framework for Android.

FolioReader-Android is an EPUB reader written in Java and Kotlin. Features Custom Fonts Custom Text Size Themes / Day mode / Night mode Text Highlight

A program to flip every private, protected and package-private access flag to public in an Android dex file!

DexExposed A program to flip every private, protected and package-private access flag to public in an Android dex file! Building Simply run gradle mak

Releases(1.0.0-alpha2)
Owner
Alex Styl
10 years of Android Development • Left my high paying job to build my own digital products.
Alex Styl
A collection of Kotlin Multiplatform Mobile cryptographic hashing functions.

crypto A collection of Kotlin Multiplatform Mobile libraries to aid in mobile app development. cryptohash: A set of cryptographic (and not so cryptogr

Appmattus Limited 58 Dec 15, 2022
BlackDex is an Android unpack tool, it supports Android 5.0~12 and need not rely to any environment. BlackDex can run on any Android mobile phones or emulators, you can unpack APK File in several seconds.

BlackDex is an Android unpack tool, it supports Android 5.0~12 and need not rely to any environment. BlackDex can run on any Android mobile phones or emulators, you can unpack APK File in several seconds.

null 4.3k Jan 2, 2023
Simple API to perform AES encryption on Android. This is the Android counterpart to the AESCrypt library Ruby and Obj-C (with the same weak security defaults :( ) created by Gurpartap Singh. https://github.com/Gurpartap/aescrypt

AESCrypt-Android Simple API to perform AES encryption on Android with no dependancies. This is the Android counterpart to the AESCrypt library Ruby an

Scott Alexander-Bown 636 Dec 18, 2022
PermissionX is an extension Android library that makes Android runtime permission request extremely easy

PermissionX is an extension Android library that makes Android runtime permission request extremely easy. You can use it for basic pe

Lin Guo 2.9k Dec 28, 2022
Signal Protocol library for Java/Android

Overview A ratcheting forward secrecy protocol that works in synchronous and asynchronous messaging environments. PreKeys This protocol uses a concept

Signal 1.8k Dec 24, 2022
Grab’n Run, a simple and effective Java Library for Android projects to secure dynamic code loading.

Grab’n Run, a simple and effective Java Library for Android projects to secure dynamic code loading.

Luca Falsina 418 Dec 29, 2022
A collection of android security related resources

android-security-awesome A collection of android security related resources. Tools Academic/Research/Publications/Books Exploits/Vulnerabilities/Bugs

Ashish Bhatia 6.6k Jan 5, 2023
a version of the official Android openssl setup to build standalone for use in app

OpenSSL on the Android platform. --- The code in this directory is based on $OPENSSL_VERSION in the file openssl.version. See patches/README for more

Guardian Project 371 Dec 8, 2022
A port of gnupg to Android (UNMAINTAINED!)

Gnu Privacy Guard for Android A port of the whole GnuPG 2.1 suite to Android. If you are using these tools in your own apps, we'd love to hear about i

Guardian Project 282 Jan 7, 2023