duytruongit / PermissionUtils

Check marshmallow permission easily

Home Page:https://rebus007.github.io/PermissionUtils/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Permission Utils

Download API GitHub issues GitHub forks GitHub stars GitHub license Android Arsenal

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:1.0.2'
}

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(requestCode, permissions, grantResults);
}

Now you can ask permission :D

PermissionManager.with(MainActivity.this)
        .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();

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);

.permission(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 two 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

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

App using Permission Utils

If you use this lib [contact me](mailto:raphaelbussa@gmail.com?subject=Header View) and I will add it to the list below:

###Developed By Raphaël Bussa - raphaelbussa@gmail.com

Twitter Google Plus Linkedin

License

The MIT License (MIT)

Copyright (c) 2016 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.

About

Check marshmallow permission easily

https://rebus007.github.io/PermissionUtils/

License:MIT License


Languages

Language:Java 100.0%