mirror of
https://github.com/status-im/realm-js.git
synced 2025-01-11 14:54:33 +00:00
defining default_realm_file_directory using application context
This commit is contained in:
parent
3067ed8bc1
commit
6426d48d57
@ -11,19 +11,26 @@ import java.util.HashMap;
|
|||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
import com.facebook.react.bridge.Callback;
|
import com.facebook.react.bridge.Callback;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
public class RealmReactAndroid extends ReactContextBaseJavaModule {
|
public class RealmReactAndroid extends ReactContextBaseJavaModule {
|
||||||
private static final String DURATION_SHORT_KEY = "SHORT";
|
private static final String DURATION_SHORT_KEY = "SHORT";
|
||||||
private static final String DURATION_LONG_KEY = "LONG";
|
private static final String DURATION_LONG_KEY = "LONG";
|
||||||
|
private String filesDirPath;
|
||||||
|
|
||||||
public RealmReactAndroid(ReactApplicationContext reactContext) {
|
public RealmReactAndroid(ReactApplicationContext reactContext) {
|
||||||
super(reactContext);
|
super(reactContext);
|
||||||
|
try {
|
||||||
|
filesDirPath = getReactApplicationContext().getFilesDir().getCanonicalPath();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IllegalStateException(e);
|
||||||
|
}
|
||||||
ReLinker.loadLibrary(reactContext, "realmreact");
|
ReLinker.loadLibrary(reactContext, "realmreact");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
Log.w("RealmReactAndroid", injectRealmJsContext());
|
Log.w("RealmReactAndroid", injectRealmJsContext(filesDirPath));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -37,7 +44,7 @@ public class RealmReactAndroid extends ReactContextBaseJavaModule {
|
|||||||
constants.put(DURATION_SHORT_KEY, Toast.LENGTH_SHORT);
|
constants.put(DURATION_SHORT_KEY, Toast.LENGTH_SHORT);
|
||||||
constants.put(DURATION_LONG_KEY, Toast.LENGTH_LONG);
|
constants.put(DURATION_LONG_KEY, Toast.LENGTH_LONG);
|
||||||
|
|
||||||
Log.w("RealmReactAndroid", injectRealmJsContext());
|
Log.w("RealmReactAndroid", injectRealmJsContext(filesDirPath));
|
||||||
|
|
||||||
return constants;
|
return constants;
|
||||||
}
|
}
|
||||||
@ -45,7 +52,7 @@ public class RealmReactAndroid extends ReactContextBaseJavaModule {
|
|||||||
@ReactMethod
|
@ReactMethod
|
||||||
public void resultOfJsContextInjection(Callback successCallback) {
|
public void resultOfJsContextInjection(Callback successCallback) {
|
||||||
// Inject our JS Context
|
// Inject our JS Context
|
||||||
successCallback.invoke(injectRealmJsContext());
|
successCallback.invoke(injectRealmJsContext(filesDirPath));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ReactMethod
|
@ReactMethod
|
||||||
@ -53,5 +60,6 @@ public class RealmReactAndroid extends ReactContextBaseJavaModule {
|
|||||||
Toast.makeText(getReactApplicationContext(), message, duration).show();
|
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);
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
* Signature: ()Ljava/lang/String;
|
* Signature: ()Ljava/lang/String;
|
||||||
*/
|
*/
|
||||||
JNIEXPORT jstring JNICALL Java_com_reacttests_RealmReactAndroid_injectRealmJsContext
|
JNIEXPORT jstring JNICALL Java_com_reacttests_RealmReactAndroid_injectRealmJsContext
|
||||||
(JNIEnv *env, jclass)
|
(JNIEnv *env, jclass, jstring fileDir)
|
||||||
{
|
{
|
||||||
|
|
||||||
void* handle = dlopen ("libreactnativejni.so", RTLD_LAZY);
|
void* handle = dlopen ("libreactnativejni.so", RTLD_LAZY);
|
||||||
@ -24,6 +24,11 @@ JNIEXPORT jstring JNICALL Java_com_reacttests_RealmReactAndroid_injectRealmJsCon
|
|||||||
return env->NewStringUTF("Cannot open library");
|
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
|
// load the symbol
|
||||||
typedef std::unordered_map<JSContextRef, facebook::react::JSCExecutor*> (*get_jsc_context_t)();
|
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();
|
msg << "Got the globalContext map, size=" << s_globalContextRefToJSCExecutor.size();
|
||||||
|
|
||||||
for (auto pair : s_globalContextRefToJSCExecutor) {
|
for (auto pair : s_globalContextRefToJSCExecutor) {
|
||||||
RJSInitializeInContext(pair.first);
|
RJSInitializeInContextUsingPath(pair.first, absoluteAppPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
return env->NewStringUTF(msg.str().c_str());
|
return env->NewStringUTF(msg.str().c_str());
|
||||||
|
@ -13,7 +13,7 @@ extern "C" {
|
|||||||
* Signature: ()Ljava/lang/String;
|
* Signature: ()Ljava/lang/String;
|
||||||
*/
|
*/
|
||||||
JNIEXPORT jstring JNICALL Java_com_reacttests_RealmReactAndroid_injectRealmJsContext
|
JNIEXPORT jstring JNICALL Java_com_reacttests_RealmReactAndroid_injectRealmJsContext
|
||||||
(JNIEnv *, jclass);
|
(JNIEnv *, jclass, jstring);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
@ -3,13 +3,15 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "../platform.hpp"
|
#include "../platform.hpp"
|
||||||
|
#include "../js_init.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace realm {
|
namespace realm {
|
||||||
|
|
||||||
std::string default_realm_file_directory()
|
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)
|
void ensure_directory_exists_for_file(const std::string &fileName)
|
||||||
|
@ -78,6 +78,16 @@ void RJSInitializeInContext(JSContextRef ctx) {
|
|||||||
assert(!exception);
|
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() {
|
void RJSClearTestState() {
|
||||||
realm::Realm::s_global_cache.clear();
|
realm::Realm::s_global_cache.clear();
|
||||||
realm::remove_realm_files_from_directory(realm::default_realm_file_directory());
|
realm::remove_realm_files_from_directory(realm::default_realm_file_directory());
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <JavaScriptCore/JSBase.h>
|
#include <JavaScriptCore/JSBase.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@ -12,8 +13,11 @@ extern "C" {
|
|||||||
|
|
||||||
JSObjectRef RJSConstructorCreate(JSContextRef ctx);
|
JSObjectRef RJSConstructorCreate(JSContextRef ctx);
|
||||||
void RJSInitializeInContext(JSContextRef ctx);
|
void RJSInitializeInContext(JSContextRef ctx);
|
||||||
|
void RJSInitializeInContextUsingPath(JSContextRef ctx, std::string path);
|
||||||
void RJSClearTestState(void);
|
void RJSClearTestState(void);
|
||||||
|
|
||||||
|
extern std::string appFilesDir;
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user