Skip to content

Commit

Permalink
Changed perimssion request tool from singleton to standard item
Browse files Browse the repository at this point in the history
  • Loading branch information
FalsinSoft committed Jul 30, 2023
1 parent 5b59abd commit 4e8789c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 46 deletions.
20 changes: 12 additions & 8 deletions Documentation/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,18 @@ <h3>AppPermissions</h3>
...
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)</pre>
QtAndroidAppPermissions {
id: permission
}

permission.requestPermissions(permissionsNameList)</pre>
<p>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:</p>
<img src="images/apppermissions2.png">
<p>Once the user will finish to make the choice a signal will be generated with a list of struct as param as follow:</p>
<pre class="prettyprint">Connections {
target: QtAndroidAppPermissions
function onRequestPermissionsResults(results)
{
<pre class="prettyprint">QtAndroidAppPermissions {
id: permission
onRequestPermissionsResults: function(results)
{
for(var i = 0; i &lt; results.length; i++)
{
if(results[i].granted === true)
Expand All @@ -121,7 +125,7 @@ <h3>AppPermissions</h3>
}
else
{
if(QtAndroidAppPermissions.shouldShowRequestPermissionInfo(results[i].name) === true)
if(permission.shouldShowRequestPermissionInfo(results[i].name) === true)
{
if(results[i].name === permissionsNameList[0])
requestPermissionWRITE_EXTERNAL_STORAGE.open();
Expand All @@ -141,11 +145,11 @@ <h3>AppPermissions</h3>
}
}</pre>
<p>Each item in the list contain the param <i>name</i> containing the permission string name and the boolean field <i>granted</i> 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:</p>
<pre class="prettyprint">QtAndroidAppPermissions.shouldShowRequestPermissionInfo(...)</pre>
<pre class="prettyprint">permission.shouldShowRequestPermissionInfo(...)</pre>
<p>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:</p>
<img src="images/apppermissions3.png">
<p>Once explained the reasons you can ask again for the same permission by using this time the single request function:</p>
<pre class="prettyprint">QtAndroidAppPermissions.requestPermission(...)</pre>
<pre class="prettyprint">permission.requestPermission(...)</pre>
<p>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:</p>
<img src="images/apppermissions4.png">
<p>As you can note in this case there is a checkbox allowing the user to not be annoyed by your request anymore (<i>Don't ask again</i>). 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:</p>
Expand Down
18 changes: 1 addition & 17 deletions QtAndroidTools/QAndroidAppPermissions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,8 @@
*/
#include "QAndroidAppPermissions.h"

QAndroidAppPermissions *QAndroidAppPermissions::m_pInstance = nullptr;

QAndroidAppPermissions::QAndroidAppPermissions(QObject *parent) : QObject(parent)
{
m_pInstance = this;
}

QAndroidAppPermissions* QAndroidAppPermissions::create(QQmlEngine *engine, QJSEngine *scriptEngine)
{
Q_UNUSED(engine);
Q_UNUSED(scriptEngine);

return new QAndroidAppPermissions();
}

QAndroidAppPermissions* QAndroidAppPermissions::instance()
QAndroidAppPermissions::QAndroidAppPermissions(QQuickItem *parent) : QQuickItem(parent)
{
return m_pInstance;
}

void QAndroidAppPermissions::requestPermissions(const QStringList &permissionsNameList)
Expand Down
16 changes: 4 additions & 12 deletions QtAndroidTools/QAndroidAppPermissions.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,18 @@

#include <QtCore/private/qandroidextras_p.h>
#include <QQmlEngine>
#include <QQuickItem>
#include <QVariant>

class QAndroidAppPermissions : public QObject
class QAndroidAppPermissions : public QQuickItem
{
Q_OBJECT
QML_SINGLETON
QML_NAMED_ELEMENT(QtAndroidAppPermissions)
Q_DISABLE_COPY(QAndroidAppPermissions)

typedef QHash<QString, QtAndroidPrivate::PermissionResult> PermissionResultMap;

QAndroidAppPermissions() : QAndroidAppPermissions(nullptr) {}
using PermissionResultMap = QHash<QString, QtAndroidPrivate::PermissionResult>;

public:
QAndroidAppPermissions(QObject *parent);

static QAndroidAppPermissions* create(QQmlEngine *engine, QJSEngine *scriptEngine);
static QAndroidAppPermissions* instance();
QAndroidAppPermissions(QQuickItem *parent = nullptr);

Q_INVOKABLE void requestPermissions(const QStringList &permissionsNameList);
Q_INVOKABLE void requestPermission(const QString &permissionName);
Expand All @@ -53,7 +47,5 @@ class QAndroidAppPermissions : public QObject
void requestPermissionsResults(const QVariantList &results);

private:
static QAndroidAppPermissions *m_pInstance;

QVariantList convertToVariantList(const PermissionResultMap &resultMap) const;
};
18 changes: 9 additions & 9 deletions QtAndroidToolsDemo/tools/AndroidAppPermissions.qml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ Page {
}
}

Connections {
target: QtAndroidAppPermissions
function onRequestPermissionsResults(results)
QtAndroidAppPermissions {
id: permission
onRequestPermissionsResults: function(results)
{
for(var i = 0; i < results.length; i++)
{
Expand All @@ -33,7 +33,7 @@ Page {
}
else
{
if(QtAndroidAppPermissions.shouldShowRequestPermissionInfo(results[i].name) === true)
if(permission.shouldShowRequestPermissionInfo(results[i].name) === true)
{
if(results[i].name === permissionsNameList[0])
requestPermissionWRITE_EXTERNAL_STORAGE.open();
Expand Down Expand Up @@ -112,7 +112,7 @@ Page {
Button {
anchors.horizontalCenter: parent.horizontalCenter
text: "Request permissions"
onClicked: QtAndroidAppPermissions.requestPermissions(permissionsNameList)
onClicked: permission.requestPermissions(permissionsNameList)
}
}

Expand All @@ -121,27 +121,27 @@ Page {
buttons: MessageDialog.Ok
title: "Advise"
text: "This app require WRITE_EXTERNAL_STORAGE permission for bla bla bla..."
onAccepted: QtAndroidAppPermissions.requestPermission(permissionsNameList[0])
onAccepted: permission.requestPermission(permissionsNameList[0])
}
MessageDialog {
id: requestPermissionREAD_CALENDAR
buttons: MessageDialog.Ok
title: "Advise"
text: "This app require READ_CALENDAR permission for bla bla bla..."
onAccepted: QtAndroidAppPermissions.requestPermission(permissionsNameList[1])
onAccepted: permission.requestPermission(permissionsNameList[1])
}
MessageDialog {
id: requestPermissionREAD_PHONE_STATE
buttons: MessageDialog.Ok
title: "Advise"
text: "This app require READ_PHONE_STATE permission for bla bla bla..."
onAccepted: QtAndroidAppPermissions.requestPermission(permissionsNameList[2])
onAccepted: permission.requestPermission(permissionsNameList[2])
}
MessageDialog {
id: requestPermissionREAD_CONTACTS
buttons: MessageDialog.Ok
title: "Advise"
text: "This app require READ_CONTACTS permission for bla bla bla..."
onAccepted: QtAndroidAppPermissions.requestPermission(permissionsNameList[3])
onAccepted: permission.requestPermission(permissionsNameList[3])
}
}

0 comments on commit 4e8789c

Please sign in to comment.