File indexing completed on 2024-11-24 04:42:07
0001 /* 0002 SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "contentresolver.h" 0008 #include "openablecolumns.h" 0009 #include "uri.h" 0010 0011 #include <KAndroidExtras/AndroidTypes> 0012 #include <KAndroidExtras/JniSignature> 0013 0014 #include <QCoreApplication> 0015 #include <QString> 0016 #include <QUrl> 0017 0018 using namespace KAndroidExtras; 0019 0020 QJniObject ContentResolver::get() 0021 { 0022 const QJniObject context = QNativeInterface::QAndroidApplication::context(); 0023 return context.callObjectMethod("getContentResolver", Jni::signature<android::content::ContentResolver()>()); 0024 } 0025 0026 QString ContentResolver::mimeType(const QUrl &url) 0027 { 0028 auto cs = ContentResolver::get(); 0029 const auto uri = Uri::fromUrl(url); 0030 auto mt = cs.callObjectMethod("getType", Jni::signature<java::lang::String(android::net::Uri)>(), uri.object<jobject>()); 0031 return mt.toString(); 0032 } 0033 0034 QString ContentResolver::fileName(const QUrl &url) 0035 { 0036 auto cs = ContentResolver::get(); 0037 const auto uri = Uri::fromUrl(url); 0038 0039 // query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) 0040 auto cursor = cs.callObjectMethod("query", Jni::signature<android::database::Cursor(android::net::Uri, java::lang::String[], java::lang::String, java::lang::String[], java::lang::String)>(), uri.object<jobject>(), 0, 0, 0, 0); 0041 0042 const QJniObject DISPLAY_NAME = OpenableColumns::DISPLAY_NAME; 0043 const auto nameIndex = cursor.callMethod<jint>("getColumnIndex", (const char*)Jni::signature<int(java::lang::String)>(), DISPLAY_NAME.object()); 0044 cursor.callMethod<jboolean>("moveToFirst", (const char*)Jni::signature<bool()>()); 0045 return cursor.callObjectMethod("getString", Jni::signature<java::lang::String(int)>(), nameIndex).toString(); 0046 }