Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package backtraceio.library;

import android.os.Looper;
import android.util.Log;

import androidx.annotation.NonNull;

Expand Down Expand Up @@ -41,10 +42,14 @@ public void start(int timeout, boolean debug) {
return;
}

this.watchdog = new AnrWatchdog(
timeout > 0 ? timeout : AnrWatchdog.DEFAULT_ANR_TIMEOUT,
debug,
this::emitAnrDetected);
try {
this.watchdog = new AnrWatchdog(
timeout > 0 ? timeout : AnrWatchdog.DEFAULT_ANR_TIMEOUT,
debug,
this::emitAnrDetected);
} catch (RuntimeException | OutOfMemoryError e) {
Log.w(NAME, "Failed to start the ANR watchdog (" + e.getClass().getName() + ")");
}
}

@ReactMethod()
Expand All @@ -53,7 +58,11 @@ public void stop() {
return;
}

this.watchdog.stopMonitoring();
try {
this.watchdog.stopMonitoring();
} catch (RuntimeException e) {
Log.w(NAME, "Failed to stop the ANR watchdog (" + e.getClass().getName() + ")");
}
this.watchdog = null;
}

Expand All @@ -70,8 +79,16 @@ public void addListener(String eventName) {}
public void removeListeners(Integer count) {}

private void emitAnrDetected(Map<Thread, StackTraceElement[]> allThreads) {
try {
emitAnrEvent(allThreads);
} catch (RuntimeException e) {
Log.w(NAME, "Failed to report an ANR (" + e.getClass().getName() + ")");
}
}

private void emitAnrEvent(Map<Thread, StackTraceElement[]> allThreads) {
ReactApplicationContext context = getReactApplicationContext();
if (!context.hasActiveReactInstance()) {
if (context == null || !context.hasActiveReactInstance()) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ public String getName() {
@ReactMethod(isBlockingSynchronousMethod = true)
public WritableMap get() {
WritableMap map = new WritableNativeMap();
map.putString("application", this.readApplicationName());
map.putString("application.version", this.readApplicationVersion());
try {
map.putString("application", this.readApplicationName());
map.putString("application.version", this.readApplicationVersion());
} catch (RuntimeException e) {
Log.w(LOG_TAG, "Could not read the application attributes: " + e.getClass().getName());
}
return map;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

import android.content.Context;
import android.os.Build;
import android.util.Log;

import java.util.Locale;

@ReactModule(name = BacktraceDeviceAttributeProvider.NAME)
public class BacktraceDeviceAttributeProvider extends ReactContextBaseJavaModule {
public static final String NAME = "BacktraceDeviceAttributeProvider";
private static final String LOG_TAG = BacktraceDeviceAttributeProvider.class.getSimpleName();

private final Context context;

Expand All @@ -34,14 +36,18 @@ public String getName() {
@ReactMethod(isBlockingSynchronousMethod = true)
public WritableMap get() {
WritableMap map = new WritableNativeMap();
map.putString("culture", this.readCulture());
map.putString("device.model", this.getDeviceModel());
map.putString("device.brand", this.getDeviceBrand());
map.putString("device.product", this.getDeviceProduct());
map.putString("device.sdk", this.getDeviceSdk());
map.putString("device.manufacturer", this.getDeviceManufacturer());
map.putString("cpu.boottime", String.valueOf(java.lang.System.currentTimeMillis() - android.os.SystemClock
.elapsedRealtime()));
try {
map.putString("culture", this.readCulture());
map.putString("device.model", this.getDeviceModel());
map.putString("device.brand", this.getDeviceBrand());
map.putString("device.product", this.getDeviceProduct());
map.putString("device.sdk", this.getDeviceSdk());
map.putString("device.manufacturer", this.getDeviceManufacturer());
map.putString("cpu.boottime", String.valueOf(java.lang.System.currentTimeMillis() - android.os.SystemClock
.elapsedRealtime()));
} catch (RuntimeException e) {
Log.w(LOG_TAG, "Could not read all device attributes: " + e.getClass().getName());
}
return map;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package backtrace.library;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

Expand All @@ -13,12 +14,12 @@
import com.facebook.react.module.annotations.ReactModule;

import java.io.File;
import java.util.stream.Stream;


@ReactModule(name = BacktraceDirectoryProvider.NAME)
public class BacktraceDirectoryProvider extends ReactContextBaseJavaModule {
public static final String NAME = "BacktraceDirectoryProvider";
private static final String LOG_TAG = BacktraceDirectoryProvider.class.getSimpleName();

private final Context context;

Expand All @@ -35,51 +36,57 @@ public String getName() {

@ReactMethod(isBlockingSynchronousMethod = true)
public WritableArray readDirSync(String path) {
File file = new File(path);

WritableArray array = new WritableNativeArray();
for (File directoryFile : file.listFiles()) {
array.pushString(directoryFile.getName());
}

return array;
return listDirectory(path);
}

@ReactMethod
public void readDir(String path, Promise promise) {
File file = new File(path);

WritableArray array = new WritableNativeArray();
for (File directoryFile : file.listFiles()) {
array.pushString(directoryFile.getName());
}

promise.resolve(array);
promise.resolve(listDirectory(path));
}

@ReactMethod(isBlockingSynchronousMethod = true)
public boolean createDirSync(String path) {
File directory = new File(path);

if (directory.exists()) {
return true;
}
return directory.mkdirs();
return createDirectory(path);
}

@ReactMethod
public void createDir(String path, Promise promise) {
File directory = new File(path);

if (directory.exists()) {
promise.resolve(true);
return;
}
promise.resolve(directory.mkdirs());
promise.resolve(createDirectory(path));
}

@ReactMethod(isBlockingSynchronousMethod = true)
public String applicationDirectory() {
return context.getFilesDir().getAbsolutePath();
try {
return context.getFilesDir().getAbsolutePath();
} catch (RuntimeException e) {
Log.w(LOG_TAG, "Cannot resolve the application files directory: " + e.getClass().getName());
return "";
}
}

private WritableArray listDirectory(String path) {
WritableArray array = new WritableNativeArray();
try {
File[] files = new File(path).listFiles();
if (files == null) {
return array;
}
for (File directoryFile : files) {
array.pushString(directoryFile.getName());
}
} catch (RuntimeException e) {
Log.w(LOG_TAG, "Cannot list directory " + path + ": " + e.getClass().getName());
}
return array;
}

private boolean createDirectory(String path) {
try {
File directory = new File(path);
return directory.exists() || directory.mkdirs();
} catch (RuntimeException e) {
Log.w(LOG_TAG, "Cannot create directory " + path + ": " + e.getClass().getName());
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,35 +102,41 @@ public void writeFile(String path, String content, Promise promise) {

@ReactMethod(isBlockingSynchronousMethod = true)
public boolean unlinkSync(String path) {
File file = new File(path);
if (!file.exists()) {
return true;
}
Boolean result = file.delete();
return result;
return delete(path);
}

@ReactMethod
public void unlink(String filePath, Promise promise) {
File file = new File(filePath);
if (!file.exists()) {
promise.resolve(true);
return;
}
Boolean result = file.delete();
promise.resolve(result);
promise.resolve(delete(filePath));
}

@ReactMethod(isBlockingSynchronousMethod = true)
public boolean existsSync(String path) {
File file = new File(path);
return file.exists();
return exists(path);
}

@ReactMethod
public void exists(String path, Promise promise) {
File file = new File(path);
promise.resolve(file.exists());
promise.resolve(exists(path));
}

private boolean delete(String path) {
try {
File file = new File(path);
return !file.exists() || file.delete();
} catch (RuntimeException e) {
Log.d(LOG_TAG, "Cannot delete " + path + ": " + e.getClass().getName());
return false;
}
}

private boolean exists(String path) {
try {
return new File(path).exists();
} catch (RuntimeException e) {
Log.d(LOG_TAG, "Cannot check " + path + ": " + e.getClass().getName());
return false;
}
}

@ReactMethod(isBlockingSynchronousMethod = true)
Expand Down
Loading
Loading