This repository has been archived by the owner on Mar 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
contentdevice.cpp
160 lines (140 loc) · 3.71 KB
/
contentdevice.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "androidutils.h"
#include "contentdevice.h"
#include <QAndroidJniEnvironment>
#include <QtAndroid>
ContentDevice::ContentDevice(QObject *parent) :
QIODevice(parent),
_context(QtAndroid::androidContext()),
_url()
{
Q_ASSERT(_context.isValid());
}
ContentDevice::ContentDevice(const QUrl &url, QObject *parent) :
QIODevice(parent),
_context(QtAndroid::androidContext()),
_url(url)
{
Q_ASSERT(_context.isValid());
}
ContentDevice::ContentDevice(const QAndroidJniObject &context, const QUrl &url, QObject *parent) :
QIODevice(parent),
_context(context),
_url(url)
{
Q_ASSERT(_context.isValid());
}
void ContentDevice::setContext(const QAndroidJniObject &context)
{
_context = context;
Q_ASSERT(_context.isValid());
}
bool ContentDevice::isSequential() const
{
return true;
}
bool ContentDevice::open(QIODevice::OpenMode mode)
{
if(!QIODevice::open(mode))
return false;
QAndroidJniEnvironment env;
try {
auto uri = QAndroidJniObject::callStaticObjectMethod("android/net/Uri",
"parse",
"(Ljava/lang/String;)Landroid/net/Uri;",
QAndroidJniObject::fromString(_url.toString(QUrl::FullyEncoded)).object());
AndroidUtils::javaThrow();
if(!uri.isValid())
return false;
auto contentResolver = _context.callObjectMethod("getContentResolver", "()Landroid/content/ContentResolver;");
AndroidUtils::javaThrow();
if(!contentResolver.isValid())
return false;
switch(mode) {
case QIODevice::ReadOnly:
_stream = contentResolver.callObjectMethod("openInputStream",
"(Landroid/net/Uri;)Ljava/io/InputStream;",
uri.object());
break;
case QIODevice::WriteOnly:
_stream = contentResolver.callObjectMethod("openOutputStream",
"(Landroid/net/Uri;)Ljava/io/OutputStream;",
uri.object());
break;
default:
setErrorString(tr("You can only open ContentDevice with QIODevice::ReadOnly OR QIODevice::WriteOnly. Other flags are not supported"));
QIODevice::close();
return false;
}
AndroidUtils::javaThrow();
if(!_stream.isValid())
return false;
return true;
} catch(QException &e) {
QIODevice::close();
setErrorString(e.what());
return false;
}
}
void ContentDevice::close()
{
if(_stream.isValid()) {
_stream.callMethod<void>("close");
_stream = QAndroidJniObject();
}
QIODevice::close();
}
qint64 ContentDevice::bytesAvailable() const
{
if(openMode().testFlag(QIODevice::ReadOnly) && _stream.isValid())
return (qint64)_stream.callMethod<jint>("available");
return -1;
}
void ContentDevice::flush()
{
if(openMode().testFlag(QIODevice::WriteOnly) && _stream.isValid())
_stream.callMethod<void>("flush");
}
QUrl ContentDevice::url() const
{
return _url;
}
void ContentDevice::setUrl(QUrl url)
{
if (_url == url)
return;
_url = url;
emit urlChanged(url);
}
qint64 ContentDevice::readData(char *data, qint64 maxlen)
{
QAndroidJniEnvironment env;
auto array = env->NewByteArray((jsize)maxlen);
try {
auto cnt = _stream.callMethod<jint>("read", "([B)I", array);
AndroidUtils::javaThrow();
if(cnt > 0)
env->GetByteArrayRegion(array, 0, cnt, reinterpret_cast<jbyte*>(data));
env->DeleteLocalRef(array);
return (qint64)cnt;
} catch(QException &e) {
setErrorString(e.what());
env->DeleteLocalRef(array);
return -1;
}
}
qint64 ContentDevice::writeData(const char *data, qint64 len)
{
QAndroidJniEnvironment env;
auto array = env->NewByteArray((jsize)len);
env->SetByteArrayRegion (array, 0, len, reinterpret_cast<const jbyte*>(data));
try {
_stream.callMethod<void>("write", "([B)V", array);
AndroidUtils::javaThrow();
env->DeleteLocalRef(array);
return len;
} catch(QException &e) {
setErrorString(e.what());
env->DeleteLocalRef(array);
return -1;
}
}