Code Monkey home page Code Monkey logo

ezpermission's Introduction

EzPermission

Go straight to Changelog.

Handling dynamic permissions on Android is a major PITA, mainly due to the braindead API design.

This library is yet another helper that makes permission handling easier.

It's based on state machine that handles the user flow. Pass a code that requires a permission as a callback and invoke it via EzPermission.call() helper. The helper will handle permission flow and call intended code when permission is available.

State machine diagram

Features

  1. single-file implementation, if you want to pull it inside your project
  2. based on state machine, so fixing corner cases should be very easy
  3. modal rationale (using dialog)
  4. modeless rationale (being integrated into screen itself)
  5. focused on permission handling logic - doesn't contain any UI code
  6. handles permissions revoked at runtime via settings menu
  7. handles permissions granted at runtime via settings menu

Limitations

This library is focused on permission handling logic and it doesn't contain any UI code.

Versioning scheme

MAJOR . FEATURE . BUGFIX

  • MAJOR - hardwired to 1, as usual in software versioning :)
  • FEATURE - increased for new features
  • BUGFIX - increased for bugfixes only

Integration

Dependencies

Add my private bintray repository in your root build.gradle:

allprojects {
    repositories {
        ...
        maven {
            url "https://dl.bintray.com/ezaquarii/android"
        }
    }
}

In module build.gradle, add a dependency:

compile ('com.ezaquarii:ezpermission:1.2.0') {
    exclude group: 'com.android.support'
}

Modeless

Modeless flow is useful in situations, when we can't construct the screen UI without proper permission. A canonical example of such case is a camera application, where camera preview occupies full screen.

class MainActivity : AppCompatActivity() {

    companion object {
        @JvmField val REQUEST_CAMERA_PERMISSION = 1000;
    }

    private val mInitCamera = EzPermission.of(this, REQUEST_CAMERA_PERMISSION, arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE))
            .onGranted{ onCameraGranted() }
            .onRationale{ onCameraRationale() }
            .onDenied{ onCameraDenied() }
            .onDeniedPermanantly{ onCameraDeniedPermanently() }
            .build()
    
    override fun onStart() {
        super.onStart()
        mInitCamera.call();
    }
    
    ...
    
}

Modal

Modal flow is probably the most popular one. Permission rationale is shown as a dialog (or similar UI widget), that breaks the user flow. Once user accepts the rationale, we launch permission query again.

With EzPermission this is... well... easy.

class MainActivity : AppCompatActivity() {

    companion object {
        @JvmField val REQUEST_AUDIO_PERMISSION = 2000;
    }

    private val mRecordAudio = EzPermission.of(this, REQUEST_AUDIO_PERMISSION, arrayOf(Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE))
            .onGranted{ onAudioGranted() }
            .onRationale{ onAudioRationale() }
            .onDenied{ onAudioPermissionDenied() }
            .onDeniedPermanantly{ onAudioPermissionDeniedPermanently() }
            .build()
            
    // on click handler, attached to some "record" button        
    fun onClickedRecordAudio(v: View) {
        mRecordAudio.call()
    }
    
    // called when permission to use microphone is obtained
    private fun onAudioGranted() {
        toast("Recording audio")
    }
    
    ...

}

What are those callbacks?

Callbacks provided to EzPermission are being invoked by the state machine at appropriate moment. You should consult provided sample to see how to use those callbacks to implement UI changes (reveal rationale, launch alerts, etc).

Credits

  1. PlantUml folks, for excelent, pragmatic UML tool. Your stuff rocks
  2. The Apache Software Foundation for Apache 2.0 Licence
  3. CryoChamber for background noise

Licence

Copyright (C) 2017 EzPermission by Krzysztof Narkiewicz ([email protected])

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.

ezpermission's People

Contributors

ezaquarii avatar

Watchers

 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.