From dafaa568b8769f9c1c91fad16cb0cccaf17d84e4 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Tue, 4 Jul 2017 16:27:12 +0200 Subject: [PATCH] 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 --- retroshare-qml-app/src/AddTrustedNode.qml | 8 ++-- retroshare-qml-app/src/ContactDetails.qml | 6 ++- .../qml_app/RetroShareQmlActivity.java | 11 +++++ .../src/androidplatforminteracions.cpp | 41 +++++++++++++++++++ .../src/androidplatforminteracions.h | 34 +++++++++++++++ .../src/defaultplatforminteracions.cpp | 25 +++++++++++ .../src/defaultplatforminteracions.h | 34 +++++++++++++++ retroshare-qml-app/src/main-app.cpp | 10 +++++ retroshare-qml-app/src/platforminteracions.h | 32 +++++++++++++++ retroshare-qml-app/src/retroshare-qml-app.pro | 13 ++++-- 10 files changed, 205 insertions(+), 9 deletions(-) create mode 100644 retroshare-qml-app/src/androidplatforminteracions.cpp create mode 100644 retroshare-qml-app/src/androidplatforminteracions.h create mode 100644 retroshare-qml-app/src/defaultplatforminteracions.cpp create mode 100644 retroshare-qml-app/src/defaultplatforminteracions.h create mode 100644 retroshare-qml-app/src/platforminteracions.h diff --git a/retroshare-qml-app/src/AddTrustedNode.qml b/retroshare-qml-app/src/AddTrustedNode.qml index 823bc8e87..f444fa957 100644 --- a/retroshare-qml-app/src/AddTrustedNode.qml +++ b/retroshare-qml-app/src/AddTrustedNode.qml @@ -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); }) } } diff --git a/retroshare-qml-app/src/ContactDetails.qml b/retroshare-qml-app/src/ContactDetails.qml index e66b267a9..39ff0dda0 100644 --- a/retroshare-qml-app/src/ContactDetails.qml +++ b/retroshare-qml-app/src/ContactDetails.qml @@ -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); } ) } diff --git a/retroshare-qml-app/src/android/src/org/retroshare/android/qml_app/RetroShareQmlActivity.java b/retroshare-qml-app/src/android/src/org/retroshare/android/qml_app/RetroShareQmlActivity.java index 389ee1448..8d3479f95 100644 --- a/retroshare-qml-app/src/android/src/org/retroshare/android/qml_app/RetroShareQmlActivity.java +++ b/retroshare-qml-app/src/android/src/org/retroshare/android/qml_app/RetroShareQmlActivity.java @@ -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); diff --git a/retroshare-qml-app/src/androidplatforminteracions.cpp b/retroshare-qml-app/src/androidplatforminteracions.cpp new file mode 100644 index 000000000..3f8314631 --- /dev/null +++ b/retroshare-qml-app/src/androidplatforminteracions.cpp @@ -0,0 +1,41 @@ +/* + * RetroShare Android QML App + * Copyright (C) 2017 Gioacchino Mazzurco + * + * 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 . + */ + +#include "androidplatforminteracions.h" + +#include +#include + +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( + "shareUrl", + "(Ljava/lang/String;)V", + uriStr.object()); + }); +} diff --git a/retroshare-qml-app/src/androidplatforminteracions.h b/retroshare-qml-app/src/androidplatforminteracions.h new file mode 100644 index 000000000..25cf50f1d --- /dev/null +++ b/retroshare-qml-app/src/androidplatforminteracions.h @@ -0,0 +1,34 @@ +#pragma once +/* + * RetroShare Android QML App + * Copyright (C) 2017 Gioacchino Mazzurco + * + * 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 . + */ + +#include +#include + +#include "platforminteracions.h" + +class AndroidPlatformInteracions : public PlatformInteracions +{ + Q_OBJECT + +public: + explicit AndroidPlatformInteracions(QObject *parent = nullptr); + +public slots: + virtual void shareUrl(QUrl url); +}; diff --git a/retroshare-qml-app/src/defaultplatforminteracions.cpp b/retroshare-qml-app/src/defaultplatforminteracions.cpp new file mode 100644 index 000000000..2bb85bffc --- /dev/null +++ b/retroshare-qml-app/src/defaultplatforminteracions.cpp @@ -0,0 +1,25 @@ +/* + * RetroShare Android QML App + * Copyright (C) 2017 Gioacchino Mazzurco + * + * 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 . + */ + +#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*/) {} diff --git a/retroshare-qml-app/src/defaultplatforminteracions.h b/retroshare-qml-app/src/defaultplatforminteracions.h new file mode 100644 index 000000000..c2cf5d9e4 --- /dev/null +++ b/retroshare-qml-app/src/defaultplatforminteracions.h @@ -0,0 +1,34 @@ +#pragma once +/* + * RetroShare Android QML App + * Copyright (C) 2017 Gioacchino Mazzurco + * + * 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 . + */ + +#include +#include + +#include "platforminteracions.h" + +class DefaultPlatformInteracions : public PlatformInteracions +{ + Q_OBJECT + +public: + explicit DefaultPlatformInteracions(QObject *parent = nullptr); + +public slots: + virtual void shareUrl(QUrl url); +}; diff --git a/retroshare-qml-app/src/main-app.cpp b/retroshare-qml-app/src/main-app.cpp index 52dd19962..5e5fd532f 100644 --- a/retroshare-qml-app/src/main-app.cpp +++ b/retroshare-qml-app/src/main-app.cpp @@ -30,10 +30,16 @@ # include # include # include +# 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)); diff --git a/retroshare-qml-app/src/platforminteracions.h b/retroshare-qml-app/src/platforminteracions.h new file mode 100644 index 000000000..251b7799a --- /dev/null +++ b/retroshare-qml-app/src/platforminteracions.h @@ -0,0 +1,32 @@ +#pragma once +/* + * RetroShare Android QML App + * Copyright (C) 2017 Gioacchino Mazzurco + * + * 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 . + */ + +#include +#include + +class PlatformInteracions : public QObject +{ + Q_OBJECT + +public: + explicit PlatformInteracions(QObject *parent = nullptr) : QObject(parent) {} + +public slots: + virtual void shareUrl(QUrl url) = 0; +}; diff --git a/retroshare-qml-app/src/retroshare-qml-app.pro b/retroshare-qml-app/src/retroshare-qml-app.pro index 338935419..cba108575 100644 --- a/retroshare-qml-app/src/retroshare-qml-app.pro +++ b/retroshare-qml-app/src/retroshare-qml-app.pro @@ -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