Commit 75862772 authored by Karel Slaný's avatar Karel Slaný

Trying to catch Android intents from running application.

parent 37462456
......@@ -140,6 +140,14 @@ HEADERS += \
src/setwrapper.h \
src/widgets/paste_input_dialogue.h
android {
SOURCES += \
src/os_android.cpp
HEADERS += \
src/os_android.h
}
RESOURCES += \
res/qml.qrc \
res/translations.qrc
......
......@@ -30,12 +30,6 @@
#include <QtSvg>
#include <QtXml>
/* Android extras. */
#if defined(Q_OS_ANDROID)
#include <QAndroidJniObject>
#include <QtAndroidExtras>
#endif /* defined(Q_OS_ANDROID) */
#include "src/accounts.h"
#include "src/common.h"
#include "src/files.h"
......@@ -44,6 +38,9 @@
#include "src/models/accountmodel.h"
#include "src/models/filemodel.h"
#include "src/models/messagemodel.h"
#if defined(Q_OS_ANDROID)
#include "src/os_android.h"
#endif /* defined(Q_OS_ANDROID) */
#include "src/settings.h"
#include "src/sqlite/db_tables.h"
#include "src/sqlite/account_db.h"
......@@ -117,43 +114,6 @@ int setupCmdLineParser(QCommandLineParser &parser)
return 0;
}
#if defined(Q_OS_ANDROID)
/*!
* @brief Obtain list of files to be opened with the application.
*
* @return List of paths.
*/
static
QStringList getAndroidArguments(void)
{
/*
* http://stackoverflow.com/questions/27771311/how-to-get-application-arguments-on-qt-for-android
*/
QAndroidJniObject activity(QtAndroid::androidActivity());
if (!activity.isValid()) {
return QStringList();
}
QAndroidJniObject intent(activity.callObjectMethod("getIntent",
"()Landroid/content/Intent;"));
if (!intent.isValid()) {
return QStringList();
}
QAndroidJniObject data(intent.callObjectMethod("getData",
"()Landroid/net/Uri;"));
if (!data.isValid()) {
return QStringList();
}
QAndroidJniObject path(data.callObjectMethod("getPath",
"()Ljava/lang/String;"));
if (!path.isValid()) {
return QStringList();
}
return QStringList(path.toString());
}
#endif /* defined(Q_OS_ANDROID) */
/*!
* @brief Registers QML types.
*
......@@ -209,7 +169,7 @@ int main(int argc, char *argv[])
QStringList cmdLineFileNames;
#if defined(Q_OS_ANDROID)
cmdLineFileNames = getAndroidArguments();
cmdLineFileNames = IntentNotification::getIntentArguments();
#else
cmdLineFileNames = parser.positionalArguments();
#endif /* defined(Q_OS_ANDROID) */
......@@ -393,6 +353,11 @@ int main(int argc, char *argv[])
"These files should be opened:",
cmdLineFileNames.join(", "), false);
}
#if defined(Q_OS_ANDROID)
IntentNotification intentNotification(isds);
connect(&app, SIGNAL(applicationStateChanged(Qt::ApplicationState)),
&intentNotification, SLOT(scanIntentsForFiles()));
#endif /* defined(Q_OS_ANDROID) */
/* Run app main event loop */
int ret = app.exec();
......
/*
* Copyright (C) 2014-2017 CZ.NIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 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 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 <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*/
#include <QAndroidJniObject>
#include <QtAndroidExtras>
IntentNotification::IntentNotification(QObject *parent)
: QObject(parent),
m_iw(IsdsWrapper &iw)
{
}
QStringList IntentNotification::getIntentArguments(void)
{
/*
* http://stackoverflow.com/questions/27771311/how-to-get-application-arguments-on-qt-for-android
*/
QAndroidJniObject activity(QtAndroid::androidActivity());
if (!activity.isValid()) {
return QStringList();
}
QAndroidJniObject intent(activity.callObjectMethod("getIntent",
"()Landroid/content/Intent;"));
if (!intent.isValid()) {
return QStringList();
}
QAndroidJniObject data(intent.callObjectMethod("getData",
"()Landroid/net/Uri;"));
if (!data.isValid()) {
return QStringList();
}
QAndroidJniObject path(data.callObjectMethod("getPath",
"()Ljava/lang/String;"));
if (!path.isValid()) {
return QStringList();
}
return QStringList(path.toString());
}
void IntentNotification::scanIntentsForFiles(void)
{
QStringList fileList(getIntentArguments());
if (fileList.isEmpty()) {
return;
}
emit isds.showErrorMessageBox(QObject::tr("Requested opening ZFO"),
"These files should be opened from IntentNotification:",
cmdLineFileNames.join(", "), false);
}
/*
* Copyright (C) 2014-2017 CZ.NIC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 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 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 <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*/
#ifndef _OS_ANDROID_H_
#define _OS_ANDROID_H_
#include <QObject>
#include "src/net/isds_wrapper.h" /* Experimental use, generates error dialogue. */
/*!
* @brief Wraps Android intent retrieval.
*
* @todo See https://bugreports.qt.io/browse/QTBUG-44945 ,
* https://bugreports.qt.io/browse/QTBUG-36014 ,
* http://grokbase.com/t/gg/android-qt/1316xratx9/canonical-way-to-handle-intent-action-view-on-start ,
* http://www.opengis.ch/2015/12/03/passing-android-intents-to-qt/ ,
* http://blog.qt.io/blog/2016/06/30/intents-with-qt-for-android-part-1/
* whether there could be a way how to handle incoming intents via a
* signal-slot mechanism.
*/
class IntentNotification : public QObject {
Q_OBJECT
public:
/*!
* @brief Constructor.
*
* @param[in] iw Isds object.
* @param[in] parent Parent object.
*/
explicit IntentNotification(IsdsWrapper &iw,
QObject *parent = Q_NULLPTR);
/*!
* @brief Obtain intent arguments.
*
* @return List of files to be opened.
*/
static
QStringList getIntentArguments(void);
public slots:
/*!
* @brief Scans intent and process the obtained files.
*/
void scanIntentsForFiles(void);
private:
IsdsWrapper &m_iw; /*!< ISDS wrapper object. */
};
#endif /* _OS_ANDROID_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