Giter Club home page Giter Club logo

android-permission-manager's Introduction

A clean approach to deal with Permissions in Android

This small project shows a clean and simplified approach for the recommended workflow for requesting permissions in Android.

Dependencies

You need to use AndroidX Fragment 1.3.0 or higher.

implementation "androidx.fragment:fragment-ktx:1.3.0-rc01"

How to use

Using it is as simple as registering the PermissionManager with your Fragment and, whenever you want to request permissions, you just have to send what permissions you need, a rationale of why you're requesting those permissions for a second time, and then simply get the results as a callback:

class YourFragment : Fragment() {

    private val permissionManager = PermissionManager.from(this)
    yourView.setOnClickListener {
        permissionManager
            .request(Permission.Camera)
            .rationale("We need permission to use the camera")
            .checkPermission { granted: Boolean ->
                if (granted) {
                    // Do something with the camera
                } else {
                    // You can't access the camera
                }
            }
    }
}

Requesting multiple permissions simultaneously

Option 1: Simply add a new entry to the Permission class with your required permissions:

sealed class Permission(vararg val permissions: String) {
    // Individual permissions
    object Camera : Permission(CAMERA)

    // Bundled permissions for MyFeature
    object MyFeature : Permission(CAMERA, WRITE_EXTERNAL_STORAGE)
}

Option 2: Send as many Permission as you require to the PermissionManager:

permissionManager.request(Permission.Camera, Permission.Storage, Permission.Location)

Checking permission results individually

checkPermission { } will return a Boolean telling you whether all permissions were granted or not. If you want more granular control about which permissions were granted or declined, you can use checkDetailedPermission { } to receive a Map<Permission, Boolean> with the results:

permissionManager
    .request(Permission.Camera, Permission.Storage)
    .rationale("We need two permissions at once!")
    .checkDetailedPermission { result: Map<Permission, Boolean> ->
        if (result.all { it.value }) {
            // We have all the permissions
        } else {
            // Check in result which permission was denied
        }
    }

android-permission-manager's People

Contributors

fmontesino avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.