QtAndroidTools: AppPermissions


AppPermissions tool allow to request Android app permissions in an easier way.

QtAndroidTools is a library that allows to simplify access to some native Android features from QML. 



QtAndroidTools library source here. The project provide a demo app showing the use of all features.

From Android version 23 (Marshmallow) there are a list of special permission called "dangerous" because are directly connected to personal data of the user like calendar, contacts list and so on (official documentation here). If your app is target for this version and above and your manifest file contains one of the dangerous permissions you have to explicitly get the user authorization for have such permission granted. In the demo app four of these dangerous permission will be requested as follow:




There are two functions for request permissions, one accept a list of permissions for allow a unique request for all and another can be used to ask for a single permission only. In this example the multiple request function will be used as follow:

import QtAndroidTools 1.0
...
readonly property var permissionsNameList: ["android.permission.WRITE_EXTERNAL_STORAGE","android.permission.READ_CALENDAR","android.permission.READ_PHONE_STATE","android.permission.READ_CONTACTS"]
...
QtAndroidAppPermissions.requestPermissions(permissionsNameList)

The call of  this function will have a result to show the system message informing the user the app require the specific permissions and ask for approval:



Once the user will finish to make the choice a signal will be generated with a list of struct as param as follow:

Connections {
    target: QtAndroidAppPermissions
    onRequestPermissionsResults: {
        for(var i = 0; i < results.length; i++)
        {
            if(results[i].granted === true)
            {
                setPermissionGranted(results[i].name, true);
            }
            else
            {
                if(QtAndroidAppPermissions.shouldShowRequestPermissionInfo(results[i].name) === true)
                {
                    if(results[i].name === permissionsNameList[0])
                        requestPermissionWRITE_EXTERNAL_STORAGE.open();
                    else if(results[i].name === permissionsNameList[1])
                        requestPermissionREAD_CALENDAR.open();
                    else if(results[i].name === permissionsNameList[2])
                        requestPermissionREAD_PHONE_STATE.open();
                    else if(results[i].name === permissionsNameList[3])
                        requestPermissionREAD_CONTACTS.open();
                }
                else
                {
                    setPermissionGranted(results[i].name, false);
                }
            }
        }
    }
}

Each item in the list contain the param name containing the permission string name and the boolean field granted informing if the user allowed the app to get the permission or not. Please note, if you app will be target under version 23 this signal will be emitted immediately without show any messagebox and, obviously, with all the permission automatically granted. This for allow the code working in the same way independently by the Android target version selected. In case the user don't granted a permission this special call is used in the code:

QtAndroidAppPermissions.shouldShowRequestPermissionInfo(...)

This function is quite particular and return true only in the case a first attempt to ask to permission has been denied. Basically it "suggest" to show a message to the user explaining why your app need the specific permission before retry to ask for permission again:



Once explained the reasons you can ask again for the same permission by using this time the single request function:

QtAndroidAppPermissions.requestPermission(...)

Now please note a very important particular. Theoretically you can ask for the same permission infinitely, however from the second time you ask for the same permission the system message window will appear as following:




As you can note in this case there is a checkbox allowing the user to not be annoyed by your request anymore (Don't ask again). If the user will check it and will deny the request again any next request for the permission will be automatically denied without show any message than be careful in decide when ask for permission and, above all, to explain very clearly why you need that permission. After the second try the signal reporting the choice result will be generated again with the updated situation:


Comments

Popular posts from this blog

Access GPIO from Linux user space

Launch an app from Android shell terminal

Android: adb push and read-only file system error