From dabc7c75436eb87dcb8cbee24aee82af986a528f Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Mon, 29 May 2023 21:17:26 +0600 Subject: [PATCH 1/2] 1304-Handle_stateLoss_on_exceptionDialog_show issue: https://gitlab.e.foundation/e/os/backlog/-/issues/1304 DialogFragment failed to show on the frament state loss. As a result, the full application crash. We don't want to crash the app for this when we try to show the crash alert dialog. --- .../exception/ExceptionDialogFragment.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/exception/ExceptionDialogFragment.java b/app/src/main/java/it/niedermann/owncloud/notes/exception/ExceptionDialogFragment.java index a0c1a0f06..aec01a10f 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/exception/ExceptionDialogFragment.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/exception/ExceptionDialogFragment.java @@ -6,12 +6,16 @@ import android.os.Bundle; import android.view.View; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatDialogFragment; import androidx.fragment.app.DialogFragment; +import androidx.fragment.app.FragmentManager; import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import java.io.PrintWriter; +import java.io.StringWriter; import java.util.ArrayList; import it.niedermann.android.util.ClipboardUtil; @@ -20,6 +24,7 @@ import it.niedermann.owncloud.notes.BuildConfig; import it.niedermann.owncloud.notes.R; import it.niedermann.owncloud.notes.databinding.DialogExceptionBinding; import it.niedermann.owncloud.notes.exception.tips.TipsAdapter; +import trikita.log.Log; public class ExceptionDialogFragment extends AppCompatDialogFragment { @@ -32,6 +37,10 @@ public class ExceptionDialogFragment extends AppCompatDialogFragment { @Override public void onAttach(@NonNull Context context) { super.onAttach(context); + setupThrowable(); + } + + private void setupThrowable() { final var args = getArguments(); if (args != null) { final var throwablesArgument = args.getSerializable(KEY_THROWABLES); @@ -89,4 +98,28 @@ public class ExceptionDialogFragment extends AppCompatDialogFragment { fragment.setArguments(args); return fragment; } + + @Override + public void show(@NonNull FragmentManager manager, @Nullable String tag) { + if (manager.isDestroyed() || !manager.isStateSaved()) { + Log.e("ExceptionDialog can't be shown as the state is lost. The cause:\n\n" + getStackStrace()); + return; + } + + super.show(manager, tag); + } + + @NonNull + private String getStackStrace() { + setupThrowable(); + StringBuilder builder = new StringBuilder(); + + for (Throwable throwable : throwables) { + StringWriter sw = new StringWriter(); + throwable.printStackTrace(new PrintWriter(sw)); + builder.append("\n\n").append(sw); + } + + return builder.toString(); + } } -- GitLab From 49ed41cfe3f7bfbbf88cc97865b7db7c2c46e375 Mon Sep 17 00:00:00 2001 From: Vincent Bourgmayer Date: Tue, 30 May 2023 06:55:08 +0000 Subject: [PATCH 2/2] Apply 2 suggestion(s) to 1 file(s) --- .../owncloud/notes/exception/ExceptionDialogFragment.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/exception/ExceptionDialogFragment.java b/app/src/main/java/it/niedermann/owncloud/notes/exception/ExceptionDialogFragment.java index aec01a10f..b460e0cab 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/exception/ExceptionDialogFragment.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/exception/ExceptionDialogFragment.java @@ -112,10 +112,10 @@ public class ExceptionDialogFragment extends AppCompatDialogFragment { @NonNull private String getStackStrace() { setupThrowable(); - StringBuilder builder = new StringBuilder(); + final StringBuilder builder = new StringBuilder(); for (Throwable throwable : throwables) { - StringWriter sw = new StringWriter(); + final StringWriter sw = new StringWriter(); throwable.printStackTrace(new PrintWriter(sw)); builder.append("\n\n").append(sw); } -- GitLab