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
7#include <functional>
8
9namespace AndroidInterface {
10void setNativeMethods();
12QString getSDCardPath();
13void setKeepScreenOn(bool on);
14void openFileImportDialog(const QString& destPath, std::function<void(const QString&)> callback);
15
16constexpr const char* kJniQGCActivityClassName = "org/mavlink/qgroundcontrol/QGCActivity";
17
18template <typename T>
20{
21public:
22 JniLocalRef(JNIEnv* env, T ref = nullptr) : _env(env), _ref(ref)
23 {
24 }
25
27 {
28 reset();
29 }
30
31 JniLocalRef(const JniLocalRef&) = delete;
33
34 JniLocalRef(JniLocalRef&& other) noexcept : _env(other._env), _ref(other._ref)
35 {
36 other._ref = nullptr;
37 }
38
40 {
41 if (this == &other) {
42 return *this;
43 }
44
45 reset();
46 _env = other._env;
47 _ref = other._ref;
48 other._ref = nullptr;
49 return *this;
50 }
51
52 T get() const
53 {
54 return _ref;
55 }
56
57 operator T() const
58 {
59 return _ref;
60 }
61
62 void reset(T ref = nullptr)
63 {
64 if (_env && _ref) {
65 _env->DeleteLocalRef(_ref);
66 }
67 _ref = ref;
68 }
69
70private:
71 JNIEnv* _env = nullptr;
72 T _ref = nullptr;
73};
74
75template <typename... Args>
76inline bool callStaticIntMethod(QJniEnvironment& env, jclass cls, jmethodID method, const char* caller,
77 const QLoggingCategory& logCategory, jint& result, Args... args)
78{
79 result = env->CallStaticIntMethod(cls, method, args...);
80 if (env.checkAndClearExceptions()) {
81 qCWarning(logCategory) << "Exception occurred while calling" << caller;
82 return false;
83 }
84
85 return true;
86}
87
88template <typename... Args>
89inline bool callStaticBooleanMethod(QJniEnvironment& env, jclass cls, jmethodID method, const char* caller,
90 const QLoggingCategory& logCategory, jboolean& result, Args... args)
91{
92 result = env->CallStaticBooleanMethod(cls, method, args...);
93 if (env.checkAndClearExceptions()) {
94 qCWarning(logCategory) << "Exception occurred while calling" << caller;
95 return false;
96 }
97
98 return true;
99}
100} // namespace AndroidInterface
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 openFileImportDialog(const QString &destPath, std::function< void(const QString &)> callback)
void setKeepScreenOn(bool on)
constexpr const char * kJniQGCActivityClassName