Commit a6641dea authored by Volker Krause's avatar Volker Krause

Retrieve file name and mime type from content: URLs

Now shows meaningful data when adding a document. Also, start a separate
library for generic Android code, so we don't mix that into the application
code.
parent 4a63b6a2
add_definitions(-DTRANSLATION_DOMAIN=\"kde-itinerary\")
add_subdirectory(weather)
if (ANDROID)
add_subdirectory(kandroidextras)
endif()
add_subdirectory(app)
......@@ -84,7 +84,7 @@ if (ANDROID)
target_include_directories(itinerary-app PRIVATE ${Qt5Core_PRIVATE_INCLUDE_DIRS})
# explicitly add runtime dependencies and transitive link dependencies,
# so androiddeployqt picks them up
target_link_libraries(itinerary PUBLIC Qt5::AndroidExtras)
target_link_libraries(itinerary PUBLIC Qt5::AndroidExtras KAndroidExtras)
target_link_libraries(itinerary-app PRIVATE
KF5::Archive
KF5::Kirigami2
......
......@@ -46,6 +46,8 @@
#include <QUrlQuery>
#ifdef Q_OS_ANDROID
#include <kandroidextras/contentresolver.h>
#include <QtAndroid>
#include <QAndroidJniObject>
#else
......@@ -503,10 +505,14 @@ void ApplicationController::addDocument(const QString &batchId, const QUrl &url)
DocumentUtil::addDocumentId(res, docId);
DigitalDocument docInfo;
docInfo.setName(url.fileName());
#ifdef Q_OS_ANDROID
docInfo.setEncodingFormat(KAndroidExtras::ContentResolver::mimeType(url));
docInfo.setName(KAndroidExtras::ContentResolver::fileName(url));
#else
QMimeDatabase db;
docInfo.setEncodingFormat(db.mimeTypeForFile(url.isLocalFile() ? url.toLocalFile() : url.toString()).name());
docInfo.setName(url.fileName());
#endif
m_docMgr->addDocument(docId, docInfo, url.isLocalFile() ? url.toLocalFile() : url.toString());
......
add_library(KAndroidExtras STATIC
contentresolver.cpp
)
target_include_directories(KAndroidExtras INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>")
target_link_libraries(KAndroidExtras PRIVATE Qt5::AndroidExtras)
/*
Copyright (C) 2019 Volker Krause <vkrause@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "contentresolver.h"
#include <QtAndroid>
#include <QAndroidJniObject>
#include <QString>
#include <QUrl>
using namespace KAndroidExtras;
QAndroidJniObject ContentResolver::get()
{
return QtAndroid::androidContext().callObjectMethod("getContentResolver", "()Landroid/content/ContentResolver;");
}
QString ContentResolver::mimeType(const QUrl &url)
{
auto cs = ContentResolver::get();
auto uri = QAndroidJniObject::callStaticObjectMethod("android/net/Uri", "parse", "(Ljava/lang/String;)Landroid/net/Uri;", QAndroidJniObject::fromString(url.toString()).object<jstring>());
auto mt = cs.callObjectMethod("getType", "(Landroid/net/Uri;)Ljava/lang/String;", uri.object<jobject>());
return mt.toString();
}
QString ContentResolver::fileName(const QUrl &url)
{
auto cs = ContentResolver::get();
auto uri = QAndroidJniObject::callStaticObjectMethod("android/net/Uri", "parse", "(Ljava/lang/String;)Landroid/net/Uri;", QAndroidJniObject::fromString(url.toString()).object<jstring>());
// query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
auto cursor = cs.callObjectMethod("query", "(Landroid/net/Uri;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;", uri.object<jobject>(), 0, 0, 0, 0);
const auto DISPLAY_NAME = QAndroidJniObject::getStaticObjectField<jstring>("android/provider/OpenableColumns", "DISPLAY_NAME");
const auto nameIndex = cursor.callMethod<jint>("getColumnIndex", "(Ljava/lang/String;)I", DISPLAY_NAME.object<jobject>());
cursor.callMethod<jboolean>("moveToFirst", "()Z");
return cursor.callObjectMethod("getString", "(I)Ljava/lang/String;", nameIndex).toString();
}
/*
Copyright (C) 2019 Volker Krause <vkrause@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef KANDROIDEXTRAS_CONTENTRESOLVER_H
#define KANDROIDEXTRAS_CONTENTRESOLVER_H
class QAndroidJniObject;
class QString;
class QUrl;
namespace KAndroidExtras {
/** Methods for working with Android's ContentResolver. */
namespace ContentResolver
{
/** Get the JNI content resolver. */
QAndroidJniObject get();
/** Returns the mime type of the given content: URL.
* @see Android ContentResolver.getType
*/
QString mimeType(const QUrl &url);
/** File name of a file provided by a content: URL. */
QString fileName(const QUrl &url);
};
}
#endif // KANDROIDEXTRAS_CONTENTRESOLVER_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment