forked from maz-1/dukto-qt5
-
Notifications
You must be signed in to change notification settings - Fork 8
/
androidutils.h
153 lines (126 loc) · 4.13 KB
/
androidutils.h
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
#ifndef ANDROIDUTILS_H
#define ANDROIDUTILS_H
#include <QtGlobal>
#ifdef Q_OS_ANDROID
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <QAndroidJniObject>
typedef QAndroidJniObject QJniObject;
#else
#include <QJniObject>
#endif
namespace AndroidEnvironment {
int sdkVersion();
QString buildInfo(const QString &name);
}
/*============================================================*/
class AndroidUtilsBase
{
public:
AndroidUtilsBase() = default;
static bool clearExceptions();
static bool hasExceptions();
protected:
static QString getPackageName();
static QJniObject getSystemService(const QString &name);
static QJniObject getContentResolver();
static QJniObject getContext();
static QJniObject getActivity();
};
/*============================================================*/
class AndroidSettings : public AndroidUtilsBase
{
public:
enum Scope {
Global,
Secure,
System,
};
AndroidSettings() = default;
static QString getStringValue(Scope scope, const QString &key);
static int32_t getIntValue(Scope scope, const QString &key);
private:
static const char *className(Scope scope);
};
/*============================================================*/
class AndroidMulticastLock : public AndroidUtilsBase
{
public:
AndroidMulticastLock();
~AndroidMulticastLock();
bool acquire();
void release();
private:
QJniObject lock;
};
class AndroidScreenOn : public AndroidUtilsBase
{
public:
AndroidScreenOn();
~AndroidScreenOn();
private:
QJniObject window;
};
/*============================================================*/
class AndroidContentReader : public AndroidUtilsBase
{
public:
explicit AndroidContentReader(const QString &uri);
explicit AndroidContentReader(const QJniObject &uri);
~AndroidContentReader();
QString getFileName();
QString getUri();
qint64 getAvaiableBytes();
bool open();
QByteArray read(int size);
int read(int size, char *buffer);
void close();
private:
QString uriString;
QJniObject uriObject;
QJniObject *stream = nullptr;
};
class AndroidContentWriter : public AndroidUtilsBase
{
public:
explicit AndroidContentWriter(const QString &uri);
explicit AndroidContentWriter(const QJniObject &uri);
~AndroidContentWriter();
QString getFileName();
QString getUri();
bool open();
bool write(const QByteArray &data);
bool write(const char *data, int size);
void close();
private:
QString uriString;
QJniObject uriObject;
QJniObject *stream = nullptr;
};
/*============================================================*/
class AndroidStorage : AndroidUtilsBase
{
public:
AndroidStorage() = default;
static QJniObject parseUri(const QString &uriString);
static bool hasUriPermission(const QString &uri, bool writable = true);
static bool hasUriPermission(const QJniObject &uri, bool writable = true);
static void grantUriPermission(const QJniObject &uri, bool writable = false);
static void revokeUriPermission(const QJniObject &uri);
static bool isDir(const QJniObject &uri);
static bool exists(const QJniObject &parentDirUri, const QString &fileName, Qt::CaseSensitivity cs = Qt::CaseInsensitive);
static QJniObject getEntry(const QJniObject &parentDirUri, const QString &childName, Qt::CaseSensitivity cs = Qt::CaseInsensitive);
static QList<QJniObject> getEntryList(const QJniObject &dirUri);
static QString getFileName(const QJniObject &uri);
static qint64 getSize(const QJniObject &uri);
static QString getMimeType(const QJniObject &uri);
static QJniObject createDir(const QJniObject &parentDirUri, const QString &childDirName);
static QJniObject createPath(const QJniObject &parentDirUri, const QStringList &path);
static QJniObject createFile(const QJniObject &parentDirUri, const QString &fileName, const QString &mimeType = "*/*");
static bool removeFile(const QJniObject &uri);
private:
static QJniObject getDocumentUri(const QJniObject &uri);
static QJniObject getChildDocumentsUri(const QJniObject &uri);
static QString dirMimeType;
};
#endif
#endif // ANDROIDUTILS_H