Oh Boy, I got the key to treasure 😉
Note: Check IMPORTANT NOTE at the end of blog 😁
After Run time permissions has launched in Marshmallow, I have suffered so much managing the whole flow manually, Everything was going good until one day I found use case where user permanently denies to provide required permission and My App was messed up.
After I have used library named EasyPermissions ,but in this library also I have to implement all callbacks for optimal control as suggested on that library page
public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// Forward results to EasyPermissions
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
@Override
public void onPermissionsGranted(int requestCode, List list) {
// Some permissions have been granted
// ...
}
@Override
public void onPermissionsDenied(int requestCode, List list) {
// Some permissions have been denied
// ...
}
}</code
So Is it easy? Ahmm… Not so easy for me at least..
My favorite pick of this year BTW!
And Then, I got the magic stick QuickPermissions
Yes and I can assure you that it is Quick as the name says…
I know this guy who also written another amazing library K4Kotlin and I am big fan of him.
This Library is a best example of minimalism and I have checked internal code and I was quite impressed that he has managed almost every possible scenarios.
As he already said on github page [And also I am so lazy so I will surely Copy+Paste many things]
Let the library do all the hard stuff
Add a single annotation, and you are all done. Library will manage everything, will ask for permissions, will show rationale dialog if denied and also ask to user to allow permissions from settings if user has permanently denied some permission(s) required by the method.
@WithPermissions(
permissions = {Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION}
)
public void methodWithPermissions() {
Toast.makeText(this, "Permissions granted", Toast.LENGTH_SHORT).show();
//
// Do the stuff with permissions safely
//
}
And this is all you need to do..
Optionally, you can provide custom messages that will be shown when rationale dialog is shown, or permanently denied dialog is shown. You can pass rationaleMessage
and/or permanentlyDeniedMessage
to override default message.
@WithPermissions(
permissions = {Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION},
rationaleMessage = "Custom rational message",
permanentlyDeniedMessage = "Custom permanently denied message"
)
public void methodWithPermissions() {
Toast.makeText(this, "Permissions granted", Toast.LENGTH_SHORT).show();
//
// Do the stuff with permissions safely
//
}
So you got 4 little annotation that won’t make pain in the neck.
@WithPermissions : Ask for all necessary permissions with this
@OnShowRationale : While showing rationale dialogue
@OnPermissionsPermanentlyDenied : On permission permanently denied
@OnPermissionsDenied : On permission denied
Wohhhoo!!
Under the hood [As far as I understood]
It uses AOP (Aspect Oriented Programming) under to hood to hook into the methods and hold its execution until permissions are granted. It checks for the permissions first, if granted, executes the method, else adds a headless fragment and asks for the permissions. Shows rationale dialog if required and at the end, if permissions are granted, that method will continue its execution normally. You don’t have to do anything to check for the permissions and mange the callbacks.
If you want to know more about AOP on Android, follow this link.
IMPORTANT NOTE
As this library faced issue with increased build time, Watch Issue, A newer implementation of this library for Kotlin language is available now. And even more amazing thing is now syntactic sugar of kotlin language is now coming with this version.
Example for asking permission.
Awesome Right? It’s literally that simple. Please check Readme for more information. It is damn fast!!