|
| 1 | +/* |
| 2 | + * Copyright 2019 Kai Pastor |
| 3 | + * |
| 4 | + * This file is part of OpenOrienteering. |
| 5 | + * |
| 6 | + * OpenOrienteering is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * OpenOrienteering is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with OpenOrienteering. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | + |
| 21 | +#ifndef OPENORIENTEERING_APP_PERMISSIONS_H |
| 22 | +#define OPENORIENTEERING_APP_PERMISSIONS_H |
| 23 | + |
| 24 | +#include <QtGlobal> |
| 25 | + |
| 26 | +#ifdef Q_OS_ANDROID |
| 27 | +# include <functional> |
| 28 | +# include <type_traits> |
| 29 | +# include <QtAndroid> |
| 30 | +# include <QObject> |
| 31 | +# include <QPointer> |
| 32 | +# include <QStringList> |
| 33 | +# include <QTimer> |
| 34 | +#endif |
| 35 | + |
| 36 | + |
| 37 | +/** |
| 38 | + * A generic utility for requesting app permissions from the user. |
| 39 | + * |
| 40 | + * This class is modeled after the patterns found in [Qt]Android, |
| 41 | + * but provides an abstraction from OS specific aspects. |
| 42 | + */ |
| 43 | +namespace AppPermissions |
| 44 | +{ |
| 45 | + /// Permissions which are required for certain features of the application. |
| 46 | + enum AppPermission |
| 47 | + { |
| 48 | + LocationAccess, |
| 49 | + StorageAccess, |
| 50 | + }; |
| 51 | + |
| 52 | + /// Possible results of requesting a permission. |
| 53 | + enum PermissionResult |
| 54 | + { |
| 55 | + Denied, |
| 56 | + Granted, |
| 57 | + }; |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + /** |
| 62 | + * Checks if the permission was granted or not. |
| 63 | + */ |
| 64 | + PermissionResult checkPermission(AppPermission permission); |
| 65 | + |
| 66 | + /** |
| 67 | + * Asynchronously requests a new permission to be granted. |
| 68 | + * |
| 69 | + * The given member function on the receiver will be called when the |
| 70 | + * permission is actually granted. |
| 71 | + * |
| 72 | + * This function must not be called while the requested permission is granted. |
| 73 | + */ |
| 74 | + template<class T> |
| 75 | + void requestPermission(AppPermission permission, T* object, void (T::* function)()); |
| 76 | + |
| 77 | + /** |
| 78 | + * Requests a permissions to be granted to the application. |
| 79 | + * |
| 80 | + * This function must not be called while the requested permission is granted. |
| 81 | + */ |
| 82 | + PermissionResult requestPermissionSync(AppPermission permission); |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +#ifdef Q_OS_ANDROID |
| 87 | + |
| 88 | + // The Android implementation uses some helper functions. |
| 89 | + // Only the generic parts are defined in the header. |
| 90 | + |
| 91 | + /// Returns the list of Android permission for the given abstract permission. |
| 92 | + QStringList androidPermissions(AppPermission permission); |
| 93 | + |
| 94 | + /// Tests if all the requested Android permissions are granted. |
| 95 | + bool permissionsGranted(const QStringList& requested_permissions, const QtAndroid::PermissionResultMap& actual_permissions); |
| 96 | + |
| 97 | + /// A utility for safely doing callbacks after asynchronuous requests. |
| 98 | + template <class T> |
| 99 | + struct PermissionCallback |
| 100 | + { |
| 101 | + Q_STATIC_ASSERT((std::is_base_of<QObject, T>::value)); |
| 102 | + |
| 103 | + using Function = void (T::*)(); |
| 104 | + const QPointer<T> object; |
| 105 | + const Function function; |
| 106 | + const QStringList requested_permissions; |
| 107 | + |
| 108 | + PermissionCallback(T* object, Function function, const QStringList& android_permissions) |
| 109 | + : object{object} |
| 110 | + , function{function} |
| 111 | + , requested_permissions{android_permissions} |
| 112 | + {} |
| 113 | + |
| 114 | + void run(const QtAndroid::PermissionResultMap& actual_permissions) |
| 115 | + { |
| 116 | + if (object && permissionsGranted(requested_permissions, actual_permissions)) |
| 117 | + QTimer::singleShot(10, object, function); |
| 118 | + delete this; |
| 119 | + } |
| 120 | + |
| 121 | + }; |
| 122 | + |
| 123 | + template<class T> |
| 124 | + void requestPermission(AppPermission permission, T* object, void (T::* function)()) |
| 125 | + { |
| 126 | + auto const android_permissions = androidPermissions(permission); |
| 127 | + auto* callback = new PermissionCallback<T>(object, function, android_permissions); |
| 128 | + QtAndroid::requestPermissions(android_permissions, [callback](const auto& result) { callback->run(result); }); |
| 129 | + } |
| 130 | + |
| 131 | +#else |
| 132 | + |
| 133 | + // The default implementation is fully inline, |
| 134 | + // in order to allow the compiler to optimize it out. |
| 135 | + |
| 136 | + inline PermissionResult checkPermission(AppPermission /*permission*/) |
| 137 | + { |
| 138 | + return Granted; |
| 139 | + } |
| 140 | + |
| 141 | + template<class T> |
| 142 | + void requestPermission(AppPermission /*permission*/, T* /*object*/, void (T::* /*function*/)()) |
| 143 | + { |
| 144 | + // requestPermission() shouldn't be called because permissions are always granted. |
| 145 | + Q_UNREACHABLE(); |
| 146 | + } |
| 147 | + |
| 148 | + inline PermissionResult requestPermissionSync(AppPermission /*permission*/) |
| 149 | + { |
| 150 | + // requestPermission() shouldn't be called because permissions are always granted. |
| 151 | + Q_UNREACHABLE(); |
| 152 | + return Granted; |
| 153 | + } |
| 154 | + |
| 155 | +#endif |
| 156 | + |
| 157 | +} // namespace AppPermissions |
| 158 | + |
| 159 | +#endif // OPENORIENTEERING_APP_PERMISSIONS_H |
0 commit comments