|
2 | 2 |
|
3 | 3 | import android.app.AlertDialog;
|
4 | 4 | import android.content.Context;
|
| 5 | +import android.content.res.ColorStateList; |
| 6 | +import android.graphics.Color; |
5 | 7 | import android.os.Bundle;
|
6 | 8 | import android.view.View;
|
7 | 9 | import android.widget.Button;
|
8 | 10 | import android.widget.EditText;
|
9 | 11 | import android.widget.ImageView;
|
10 | 12 | import android.widget.TextView;
|
11 | 13 | import android.widget.Toast;
|
12 |
| - |
| 14 | +import io.sentry.Sentry; |
| 15 | +import io.sentry.SentryFeedbackOptions; |
| 16 | +import io.sentry.protocol.Feedback; |
| 17 | +import io.sentry.protocol.SentryId; |
| 18 | +import io.sentry.protocol.User; |
13 | 19 | import org.jetbrains.annotations.NotNull;
|
14 | 20 | import org.jetbrains.annotations.Nullable;
|
15 | 21 |
|
16 |
| -public class SentryUserFeedbackDialog extends AlertDialog { |
17 |
| - |
18 |
| - private boolean isCancelable = false; |
19 |
| - |
20 |
| - protected SentryUserFeedbackDialog(@NotNull final Context context) { |
21 |
| - super(context); |
22 |
| - isCancelable = false; |
| 22 | +public final class SentryUserFeedbackDialog extends AlertDialog { |
| 23 | + |
| 24 | + private boolean isCancelable = false; |
| 25 | + |
| 26 | + public SentryUserFeedbackDialog(final @NotNull Context context) { |
| 27 | + super(context); |
| 28 | + isCancelable = false; |
| 29 | + } |
| 30 | + |
| 31 | + public SentryUserFeedbackDialog( |
| 32 | + final @NotNull Context context, |
| 33 | + final boolean cancelable, |
| 34 | + @Nullable final OnCancelListener cancelListener) { |
| 35 | + super(context, cancelable, cancelListener); |
| 36 | + isCancelable = cancelable; |
| 37 | + } |
| 38 | + |
| 39 | + public SentryUserFeedbackDialog(final @NotNull Context context, final int themeResId) { |
| 40 | + super(context, themeResId); |
| 41 | + isCancelable = false; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public void setCancelable(boolean cancelable) { |
| 46 | + super.setCancelable(cancelable); |
| 47 | + isCancelable = cancelable; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + protected void onCreate(Bundle savedInstanceState) { |
| 52 | + super.onCreate(savedInstanceState); |
| 53 | + setContentView(R.layout.sentry_dialog_user_feedback); |
| 54 | + setCancelable(isCancelable); |
| 55 | + |
| 56 | + final @NotNull SentryFeedbackOptions feedbackOptions = |
| 57 | + Sentry.getCurrentScopes().getOptions().getFeedbackOptions(); |
| 58 | + final @NotNull TextView lblTitle = findViewById(R.id.sentry_dialog_user_feedback_title); |
| 59 | + final @NotNull ImageView imgLogo = findViewById(R.id.sentry_dialog_user_feedback_logo); |
| 60 | + final @NotNull TextView lblName = findViewById(R.id.sentry_dialog_user_feedback_txt_name); |
| 61 | + final @NotNull EditText edtName = findViewById(R.id.sentry_dialog_user_feedback_edt_name); |
| 62 | + final @NotNull TextView lblEmail = findViewById(R.id.sentry_dialog_user_feedback_txt_email); |
| 63 | + final @NotNull EditText edtEmail = findViewById(R.id.sentry_dialog_user_feedback_edt_email); |
| 64 | + final @NotNull TextView lblMessage = |
| 65 | + findViewById(R.id.sentry_dialog_user_feedback_txt_description); |
| 66 | + final @NotNull EditText edtMessage = |
| 67 | + findViewById(R.id.sentry_dialog_user_feedback_edt_description); |
| 68 | + final @NotNull Button btnSend = findViewById(R.id.sentry_dialog_user_feedback_btn_send); |
| 69 | + final @NotNull Button btnCancel = findViewById(R.id.sentry_dialog_user_feedback_btn_cancel); |
| 70 | + |
| 71 | + if (feedbackOptions.isShowBranding()) { |
| 72 | + imgLogo.setVisibility(View.VISIBLE); |
| 73 | + } else { |
| 74 | + imgLogo.setVisibility(View.GONE); |
23 | 75 | }
|
24 |
| - |
25 |
| - protected SentryUserFeedbackDialog(@NotNull final Context context, final boolean cancelable, @Nullable final OnCancelListener cancelListener) { |
26 |
| - super(context, cancelable, cancelListener); |
27 |
| - isCancelable = cancelable; |
| 76 | + imgLogo.setImageTintList(ColorStateList.valueOf(lblTitle.getTextColors().getDefaultColor())); |
| 77 | + |
| 78 | + if (!feedbackOptions.isShowName() && !feedbackOptions.isNameRequired()) { |
| 79 | + lblName.setVisibility(View.GONE); |
| 80 | + edtName.setVisibility(View.GONE); |
| 81 | + } else { |
| 82 | + lblName.setVisibility(View.VISIBLE); |
| 83 | + edtName.setVisibility(View.VISIBLE); |
| 84 | + lblName.setText(feedbackOptions.getNameLabel()); |
| 85 | + edtName.setHint(feedbackOptions.getNamePlaceholder()); |
| 86 | + if (feedbackOptions.isNameRequired()) { |
| 87 | + lblName.append(feedbackOptions.getIsRequiredLabel()); |
| 88 | + } |
28 | 89 | }
|
29 | 90 |
|
30 |
| - protected SentryUserFeedbackDialog(@NotNull final Context context, final int themeResId) { |
31 |
| - super(context, themeResId); |
32 |
| - isCancelable = false; |
| 91 | + if (!feedbackOptions.isShowEmail() && !feedbackOptions.isEmailRequired()) { |
| 92 | + lblEmail.setVisibility(View.GONE); |
| 93 | + edtEmail.setVisibility(View.GONE); |
| 94 | + } else { |
| 95 | + lblEmail.setVisibility(View.VISIBLE); |
| 96 | + edtEmail.setVisibility(View.VISIBLE); |
| 97 | + lblEmail.setText(feedbackOptions.getEmailLabel()); |
| 98 | + edtEmail.setHint(feedbackOptions.getEmailPlaceholder()); |
| 99 | + if (feedbackOptions.isEmailRequired()) { |
| 100 | + lblEmail.append(feedbackOptions.getIsRequiredLabel()); |
| 101 | + } |
33 | 102 | }
|
34 | 103 |
|
35 |
| - @Override |
36 |
| - public void setCancelable(boolean cancelable) { |
37 |
| - super.setCancelable(cancelable); |
38 |
| - isCancelable = cancelable; |
| 104 | + if (feedbackOptions.isUseSentryUser()) { |
| 105 | + final @Nullable User user = Sentry.getCurrentScopes().getScope().getUser(); |
| 106 | + if (user != null) { |
| 107 | + edtName.setText(user.getName()); |
| 108 | + edtEmail.setText(user.getEmail()); |
| 109 | + } |
39 | 110 | }
|
40 | 111 |
|
41 |
| - @Override |
42 |
| - protected void onCreate(Bundle savedInstanceState) { |
43 |
| - super.onCreate(savedInstanceState); |
44 |
| - setContentView(R.layout.sentry_dialog_user_feedback); |
45 |
| - setCancelable(isCancelable); |
46 |
| - |
47 |
| - @NotNull final TextView lblTitle = findViewById(R.id.sentry_dialog_user_feedback_title); |
48 |
| - @NotNull final ImageView imgLogo = findViewById(R.id.sentry_dialog_user_feedback_logo); |
49 |
| - @NotNull final TextView lblName = findViewById(R.id.sentry_dialog_user_feedback_txt_name); |
50 |
| - @NotNull final EditText edtName = findViewById(R.id.sentry_dialog_user_feedback_edt_name); |
51 |
| - @NotNull final TextView lblEmail = findViewById(R.id.sentry_dialog_user_feedback_txt_email); |
52 |
| - @NotNull final EditText edtEmail = findViewById(R.id.sentry_dialog_user_feedback_edt_email); |
53 |
| - @NotNull final TextView lblMessage = findViewById(R.id.sentry_dialog_user_feedback_txt_description); |
54 |
| - @NotNull final EditText edtMessage = findViewById(R.id.sentry_dialog_user_feedback_edt_description); |
55 |
| - @NotNull final Button btnSend = findViewById(R.id.sentry_dialog_user_feedback_btn_send); |
56 |
| - @NotNull final Button btnCancel = findViewById(R.id.sentry_dialog_user_feedback_btn_cancel); |
57 |
| - |
58 |
| - if (showBranding) { |
59 |
| - imgLogo.setVisibility(View.VISIBLE); |
60 |
| - } else { |
61 |
| - imgLogo.setVisibility(View.GONE); |
62 |
| - } |
63 |
| - |
64 |
| - if (!showName && !isNameRequired) { |
65 |
| - lblName.setVisibility(View.GONE); |
66 |
| - edtName.setVisibility(View.GONE); |
67 |
| - } else { |
68 |
| - lblName.setVisibility(View.VISIBLE); |
69 |
| - edtName.setVisibility(View.VISIBLE); |
70 |
| - lblName.setText(nameLabel); |
71 |
| - edtName.setHint(namePlaceholder); |
72 |
| - if (isNameRequired) { |
73 |
| - lblName.append(isRequiredLabel); |
| 112 | + lblMessage.setText(feedbackOptions.getMessageLabel()); |
| 113 | + edtMessage.setHint(feedbackOptions.getMessagePlaceholder()); |
| 114 | + lblTitle.setText(feedbackOptions.getFormTitle()); |
| 115 | + |
| 116 | + btnSend.setBackgroundColor(Color.parseColor(feedbackOptions.getSubmitBackgroundHex())); |
| 117 | + btnSend.setTextColor(Color.parseColor(feedbackOptions.getSubmitForegroundHex())); |
| 118 | + btnSend.setText(feedbackOptions.getSubmitButtonLabel()); |
| 119 | + btnSend.setOnClickListener( |
| 120 | + v -> { |
| 121 | + final @NotNull Feedback feedback = new Feedback(edtMessage.getText().toString()); |
| 122 | + feedback.setName(edtName.getText().toString()); |
| 123 | + feedback.setContactEmail(edtEmail.getText().toString()); |
| 124 | + |
| 125 | + SentryId id = Sentry.captureFeedback(feedback); |
| 126 | + if (!id.equals(SentryId.EMPTY_ID)) { |
| 127 | + Toast.makeText( |
| 128 | + getContext(), feedbackOptions.getSuccessMessageText(), Toast.LENGTH_SHORT) |
| 129 | + .show(); |
| 130 | + final @Nullable SentryFeedbackOptions.SentryFeedbackCallback onSubmitSuccess = |
| 131 | + feedbackOptions.getOnSubmitSuccess(); |
| 132 | + if (onSubmitSuccess != null) { |
| 133 | + onSubmitSuccess.call(feedback); |
74 | 134 | }
|
75 |
| - } |
76 |
| - |
77 |
| - if (!showEmail && !isEmailRequired) { |
78 |
| - lblEmail.setVisibility(View.GONE); |
79 |
| - edtEmail.setVisibility(View.GONE); |
80 |
| - } else { |
81 |
| - lblEmail.setVisibility(View.VISIBLE); |
82 |
| - edtEmail.setVisibility(View.VISIBLE); |
83 |
| - lblEmail.setText(emailLabel); |
84 |
| - edtEmail.setHint(emailPlaceholder); |
85 |
| - if (isEmailRequired) { |
86 |
| - lblEmail.append(isRequiredLabel); |
| 135 | + } else { |
| 136 | + final @Nullable SentryFeedbackOptions.SentryFeedbackCallback onSubmitError = |
| 137 | + feedbackOptions.getOnSubmitError(); |
| 138 | + if (onSubmitError != null) { |
| 139 | + onSubmitError.call(feedback); |
87 | 140 | }
|
88 |
| - } |
89 |
| - |
90 |
| - if (useSentryUser) { |
91 |
| - edtName.setText(); |
92 |
| - edtEmail.setText(); |
93 |
| - } |
94 |
| - |
95 |
| - lblMessage.setText(messageLabel); |
96 |
| - edtMessage.setHint(messagePlaceholder); |
97 |
| - lblTitle.setText(formTitle); |
98 |
| - |
99 |
| - btnSend.setText(submitButtonLabel); |
100 |
| - btnSend.setOnClickListener(v -> { |
101 |
| - Toast.makeText(getContext(), successMessageText, Toast.LENGTH_SHORT).show(); |
102 |
| - cancel(); |
| 141 | + } |
| 142 | + cancel(); |
103 | 143 | });
|
104 | 144 |
|
105 |
| - btnCancel.setText(cancelButtonLabel); |
106 |
| - btnCancel.setOnClickListener(v -> cancel()); |
107 |
| - } |
| 145 | + btnCancel.setText(feedbackOptions.getCancelButtonLabel()); |
| 146 | + btnCancel.setOnClickListener(v -> cancel()); |
108 | 147 |
|
109 |
| - @Override |
110 |
| - protected void onStart() { |
111 |
| - super.onStart(); |
| 148 | + final @Nullable Runnable onFormClose = feedbackOptions.getOnFormClose(); |
| 149 | + if (onFormClose != null) { |
| 150 | + setOnDismissListener(dialog -> onFormClose.run()); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + protected void onStart() { |
| 156 | + super.onStart(); |
| 157 | + final @NotNull SentryFeedbackOptions feedbackOptions = |
| 158 | + Sentry.getCurrentScopes().getOptions().getFeedbackOptions(); |
| 159 | + final @Nullable Runnable onFormOpen = feedbackOptions.getOnFormOpen(); |
| 160 | + if (onFormOpen != null) { |
| 161 | + onFormOpen.run(); |
112 | 162 | }
|
| 163 | + } |
113 | 164 | }
|
0 commit comments