Refactor & formatting code
This commit is contained in:
parent
97345e62a9
commit
49a74e5646
|
@ -1,41 +1,39 @@
|
|||
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.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;
|
||||
|
||||
public class RealmReactAndroid extends ReactContextBaseJavaModule {
|
||||
private static final String DURATION_SHORT_KEY = "SHORT";
|
||||
private static final String DURATION_LONG_KEY = "LONG";
|
||||
private String filesDirPath;
|
||||
import java.io.IOException;
|
||||
import java.lang.IllegalStateException;
|
||||
import java.util.Collections;
|
||||
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 AndroidWebServer webServer;
|
||||
private long rpcServerPtr;
|
||||
private Handler handler = new Handler(Looper.getMainLooper());
|
||||
|
||||
public RealmReactAndroid(ReactApplicationContext reactContext) {
|
||||
super(reactContext);
|
||||
private String filesDirPath;
|
||||
private Handler handler = new Handler(Looper.getMainLooper());
|
||||
|
||||
public RealmReactAndroid(ReactApplicationContext reactContext) {
|
||||
super(reactContext);
|
||||
try {
|
||||
filesDirPath = getReactApplicationContext().getFilesDir().getCanonicalPath();
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
SoLoader.loadLibrary("realmreact");
|
||||
SoLoader.loadLibrary("realmreact");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -46,26 +44,34 @@ public class RealmReactAndroid extends ReactContextBaseJavaModule {
|
|||
@Override
|
||||
public Map<String, Object> getConstants() {
|
||||
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");
|
||||
|
||||
if (contexts == 0) {// we're running in Chrome debug mode
|
||||
startWebServer();
|
||||
}
|
||||
return new HashMap<>();
|
||||
return Collections.EMPTY_MAP;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCatalystInstanceDestroy() {
|
||||
if (webServer != null) {
|
||||
Log.i("RealmReactAndroid", "Stopping the webserver");
|
||||
if (webServer != null) {
|
||||
Log.i("RealmReactAndroid", "Stopping the debugging Webserver");
|
||||
webServer.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void startWebServer() {
|
||||
rpcServerPtr = setupChromeDebugModeRealmJsContext();
|
||||
webServer = new AndroidWebServer(DEFAULT_PORT);
|
||||
try {
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -84,7 +90,6 @@ public class RealmReactAndroid extends ReactContextBaseJavaModule {
|
|||
@Override
|
||||
public Response serve(IHTTPSession session) {
|
||||
final String cmdUri = session.getUri();
|
||||
Log.v("AndroidWebServer", "Session Uri: " + cmdUri + " Mehtod: " + session.getMethod().name());
|
||||
final HashMap<String, String> map = new HashMap<String, String>();
|
||||
try {
|
||||
session.parseBody(map);
|
||||
|
@ -94,33 +99,34 @@ public class RealmReactAndroid extends ReactContextBaseJavaModule {
|
|||
e.printStackTrace();
|
||||
}
|
||||
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 CountDownLatch latch = new CountDownLatch(1);
|
||||
// Process the command on the UI thread
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
jsonResponse[0] = processChromeDebugCommand(rpcServerPtr, cmdUri, json);
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
try {
|
||||
latch.await();
|
||||
Response response = newFixedLengthResponse(jsonResponse[0]);
|
||||
response.addHeader("Access-Control-Allow-Origin", "http://localhost:8081");
|
||||
return response;
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
final String[] jsonResponse = new String[1];
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
// Process the command on the UI thread
|
||||
handler.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
jsonResponse[0] = processChromeDebugCommand(rpcServerPtr, cmdUri, json);
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
try {
|
||||
latch.await();
|
||||
Response response = newFixedLengthResponse(jsonResponse[0]);
|
||||
response.addHeader("Access-Control-Allow-Origin", "http://localhost:8081");
|
||||
return response;
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// fileDir: path of the internal storage of the application
|
||||
private native long injectRealmJsContext(String fileDir);
|
||||
// responsible for creating the rpcServer that will accept thw chrome Websocket command
|
||||
private native long injectRealmJsContext(String fileDir);
|
||||
|
||||
// responsible for creating the rpcServer that will accept the chrome Websocket command
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
/* DO NOT EDIT THIS FILE - it is machine generated */
|
||||
#include <jni.h>
|
||||
/* Header for class io_realm_react_RealmReactAndroid */
|
||||
#include <dlfcn.h>
|
||||
#include <iostream>
|
||||
#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");
|
||||
void* handle = dlopen ("libreactnativejni.so", RTLD_LAZY);
|
||||
if (!handle) {
|
||||
return 0;
|
||||
//return env->NewStringUTF("Cannot open library");
|
||||
__android_log_print(ANDROID_LOG_ERROR, "JSRealm", "Cannot open libreactnativejni.so");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 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);
|
||||
env->ReleaseStringUTFChars(fileDir , strFileDir);
|
||||
|
||||
__android_log_print(ANDROID_LOG_DEBUG, "JSRealm", "Absolute path %s", realm::default_realm_file_directory().c_str());
|
||||
|
||||
// load the symbol
|
||||
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);
|
||||
}
|
||||
return s_globalContextRefToJSCExecutor.size();
|
||||
//std::stringstream msg;
|
||||
//msg << "Got the globalContext map, size=" << s_globalContextRefToJSCExecutor.size();
|
||||
//return env->NewStringUTF(msg.str().c_str());
|
||||
} else {
|
||||
return 0;
|
||||
// return env->NewStringUTF("Cannot find symbol get_jsc_context");
|
||||
__android_log_print(ANDROID_LOG_ERROR, "JSRealm", "Cannot find symbol get_jsc_context");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,24 +11,24 @@ std::string s_default_realm_directory;
|
|||
|
||||
namespace realm {
|
||||
|
||||
void set_default_realm_file_directory(std::string dir) {
|
||||
s_default_realm_directory = dir;
|
||||
}
|
||||
void set_default_realm_file_directory(std::string dir)
|
||||
{
|
||||
s_default_realm_directory = dir;
|
||||
}
|
||||
|
||||
std::string default_realm_file_directory()
|
||||
{
|
||||
return s_default_realm_directory;
|
||||
}
|
||||
std::string default_realm_file_directory()
|
||||
{
|
||||
return s_default_realm_directory;
|
||||
}
|
||||
|
||||
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 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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue