Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -531,10 +531,6 @@ run-android-core-test-$1-%: android-core-test-$1
# Ensure clean state on the device
adb shell "rm -Rf $(MBGL_ANDROID_LOCAL_WORK_DIR) && mkdir -p $(MBGL_ANDROID_LOCAL_WORK_DIR)/test"

# Generate zipped asset files
cd test/fixtures/api && zip -r assets.zip assets && cd -
cd test/fixtures/storage && zip -r assets.zip assets && cd -

# Push all needed files to the device
adb push $(MBGL_ANDROID_CORE_TEST_DIR)/test.jar $(MBGL_ANDROID_LOCAL_WORK_DIR) > /dev/null 2>&1
adb push test/fixtures $(MBGL_ANDROID_LOCAL_WORK_DIR)/test > /dev/null 2>&1
Expand Down
3 changes: 3 additions & 0 deletions include/mbgl/storage/default_file_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class DefaultFileSource : public FileSource {
DefaultFileSource(const std::string& cachePath,
const std::string& assetRoot,
uint64_t maximumCacheSize = util::DEFAULT_MAX_CACHE_SIZE);
DefaultFileSource(const std::string& cachePath,
std::unique_ptr<FileSource>&& assetFileSource,
uint64_t maximumCacheSize = util::DEFAULT_MAX_CACHE_SIZE);
~DefaultFileSource() override;

bool supportsOptionalRequests() const override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.pm.PackageManager;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.content.res.AssetManager;

import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
Expand Down Expand Up @@ -40,8 +41,7 @@ public interface ResourceTransformCallback {
public static synchronized FileSource getInstance(Context context) {
if (INSTANCE == null) {
String cachePath = getCachePath(context);
String apkPath = context.getPackageCodePath();
INSTANCE = new FileSource(cachePath, apkPath);
INSTANCE = new FileSource(cachePath, context.getResources().getAssets());
}

return INSTANCE;
Expand Down Expand Up @@ -107,8 +107,8 @@ public static boolean isExternalStorageReadable() {

private long nativePtr;

private FileSource(String cachePath, String apkPath) {
initialize(Mapbox.getAccessToken(), cachePath, apkPath);
private FileSource(String cachePath, AssetManager assetManager) {
initialize(Mapbox.getAccessToken(), cachePath, assetManager);
}

public native void setAccessToken(@NonNull String accessToken);
Expand All @@ -127,7 +127,7 @@ private FileSource(String cachePath, String apkPath) {
*/
public native void setResourceTransform(final ResourceTransformCallback callback);

private native void initialize(String accessToken, String cachePath, String apkPath);
private native void initialize(String accessToken, String cachePath, AssetManager assetManager);

@Override
protected native void finalize() throws Throwable;
Expand Down
11 changes: 4 additions & 7 deletions platform/android/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ if ((ANDROID_ABI STREQUAL "armeabi") OR (ANDROID_ABI STREQUAL "armeabi-v7a") OR
endif()

mason_use(jni.hpp VERSION 3.0.0 HEADER_ONLY)
mason_use(libzip VERSION 1.1.3)
mason_use(nunicode VERSION 1.7.1)
mason_use(sqlite VERSION 3.14.2)
mason_use(gtest VERSION 1.8.0)
Expand All @@ -34,8 +33,11 @@ macro(mbgl_platform_core)

# File source
PRIVATE platform/android/src/http_file_source.cpp
PRIVATE platform/android/src/asset_file_source.cpp
PRIVATE platform/android/src/asset_manager.hpp
PRIVATE platform/android/src/asset_manager_file_source.cpp
PRIVATE platform/android/src/asset_manager_file_source.hpp
PRIVATE platform/default/default_file_source.cpp
PRIVATE platform/default/asset_file_source.cpp
PRIVATE platform/default/local_file_source.cpp
PRIVATE platform/default/online_file_source.cpp

Expand Down Expand Up @@ -76,7 +78,6 @@ macro(mbgl_platform_core)

target_add_mason_package(mbgl-core PUBLIC sqlite)
target_add_mason_package(mbgl-core PUBLIC nunicode)
target_add_mason_package(mbgl-core PUBLIC libzip)
target_add_mason_package(mbgl-core PUBLIC geojson)
target_add_mason_package(mbgl-core PUBLIC jni.hpp)
target_add_mason_package(mbgl-core PUBLIC rapidjson)
Expand Down Expand Up @@ -303,10 +304,6 @@ macro(mbgl_platform_test)
PRIVATE -fvisibility=hidden
)

target_compile_definitions(mbgl-test
PRIVATE MBGL_ASSET_ZIP=1
)

target_link_libraries(mbgl-test
PRIVATE mbgl-android
PRIVATE -Wl,--gc-sections
Expand Down
113 changes: 0 additions & 113 deletions platform/android/src/asset_file_source.cpp

This file was deleted.

14 changes: 14 additions & 0 deletions platform/android/src/asset_manager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

namespace mbgl {
namespace android {

class AssetManager {
public:
static constexpr auto Name() {
return "android/content/res/AssetManager";
};
};

} // namespace android
} // namespace mbgl
53 changes: 53 additions & 0 deletions platform/android/src/asset_manager_file_source.cpp
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions platform/android/src/asset_manager_file_source.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <mbgl/storage/file_source.hpp>

#include "asset_manager.hpp"

#include <jni/jni.hpp>

namespace mbgl {

namespace util {
template <typename T> class Thread;
} // namespace util

class AssetManagerFileSource : public FileSource {
public:
AssetManagerFileSource(jni::JNIEnv&, jni::Object<android::AssetManager>);
~AssetManagerFileSource() override;

std::unique_ptr<AsyncRequest> request(const Resource&, Callback) override;

private:
jni::UniqueObject<android::AssetManager> assetManager;
class Impl;
std::unique_ptr<util::Thread<Impl>> thread;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to use the thread pool?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do, but I've modeled this FileSource after all the other FileSources, and they are still using util::Thread. I attempted to port them to the actor system in #7678

};

} // namespace mbgl
13 changes: 8 additions & 5 deletions platform/android/src/file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@

#include <mbgl/util/logging.hpp>

#include <string>

#include "asset_manager_file_source.hpp"
#include "jni/generic_global_ref_deleter.hpp"

#include <string>

namespace mbgl {
namespace android {

// FileSource //

FileSource::FileSource(jni::JNIEnv& _env, jni::String accessToken, jni::String _cachePath, jni::String _apkPath) {
FileSource::FileSource(jni::JNIEnv& _env,
jni::String accessToken,
jni::String _cachePath,
jni::Object<AssetManager> assetManager) {
// Create a core default file source
fileSource = std::make_unique<mbgl::DefaultFileSource>(
jni::Make<std::string>(_env, _cachePath) + "/mbgl-offline.db",
jni::Make<std::string>(_env, _apkPath));
std::make_unique<AssetManagerFileSource>(_env, assetManager));

// Set access token
fileSource->setAccessToken(jni::Make<std::string>(_env, accessToken));
Expand Down Expand Up @@ -80,7 +83,7 @@ void FileSource::registerNative(jni::JNIEnv& env) {
// Register the peer
jni::RegisterNativePeer<FileSource>(
env, FileSource::javaClass, "nativePtr",
std::make_unique<FileSource, JNIEnv&, jni::String, jni::String, jni::String>,
std::make_unique<FileSource, JNIEnv&, jni::String, jni::String, jni::Object<AssetManager>>,
"initialize",
"finalize",
METHOD(&FileSource::getAccessToken, "getAccessToken"),
Expand Down
4 changes: 3 additions & 1 deletion platform/android/src/file_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <mbgl/storage/default_file_source.hpp>

#include "asset_manager.hpp"

#include <jni/jni.hpp>

namespace mbgl {
Expand All @@ -23,7 +25,7 @@ class FileSource {
static jni::Class<ResourceTransformCallback> javaClass;
};

FileSource(jni::JNIEnv&, jni::String, jni::String, jni::String);
FileSource(jni::JNIEnv&, jni::String, jni::String, jni::Object<AssetManager>);

~FileSource();

Expand Down
1 change: 0 additions & 1 deletion platform/android/src/jni.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ extern JavaVM* theJVM;

extern std::string cachePath;
extern std::string dataPath;
extern std::string apkPath;

bool attach_jni_thread(JavaVM* vm, JNIEnv** env, std::string threadName);
void detach_jni_thread(JavaVM* vm, JNIEnv** env, bool detach);
Expand Down
8 changes: 7 additions & 1 deletion platform/default/default_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,15 @@ class DefaultFileSource::Impl {
DefaultFileSource::DefaultFileSource(const std::string& cachePath,
const std::string& assetRoot,
uint64_t maximumCacheSize)
: DefaultFileSource(cachePath, std::make_unique<AssetFileSource>(assetRoot), maximumCacheSize) {
}

DefaultFileSource::DefaultFileSource(const std::string& cachePath,
std::unique_ptr<FileSource>&& assetFileSource_,
uint64_t maximumCacheSize)
: thread(std::make_unique<util::Thread<Impl>>(util::ThreadContext{"DefaultFileSource", util::ThreadPriority::Low},
cachePath, maximumCacheSize)),
assetFileSource(std::make_unique<AssetFileSource>(assetRoot)),
assetFileSource(std::move(assetFileSource_)),
localFileSource(std::make_unique<LocalFileSource>()) {
}

Expand Down
Loading