KPublicTransport

wifimonitor_android.cpp
1/*
2 SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "wifimonitor_p.h"
7#include "logging.h"
8
9#include <QSharedData>
10
11#include <private/qandroidextras_p.h>
12#include <QCoreApplication>
13#include <QJniEnvironment>
14#include <QJniObject>
15
16using namespace KPublicTransport;
17
18namespace KPublicTransport {
19class WifiMonitorBackend : public QSharedData
20{
21public:
22 void setSsid(const QString &ssid);
23 void setStatus(int statusCode);
24
25 std::vector<WifiMonitor*> frontends;
26 WifiMonitor::Status status = WifiMonitor::NotAvailable;
27 QJniObject wifiMonitor;
28};
29}
30
31void WifiMonitorBackend::setSsid(const QString &ssid)
32{
33 for (auto m : frontends) {
34 m->setSsid(ssid);
35 }
36}
37
38void WifiMonitorBackend::setStatus(int statusCode)
39{
40 if (status == statusCode) {
41 return;
42 }
43 status = static_cast<WifiMonitor::Status>(statusCode);
44 for (auto m : frontends) {
45 Q_EMIT m->statusChanged();
46 }
47}
48
49static WifiMonitorBackend *s_backend = nullptr;
50
51static void setSsid(JNIEnv *env, [[maybe_unused]] jobject that, jstring ssid)
52{
53 if (s_backend) {
54 const char *str = env->GetStringUTFChars(ssid, nullptr);
55 s_backend->setSsid(QString::fromUtf8(str));
56 env->ReleaseStringUTFChars(ssid, str);
57 }
58}
59
60static void setStatus([[maybe_unused]] JNIEnv *env, [[maybe_unused]] jobject that, jint statusCode)
61{
62 if (s_backend) {
63 s_backend->setStatus(statusCode);
64 }
65}
66
67static const JNINativeMethod methods[] = {
68 {"setSsid", "(Ljava/lang/String;)V", (void *)setSsid},
69 {"setStatus", "(I)V", (void *)setStatus},
70};
71
72WifiMonitor::WifiMonitor(QObject *parent)
73 : QObject(parent)
74{
75 static bool nativesRegistered = false;
76 if (!nativesRegistered) {
78 jclass cls = env->FindClass("org/kde/publictransport/onboard/WifiMonitor");
79 if (env->RegisterNatives(cls, methods, sizeof(methods) / sizeof(JNINativeMethod)) < 0) {
80 qCWarning(Log) << "Failed to register native functions!";
81 }
82 nativesRegistered = true;
83 }
84
85 if (!s_backend) {
86 s_backend = new WifiMonitorBackend;
87#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
88 s_backend->wifiMonitor = QJniObject("org/kde/publictransport/onboard/WifiMonitor", QNativeInterface::QAndroidApplication::context());
89#else
90 s_backend->wifiMonitor = QJniObject("org/kde/publictransport/onboard/WifiMonitor", "(Landroid/content/Context;)V",
92#endif
93 }
94
95 s_backend->ref.ref();
96 s_backend->frontends.push_back(this);
97}
98
99WifiMonitor::~WifiMonitor()
100{
101 s_backend->ref.deref();
102 if (!s_backend->ref) {
103 delete s_backend;
104 s_backend = nullptr;
105 } else {
106 auto it = std::find(s_backend->frontends.begin(), s_backend->frontends.end(), this);
107 if (it != s_backend->frontends.end()) {
108 s_backend->frontends.erase(it);
109 }
110 }
111}
112
113WifiMonitor::Status WifiMonitor::status() const
114{
115 return s_backend->status;
116}
117
118void WifiMonitor::requestPermissions()
119{
120 // TODO make this properly async
121 QtAndroidPrivate::requestPermission(QStringLiteral("android.permission.ACCESS_FINE_LOCATION"));
122 s_backend->wifiMonitor.callMethod<void>("checkStatus", "()V");
123}
Query operations and data types for accessing realtime public transport information from online servi...
auto callMethod(const char *methodName, Args &&... args) const const
QString fromUtf8(QByteArrayView str)
QFuture< QtAndroidPrivate::PermissionResult > requestPermission(const QString &permission)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:57:19 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.