Commit df32b369 authored by BogDan Vatra's avatar BogDan Vatra
Browse files

Seamless Android assets support

Add seamless Android assets support, every time your app will try to
open a file, first Qt will search for that file into android assets and
only if not exists there will try to open it from your local file
system.
Checking if a file exists in andoid assets is very fast (<1ms).

FEATURE: 276766
parent 1b8c488e
......@@ -190,8 +190,8 @@ for(QTLIB, $$list($$lower($$unique(QT)))) {
QMAKE_LFLAGS += -Wl,--no-undefined -Wl,-z,noexecstack -shared
}
CONFIG(armeabi-v7a) : target.path=/android/libs/armeabi-v7a/
else : target.path=/android/libs/armeabi/
CONFIG(armeabi-v7a) : target.path=/libs/armeabi-v7a/
else : target.path=/libs/armeabi/
INSTALLS *= target
}
......
......@@ -224,16 +224,18 @@ namespace QtAndroid
return m_javaVM;
}
jclass applicationClass()
AAssetManager * assetManager()
{
return m_applicationClass;
return m_assetManager;
}
AAssetManager* assetManager()
jclass applicationClass()
{
return m_assetManager;
return m_applicationClass;
}
}
static void startQtAndroidPlugin(JNIEnv* env, jobject /*object*//*, jobject applicationAssetManager*/)
......@@ -679,13 +681,16 @@ static int registerNativeMethods(JNIEnv* env, const char* className,
m_redrawSurfaceMethodID = env->GetStaticMethodID(m_applicationClass, "redrawSurface", "(IIII)V");
m_showSoftwareKeyboardMethodID = env->GetStaticMethodID(m_applicationClass, "showSoftwareKeyboard", "()V");
m_hideSoftwareKeyboardMethodID = env->GetStaticMethodID(m_applicationClass, "hideSoftwareKeyboard", "()V");
m_setFullScreenMethodID = env->GetStaticMethodID(m_applicationClass, "setFullScreen", "(Z)V");
jmethodID methodID=env->GetStaticMethodID(m_applicationClass, "mainActivity", "()Leu/licentia/necessitas/industrius/QtActivity;");
jobject activityObject=env->CallStaticObjectMethod(m_applicationClass, methodID);
clazz=env->FindClass(ContextWrapperClassPathName);
methodID=env->GetMethodID(clazz, "getAssets", "()Landroid/content/res/AssetManager;");
m_assetManager=AAssetManager_fromJava(env, env->CallObjectMethod(activityObject, methodID));
m_setFullScreenMethodID = env->GetStaticMethodID(m_applicationClass, "setFullScreen", "(Z)V");
return JNI_TRUE;
}
......
......@@ -50,8 +50,7 @@ namespace QtAndroid
// Software keyboard support
JavaVM * javaVM();
AAssetManager* assetManager();
AAssetManager * assetManager();
jclass applicationClass();
}
#endif // ANDROID_APP_H
#include "qandroidassetsfileenginehandler.h"
#include "androidjnimain.h"
#include <QCoreApplication>
#include <QDateTime>
class AndroidAbstractFileEngine : public QAbstractFileEngine
{
public:
AndroidAbstractFileEngine(AAsset* asset)
{
m_asset = asset;
}
~AndroidAbstractFileEngine()
{
close();
}
virtual bool open(QIODevice::OpenMode openMode)
{
if (m_asset)
return openMode&QIODevice::ReadOnly;
return false;
}
virtual bool close()
{
if (m_asset)
{
AAsset_close(m_asset);
m_asset = 0;
return true;
}
return false;
}
virtual qint64 size() const
{
if (m_asset)
return AAsset_getLength(m_asset);
return -1;
}
virtual qint64 pos() const
{
if (m_asset)
return AAsset_seek(m_asset, 0, SEEK_CUR);
return -1;
}
virtual bool seek(qint64 pos)
{
if (m_asset)
return pos==AAsset_seek(m_asset, pos, SEEK_SET);
return false;
}
virtual qint64 read(char *data, qint64 maxlen)
{
if (m_asset)
return AAsset_read(m_asset, data, maxlen);
return -1;
}
virtual bool isSequential() const
{
return false;
}
virtual bool caseSensitive() const
{
return true;
}
virtual bool isRelativePath() const
{
return false;
}
private:
AAsset* m_asset;
};
AndroidAssetsFileEngineHandler::AndroidAssetsFileEngineHandler()
{
m_assetManager = QtAndroid::assetManager();
}
AndroidAssetsFileEngineHandler::~AndroidAssetsFileEngineHandler()
{
}
QAbstractFileEngine * AndroidAssetsFileEngineHandler::create ( const QString & fileName ) const
{
AAsset* asset;
if (fileName[0]==QChar(QLatin1Char('/')))
asset=AAssetManager_open(m_assetManager, fileName.toUtf8().constData()+1, AASSET_MODE_BUFFER);
else
asset=AAssetManager_open(m_assetManager, fileName.toUtf8().constData(), AASSET_MODE_BUFFER);
if (asset)
return new AndroidAbstractFileEngine(asset);
return 0;
}
#ifndef QANDROIDASSETSFILEENGINEHANDLER_H
#define QANDROIDASSETSFILEENGINEHANDLER_H
#include <QAbstractFileEngineHandler>
#include <android/asset_manager.h>
class AndroidAssetsFileEngineHandler: public QAbstractFileEngineHandler
{
public:
AndroidAssetsFileEngineHandler();
virtual ~AndroidAssetsFileEngineHandler();
QAbstractFileEngine * create ( const QString & fileName ) const;
private:
AAssetManager * m_assetManager;
};
#endif // QANDROIDASSETSFILEENGINEHANDLER_H
......@@ -62,7 +62,7 @@ public:
qPrintable(fontpath));
}
QDir dir(fontpath, QLatin1String("Droid*.ttf"));
QDir dir(fontpath, QLatin1String("*.ttf"));
for (int i = 0; i < int(dir.count()); ++i) {
const QByteArray file = QFile::encodeName(dir.absoluteFilePath(dir[i]));
addTTFile(QByteArray(), file);
......
......@@ -2,4 +2,4 @@ TEMPLATE = subdirs
INCLUDEPATH += $$QT_SOURCE_TREE/src/plugins/platforms/android/common
SUBDIRS += android-4 android-5 android-9 # android-9
SUBDIRS += android-4 android-5 android-9 # android-8
Supports Markdown
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