[android] use AAssetManager instead of libzip

This commit is contained in:
Konstantin Käfer
2017-04-11 17:18:09 +02:00
parent eed7dedf03
commit 197f0ca6b4
18 changed files with 135 additions and 189 deletions

View File

@@ -0,0 +1,53 @@
#include "asset_manager_file_source.hpp"
#include <mbgl/storage/response.hpp>
#include <mbgl/util/util.hpp>
#include <mbgl/util/thread.hpp>
#include <mbgl/util/url.hpp>
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
namespace mbgl {
class AssetManagerFileSource::Impl {
public:
Impl(AAssetManager* assetManager_) : assetManager(assetManager_) {
}
void request(const std::string& url, FileSource::Callback callback) {
// Note: AssetManager already prepends "assets" to the filename.
const std::string path = mbgl::util::percentDecode(url.substr(8));
Response response;
if (AAsset* asset = AAssetManager_open(assetManager, path.c_str(), AASSET_MODE_BUFFER)) {
response.data = std::make_shared<std::string>(
reinterpret_cast<const char*>(AAsset_getBuffer(asset)), AAsset_getLength64(asset));
AAsset_close(asset);
} else {
response.error = std::make_unique<Response::Error>(Response::Error::Reason::NotFound,
"Could not read asset");
}
callback(response);
}
private:
AAssetManager* assetManager;
};
AssetManagerFileSource::AssetManagerFileSource(jni::JNIEnv& env, jni::Object<android::AssetManager> assetManager_)
: assetManager(assetManager_.NewGlobalRef(env)),
thread(std::make_unique<util::Thread<Impl>>(
util::ThreadContext{"AssetManagerFileSource", util::ThreadPriority::Low},
AAssetManager_fromJava(&env, jni::Unwrap(**assetManager)))) {
}
AssetManagerFileSource::~AssetManagerFileSource() = default;
std::unique_ptr<AsyncRequest> AssetManagerFileSource::request(const Resource& resource, Callback callback) {
return thread->invokeWithCallback(&Impl::request, resource.url, callback);
}
} // namespace mbgl