Qml App: better Android interaction on URL export

Clipboard usage is uncommon in Android so export the URL via Intent too
that is the common pattern on Android
This commit is contained in:
Gioacchino Mazzurco 2017-07-04 16:27:12 +02:00
parent a97ba803d8
commit dafaa568b8
10 changed files with 205 additions and 9 deletions

View File

@ -50,15 +50,15 @@ Item
var radix = JSON.parse(par.response).data.cert_string
var name = mainWindow.user_name
var encodedName = UriJs.URI.encode(name)
ClipboardWrapper.postToClipBoard(
var nodeUrl = (
"retroshare://certificate?" +
"name=" + encodedName +
"&radix=" + UriJs.URI.encode(radix) +
"&location=" + encodedName
)
"&location=" + encodedName )
ClipboardWrapper.postToClipBoard(nodeUrl)
linkCopiedPopup.itemName = name
linkCopiedPopup.open()
platformGW.shareUrl(nodeUrl);
})
}
}

View File

@ -123,16 +123,18 @@ Item
function(par)
{
var jD = JSON.parse(par.response).data
ClipboardWrapper.postToClipBoard(
var contactUrl = (
"retroshare://" +
"identity?gxsid=" +
cntDt.md.gxs_id +
"&name=" +
UriJs.URI.encode(cntDt.md.name) +
"&groupdata=" +
UriJs.URI.encode(jD.radix))
UriJs.URI.encode(jD.radix) )
ClipboardWrapper.postToClipBoard(contactUrl)
linkCopiedPopup.itemName = cntDt.md.name
linkCopiedPopup.visible = true
platformGW.shareUrl(contactUrl);
}
)
}

View File

@ -65,6 +65,17 @@ public class RetroShareQmlActivity extends QtActivity
if (uri != null) NativeCalls.notifyIntentUri(uri);
}
@UsedByNativeCode @SuppressWarnings("unused")
public void shareUrl(String urlStr)
{
Intent shareIntent = new Intent()
.setAction(Intent.ACTION_SEND)
.putExtra(Intent.EXTRA_TEXT, urlStr)
.setType("text/plain");
startActivity(Intent.createChooser(shareIntent,"")); // TODO: Need proper title?
}
private boolean isMyServiceRunning(Class<?> serviceClass)
{
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

View File

@ -0,0 +1,41 @@
/*
* RetroShare Android QML App
* Copyright (C) 2017 Gioacchino Mazzurco <gio@eigenlab.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "androidplatforminteracions.h"
#include <QtAndroid>
#include <QtAndroidExtras/QAndroidJniObject>
AndroidPlatformInteracions::AndroidPlatformInteracions(QObject *parent) :
PlatformInteracions(parent) {}
void AndroidPlatformInteracions::shareUrl(QUrl url)
{
QString encUri = url.toString(QUrl::FullyEncoded);
QAndroidJniObject uriStr = QAndroidJniObject::fromString(encUri);
QtAndroid::runOnAndroidThread(
[uriStr]()
{
QtAndroid::androidActivity()
.callMethod<void>(
"shareUrl",
"(Ljava/lang/String;)V",
uriStr.object<jstring>());
});
}

View File

@ -0,0 +1,34 @@
#pragma once
/*
* RetroShare Android QML App
* Copyright (C) 2017 Gioacchino Mazzurco <gio@eigenlab.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QObject>
#include <QUrl>
#include "platforminteracions.h"
class AndroidPlatformInteracions : public PlatformInteracions
{
Q_OBJECT
public:
explicit AndroidPlatformInteracions(QObject *parent = nullptr);
public slots:
virtual void shareUrl(QUrl url);
};

View File

@ -0,0 +1,25 @@
/*
* RetroShare Android QML App
* Copyright (C) 2017 Gioacchino Mazzurco <gio@eigenlab.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "defaultplatforminteracions.h"
DefaultPlatformInteracions::DefaultPlatformInteracions(QObject *parent) :
PlatformInteracions(parent) {}
// Do nothing as the url is pasted to clipboard directly in QML
void DefaultPlatformInteracions::shareUrl(QUrl /*url*/) {}

View File

@ -0,0 +1,34 @@
#pragma once
/*
* RetroShare Android QML App
* Copyright (C) 2017 Gioacchino Mazzurco <gio@eigenlab.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QObject>
#include <QUrl>
#include "platforminteracions.h"
class DefaultPlatformInteracions : public PlatformInteracions
{
Q_OBJECT
public:
explicit DefaultPlatformInteracions(QObject *parent = nullptr);
public slots:
virtual void shareUrl(QUrl url);
};

View File

@ -30,10 +30,16 @@
# include <QtAndroid>
# include <QtAndroidExtras/QAndroidJniObject>
# include <atomic>
# include "androidplatforminteracions.h"
#else
# include "defaultplatforminteracions.h"
#endif // Q_OS_ANDROID
#include "libresapilocalclient.h"
#include "rsqmlappengine.h"
#include "platforminteracions.h"
int main(int argc, char *argv[])
{
@ -60,6 +66,8 @@ int main(int argc, char *argv[])
#ifdef Q_OS_ANDROID
rootContext.setContextProperty("Q_OS_ANDROID", QVariant(true));
AndroidPlatformInteracions platformGW(&app);
/* Add Activity Intent data to args, because onNewIntent is called only if
* the Intet was triggered when the Activity was already created, so only in
* case onCreate is not called.
@ -114,10 +122,12 @@ int main(int argc, char *argv[])
if(!uriStr.isEmpty()) mainArgs.append(uriStr);
#else
DefaultPlatformInteracions platformGW(&app);
rootContext.setContextProperty("Q_OS_ANDROID", QVariant(false));
#endif
rootContext.setContextProperty("mainArgs", mainArgs);
rootContext.setContextProperty("platformGW", &platformGW);
#ifdef QT_DEBUG
rootContext.setContextProperty("QT_DEBUG", QVariant(true));

View File

@ -0,0 +1,32 @@
#pragma once
/*
* RetroShare Android QML App
* Copyright (C) 2017 Gioacchino Mazzurco <gio@eigenlab.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QObject>
#include <QUrl>
class PlatformInteracions : public QObject
{
Q_OBJECT
public:
explicit PlatformInteracions(QObject *parent = nullptr) : QObject(parent) {}
public slots:
virtual void shareUrl(QUrl url) = 0;
};

View File

@ -5,17 +5,24 @@ QT += core network qml quick
CONFIG += c++11
HEADERS += libresapilocalclient.h \
rsqmlappengine.h
rsqmlappengine.h \
platforminteracions.h
SOURCES += main-app.cpp \
libresapilocalclient.cpp \
rsqmlappengine.cpp
RESOURCES += qml.qrc
# Platform interaction specific code
android-g++ {
QT += androidextras
SOURCES += NativeCalls.cpp
HEADERS += NativeCalls.h
HEADERS += NativeCalls.h androidplatforminteracions.h
SOURCES += NativeCalls.cpp androidplatforminteracions.cpp
} else {
HEADERS += defaultplatforminteracions.h
SOURCES += defaultplatforminteracions.cpp
}
# Additional import path used to resolve QML modules in Qt Creator's code model