diff options
Diffstat (limited to 'tests/manual/examples/corelib/permissions')
4 files changed, 244 insertions, 0 deletions
diff --git a/tests/manual/examples/corelib/permissions/CMakeLists.txt b/tests/manual/examples/corelib/permissions/CMakeLists.txt new file mode 100644 index 0000000000..699c90647c --- /dev/null +++ b/tests/manual/examples/corelib/permissions/CMakeLists.txt @@ -0,0 +1,47 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +cmake_minimum_required(VERSION 3.16) +project(permissions LANGUAGES CXX) + +if(NOT DEFINED INSTALL_EXAMPLESDIR) + set(INSTALL_EXAMPLESDIR "examples") +endif() + +set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/corelib/permissions") + +find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets) + +qt_standard_project_setup() + +qt_add_executable(permissions + MANUAL_FINALIZATION + main.cpp + android/AndroidManifest.xml +) + +set_target_properties(permissions PROPERTIES + MACOSX_BUNDLE TRUE + MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/Info.plist" + MACOSX_BUNDLE_GUI_IDENTIFIER "io.qt.examples.permissions" + QT_ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android" +) + +target_link_libraries(permissions PRIVATE + Qt6::Core + Qt6::Gui + Qt6::Widgets +) + +install(TARGETS permissions + RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" + BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" + LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}" +) + +if(APPLE AND NOT CMAKE_GENERATOR STREQUAL "Xcode") + add_custom_command(TARGET permissions + POST_BUILD COMMAND codesign -s - permissions.app) +endif() + +qt_finalize_executable(permissions) diff --git a/tests/manual/examples/corelib/permissions/Info.plist b/tests/manual/examples/corelib/permissions/Info.plist new file mode 100644 index 0000000000..57625d03dc --- /dev/null +++ b/tests/manual/examples/corelib/permissions/Info.plist @@ -0,0 +1,59 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> +<plist version="1.0"> +<dict> + <key>CFBundleInfoDictionaryVersion</key> + <string>6.0</string> + <key>CFBundlePackageType</key> + <string>APPL</string> + + <key>CFBundleName</key> + <string>${MACOSX_BUNDLE_BUNDLE_NAME}</string> + <key>CFBundleIdentifier</key> + <string>${MACOSX_BUNDLE_GUI_IDENTIFIER}</string> + <key>CFBundleExecutable</key> + <string>${MACOSX_BUNDLE_EXECUTABLE_NAME}</string> + + <key>CFBundleVersion</key> + <string>${MACOSX_BUNDLE_BUNDLE_VERSION}</string> + <key>CFBundleShortVersionString</key> + <string>${MACOSX_BUNDLE_SHORT_VERSION_STRING}</string> + + <key>LSMinimumSystemVersion</key> + <string>${CMAKE_OSX_DEPLOYMENT_TARGET}</string> + + <key>NSHumanReadableCopyright</key> + <string>${MACOSX_BUNDLE_COPYRIGHT}</string> + + <key>CFBundleIconFile</key> + <string>${MACOSX_BUNDLE_ICON_FILE}</string> + + <key>CFBundleDevelopmentRegion</key> + <string>English</string> + + <key>NSSupportsAutomaticGraphicsSwitching</key> + <true/> + + <key>NSBluetoothAlwaysUsageDescription</key> + <string>Testing BluetoothAlways</string> + <key>NSCalendarsUsageDescription</key> + <string>Testing Calendars</string> + <key>NSCameraUsageDescription</key> + <string>Testing Camera</string> + <key>NSContactsUsageDescription</key> + <string>Testing Contacts</string> + <key>NSHealthShareUsageDescription</key> + <string>Testing HealthShare</string> + <key>NSHealthUpdateUsageDescription</key> + <string>Testing HealthUpdate</string> + <key>NSLocationUsageDescription</key> + <string>Testing Location on macOS</string> + <key>NSLocationWhenInUseUsageDescription</key> + <string>Testing Location when in use on iOS</string> + <key>NSLocationAlwaysAndWhenInUseUsageDescription</key> + <string>Testing Location always and when in use on iOS</string> + <key>NSMicrophoneUsageDescription</key> + <string>Testing Microphone</string> + +</dict> +</plist> diff --git a/tests/manual/examples/corelib/permissions/android/AndroidManifest.xml b/tests/manual/examples/corelib/permissions/android/AndroidManifest.xml new file mode 100644 index 0000000000..baf025d4bb --- /dev/null +++ b/tests/manual/examples/corelib/permissions/android/AndroidManifest.xml @@ -0,0 +1,51 @@ +<?xml version="1.0"?> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="org.qtproject.example.permissions" + android:installLocation="auto" + android:versionCode="-- %%INSERT_VERSION_CODE%% --" + android:versionName="-- %%INSERT_VERSION_NAME%% --"> + <uses-permission android:name="android.permission.CAMERA" /> + <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" /> + <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> + <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> + <uses-permission android:name="android.permission.RECORD_AUDIO" /> + <uses-permission android:name="android.permission.BLUETOOTH" + android:maxSdkVersion="30" /> + <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> + <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" /> + <uses-permission android:name="android.permission.BLUETOOTH_SCAN" /> + <uses-permission android:name="android.permission.READ_CONTACTS" /> + <uses-permission android:name="android.permission.WRITE_CONTACTS" /> + <uses-permission android:name="android.permission.READ_CALENDAR" /> + <uses-permission android:name="android.permission.WRITE_CALENDAR" /> + <!-- %%INSERT_PERMISSIONS --> + <!-- %%INSERT_FEATURES --> + <supports-screens + android:anyDensity="true" + android:largeScreens="true" + android:normalScreens="true" + android:smallScreens="true" /> + <application + android:name="org.qtproject.qt.android.bindings.QtApplication" + android:hardwareAccelerated="true" + android:label="-- %%INSERT_APP_NAME%% --" + android:requestLegacyExternalStorage="true" + android:allowBackup="true" + android:fullBackupOnly="false"> + <activity + android:name="org.qtproject.qt.android.bindings.QtActivity" + android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation|mcc|mnc|density" + android:launchMode="singleTop" + android:screenOrientation="unspecified" + android:exported="true"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" /> + </intent-filter> + + <meta-data + android:name="android.app.lib_name" + android:value="-- %%INSERT_APP_LIB_NAME%% --" /> + </activity> + </application> +</manifest> diff --git a/tests/manual/examples/corelib/permissions/main.cpp b/tests/manual/examples/corelib/permissions/main.cpp new file mode 100644 index 0000000000..913aed2fec --- /dev/null +++ b/tests/manual/examples/corelib/permissions/main.cpp @@ -0,0 +1,87 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +#include <QtCore/qmetaobject.h> +#include <QtWidgets/qapplication.h> +#include <QtWidgets/qwidget.h> +#include <QtWidgets/qpushbutton.h> +#include <QtWidgets/qlayout.h> +#include <QtWidgets/qmessagebox.h> + +#if !QT_CONFIG(permissions) +#error "This example requires the permissions feature, which is not enabled on this platform" +#endif + +#include <QtCore/qpermissions.h> + +class PermissionWidget : public QWidget +{ + Q_OBJECT +public: + explicit PermissionWidget(QWidget *parent = nullptr) : QWidget(parent) + { + QVBoxLayout *layout = new QVBoxLayout(this); + + static const QPermission permissions[] = { + QCameraPermission{}, + QMicrophonePermission{}, + QBluetoothPermission{}, + QContactsPermission{}, + QCalendarPermission{}, + QLocationPermission{} + }; + + for (auto permission : permissions) { + auto permissionName = QString::fromLatin1(permission.type().name()); + QPushButton *button = new QPushButton(permissionName.sliced(1, permissionName.length() - 11)); + connect(button, &QPushButton::clicked, this, &PermissionWidget::buttonClicked); + button->setProperty("permission", QVariant::fromValue(permission)); + layout->addWidget(button); + } + + QPalette pal = palette(); + pal.setBrush(QPalette::Window, QGradient(QGradient::HappyAcid)); + setPalette(pal); + } + +private: + void buttonClicked() + { + auto *button = static_cast<QPushButton*>(sender()); + + auto permission = button->property("permission").value<QPermission>(); + Q_ASSERT(permission.type().isValid()); + + switch (qApp->checkPermission(permission)) { + case Qt::PermissionStatus::Undetermined: + qApp->requestPermission(permission, this, + [button](const QPermission &permission) { + Q_UNUSED(permission); + emit button->clicked(); // Try again + } + ); + return; + case Qt::PermissionStatus::Denied: + QMessageBox::warning(this, button->text(), + tr("Permission is needed to use %1. Please grant permission "\ + "to this application in the system settings.").arg(button->text())); + return; + case Qt::PermissionStatus::Granted: + break; // Proceed + } + + // All good, can use the feature + QMessageBox::information(this, button->text(), + tr("Accessing %1").arg(button->text())); + } +}; + +int main(int argc, char **argv) +{ + QApplication app(argc, argv); + PermissionWidget widget; + widget.show(); + return app.exec(); +} + +#include "main.moc" |