File indexing completed on 2024-04-21 16:33:24

0001 /**
0002  * SPDX-FileCopyrightText: 2019 Nicolas Fella <nicolas.fella@gmx.de>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "androidutils.h"
0008 
0009 #include <QCoreApplication>
0010 #include <QDateTime>
0011 #include <QDebug>
0012 #include <QJniObject>
0013 
0014 #define JSTRING(s) QAndroidJniObject::fromString(s).object<jstring>()
0015 
0016 AndroidUtils *AndroidUtils::s_instance = nullptr;
0017 
0018 AndroidUtils *AndroidUtils::instance()
0019 {
0020     if (!s_instance) {
0021         s_instance = new AndroidUtils();
0022     }
0023 
0024     return s_instance;
0025 }
0026 
0027 static void dateSelected(JNIEnv *env, jobject that, jstring data)
0028 {
0029     Q_UNUSED(that);
0030     AndroidUtils::instance()->_dateSelected(QString::fromUtf8(env->GetStringUTFChars(data, nullptr)));
0031 }
0032 
0033 static void dateCancelled(JNIEnv *env, jobject that)
0034 {
0035     Q_UNUSED(that);
0036     Q_UNUSED(env);
0037     AndroidUtils::instance()->_dateCancelled();
0038 }
0039 
0040 static void timeSelected(JNIEnv *env, jobject that, jstring data)
0041 {
0042     Q_UNUSED(that);
0043     AndroidUtils::instance()->_timeSelected(QString::fromUtf8(env->GetStringUTFChars(data, nullptr)));
0044 }
0045 
0046 static void timeCancelled(JNIEnv *env, jobject that)
0047 {
0048     Q_UNUSED(that);
0049     Q_UNUSED(env);
0050     AndroidUtils::instance()->_timeCancelled();
0051 }
0052 
0053 static const JNINativeMethod methods[] = {{"dateSelected", "(Ljava/lang/String;)V", (void *)dateSelected}, {"cancelled", "()V", (void *)dateCancelled}};
0054 
0055 static const JNINativeMethod timeMethods[] = {{"timeSelected", "(Ljava/lang/String;)V", (void *)timeSelected}, {"cancelled", "()V", (void *)timeCancelled}};
0056 
0057 Q_DECL_EXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *)
0058 {
0059     static bool initialized = false;
0060     if (initialized)
0061         return JNI_VERSION_1_6;
0062     initialized = true;
0063 
0064     JNIEnv *env = nullptr;
0065     if (vm->GetEnv((void **)&env, JNI_VERSION_1_4) != JNI_OK) {
0066         qWarning() << "Failed to get JNI environment.";
0067         return -1;
0068     }
0069     jclass theclass = env->FindClass("org/kde/ktrip/DatePicker");
0070     if (env->RegisterNatives(theclass, methods, sizeof(methods) / sizeof(JNINativeMethod)) < 0) {
0071         qWarning() << "Failed to register native functions.";
0072         return -1;
0073     }
0074 
0075     jclass timeclass = env->FindClass("org/kde/ktrip/TimePicker");
0076     if (env->RegisterNatives(timeclass, timeMethods, sizeof(timeMethods) / sizeof(JNINativeMethod)) < 0) {
0077         qWarning() << "Failed to register native functions.";
0078         return -1;
0079     }
0080 
0081     return JNI_VERSION_1_4;
0082 }
0083 
0084 void AndroidUtils::showDatePicker()
0085 {
0086     QJniObject picker("org/kde/ktrip/DatePicker",
0087                       "(Landroid/app/Activity;J)V",
0088                       QNativeInterface::QAndroidApplication::context(),
0089                       QDateTime::currentDateTime().toMSecsSinceEpoch());
0090     picker.callMethod<void>("doShow");
0091 }
0092 
0093 void AndroidUtils::_dateSelected(const QString &data)
0094 {
0095     Q_EMIT datePickerFinished(true, QDate::fromString(data, QStringLiteral("yyyy-MM-dd")));
0096 }
0097 
0098 void AndroidUtils::_dateCancelled()
0099 {
0100     Q_EMIT datePickerFinished(false, QDate());
0101 }
0102 
0103 void AndroidUtils::showTimePicker()
0104 {
0105     QJniObject picker("org/kde/ktrip/TimePicker",
0106                       "(Landroid/app/Activity;J)V",
0107                       QNativeInterface::QAndroidApplication::context(),
0108                       QDateTime::currentDateTime().toMSecsSinceEpoch());
0109     picker.callMethod<void>("doShow");
0110 }
0111 
0112 void AndroidUtils::_timeSelected(const QString &data)
0113 {
0114     Q_EMIT timePickerFinished(true, QTime::fromString(data, QStringLiteral("HH:mm")));
0115 }
0116 
0117 void AndroidUtils::_timeCancelled()
0118 {
0119     Q_EMIT timePickerFinished(false, QTime());
0120 }