defining default_realm_file_directory using application context

This commit is contained in:
Nabil Hachicha 2016-01-11 16:18:15 +00:00
parent 3067ed8bc1
commit 6426d48d57
6 changed files with 37 additions and 8 deletions

View File

@ -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);
}

View File

@ -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<JSContextRef, facebook::react::JSCExecutor*> (*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());

View File

@ -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
}

View File

@ -3,13 +3,15 @@
*/
#include "../platform.hpp"
#include "../js_init.h"
#include <string>
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)

View File

@ -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());

View File

@ -5,6 +5,7 @@
#pragma once
#include <JavaScriptCore/JSBase.h>
#include <string>
#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