Check marshmallow permission easily
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'
}
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()
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
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());
Browse the sample code here
Browse Javadoc here
If you use this lib contact me and I will add it to the list below:
Raphaël Bussa - raphaelbussa@gmail.com
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.