mirror of
https://github.com/tumic0/GPXSee.git
synced 2025-06-26 19:19:16 +02:00
Android port
This commit is contained in:
@ -19,65 +19,97 @@
|
||||
#define TYP_FILE "style.typ"
|
||||
#define RENDERTHEME_FILE "style.xml"
|
||||
|
||||
#ifdef Q_OS_ANDROID
|
||||
#define DATA_LOCATION QStandardPaths::GenericDataLocation
|
||||
#else // Q_OS_ANDROID
|
||||
#define DATA_LOCATION QStandardPaths::AppDataLocation
|
||||
#endif // Q_OS_ANDROID
|
||||
|
||||
#ifdef Q_OS_ANDROID
|
||||
static QString assetsPath(const QString &path, const QString &dir)
|
||||
{
|
||||
QDir pd(path);
|
||||
|
||||
if (pd.isAbsolute() && pd.exists())
|
||||
return pd.absolutePath();
|
||||
else
|
||||
return QString("assets://") + dir;
|
||||
}
|
||||
#endif // Q_OS_ANDROID
|
||||
|
||||
QString ProgramPaths::mapDir(bool writable)
|
||||
{
|
||||
if (writable)
|
||||
return QDir(QStandardPaths::writableLocation(
|
||||
QStandardPaths::AppDataLocation)).filePath(MAP_DIR);
|
||||
return QDir(QStandardPaths::writableLocation(DATA_LOCATION))
|
||||
.filePath(MAP_DIR);
|
||||
else
|
||||
return QStandardPaths::locate(QStandardPaths::AppDataLocation,
|
||||
#ifdef Q_OS_ANDROID
|
||||
return assetsPath(QStandardPaths::locate(DATA_LOCATION, MAP_DIR,
|
||||
QStandardPaths::LocateDirectory), MAP_DIR);
|
||||
#else // Q_OS_ANDROID
|
||||
return QStandardPaths::locate(DATA_LOCATION,
|
||||
MAP_DIR, QStandardPaths::LocateDirectory);
|
||||
#endif // Q_OS_ANDROID
|
||||
}
|
||||
|
||||
QString ProgramPaths::poiDir(bool writable)
|
||||
{
|
||||
if (writable)
|
||||
return QDir(QStandardPaths::writableLocation(
|
||||
QStandardPaths::AppDataLocation)).filePath(POI_DIR);
|
||||
return QDir(QStandardPaths::writableLocation(DATA_LOCATION))
|
||||
.filePath(POI_DIR);
|
||||
else
|
||||
return QStandardPaths::locate(QStandardPaths::AppDataLocation,
|
||||
POI_DIR, QStandardPaths::LocateDirectory);
|
||||
return QStandardPaths::locate(DATA_LOCATION, POI_DIR,
|
||||
QStandardPaths::LocateDirectory);
|
||||
}
|
||||
|
||||
QString ProgramPaths::csvDir(bool writable)
|
||||
{
|
||||
if (writable)
|
||||
return QDir(QStandardPaths::writableLocation(
|
||||
QStandardPaths::AppDataLocation)).filePath(CSV_DIR);
|
||||
return QDir(QStandardPaths::writableLocation(DATA_LOCATION))
|
||||
.filePath(CSV_DIR);
|
||||
else
|
||||
return QStandardPaths::locate(QStandardPaths::AppDataLocation,
|
||||
CSV_DIR, QStandardPaths::LocateDirectory);
|
||||
#ifdef Q_OS_ANDROID
|
||||
return assetsPath(QStandardPaths::locate(DATA_LOCATION, CSV_DIR,
|
||||
QStandardPaths::LocateDirectory), CSV_DIR);
|
||||
#else // Q_OS_ANDROID
|
||||
return QStandardPaths::locate(DATA_LOCATION, CSV_DIR,
|
||||
QStandardPaths::LocateDirectory);
|
||||
#endif // Q_OS_ANDROID
|
||||
}
|
||||
|
||||
QString ProgramPaths::demDir(bool writable)
|
||||
{
|
||||
if (writable)
|
||||
return QDir(QStandardPaths::writableLocation(
|
||||
QStandardPaths::AppDataLocation)).filePath(DEM_DIR);
|
||||
return QDir(QStandardPaths::writableLocation(DATA_LOCATION))
|
||||
.filePath(DEM_DIR);
|
||||
else
|
||||
return QStandardPaths::locate(QStandardPaths::AppDataLocation,
|
||||
DEM_DIR, QStandardPaths::LocateDirectory);
|
||||
return QStandardPaths::locate(DATA_LOCATION, DEM_DIR,
|
||||
QStandardPaths::LocateDirectory);
|
||||
}
|
||||
|
||||
QString ProgramPaths::styleDir(bool writable)
|
||||
{
|
||||
if (writable)
|
||||
return QDir(QStandardPaths::writableLocation(
|
||||
QStandardPaths::AppDataLocation)).filePath(STYLE_DIR);
|
||||
return QDir(QStandardPaths::writableLocation(DATA_LOCATION))
|
||||
.filePath(STYLE_DIR);
|
||||
else
|
||||
return QStandardPaths::locate(QStandardPaths::AppDataLocation,
|
||||
STYLE_DIR, QStandardPaths::LocateDirectory);
|
||||
return QStandardPaths::locate(DATA_LOCATION, STYLE_DIR,
|
||||
QStandardPaths::LocateDirectory);
|
||||
}
|
||||
|
||||
QString ProgramPaths::symbolsDir(bool writable)
|
||||
{
|
||||
if (writable)
|
||||
return QDir(QStandardPaths::writableLocation(
|
||||
QStandardPaths::AppDataLocation)).filePath(SYMBOLS_DIR);
|
||||
return QDir(QStandardPaths::writableLocation(DATA_LOCATION))
|
||||
.filePath(SYMBOLS_DIR);
|
||||
else
|
||||
return QStandardPaths::locate(QStandardPaths::AppDataLocation,
|
||||
SYMBOLS_DIR, QStandardPaths::LocateDirectory);
|
||||
#ifdef Q_OS_ANDROID
|
||||
return assetsPath(QStandardPaths::locate(DATA_LOCATION, SYMBOLS_DIR,
|
||||
QStandardPaths::LocateDirectory), SYMBOLS_DIR);
|
||||
#else // Q_OS_ANDROID
|
||||
return QStandardPaths::locate(DATA_LOCATION, SYMBOLS_DIR,
|
||||
QStandardPaths::LocateDirectory);
|
||||
#endif // Q_OS_ANDROID
|
||||
}
|
||||
|
||||
QString ProgramPaths::tilesDir()
|
||||
@ -88,36 +120,31 @@ QString ProgramPaths::tilesDir()
|
||||
|
||||
QString ProgramPaths::translationsDir()
|
||||
{
|
||||
return QStandardPaths::locate(QStandardPaths::AppDataLocation,
|
||||
TRANSLATIONS_DIR, QStandardPaths::LocateDirectory);
|
||||
return QStandardPaths::locate(DATA_LOCATION, TRANSLATIONS_DIR,
|
||||
QStandardPaths::LocateDirectory);
|
||||
}
|
||||
|
||||
QString ProgramPaths::ellipsoidsFile()
|
||||
{
|
||||
return QStandardPaths::locate(QStandardPaths::AppDataLocation,
|
||||
CSV_DIR "/" ELLIPSOID_FILE, QStandardPaths::LocateFile);
|
||||
return QDir(csvDir()).filePath(ELLIPSOID_FILE);
|
||||
}
|
||||
|
||||
QString ProgramPaths::gcsFile()
|
||||
{
|
||||
return QStandardPaths::locate(QStandardPaths::AppDataLocation,
|
||||
CSV_DIR "/" GCS_FILE, QStandardPaths::LocateFile);
|
||||
return QDir(csvDir()).filePath(GCS_FILE);
|
||||
}
|
||||
|
||||
QString ProgramPaths::pcsFile()
|
||||
{
|
||||
return QStandardPaths::locate(QStandardPaths::AppDataLocation,
|
||||
CSV_DIR "/" PCS_FILE, QStandardPaths::LocateFile);
|
||||
return QDir(csvDir()).filePath(PCS_FILE);
|
||||
}
|
||||
|
||||
QString ProgramPaths::typFile()
|
||||
{
|
||||
return QStandardPaths::locate(QStandardPaths::AppDataLocation,
|
||||
STYLE_DIR "/" TYP_FILE, QStandardPaths::LocateFile);
|
||||
return QDir(styleDir()).filePath(TYP_FILE);
|
||||
}
|
||||
|
||||
QString ProgramPaths::renderthemeFile()
|
||||
{
|
||||
return QStandardPaths::locate(QStandardPaths::AppDataLocation,
|
||||
STYLE_DIR "/" RENDERTHEME_FILE, QStandardPaths::LocateFile);
|
||||
return QDir(styleDir()).filePath(RENDERTHEME_FILE);
|
||||
}
|
||||
|
@ -1,9 +1,71 @@
|
||||
#include <cctype>
|
||||
#include <cmath>
|
||||
#include <QFileInfo>
|
||||
#ifdef Q_OS_ANDROID
|
||||
#include <QUrl>
|
||||
#include <QCoreApplication>
|
||||
#include <QJniEnvironment>
|
||||
#include <QJniObject>
|
||||
#endif // Q_OS_ANDROID
|
||||
#include "util.h"
|
||||
|
||||
|
||||
#ifdef Q_OS_ANDROID
|
||||
static QString documentName(const QString &path)
|
||||
{
|
||||
QJniEnvironment env;
|
||||
|
||||
QJniObject urlString = QJniObject::fromString(path);
|
||||
QJniObject uri = QJniObject::callStaticObjectMethod("android/net/Uri",
|
||||
"parse", "(Ljava/lang/String;)Landroid/net/Uri;",
|
||||
urlString.object<jstring>());
|
||||
if (!uri.isValid()) {
|
||||
env->ExceptionClear();
|
||||
return QString();
|
||||
}
|
||||
QJniObject context = QNativeInterface::QAndroidApplication::context();
|
||||
if (!context.isValid()) {
|
||||
env->ExceptionClear();
|
||||
return QString();
|
||||
}
|
||||
QJniObject contentResolver = context.callObjectMethod(
|
||||
"getContentResolver", "()Landroid/content/ContentResolver;");
|
||||
if (!contentResolver.isValid()) {
|
||||
env->ExceptionClear();
|
||||
return QString();
|
||||
}
|
||||
QJniObject columnName = QJniObject::getStaticObjectField<jstring>(
|
||||
"android/provider/MediaStore$MediaColumns", "DISPLAY_NAME");
|
||||
if (!columnName.isValid()) {
|
||||
env->ExceptionClear();
|
||||
return QString();
|
||||
}
|
||||
jobjectArray stringArray = env->NewObjectArray(
|
||||
1, env->FindClass("java/lang/String"), 0);
|
||||
env->SetObjectArrayElement(stringArray, 0, columnName.object<jstring>());
|
||||
QJniObject cursor = contentResolver.callObjectMethod("query",
|
||||
"(Landroid/net/Uri;[Ljava/lang/String;Landroid/os/Bundle;"
|
||||
"Landroid/os/CancellationSignal;)Landroid/database/Cursor;",
|
||||
uri.object(), stringArray, 0, 0);
|
||||
if (!cursor.isValid()) {
|
||||
env->ExceptionClear();
|
||||
return QString();
|
||||
}
|
||||
if (!cursor.callMethod<jboolean>("moveToFirst")) {
|
||||
env->ExceptionClear();
|
||||
return QString();
|
||||
}
|
||||
QJniObject str = cursor.callObjectMethod("getString",
|
||||
"(I)Ljava/lang/String;", 0);
|
||||
if (!str.isValid()) {
|
||||
env->ExceptionClear();
|
||||
return QString();
|
||||
}
|
||||
|
||||
return str.toString();
|
||||
}
|
||||
#endif // Q_OS_ANDROID
|
||||
|
||||
int Util::str2int(const char *str, int len)
|
||||
{
|
||||
int res = 0;
|
||||
@ -52,6 +114,28 @@ double Util::niceNum(double x, bool round)
|
||||
|
||||
QString Util::file2name(const QString &path)
|
||||
{
|
||||
QFileInfo fi(path);
|
||||
QFileInfo fi(displayName(path));
|
||||
return fi.baseName().replace('_', ' ');
|
||||
}
|
||||
|
||||
QString Util::displayName(const QString &path)
|
||||
{
|
||||
#ifdef Q_OS_ANDROID
|
||||
QUrl url(path);
|
||||
|
||||
// Not an Android URL, return standard filename.
|
||||
if (url.scheme() != "content") {
|
||||
QFileInfo fi(path);
|
||||
return fi.fileName();
|
||||
// Directory browsing URLs. Those can not be translated using the Android
|
||||
// content resolver but we can get the filename from the URL path.
|
||||
} else if (url.path().startsWith("/tree/")) {
|
||||
QFileInfo fi(url.fileName());
|
||||
return fi.fileName();
|
||||
// Translate all "regular" android URLs using the Android content resolver.
|
||||
} else
|
||||
return documentName(path);
|
||||
#else
|
||||
return path;
|
||||
#endif // Q_OS_ANDROID
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ namespace Util
|
||||
int str2int(const char *str, int len);
|
||||
double niceNum(double x, bool round);
|
||||
QString file2name(const QString &path);
|
||||
QString displayName(const QString &path);
|
||||
}
|
||||
|
||||
#endif // UTIL_H
|
||||
|
Reference in New Issue
Block a user