Added tool for share text with other apps

This commit is contained in:
FalsinSoft 2020-01-30 10:33:19 +01:00
parent 934b6c7c5d
commit ea98dc0504
10 changed files with 218 additions and 1 deletions

View File

@ -0,0 +1,57 @@
/*
* MIT License
*
* Copyright (c) 2018 Fabio Falsini <falsinsoft@gmail.com>
*
* 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<jobject>())
{
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<void>("shareText",
"(Ljava/lang/String;)V",
QAndroidJniObject::fromString(Text).object<jstring>()
);
}
}

View File

@ -0,0 +1,46 @@
/*
* MIT License
*
* Copyright (c) 2018 Fabio Falsini <falsinsoft@gmail.com>
*
* 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 <QtAndroidExtras>
#include <QQmlEngine>
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;
};

View File

@ -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<QAndroidGoogleDrive>("QtAndroidTools", 1, 0, "QtAndroidGoogleDrive", &QAndroidGoogleDrive::qmlInstance);
#endif
#ifdef QTAT_SHARING
qmlRegisterSingletonType<QAndroidSharing>("QtAndroidTools", 1, 0, "QtAndroidSharing", &QAndroidSharing::qmlInstance);
#endif
}

View File

@ -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
}

View File

@ -0,0 +1,54 @@
/*
* MIT License
*
* Copyright (c) 2018 Fabio Falsini <falsinsoft@gmail.com>
*
* 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);
}
}

View File

@ -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" }
}

View File

@ -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)
}

View File

@ -17,5 +17,6 @@
<file>tools/AndroidGoogleAccount.qml</file>
<file>tools/AndroidGoogleDrive.qml</file>
<file>tools/AndroidSystem.qml</file>
<file>tools/AndroidSharing.qml</file>
</qresource>
</RCC>

View File

@ -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'
}
}

View File

@ -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)
}
}
}