From 9c97b592828fd39a8ec9b895f5203d0d2757cd81 Mon Sep 17 00:00:00 2001 From: Daria Bialobrzeska <88393714+DariaKunoichi@users.noreply.github.com> Date: Mon, 2 Dec 2024 11:48:34 +0100 Subject: [PATCH] Add android implementation for FileSystem operations (#537) * Change visibility of NativeBugsnagPerformanceImpl methods --- .../NativeBugsnagPerformanceImpl.java | 111 +++++++++++++++++- .../BugsnagReactNativePerformance.java | 40 +++++++ .../BugsnagReactNativePerformance.java | 40 +++++++ 3 files changed, 188 insertions(+), 3 deletions(-) diff --git a/packages/platforms/react-native/android/src/main/java/com/bugsnag/android/performance/NativeBugsnagPerformanceImpl.java b/packages/platforms/react-native/android/src/main/java/com/bugsnag/android/performance/NativeBugsnagPerformanceImpl.java index 6c10268eb..2c89f5b16 100644 --- a/packages/platforms/react-native/android/src/main/java/com/bugsnag/android/performance/NativeBugsnagPerformanceImpl.java +++ b/packages/platforms/react-native/android/src/main/java/com/bugsnag/android/performance/NativeBugsnagPerformanceImpl.java @@ -12,6 +12,13 @@ import com.facebook.react.bridge.WritableArray; import com.facebook.react.bridge.WritableMap; import java.security.SecureRandom; +import java.io.File; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.io.FileInputStream; +import java.io.FileOutputStream; class NativeBugsnagPerformanceImpl { @@ -25,7 +32,7 @@ public NativeBugsnagPerformanceImpl(ReactApplicationContext reactContext) { this.reactContext = reactContext; } - public WritableMap getDeviceInfo() { + WritableMap getDeviceInfo() { WritableMap map = Arguments.createMap(); try { String bundleIdentifier = this.reactContext.getPackageName(); @@ -55,7 +62,7 @@ public WritableMap getDeviceInfo() { return map; } - public String requestEntropy() { + String requestEntropy() { byte[] bytes = new byte[1024]; random.nextBytes(bytes); @@ -70,10 +77,108 @@ public String requestEntropy() { return hex.toString(); } - public void requestEntropyAsync(Promise promise) { + void requestEntropyAsync(Promise promise) { promise.resolve(requestEntropy()); } + WritableMap getNativeConstants() { + WritableMap map = Arguments.createMap(); + map.putString("CacheDir", this.reactContext.getCacheDir().getAbsolutePath()); + map.putString("DocumentDir", this.reactContext.getFilesDir().getAbsolutePath()); + + return map; + } + + void exists(String path, Promise promise) { + try { + boolean result = new File(path).exists(); + if (result) { + promise.resolve(result); + } else { + promise.reject(new Exception("File does not exist")); + } + } catch(Exception e) { + promise.reject(e); + } + } + + void isDir(String path, Promise promise) { + try { + boolean result = new File(path).isDirectory(); + if (result) { + promise.resolve(result); + } else { + promise.reject(new Exception("Path is not a directory")); + } + } catch(Exception e) { + promise.reject(e); + } + } + + void ls(String path, Promise promise) { + try { + promise.resolve(new File(path).list()); + } catch(Exception e) { + promise.reject(e); + } + } + + void mkdir(String path, Promise promise) { + try { + boolean result = new File(path).mkdir(); + if (result) { + promise.resolve(path); + } else { + promise.reject(new Exception("Failed to create directory")); + } + } catch(Exception e) { + promise.reject(e); + } + } + + void readFile(String path, String encoding, Promise promise) { + File file = new File(path); + StringBuilder fileContent = new StringBuilder((int) file.length()); + try( + FileInputStream fin = new FileInputStream(file); + InputStreamReader isr = new InputStreamReader(fin, encoding); + ) { + char[] buffer = new char[4096]; + int charsRead = 0; + while ((charsRead = isr.read(buffer)) != -1) { + fileContent.append(buffer, 0, charsRead); + } + promise.resolve(fileContent.toString()); + } catch (Exception e) { + promise.reject(e); + } + } + + void unlink(String path, Promise promise) { + try { + boolean result = new File(path).delete(); + if (result) { + promise.resolve(null); + } else { + promise.reject(new Exception("Failed to delete file/directory")); + } + } catch(Exception e) { + promise.reject(e); + } + } + + void writeFile(String path, String data, String encoding, Promise promise){ + try( + FileOutputStream fout = new FileOutputStream(path); + Writer w = new OutputStreamWriter(fout, encoding); + ) { + w.write(data); + promise.resolve(null); + } catch (Exception e) { + promise.reject(e); + } + } + @Nullable private String abiToArchitecture(@Nullable String abi) { if (abi == null) { diff --git a/packages/platforms/react-native/android/src/newarch/java/com/bugsnag/android/performance/BugsnagReactNativePerformance.java b/packages/platforms/react-native/android/src/newarch/java/com/bugsnag/android/performance/BugsnagReactNativePerformance.java index 31e8aea46..7eb0fa218 100644 --- a/packages/platforms/react-native/android/src/newarch/java/com/bugsnag/android/performance/BugsnagReactNativePerformance.java +++ b/packages/platforms/react-native/android/src/newarch/java/com/bugsnag/android/performance/BugsnagReactNativePerformance.java @@ -34,5 +34,45 @@ public String requestEntropy() { public void requestEntropyAsync(Promise promise) { impl.requestEntropyAsync(promise); } + + @Override + public WritableMap getNativeConstants() { + return impl.getNativeConstants(); + } + + @Override + public void exists(String path, Promise promise) { + impl.exists(path, promise); + } + + @Override + public void isDir(String path, Promise promise) { + impl.isDir(path, promise); + } + + @Override + public void ls(String path, Promise promise) { + impl.ls(path, promise); + } + + @Override + public void mkdir(String path, Promise promise) { + impl.mkdir(path, promise); + } + + @Override + public void readFile(String path, String encoding, Promise promise) { + impl.readFile(path, encoding, promise); + } + + @Override + public void unlink(String path, Promise promise) { + impl.unlink(path, promise); + } + + @Override + public void writeFile(String path, String data, String encoding, Promise promise){ + impl.writeFile(path, data, encoding, promise); + } } diff --git a/packages/platforms/react-native/android/src/oldarch/java/com/bugsnag/android/performance/BugsnagReactNativePerformance.java b/packages/platforms/react-native/android/src/oldarch/java/com/bugsnag/android/performance/BugsnagReactNativePerformance.java index d54f3d6a2..e376b2aab 100644 --- a/packages/platforms/react-native/android/src/oldarch/java/com/bugsnag/android/performance/BugsnagReactNativePerformance.java +++ b/packages/platforms/react-native/android/src/oldarch/java/com/bugsnag/android/performance/BugsnagReactNativePerformance.java @@ -36,4 +36,44 @@ public String requestEntropy() { public void requestEntropyAsync(Promise promise) { impl.requestEntropyAsync(promise); } + + @ReactMethod(isBlockingSynchronousMethod = true) + public WritableMap getNativeConstants() { + return impl.getNativeConstants(); + } + + @ReactMethod + public void exists(String path, Promise promise) { + impl.exists(path, promise); + } + + @ReactMethod + public void isDir(String path, Promise promise) { + impl.isDir(path, promise); + } + + @ReactMethod + public void ls(String path, Promise promise) { + impl.ls(path, promise); + } + + @ReactMethod + public void mkdir(String path, Promise promise) { + impl.mkdir(path, promise); + } + + @ReactMethod + public void readFile(String path, String encoding, Promise promise) { + impl.readFile(path, encoding, promise); + } + + @ReactMethod + public void unlink(String path, Promise promise) { + impl.unlink(path, promise); + } + + @ReactMethod + public void writeFile(String path, String data, String encoding, Promise promise) { + impl.writeFile(path, data, encoding, promise); + } }