Skip to content

Commit

Permalink
refactor(crashlytics): remove deprecated Tasks.call for android and…
Browse files Browse the repository at this point in the history
… replace with `TaskCompletionSource`. (#8582)
  • Loading branch information
russellwheatley committed May 9, 2022
1 parent 72158aa commit 9539c92
Showing 1 changed file with 198 additions and 107 deletions.
Expand Up @@ -10,6 +10,7 @@
import android.util.Log;
import androidx.annotation.NonNull;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.FirebaseApp;
import com.google.firebase.crashlytics.FirebaseCrashlytics;
Expand Down Expand Up @@ -54,18 +55,26 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
}

private Task<Map<String, Object>> checkForUnsentReports() {
return Tasks.call(
cachedThreadPool,
() -> {
final boolean unsentReports =
Tasks.await(FirebaseCrashlytics.getInstance().checkForUnsentReports());
TaskCompletionSource<Map<String, Object>> taskCompletionSource = new TaskCompletionSource<>();

return new HashMap<String, Object>() {
{
put(Constants.UNSENT_REPORTS, unsentReports);
}
};
cachedThreadPool.execute(
() -> {
try {
final boolean unsentReports =
Tasks.await(FirebaseCrashlytics.getInstance().checkForUnsentReports());

taskCompletionSource.setResult(
new HashMap<String, Object>() {
{
put(Constants.UNSENT_REPORTS, unsentReports);
}
});
} catch (Exception e) {
taskCompletionSource.setException(e);
}
});

return taskCompletionSource.getTask();
}

private void crash() {
Expand All @@ -78,137 +87,198 @@ private void crash() {
}

private Task<Void> deleteUnsentReports() {
return Tasks.call(
cachedThreadPool,
TaskCompletionSource<Void> taskCompletionSource = new TaskCompletionSource<>();

cachedThreadPool.execute(
() -> {
FirebaseCrashlytics.getInstance().deleteUnsentReports();
return null;
try {
FirebaseCrashlytics.getInstance().deleteUnsentReports();
taskCompletionSource.setResult(null);
} catch (Exception e) {
taskCompletionSource.setException(e);
}
});

return taskCompletionSource.getTask();
}

private Task<Map<String, Object>> didCrashOnPreviousExecution() {
return Tasks.call(
cachedThreadPool,
() -> {
final boolean didCrashOnPreviousExecution =
FirebaseCrashlytics.getInstance().didCrashOnPreviousExecution();
TaskCompletionSource<Map<String, Object>> taskCompletionSource = new TaskCompletionSource<>();

return new HashMap<String, Object>() {
{
put(Constants.DID_CRASH_ON_PREVIOUS_EXECUTION, didCrashOnPreviousExecution);
}
};
cachedThreadPool.execute(
() -> {
try {
final boolean didCrashOnPreviousExecution =
FirebaseCrashlytics.getInstance().didCrashOnPreviousExecution();

taskCompletionSource.setResult(
new HashMap<String, Object>() {
{
put(Constants.DID_CRASH_ON_PREVIOUS_EXECUTION, didCrashOnPreviousExecution);
}
});
} catch (Exception e) {
taskCompletionSource.setException(e);
}
});

return taskCompletionSource.getTask();
}

private Task<Void> recordError(final Map<String, Object> arguments) {
return Tasks.call(
cachedThreadPool,
TaskCompletionSource<Void> taskCompletionSource = new TaskCompletionSource<>();

cachedThreadPool.execute(
() -> {
FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();

final String dartExceptionMessage =
(String) Objects.requireNonNull(arguments.get(Constants.EXCEPTION));
final String reason = (String) arguments.get(Constants.REASON);
final String information =
(String) Objects.requireNonNull(arguments.get(Constants.INFORMATION));
final boolean fatal = (boolean) Objects.requireNonNull(arguments.get(Constants.FATAL));

Exception exception;
if (reason != null) {
// Set a "reason" (to match iOS) to show where the exception was thrown.
crashlytics.setCustomKey(Constants.FLUTTER_ERROR_REASON, "thrown " + reason);
exception =
new FlutterError(dartExceptionMessage + ". " + "Error thrown " + reason + ".");
} else {
exception = new FlutterError(dartExceptionMessage);
}
try {
FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();

final String dartExceptionMessage =
(String) Objects.requireNonNull(arguments.get(Constants.EXCEPTION));
final String reason = (String) arguments.get(Constants.REASON);
final String information =
(String) Objects.requireNonNull(arguments.get(Constants.INFORMATION));
final boolean fatal = (boolean) Objects.requireNonNull(arguments.get(Constants.FATAL));

Exception exception;
if (reason != null) {
// Set a "reason" (to match iOS) to show where the exception was thrown.
crashlytics.setCustomKey(Constants.FLUTTER_ERROR_REASON, "thrown " + reason);
exception =
new FlutterError(dartExceptionMessage + ". " + "Error thrown " + reason + ".");
} else {
exception = new FlutterError(dartExceptionMessage);
}

crashlytics.setCustomKey(Constants.FLUTTER_ERROR_EXCEPTION, dartExceptionMessage);
crashlytics.setCustomKey(Constants.FLUTTER_ERROR_EXCEPTION, dartExceptionMessage);

final List<StackTraceElement> elements = new ArrayList<>();
@SuppressWarnings("unchecked")
final List<Map<String, String>> errorElements =
(List<Map<String, String>>)
Objects.requireNonNull(arguments.get(Constants.STACK_TRACE_ELEMENTS));
final List<StackTraceElement> elements = new ArrayList<>();
@SuppressWarnings("unchecked")
final List<Map<String, String>> errorElements =
(List<Map<String, String>>)
Objects.requireNonNull(arguments.get(Constants.STACK_TRACE_ELEMENTS));

for (Map<String, String> errorElement : errorElements) {
final StackTraceElement stackTraceElement = generateStackTraceElement(errorElement);
if (stackTraceElement != null) {
elements.add(stackTraceElement);
for (Map<String, String> errorElement : errorElements) {
final StackTraceElement stackTraceElement = generateStackTraceElement(errorElement);
if (stackTraceElement != null) {
elements.add(stackTraceElement);
}
}
}
exception.setStackTrace(elements.toArray(new StackTraceElement[0]));
exception.setStackTrace(elements.toArray(new StackTraceElement[0]));

// Log information.
if (!information.isEmpty()) {
crashlytics.log(information);
}
// Log information.
if (!information.isEmpty()) {
crashlytics.log(information);
}

if (fatal) {
FlutterFirebaseCrashlyticsInternal.recordFatalException(exception);
} else {
crashlytics.recordException(exception);
if (fatal) {
FlutterFirebaseCrashlyticsInternal.recordFatalException(exception);
} else {
crashlytics.recordException(exception);
}

taskCompletionSource.setResult(null);
} catch (Exception e) {
taskCompletionSource.setException(e);
}
return null;
});

return taskCompletionSource.getTask();
}

private Task<Void> log(final Map<String, Object> arguments) {
return Tasks.call(
cachedThreadPool,
TaskCompletionSource<Void> taskCompletionSource = new TaskCompletionSource<>();

cachedThreadPool.execute(
() -> {
String message = (String) Objects.requireNonNull(arguments.get(Constants.MESSAGE));
FirebaseCrashlytics.getInstance().log(message);
return null;
try {
String message = (String) Objects.requireNonNull(arguments.get(Constants.MESSAGE));
FirebaseCrashlytics.getInstance().log(message);
taskCompletionSource.setResult(null);
} catch (Exception e) {
taskCompletionSource.setException(e);
}
});

return taskCompletionSource.getTask();
}

private Task<Void> sendUnsentReports() {
return Tasks.call(
cachedThreadPool,
TaskCompletionSource<Void> taskCompletionSource = new TaskCompletionSource<>();

cachedThreadPool.execute(
() -> {
FirebaseCrashlytics.getInstance().sendUnsentReports();
return null;
try {
FirebaseCrashlytics.getInstance().sendUnsentReports();
taskCompletionSource.setResult(null);
} catch (Exception e) {
taskCompletionSource.setException(e);
}
});

return taskCompletionSource.getTask();
}

private Task<Map<String, Object>> setCrashlyticsCollectionEnabled(
final Map<String, Object> arguments) {
return Tasks.call(
cachedThreadPool,
TaskCompletionSource<Map<String, Object>> taskCompletionSource = new TaskCompletionSource<>();

cachedThreadPool.execute(
() -> {
Boolean enabled = (Boolean) Objects.requireNonNull(arguments.get(Constants.ENABLED));
FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(enabled);
return new HashMap<String, Object>() {
{
put(
Constants.IS_CRASHLYTICS_COLLECTION_ENABLED,
isCrashlyticsCollectionEnabled(FirebaseApp.getInstance()));
}
};
try {
Boolean enabled = (Boolean) Objects.requireNonNull(arguments.get(Constants.ENABLED));
FirebaseCrashlytics.getInstance().setCrashlyticsCollectionEnabled(enabled);

taskCompletionSource.setResult(
new HashMap<String, Object>() {
{
put(
Constants.IS_CRASHLYTICS_COLLECTION_ENABLED,
isCrashlyticsCollectionEnabled(FirebaseApp.getInstance()));
}
});
} catch (Exception e) {
taskCompletionSource.setException(e);
}
});

return taskCompletionSource.getTask();
}

private Task<Void> setUserIdentifier(final Map<String, Object> arguments) {
return Tasks.call(
cachedThreadPool,
TaskCompletionSource<Void> taskCompletionSource = new TaskCompletionSource<>();

cachedThreadPool.execute(
() -> {
String identifier = (String) Objects.requireNonNull(arguments.get(Constants.IDENTIFIER));
FirebaseCrashlytics.getInstance().setUserId(identifier);
return null;
try {
String identifier =
(String) Objects.requireNonNull(arguments.get(Constants.IDENTIFIER));
FirebaseCrashlytics.getInstance().setUserId(identifier);
taskCompletionSource.setResult(null);
} catch (Exception e) {
taskCompletionSource.setException(e);
}
});

return taskCompletionSource.getTask();
}

private Task<Void> setCustomKey(final Map<String, Object> arguments) {
return Tasks.call(
cachedThreadPool,
TaskCompletionSource<Void> taskCompletionSource = new TaskCompletionSource<>();

cachedThreadPool.execute(
() -> {
String key = (String) Objects.requireNonNull(arguments.get(Constants.KEY));
String value = (String) Objects.requireNonNull(arguments.get(Constants.VALUE));
FirebaseCrashlytics.getInstance().setCustomKey(key, value);
return null;
try {
String key = (String) Objects.requireNonNull(arguments.get(Constants.KEY));
String value = (String) Objects.requireNonNull(arguments.get(Constants.VALUE));
FirebaseCrashlytics.getInstance().setCustomKey(key, value);
taskCompletionSource.setResult(null);
} catch (Exception e) {
taskCompletionSource.setException(e);
}
});

return taskCompletionSource.getTask();
}

@Override
Expand Down Expand Up @@ -319,20 +389,41 @@ private boolean isCrashlyticsCollectionEnabled(FirebaseApp app) {

@Override
public Task<Map<String, Object>> getPluginConstantsForFirebaseApp(FirebaseApp firebaseApp) {
return Tasks.call(
() ->
new HashMap<String, Object>() {
{
if (firebaseApp.getName().equals("[DEFAULT]"))
put(
Constants.IS_CRASHLYTICS_COLLECTION_ENABLED,
isCrashlyticsCollectionEnabled(FirebaseApp.getInstance()));
}
});
TaskCompletionSource<Map<String, Object>> taskCompletionSource = new TaskCompletionSource<>();

cachedThreadPool.execute(
() -> {
try {
taskCompletionSource.setResult(
new HashMap<String, Object>() {
{
if (firebaseApp.getName().equals("[DEFAULT]"))
put(
Constants.IS_CRASHLYTICS_COLLECTION_ENABLED,
isCrashlyticsCollectionEnabled(FirebaseApp.getInstance()));
}
});
} catch (Exception e) {
taskCompletionSource.setException(e);
}
});

return taskCompletionSource.getTask();
}

@Override
public Task<Void> didReinitializeFirebaseCore() {
return Tasks.call(() -> null);
TaskCompletionSource<Void> taskCompletionSource = new TaskCompletionSource<>();

cachedThreadPool.execute(
() -> {
try {
taskCompletionSource.setResult(null);
} catch (Exception e) {
taskCompletionSource.setException(e);
}
});

return taskCompletionSource.getTask();
}
}

0 comments on commit 9539c92

Please sign in to comment.