QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
AndroidInterface.h
Go to the documentation of this file.
1#pragma once
2
3#include <QtCore/QJniEnvironment>
4#include <QtCore/QLoggingCategory>
5#include <QtCore/QString>
6
7Q_DECLARE_LOGGING_CATEGORY(AndroidInterfaceLog)
8
9namespace AndroidInterface {
10void setNativeMethods();
12QString getSDCardPath();
13void setKeepScreenOn(bool on);
14
15constexpr const char* kJniQGCActivityClassName = "org/mavlink/qgroundcontrol/QGCActivity";
16
17template <typename T>
19{
20public:
21 JniLocalRef(JNIEnv* env, T ref = nullptr) : _env(env), _ref(ref)
22 {
23 }
24
26 {
27 reset();
28 }
29
30 JniLocalRef(const JniLocalRef&) = delete;
32
33 JniLocalRef(JniLocalRef&& other) noexcept : _env(other._env), _ref(other._ref)
34 {
35 other._ref = nullptr;
36 }
37
39 {
40 if (this == &other) {
41 return *this;
42 }
43
44 reset();
45 _env = other._env;
46 _ref = other._ref;
47 other._ref = nullptr;
48 return *this;
49 }
50
51 T get() const
52 {
53 return _ref;
54 }
55
56 operator T() const
57 {
58 return _ref;
59 }
60
61 void reset(T ref = nullptr)
62 {
63 if (_env && _ref) {
64 _env->DeleteLocalRef(_ref);
65 }
66 _ref = ref;
67 }
68
69private:
70 JNIEnv* _env = nullptr;
71 T _ref = nullptr;
72};
73
74template <typename... Args>
75inline bool callStaticIntMethod(QJniEnvironment& env, jclass cls, jmethodID method, const char* caller,
76 const QLoggingCategory& logCategory, jint& result, Args... args)
77{
78 result = env->CallStaticIntMethod(cls, method, args...);
79 if (env.checkAndClearExceptions()) {
80 qCWarning(logCategory) << "Exception occurred while calling" << caller;
81 return false;
82 }
83
84 return true;
85}
86
87template <typename... Args>
88inline bool callStaticBooleanMethod(QJniEnvironment& env, jclass cls, jmethodID method, const char* caller,
89 const QLoggingCategory& logCategory, jboolean& result, Args... args)
90{
91 result = env->CallStaticBooleanMethod(cls, method, args...);
92 if (env.checkAndClearExceptions()) {
93 qCWarning(logCategory) << "Exception occurred while calling" << caller;
94 return false;
95 }
96
97 return true;
98}
99} // namespace AndroidInterface
Q_DECLARE_LOGGING_CATEGORY(AndroidSerialLog)
JniLocalRef & operator=(JniLocalRef &&other) noexcept
JniLocalRef & operator=(const JniLocalRef &)=delete
JniLocalRef(const JniLocalRef &)=delete
JniLocalRef(JNIEnv *env, T ref=nullptr)
JniLocalRef(JniLocalRef &&other) noexcept
bool callStaticBooleanMethod(QJniEnvironment &env, jclass cls, jmethodID method, const char *caller, const QLoggingCategory &logCategory, jboolean &result, Args... args)
bool callStaticIntMethod(QJniEnvironment &env, jclass cls, jmethodID method, const char *caller, const QLoggingCategory &logCategory, jint &result, Args... args)
void setKeepScreenOn(bool on)
constexpr const char * kJniQGCActivityClassName