mirror of
https://github.com/QuasarApp/SoundBand.git
synced 2025-04-26 15:24:31 +00:00
added android file dialog
This commit is contained in:
parent
4006903192
commit
a817e367ab
@ -1,5 +1,7 @@
|
||||
QT += quick core gui network multimedia sql
|
||||
CONFIG += c++11
|
||||
android: QT += androidextras
|
||||
mac: QT += macextras
|
||||
CONFIG += c++14
|
||||
|
||||
# The following define makes your compiler emit warnings if you use
|
||||
# any feature of Qt which as been marked deprecated (the exact warnings
|
||||
@ -23,7 +25,8 @@ SOURCES += main.cpp \
|
||||
../sync/song.cpp \
|
||||
../sync/sync.cpp \
|
||||
../sync/Log.cpp\
|
||||
../sync/exaptions.cpp
|
||||
../sync/exaptions.cpp \
|
||||
androidfiledialog.cpp
|
||||
|
||||
|
||||
RESOURCES += qml.qrc
|
||||
@ -53,5 +56,6 @@ HEADERS += \
|
||||
../sync/player.h \
|
||||
../sync/song.h \
|
||||
../sync/sync.h \
|
||||
../sync/Log.h
|
||||
../sync/Log.h \
|
||||
androidfiledialog.h
|
||||
|
||||
|
63
SoundBand/androidfiledialog.cpp
Normal file
63
SoundBand/androidfiledialog.cpp
Normal file
@ -0,0 +1,63 @@
|
||||
#ifdef Q_OS_ANDROID
|
||||
|
||||
#include "androidfiledialog.h"
|
||||
|
||||
AndroidFileDialog::ResultReceiver::ResultReceiver(AndroidFileDialog *dialog) : _dialog(dialog) {}
|
||||
AndroidFileDialog::ResultReceiver::~ResultReceiver() {}
|
||||
|
||||
void AndroidFileDialog::ResultReceiver::handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data)
|
||||
{
|
||||
jint RESULT_OK = QAndroidJniObject::getStaticField<jint>("android/app/Activity", "RESULT_OK");
|
||||
if (receiverRequestCode == EXISTING_FILE_NAME_REQUEST && resultCode == RESULT_OK) {
|
||||
QAndroidJniObject uri = data.callObjectMethod("getData", "()Landroid/net/Uri;");
|
||||
QString path = uriToPath(uri);
|
||||
_dialog->emitExistingFileNameReady(path);
|
||||
} else {
|
||||
_dialog->emitExistingFileNameReady(QString());
|
||||
}
|
||||
}
|
||||
|
||||
QString AndroidFileDialog::ResultReceiver::uriToPath(QAndroidJniObject uri)
|
||||
{
|
||||
if (uri.toString().startsWith("file:", Qt::CaseInsensitive)) {
|
||||
return uri.callObjectMethod("getPath", "()Ljava/lang/String;").toString();
|
||||
} else {
|
||||
QAndroidJniObject contentResolver = QtAndroid::androidActivity().callObjectMethod("getContentResolver", "()Landroid/content/ContentResolver;");
|
||||
QAndroidJniObject cursor = contentResolver.callObjectMethod("query", "(Landroid/net/Uri;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;", uri.object<jobject>(), 0, 0, 0, 0);
|
||||
QAndroidJniObject DATA = QAndroidJniObject::fromString("_data");
|
||||
jint columnIndex = cursor.callMethod<jint>("getColumnIndexOrThrow", "(Ljava/lang/String;)I", DATA.object<jstring>());
|
||||
cursor.callMethod<jboolean>("moveToFirst", "()Z");
|
||||
QAndroidJniObject result = cursor.callObjectMethod("getString", "(I)Ljava/lang/String;", columnIndex);
|
||||
return result.isValid() ? result.toString() : QString();
|
||||
}
|
||||
}
|
||||
|
||||
AndroidFileDialog::AndroidFileDialog(QObject *parent) : QObject(parent)
|
||||
{
|
||||
receiver = new ResultReceiver(this);
|
||||
}
|
||||
|
||||
AndroidFileDialog::~AndroidFileDialog()
|
||||
{
|
||||
delete receiver;
|
||||
}
|
||||
|
||||
bool AndroidFileDialog::provideExistingFileName()
|
||||
{
|
||||
QAndroidJniObject ACTION_GET_CONTENT = QAndroidJniObject::fromString("android.intent.action.GET_CONTENT");
|
||||
QAndroidJniObject intent("android/content/Intent");
|
||||
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>());
|
||||
QtAndroid::startActivity(intent.object<jobject>(), EXISTING_FILE_NAME_REQUEST, receiver);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void AndroidFileDialog::emitExistingFileNameReady(QString result)
|
||||
{
|
||||
emit existingFileNameReady(result);
|
||||
}
|
||||
#endif // Q_OS_ANDROID
|
42
SoundBand/androidfiledialog.h
Normal file
42
SoundBand/androidfiledialog.h
Normal file
@ -0,0 +1,42 @@
|
||||
#ifndef FILEDIALOG_H
|
||||
#define FILEDIALOG_H
|
||||
|
||||
#ifdef Q_OS_ANDROID
|
||||
#ifndef ANDROIDFILEDIALOG_H
|
||||
#define ANDROIDFILEDIALOG_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QAndroidJniObject>
|
||||
#include <QtAndroid>
|
||||
#include <QAndroidActivityResultReceiver>
|
||||
|
||||
class AndroidFileDialog : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit AndroidFileDialog(QObject *parent = 0);
|
||||
virtual ~AndroidFileDialog();
|
||||
bool provideExistingFileName();
|
||||
|
||||
private:
|
||||
class ResultReceiver : public QAndroidActivityResultReceiver {
|
||||
AndroidFileDialog *_dialog;
|
||||
public:
|
||||
ResultReceiver(AndroidFileDialog *dialog);
|
||||
virtual ~ResultReceiver();
|
||||
void handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data);
|
||||
QString uriToPath(QAndroidJniObject uri);
|
||||
};
|
||||
|
||||
static const int EXISTING_FILE_NAME_REQUEST = 1;
|
||||
ResultReceiver *receiver;
|
||||
void emitExistingFileNameReady(QString result);
|
||||
|
||||
signals:
|
||||
void existingFileNameReady(QString result);
|
||||
};
|
||||
|
||||
#endif // ANDROIDFILEDIALOG_H
|
||||
|
||||
#endif // Q_OS_ANDROID
|
Loading…
x
Reference in New Issue
Block a user