Code Monkey home page Code Monkey logo

convenience's Introduction

Convenience

At a Glance

Convenience is a set of tools for making permissions management in Android significantly easier.

How to Get Started

Add jitpack.io repository to your project:

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

Then add Convenience to dependencies list:

dependencies {
    implementation 'com.github.igormatyushkin014:Convenience:2.0.1'
}

Requirements

  • Android SDK 23 and later
  • Android Studio 3.0 and later
  • Java 7 and later

Usage

Permission Manager

All operations with permissions are done with PermissionManager instance which is available via:

PermissionManager permissionManager = Convenience.getPermissions();

Preparing Activity

Before using Convenience for permission request, you have to override activity's onRequestPermissionsResult method:

@Override
public void onRequestPermissionsResult(
    int requestCode,
    @NonNull String[] permissions,
    @NonNull int[] grantResults
) {
    boolean handledByConvenience = Convenience.getPermissions().onRequestPermissionsResult(
        requestCode,
        permissions,
        grantResults
    );

    if (!handledByConvenience) {
        super.onRequestPermissionsResult(
            requestCode,
            permissions,
            grantResults
        );
    }
}

Check Permissions

String[] permissions = new String[] {
    Manifest.permission.CAMERA,
    Manifest.permission.WRITE_EXTERNAL_STORAGE
};

Convenience.getPermissions().check(
    permissions,
    activity,
    new PermissionManager.OnRequestListener() {
        @Override
        public void onResult(CheckResult checkResult) {
            if (requestResult.hasBlockedPermissions()) {
                // Handle blocked permissions
            } else {
                // All permissions available
            }
        }
    }
);

CheckResult instance includes both allowed and blocked permissions:

String[] allowedPermissions = checkResult.getAllowedPermissions();
String[] blockedPermissions = checkResult.getBlockedPermissions();

Request Permissions

Ask user to accept permissions:

Convenience.getPermissions().request(
    permissions,
    activity,
    new PermissionManager.OnRequestListener() {
        @Override
        public void onResult(RequestResult requestResult) {
            if (requestResult.hasBlockedPermissions()) {
                // Handle blocked permissions
            } else {
                // All permissions available
            }
        }
    }
);

Also, you can request only those permissions from the list that are currently blocked. It means that, if the permissions list includes accepted permissions, they will not be requested again. User will be asked to accept other permissions that were not accepted before:

Convenience.getPermissions().requestIfNeeded(
    permissions,
    activity,
    new PermissionManager.OnRequestListener() {
        @Override
        public void onResult(RequestResult requestResult) {
            if (requestResult.hasBlockedPermissions()) {
                // Handle blocked permissions
            } else {
                // All permissions available
            }
        }
    }
);

RequestResult instance includes both allowed and blocked permissions:

String[] allowedPermissions = requestResult.getAllowedPermissions();
String[] blockedPermissions = requestResult.getBlockedPermissions();

License

Convenience is available under the Apache 2.0 license. See the LICENSE file for more info.

convenience's People

Stargazers

Julia Loseva 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.