Skip to content

Commit ade4a5b

Browse files
committed
first working http version
1 parent 86771cd commit ade4a5b

File tree

14 files changed

+243
-49
lines changed

14 files changed

+243
-49
lines changed

app/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ tasks.register('conanProfile', Copy) {
2020
["armv8", "armv7", "x86", "x86_64"].each { architecture ->
2121
tasks.named("conanInstall-" + architecture) {
2222
profile.set('build/conanprofile.txt')
23+
deployer.set('conandeployer.py')
24+
deployerFolder.set(outputDirectory.get().asFile.toString() + "/assets")
25+
conanExecutable.set('/Users/andreas/odr/venv/bin/conan')
2326
dependsOn(tasks.named('conanProfile'))
2427
}
2528
}
@@ -121,6 +124,11 @@ android {
121124
}
122125
}
123126
namespace 'at.tomtasche.reader'
127+
compileSdk 35
128+
buildToolsVersion '34.0.0'
129+
130+
// TODO can and should this be architecture dependent?
131+
sourceSets.main.assets.srcDirs += "build/conan/armv8/assets"
124132
}
125133

126134
dependencies {

app/conandeployer.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import shutil
2+
3+
4+
def deploy(graph, output_folder: str, **kwargs):
5+
conanfile = graph.root.conanfile
6+
conanfile.output.info(f"Custom deployer to {output_folder}")
7+
8+
symlinks = conanfile.conf.get("tools.deployer:symlinks", check_type=bool, default=True)
9+
arch = conanfile.settings.get_safe("arch")
10+
11+
conanfile.output.info(f"Symlinks: {symlinks}")
12+
conanfile.output.info(f"Arch: {arch}")
13+
14+
deps = {dep.ref.name: dep for dep in conanfile.dependencies.values()}
15+
16+
print(f"Dependencies: {list(deps.keys())}")
17+
18+
if "odrcore" in deps:
19+
dep = deps["odrcore"]
20+
conanfile.output.info(f"Deploying odrcore to {output_folder}")
21+
# nothing to deploy so far
22+
23+
copytree_kwargs = {"symlinks": symlinks, "dirs_exist_ok": True}
24+
25+
if "pdf2htmlex" in deps:
26+
dep = deps["pdf2htmlex"]
27+
conanfile.output.info(f"Deploying pdf2htmlex to {output_folder}")
28+
shutil.copytree(
29+
f"{dep.package_folder}/share/pdf2htmlex",
30+
f"{output_folder}/pdf2htmlex",
31+
**copytree_kwargs,
32+
)
33+
34+
if "poppler-data" in deps:
35+
dep = deps["poppler-data"]
36+
conanfile.output.info(f"Deploying poppler-data to {output_folder}")
37+
shutil.copytree(
38+
f"{dep.package_folder}/share/poppler",
39+
f"{output_folder}/poppler",
40+
**copytree_kwargs,
41+
)
42+
43+
if "fontconfig" in deps:
44+
dep = deps["fontconfig"]
45+
conanfile.output.info(f"Deploying fontconfig to {output_folder}")
46+
shutil.copytree(
47+
f"{dep.package_folder}/res/share",
48+
f"{output_folder}/fontconfig",
49+
**copytree_kwargs,
50+
)

app/src/androidTest/java/at/tomtasche/reader/test/CoreTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ private static void copy(InputStream src, File dst) throws IOException {
5959
@Test
6060
public void test() {
6161
CoreWrapper core = new CoreWrapper();
62-
core.initialize();
6362

6463
File cacheDir = InstrumentationRegistry.getInstrumentation().getTargetContext().getCacheDir();
6564
File htmlFile = new File(cacheDir, "html");

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
android:label="@string/app_title"
5050
android:roundIcon="@mipmap/ic_launcher_round"
5151
android:theme="@style/MainTheme"
52+
android:networkSecurityConfig="@xml/network_security_config"
5253
tools:replace="android:label">
5354

5455
<!-- https://stackoverflow.com/a/78086604/198996 -->

app/src/main/cpp/CoreWrapper.cpp

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111

1212
#include <string>
1313
#include <optional>
14+
#include <filesystem>
1415

1516
std::optional<odr::Html> s_html;
1617

1718
JNIEXPORT jobject JNICALL
18-
Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jobject instance,
19+
Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jclass clazz,
1920
jobject options) {
2021
jboolean isCopy;
2122

@@ -168,7 +169,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jobject
168169
}
169170

170171
JNIEXPORT jobject JNICALL
171-
Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative(JNIEnv *env, jobject instance,
172+
Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative(JNIEnv *env, jclass clazz,
172173
jobject options,
173174
jstring htmlDiff) {
174175
jboolean isCopy;
@@ -228,21 +229,28 @@ Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative(JNIEnv *env,
228229
}
229230

230231
JNIEXPORT void JNICALL
231-
Java_at_tomtasche_reader_background_CoreWrapper_closeNative(JNIEnv *env, jobject instance,
232+
Java_at_tomtasche_reader_background_CoreWrapper_closeNative(JNIEnv *env, jclass clazz,
232233
jobject options) {
233234
s_html.reset();
234235
}
235236

236237
std::optional<odr::HttpServer> s_server;
237238

238239
JNIEXPORT void JNICALL
239-
Java_at_tomtasche_reader_background_CoreWrapper_createServerNative(JNIEnv *env, jobject instance) {
240+
Java_at_tomtasche_reader_background_CoreWrapper_createServerNative(JNIEnv *env, jclass clazz, jstring outputPath) {
241+
const char* outputPathC = env->GetStringUTFChars(outputPath, nullptr);
242+
std::string output_path = outputPathC;
243+
env->ReleaseStringUTFChars(outputPath, outputPathC);
244+
245+
std::filesystem::create_directories(output_path);
246+
240247
odr::HttpServer::Config config;
248+
config.output_path = output_path;
241249
s_server = odr::HttpServer(config);
242250
}
243251

244252
JNIEXPORT jstring JNICALL
245-
Java_at_tomtasche_reader_background_CoreWrapper_hostFileNative(JNIEnv *env, jobject instance, jobject options) {
253+
Java_at_tomtasche_reader_background_CoreWrapper_hostFileNative(JNIEnv *env, jclass clazz, jobject options) {
246254
jboolean isCopy;
247255

248256
jclass optionsClass = env->GetObjectClass(options);
@@ -253,23 +261,29 @@ Java_at_tomtasche_reader_background_CoreWrapper_hostFileNative(JNIEnv *env, jobj
253261
auto inputPathCpp = std::string(inputPathC, env->GetStringUTFLength(inputPath));
254262
env->ReleaseStringUTFChars(inputPath, inputPathC);
255263

256-
odr::DecodePreference decode_preference;
257-
decode_preference.engine_priority = {
264+
odr::DecodePreference decodePreference;
265+
decodePreference.engine_priority = {
258266
odr::DecoderEngine::poppler, odr::DecoderEngine::wvware, odr::DecoderEngine::odr};
259-
odr::DecodedFile file = odr::OpenDocumentReader::open(inputPathCpp, decode_preference);
267+
odr::DecodedFile file = odr::OpenDocumentReader::open(inputPathCpp, decodePreference);
260268

261-
std::string id = s_server->host_file(file);
269+
__android_log_print(ANDROID_LOG_INFO, "smn", "file type %i", file.file_type());
262270

263-
return env->NewStringUTF(id.c_str());
271+
try {
272+
std::string id = s_server->host_file(file);
273+
return env->NewStringUTF(id.c_str());
274+
} catch (...) {
275+
__android_log_print(ANDROID_LOG_ERROR, "smn", "error");
276+
return env->NewStringUTF("error");
277+
}
264278
}
265279

266280
JNIEXPORT void JNICALL
267-
Java_at_tomtasche_reader_background_CoreWrapper_listenServerNative(JNIEnv *env, jobject instance) {
268-
s_server->listen("0.0.0.0", 8080);
281+
Java_at_tomtasche_reader_background_CoreWrapper_listenServerNative(JNIEnv *env, jclass clazz) {
282+
s_server->listen("127.0.0.1", 29665);
269283
}
270284

271285
JNIEXPORT void JNICALL
272-
Java_at_tomtasche_reader_background_CoreWrapper_stopServerNative(JNIEnv *env, jobject instance) {
286+
Java_at_tomtasche_reader_background_CoreWrapper_stopServerNative(JNIEnv *env, jclass clazz) {
273287
s_server->stop();
274288
s_server.reset();
275-
}
289+
}

app/src/main/cpp/CoreWrapper.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,25 @@
66
extern "C" {
77

88
JNIEXPORT jobject JNICALL
9-
Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jobject instance, jobject options);
9+
Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jclass clazz, jobject options);
1010

1111
JNIEXPORT jobject JNICALL
12-
Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative(JNIEnv *env, jobject instance, jobject options, jstring htmlDiff);
12+
Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative(JNIEnv *env, jclass clazz, jobject options, jstring htmlDiff);
1313

1414
JNIEXPORT void JNICALL
15-
Java_at_tomtasche_reader_background_CoreWrapper_closeNative(JNIEnv *env, jobject instance, jobject options);
15+
Java_at_tomtasche_reader_background_CoreWrapper_closeNative(JNIEnv *env, jclass clazz, jobject options);
1616

1717
JNIEXPORT void JNICALL
18-
Java_at_tomtasche_reader_background_CoreWrapper_createServerNative(JNIEnv *env, jobject instance);
18+
Java_at_tomtasche_reader_background_CoreWrapper_createServerNative(JNIEnv *env, jclass clazz, jstring outputPath);
1919

2020
JNIEXPORT jstring JNICALL
21-
Java_at_tomtasche_reader_background_CoreWrapper_hostFileNative(JNIEnv *env, jobject instance, jobject options);
21+
Java_at_tomtasche_reader_background_CoreWrapper_hostFileNative(JNIEnv *env, jclass clazz, jobject options);
2222

2323
JNIEXPORT void JNICALL
24-
Java_at_tomtasche_reader_background_CoreWrapper_listenServerNative(JNIEnv *env, jobject instance);
24+
Java_at_tomtasche_reader_background_CoreWrapper_listenServerNative(JNIEnv *env, jclass clazz);
2525

2626
JNIEXPORT void JNICALL
27-
Java_at_tomtasche_reader_background_CoreWrapper_stopServerNative(JNIEnv *env, jobject instance);
27+
Java_at_tomtasche_reader_background_CoreWrapper_stopServerNative(JNIEnv *env, jclass clazz);
2828
}
2929

3030
#endif //ANDROID_CORE_WRAPPER_H
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package at.tomtasche.reader.background;
2+
3+
import android.content.Context;
4+
import android.net.Uri;
5+
6+
import java.io.File;
7+
8+
import at.tomtasche.reader.nonfree.ConfigManager;
9+
10+
public class CoreHttpLoader extends FileLoader {
11+
12+
private final ConfigManager configManager;
13+
14+
private CoreWrapper lastCore;
15+
16+
public CoreHttpLoader(Context context, ConfigManager configManager) {
17+
super(context, LoaderType.ODF);
18+
19+
this.configManager = configManager;
20+
}
21+
22+
@Override
23+
public boolean isSupported(Options options) {
24+
return options.fileType.startsWith("application/vnd.oasis.opendocument") || options.fileType.startsWith("application/x-vnd.oasis.opendocument") || options.fileType.startsWith("application/vnd.oasis.opendocument.text-master");
25+
}
26+
27+
@Override
28+
public void loadSync(Options options) {
29+
final Result result = new Result();
30+
result.options = options;
31+
result.loaderType = type;
32+
33+
try {
34+
translate(options, result);
35+
36+
callOnSuccess(result);
37+
} catch (Throwable e) {
38+
if (e instanceof CoreWrapper.CoreEncryptedException) {
39+
e = new EncryptedDocumentException();
40+
}
41+
42+
callOnError(result, e);
43+
}
44+
}
45+
46+
private void translate(Options options, Result result) throws Exception {
47+
File cachedFile = AndroidFileCache.getCacheFile(context, options.cacheUri);
48+
49+
if (lastCore != null) {
50+
CoreWrapper.close();
51+
lastCore = null;
52+
}
53+
54+
CoreWrapper core = new CoreWrapper();
55+
try {
56+
lastCore = core;
57+
} catch (Throwable e) {
58+
crashManager.log(e);
59+
}
60+
61+
File cacheDirectory = AndroidFileCache.getCacheDirectory(cachedFile);
62+
63+
CoreWrapper.CoreOptions coreOptions = new CoreWrapper.CoreOptions();
64+
coreOptions.inputPath = cachedFile.getPath();
65+
coreOptions.outputPath = cacheDirectory.getPath();
66+
coreOptions.password = options.password;
67+
coreOptions.editable = options.translatable;
68+
coreOptions.ooxml = false;
69+
70+
Boolean usePaging = configManager.getBooleanConfig("use_paging");
71+
if (usePaging == null || usePaging) {
72+
coreOptions.paging = true;
73+
}
74+
75+
String id = CoreWrapper.hostFile(coreOptions);
76+
77+
result.partTitles.add("document");
78+
result.partUris.add(Uri.parse("http://localhost:29665/" + id + "/document.html"));
79+
}
80+
81+
@Override
82+
public File retranslate(Options options, String htmlDiff) {
83+
return null;
84+
}
85+
86+
@Override
87+
public void close() {
88+
super.close();
89+
90+
if (lastCore != null) {
91+
CoreWrapper.close();
92+
lastCore = null;
93+
}
94+
}
95+
}

app/src/main/java/at/tomtasche/reader/background/CoreWrapper.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
public class CoreWrapper {
77

8-
public void initialize() {
8+
static {
99
System.loadLibrary("odr-core");
1010
}
1111

12-
public CoreResult parse(CoreOptions options) {
12+
public static CoreResult parse(CoreOptions options) {
1313
CoreResult result = parseNative(options);
1414

1515
switch (result.errorCode) {
@@ -43,9 +43,9 @@ public CoreResult parse(CoreOptions options) {
4343
return result;
4444
}
4545

46-
private native CoreResult parseNative(CoreOptions options);
46+
private static native CoreResult parseNative(CoreOptions options);
4747

48-
public CoreResult backtranslate(CoreOptions options, String htmlDiff) {
48+
public static CoreResult backtranslate(CoreOptions options, String htmlDiff) {
4949
CoreResult result = backtranslateNative(options, htmlDiff);
5050

5151
switch (result.errorCode) {
@@ -68,39 +68,39 @@ public CoreResult backtranslate(CoreOptions options, String htmlDiff) {
6868
return result;
6969
}
7070

71-
private native CoreResult backtranslateNative(CoreOptions options, String htmlDiff);
71+
private static native CoreResult backtranslateNative(CoreOptions options, String htmlDiff);
7272

73-
public void close() {
73+
public static void close() {
7474
CoreOptions options = new CoreOptions();
7575

7676
closeNative(options);
7777
}
7878

79-
private native void closeNative(CoreOptions options);
79+
private static native void closeNative(CoreOptions options);
8080

81-
public void createServer() {
82-
createServerNative();
81+
public static void createServer(String outputPath) {
82+
createServerNative(outputPath);
8383
}
8484

85-
private native void createServerNative();
85+
private static native void createServerNative(String outputPath);
8686

87-
public String hostFile(CoreOptions options) {
87+
public static String hostFile(CoreOptions options) {
8888
return hostFileNative(options);
8989
}
9090

91-
private native String hostFileNative(CoreOptions options);
91+
private static native String hostFileNative(CoreOptions options);
9292

93-
public void listenServer() {
93+
public static void listenServer() {
9494
listenServerNative();
9595
}
9696

97-
private native void listenServerNative();
97+
private static native void listenServerNative();
9898

99-
public void stopServer() {
99+
public static void stopServer() {
100100
stopServerNative();
101101
}
102102

103-
private native void stopServerNative();
103+
private static native void stopServerNative();
104104

105105
public static class CoreOptions {
106106

0 commit comments

Comments
 (0)