Use dialog fragment for unexpected errors

This commit is contained in:
M66B
2019-07-01 14:06:15 +02:00
parent b855358169
commit 9f5a829206
32 changed files with 178 additions and 158 deletions

View File

@@ -19,6 +19,7 @@ package eu.faircode.email;
Copyright 2018-2019 by Marcel Bokhorst (M66B)
*/
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
@@ -51,11 +52,14 @@ import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import androidx.exifinterface.media.ExifInterface;
import androidx.lifecycle.Lifecycle;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.LifecycleOwner;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.PreferenceManager;
@@ -207,7 +211,7 @@ public class Helper {
Toast.makeText(context, context.getString(R.string.title_no_viewer, uri.toString()), Toast.LENGTH_LONG).show();
} catch (Throwable ex) {
Log.e(ex);
unexpectedError(context, owner, ex);
Toast.makeText(context, Helper.formatThrowable(ex, false), Toast.LENGTH_LONG).show();
}
}
}
@@ -434,15 +438,31 @@ public class Helper {
return sb.toString();
}
static void unexpectedError(final Context context, final LifecycleOwner owner, final Throwable ex) {
if (owner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED))
new DialogBuilderLifecycle(context, owner)
static void unexpectedError(FragmentManager manager, final Throwable ex) {
Bundle args = new Bundle();
args.putSerializable("ex", ex);
FragmentDialogUnexpected fragment = new FragmentDialogUnexpected();
fragment.setArguments(args);
fragment.show(manager, "error:unexpected");
}
public static class FragmentDialogUnexpected extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
final Throwable ex = (Throwable) getArguments().getSerializable("ex");
return new AlertDialog.Builder(getContext())
.setTitle(R.string.title_unexpected_error)
.setMessage(ex.toString())
.setMessage(Helper.formatThrowable(ex, false))
.setPositiveButton(android.R.string.cancel, null)
.setNeutralButton(R.string.title_report, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Dialog will be dismissed
final Context context = getContext();
new SimpleTask<Long>() {
@Override
protected Long onExecute(Context context, Bundle args) throws Throwable {
@@ -451,25 +471,24 @@ public class Helper {
@Override
protected void onExecuted(Bundle args, Long id) {
context.startActivity(
new Intent(context, ActivityCompose.class)
.putExtra("action", "edit")
.putExtra("id", id));
context.startActivity(new Intent(context, ActivityCompose.class)
.putExtra("action", "edit")
.putExtra("id", id));
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.e(ex);
if (ex instanceof IllegalArgumentException)
Toast.makeText(context, ex.getMessage(), Toast.LENGTH_LONG).show();
else
Toast.makeText(context, ex.toString(), Toast.LENGTH_LONG).show();
}
}.execute(context, owner, new Bundle(), "error:unexpected");
}.execute(context, getActivity(), new Bundle(), "error:unexpected");
}
})
.show();
else
ApplicationEx.writeCrashLog(context, ex);
.create();
}
}
// Files