From dfe6aa7557b38b10c50f767138da66bb15d604ea Mon Sep 17 00:00:00 2001
From: Armin <armin.felder@osalliance.com>
Date: Sun, 8 Apr 2018 01:04:14 +0200
Subject: [PATCH] added support for video/* and image/* mimetypes

---
 android/androidfiledialog.cpp | 11 ++++++++++-
 android/androidfiledialog.h   |  2 ++
 api/meteorddp.cpp             |  4 ++--
 rocketchat.cpp                |  8 +++++---
 rocketchat.h                  |  1 -
 5 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/android/androidfiledialog.cpp b/android/androidfiledialog.cpp
index bb0b7e3..e77b5c0 100755
--- a/android/androidfiledialog.cpp
+++ b/android/androidfiledialog.cpp
@@ -67,9 +67,18 @@ bool AndroidFileDialog::provideExistingFileName()
     QAndroidJniObject ACTION_GET_CONTENT = QAndroidJniObject::fromString( "android.intent.action.GET_CONTENT" );
     QAndroidJniObject intent( "android/content/Intent" );
 
+    QAndroidJniEnvironment env;
+    jobjectArray stringArray = env->NewObjectArray( 3, env->FindClass( "java/lang/String" ), NULL );
+    env->SetObjectArrayElement( stringArray, 0, QAndroidJniObject::fromString( QStringLiteral( "file/*" ) ).object<jstring>() );
+    env->SetObjectArrayElement( stringArray, 1, QAndroidJniObject::fromString( QStringLiteral( "image/*" ) ).object<jstring>() );
+    env->SetObjectArrayElement( stringArray, 2, QAndroidJniObject::fromString( QStringLiteral( "video/*" ) ).object<jstring>() );
+    QAndroidJniObject jniArray = QAndroidJniObject::fromLocalRef( stringArray );
+    QAndroidJniObject EXTRA_MIME_TYPES = QAndroidJniObject::getStaticObjectField<jstring>( "android/content/Intent", "EXTRA_MIME_TYPES" );
+
     if ( ACTION_GET_CONTENT.isValid() && intent.isValid() ) {
         intent.callObjectMethod( "setAction", "(Ljava/lang/String;)Landroid/content/Intent;", ACTION_GET_CONTENT.object<jstring>() );
-        intent.callObjectMethod( "setType", "(Ljava/lang/String;)Landroid/content/Intent;", QAndroidJniObject::fromString( "file/*" ).object<jstring>() );
+        intent.callObjectMethod( "setType", "(Ljava/lang/String;)Landroid/content/Intent;", QAndroidJniObject::fromString( "*/*" ).object<jstring>() );
+        intent.callObjectMethod( "putExtra", "(Ljava/lang/String;[Ljava/lang/String;)Landroid/content/Intent;", EXTRA_MIME_TYPES.object<jstring>(), jniArray.object<jobjectArray>() );
         QtAndroid::startActivity( intent.object<jobject>(), EXISTING_FILE_NAME_REQUEST, receiver );
         return true;
     } else {
diff --git a/android/androidfiledialog.h b/android/androidfiledialog.h
index bd96e53..63f67a6 100755
--- a/android/androidfiledialog.h
+++ b/android/androidfiledialog.h
@@ -25,6 +25,8 @@
 #include <QAndroidJniObject>
 #include <QtAndroid>
 #include <QAndroidActivityResultReceiver>
+#include <QAndroidJniEnvironment>
+
 class AndroidFileDialog : public QObject
 {
     Q_OBJECT public:
diff --git a/api/meteorddp.cpp b/api/meteorddp.cpp
index 8a13095..f44decb 100755
--- a/api/meteorddp.cpp
+++ b/api/meteorddp.cpp
@@ -77,7 +77,7 @@ void MeteorDDP::connectWithServer()
 void MeteorDDP::onTextMessageReceived( const QString &pMsg )
 {
 #if defined(Q_OS_LINUX)|| defined(Q_OS_IOS)
-    qWarning() << pMsg << "\n";
+    qDebug() << pMsg << "\n";
 #endif
 
     if ( Q_LIKELY( pMsg.length() ) ) {
@@ -168,7 +168,7 @@ void MeteorDDP::sendJson( const QJsonObject &pMsgObj )
 
     QJsonDocument msgJson = QJsonDocument( pMsgObj );
     QString msgString = msgJson.toJson( QJsonDocument::Compact );
-    qWarning() << "message String" << msgString;
+    qDebug() << "message String" << msgString;
     mWsClient->sendTextMessage( msgString );
 }
 
diff --git a/rocketchat.cpp b/rocketchat.cpp
index 4e71557..11b85d5 100755
--- a/rocketchat.cpp
+++ b/rocketchat.cpp
@@ -750,19 +750,21 @@ void RocketChat::storageReadySlot()
     emit storageReady();
 }
 #ifdef Q_OS_ANDROID
+//TODO: do this in a clean way
 void RocketChat::openAndroidFileDialog( QString channelId )
 {
     currentChannel = channelId;
-    AndroidFileDialog *fileDialog = new AndroidFileDialog();
-    connect( fileDialog, &AndroidFileDialog::existingFileNameReady, this, &RocketChat::openFileNameReady, Qt::UniqueConnection );
+    QSharedPointer<AndroidFileDialog> fileDialog( new AndroidFileDialog() );
+    connect( fileDialog.data(), &AndroidFileDialog::existingFileNameReady, this, &RocketChat::openFileNameReady, Qt::UniqueConnection );
     bool success = fileDialog->provideExistingFileName();
 
     if ( !success ) {
         qWarning() << "Problem with JNI or sth like that...";
-        disconnect( fileDialog, &AndroidFileDialog::existingFileNameReady, this, &RocketChat::openFileNameReady );
+        disconnect( fileDialog.data(), &AndroidFileDialog::existingFileNameReady, this, &RocketChat::openFileNameReady );
         //or just delete fileDialog instead of disconnect
     }
 }
+
 #endif
 
 #if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
diff --git a/rocketchat.h b/rocketchat.h
index 9edefa3..42ddb59 100755
--- a/rocketchat.h
+++ b/rocketchat.h
@@ -35,7 +35,6 @@
 #ifdef Q_OS_ANDROID
 #include "android/androidfiledialog.h"
 #include "android/androidstatusbarcolor.h"
-
 #endif
 
 
-- 
GitLab