2021-05-24 07:25:56 +00:00
/*
SPDX - FileCopyrightText : 2018 Aleix Pol < aleixpol @ kde . org >
SPDX - License - Identifier : GPL - 2.0 - or - later
*/
2018-05-17 17:52:01 +00:00
# include "android.h"
2018-05-17 23:44:18 +00:00
# include <QAndroidJniEnvironment>
# include <QAndroidJniObject>
# include <QDebug>
# include <QStringList>
2019-01-19 21:25:14 +00:00
# include <QtAndroid>
2018-05-17 23:44:18 +00:00
2019-01-19 21:25:14 +00:00
URIHandler URIHandler : : handler ;
2018-05-17 23:44:18 +00:00
static AndroidInstance * s_instance = nullptr ;
void AndroidInstance : : openFile ( const QString & title , const QStringList & mimes )
{
s_instance = this ;
QAndroidJniObject activity = QAndroidJniObject : : callStaticObjectMethod ( " org/qtproject/qt5/android/QtNative " , " activity " , " ()Landroid/app/Activity; " ) ; // activity is valid
Q_ASSERT ( activity . isValid ( ) ) ;
QAndroidJniEnvironment _env ;
QAndroidJniObject : : callStaticMethod < void > ( " org/kde/something/OpenFileActivity " ,
" openFile " ,
" (Landroid/app/Activity;Ljava/lang/String;Ljava/lang/String;)V " ,
activity . object < jobject > ( ) ,
QAndroidJniObject : : fromString ( title ) . object < jstring > ( ) ,
2022-04-02 21:21:33 +00:00
QAndroidJniObject : : fromString ( mimes . join ( QLatin1Char ( ' ; ' ) ) ) . object < jstring > ( ) ) ;
2018-05-17 23:44:18 +00:00
if ( _env - > ExceptionCheck ( ) ) {
_env - > ExceptionClear ( ) ;
qWarning ( ) < < " couldn't launch intent " ;
}
}
2018-05-17 17:52:01 +00:00
2019-01-19 21:25:14 +00:00
void AndroidInstance : : handleViewIntent ( )
{
QtAndroid : : androidActivity ( ) . callMethod < void > ( " handleViewIntent " , " ()V " ) ;
}
2018-05-17 17:52:01 +00:00
void Java_org_kde_something_FileClass_openUri ( JNIEnv * env , jobject /*obj*/ , jstring uri )
{
jboolean isCopy = false ;
const char * utf = env - > GetStringUTFChars ( uri , & isCopy ) ;
2018-05-17 23:44:18 +00:00
const QString uriString = QString : : fromUtf8 ( utf ) ;
if ( s_instance )
s_instance - > openUri ( QUrl ( uriString ) ) ;
else
2019-01-19 21:25:14 +00:00
URIHandler : : handler . openUri ( uriString ) ;
2018-05-17 17:52:01 +00:00
env - > ReleaseStringUTFChars ( uri , utf ) ;
}