File indexing completed on 2022-09-20 16:28:45

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