Refactor & formatting code
This commit is contained in:
parent
97345e62a9
commit
49a74e5646
|
@ -1,41 +1,39 @@
|
||||||
package io.realm.react;
|
package io.realm.react;
|
||||||
|
|
||||||
import com.facebook.react.bridge.NativeModule;
|
|
||||||
import com.facebook.react.bridge.ReactApplicationContext;
|
|
||||||
import com.facebook.react.bridge.ReactContext;
|
|
||||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
||||||
import com.facebook.react.bridge.ReactMethod;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import android.widget.Toast;
|
|
||||||
import com.facebook.react.bridge.Callback;
|
|
||||||
import android.util.Log;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Map;
|
|
||||||
import fi.iki.elonen.NanoHTTPD;
|
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
import java.util.concurrent.CountDownLatch;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.facebook.react.bridge.ReactApplicationContext;
|
||||||
|
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||||
import com.facebook.soloader.SoLoader;
|
import com.facebook.soloader.SoLoader;
|
||||||
|
|
||||||
public class RealmReactAndroid extends ReactContextBaseJavaModule {
|
import java.io.IOException;
|
||||||
private static final String DURATION_SHORT_KEY = "SHORT";
|
import java.lang.IllegalStateException;
|
||||||
private static final String DURATION_LONG_KEY = "LONG";
|
import java.util.Collections;
|
||||||
private String filesDirPath;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
|
||||||
|
import fi.iki.elonen.NanoHTTPD;
|
||||||
|
|
||||||
|
public class RealmReactAndroid extends ReactContextBaseJavaModule {
|
||||||
private static final int DEFAULT_PORT = 8082;
|
private static final int DEFAULT_PORT = 8082;
|
||||||
|
|
||||||
private AndroidWebServer webServer;
|
private AndroidWebServer webServer;
|
||||||
private long rpcServerPtr;
|
private long rpcServerPtr;
|
||||||
private Handler handler = new Handler(Looper.getMainLooper());
|
|
||||||
|
|
||||||
public RealmReactAndroid(ReactApplicationContext reactContext) {
|
private String filesDirPath;
|
||||||
super(reactContext);
|
private Handler handler = new Handler(Looper.getMainLooper());
|
||||||
|
|
||||||
|
public RealmReactAndroid(ReactApplicationContext reactContext) {
|
||||||
|
super(reactContext);
|
||||||
try {
|
try {
|
||||||
filesDirPath = getReactApplicationContext().getFilesDir().getCanonicalPath();
|
filesDirPath = getReactApplicationContext().getFilesDir().getCanonicalPath();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new IllegalStateException(e);
|
throw new IllegalStateException(e);
|
||||||
}
|
}
|
||||||
SoLoader.loadLibrary("realmreact");
|
SoLoader.loadLibrary("realmreact");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -46,26 +44,34 @@ public class RealmReactAndroid extends ReactContextBaseJavaModule {
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> getConstants() {
|
public Map<String, Object> getConstants() {
|
||||||
long contexts = injectRealmJsContext(filesDirPath);
|
long contexts = injectRealmJsContext(filesDirPath);
|
||||||
|
|
||||||
|
if (contexts == -1) {
|
||||||
|
Log.e("RealmReactAndroid", "Error during initializing Realm context");
|
||||||
|
throw new IllegalStateException("Error during initializing Realm context");
|
||||||
|
}
|
||||||
|
|
||||||
Log.i("RealmReactAndroid", "Initialized " + contexts + " contexts");
|
Log.i("RealmReactAndroid", "Initialized " + contexts + " contexts");
|
||||||
|
|
||||||
if (contexts == 0) {// we're running in Chrome debug mode
|
if (contexts == 0) {// we're running in Chrome debug mode
|
||||||
startWebServer();
|
startWebServer();
|
||||||
}
|
}
|
||||||
return new HashMap<>();
|
return Collections.EMPTY_MAP;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCatalystInstanceDestroy() {
|
public void onCatalystInstanceDestroy() {
|
||||||
if (webServer != null) {
|
if (webServer != null) {
|
||||||
Log.i("RealmReactAndroid", "Stopping the webserver");
|
Log.i("RealmReactAndroid", "Stopping the debugging Webserver");
|
||||||
webServer.stop();
|
webServer.stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startWebServer() {
|
private void startWebServer() {
|
||||||
rpcServerPtr = setupChromeDebugModeRealmJsContext();
|
rpcServerPtr = setupChromeDebugModeRealmJsContext();
|
||||||
webServer = new AndroidWebServer(DEFAULT_PORT);
|
webServer = new AndroidWebServer(DEFAULT_PORT);
|
||||||
try {
|
try {
|
||||||
webServer.start();
|
webServer.start();
|
||||||
Log.i("RealmReactAndroid", "Starting WebServer, Host: " + webServer.getHostname() + " Port: " + webServer.getListeningPort());
|
Log.i("RealmReactAndroid", "Starting the debugging WebServer, Host: " + webServer.getHostname() + " Port: " + webServer.getListeningPort());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -84,7 +90,6 @@ public class RealmReactAndroid extends ReactContextBaseJavaModule {
|
||||||
@Override
|
@Override
|
||||||
public Response serve(IHTTPSession session) {
|
public Response serve(IHTTPSession session) {
|
||||||
final String cmdUri = session.getUri();
|
final String cmdUri = session.getUri();
|
||||||
Log.v("AndroidWebServer", "Session Uri: " + cmdUri + " Mehtod: " + session.getMethod().name());
|
|
||||||
final HashMap<String, String> map = new HashMap<String, String>();
|
final HashMap<String, String> map = new HashMap<String, String>();
|
||||||
try {
|
try {
|
||||||
session.parseBody(map);
|
session.parseBody(map);
|
||||||
|
@ -94,33 +99,34 @@ public class RealmReactAndroid extends ReactContextBaseJavaModule {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
final String json = map.get("postData");
|
final String json = map.get("postData");
|
||||||
Log.v("AndroidWebServer", "Post Data: \n" + json + "\n Thread id: " + Thread.currentThread().getName());
|
final String[] jsonResponse = new String[1];
|
||||||
final String[] jsonResponse = new String[1];
|
final CountDownLatch latch = new CountDownLatch(1);
|
||||||
final CountDownLatch latch = new CountDownLatch(1);
|
// Process the command on the UI thread
|
||||||
// Process the command on the UI thread
|
handler.post(new Runnable() {
|
||||||
handler.post(new Runnable() {
|
@Override
|
||||||
@Override
|
public void run() {
|
||||||
public void run() {
|
jsonResponse[0] = processChromeDebugCommand(rpcServerPtr, cmdUri, json);
|
||||||
jsonResponse[0] = processChromeDebugCommand(rpcServerPtr, cmdUri, json);
|
latch.countDown();
|
||||||
latch.countDown();
|
}
|
||||||
}
|
});
|
||||||
});
|
try {
|
||||||
try {
|
latch.await();
|
||||||
latch.await();
|
Response response = newFixedLengthResponse(jsonResponse[0]);
|
||||||
Response response = newFixedLengthResponse(jsonResponse[0]);
|
response.addHeader("Access-Control-Allow-Origin", "http://localhost:8081");
|
||||||
response.addHeader("Access-Control-Allow-Origin", "http://localhost:8081");
|
return response;
|
||||||
return response;
|
} catch (InterruptedException e) {
|
||||||
} catch (InterruptedException e) {
|
e.printStackTrace();
|
||||||
e.printStackTrace();
|
return null;
|
||||||
return null;
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// fileDir: path of the internal storage of the application
|
// fileDir: path of the internal storage of the application
|
||||||
private native long injectRealmJsContext(String fileDir);
|
private native long injectRealmJsContext(String fileDir);
|
||||||
// responsible for creating the rpcServer that will accept thw chrome Websocket command
|
|
||||||
|
// responsible for creating the rpcServer that will accept the chrome Websocket command
|
||||||
private native long setupChromeDebugModeRealmJsContext();
|
private native long setupChromeDebugModeRealmJsContext();
|
||||||
// this receives one command from Chrome debug, & return the processing we should post back
|
|
||||||
|
// this receives one command from Chrome debug then return the processing we should post back
|
||||||
private native String processChromeDebugCommand(long rpcServerPointer, String cmd, String args);
|
private native String processChromeDebugCommand(long rpcServerPointer, String cmd, String args);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
/* DO NOT EDIT THIS FILE - it is machine generated */
|
|
||||||
#include <jni.h>
|
#include <jni.h>
|
||||||
/* Header for class io_realm_react_RealmReactAndroid */
|
|
||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
@ -30,8 +28,8 @@ JNIEXPORT jlong JNICALL Java_io_realm_react_RealmReactAndroid_injectRealmJsConte
|
||||||
__android_log_print(ANDROID_LOG_DEBUG, "JSRealm", "Java_io_realm_react_RealmReactAndroid_injectRealmJsContext");
|
__android_log_print(ANDROID_LOG_DEBUG, "JSRealm", "Java_io_realm_react_RealmReactAndroid_injectRealmJsContext");
|
||||||
void* handle = dlopen ("libreactnativejni.so", RTLD_LAZY);
|
void* handle = dlopen ("libreactnativejni.so", RTLD_LAZY);
|
||||||
if (!handle) {
|
if (!handle) {
|
||||||
return 0;
|
__android_log_print(ANDROID_LOG_ERROR, "JSRealm", "Cannot open libreactnativejni.so");
|
||||||
//return env->NewStringUTF("Cannot open library");
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getting the internal storage path for the application
|
// Getting the internal storage path for the application
|
||||||
|
@ -39,8 +37,6 @@ JNIEXPORT jlong JNICALL Java_io_realm_react_RealmReactAndroid_injectRealmJsConte
|
||||||
realm::set_default_realm_file_directory(strFileDir);
|
realm::set_default_realm_file_directory(strFileDir);
|
||||||
env->ReleaseStringUTFChars(fileDir , strFileDir);
|
env->ReleaseStringUTFChars(fileDir , strFileDir);
|
||||||
|
|
||||||
__android_log_print(ANDROID_LOG_DEBUG, "JSRealm", "Absolute path %s", realm::default_realm_file_directory().c_str());
|
|
||||||
|
|
||||||
// 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)();
|
||||||
|
|
||||||
|
@ -54,12 +50,9 @@ JNIEXPORT jlong JNICALL Java_io_realm_react_RealmReactAndroid_injectRealmJsConte
|
||||||
RJSInitializeInContext(pair.first);
|
RJSInitializeInContext(pair.first);
|
||||||
}
|
}
|
||||||
return s_globalContextRefToJSCExecutor.size();
|
return s_globalContextRefToJSCExecutor.size();
|
||||||
//std::stringstream msg;
|
|
||||||
//msg << "Got the globalContext map, size=" << s_globalContextRefToJSCExecutor.size();
|
|
||||||
//return env->NewStringUTF(msg.str().c_str());
|
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
__android_log_print(ANDROID_LOG_ERROR, "JSRealm", "Cannot find symbol get_jsc_context");
|
||||||
// return env->NewStringUTF("Cannot find symbol get_jsc_context");
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,24 +11,24 @@ std::string s_default_realm_directory;
|
||||||
|
|
||||||
namespace realm {
|
namespace realm {
|
||||||
|
|
||||||
void set_default_realm_file_directory(std::string dir) {
|
void set_default_realm_file_directory(std::string dir)
|
||||||
s_default_realm_directory = dir;
|
{
|
||||||
}
|
s_default_realm_directory = dir;
|
||||||
|
}
|
||||||
|
|
||||||
std::string default_realm_file_directory()
|
std::string default_realm_file_directory()
|
||||||
{
|
{
|
||||||
return s_default_realm_directory;
|
return s_default_realm_directory;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ensure_directory_exists_for_file(const std::string &fileName)
|
void ensure_directory_exists_for_file(const std::string &fileName)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void remove_realm_files_from_directory(const std::string &directory)
|
|
||||||
{
|
|
||||||
std::string cmd = "rm " + s_default_realm_directory + "/*.realm " +
|
|
||||||
s_default_realm_directory + "/*.realm.lock";
|
|
||||||
system(cmd.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
void remove_realm_files_from_directory(const std::string &directory)
|
||||||
|
{
|
||||||
|
std::string cmd = "rm " + s_default_realm_directory + "/*.realm " +
|
||||||
|
s_default_realm_directory + "/*.realm.lock";
|
||||||
|
system(cmd.c_str());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue