#include #include #include #include #include #include "jni.hpp" #include #include #include #include namespace mbgl { namespace { int severityToPriority(EventSeverity severity) { switch (severity) { case EventSeverity::Debug: return ANDROID_LOG_DEBUG; case EventSeverity::Info: return ANDROID_LOG_INFO; case EventSeverity::Warning: return ANDROID_LOG_WARN; case EventSeverity::Error: return ANDROID_LOG_ERROR; default: return ANDROID_LOG_VERBOSE; } } } // namespace void Log::platformRecord(EventSeverity severity, const std::string& msg) { __android_log_print(severityToPriority(severity), "mbgl", "%s", msg.c_str()); } } // namespace mbgl namespace { template class JavaWrapper { public: JavaWrapper(JNIEnv* env_, T obj_) : env(env_), obj(obj_) {} ~JavaWrapper() { env->DeleteLocalRef(obj); env = nullptr; obj = NULL; } T& get() { return obj; } private: JavaWrapper() = delete; JNIEnv* env; T obj; }; std::string jstringToStdString(JNIEnv* env, jstring jStr) { if (!jStr) { return std::string(); } JavaWrapper stringClass(env, env->GetObjectClass(jStr)); const jmethodID getBytes = env->GetMethodID(stringClass.get(), "getBytes", "(Ljava/lang/String;)[B"); JavaWrapper stringJbytes( env, static_cast( env->CallObjectMethod(jStr, getBytes, JavaWrapper(env, env->NewStringUTF("UTF-8")).get()))); size_t length = static_cast(env->GetArrayLength(stringJbytes.get())); jbyte* pBytes = env->GetByteArrayElements(stringJbytes.get(), NULL); std::string ret = std::string(reinterpret_cast(pBytes), length); env->ReleaseByteArrayElements(stringJbytes.get(), pBytes, JNI_ABORT); return ret; } void changeState(JNIEnv* env, struct android_app* app) { jobject nativeActivity = app->activity->clazz; jclass acl = env->GetObjectClass(nativeActivity); jmethodID getClassLoader = env->GetMethodID(acl, "getClassLoader", "()Ljava/lang/ClassLoader;"); jobject cls = env->CallObjectMethod(nativeActivity, getClassLoader); JavaWrapper classLoader(env, env->FindClass("java/lang/ClassLoader")); jmethodID findClass = env->GetMethodID(classLoader.get(), "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"); JavaWrapper strClassName(env, env->NewStringUTF("android.app.TestState")); jclass testStateClass = static_cast(env->CallObjectMethod(cls, findClass, strClassName.get())); if (testStateClass != NULL) { jfieldID id = env->GetStaticFieldID(testStateClass, "running", "Z"); env->SetStaticBooleanField(testStateClass, id, false); } } bool copyFile(JNIEnv* env, AAssetManager* assetManager, const std::string& filePath, const std::string& destinationPath, const std::string& fileName) { JavaWrapper fileClass(env, env->FindClass("java/io/File")); jmethodID fileCtor = env->GetMethodID(fileClass.get(), "", "(Ljava/lang/String;Ljava/lang/String;)V"); jmethodID fileExists = env->GetMethodID(fileClass.get(), "exists", "()Z"); JavaWrapper destination(env, env->NewStringUTF(destinationPath.c_str())); JavaWrapper file(env, env->NewStringUTF(fileName.c_str())); JavaWrapper fileToCopy(env, env->NewObject(fileClass.get(), fileCtor, destination.get(), file.get())); bool stateOk = true; if (env->CallBooleanMethod(fileToCopy.get(), fileExists)) { mbgl::Log::Warning(mbgl::Event::General, "File '%s' already exists", filePath.c_str()); } else { std::unique_ptr> fileAsset( AAssetManager_open(assetManager, fileName.c_str(), AASSET_MODE_BUFFER), [](AAsset* asset) { AAsset_close(asset); }); if (fileAsset == nullptr) { mbgl::Log::Warning(mbgl::Event::General, "Failed to open asset file %s", fileName.c_str()); return false; } const void* fileData = AAsset_getBuffer(fileAsset.get()); const off_t fileLen = AAsset_getLength(fileAsset.get()); std::unique_ptr> newFile(std::fopen(filePath.c_str(), "w+"), [](FILE* file) { std::fclose(file); }); stateOk = newFile != nullptr; if (!stateOk) { mbgl::Log::Warning(mbgl::Event::General, "Failed to create new file entry %s", fileName.c_str()); } else { auto res = static_cast(std::fwrite(fileData, sizeof(char), fileLen, newFile.get())); if (fileLen != res) { mbgl::Log::Warning( mbgl::Event::General, "Failed to generate file entry %s from assets", fileName.c_str()); } } } return stateOk; } void unZipFile(JNIEnv* env, const std::string& zipFilePath, const std::string& destinationPath) { JavaWrapper fileClassWrapper(env, env->FindClass("java/io/File")); auto fileClass = fileClassWrapper.get(); jmethodID fileCtor = env->GetMethodID(fileClass, "", "(Ljava/lang/String;Ljava/lang/String;)V"); jmethodID fileExists = env->GetMethodID(fileClass, "exists", "()Z"); jmethodID fileIsDirectory = env->GetMethodID(fileClass, "isDirectory", "()Z"); jmethodID deleteFile = env->GetMethodID(fileClass, "delete", "()Z"); jmethodID createNewFile = env->GetMethodID(fileClass, "createNewFile", "()Z"); jmethodID fileGetName = env->GetMethodID(fileClass, "getName", "()Ljava/lang/String;"); JavaWrapper fileInputStreamWrapper(env, env->FindClass("java/io/FileInputStream")); auto fileInputStream = fileInputStreamWrapper.get(); jmethodID finCtor = env->GetMethodID(fileInputStream, "", "(Ljava/lang/String;)V"); JavaWrapper fileOutputStreamWrapper(env, env->FindClass("java/io/FileOutputStream")); auto fileOutputStream = fileOutputStreamWrapper.get(); jmethodID foutCtor = env->GetMethodID(fileOutputStream, "", "(Ljava/io/File;)V"); jmethodID foutClose = env->GetMethodID(fileOutputStream, "close", "()V"); jmethodID foutWrite = env->GetMethodID(fileOutputStream, "write", "([BII)V"); JavaWrapper zipInputStreamWrapper(env, env->FindClass("java/util/zip/ZipInputStream")); auto zipInputStream = zipInputStreamWrapper.get(); jmethodID zinCtor = env->GetMethodID(zipInputStream, "", "(Ljava/io/InputStream;)V"); jmethodID zinGetNextEntry = env->GetMethodID(zipInputStream, "getNextEntry", "()Ljava/util/zip/ZipEntry;"); jmethodID zinRead = env->GetMethodID(zipInputStream, "read", "([B)I"); jmethodID zinCloseEntry = env->GetMethodID(zipInputStream, "closeEntry", "()V"); JavaWrapper zipEntryWrapper(env, env->FindClass("java/util/zip/ZipEntry")); auto zipEntry = zipEntryWrapper.get(); jmethodID zipGetName = env->GetMethodID(zipEntry, "getName", "()Ljava/lang/String;"); jmethodID zipIsDirectory = env->GetMethodID(zipEntry, "isDirectory", "()Z"); // Unzip the resource folder to destination path JavaWrapper destination(env, env->NewStringUTF(destinationPath.c_str())); JavaWrapper zipFile(env, env->NewStringUTF(zipFilePath.c_str())); JavaWrapper fileIn(env, env->NewObject(fileInputStream, finCtor, zipFile.get())); JavaWrapper zipIn(env, env->NewObject(zipInputStream, zinCtor, fileIn.get())); while (true) { JavaWrapper obj(env, env->CallObjectMethod(zipIn.get(), zinGetNextEntry)); jobject zEntry = obj.get(); if (zEntry == NULL) { break; } jstring dir = static_cast(env->CallObjectMethod(zEntry, zipGetName)); std::string name = jstringToStdString(env, dir); bool isDir = env->CallBooleanMethod(zEntry, zipIsDirectory); JavaWrapper fileHandle(env, env->NewObject(fileClass, fileCtor, destination.get(), dir)); jobject& f = fileHandle.get(); std::string fileName = jstringToStdString(env, static_cast(env->CallObjectMethod(f, fileGetName))); if (isDir) { if (!(env->CallBooleanMethod(f, fileIsDirectory))) { jmethodID mkdirs = env->GetMethodID(fileClass, "mkdirs", "()Z"); bool success = (env->CallBooleanMethod(f, mkdirs)); std::string fileName = jstringToStdString(env, static_cast(env->CallObjectMethod(f, fileGetName))); if (!success) { mbgl::Log::Warning( mbgl::Event::General, "Failed to create folder entry %s from zip", fileName.c_str()); } } } else if (!(env->CallBooleanMethod(f, fileExists))) { bool success = env->CallBooleanMethod(f, createNewFile); std::string fileName = jstringToStdString(env, static_cast(env->CallObjectMethod(f, fileGetName))); if (!success) { mbgl::Log::Warning(mbgl::Event::General, "Failed to create folder entry %s from zip", fileName.c_str()); continue; } JavaWrapper fout(env, env->NewObject(fileOutputStream, foutCtor, f)); JavaWrapper jBuff(env, env->NewByteArray(2048)); int count; while ((count = env->CallIntMethod(zipIn.get(), zinRead, jBuff.get())) != -1) { env->CallVoidMethod(fout.get(), foutWrite, jBuff.get(), 0, count); } env->CallVoidMethod(zipIn.get(), zinCloseEntry); env->CallVoidMethod(fout.get(), foutClose); } } JavaWrapper fileToDelete(env, env->NewObject(fileClass, fileCtor, destination.get(), zipFile.get())); if (env->CallBooleanMethod(fileToDelete.get(), fileExists)) { jboolean success = (env->CallBooleanMethod(fileToDelete.get(), deleteFile)); if (!success) { mbgl::Log::Warning(mbgl::Event::General, "Failed to delete file entry %s", zipFilePath.c_str()); } } } } // namespace void android_main(struct android_app* app) { mbgl::android::theJVM = app->activity->vm; JNIEnv* env = nullptr; app->activity->vm->AttachCurrentThread(&env, NULL); const char* storage_chars = app->activity->internalDataPath; std::string storagePath(storage_chars); std::string zipFile = storagePath + "/data.zip"; int outFd, outEvents; struct android_poll_source* source = nullptr; if (!copyFile(env, app->activity->assetManager, zipFile, storagePath, "data.zip")) { mbgl::Log::Error( mbgl::Event::General, "Failed to copy zip File '%s' to external storage for upzipping", zipFile.c_str()); } else { unZipFile(env, zipFile, storagePath); std::string configFile = storagePath + "/android-manifest.json"; std::vector arguments = {"mbgl-render-test-runner", "-p", configFile}; std::vector argv; for (const auto& arg : arguments) { argv.push_back((char*)arg.data()); } argv.push_back(nullptr); int finishedTestCount = 0; std::function testStatus = [&]() { ALooper_pollAll(0, &outFd, &outEvents, reinterpret_cast(&source)); if (source != nullptr) { source->process(app, source); } mbgl::Log::Info(mbgl::Event::General, "Current finished tests number is '%d' ", ++finishedTestCount); }; mbgl::runRenderTests(argv.size() - 1, argv.data(), testStatus); mbgl::Log::Info(mbgl::Event::General, "All tests are finished!"); changeState(env, app); } while (true) { ALooper_pollAll(0, &outFd, &outEvents, reinterpret_cast(&source)); if (source != nullptr) { source->process(app, source); } if (app->destroyRequested != 0) { app->activity->vm->DetachCurrentThread(); mbgl::Log::Info(mbgl::Event::General, "Close the App!"); return; } } }