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

Commit 20276b4

Browse files
committed
added content device to read generic android content
1 parent 47ade77 commit 20276b4

7 files changed

+369
-68
lines changed

Demo/android/AndroidManifest.xml

Lines changed: 65 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,79 @@
1-
<?xml version='1.0' encoding='utf-8'?>
1+
<?xml version="1.0"?>
22
<manifest package="org.qtproject.example" xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="1.0" android:versionCode="1" android:installLocation="auto">
3-
<application android:hardwareAccelerated="true" android:name="org.qtproject.qt5.android.bindings.QtApplication" android:label="-- %%INSERT_APP_NAME%% --">
4-
<activity android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation"
5-
android:name="org.qtproject.qt5.android.bindings.QtActivity"
6-
android:label="-- %%INSERT_APP_NAME%% --"
7-
android:screenOrientation="unspecified"
8-
android:launchMode="singleTop">
9-
<intent-filter>
10-
<action android:name="android.intent.action.MAIN"/>
11-
<category android:name="android.intent.category.LAUNCHER"/>
12-
</intent-filter>
3+
<application android:hardwareAccelerated="true" android:name="org.qtproject.qt5.android.bindings.QtApplication" android:label="-- %%INSERT_APP_NAME%% --">
4+
<activity android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation" android:name="org.qtproject.qt5.android.bindings.QtActivity" android:label="-- %%INSERT_APP_NAME%% --" android:screenOrientation="unspecified" android:launchMode="singleTop">
5+
<intent-filter>
6+
<action android:name="android.intent.action.MAIN"/>
7+
<category android:name="android.intent.category.LAUNCHER"/>
8+
</intent-filter>
139

14-
<!-- Application arguments -->
15-
<!-- meta-data android:name="android.app.arguments" android:value="arg1 arg2 arg3"/ -->
16-
<!-- Application arguments -->
10+
<!-- Application arguments -->
11+
<!-- meta-data android:name="android.app.arguments" android:value="arg1 arg2 arg3"/ -->
12+
<!-- Application arguments -->
1713

18-
<meta-data android:name="android.app.lib_name" android:value="-- %%INSERT_APP_LIB_NAME%% --"/>
19-
<meta-data android:name="android.app.qt_sources_resource_id" android:resource="@array/qt_sources"/>
20-
<meta-data android:name="android.app.repository" android:value="default"/>
21-
<meta-data android:name="android.app.qt_libs_resource_id" android:resource="@array/qt_libs"/>
22-
<meta-data android:name="android.app.bundled_libs_resource_id" android:resource="@array/bundled_libs"/>
23-
<!-- Deploy Qt libs as part of package -->
24-
<meta-data android:name="android.app.bundle_local_qt_libs" android:value="-- %%BUNDLE_LOCAL_QT_LIBS%% --"/>
25-
<meta-data android:name="android.app.bundled_in_lib_resource_id" android:resource="@array/bundled_in_lib"/>
26-
<meta-data android:name="android.app.bundled_in_assets_resource_id" android:resource="@array/bundled_in_assets"/>
27-
<!-- Run with local libs -->
28-
<meta-data android:name="android.app.use_local_qt_libs" android:value="-- %%USE_LOCAL_QT_LIBS%% --"/>
29-
<meta-data android:name="android.app.libs_prefix" android:value="/data/local/tmp/qt/"/>
30-
<meta-data android:name="android.app.load_local_libs" android:value="-- %%INSERT_LOCAL_LIBS%% --"/>
31-
<meta-data android:name="android.app.load_local_jars" android:value="-- %%INSERT_LOCAL_JARS%% --"/>
32-
<meta-data android:name="android.app.static_init_classes" android:value="-- %%INSERT_INIT_CLASSES%% --"/>
33-
<!-- Messages maps -->
34-
<meta-data android:value="@string/ministro_not_found_msg" android:name="android.app.ministro_not_found_msg"/>
35-
<meta-data android:value="@string/ministro_needed_msg" android:name="android.app.ministro_needed_msg"/>
36-
<meta-data android:value="@string/fatal_error_msg" android:name="android.app.fatal_error_msg"/>
37-
<!-- Messages maps -->
14+
<meta-data android:name="android.app.lib_name" android:value="-- %%INSERT_APP_LIB_NAME%% --"/>
15+
<meta-data android:name="android.app.qt_sources_resource_id" android:resource="@array/qt_sources"/>
16+
<meta-data android:name="android.app.repository" android:value="default"/>
17+
<meta-data android:name="android.app.qt_libs_resource_id" android:resource="@array/qt_libs"/>
18+
<meta-data android:name="android.app.bundled_libs_resource_id" android:resource="@array/bundled_libs"/>
19+
<!-- Deploy Qt libs as part of package -->
20+
<meta-data android:name="android.app.bundle_local_qt_libs" android:value="-- %%BUNDLE_LOCAL_QT_LIBS%% --"/>
21+
<meta-data android:name="android.app.bundled_in_lib_resource_id" android:resource="@array/bundled_in_lib"/>
22+
<meta-data android:name="android.app.bundled_in_assets_resource_id" android:resource="@array/bundled_in_assets"/>
23+
<!-- Run with local libs -->
24+
<meta-data android:name="android.app.use_local_qt_libs" android:value="-- %%USE_LOCAL_QT_LIBS%% --"/>
25+
<meta-data android:name="android.app.libs_prefix" android:value="/data/local/tmp/qt/"/>
26+
<meta-data android:name="android.app.load_local_libs" android:value="-- %%INSERT_LOCAL_LIBS%% --"/>
27+
<meta-data android:name="android.app.load_local_jars" android:value="-- %%INSERT_LOCAL_JARS%% --"/>
28+
<meta-data android:name="android.app.static_init_classes" android:value="-- %%INSERT_INIT_CLASSES%% --"/>
29+
<!-- Messages maps -->
30+
<meta-data android:value="@string/ministro_not_found_msg" android:name="android.app.ministro_not_found_msg"/>
31+
<meta-data android:value="@string/ministro_needed_msg" android:name="android.app.ministro_needed_msg"/>
32+
<meta-data android:value="@string/fatal_error_msg" android:name="android.app.fatal_error_msg"/>
33+
<!-- Messages maps -->
3834

39-
<!-- Splash screen -->
40-
<!-- meta-data android:name="android.app.splash_screen_drawable" android:resource="@drawable/logo"/ -->
41-
<!-- meta-data android:name="android.app.splash_screen_sticky" android:value="true"/ -->
42-
<!-- Splash screen -->
35+
<!-- Splash screen -->
36+
<!-- meta-data android:name="android.app.splash_screen_drawable" android:resource="@drawable/logo"/ -->
37+
<!-- meta-data android:name="android.app.splash_screen_sticky" android:value="true"/ -->
38+
<!-- Splash screen -->
4339

44-
<!-- Background running -->
45-
<!-- Warning: changing this value to true may cause unexpected crashes if the
46-
application still try to draw after
47-
"applicationStateChanged(Qt::ApplicationSuspended)"
48-
signal is sent! -->
49-
<meta-data android:name="android.app.background_running" android:value="false"/>
50-
<!-- Background running -->
40+
<!-- Background running -->
41+
<!-- Warning: changing this value to true may cause unexpected crashes if the
42+
application still try to draw after
43+
"applicationStateChanged(Qt::ApplicationSuspended)"
44+
signal is sent! -->
45+
<meta-data android:name="android.app.background_running" android:value="false"/>
46+
<!-- Background running -->
5147

52-
<!-- auto screen scale factor -->
53-
<meta-data android:name="android.app.auto_screen_scale_factor" android:value="false"/>
54-
<!-- auto screen scale factor -->
48+
<!-- auto screen scale factor -->
49+
<meta-data android:name="android.app.auto_screen_scale_factor" android:value="false"/>
50+
<!-- auto screen scale factor -->
5551

56-
<!-- extract android style -->
57-
<!-- available android:values :
58-
* full - useful QWidget & Quick Controls 1 apps
59-
* minimal - useful for Quick Controls 2 apps, it is much faster than "full"
60-
* none - useful for apps that don't use any of the above Qt modules
61-
-->
62-
<meta-data android:name="android.app.extract_android_style" android:value="full"/>
63-
<!-- extract android style -->
64-
</activity>
52+
<!-- extract android style -->
53+
<!-- available android:values :
54+
* full - useful QWidget & Quick Controls 1 apps
55+
* minimal - useful for Quick Controls 2 apps, it is much faster than "full"
56+
* none - useful for apps that don't use any of the above Qt modules
57+
-->
58+
<meta-data android:name="android.app.extract_android_style" android:value="full"/>
59+
<!-- extract android style -->
60+
</activity>
6561

66-
<!-- For adding service(s) please check: https://wiki.qt.io/AndroidServices -->
62+
<!-- For adding service(s) please check: https://wiki.qt.io/AndroidServices -->
6763

68-
</application>
64+
</application>
6965

70-
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="16"/>
71-
<supports-screens android:largeScreens="true" android:normalScreens="true" android:anyDensity="true" android:smallScreens="true"/>
66+
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="16"/>
67+
<supports-screens android:largeScreens="true" android:normalScreens="true" android:anyDensity="true" android:smallScreens="true"/>
7268

73-
<!-- The following comment will be replaced upon deployment with default permissions based on the dependencies of the application.
74-
Remove the comment if you do not require these default permissions. -->
75-
<!-- %%INSERT_PERMISSIONS -->
69+
<!-- The following comment will be replaced upon deployment with default permissions based on the dependencies of the application.
70+
Remove the comment if you do not require these default permissions. -->
71+
<!-- %%INSERT_PERMISSIONS -->
7672

77-
<!-- The following comment will be replaced upon deployment with default features based on the dependencies of the application.
78-
Remove the comment if you do not require these default features. -->
79-
<!-- %%INSERT_FEATURES -->
73+
<!-- The following comment will be replaced upon deployment with default features based on the dependencies of the application.
74+
Remove the comment if you do not require these default features. -->
75+
<!-- %%INSERT_FEATURES -->
8076

77+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
78+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
8179
</manifest>

Demo/main.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
1+
#include <QFile>
12
#include <QGuiApplication>
23
#include <QQmlApplicationEngine>
4+
#include <QDebug>
5+
#include <contentdevice.h>
36

47
int main(int argc, char *argv[])
58
{
69
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
710
QGuiApplication app(argc, argv);
811

12+
#ifdef Q_OS_ANDROID
13+
QUrl url("file:///sdcard/temp/baum");
14+
ContentDevice device(url);
15+
16+
if(device.open(QIODevice::WriteOnly)) {
17+
device.write("baum == 42");
18+
device.close();
19+
} else
20+
qCritical() << "ContentDevice:" << device.errorString();
21+
22+
if(device.open(QIODevice::ReadOnly)) {
23+
qDebug() << "ContentDevice:" << device.readAll();
24+
device.close();
25+
} else
26+
qCritical() << "ContentDevice:" << device.errorString();
27+
#endif
28+
929
QQmlApplicationEngine engine;
1030
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
1131

androidutils.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,27 @@ Q_COREAPP_STARTUP_FUNCTION(setupUtils)
2525

2626
AndroidUtils *AndroidUtils::_instance = nullptr;
2727

28+
void AndroidUtils::javaThrow()
29+
{
30+
#ifdef Q_OS_ANDROID
31+
QAndroidJniEnvironment env;
32+
if (env->ExceptionCheck()) {
33+
auto exception = QAndroidJniObject::fromLocalRef(env->ExceptionOccurred());
34+
JavaException exc;
35+
exc._what = exception.callObjectMethod("getLocalizedMessage", "()Ljava/lang/String;").toString().toUtf8();
36+
37+
QAndroidJniObject stringWriter("java/io/StringWriter");
38+
QAndroidJniObject printWriter("java/io/PrintWriter", "(Ljava/lang/Writer;)V", stringWriter.object());
39+
exception.callMethod<void>("printStackTrace", "(Ljava/lang/PrintWriter;)V", printWriter.object());
40+
exc._stackTrace = stringWriter.callObjectMethod("toString", "()Ljava/lang/String;").toString().toUtf8();
41+
42+
env->ExceptionClear();
43+
44+
throw exc;
45+
}
46+
#endif
47+
}
48+
2849
AndroidUtils::AndroidUtils(QObject *parent) :
2950
QObject(parent)
3051
{}
@@ -86,3 +107,33 @@ static QObject *createQmlSingleton(QQmlEngine *qmlEngine, QJSEngine *jsEngine)
86107
Q_UNUSED(jsEngine)
87108
return new AndroidUtils(qmlEngine);
88109
}
110+
111+
112+
113+
JavaException::JavaException() :
114+
_what(),
115+
_stackTrace()
116+
{}
117+
118+
const char *JavaException::what() const noexcept
119+
{
120+
return _what.constData();
121+
}
122+
123+
const QByteArray JavaException::printStackTrace() const noexcept
124+
{
125+
return _stackTrace;
126+
}
127+
128+
void JavaException::raise() const
129+
{
130+
throw *this;
131+
}
132+
133+
QException *JavaException::clone() const
134+
{
135+
auto e = new JavaException();
136+
e->_what = _what;
137+
e->_stackTrace = _stackTrace;
138+
return e;
139+
}

androidutils.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@
44
#include <QObject>
55
#include <QString>
66
#include <QColor>
7+
#include <QException>
8+
9+
class JavaException : public QException
10+
{
11+
friend class AndroidUtils;
12+
public:
13+
JavaException();
14+
15+
const char *what() const noexcept override;
16+
const QByteArray printStackTrace() const noexcept;
17+
18+
void raise() const override;
19+
QException *clone() const override;
20+
21+
private:
22+
QByteArray _what;
23+
QByteArray _stackTrace;
24+
};
725

826
class AndroidUtils : public QObject
927
{
@@ -19,6 +37,8 @@ class AndroidUtils : public QObject
1937
};
2038
Q_ENUM(HapticFeedbackConstant)
2139

40+
static void javaThrow();
41+
2242
AndroidUtils(QObject *parent = nullptr);
2343
static AndroidUtils *instance();
2444

0 commit comments

Comments
 (0)