Commit 187c4740 authored by Nicolas Fella's avatar Nicolas Fella

[android] Use filedialog from Qt

parent 13773d51
......@@ -11,6 +11,10 @@ project(okular VERSION 1.8.${KDE_APPLICATIONS_VERSION_MICRO})
set(QT_REQUIRED_VERSION "5.9.0")
set(KF5_REQUIRED_VERSION "5.44.0")
if (ANDROID)
set(QT_REQUIRED_VERSION "5.13.0")
endif()
find_package(ECM 5.33.0 CONFIG REQUIRED)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${ECM_MODULE_PATH})
......@@ -40,6 +44,10 @@ else()
add_definitions(-DHAVE_SPEECH)
endif()
if(ANDROID)
find_package(Qt5 ${QT_REQUIRED_VERSION} CONFIG REQUIRED COMPONENTS AndroidExtras)
endif()
if(NOT CMAKE_VERSION VERSION_LESS "3.10.0")
# CMake 3.9+ warns about automoc on files without Q_OBJECT, and doesn't know about other macros.
# 3.10+ lets us provide more macro names that require automoc.
......
......@@ -8,6 +8,8 @@ import android.os.ParcelFileDescriptor;
import android.net.Uri;
import android.app.Activity;
import java.io.FileNotFoundException;
import org.qtproject.qt5.android.bindings.QtActivity;
class FileClass
......@@ -17,6 +19,20 @@ class FileClass
public class OpenFileActivity extends QtActivity
{
public String contentUrlToFd(String url)
{
try {
ContentResolver resolver = getBaseContext().getContentResolver();
ParcelFileDescriptor fdObject = resolver.openFileDescriptor(Uri.parse(url), "r");
return "fd:///" + fdObject.detachFd();
} catch (FileNotFoundException e) {
Log.e("Okular", "Cannot find file", e);
}
return "";
}
private void displayUri(Uri uri)
{
if (uri == null)
......@@ -51,27 +67,4 @@ public class OpenFileActivity extends QtActivity
displayUri(bundleIntent.getData());
}
}
private static int OpenDocumentRequest = 42;
public static void openFile(Activity context, String title, String mimes)
{
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("application/pdf");
Log.v("Okular", "opening: " + mimes);
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimes.split(";"));
context.startActivityForResult(intent, OpenDocumentRequest);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Log.v("Okular", "Activity Result: " + String.valueOf(requestCode) + " with code: " + String.valueOf(resultCode));
if (resultCode == RESULT_OK && requestCode == OpenDocumentRequest) {
Uri uri = intent.getData();
Log.v("Okular", "Opening document: " + uri.toString());
displayUri(uri);
}
}
}
......@@ -45,27 +45,9 @@ Kirigami.ApplicationWindow {
Kirigami.Action {
text: i18n("Open...")
icon.name: "document-open"
visible: Qt.platform.os !== "android"
onTriggered: {
fileDialog.open()
}
},
Kirigami.Action {
text: i18n("Open...")
icon.name: "document-open"
visible: p0.enabled
readonly property var p0: Connections {
target: AndroidInstance
enabled: AndroidInstance.hasOwnProperty("openFile")
onOpenUri: {
console.log("open uri!", uri)
documentItem.url = uri
}
}
onTriggered: {
// var mimetypes = Okular.Okular.mimeTypes.join(",")
AndroidInstance.openFile(i18n("Document to open..."), "*/*")
}
}
]
}
......
......@@ -37,6 +37,10 @@ target_link_libraries(okularplugin
okularcore
)
if(ANDROID)
target_link_libraries(okularplugin Qt5::AndroidExtras)
endif()
install(TARGETS okularplugin DESTINATION ${KDE_INSTALL_QMLDIR}/org/kde/okular)
install(FILES qmldir DESTINATION ${KDE_INSTALL_QMLDIR}/org/kde/okular)
install(FILES DocumentView.qml DESTINATION ${KDE_INSTALL_QMLDIR}/org/kde/okular)
......
......@@ -22,6 +22,11 @@
#include <QtQml> // krazy:exclude=includes
#include <QMimeDatabase>
#ifdef Q_OS_ANDROID
#include <QAndroidJniObject>
#include <QtAndroid>
#endif
#include <core/document_p.h>
#include <core/page.h>
#include <core/bookmarkmanager.h>
......@@ -59,9 +64,18 @@ void DocumentItem::setUrl(const QUrl & url)
//TODO: password
QMimeDatabase db;
const QString path = url.isLocalFile() ? url.toLocalFile() : QStringLiteral("-");
QUrl realUrl = url;
#ifdef Q_OS_ANDROID
realUrl = QUrl(QtAndroid::androidActivity().callObjectMethod("contentUrlToFd",
"(Ljava/lang/String;)Ljava/lang/String;",
QAndroidJniObject::fromString(url.toString()).object<jstring>()
).toString());
#endif
const QString path = realUrl.isLocalFile() ? realUrl.toLocalFile() : QStringLiteral("-");
m_document->openDocument(path, url, db.mimeTypeForUrl(url));
m_document->openDocument(path, realUrl, db.mimeTypeForUrl(realUrl));
m_tocModel->clear();
m_tocModel->fill(m_document->documentSynopsis());
......
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