From ea98dc0504031ad20bfee1611127d2dbff3b5818 Mon Sep 17 00:00:00 2001 From: FalsinSoft Date: Thu, 30 Jan 2020 10:33:19 +0100 Subject: [PATCH] Added tool for share text with other apps --- QtAndroidTools/QAndroidSharing.cpp | 57 +++++++++++++++++++ QtAndroidTools/QAndroidSharing.h | 46 +++++++++++++++ QtAndroidTools/QtAndroidTools.cpp | 6 ++ QtAndroidTools/QtAndroidTools.pri | 9 +++ .../qtandroidtools/AndroidSharing.java | 54 ++++++++++++++++++ QtAndroidToolsDemo/Main.qml | 1 + QtAndroidToolsDemo/QtAndroidToolsDemo.pro | 3 +- QtAndroidToolsDemo/Sources.qrc | 1 + QtAndroidToolsDemo/android/build.gradle | 11 ++++ QtAndroidToolsDemo/tools/AndroidSharing.qml | 31 ++++++++++ 10 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 QtAndroidTools/QAndroidSharing.cpp create mode 100644 QtAndroidTools/QAndroidSharing.h create mode 100644 QtAndroidTools/src/com/falsinsoft/qtandroidtools/AndroidSharing.java create mode 100644 QtAndroidToolsDemo/tools/AndroidSharing.qml diff --git a/QtAndroidTools/QAndroidSharing.cpp b/QtAndroidTools/QAndroidSharing.cpp new file mode 100644 index 0000000..f36bc01 --- /dev/null +++ b/QtAndroidTools/QAndroidSharing.cpp @@ -0,0 +1,57 @@ +/* + * MIT License + * + * Copyright (c) 2018 Fabio Falsini + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "QAndroidSharing.h" + +QAndroidSharing *QAndroidSharing::m_pInstance = nullptr; + +QAndroidSharing::QAndroidSharing() : m_JavaSharing("com/falsinsoft/qtandroidtools/AndroidSharing", + "(Landroid/app/Activity;)V", + QtAndroid::androidActivity().object()) +{ + m_pInstance = this; +} + +QObject* QAndroidSharing::qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine) +{ + Q_UNUSED(engine); + Q_UNUSED(scriptEngine); + + return new QAndroidSharing(); +} + +QAndroidSharing* QAndroidSharing::instance() +{ + return m_pInstance; +} + +void QAndroidSharing::shareText(const QString &Text) +{ + if(m_JavaSharing.isValid()) + { + m_JavaSharing.callMethod("shareText", + "(Ljava/lang/String;)V", + QAndroidJniObject::fromString(Text).object() + ); + } +} diff --git a/QtAndroidTools/QAndroidSharing.h b/QtAndroidTools/QAndroidSharing.h new file mode 100644 index 0000000..7a5a301 --- /dev/null +++ b/QtAndroidTools/QAndroidSharing.h @@ -0,0 +1,46 @@ +/* + * MIT License + * + * Copyright (c) 2018 Fabio Falsini + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#pragma once + +#include +#include + +class QAndroidSharing : public QObject +{ + Q_DISABLE_COPY(QAndroidSharing) + Q_OBJECT + + QAndroidSharing(); + +public: + + static QObject* qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine); + static QAndroidSharing* instance(); + + Q_INVOKABLE void shareText(const QString &Text); + +private: + const QAndroidJniObject m_JavaSharing; + static QAndroidSharing *m_pInstance; +}; diff --git a/QtAndroidTools/QtAndroidTools.cpp b/QtAndroidTools/QtAndroidTools.cpp index 2aca906..4b1b3c3 100644 --- a/QtAndroidTools/QtAndroidTools.cpp +++ b/QtAndroidTools/QtAndroidTools.cpp @@ -67,6 +67,9 @@ #ifdef QTAT_GOOGLE_DRIVE #include "QAndroidGoogleDrive.h" #endif +#ifdef QTAT_SHARING +#include "QAndroidSharing.h" +#endif #include "QtAndroidTools.h" QtAndroidTools::QtAndroidTools() @@ -120,4 +123,7 @@ void QtAndroidTools::InitializeQmlTools() #ifdef QTAT_GOOGLE_DRIVE qmlRegisterSingletonType("QtAndroidTools", 1, 0, "QtAndroidGoogleDrive", &QAndroidGoogleDrive::qmlInstance); #endif +#ifdef QTAT_SHARING + qmlRegisterSingletonType("QtAndroidTools", 1, 0, "QtAndroidSharing", &QAndroidSharing::qmlInstance); +#endif } diff --git a/QtAndroidTools/QtAndroidTools.pri b/QtAndroidTools/QtAndroidTools.pri index fe07d9f..73c08bc 100644 --- a/QtAndroidTools/QtAndroidTools.pri +++ b/QtAndroidTools/QtAndroidTools.pri @@ -142,3 +142,12 @@ contains(DEFINES, QTAT_GOOGLE_DRIVE) { PRE_TARGETDEPS += copy_google_drive QMAKE_EXTRA_TARGETS += copy_google_drive } + +contains(DEFINES, QTAT_SHARING) { + HEADERS += $$PWD/QAndroidSharing.h + SOURCES += $$PWD/QAndroidSharing.cpp + OTHER_FILES += $$PWD/src/com/falsinsoft/qtandroidtools/AndroidSharing.java + copy_sharing.commands = $(COPY_FILE) $$shell_path($$PWD/src/com/falsinsoft/qtandroidtools/AndroidSharing.java) $$shell_path($$ANDROID_PACKAGE_SOURCE_DIR/src/com/falsinsoft/qtandroidtools/) + PRE_TARGETDEPS += copy_sharing + QMAKE_EXTRA_TARGETS += copy_sharing +} diff --git a/QtAndroidTools/src/com/falsinsoft/qtandroidtools/AndroidSharing.java b/QtAndroidTools/src/com/falsinsoft/qtandroidtools/AndroidSharing.java new file mode 100644 index 0000000..c38d73d --- /dev/null +++ b/QtAndroidTools/src/com/falsinsoft/qtandroidtools/AndroidSharing.java @@ -0,0 +1,54 @@ +/* + * MIT License + * + * Copyright (c) 2018 Fabio Falsini + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.falsinsoft.qtandroidtools; + +import android.content.Context; +import android.app.Activity; +import android.content.Intent; +import android.net.Uri; +import android.content.pm.ActivityInfo; +import android.content.pm.ResolveInfo; +import android.content.ComponentName; + +public class AndroidSharing +{ + private final Activity mActivityInstance; + + public AndroidSharing(Activity ActivityInstance) + { + mActivityInstance = ActivityInstance; + } + + public void shareText(String Text) + { + Intent SendIntent = new Intent(); + SendIntent.setAction(Intent.ACTION_SEND); + SendIntent.putExtra(Intent.EXTRA_TEXT, Text); + SendIntent.setType("text/plain"); + + Intent ShareIntent = Intent.createChooser(SendIntent, null); + mActivityInstance.startActivity(ShareIntent); + } +} diff --git a/QtAndroidToolsDemo/Main.qml b/QtAndroidToolsDemo/Main.qml index b545c87..bfcd7e3 100644 --- a/QtAndroidToolsDemo/Main.qml +++ b/QtAndroidToolsDemo/Main.qml @@ -97,6 +97,7 @@ ApplicationWindow { ListElement { title: "PlayStore"; source: "qrc:/tools/AndroidPlayStore.qml" } ListElement { title: "GoogleAccount"; source: "qrc:/tools/AndroidGoogleAccount.qml" } ListElement { title: "GoogleDrive"; source: "qrc:/tools/AndroidGoogleDrive.qml" } + ListElement { title: "Sharing"; source: "qrc:/tools/AndroidSharing.qml" } ListElement { title: "System"; source: "qrc:/tools/AndroidSystem.qml" } } diff --git a/QtAndroidToolsDemo/QtAndroidToolsDemo.pro b/QtAndroidToolsDemo/QtAndroidToolsDemo.pro index c389bc0..e4329d1 100644 --- a/QtAndroidToolsDemo/QtAndroidToolsDemo.pro +++ b/QtAndroidToolsDemo/QtAndroidToolsDemo.pro @@ -36,7 +36,8 @@ DEFINES += \ QTAT_ADMOB_REWARDED_VIDEO \ QTAT_PLAY_STORE \ QTAT_GOOGLE_ACCOUNT \ - QTAT_GOOGLE_DRIVE + QTAT_GOOGLE_DRIVE \ + QTAT_SHARING include(../QtAndroidTools/QtAndroidTools.pri) } diff --git a/QtAndroidToolsDemo/Sources.qrc b/QtAndroidToolsDemo/Sources.qrc index 9cdd7ec..705e70b 100644 --- a/QtAndroidToolsDemo/Sources.qrc +++ b/QtAndroidToolsDemo/Sources.qrc @@ -17,5 +17,6 @@ tools/AndroidGoogleAccount.qml tools/AndroidGoogleDrive.qml tools/AndroidSystem.qml + tools/AndroidSharing.qml diff --git a/QtAndroidToolsDemo/android/build.gradle b/QtAndroidToolsDemo/android/build.gradle index e035602..3488c40 100644 --- a/QtAndroidToolsDemo/android/build.gradle +++ b/QtAndroidToolsDemo/android/build.gradle @@ -69,4 +69,15 @@ android { aaptOptions { noCompress 'rcc' } + + packagingOptions { + exclude 'META-INF/DEPENDENCIES' + exclude 'META-INF/LICENSE' + exclude 'META-INF/LICENSE.txt' + exclude 'META-INF/license.txt' + exclude 'META-INF/NOTICE' + exclude 'META-INF/NOTICE.txt' + exclude 'META-INF/notice.txt' + exclude 'META-INF/ASL2.0' + } } diff --git a/QtAndroidToolsDemo/tools/AndroidSharing.qml b/QtAndroidToolsDemo/tools/AndroidSharing.qml new file mode 100644 index 0000000..d27f303 --- /dev/null +++ b/QtAndroidToolsDemo/tools/AndroidSharing.qml @@ -0,0 +1,31 @@ +import QtQuick 2.12 +import QtQuick.Controls 2.12 +import QtQuick.Dialogs 1.3 +import QtAndroidTools 1.0 + +ScrollablePage { + id: page + padding: 20 + + Column { + anchors.fill: parent + spacing: 20 + + Label { + anchors.horizontalCenter: parent.horizontalCenter + text: "Text to share" + font.bold: true + } + TextInput { + id: sharedText + width: parent.width + text: "Hello Qt!" + horizontalAlignment: TextInput.AlignHCenter + } + Button { + anchors.horizontalCenter: parent.horizontalCenter + text: "Share" + onClicked: QtAndroidSharing.shareText(sharedText.text) + } + } +}