diff --git a/react-native/android/app/src/main/java/com/reacttests/RealmReactAndroid.java b/react-native/android/app/src/main/java/com/reacttests/RealmReactAndroid.java index c5f3efd7..ea7c248b 100644 --- a/react-native/android/app/src/main/java/com/reacttests/RealmReactAndroid.java +++ b/react-native/android/app/src/main/java/com/reacttests/RealmReactAndroid.java @@ -11,19 +11,26 @@ import java.util.HashMap; import android.widget.Toast; import com.facebook.react.bridge.Callback; import android.util.Log; +import java.io.IOException; public class RealmReactAndroid extends ReactContextBaseJavaModule { private static final String DURATION_SHORT_KEY = "SHORT"; private static final String DURATION_LONG_KEY = "LONG"; + private String filesDirPath; public RealmReactAndroid(ReactApplicationContext reactContext) { super(reactContext); + try { + filesDirPath = getReactApplicationContext().getFilesDir().getCanonicalPath(); + } catch (IOException e) { + throw new IllegalStateException(e); + } ReLinker.loadLibrary(reactContext, "realmreact"); } @Override public void initialize() { - Log.w("RealmReactAndroid", injectRealmJsContext()); + Log.w("RealmReactAndroid", injectRealmJsContext(filesDirPath)); } @Override @@ -37,7 +44,7 @@ public class RealmReactAndroid extends ReactContextBaseJavaModule { constants.put(DURATION_SHORT_KEY, Toast.LENGTH_SHORT); constants.put(DURATION_LONG_KEY, Toast.LENGTH_LONG); - Log.w("RealmReactAndroid", injectRealmJsContext()); + Log.w("RealmReactAndroid", injectRealmJsContext(filesDirPath)); return constants; } @@ -45,7 +52,7 @@ public class RealmReactAndroid extends ReactContextBaseJavaModule { @ReactMethod public void resultOfJsContextInjection(Callback successCallback) { // Inject our JS Context - successCallback.invoke(injectRealmJsContext()); + successCallback.invoke(injectRealmJsContext(filesDirPath)); } @ReactMethod @@ -53,5 +60,6 @@ public class RealmReactAndroid extends ReactContextBaseJavaModule { Toast.makeText(getReactApplicationContext(), message, duration).show(); } - private native String injectRealmJsContext(); + // fileDir: path of the internal storage of the application + private native String injectRealmJsContext(String fileDir); } diff --git a/src/android/com_reacttests_RealmReactAndroid.cpp b/src/android/com_reacttests_RealmReactAndroid.cpp index 70297588..3fb2da0b 100644 --- a/src/android/com_reacttests_RealmReactAndroid.cpp +++ b/src/android/com_reacttests_RealmReactAndroid.cpp @@ -16,13 +16,18 @@ * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_reacttests_RealmReactAndroid_injectRealmJsContext - (JNIEnv *env, jclass) + (JNIEnv *env, jclass, jstring fileDir) { void* handle = dlopen ("libreactnativejni.so", RTLD_LAZY); if (!handle) { return env->NewStringUTF("Cannot open library"); } + + // Getting the internal storage path for the application + const char* strFileDir = env->GetStringUTFChars(fileDir , NULL); + std::string absoluteAppPath(strFileDir); + env->ReleaseStringUTFChars(fileDir , strFileDir); // load the symbol typedef std::unordered_map (*get_jsc_context_t)(); @@ -35,7 +40,7 @@ JNIEXPORT jstring JNICALL Java_com_reacttests_RealmReactAndroid_injectRealmJsCon msg << "Got the globalContext map, size=" << s_globalContextRefToJSCExecutor.size(); for (auto pair : s_globalContextRefToJSCExecutor) { - RJSInitializeInContext(pair.first); + RJSInitializeInContextUsingPath(pair.first, absoluteAppPath); } return env->NewStringUTF(msg.str().c_str()); diff --git a/src/android/com_reacttests_RealmReactAndroid.h b/src/android/com_reacttests_RealmReactAndroid.h index 156d09b7..edfb76a0 100644 --- a/src/android/com_reacttests_RealmReactAndroid.h +++ b/src/android/com_reacttests_RealmReactAndroid.h @@ -13,7 +13,7 @@ extern "C" { * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_reacttests_RealmReactAndroid_injectRealmJsContext - (JNIEnv *, jclass); + (JNIEnv *, jclass, jstring); #ifdef __cplusplus } diff --git a/src/android/platform.cpp b/src/android/platform.cpp index 2952643f..a2ad3b3c 100644 --- a/src/android/platform.cpp +++ b/src/android/platform.cpp @@ -3,13 +3,15 @@ */ #include "../platform.hpp" + #include "../js_init.h" #include namespace realm { std::string default_realm_file_directory() { - return std::string("/data/data/com.demo/files/"); + // appFilesDir is defined in js_init.cpp + return appFilesDir; } void ensure_directory_exists_for_file(const std::string &fileName) diff --git a/src/js_init.cpp b/src/js_init.cpp index 09572007..7659f77d 100644 --- a/src/js_init.cpp +++ b/src/js_init.cpp @@ -78,6 +78,16 @@ void RJSInitializeInContext(JSContextRef ctx) { assert(!exception); } +// The default (internal) storage for each application is unique +// the only way to get this path is using the android.content.Context via the JNI +// we set this path when we initialise the Realm by calling RJSConstructorCreate, as it's the +// only contact between the JNI layer and the Realm JS API. +std::string appFilesDir; +void RJSInitializeInContextUsingPath(JSContextRef ctx, std::string path) { + RJSInitializeInContext(ctx); + appFilesDir = path; +} + void RJSClearTestState() { realm::Realm::s_global_cache.clear(); realm::remove_realm_files_from_directory(realm::default_realm_file_directory()); diff --git a/src/js_init.h b/src/js_init.h index 876844e2..95b6757f 100644 --- a/src/js_init.h +++ b/src/js_init.h @@ -5,6 +5,7 @@ #pragma once #include +#include #ifdef __cplusplus extern "C" { @@ -12,8 +13,11 @@ extern "C" { JSObjectRef RJSConstructorCreate(JSContextRef ctx); void RJSInitializeInContext(JSContextRef ctx); +void RJSInitializeInContextUsingPath(JSContextRef ctx, std::string path); void RJSClearTestState(void); +extern std::string appFilesDir; + #ifdef __cplusplus } #endif