Commit 150c903a authored by Volker Krause's avatar Volker Krause

Add Intent class hiding the corresponding JNI calls

parent e2febbde
......@@ -47,6 +47,7 @@
#ifdef Q_OS_ANDROID
#include <kandroidextras/contentresolver.h>
#include <kandroidextras/intent.h>
#include <kandroidextras/uri.h>
#include <QtAndroid>
......@@ -177,23 +178,19 @@ void ApplicationController::setDocumentManager(DocumentManager* docMgr)
#ifdef Q_OS_ANDROID
void ApplicationController::importFromIntent(const QAndroidJniObject &intent)
{
if (!intent.isValid()) {
return;
}
const auto uri = intent.callObjectMethod("getData", "()Landroid/net/Uri;");
importFromUrl(KAndroidExtras::Uri::toUrl(uri));
KAndroidExtras::Intent i(intent);
importFromUrl(i.getData());
}
#endif
void ApplicationController::showImportFileDialog()
{
#ifdef Q_OS_ANDROID
const auto ACTION_OPEN_DOCUMENT = QAndroidJniObject::getStaticObjectField<jstring>("android/content/Intent", "ACTION_OPEN_DOCUMENT");
QAndroidJniObject intent("android/content/Intent", "(Ljava/lang/String;)V", ACTION_OPEN_DOCUMENT.object());
const auto CATEGORY_OPENABLE = QAndroidJniObject::getStaticObjectField<jstring>("android/content/Intent", "CATEGORY_OPENABLE");
intent.callObjectMethod("addCategory", "(Ljava/lang/String;)Landroid/content/Intent;", CATEGORY_OPENABLE.object());
intent.callObjectMethod("setType", "(Ljava/lang/String;)Landroid/content/Intent;", QAndroidJniObject::fromString(QStringLiteral("*/*")).object());
using namespace KAndroidExtras;
Intent intent;
intent.setAction(Intent::ACTION_OPEN_DOCUMENT());
intent.addCategory(Intent::CATEGORY_OPENABLE());
intent.setType(QStringLiteral("*/*"));
QtAndroid::startActivity(intent, 0, new ActivityResultReceiver([this](int, int, const QAndroidJniObject &intent) { importFromIntent(intent); }));
#else
const auto url = QFileDialog::getOpenFileUrl(nullptr, i18n("Import Reservation"),
......@@ -348,12 +345,15 @@ void ApplicationController::exportData()
{
qCDebug(Log);
#ifdef Q_OS_ANDROID
const auto ACTION_OPEN_DOCUMENT = QAndroidJniObject::getStaticObjectField<jstring>("android/content/Intent", "ACTION_CREATE_DOCUMENT");
QAndroidJniObject intent("android/content/Intent", "(Ljava/lang/String;)V", ACTION_OPEN_DOCUMENT.object());
const auto CATEGORY_OPENABLE = QAndroidJniObject::getStaticObjectField<jstring>("android/content/Intent", "CATEGORY_OPENABLE");
intent.callObjectMethod("addCategory", "(Ljava/lang/String;)Landroid/content/Intent;", CATEGORY_OPENABLE.object());
intent.callObjectMethod("setType", "(Ljava/lang/String;)Landroid/content/Intent;", QAndroidJniObject::fromString(QStringLiteral("*/*")).object());
QtAndroid::startActivity(intent, 0, new ActivityResultReceiver([this](int, int, const QAndroidJniObject &intent) { exportToIntent(intent); }));
using namespace KAndroidExtras;
Intent intent;
intent.setAction(Intent::ACTION_CREATE_DOCUMENT());
intent.addCategory(Intent::CATEGORY_OPENABLE());
intent.setType(QStringLiteral("*/*"));
QtAndroid::startActivity(intent, 0, new ActivityResultReceiver([this](int, int, const QAndroidJniObject &jniIntent) {
Intent intent(jniIntent);
exportToFile(intent.getData().toString());
}));
#else
const auto filePath = QFileDialog::getSaveFileName(nullptr, i18n("Export Itinerary Data"),
QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation),
......@@ -362,26 +362,12 @@ void ApplicationController::exportData()
#endif
}
#ifdef Q_OS_ANDROID
void ApplicationController::exportToIntent(const QAndroidJniObject &intent)
{
if (!intent.isValid()) {
return;
}
const auto uri = intent.callObjectMethod("getData", "()Landroid/net/Uri;");
if (!uri.isValid()) {
return;
}
const auto uriStr = uri.callObjectMethod("toString", "()Ljava/lang/String;");
exportToFile(uriStr.toString());
}
#endif
void ApplicationController::exportToFile(const QString &filePath)
{
qCDebug(Log) << filePath;
if (filePath.isEmpty()) {
return;
}
File f(filePath);
if (!f.open(File::Write)) {
......@@ -471,27 +457,26 @@ void ApplicationController::addDocument(const QString &batchId)
const auto url = QFileDialog::getOpenFileUrl(nullptr, i18n("Add Document"),
QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)),
i18n("All Files (*.*)"));
if (url.isValid()) {
addDocument(batchId, url);
}
addDocument(batchId, url);
#else
const auto ACTION_OPEN_DOCUMENT = QAndroidJniObject::getStaticObjectField<jstring>("android/content/Intent", "ACTION_OPEN_DOCUMENT");
QAndroidJniObject intent("android/content/Intent", "(Ljava/lang/String;)V", ACTION_OPEN_DOCUMENT.object());
const auto CATEGORY_OPENABLE = QAndroidJniObject::getStaticObjectField<jstring>("android/content/Intent", "CATEGORY_OPENABLE");
intent.callObjectMethod("addCategory", "(Ljava/lang/String;)Landroid/content/Intent;", CATEGORY_OPENABLE.object());
intent.callObjectMethod("setType", "(Ljava/lang/String;)Landroid/content/Intent;", QAndroidJniObject::fromString(QStringLiteral("*/*")).object());
QtAndroid::startActivity(intent, 0, new ActivityResultReceiver([this, batchId](int, int, const QAndroidJniObject &intent) {
if (!intent.isValid()) {
return;
}
const auto uri = intent.callObjectMethod("getData", "()Landroid/net/Uri;");
addDocument(batchId, KAndroidExtras::Uri::toUrl(uri));
using namespace KAndroidExtras;
Intent intent;
intent.setAction(Intent::ACTION_OPEN_DOCUMENT());
intent.addCategory(Intent::CATEGORY_OPENABLE());
intent.setType(QStringLiteral("*/*"));
QtAndroid::startActivity(intent, 0, new ActivityResultReceiver([this, batchId](int, int, const QAndroidJniObject &jniIntent) {
Intent intent(jniIntent);
addDocument(batchId, intent.getData());
}));
#endif
}
void ApplicationController::addDocument(const QString &batchId, const QUrl &url)
{
if (!url.isValid()) {
return;
}
const auto docId = QUuid::createUuid().toString();
auto res = m_resMgr->reservation(batchId);
DocumentUtil::addDocumentId(res, docId);
......
......@@ -72,7 +72,6 @@ public:
// for internal use
#ifdef Q_OS_ANDROID
void importFromIntent(const QAndroidJniObject &intent);
void exportToIntent(const QAndroidJniObject &intent);
#endif
Q_SIGNALS:
void clipboardContentChanged();
......
add_library(KAndroidExtras STATIC
contentresolver.cpp
intent.cpp
uri.cpp
)
target_include_directories(KAndroidExtras INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>")
......
/*
Copyright (C) 2019 Volker Krause <vkrause@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "intent.h"
#include "uri.h"
#include <QAndroidJniObject>
#include <QUrl>
using namespace KAndroidExtras;
Intent::Intent()
{
m_intent = QAndroidJniObject("android/content/Intent");
}
Intent::Intent(const QAndroidJniObject &intent)
: m_intent(intent)
{
}
Intent::~Intent() = default;
void Intent::addCategory(const QAndroidJniObject &category)
{
m_intent.callObjectMethod("addCategory", "(Ljava/lang/String;)Landroid/content/Intent;", category.object());
}
QUrl Intent::getData() const
{
if (!m_intent.isValid()) {
return {};
}
const auto uri = m_intent.callObjectMethod("getData", "()Landroid/net/Uri;");
return Uri::toUrl(uri);
}
void Intent::setAction(const QAndroidJniObject &action)
{
m_intent.callObjectMethod("setAction", "(Ljava/lang/String;)Landroid/content/Intent;", action.object());
}
void Intent::setType(const QString &type)
{
m_intent.callObjectMethod("setType", "(Ljava/lang/String;)Landroid/content/Intent;", QAndroidJniObject::fromString(type).object());
}
Intent::operator QAndroidJniObject() const
{
return m_intent;
}
QAndroidJniObject Intent::ACTION_CREATE_DOCUMENT()
{
return QAndroidJniObject::getStaticObjectField<jstring>("android/content/Intent", "ACTION_CREATE_DOCUMENT");
}
QAndroidJniObject Intent::ACTION_OPEN_DOCUMENT()
{
return QAndroidJniObject::getStaticObjectField<jstring>("android/content/Intent", "ACTION_OPEN_DOCUMENT");
}
QAndroidJniObject Intent::ACTION_VIEW()
{
return QAndroidJniObject::getStaticObjectField<jstring>("android/content/Intent", "ACTION_VIEW");
}
QAndroidJniObject Intent::CATEGORY_OPENABLE()
{
return QAndroidJniObject::getStaticObjectField<jstring>("android/content/Intent", "CATEGORY_OPENABLE");
}
/*
Copyright (C) 2019 Volker Krause <vkrause@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef KANDROIDEXTRAS_INTENT_H
#define KANDROIDEXTRAS_INTENT_H
#include <QAndroidJniObject>
class QUrl;
namespace KAndroidExtras {
/** Methods to interact with android.content.Intent objects, beyond what QAndroidIntent offers. */
class Intent
{
public:
/** Creates a new empty intent. */
Intent();
/** Adopts an existing intent available as a JNI object. */
explicit Intent(const QAndroidJniObject &intent);
~Intent();
/** Add a category to the intent. */
void addCategory(const QAndroidJniObject &category);
/** Returns the data of this intent. */
QUrl getData() const;
/** Sets the action of the intent. */
void setAction(const QAndroidJniObject &action);
/** Set the mime type for this intent. */
void setType(const QString &type);
/** Implicit conversion to an QAndroidJniObject. */
operator QAndroidJniObject() const;
/** Action contstant for create document intents. */
static QAndroidJniObject ACTION_CREATE_DOCUMENT();
/** Action contstant for open document intents. */
static QAndroidJniObject ACTION_OPEN_DOCUMENT();
/** Action contstant for viewing intents. */
static QAndroidJniObject ACTION_VIEW();
/** Category constant for openable content. */
static QAndroidJniObject CATEGORY_OPENABLE();
private:
QAndroidJniObject m_intent;
};
}
#endif // KANDROIDEXTRAS_INTENT_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