summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/blockreportspam/BlockReportSpamDialogs.java
blob: b75669fa390cfdd855a4b0eceac833c66880e2a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dialer.blockreportspam;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import com.android.dialer.blocking.FilteredNumberCompat;

/**
 * Helper class for creating dialog fragments to block a number and/or report it as spam/not spam.
 */
public final class BlockReportSpamDialogs {

  public static final String BLOCK_REPORT_SPAM_DIALOG_TAG = "BlockReportSpamDialog";
  public static final String BLOCK_DIALOG_TAG = "BlockDialog";
  public static final String UNBLOCK_DIALOG_TAG = "UnblockDialog";
  public static final String NOT_SPAM_DIALOG_TAG = "NotSpamDialog";

  /** Creates a dialog with the default cancel button listener (dismisses dialog). */
  private static AlertDialog.Builder createDialogBuilder(
      Activity activity, final DialogFragment fragment) {
    return new AlertDialog.Builder(activity, R.style.AlertDialogTheme)
        .setCancelable(true)
        .setNegativeButton(android.R.string.cancel, (dialog, which) -> fragment.dismiss());
  }

  /**
   * Creates a generic click listener which dismisses the fragment and then calls the actual
   * listener.
   */
  private static DialogInterface.OnClickListener createGenericOnClickListener(
      final DialogFragment fragment, final OnConfirmListener listener) {
    return (dialog, which) -> {
      fragment.dismiss();
      listener.onClick();
    };
  }

  private static String getBlockMessage(Context context) {
    String message;
    if (FilteredNumberCompat.useNewFiltering(context)) {
      message = context.getString(R.string.block_number_confirmation_message_new_filtering);
    } else {
      message = context.getString(R.string.block_report_number_alert_details);
    }
    return message;
  }

  /**
   * Listener passed to block/report spam dialog for positive click in {@link
   * BlockReportSpamDialogFragment}.
   */
  public interface OnSpamDialogClickListener {

    /**
     * Called when user clicks on positive button in block/report spam dialog.
     *
     * @param isSpamChecked Whether the spam checkbox is checked.
     */
    void onClick(boolean isSpamChecked);
  }

  /** Listener passed to all dialogs except the block/report spam dialog for positive click. */
  public interface OnConfirmListener {

    /** Called when user clicks on positive button in the dialog. */
    void onClick();
  }

  /** Contains the common attributes between all block/unblock/report spam dialog fragments. */
  private abstract static class CommonDialogsFragment extends DialogFragment {

    /** The number to display in the dialog title. */
    protected String displayNumber;

    /** Called when dialog positive button is pressed. */
    protected OnConfirmListener positiveListener;

    /** Called when dialog is dismissed. */
    @Nullable protected DialogInterface.OnDismissListener dismissListener;

    @Override
    public void onDismiss(DialogInterface dialog) {
      if (dismissListener != null) {
        dismissListener.onDismiss(dialog);
      }
      super.onDismiss(dialog);
    }

    @Override
    public void onPause() {
      // The dialog is dismissed onPause, i.e. rotation.
      dismiss();
      dismissListener = null;
      positiveListener = null;
      displayNumber = null;
      super.onPause();
    }
  }

  /** Dialog for block/report spam with the mark as spam checkbox. */
  public static class BlockReportSpamDialogFragment extends CommonDialogsFragment {

    /** Called when dialog positive button is pressed. */
    private OnSpamDialogClickListener onSpamDialogClickListener;

    /** Whether the mark as spam checkbox is checked before displaying the dialog. */
    private boolean spamChecked;

    public static DialogFragment newInstance(
        String displayNumber,
        boolean spamChecked,
        OnSpamDialogClickListener onSpamDialogClickListener,
        @Nullable DialogInterface.OnDismissListener dismissListener) {
      BlockReportSpamDialogFragment fragment = new BlockReportSpamDialogFragment();
      fragment.spamChecked = spamChecked;
      fragment.displayNumber = displayNumber;
      fragment.onSpamDialogClickListener = onSpamDialogClickListener;
      fragment.dismissListener = dismissListener;
      return fragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
      super.onCreateDialog(savedInstanceState);
      View dialogView = View.inflate(getActivity(), R.layout.block_report_spam_dialog, null);
      final CheckBox isSpamCheckbox =
          (CheckBox) dialogView.findViewById(R.id.report_number_as_spam_action);
      // Listen for changes on the checkbox and update if orientation changes
      isSpamCheckbox.setChecked(spamChecked);
      isSpamCheckbox.setOnCheckedChangeListener((buttonView, isChecked) -> spamChecked = isChecked);

      TextView details = (TextView) dialogView.findViewById(R.id.block_details);
      details.setText(getBlockMessage(getContext()));

      AlertDialog.Builder alertDialogBuilder = createDialogBuilder(getActivity(), this);
      Dialog blockReportSpamDialog =
          alertDialogBuilder
              .setView(dialogView)
              .setTitle(getString(R.string.block_report_number_alert_title, displayNumber))
              .setPositiveButton(
                  R.string.block_number_ok,
                  (dialog, which) -> {
                    dismiss();
                    onSpamDialogClickListener.onClick(isSpamCheckbox.isChecked());
                  })
              .create();
      blockReportSpamDialog.setCanceledOnTouchOutside(true);
      return blockReportSpamDialog;
    }
  }

  /** Dialog for blocking a number. */
  public static class BlockDialogFragment extends CommonDialogsFragment {

    private boolean isSpamEnabled;

    public static DialogFragment newInstance(
        String displayNumber,
        boolean isSpamEnabled,
        OnConfirmListener positiveListener,
        @Nullable DialogInterface.OnDismissListener dismissListener) {
      BlockDialogFragment fragment = new BlockDialogFragment();
      fragment.displayNumber = displayNumber;
      fragment.positiveListener = positiveListener;
      fragment.dismissListener = dismissListener;
      fragment.isSpamEnabled = isSpamEnabled;
      return fragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
      super.onCreateDialog(savedInstanceState);
      // Return the newly created dialog
      AlertDialog.Builder alertDialogBuilder = createDialogBuilder(getActivity(), this);
      Dialog dialog =
          alertDialogBuilder
              .setTitle(getString(R.string.block_number_confirmation_title, displayNumber))
              .setMessage(
                  isSpamEnabled
                      ? getString(
                          R.string.block_number_alert_details, getBlockMessage(getContext()))
                      : getString(R.string.block_report_number_alert_details))
              .setPositiveButton(
                  R.string.block_number_ok, createGenericOnClickListener(this, positiveListener))
              .create();
      dialog.setCanceledOnTouchOutside(true);
      return dialog;
    }
  }

  /** Dialog for unblocking a number. */
  public static class UnblockDialogFragment extends CommonDialogsFragment {

    /** Whether or not the number is spam. */
    private boolean isSpam;

    public static DialogFragment newInstance(
        String displayNumber,
        boolean isSpam,
        OnConfirmListener positiveListener,
        @Nullable DialogInterface.OnDismissListener dismissListener) {
      UnblockDialogFragment fragment = new UnblockDialogFragment();
      fragment.displayNumber = displayNumber;
      fragment.isSpam = isSpam;
      fragment.positiveListener = positiveListener;
      fragment.dismissListener = dismissListener;
      return fragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
      super.onCreateDialog(savedInstanceState);
      // Return the newly created dialog
      AlertDialog.Builder alertDialogBuilder = createDialogBuilder(getActivity(), this);
      if (isSpam) {
        alertDialogBuilder
            .setMessage(R.string.unblock_number_alert_details)
            .setTitle(getString(R.string.unblock_report_number_alert_title, displayNumber));
      } else {
        alertDialogBuilder.setMessage(
            getString(R.string.unblock_report_number_alert_title, displayNumber));
      }
      Dialog dialog =
          alertDialogBuilder
              .setPositiveButton(
                  R.string.unblock_number_ok, createGenericOnClickListener(this, positiveListener))
              .create();
      dialog.setCanceledOnTouchOutside(true);
      return dialog;
    }
  }

  /** Dialog for reporting a number as not spam. */
  public static class ReportNotSpamDialogFragment extends CommonDialogsFragment {

    public static DialogFragment newInstance(
        String displayNumber,
        OnConfirmListener positiveListener,
        @Nullable DialogInterface.OnDismissListener dismissListener) {
      ReportNotSpamDialogFragment fragment = new ReportNotSpamDialogFragment();
      fragment.displayNumber = displayNumber;
      fragment.positiveListener = positiveListener;
      fragment.dismissListener = dismissListener;
      return fragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
      super.onCreateDialog(savedInstanceState);
      // Return the newly created dialog
      AlertDialog.Builder alertDialogBuilder = createDialogBuilder(getActivity(), this);
      Dialog dialog =
          alertDialogBuilder
              .setTitle(R.string.report_not_spam_alert_title)
              .setMessage(getString(R.string.report_not_spam_alert_details, displayNumber))
              .setPositiveButton(
                  R.string.report_not_spam_alert_button,
                  createGenericOnClickListener(this, positiveListener))
              .create();
      dialog.setCanceledOnTouchOutside(true);
      return dialog;
    }
  }
}