File indexing completed on 2024-05-19 04:56:13

0001 /**
0002  * \file androidutils.cpp
0003  * Platform utility functions for Android.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 27 Feb 2019
0008  *
0009  * Copyright (C) 2019  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "androidutils.h"
0028 #ifdef Q_OS_ANDROID
0029 #include <jni.h>
0030 #if QT_VERSION >= 0x060000
0031 #include <QCoreApplication>
0032 #include <QJniObject>
0033 #else
0034 #include <QtAndroid>
0035 #include <QAndroidJniObject>
0036 #endif
0037 #endif
0038 
0039 AndroidUtils* AndroidUtils::s_self = nullptr;
0040 
0041 AndroidUtils::AndroidUtils(QObject* parent) : QObject(parent)
0042 {
0043   Q_ASSERT_X(!s_self, "AndroidUtils", "there should be only one instance");
0044   s_self = this;
0045 }
0046 
0047 void AndroidUtils::checkPendingIntents()
0048 {
0049 #ifdef Q_OS_ANDROID
0050 #if QT_VERSION >= 0x060000
0051   QJniObject activity = QNativeInterface::QAndroidApplication::context();
0052 #else
0053   QAndroidJniObject activity = QtAndroid::androidActivity();
0054 #endif
0055   if (activity.isValid()) {
0056     activity.callMethod<void>("checkPendingIntents");
0057   }
0058 #endif
0059 }
0060 
0061 void AndroidUtils::emitFilePathReceived(const QString& path)
0062 {
0063   emit filePathReceived(path);
0064 }
0065 
0066 
0067 #ifdef Q_OS_ANDROID
0068 #ifdef __cplusplus
0069 extern "C" {
0070 #endif
0071 
0072 JNIEXPORT void JNICALL Java_net_sourceforge_kid3_Kid3Activity_setFilePathFromIntent(
0073     JNIEnv* env, jobject obj, jstring path)
0074 {
0075   Q_UNUSED(obj)
0076   const char* pathStr = env->GetStringUTFChars(path, NULL);
0077   if (AndroidUtils* utils = AndroidUtils::instance()) {
0078     utils->emitFilePathReceived(QString::fromUtf8(pathStr));
0079   }
0080   env->ReleaseStringUTFChars(path, pathStr);
0081 }
0082 
0083 #ifdef __cplusplus
0084 }
0085 #endif
0086 #endif