Skip to content
This repository was archived by the owner on Mar 4, 2023. It is now read-only.

Commit 4c5295e

Browse files
committed
added first chooser code
1 parent c9639f4 commit 4c5295e

File tree

10 files changed

+344
-33
lines changed

10 files changed

+344
-33
lines changed

AndroidFileDialog.qml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import QtQuick 2.8
2+
import de.skycoder42.androidutils 1.0
3+
4+
QtObject {
5+
id: fileDialog
6+
7+
property string defaultSuffix
8+
}

Demo/main.qml

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,76 @@ ApplicationWindow {
99
height: 480
1010
title: qsTr("Hello World")
1111

12+
FileChooser {
13+
id: chooser
14+
}
15+
16+
footer: TabBar {
17+
id: tabBar
18+
currentIndex: swipeView.currentIndex
19+
20+
TabButton {
21+
id: btn0
22+
text: "Utils"
23+
}
24+
25+
TabButton {
26+
id: btn1
27+
text: "File Chooser"
28+
}
29+
}
30+
1231
Page {
1332
anchors.fill: parent
14-
Pane {
33+
34+
SwipeView {
35+
id: swipeView
1536
anchors.fill: parent
16-
ColumnLayout {
17-
anchors.centerIn: parent
18-
19-
TextField {
20-
id: color
21-
placeholderText: "Enter a hex color value"
22-
inputMask: "\\#HHHHHH"
23-
text: "#ABCDEF"
24-
}
25-
Button {
26-
id: changeButton
27-
text: "Change color"
28-
onClicked: AndroidUtils.setStatusBarColor(color.text)
29-
}
37+
currentIndex: tabBar.currentIndex
3038

31-
CheckBox {
32-
id: longBox
33-
text: "Long toast"
34-
}
35-
Button {
36-
id: toastButton
37-
text: "Show Toast"
38-
onClicked: AndroidUtils.showToast("This is a toast", longBox.checked)
39+
Pane {
40+
ColumnLayout {
41+
anchors.centerIn: parent
42+
43+
TextField {
44+
id: color
45+
placeholderText: "Enter a hex color value"
46+
inputMask: "\\#HHHHHH"
47+
text: "#ABCDEF"
48+
}
49+
Button {
50+
id: changeButton
51+
text: "Change color"
52+
onClicked: AndroidUtils.setStatusBarColor(color.text)
53+
}
54+
55+
CheckBox {
56+
id: longBox
57+
text: "Long toast"
58+
}
59+
Button {
60+
id: toastButton
61+
text: "Show Toast"
62+
onClicked: AndroidUtils.showToast("This is a toast", longBox.checked)
63+
}
64+
65+
Button {
66+
id: longButton
67+
text: "Press me long!"
68+
onPressAndHold: AndroidUtils.hapticFeedback(AndroidUtils.LongPress)
69+
}
3970
}
71+
}
72+
73+
Pane {
74+
ColumnLayout {
75+
anchors.centerIn: parent
4076

41-
Button {
42-
id: longButton
43-
text: "Press me long!"
44-
onPressAndHold: AndroidUtils.hapticFeedback(AndroidUtils.LongPress)
77+
Button {
78+
id: chooserButton
79+
text: "Open file chooser"
80+
onClicked: chooser.open()
81+
}
4582
}
4683
}
4784
}

FileDialog.qml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Qt.labs.platform 1.0
2+
3+
FileDialog {
4+
id: fileDialog
5+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package de.skycoder42.androidutils;
2+
3+
import java.util.Map;
4+
import android.util.Log;
5+
import android.content.Context;
6+
import android.content.Intent;
7+
import android.app.Activity;
8+
import org.qtproject.qt5.android.QtNative;
9+
import androidnative.SystemDispatcher;
10+
11+
public class FileChooser {
12+
public static final int GET_CONTENT_ACTION = 0x1091c657;
13+
public static final int OPEN_DOCUMENT_ACTION = 0x83895290;
14+
public static final int CREATE_DOCUMENT_ACTION = 0x7e97d25e;
15+
16+
public static final String GET_CONTENT_MESSAGE = "AndroidUtils.FileChooser.getContent";
17+
public static final String OPEN_DOCUMENT_MESSAGE = "AndroidUtils.FileChooser.openDocument";
18+
public static final String CREATE_DOCUMENT_MESSAGE = "AndroidUtils.FileChooser.createDocument";
19+
20+
static {
21+
SystemDispatcher.addListener(new SystemDispatcher.Listener() {
22+
public void onDispatched(String type , Map message) {
23+
if (type.equals(GET_CONTENT_MESSAGE)) {
24+
getContent((String)message.get("title"),
25+
(String)message.get("mime"),
26+
(Boolean)message.get("openable"),
27+
(Boolean)message.get("localOnly"),
28+
(Boolean)message.get("allowMultiple"));
29+
} else if (type.equals(OPEN_DOCUMENT_MESSAGE)) {
30+
openDocument((String)message.get("title"),
31+
(String)message.get("mime"),
32+
(String)message.get("url"),
33+
(Boolean)message.get("openable"),
34+
(Boolean)message.get("allowMultiple"));
35+
} else if (type.equals(CREATE_DOCUMENT_MESSAGE)) {
36+
createDocument((String)message.get("title"),
37+
(String)message.get("mime"),
38+
(String)message.get("url"),
39+
(String)message.get("name"),
40+
(Boolean)message.get("openable"));
41+
} else if (type.equals(SystemDispatcher.ACTIVITY_RESULT_MESSAGE)) {
42+
onActivityResult(message);
43+
}
44+
}
45+
});
46+
}
47+
48+
static private void getContent(final String title, String mime, boolean openable, boolean localOnly, boolean allowMultiple) {
49+
final Activity activity = QtNative.activity();
50+
51+
final Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
52+
intent.setType(mime);
53+
if(openable)
54+
intent.addCategory(Intent.CATEGORY_OPENABLE);
55+
if(localOnly)
56+
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
57+
if(allowMultiple)
58+
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
59+
60+
Runnable runnable = new Runnable () {
61+
public void run() {
62+
activity.startActivityForResult(
63+
Intent.createChooser(intent, title),
64+
GET_CONTENT_ACTION);
65+
};
66+
};
67+
activity.runOnUiThread(runnable);
68+
}
69+
70+
static private void openDocument(final String title, String mime, String url, boolean openable, boolean allowMultiple) {
71+
72+
}
73+
74+
static private void createDocument(final String title, String mime, String url, String name, boolean openable) {
75+
76+
}
77+
78+
static private void onActivityResult(Map message) {
79+
Log.d("AndroidUtils", message.toString());
80+
}
81+
}

androidutils.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#else
99
#include <QDebug>
1010
#endif
11+
#include "filechooser.h"
1112

1213
#if defined(Q_OS_ANDROID) && !defined(NO_JNI_ONLOAD)
1314
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void*) {
@@ -98,6 +99,7 @@ static void setupUtils()
9899
AndroidNative::SystemDispatcher::instance()->loadClass("de.skycoder42.androidutils.AndroidUtils");
99100

100101
qmlRegisterSingletonType<AndroidUtils>("de.skycoder42.androidutils", 1, 0, "AndroidUtils", createQmlSingleton);
102+
qmlRegisterType<FileChooser>("de.skycoder42.androidutils", 1, 0, "FileChooser");
101103
//qmlProtectModule("de.skycoder42.quickextras", 1);
102104
}
103105

de_skycoder42_androidutils.pri

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
android: QT *= androidextras
22

33
HEADERS += \
4-
$$PWD/androidutils.h
4+
$$PWD/androidutils.h \
5+
$$PWD/filechooser.h
56

67
SOURCES += \
7-
$$PWD/androidutils.cpp
8+
$$PWD/androidutils.cpp \
9+
$$PWD/filechooser.cpp
810
RESOURCES += \
911
$$PWD/de_skycoder42_androidutils.qrc
1012

@@ -13,7 +15,8 @@ INCLUDEPATH += $$PWD
1315
DISTFILES += \
1416
$$PWD/android/androidutils.gradle \
1517
$$PWD/android/src/de/skycoder42/androidutils/AlarmReceiver.java \
16-
$$PWD/android/src/de/skycoder42/androidutils/AndroidUtils.java
18+
$$PWD/android/src/de/skycoder42/androidutils/AndroidUtils.java \
19+
$$PWD/android/src/de/skycoder42/androidutils/FileChooser.java
1720

1821
android {
1922
HEADERS += $$PWD/contentdevice.h

de_skycoder42_androidutils.qrc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
21
<RCC>
3-
<qresource prefix="/de/skycoder42/androidutils">
2+
<qresource prefix="/qt-project.org/imports/de/skycoder42/androidutils">
43
<file>qmldir</file>
4+
<file>FileDialog.qml</file>
5+
<file alias="+android/FileDialog.qml">AndroidFileDialog.qml</file>
56
</qresource>
67
</RCC>

filechooser.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include "filechooser.h"
2+
#include <AndroidNative/systemdispatcher.h>
3+
4+
FileChooser::FileChooser(QObject *parent) :
5+
QObject(parent),
6+
_contentUrl(),
7+
_type(GetContent),
8+
_mimeType(QStringLiteral("*/*")),
9+
_flags(OpenableFlag)
10+
{
11+
auto dispatcher = AndroidNative::SystemDispatcher::instance();
12+
dispatcher->loadClass(QStringLiteral("de.skycoder42.androidutils.FileChooser"));
13+
}
14+
15+
QUrl FileChooser::contentUrl() const
16+
{
17+
return _contentUrl;
18+
}
19+
20+
FileChooser::ChooserType FileChooser::type() const
21+
{
22+
return _type;
23+
}
24+
25+
QString FileChooser::mimeType() const
26+
{
27+
return _mimeType;
28+
}
29+
30+
FileChooser::ChooserFlags FileChooser::flags() const
31+
{
32+
return _flags;
33+
}
34+
35+
void FileChooser::setContentUrl(QUrl contentUrl)
36+
{
37+
if (_contentUrl == contentUrl)
38+
return;
39+
40+
_contentUrl = contentUrl;
41+
emit contentUrlChanged(contentUrl);
42+
}
43+
44+
void FileChooser::setType(FileChooser::ChooserType type)
45+
{
46+
if (_type == type)
47+
return;
48+
49+
_type = type;
50+
emit typeChanged(type);
51+
}
52+
53+
void FileChooser::setMimeType(QString mimeType)
54+
{
55+
if (_mimeType == mimeType)
56+
return;
57+
58+
_mimeType = mimeType;
59+
emit mimeTypeChanged(mimeType);
60+
}
61+
62+
void FileChooser::setFlags(ChooserFlags flags)
63+
{
64+
if (_flags == flags)
65+
return;
66+
67+
_flags = flags;
68+
emit flagsChanged(flags);
69+
}
70+
71+
void FileChooser::open()
72+
{
73+
QString message;
74+
QVariantMap data;
75+
76+
switch (_type) {
77+
case FileChooser::GetContent:
78+
message = QStringLiteral("AndroidUtils.FileChooser.getContent");
79+
data = {
80+
{"mime", _mimeType},
81+
{"openable", _flags.testFlag(OpenableFlag)},
82+
{"localOnly", _flags.testFlag(LocalOnlyFlag)},
83+
{"allowMultiple", _flags.testFlag(AllowMultipleFlag)}
84+
};
85+
break;
86+
case FileChooser::OpenDocument:
87+
message = QStringLiteral("AndroidUtils.FileChooser.openDocument");
88+
data = {
89+
{"mime", _mimeType},
90+
{"url", _contentUrl.toString()},
91+
{"openable", _flags.testFlag(OpenableFlag)},
92+
{"allowMultiple", _flags.testFlag(AllowMultipleFlag)}
93+
};
94+
break;
95+
case FileChooser::CreateDocument:
96+
message = QStringLiteral("AndroidUtils.FileChooser.createDocument");
97+
data = {
98+
{"mime", _mimeType},
99+
{"url", _contentUrl.toString()},
100+
{"title", _contentUrl.fileName()},
101+
{"openable", _flags.testFlag(OpenableFlag)}
102+
};
103+
break;
104+
default:
105+
Q_UNREACHABLE();
106+
break;
107+
}
108+
109+
AndroidNative::SystemDispatcher::instance()->dispatch(message, data);
110+
}

0 commit comments

Comments
 (0)