-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathpermissionhelperandroid.cpp
91 lines (75 loc) · 4.42 KB
/
permissionhelperandroid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "permissionhelperandroid.h"
#include <QHash>
#include <QAndroidIntent>
#include <QApplication>
#include <QSettings>
static QHash<PermissionHelper::Permission, QStringList> permissionMap = {
{PermissionHelper::PermissionBluetooth, {"android.permission.ACCESS_COARSE_LOCATION", "android.permission.ACCESS_FINE_LOCATION"}},
{PermissionHelper::PermissionForegroundLocation, {"android.permission.ACCESS_COARSE_LOCATION", "android.permission.ACCESS_FINE_LOCATION"}},
{PermissionHelper::PermissionBackgroundLocation, {"android.permission.ACCESS_COARSE_LOCATION", "android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_BACKGROUND_LOCATION"}}
};
PermissionHelperAndroid::PermissionHelperAndroid(QObject *parent) : PermissionHelper(parent)
{
qCDebug(dcPermissionHelper()) << "Creating Permission helper" << this;
connect(qApp, &QApplication::applicationStateChanged, this, [this](Qt::ApplicationState state){
if (state == Qt::ApplicationActive) {
emit bluetoothPermissionChanged();
emit foregroundLocationPermissionChanged();
emit backgroundLocationPermissionChanged();
}
});
}
PermissionHelperAndroid::~PermissionHelperAndroid()
{
qCDebug(dcPermissionHelper()) << "Destroying Permission helper" << this;
}
PermissionHelper::PermissionStatus PermissionHelperAndroid::bluetoothPermission() const
{
return permission(PermissionHelper::PermissionBluetooth);
}
void PermissionHelperAndroid::requestPermission(PermissionHelper::Permission permission)
{
qCDebug(dcPermissionHelper()) << "Requesting";
m_requestedPermissions.append(permission);
QtAndroid::requestPermissions({permissionMap.value(permission)}, &permissionResultCallback);
}
void PermissionHelperAndroid::openPermissionSettings()
{
qCDebug(dcPermissionHelper()) << "Opening permission settings";
QAndroidJniObject settingsAction = QAndroidJniObject::getStaticObjectField<jstring>("android/provider/Settings", "ACTION_APPLICATION_DETAILS_SETTINGS");
QAndroidJniObject packageName = QtAndroid::androidActivity().callObjectMethod("getPackageName", "()Ljava/lang/String;");
QString packageUri = QString("package:%1").arg(packageName.toString());
QAndroidJniObject uri = QAndroidJniObject::callStaticObjectMethod("android/net/Uri", "parse", "(Ljava/lang/String;)Landroid/net/Uri;", QAndroidJniObject::fromString(packageUri).object<jstring>());
jint flag = QAndroidJniObject::getStaticField<jint>("android/content/Intent", "FLAG_ACTIVITY_NEW_TASK");
QAndroidJniObject intent("android/content/Intent","(Ljava/lang/String;Landroid/net/Uri;)V", settingsAction.object<jstring>(), uri.object<jobject>());
intent.callObjectMethod("addCategory", "(Ljava/lang/String;)Landroid/content/Intent;", QAndroidJniObject::fromString("android.intent.category.DEFAULT").object<jstring>());
intent.callObjectMethod("addFlags", "(I)Landroid/content/Intent;", flag);
QtAndroid::androidActivity().callMethod<void>("startActivity", "(Landroid/content/Intent;)V", intent.object<jobject>());
}
PermissionHelper::PermissionStatus PermissionHelperAndroid::permission(PermissionHelper::Permission permission) const
{
PermissionStatus status = PermissionStatusGranted;
QStringList androidPermissions = permissionMap.value(permission);
foreach (const QString androidPermission, androidPermissions) {
qCDebug(dcPermissionHelper()) << "Checking permission:" << androidPermission;
qCDebug(dcPermissionHelper()) << "checked:" << m_requestedPermissions;
qCDebug(dcPermissionHelper()) << "Done";
if (QtAndroid::checkPermission(androidPermission) == QtAndroid::PermissionResult::Denied) {
if (m_requestedPermissions.contains(permission)) {
return PermissionStatusDenied;
}
status = PermissionStatusNotDetermined;
}
}
return status;
}
void PermissionHelperAndroid::permissionResultCallback(const QtAndroid::PermissionResultMap &results)
{
// qCDebug(dcPermissionHelper()) << "Permission result callback" << results;
foreach (const QString &key, results.keys()) {
qCDebug(dcPermissionHelper()) << "Permission result:" << key << (results.value(key) == QtAndroid::PermissionResult::Granted);
}
emit PermissionHelper::instance()->bluetoothPermissionChanged();
emit PermissionHelper::instance()->foregroundLocationPermissionChanged();
emit PermissionHelper::instance()->backgroundLocationPermissionChanged();
}