Coder Social home page Coder Social logo

permissionhelper's Introduction

PermissionHelper

A permission helper library that simplifies asking user for permission in Android M. With a customisable explanation dialog, you can easily explain to the user the permissions you need and why you need it, with buttons for permissions or take users to the app settings page in case the permission was denied.

Playstore Sample

Screenshots can be found on the store.
[PermissionHelper Sample APK] (https://play.google.com/store/apps/details?id=com.ayz4sci.androidfactory.permissionhelpersample)

Version

1.0.0

Usage

To use this library in your android project, just simply add the following repositories and dependency into your build.gradle

dependencies {
    compile 'com.ayz4sci.androidfactory:permissionhelper:1.0.0'
}

Also add this repository because the library is dependent on Nammu permission helper library https://github.com/tajchert/Nammu

repositories {
    maven {
        url "https://jitpack.io"
    }
}

In your main activity init the library as follows:

    PermissionHelper permissionHelper = PermissionHelper.getInstance(this);

Then override the following in your app root activity or the root activity where the permissions will be required:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        permissionHelper.onActivityResult(requestCode, resultCode, data);
    }

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        permissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onDestroy() {
        permissionHelper.finish();
        super.onDestroy();
    }

Now, to perform an action that requires Android permission, call permissionHelper.verifyPermission. It accepts the following parameters:

  • String Array - description of each permission required, this will be displayed to the user in the explanation dialog.
  • String array - an array of Manifest permissions
  • PermissionCallback - you put the actions you want to perform when permission is granted or rejected. See example below:
    permissionHelper.verifyPermission(
        new String[]{"dial this number", "take picture"},
        new String[]{Manifest.permission.CALL_PHONE, Manifest.permission.CAMERA}, 
        new PermissionCallback() {
            @Override
            public void permissionGranted() {
                //action to perform when permission granteed
            }
    
            @Override
            public void permissionRefused() {
                //action to perform when permission refused
            }
        }
    );

To simply verify whether user has granted the permission without showing any explanation dialog or asking for permission:

  • String array - an array of Manifest permissions
  • PermissionCallback - you put the actions you want to perform when permission is granted or rejected. e.g.
    permissionHelper.verifyPermission(
        new String[]{Manifest.permission.CALL_PHONE}, 
        new PermissionCallback() {
            @Override
            public void permissionGranted() {
                //action to perform when permission granted
            }
    
            @Override
            public void permissionRefused() {
                //action to perform when permission refused
            }
        }
    );

Customise Explanation UI

To modify the explanation dialog displayed to the user, use the following getters:

  • customiseUI(int background, Drawable icon) customise the background, set center (app) icon
  • getPermissionDialog() get the permission dialog
  • getCenterIcon() get the center icon (Logo), returns an ImageView
  • getNotAllowButton() get the not allow button
  • getAllowButton() get the allow button
  • getBackButton() get the back button - cancel button

That's all.

Thanks to

License

MIT

permissionhelper's People

Contributors

ayz4sci avatar

Watchers

 avatar  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.