Skip to content

Commit

Permalink
Add android implementation for FileSystem operations (#537)
Browse files Browse the repository at this point in the history
* Change visibility of NativeBugsnagPerformanceImpl methods
  • Loading branch information
DariaKunoichi authored Dec 2, 2024
1 parent 77f7558 commit 9c97b59
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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();
Expand Down Expand Up @@ -55,7 +62,7 @@ public WritableMap getDeviceInfo() {
return map;
}

public String requestEntropy() {
String requestEntropy() {
byte[] bytes = new byte[1024];
random.nextBytes(bytes);

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 9c97b59

Please sign in to comment.