mirror of
https://github.com/QuasarApp/QtAndroidTools.git
synced 2025-04-26 21:14:31 +00:00
Moved the properties for get main activity action and mimetype to the main tool
This commit is contained in:
parent
5972721500
commit
4e4c4c7e63
@ -667,24 +667,24 @@ QtAndroidSharing.shareBinaryData(mimeType, dataFilePath)</pre>
|
||||
<pre>QtAndroidSharing.closeRequestedSharedFile()</pre>
|
||||
<p>Now the opposite part, that's mean reply to shared requested from other apps. As explained in the first part of this section the activity receiving the request is always the main one. This mean the only way to know if our app has been lanuched from another app asking for share something is to check on startup phase as follow:</p>
|
||||
<pre>Component.onCompleted: {
|
||||
if(QtAndroidSharing.receivedSharingAction === QtAndroidSharing.ACTION_SEND)
|
||||
if(QtAndroidTools.activityAction === QtAndroidTools.ACTION_SEND)
|
||||
{
|
||||
if(QtAndroidSharing.receivedSharingMimeType === "text/plain")
|
||||
if(QtAndroidTools.activityMimeType === "text/plain")
|
||||
{
|
||||
}
|
||||
else if(QtAndroidSharing.receivedSharingMimeType.startsWith("image") === true)
|
||||
else if(QtAndroidTools.activityMimeType.startsWith("image") === true)
|
||||
{
|
||||
}
|
||||
}
|
||||
else if(QtAndroidSharing.receivedSharingAction === QtAndroidSharing.ACTION_PICK)
|
||||
else if(QtAndroidTools.activityAction === QtAndroidTools.ACTION_PICK)
|
||||
{
|
||||
}
|
||||
}</pre>
|
||||
<p>Currently the supported actions are the following:</p>
|
||||
<pre>QtAndroidSharing.ACTION_NONE
|
||||
QtAndroidSharing.ACTION_SEND
|
||||
QtAndroidSharing.ACTION_SEND_MULTIPLE
|
||||
QtAndroidSharing.ACTION_PICK</pre>
|
||||
<pre>QtAndroidTools.ACTION_NONE
|
||||
QtAndroidTools.ACTION_SEND
|
||||
QtAndroidTools.ACTION_SEND_MULTIPLE
|
||||
QtAndroidTools.ACTION_PICK</pre>
|
||||
<p>For the first two requests (ACTION_SEND and ACTION_SEND_MULTIPLE) you can receive the shared data sent to you by using the following functions:</p>
|
||||
<pre>QtAndroidSharing.getReceivedSharedText()
|
||||
QtAndroidSharing.getReceivedSharedBinaryData()
|
||||
|
@ -43,8 +43,6 @@ QAndroidSharing::QAndroidSharing() : m_JavaSharing("com/falsinsoft/qtandroidtool
|
||||
JniEnv->RegisterNatives(ObjectClass, JniMethod, sizeof(JniMethod)/sizeof(JNINativeMethod));
|
||||
JniEnv->DeleteLocalRef(ObjectClass);
|
||||
}
|
||||
|
||||
CheckReceivedSharingRequest();
|
||||
}
|
||||
|
||||
QObject* QAndroidSharing::qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine)
|
||||
@ -60,32 +58,6 @@ QAndroidSharing* QAndroidSharing::instance()
|
||||
return m_pInstance;
|
||||
}
|
||||
|
||||
QAndroidSharing::ACTION_ID QAndroidSharing::getReceivedSharingAction() const
|
||||
{
|
||||
return m_ReceivedSharingAction;
|
||||
}
|
||||
|
||||
QString QAndroidSharing::getReceivedSharingMimeType() const
|
||||
{
|
||||
return m_ReceivedSharingMimeType;
|
||||
}
|
||||
|
||||
void QAndroidSharing::CheckReceivedSharingRequest()
|
||||
{
|
||||
if(m_JavaSharing.isValid())
|
||||
{
|
||||
QAndroidJniObject MimeTypeObj;
|
||||
|
||||
m_ReceivedSharingAction = static_cast<ACTION_ID>(m_JavaSharing.callMethod<jint>("getReceivedSharingAction", "()I"));
|
||||
|
||||
MimeTypeObj = m_JavaSharing.callObjectMethod("getReceivedSharingMimeType", "()Ljava/lang/String;");
|
||||
if(MimeTypeObj.isValid())
|
||||
{
|
||||
m_ReceivedSharingMimeType = MimeTypeObj.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool QAndroidSharing::shareText(const QString &Text)
|
||||
{
|
||||
if(m_JavaSharing.isValid())
|
||||
|
@ -28,24 +28,12 @@
|
||||
|
||||
class QAndroidSharing : public QObject, public QAndroidActivityResultReceiver
|
||||
{
|
||||
Q_PROPERTY(ACTION_ID receivedSharingAction READ getReceivedSharingAction CONSTANT)
|
||||
Q_PROPERTY(QString receivedSharingMimeType READ getReceivedSharingMimeType CONSTANT)
|
||||
Q_DISABLE_COPY(QAndroidSharing)
|
||||
Q_ENUMS(ACTION_ID)
|
||||
Q_OBJECT
|
||||
|
||||
QAndroidSharing();
|
||||
|
||||
public:
|
||||
|
||||
enum ACTION_ID
|
||||
{
|
||||
ACTION_NONE = 0,
|
||||
ACTION_SEND,
|
||||
ACTION_SEND_MULTIPLE,
|
||||
ACTION_PICK
|
||||
};
|
||||
|
||||
static QObject* qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine);
|
||||
static QAndroidSharing* instance();
|
||||
|
||||
@ -59,9 +47,6 @@ public:
|
||||
Q_INVOKABLE void closeRequestedSharedFile();
|
||||
Q_INVOKABLE bool shareFile(bool FileAvailable, const QString &MimeType = QString(), const QString &FilePath = QString());
|
||||
|
||||
ACTION_ID getReceivedSharingAction() const;
|
||||
QString getReceivedSharingMimeType() const;
|
||||
|
||||
signals:
|
||||
void requestedSharedFileReadyToSave(const QString &mimeType, const QString &name, long size);
|
||||
void requestedSharedFileNotAvailable();
|
||||
@ -70,13 +55,10 @@ private:
|
||||
const QAndroidJniObject m_JavaSharing;
|
||||
static QAndroidSharing *m_pInstance;
|
||||
const int m_SharedFileRequestId = 9002;
|
||||
ACTION_ID m_ReceivedSharingAction = ACTION_NONE;
|
||||
QString m_ReceivedSharingMimeType;
|
||||
|
||||
void handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data) override;
|
||||
|
||||
static void RequestedSharedFileInfo(JNIEnv *env, jobject thiz, jstring mimeType, jstring name, jlong size);
|
||||
|
||||
inline QByteArray ConvertByteArray(const QAndroidJniObject &JavaByteArray);
|
||||
void CheckReceivedSharingRequest();
|
||||
};
|
||||
|
@ -74,9 +74,12 @@
|
||||
|
||||
QtAndroidTools *QtAndroidTools::m_pInstance = nullptr;
|
||||
|
||||
QtAndroidTools::QtAndroidTools()
|
||||
QtAndroidTools::QtAndroidTools() : m_JavaTools("com/falsinsoft/qtandroidtools/AndroidTools",
|
||||
"(Landroid/app/Activity;)V",
|
||||
QtAndroid::androidActivity().object<jobject>())
|
||||
{
|
||||
m_pInstance = this;
|
||||
GetActivityData();
|
||||
}
|
||||
|
||||
QObject* QtAndroidTools::qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine)
|
||||
@ -94,6 +97,32 @@ QtAndroidTools* QtAndroidTools::instance()
|
||||
return m_pInstance;
|
||||
}
|
||||
|
||||
QtAndroidTools::ACTION_ID QtAndroidTools::getActivityAction() const
|
||||
{
|
||||
return m_ActivityAction;
|
||||
}
|
||||
|
||||
QString QtAndroidTools::getActivityMimeType() const
|
||||
{
|
||||
return m_ActivityMimeType;
|
||||
}
|
||||
|
||||
void QtAndroidTools::GetActivityData()
|
||||
{
|
||||
if(m_JavaTools.isValid())
|
||||
{
|
||||
QAndroidJniObject MimeTypeObj;
|
||||
|
||||
m_ActivityAction = static_cast<ACTION_ID>(m_JavaTools.callMethod<jint>("getActivityAction", "()I"));
|
||||
|
||||
MimeTypeObj = m_JavaTools.callObjectMethod("getActivityMimeType", "()Ljava/lang/String;");
|
||||
if(MimeTypeObj.isValid())
|
||||
{
|
||||
m_ActivityMimeType = MimeTypeObj.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool QtAndroidTools::insertImage(const QString &Name, const QByteArray &Data)
|
||||
{
|
||||
QPixmap DataImage;
|
||||
|
@ -31,7 +31,10 @@
|
||||
|
||||
class QtAndroidTools : public QObject
|
||||
{
|
||||
Q_PROPERTY(ACTION_ID activityAction READ getActivityAction CONSTANT)
|
||||
Q_PROPERTY(QString activityMimeType READ getActivityMimeType CONSTANT)
|
||||
Q_DISABLE_COPY(QtAndroidTools)
|
||||
Q_ENUMS(ACTION_ID)
|
||||
Q_OBJECT
|
||||
|
||||
class PhotoImageProvider : public QQuickImageProvider
|
||||
@ -65,6 +68,15 @@ class QtAndroidTools : public QObject
|
||||
QtAndroidTools();
|
||||
|
||||
public:
|
||||
|
||||
enum ACTION_ID
|
||||
{
|
||||
ACTION_NONE = 0,
|
||||
ACTION_SEND,
|
||||
ACTION_SEND_MULTIPLE,
|
||||
ACTION_PICK
|
||||
};
|
||||
|
||||
static QObject* qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine);
|
||||
static QtAndroidTools* instance();
|
||||
static void InitializeQmlTools();
|
||||
@ -74,7 +86,15 @@ public:
|
||||
Q_INVOKABLE bool binaryDataToFile(const QByteArray &BinaryData, const QString &FilePath);
|
||||
Q_INVOKABLE QByteArray fileToBinaryData(const QString &FilePath);
|
||||
|
||||
ACTION_ID getActivityAction() const;
|
||||
QString getActivityMimeType() const;
|
||||
|
||||
private:
|
||||
const QAndroidJniObject m_JavaTools;
|
||||
static QtAndroidTools *m_pInstance;
|
||||
QMap<QString, QPixmap> m_PhotoMap;
|
||||
ACTION_ID m_ActivityAction = ACTION_NONE;
|
||||
QString m_ActivityMimeType;
|
||||
|
||||
void GetActivityData();
|
||||
};
|
||||
|
@ -11,6 +11,10 @@ QMAKE_EXTRA_TARGETS += make_dir
|
||||
|
||||
HEADERS += $$PWD/QtAndroidTools.h
|
||||
SOURCES += $$PWD/QtAndroidTools.cpp
|
||||
OTHER_FILES += $$PWD/src/com/falsinsoft/qtandroidtools/AndroidTools.java
|
||||
copy_tools.commands = $(COPY_FILE) $$shell_path($$PWD/src/com/falsinsoft/qtandroidtools/AndroidTools.java) $$shell_path($$ANDROID_PACKAGE_SOURCE_DIR/src/com/falsinsoft/qtandroidtools/)
|
||||
PRE_TARGETDEPS += copy_tools
|
||||
QMAKE_EXTRA_TARGETS += copy_tools
|
||||
|
||||
contains(DEFINES, QTAT_APP_PERMISSIONS) {
|
||||
HEADERS += $$PWD/QAndroidAppPermissions.h
|
||||
|
@ -58,35 +58,6 @@ public class AndroidSharing
|
||||
mActivityInstance = ActivityInstance;
|
||||
}
|
||||
|
||||
public int getReceivedSharingAction()
|
||||
{
|
||||
final String ActionValue = mActivityInstance.getIntent().getAction();
|
||||
int ActionId = ACTION_NONE;
|
||||
|
||||
if(ActionValue != null)
|
||||
{
|
||||
switch(ActionValue)
|
||||
{
|
||||
case Intent.ACTION_SEND:
|
||||
ActionId = ACTION_SEND;
|
||||
break;
|
||||
case Intent.ACTION_SEND_MULTIPLE:
|
||||
ActionId = ACTION_SEND_MULTIPLE;
|
||||
break;
|
||||
case Intent.ACTION_PICK:
|
||||
ActionId = ACTION_PICK;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ActionId;
|
||||
}
|
||||
|
||||
public String getReceivedSharingMimeType()
|
||||
{
|
||||
return mActivityInstance.getIntent().getType();
|
||||
}
|
||||
|
||||
public String getReceivedSharedText()
|
||||
{
|
||||
return mActivityInstance.getIntent().getStringExtra(Intent.EXTRA_TEXT);
|
||||
@ -301,10 +272,5 @@ public class AndroidSharing
|
||||
}
|
||||
}
|
||||
|
||||
private int ACTION_NONE = 0;
|
||||
private int ACTION_SEND = 1;
|
||||
private int ACTION_SEND_MULTIPLE = 2;
|
||||
private int ACTION_PICK = 3;
|
||||
|
||||
private static native void requestedSharedFileInfo(String mimeType, String name, long size);
|
||||
}
|
||||
|
@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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.util.Log;
|
||||
|
||||
public class AndroidTools
|
||||
{
|
||||
private static final String TAG = "AndroidTools";
|
||||
private final Activity mActivityInstance;
|
||||
|
||||
public AndroidTools(Activity ActivityInstance)
|
||||
{
|
||||
mActivityInstance = ActivityInstance;
|
||||
}
|
||||
|
||||
public int getActivityAction()
|
||||
{
|
||||
final String ActionValue = mActivityInstance.getIntent().getAction();
|
||||
int ActionId = ACTION_NONE;
|
||||
|
||||
if(ActionValue != null)
|
||||
{
|
||||
switch(ActionValue)
|
||||
{
|
||||
case Intent.ACTION_SEND:
|
||||
ActionId = ACTION_SEND;
|
||||
break;
|
||||
case Intent.ACTION_SEND_MULTIPLE:
|
||||
ActionId = ACTION_SEND_MULTIPLE;
|
||||
break;
|
||||
case Intent.ACTION_PICK:
|
||||
ActionId = ACTION_PICK;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ActionId;
|
||||
}
|
||||
|
||||
public String getActivityMimeType()
|
||||
{
|
||||
return mActivityInstance.getIntent().getType();
|
||||
}
|
||||
|
||||
private int ACTION_NONE = 0;
|
||||
private int ACTION_SEND = 1;
|
||||
private int ACTION_SEND_MULTIPLE = 2;
|
||||
private int ACTION_PICK = 3;
|
||||
}
|
@ -13,7 +13,7 @@ ApplicationWindow {
|
||||
title: "QtAndroidTools Demo"
|
||||
|
||||
Component.onCompleted: {
|
||||
if(QtAndroidSharing.receivedSharingAction !== QtAndroidSharing.ACTION_NONE)
|
||||
if(QtAndroidTools.activityAction !== QtAndroidTools.ACTION_NONE)
|
||||
{
|
||||
stackView.push("qrc:/tools/AndroidSharing.qml");
|
||||
}
|
||||
|
@ -69,8 +69,12 @@ android {
|
||||
aaptOptions {
|
||||
noCompress 'rcc'
|
||||
}
|
||||
|
||||
defaultConfig {
|
||||
resConfigs "en"
|
||||
}
|
||||
|
||||
packagingOptions {
|
||||
packagingOptions {
|
||||
exclude 'META-INF/DEPENDENCIES'
|
||||
exclude 'META-INF/LICENSE'
|
||||
exclude 'META-INF/LICENSE.txt'
|
||||
|
@ -8,21 +8,21 @@ Page {
|
||||
padding: 20
|
||||
|
||||
Component.onCompleted: {
|
||||
if(QtAndroidSharing.receivedSharingAction === QtAndroidSharing.ACTION_SEND)
|
||||
if(QtAndroidTools.activityAction === QtAndroidTools.ACTION_SEND)
|
||||
{
|
||||
if(QtAndroidSharing.receivedSharingMimeType === "text/plain")
|
||||
if(QtAndroidTools.activityMimeType === "text/plain")
|
||||
{
|
||||
receivedSharedText.text = QtAndroidSharing.getReceivedSharedText();
|
||||
receivedSharedText.open();
|
||||
}
|
||||
else if(QtAndroidSharing.receivedSharingMimeType.startsWith("image") === true)
|
||||
else if(QtAndroidTools.activityMimeType.startsWith("image") === true)
|
||||
{
|
||||
QtAndroidTools.insertImage("SharedImage", QtAndroidSharing.getReceivedSharedBinaryData());
|
||||
sharedImage.source = "image://QtAndroidTools/SharedImage";
|
||||
receivedSharedImage.open();
|
||||
}
|
||||
}
|
||||
else if(QtAndroidSharing.receivedSharingAction === QtAndroidSharing.ACTION_PICK)
|
||||
else if(QtAndroidTools.activityAction === QtAndroidTools.ACTION_PICK)
|
||||
{
|
||||
imageToShareDialog.open();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user