Commit e2febbde authored by Volker Krause's avatar Volker Krause

Factor out android.net.Uri conversion methods

parent a6641dea
......@@ -47,6 +47,7 @@
#ifdef Q_OS_ANDROID
#include <kandroidextras/contentresolver.h>
#include <kandroidextras/uri.h>
#include <QtAndroid>
#include <QAndroidJniObject>
......@@ -181,12 +182,7 @@ void ApplicationController::importFromIntent(const QAndroidJniObject &intent)
}
const auto uri = intent.callObjectMethod("getData", "()Landroid/net/Uri;");
if (!uri.isValid()) {
return;
}
const auto uriStr = uri.callObjectMethod("toString", "()Ljava/lang/String;");
importFromUrl(QUrl(uriStr.toString()));
importFromUrl(KAndroidExtras::Uri::toUrl(uri));
}
#endif
......@@ -489,11 +485,7 @@ void ApplicationController::addDocument(const QString &batchId)
return;
}
const auto uri = intent.callObjectMethod("getData", "()Landroid/net/Uri;");
if (!uri.isValid()) {
return;
}
const auto uriStr = uri.callObjectMethod("toString", "()Ljava/lang/String;");
addDocument(batchId, QUrl(uriStr.toString()));
addDocument(batchId, KAndroidExtras::Uri::toUrl(uri));
}));
#endif
}
......
add_library(KAndroidExtras STATIC
contentresolver.cpp
uri.cpp
)
target_include_directories(KAndroidExtras INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>")
target_link_libraries(KAndroidExtras PRIVATE Qt5::AndroidExtras)
......@@ -16,6 +16,7 @@
*/
#include "contentresolver.h"
#include "uri.h"
#include <QtAndroid>
#include <QAndroidJniObject>
......@@ -33,7 +34,7 @@ QAndroidJniObject ContentResolver::get()
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>());
const auto uri = Uri::fromUrl(url);
auto mt = cs.callObjectMethod("getType", "(Landroid/net/Uri;)Ljava/lang/String;", uri.object<jobject>());
return mt.toString();
}
......@@ -41,7 +42,7 @@ QString ContentResolver::mimeType(const QUrl &url)
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>());
const auto uri = Uri::fromUrl(url);
// 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);
......
/*
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 "uri.h"
#include <QAndroidJniObject>
#include <QUrl>
using namespace KAndroidExtras;
QAndroidJniObject Uri::fromUrl(const QUrl &url)
{
return QAndroidJniObject::callStaticObjectMethod("android/net/Uri", "parse", "(Ljava/lang/String;)Landroid/net/Uri;",
QAndroidJniObject::fromString(url.toString()).object<jstring>());
}
QUrl Uri::toUrl(const QAndroidJniObject &uri)
{
if (!uri.isValid()) {
return QUrl();
}
return QUrl(uri.callObjectMethod("toString", "()Ljava/lang/String;").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_URI_H
#define KANDROIDEXTRAS_URI_H
class QAndroidJniObject;
class QUrl;
namespace KAndroidExtras {
/** Conversion methods for android.net.Uri. */
namespace Uri
{
/** Create an android.net.Uri from a QUrl. */
QAndroidJniObject fromUrl(const QUrl &url);
/** Convert a android.net.Uri to a QUrl. */
QUrl toUrl(const QAndroidJniObject &uri);
};
}
#endif // KANDROIDEXTRAS_URI_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