Permission Utils

Additional

Language
Java
Version
3.0.0 (Sep 21, 2018)
Created
Jul 15, 2016
Updated
Feb 14, 2021 (Retired)
Owner
Raphaël Bussa (raphaelbussa)
Contributors
Bernat Borrás Paronella (alorma)
Raphaël Bussa (raphaelbussa)
2
Activity
Badge
Generate
Download
Source code

Permission Utils

Check marshmallow permission easily

Import

At the moment the library is in my personal maven repo

repositories {
    maven {
        url 'http://dl.bintray.com/raphaelbussa/maven'
    }
}
dependencies {
    compile 'rebus:permission-utils:2.0.7'
}

How to use

Request a permission

First, override onRequestPermissionsResult and call PermissionManager.handleResult(requestCode, permissions, grantResults);

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    PermissionManager.handleResult(this, requestCode, permissions, grantResults);
}

Now you can ask permission :D

PermissionManager.Builder()
        .permission(PermissionEnum.WRITE_EXTERNAL_STORAGE)
        .askAgain(true)
        .askAgainCallback(new AskAgainCallback() {
            @Override
            public void showRequestPermission(UserResponse response) {
                    showDialog(response);
                }
            })
        .callback(new FullCallback() {
            @Override
            public void result(ArrayList<PermissionEnum> permissionsGranted, ArrayList<PermissionEnum> permissionsDenied, ArrayList<PermissionEnum> permissionsDeniedForever, ArrayList<PermissionEnum> permissionsAsked) {
                }
            })
        .ask(this);

or more permission

.permission(PermissionEnum.GET_ACCOUNTS, PermissionEnum.ACCESS_FINE_LOCATION, PermissionEnum.READ_SMS)

or

ArrayList<PermissionEnum> permissionEnumArrayList = new ArrayList<>();
permissionEnumArrayList.add(PermissionEnum.ACCESS_FINE_LOCATION);
permissionEnumArrayList.add(PermissionEnum.GET_ACCOUNTS);
permissionEnumArrayList.add(PermissionEnum.READ_CONTACTS);

.permissions(permissionEnumArrayList)

if you need to simply check a permission, just call utils

PermissionEnum permissionEnum = PermissionEnum.WRITE_EXTERNAL_STORAGE;
boolean granted = PermissionUtils.isGranted(MainActivity.this, PermissionEnum.WRITE_EXTERNAL_STORAGE);
Toast.makeText(MainActivity.this, permissionEnum.toString() + " isGranted [" + granted + "]", Toast.LENGTH_SHORT).show()

Callbacks

You can use three different callback, it depends of your needs.

  • FullCallback: gives you all the information on permission requested by you
  • SimpleCallback: returns a boolean that says if all permission requests were permitted
  • SmartCallback: returns a boolean that says if all permission requests were permitted and a boolean that says if some permissions are denied forever

Little extra

If user answer "Never ask again" to a request for permission, you can redirect user to app settings, with an utils

PermissionUtils.openApplicationSettings(MainActivity.this, R.class.getPackage().getName());

That's all folks!

Sample

Browse the sample code here

Javadoc

Browse Javadoc here

App using Permission Utils

If you use this lib contact me and I will add it to the list below:

Developed By

Raphaël Bussa - raphaelbussa@gmail.com

License

The MIT License (MIT)

Copyright (c) 2017-2019 Raphaël Bussa

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.