Commit fd6719ec authored by Volker Krause's avatar Volker Krause
Browse files

Resolve Android content: URLs

This allows opening boarding passes from other apps, such as the
file manager.
parent 8c8afd67
#!/bin/bash
find "$@" -name '*.h' -o -name '*.cpp' -o -name '*.qml' | grep -v /3rdparty/ | while read FILE; do
find "$@" -name '*.h' -o -name '*.cpp' -o -name '*.qml' -o -name '*.java'| grep -v /3rdparty/ | while read FILE; do
if grep -qiE "Copyright \(C\) [0-9, -]{4,} " "$FILE" ; then continue; fi
thisfile=`basename $FILE`
authorName=`git config user.name`
......
......@@ -6,7 +6,7 @@
android:installLocation="auto">
<application android:name="org.qtproject.qt5.android.bindings.QtApplication" android:label="KDE Itinerary" android:icon="@drawable/icon">
<activity android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation"
android:name="org.qtproject.qt5.android.bindings.QtActivity"
android:name="org.kde.itinerary.Activity"
android:label="KDE Itinerary"
android:windowSoftInputMode="adjustResize"
android:launchMode="singleTop">
......
add_library(itinerary STATIC
set(itinerary_srcs
pkpassmanager.cpp
timelinemodel.cpp
)
ecm_qt_declare_logging_category(itinerary_srcs
HEADER logging.h
IDENTIFIER Log
CATEGORY_NAME org.kde.itinerary
)
add_library(itinerary STATIC ${itinerary_srcs})
target_link_libraries(itinerary PUBLIC
KPkPass
)
......
......@@ -16,6 +16,7 @@
*/
#include "itinerary_version.h"
#include "logging.h"
#include "pkpassmanager.h"
#include "timelinemodel.h"
......@@ -33,9 +34,43 @@
#endif
#include <QCommandLineParser>
#include <QDebug>
#include <QGuiApplication>
#include <QIcon>
void handleViewIntent(PkPassManager *passMgr)
{
#ifdef Q_OS_ANDROID
// handle opened files
const auto activity = QtAndroid::androidActivity();
if (!activity.isValid())
return;
const auto intent = activity.callObjectMethod("getIntent", "()Landroid/content/Intent;");
if (!intent.isValid())
return;
const auto uri = intent.callObjectMethod("getData", "()Landroid/net/Uri;");
if (!uri.isValid())
return;
const auto scheme = uri.callObjectMethod("getScheme", "()Ljava/lang/String;");
if (scheme.toString() == QLatin1String("content")) {
const auto tmpFile = activity.callObjectMethod("receiveContent", "(Landroid/net/Uri;)Ljava/lang/String;", uri.object<jobject>());
passMgr->importPass(QUrl::fromLocalFile(tmpFile.toString()));
} else if (scheme.toString() == QLatin1String("file")) {
const auto uriStr = uri.callObjectMethod("toString", "()Ljava/lang/String;");
passMgr->importPass(QUrl(uriStr.toString()));
} else {
const auto uriStr = uri.callObjectMethod("toString", "()Ljava/lang/String;");
qCWarning(Log) << "Unknown intent URI:" << uriStr.toString();
}
#else
Q_UNUSED(passMgr);
#endif
}
#ifdef Q_OS_ANDROID
Q_DECL_EXPORT
#endif
......@@ -73,21 +108,7 @@ int main(int argc, char **argv)
for (const auto &file : parser.positionalArguments())
passMgr.importPass(QUrl::fromLocalFile(file));
#ifdef Q_OS_ANDROID
// handle opened files
const auto activity = QtAndroid::androidActivity();
if (activity.isValid()) {
const auto intent = activity.callObjectMethod("getIntent", "()Landroid/content/Intent;");
if (intent.isValid()) {
const auto data = intent.callObjectMethod("getDataString", "()Ljava/lang/String;");
if (data.isValid()) {
// TODO handle content:// urls
passMgr.importPass(QUrl(data.toString()));
}
}
}
#endif
handleViewIntent(&passMgr);
return app.exec();
}
/*
Copyright (C) 2018 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 <http://www.gnu.org/licenses/>.
*/
package org.kde.itinerary;
import org.qtproject.qt5.android.bindings.QtActivity;
import android.content.ContentResolver;
import android.net.Uri;
import android.util.Log;
import java.io.*;
public class Activity extends QtActivity
{
private static final String TAG = "org.kde.itinerary";
public String receiveContent(Uri uri)
{
ContentResolver resolver = getContentResolver();
File tempFile = new File(getCacheDir(), "content"); // TODO use random name
try {
InputStream is = resolver.openInputStream(uri);
tempFile.createNewFile();
FileOutputStream os = new FileOutputStream(tempFile);
while (true) {
byte[] buffer = new byte[4096];
int size = is.read(buffer, 0, 4096);
if (size < 0) {
break;
}
os.write(buffer, 0, size);
}
os.close();
} catch (Exception e) {
Log.e(TAG, e.toString());
return "";
}
return tempFile.toString();
}
}
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