This repository has been archived by the owner on Apr 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathqadevice.cpp
112 lines (86 loc) · 2.98 KB
/
qadevice.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
#include <QObject>
#include <QtQml>
#include <QSysInfo>
#include <QGuiApplication>
#include "qadevice.h"
#include "qadrawableprovider.h"
#ifdef Q_OS_ANDROID
#include <QAndroidJniEnvironment>
#include <QAndroidJniObject>
#endif
/*!
\qmltype Device
\instantiates QADevice
\inqmlmodule QuickAndroid 1.0
\inherits QtObject
\brief Provider of device related information
*/
static qreal m_dp = 1;
static qreal m_dpi = 72;
static bool m_isTablet = false;
QADevice::QADevice(QObject *parent) : QObject(parent)
{
}
qreal QADevice::readDp()
{
return m_dp;
}
qreal QADevice::dp() const
{
return m_dp;
}
qreal QADevice::dpi() const
{
return m_dpi;
}
/*!
\qmlproperty bool Device::isTablet
This property hold an indicator for tablet device
*/
qreal QADevice::isTablet() const
{
return m_isTablet;
}
/*!
\qmlproperty string Device::os
This property hold the name of the type of current running OS
*/
QString QADevice::os() const
{
return QSysInfo::productType();
}
static QObject *provider(QQmlEngine *engine, QJSEngine *scriptEngine) {
Q_UNUSED(engine);
Q_UNUSED(scriptEngine);
QADevice* device = new QADevice();
if (!engine->imageProvider("quickandroid-drawable")) {
QADrawableProvider* provider = new QADrawableProvider();
provider->setBasePath("qrc:///QuickAndroid");
engine->addImageProvider("quickandroid-drawable",provider);
}
return device;
}
static void init() {
#ifdef Q_OS_ANDROID
QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
QAndroidJniObject resource = activity.callObjectMethod("getResources","()Landroid/content/res/Resources;");
QAndroidJniObject metrics = resource.callObjectMethod("getDisplayMetrics","()Landroid/util/DisplayMetrics;");
m_dp = metrics.getField<float>("density");
m_dpi = metrics.getField<int>("densityDpi");
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
QGuiApplication *app = qobject_cast<QGuiApplication*>(QGuiApplication::instance());
if (app->testAttribute(Qt::AA_EnableHighDpiScaling)) {
m_dp = m_dp / app->devicePixelRatio();
m_dpi = m_dpi / app->devicePixelRatio();
}
#endif
/* Is Tablet. Experimental code */
QAndroidJniObject configuration = resource.callObjectMethod("getConfiguration","()Landroid/content/res/Configuration;");
int screenLayout = configuration.getField<int>("screenLayout");
int SCREENLAYOUT_SIZE_MASK = QAndroidJniObject::getStaticField<int>("android/content/res/Configuration","SCREENLAYOUT_SIZE_MASK");
int SCREENLAYOUT_SIZE_LARGE = QAndroidJniObject::getStaticField<int>("android/content/res/Configuration","SCREENLAYOUT_SIZE_LARGE");
m_isTablet = (screenLayout & SCREENLAYOUT_SIZE_MASK) >= SCREENLAYOUT_SIZE_LARGE;
#endif
qmlRegisterSingletonType<QADevice>("QuickAndroid", 0, 1, "Device", provider);
}
Q_COREAPP_STARTUP_FUNCTION(init)