diff --git a/android/src/main/java/land/fx/FulaModule.java b/android/src/main/java/land/fx/FulaModule.java index 100a402..e87073f 100644 --- a/android/src/main/java/land/fx/FulaModule.java +++ b/android/src/main/java/land/fx/FulaModule.java @@ -65,107 +65,120 @@ public String getName() { @ReactMethod public void send(String path, Promise promise) { - try{ + ThreadUtils.runOnExecutor(() -> { + try{ String cid = fula.send(path); promise.resolve(cid); - } - catch(Exception e){ + } + catch(Exception e){ promise.reject(e); - } + } + }); } @ReactMethod public void encryptSend(String path, Promise promise) { - try{ + ThreadUtils.runOnExecutor(() -> { + try{ String res = fula.encryptSend(path); Log.d(NAME,res); promise.resolve(res); - } - catch(Exception e){ + } + catch(Exception e){ promise.reject(e); - } + } + }); } @ReactMethod public void addBox(String boxId, Promise promise) { - Log.d("fulaModule", appDir); - try{ + ThreadUtils.runOnExecutor(() -> { + try{ fula.addBox(boxId); promise.resolve(true); - } - catch(Exception e){ + } + catch(Exception e){ promise.reject(e); - } + } + }); } @ReactMethod public void receiveFileInfo(String fileId, Promise promise){ - try{ + ThreadUtils.runOnExecutor(() -> { + try{ byte[] res = fula.receiveFileInfo(fileId); WritableNativeArray arr = new WritableNativeArray(); for(byte b : res){ - arr.pushInt(b); + arr.pushInt(b); } promise.resolve(arr); - }catch (Exception e){ + }catch (Exception e){ promise.reject(e); - } + } + }); } @ReactMethod public void receiveFile(String fileId, String fileName, boolean includeBS64,Promise promise) { - try{ + ThreadUtils.runOnExecutor(() -> { + try{ String filePath = storeDirPath + fileName; fula.receiveFile(fileId, filePath); Uri uri = Uri.fromFile(new File(filePath)); Log.d(NAME,"File Downloaded"); WritableMap map; if(includeBS64){ - String bs64 = getBase64StringFromFile(filePath); - Log.d(NAME,"File Transform to bs64"); - map = makeResponseMap(uri.toString(), bs64); + String bs64 = getBase64StringFromFile(filePath); + Log.d(NAME,"File Transform to bs64"); + map = makeResponseMap(uri.toString(), bs64); }else{ - map = makeResponseMap(uri.toString(), ""); + map = makeResponseMap(uri.toString(), ""); } promise.resolve(map); - }catch (Exception e){ + }catch (Exception e){ promise.reject(e); - } + } + }); } @ReactMethod public void receiveDecryptFile(String fileRef, String fileName, boolean includeBS64,Promise promise) { - try{ + ThreadUtils.runOnExecutor(() -> { + try{ String filePath = storeDirPath + fileName; fula.receiveDecryptFile(fileRef, filePath); Uri uri = Uri.fromFile(new File(filePath)); Log.d(NAME,"File Downloaded"); WritableMap map; if(includeBS64){ - String bs64 = getBase64StringFromFile(filePath); - Log.d(NAME,"File Transform to bs64"); - map = makeResponseMap(uri.toString(), bs64); + String bs64 = getBase64StringFromFile(filePath); + Log.d(NAME,"File Transform to bs64"); + map = makeResponseMap(uri.toString(), bs64); }else{ - map = makeResponseMap(uri.toString(), ""); + map = makeResponseMap(uri.toString(), ""); } promise.resolve(map); - }catch (Exception e){ + }catch (Exception e){ promise.reject(e); - } + } + }); } @ReactMethod public void graphQL(String query, String variableValues, Promise promise) { - try{ + ThreadUtils.runOnExecutor(() -> { + try{ byte[] res = fula.graphQL(query, variableValues); WritableNativeArray arr = new WritableNativeArray(); for(byte b : res){ - arr.pushInt(b); + arr.pushInt(b); } promise.resolve(arr); - }catch (Exception e){ + }catch (Exception e){ promise.reject(e); - } + } + }); } private static WritableMap makeResponseMap(String uri, String base64) { diff --git a/android/src/main/java/land/fx/ThreadUtils.java b/android/src/main/java/land/fx/ThreadUtils.java new file mode 100644 index 0000000..78fdbac --- /dev/null +++ b/android/src/main/java/land/fx/ThreadUtils.java @@ -0,0 +1,42 @@ +package land.fx.fula; + +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +final class ThreadUtils { + /** + * Thread which will be used to call all Libp2p APIs. They + * they don't run on the calling thread anyway, we are deferring the calls + * to this thread to avoid (potentially) blocking the calling thread. + */ + private static final ExecutorService executor + = Executors.newSingleThreadExecutor(); + + /** + * Runs the given {@link Runnable} on the executor. + * @param runnable + */ + public static void runOnExecutor(Runnable runnable) { + executor.execute(runnable); + } + + /** + * Submits the given {@link Callable} to be run on the executor. + * @param callable + * @return Future. + */ + public static Future submitToExecutor(Callable callable) { + return executor.submit(callable); + } + + /** + * Submits the given {@link Runnable} to be run on the executor. + * @param runnable + * @return Future. + */ + public static Future submitToExecutor(Runnable runnable) { + return executor.submit(runnable); + } +}