Hi all,
With new versions of Android, the library needs to be adapted to deal with new possibilities and requirements. In this case, the new library JavocSoft Android Toolbox version has resources to handle new Android 6+ Permission system. This will allow us to embrace and adapt our applications to Android 6 new permissions usage approach.
Android 6 and Permissions
Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. If the device is running Android 5.1 (API level 22) or lower, or the app’s targetSdkVersion is 22 or lower, the system asks the user to grant the permissions at install time.
System permissions are divided into two categories, normal and dangerous (more info):
- Normal permissions do not directly risk the user’s privacy. If your app lists a normal permission in its manifest, the system grants the permission automatically. See the list here.
- Dangerous permissions can give the app access to the user’s confidential data. If your app lists a normal permission in its manifest, the system grants the permission automatically. If you list a dangerous permission, the user has to explicitly give approval to your app. If an app requests a dangerous permission listed in its manifest, and the app already has another dangerous permission in the same permission group, the system immediately grants the permission without any interaction with the user.
See Google Developer Permissions to know more about it.
Adapt your application
To adapt an application to the new permissions usage approach of Android 6+, the application has to be able to deal with these use cases:
- Start the App. Ask for permission (showing before a message telling why do you need before ask for them)
- Accept. Ask for permissions:
- Accept -> App runs normally with the service that requires the permissions.
- Not accept -> App runs normally without the service that requires the permissions.
- Cancel. App runs normally without the service that requires the permissions.
- Accept. Ask for permissions:
- With previously granted permissions, start the App:
- Start the App -> App runs normally with the service that requires the permissions.
- In a running App with granted permissions:
- Deny the permissions -> When returning to the App, it Asks for permissions (showing before a message telling why do you need before ask for them):
- Accept. Ask for permissions:
- Accept -> App runs normally with the service that requires the permissions.
- Not accept -> App runs normally without the service that requires the permissions.
- Cancel. App runs normally without the service that requires the permissions.
- Accept. Ask for permissions:
- Deny the permissions -> When returning to the App, it Asks for permissions (showing before a message telling why do you need before ask for them):
- User denies the permission and marks «Never ask again»
- Start the App:
- Show a message to the user about the required permissions (showing before a message telling why do you need before ask for them)
- Accept. Ask for permissions:
- Accept -> App runs normally with the service that requires the permissions.
- Not accept -> App runs normally without the service that requires the permissions.
- Cancel. App runs normally without the service that requires the permissions.
- Accept. Ask for permissions:
- Show a message to the user about the required permissions (showing before a message telling why do you need before ask for them)
- Start the App:
For devices with Android minor than 6 version, API Level 23, you should be able to open the application normally and use it without prompting for any permission because they are already granted when the application is installed.
The library JavocSoft Android Toolbox gives you the method to achieve these point in a relative easy way.
Usage
ToolBox provides methods to check for permissions allowing us to present to the user an informative dialog and also ask to allow them if they are not already granted. ToolBox uses the Android Support Library for backward Android compatibility.
The list of functions to handle permissions are:
- permission_askFor
- permission_checkAskPermissionsresult
- permission_isGranted
- permission_areGranted
ToolBox also provides a set of permissions packages according with Google permissions group. This makes easy for you to ask for permissions for a service that requires a set of permissions.
- PERMISSION_CALENDAR
- PERMISSION_CAMERA
- PERMISSION_LOCATION
- PERMISSION_MICROPHONE
- PERMISSION_PHONE
- PERMISSION_SENSORS
- PERMISSION_SMS
- PERMISSION_STORAGE
Here is an example of usage. In this case we are going to use the localization service.
This service requires two permissions that are not automatically granted (not in Google NORMAL permissions group):
- ACCESS_COARSE_LOCATION
- ACCESS_FINE_LOCATION
ToolBox has these two permissions in the PERMISSION_LOCATION set. We will use this set to ask for permission to the user:
[codesyntax lang=»java»]
<!-- Before use it, we check if the permissions are already granted. --> if(!ToolBox.permission_areGranted(TheActivity.this, ToolBox.PERMISSION_LOCATION.keySet())) { //Permissions not yet granted, we need to be ask. ToolBox.permission_askFor(TheActivity.this, ToolBox.PERMISSION_LOCATION, ToolBox.PERMISSIONS_REQUEST_CODE_ASK_LOCATION, getString(R.string.permissions_required_title), getString(R.string.permissions_button_ok), getString(R.string.permissions_button_deny), getString(R.string.permissions_location_required_text)); }else{ //Permissions are already granted, continue using the localization service... }
[/codesyntax]
To handle the permissions ask response, we must do the following:
[codesyntax lang=»java»]
<!-- This method handles the response --> @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { permissionsLocationGranted = checkAskPermissionsresult(requestCode, permissions, grantResults); Log.i(Constants.TAG, "Location permissions granted? " + permissionsLocationGranted); super.onRequestPermissionsResult(requestCode, permissions, grantResults); if(permissionsLocationGranted) { //Continue with the usage of the service that needs the permissions... }else{ //Do something if required... } } private boolean checkAskPermissionsresult(int requestCode, String[] permissions, int[] grantResults) { boolean res = false; if(requestCode == ToolBox.PERMISSIONS_REQUEST_CODE_ASK_LOCATION) { //We could check the permissions in our system //res = ToolBox.permission_areGranted(SplashActivity.this, Arrays.asList(permissions)); //We check the returned result. Only returns TRUE if all permissions are granted. res = ToolBox.permission_checkAskPermissionsresult(permissions, grantResults); } return res; }
[/codesyntax]
Have in mind that every time you use a service o function that requires permissions, you should check the current permissions by using:
[codesyntax lang=»java»]
ToolBox.permission_areGranted(TheActivity.this, ToolBox.PERMISSION_LOCATION.keySet());
[/codesyntax]
To know more about it goto https://github.com/javocsoft/javocsoft-toolbox/wiki#android-6-permissions-handle.
Recommended videos about permissions in Android M that you should check:
I hope these addition could be useful for your applications.
Do not forget to check the blog to get more updates!
More info and HowTo at https://github.com/javocsoft/javocsoft-toolbox/wiki.
As always, library is available on GitHub
Bye.
JavocSoft 2015.