Skip to content
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
11 changes: 9 additions & 2 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ set(CMAKE_CXX_EXTENSIONS OFF)

find_package(odrcore REQUIRED)

add_library(tmpfile_hack STATIC
src/main/cpp/tmpfile_hack.cpp)
target_include_directories(tmpfile_hack
PRIVATE src/main/cpp)
target_link_libraries(tmpfile_hack
PRIVATE log)

add_library(odr-core SHARED
src/main/cpp/CoreWrapper.cpp)
src/main/cpp/core_wrapper.cpp)
target_include_directories(odr-core
PRIVATE src/main/cpp)
target_link_libraries(odr-core
PRIVATE odrcore::odrcore log)
PRIVATE odrcore::odrcore tmpfile_hack log)
2 changes: 1 addition & 1 deletion app/conanprofile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ compiler.cppstd=20
compiler.libcxx=c++_shared
&:build_type=RelWithDebInfo
odrcore/*:build_type=RelWithDebInfo
build_type=Release
build_type=RelWithDebInfo

[conf]
tools.android:ndk_path=@NDK_PATH@
Expand Down
34 changes: 0 additions & 34 deletions app/src/main/cpp/CoreWrapper.hpp

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "CoreWrapper.hpp"
#include "core_wrapper.hpp"

#include "tmpfile_hack.hpp"

#include <odr/document.hpp>
#include <odr/file.hpp>
Expand Down Expand Up @@ -80,19 +82,20 @@ std::optional<odr::Document> s_document;
JNIEXPORT void JNICALL
Java_at_tomtasche_reader_background_CoreWrapper_setGlobalParams(JNIEnv *env, jclass clazz,
jobject params) {
jboolean isCopy;

jclass paramsClass = env->GetObjectClass(params);

std::string odrCoreDataPath = getStringField(env, paramsClass, params, "coreDataPath");
std::string fontconfigDataPath = getStringField(env, paramsClass, params, "fontconfigDataPath");
std::string popplerDataPath = getStringField(env, paramsClass, params, "popplerDataPath");
std::string pdf2htmlexDataPath = getStringField(env, paramsClass, params, "pdf2htmlexDataPath");
std::string customTmpfilePath = getStringField(env, paramsClass, params, "customTmpfilePath");

odr::GlobalParams::set_odr_core_data_path(odrCoreDataPath);
odr::GlobalParams::set_fontconfig_data_path(fontconfigDataPath);
odr::GlobalParams::set_poppler_data_path(popplerDataPath);
odr::GlobalParams::set_pdf2htmlex_data_path(pdf2htmlexDataPath);

tmpfile_hack::set_tmpfile_directory(customTmpfilePath);
}

JNIEXPORT jobject JNICALL
Expand Down
39 changes: 39 additions & 0 deletions app/src/main/cpp/core_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <jni.h>

extern "C" {

JNIEXPORT void JNICALL
Java_at_tomtasche_reader_background_CoreWrapper_setGlobalParams(JNIEnv *env, jclass clazz,
jobject params);

JNIEXPORT jobject JNICALL
Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jclass clazz,
jobject options);

JNIEXPORT jobject JNICALL
Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative(JNIEnv *env, jclass clazz,
jobject options,
jstring htmlDiff);

JNIEXPORT void JNICALL
Java_at_tomtasche_reader_background_CoreWrapper_closeNative(JNIEnv *env, jclass clazz,
jobject options);

JNIEXPORT void JNICALL
Java_at_tomtasche_reader_background_CoreWrapper_createServerNative(JNIEnv *env, jclass clazz,
jstring outputPath);

JNIEXPORT jobject JNICALL
Java_at_tomtasche_reader_background_CoreWrapper_hostFileNative(JNIEnv *env, jclass clazz,
jstring prefix, jobject options);

JNIEXPORT void JNICALL
Java_at_tomtasche_reader_background_CoreWrapper_listenServerNative(JNIEnv *env, jclass clazz,
jint port);

JNIEXPORT void JNICALL
Java_at_tomtasche_reader_background_CoreWrapper_stopServerNative(JNIEnv *env, jclass clazz);

}
59 changes: 59 additions & 0 deletions app/src/main/cpp/tmpfile_hack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "tmpfile_hack.hpp"

#include <string>
#include <optional>
#include <random>

#include <android/log.h>

#include <cstdlib>
#include <unistd.h>

static constexpr std::string_view s_filename_template = "tmpfile-XXXXXX";
static constexpr std::string_view s_default_directory = "/data/local/tmp";

static std::optional<std::string> s_tmpfile_directory;

std::string tmpfile_hack::get_tmpfile_directory() {
if (s_tmpfile_directory.has_value()) {
return s_tmpfile_directory.value();
}

if (const char *tmpfile_directory = std::getenv("TMPDIR"); tmpfile_directory != nullptr) {
return tmpfile_directory;
}

return std::string(s_default_directory);
}

void tmpfile_hack::set_tmpfile_directory(std::string_view tmpfile_dir) {
s_tmpfile_directory = std::string(tmpfile_dir);
}

extern "C" {

extern FILE *tmpfile() {
std::string tmpfile_path =
tmpfile_hack::get_tmpfile_directory() + "/" + std::string(s_filename_template);

int descriptor = mkstemp(tmpfile_path.data());
if (descriptor == -1) {
__android_log_print(ANDROID_LOG_ERROR, "tmpfile_hack",
"Failed to create temporary file: %s", tmpfile_path.c_str());
return nullptr;
}
__android_log_print(ANDROID_LOG_VERBOSE, "tmpfile_hack", "Temporary file created: %s",
tmpfile_path.c_str());

FILE *handle = fdopen(descriptor, "w+b");
unlink(tmpfile_path.c_str());

if (handle == nullptr) {
close(descriptor);
__android_log_print(ANDROID_LOG_ERROR, "tmpfile_hack", "Failed to open temporary file: %s",
tmpfile_path.c_str());
}
return handle;
}

}
12 changes: 12 additions & 0 deletions app/src/main/cpp/tmpfile_hack.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <string>
#include <string_view>

namespace tmpfile_hack {

std::string get_tmpfile_directory();

void set_tmpfile_directory(std::string_view tmpfile_dir);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.webkit.MimeTypeMap;

import java.io.File;
import java.util.Objects;

import at.tomtasche.reader.nonfree.AnalyticsManager;
import at.tomtasche.reader.nonfree.ConfigManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static class GlobalParams {
public String fontconfigDataPath;
public String popplerDataPath;
public String pdf2htmlexDataPath;
public String customTmpfilePath;
}

public static native void setGlobalParams(GlobalParams params);
Expand All @@ -41,6 +42,7 @@ public static void initialize(Context context) {
globalParams.fontconfigDataPath = fontconfigDataDirectory.getAbsolutePath();
globalParams.popplerDataPath = popplerDataDirectory.getAbsolutePath();
globalParams.pdf2htmlexDataPath = pdf2htmlexDataDirectory.getAbsolutePath();
globalParams.customTmpfilePath = context.getCacheDir().getAbsolutePath();
CoreWrapper.setGlobalParams(globalParams);
}

Expand Down
Loading