add crash handling dialog with optional sending of the crash to the author
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / CrashReportDialogFragment.java
1 package net.ktnx.mobileledger.ui;
2
3 import android.app.AlertDialog;
4 import android.app.Dialog;
5 import android.content.DialogInterface;
6 import android.content.Intent;
7 import android.os.Bundle;
8 import android.support.annotation.NonNull;
9 import android.support.annotation.Nullable;
10 import android.support.v4.app.DialogFragment;
11 import android.view.LayoutInflater;
12 import android.view.View;
13 import android.widget.ScrollView;
14 import android.widget.TextView;
15
16 import net.ktnx.mobileledger.R;
17 import net.ktnx.mobileledger.utils.Globals;
18
19 public class CrashReportDialogFragment extends DialogFragment {
20     private String mCrashReportText;
21     private ScrollView repScroll = null;
22     @NonNull
23     @Override
24     public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
25         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
26         LayoutInflater inflater = getActivity().getLayoutInflater();
27
28         if (savedInstanceState != null)
29             mCrashReportText = savedInstanceState.getString("crash_text");
30
31         View view = inflater.inflate(R.layout.crash_dialog, null);
32         ((TextView) view.findViewById(R.id.textCrashReport)).setText(mCrashReportText);
33         repScroll = view.findViewById(R.id.scrollText);
34         builder.setTitle(R.string.crash_dialog_title).setView(view)
35                 .setPositiveButton(R.string.btn_send_crash_report,
36                         new DialogInterface.OnClickListener() {
37                             @Override
38                             public void onClick(DialogInterface dialog, int which) {
39                                 // still nothing
40                                 Intent email = new Intent(Intent.ACTION_SEND);
41                                 email.putExtra(Intent.EXTRA_EMAIL,
42                                         new String[]{Globals.developerEmail});
43                                 email.putExtra(Intent.EXTRA_SUBJECT, "MoLe crash report");
44                                 email.putExtra(Intent.EXTRA_TEXT, mCrashReportText);
45                                 email.setType("message/rfc822");
46                                 startActivity(Intent.createChooser(email,
47                                         getResources().getString(R.string.send_crash_via)));
48                             }
49                         })
50                 .setNegativeButton(R.string.btn_not_now, new DialogInterface.OnClickListener() {
51                     @Override
52                     public void onClick(DialogInterface dialog, int which) {
53                         CrashReportDialogFragment.this.getDialog().cancel();
54                     }
55                 })
56                 .setNeutralButton(R.string.btn_show_report, new DialogInterface.OnClickListener() {
57                     @Override
58                     public void onClick(DialogInterface dialog, int which) {
59                     }
60                 });
61
62         AlertDialog dialog = builder.create();
63         dialog.setOnShowListener(new DialogInterface.OnShowListener() {
64             @Override
65             public void onShow(DialogInterface dialogIinterface) {
66                 dialog.getButton(AlertDialog.BUTTON_NEUTRAL)
67                         .setOnClickListener(new View.OnClickListener() {
68                             @Override
69                             public void onClick(View v) {
70                                 if (repScroll != null) {
71                                     repScroll.setVisibility(View.VISIBLE);
72                                     v.setVisibility(View.GONE);
73                                 }
74                             }
75                         });
76             }
77         });
78         return dialog;
79     }
80     @Override
81     public void onSaveInstanceState(@NonNull Bundle outState) {
82         super.onSaveInstanceState(outState);
83         outState.putString("crash_text", mCrashReportText);
84     }
85     public void setCrashReportText(String text) {
86         mCrashReportText = text;
87     }
88 }