Commit 81a94ca5 authored by Volker Krause's avatar Volker Krause
Browse files

Allow to enable full KPublicTransport logging in development mode

This is particularly useful on Android, where we can't just set the
environment variable externally, and where we have to deal with getting
the logs out of the app containment.

While at it, this also removes the use of hard-coded permission strings
from other places in the code in favor of JNI property access.
parent 5b190fc2
......@@ -44,5 +44,11 @@ Kirigami.ScrollablePage {
Layout.fillWidth: true
onClicked: DevelopmentModeController.purgeMapCSS();
}
QQC2.Button {
text: "Enable KPT Logging"
Layout.fillWidth: true
onClicked: DevelopmentModeController.enablePublicTransportLogging();
}
}
}
......@@ -8,6 +8,7 @@
<supports-screens android:largeScreens="true" android:normalScreens="true" android:anyDensity="true" android:smallScreens="true"/>
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- %%INSERT_PERMISSIONS -->
<!-- %%INSERT_FEATURES -->
......
......@@ -48,6 +48,7 @@
#include <kandroidextras/uri.h>
#include <kandroidextras/jnisignature.h>
#include <kandroidextras/jnitypes.h>
#include <kandroidextras/manifestpermission.h>
#include <QtAndroid>
#include <QAndroidJniObject>
......@@ -59,8 +60,6 @@ using namespace KItinerary;
#ifdef Q_OS_ANDROID
#define PERMISSION_CALENDAR QStringLiteral("android.permission.READ_CALENDAR")
static void importReservation(JNIEnv *env, jobject that, jstring data)
{
Q_UNUSED(that);
......@@ -297,15 +296,15 @@ void ApplicationController::importData(const QByteArray &data)
void ApplicationController::checkCalendar()
{
#ifdef Q_OS_ANDROID
if (QtAndroid::checkPermission(PERMISSION_CALENDAR) == QtAndroid::PermissionResult::Granted) {
using namespace KAndroidExtras;
if (QtAndroid::checkPermission(ManifestPermission::READ_CALENDAR) == QtAndroid::PermissionResult::Granted) {
const auto activity = QtAndroid::androidActivity();
if (activity.isValid()) {
activity.callMethod<void>("checkCalendar");
}
} else {
QtAndroid::requestPermissions({PERMISSION_CALENDAR}, [this] (const QtAndroid::PermissionResultMap &result){
if (result[PERMISSION_CALENDAR] == QtAndroid::PermissionResult::Granted) {
QtAndroid::requestPermissions({ManifestPermission::READ_CALENDAR}, [this] (const QtAndroid::PermissionResultMap &result){
if (result[ManifestPermission::READ_CALENDAR] == QtAndroid::PermissionResult::Granted) {
checkCalendar();
}
});
......
......@@ -6,14 +6,42 @@
#include "developmentmodecontroller.h"
#include <QByteArray>
#include <QDebug>
#include <QDir>
#include <QFileDialog>
#include <QFile>
#include <QFileInfo>
#include <QStandardPaths>
#ifdef Q_OS_ANDROID
#include <kandroidextras/contentresolver.h>
#include <kandroidextras/jnisignature.h>
#include <kandroidextras/jnitypes.h>
#include <kandroidextras/manifestpermission.h>
using namespace KAndroidExtras;
#include <QtAndroid>
#endif
void DevelopmentModeController::enablePublicTransportLogging()
{
#ifdef Q_OS_ANDROID
if (QtAndroid::checkPermission(ManifestPermission::WRITE_EXTERNAL_STORAGE) != QtAndroid::PermissionResult::Granted) {
QtAndroid::requestPermissions({ManifestPermission::WRITE_EXTERNAL_STORAGE}, [this] (const QtAndroid::PermissionResultMap &result){
if (result[ManifestPermission::WRITE_EXTERNAL_STORAGE] == QtAndroid::PermissionResult::Granted) {
enablePublicTransportLogging();
}
});
return;
}
const auto f = QtAndroid::androidContext().callObjectMethod("getExternalFilesDir", Jni::signature<java::io::File(java::lang::String)>(), nullptr);
const auto baseDir = f.callObjectMethod("getPath", Jni::signature<java::lang::String()>()).toString();
#else
const auto baseDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
#endif
qputenv("KPUBLICTRANSPORT_LOG_DIR", (baseDir + QLatin1String("/kpublictransport-log")).toUtf8());
}
void DevelopmentModeController::importMapCSS(const QUrl &url)
{
......
......@@ -15,6 +15,7 @@ class DevelopmentModeController
{
Q_GADGET
public:
Q_INVOKABLE void enablePublicTransportLogging();
Q_INVOKABLE void importMapCSS(const QUrl &url);
Q_INVOKABLE void purgeMapCSS();
};
......
......@@ -64,6 +64,8 @@ public:
/** Flag for granting read URI permissions on content providers. */
JNI_CONSTANT(jint, FLAG_GRANT_READ_URI_PERMISSION)
/** Flag for granting write URI permissions on content providers. */
JNI_CONSTANT(jint, FLAG_GRANT_WRITE_URI_PERMISSION)
private:
QAndroidJniObject m_intent;
......
......@@ -45,9 +45,11 @@ namespace KAndroidExtras {
JNI_TYPE(android, content, ContentResolver)
JNI_TYPE(android, content, Intent)
JNI_TYPE(android, database, Cursor)
JNI_TYPE(android, Manifest, permission)
JNI_TYPE(android, net, Uri)
JNI_TYPE(android, provider, OpenableColumns)
JNI_TYPE(android, provider, Settings)
JNI_TYPE(java, io, File)
JNI_TYPE(java, lang, String)
JNI_TYPE(java, util, Locale)
......
/*
SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef KANDROIDEXTRAS_MANIFESTPERMISSIONM_H
#define KANDROIDEXTRAS_MANIFESTPERMISSIONM_H
#include "jniproperty.h"
namespace KAndroidExtras {
/**
* Access to manifest permissions.
* @see https://developer.android.com/reference/android/Manifest.permission
*/
class ManifestPermission
{
public:
// TODO this is a workaround for missing support for nested classes in the JNI type code
typedef ManifestPermission JniBaseType;
static constexpr const char* jniName() { return "android/Manifest$permission"; }
JNI_CONSTANT(java::lang::String, READ_CALENDAR)
JNI_CONSTANT(java::lang::String, READ_EXTERNAL_STORAGE)
JNI_CONSTANT(java::lang::String, WRITE_EXTERNAL_STORAGE)
};
}
#endif // KANDROIDEXTRAS_MANIFESTPERMISSIONM_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment