|
| 1 | +package io.sentry.android.core; |
| 2 | + |
| 3 | +import android.app.AlertDialog; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.res.ColorStateList; |
| 6 | +import android.graphics.Color; |
| 7 | +import android.os.Bundle; |
| 8 | +import android.view.View; |
| 9 | +import android.widget.Button; |
| 10 | +import android.widget.EditText; |
| 11 | +import android.widget.ImageView; |
| 12 | +import android.widget.TextView; |
| 13 | +import android.widget.Toast; |
| 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; |
| 19 | +import org.jetbrains.annotations.NotNull; |
| 20 | +import org.jetbrains.annotations.Nullable; |
| 21 | + |
| 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); |
| 75 | + } |
| 76 | + |
| 77 | + if (!feedbackOptions.isShowName() && !feedbackOptions.isNameRequired()) { |
| 78 | + lblName.setVisibility(View.GONE); |
| 79 | + edtName.setVisibility(View.GONE); |
| 80 | + } else { |
| 81 | + lblName.setVisibility(View.VISIBLE); |
| 82 | + edtName.setVisibility(View.VISIBLE); |
| 83 | + lblName.setText(feedbackOptions.getNameLabel()); |
| 84 | + edtName.setHint(feedbackOptions.getNamePlaceholder()); |
| 85 | + if (feedbackOptions.isNameRequired()) { |
| 86 | + lblName.append(feedbackOptions.getIsRequiredLabel()); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if (!feedbackOptions.isShowEmail() && !feedbackOptions.isEmailRequired()) { |
| 91 | + lblEmail.setVisibility(View.GONE); |
| 92 | + edtEmail.setVisibility(View.GONE); |
| 93 | + } else { |
| 94 | + lblEmail.setVisibility(View.VISIBLE); |
| 95 | + edtEmail.setVisibility(View.VISIBLE); |
| 96 | + lblEmail.setText(feedbackOptions.getEmailLabel()); |
| 97 | + edtEmail.setHint(feedbackOptions.getEmailPlaceholder()); |
| 98 | + if (feedbackOptions.isEmailRequired()) { |
| 99 | + lblEmail.append(feedbackOptions.getIsRequiredLabel()); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if (feedbackOptions.isUseSentryUser()) { |
| 104 | + final @Nullable User user = Sentry.getCurrentScopes().getScope().getUser(); |
| 105 | + if (user != null) { |
| 106 | + edtName.setText(user.getName()); |
| 107 | + edtEmail.setText(user.getEmail()); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + lblMessage.setText(feedbackOptions.getMessageLabel()); |
| 112 | + edtMessage.setHint(feedbackOptions.getMessagePlaceholder()); |
| 113 | + lblTitle.setText(feedbackOptions.getFormTitle()); |
| 114 | + |
| 115 | + btnSend.setBackgroundTintList( |
| 116 | + ColorStateList.valueOf(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); |
| 134 | + } |
| 135 | + } else { |
| 136 | + final @Nullable SentryFeedbackOptions.SentryFeedbackCallback onSubmitError = |
| 137 | + feedbackOptions.getOnSubmitError(); |
| 138 | + if (onSubmitError != null) { |
| 139 | + onSubmitError.call(feedback); |
| 140 | + } |
| 141 | + } |
| 142 | + cancel(); |
| 143 | + }); |
| 144 | + |
| 145 | + btnCancel.setText(feedbackOptions.getCancelButtonLabel()); |
| 146 | + btnCancel.setOnClickListener(v -> cancel()); |
| 147 | + |
| 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(); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments