Kirigami-addons

androidintegration.cpp
1/*
2​ * SPDX-FileCopyrightText: 2020 Nicolas Fella <nicolas.fella@gmx.de>
3​ * SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
4​ *
5​ * SPDX-License-Identifier: LGPL-2.0-or-later
6​ */
7
8#include "androidintegration.h"
9
10#include <QCoreApplication>
11#include <QJniObject>
12#include <QDebug>
13
14using namespace KirigamiAddonsDateAndTime;
15
16AndroidIntegration &AndroidIntegration::instance()
17{
18 static AndroidIntegration instance;
19 return instance;
20}
21
22static void dateSelected(JNIEnv *env, jobject that, jint day, jint month, jint year)
23{
24 Q_UNUSED(that);
25 Q_UNUSED(env);
26 Q_EMIT AndroidIntegration::instance().datePickerFinished(true, QDate(year, month, day).startOfDay());
27}
28
29static void dateCancelled(JNIEnv *env, jobject that)
30{
31 Q_UNUSED(that);
32 Q_UNUSED(env);
33 Q_EMIT AndroidIntegration::instance().datePickerFinished(false, {});
34}
35
36static void timeSelected(JNIEnv *env, jobject that, jint hours, jint minutes)
37{
38 Q_UNUSED(that);
39 Q_UNUSED(env);
40 Q_EMIT AndroidIntegration::instance().timePickerFinished(true, QDateTime(QDate::currentDate(), QTime(hours, minutes)));
41}
42
43static void timeCancelled(JNIEnv *env, jobject that)
44{
45 Q_UNUSED(that);
46 Q_UNUSED(env);
47 Q_EMIT AndroidIntegration::instance().timePickerFinished(false, {});
48}
49
50static const JNINativeMethod dateMethods[] = {
51 {"dateSelected", "(III)V", (void *)dateSelected},
52 {"cancelled", "()V", (void *)dateCancelled}
53};
54
55static const JNINativeMethod timeMethods[] = {
56 {"timeSelected", "(II)V", (void *)timeSelected},
57 {"cancelled", "()V", (void *)timeCancelled}
58};
59
60Q_DECL_EXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *)
61{
62 static bool initialized = false;
63 if (initialized) {
64 return JNI_VERSION_1_6;
65 }
66 initialized = true;
67
68 JNIEnv *env = nullptr;
69 if (vm->GetEnv((void **)&env, JNI_VERSION_1_4) != JNI_OK) {
70 qWarning() << "Failed to get JNI environment.";
71 return -1;
72 }
73 jclass theclass = env->FindClass("org/kde/kirigamiaddons/dateandtime/DatePicker");
74 if (env->RegisterNatives(theclass, dateMethods, sizeof(dateMethods) / sizeof(JNINativeMethod)) < 0) {
75 qWarning() << "Failed to register native functions.";
76 return -1;
77 }
78
79 jclass timeclass = env->FindClass("org/kde/kirigamiaddons/dateandtime/TimePicker");
80 if (env->RegisterNatives(timeclass, timeMethods, sizeof(timeMethods) / sizeof(JNINativeMethod)) < 0) {
81 qWarning() << "Failed to register native functions.";
82 return -1;
83 }
84
85 return JNI_VERSION_1_4;
86}
87
88void AndroidIntegration::showDatePicker(qint64 initialDate)
89{
90#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
91 QJniObject picker("org/kde/kirigamiaddons/dateandtime/DatePicker", "(Landroid/app/Activity;J)V", QNativeInterface::QAndroidApplication::context().object<jobject>(), initialDate);
92#else
93 QJniObject picker("org/kde/kirigamiaddons/dateandtime/DatePicker", "(Landroid/app/Activity;J)V", QNativeInterface::QAndroidApplication::context(), initialDate);
94#endif
95 picker.callMethod<void>("doShow");
96}
97
98void AndroidIntegration::showTimePicker(qint64 initialTime)
99{
100#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
101 QJniObject picker("org/kde/kirigamiaddons/dateandtime/TimePicker", "(Landroid/app/Activity;J)V", QNativeInterface::QAndroidApplication::context().object<jobject>(), initialTime);
102#else
103 QJniObject picker("org/kde/kirigamiaddons/dateandtime/TimePicker", "(Landroid/app/Activity;J)V", QNativeInterface::QAndroidApplication::context(), initialTime);
104#endif
105 picker.callMethod<void>("doShow");
106}
107
108#include "moc_androidintegration.cpp"
Interface to native Android date/time pickers.
QDate currentDate()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 27 2024 11:48:17 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.