summaryrefslogtreecommitdiffstats
path: root/java/com/android/dialer/blocking/BlockNumberDialogFragment.java
blob: 7ef77c4b68503b740a1872780a2ac36333be5c23 (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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * Copyright (C) 2015 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.blocking;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.telephony.PhoneNumberUtils;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;
import com.android.contacts.common.util.ContactDisplayUtils;
import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler.OnBlockNumberListener;
import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler.OnUnblockNumberListener;
import com.android.dialer.logging.InteractionEvent;
import com.android.dialer.logging.Logger;
import com.android.dialer.voicemailstatus.VisualVoicemailEnabledChecker;

/**
 * Fragment for confirming and enacting blocking/unblocking a number. Also invokes snackbar
 * providing undo functionality.
 */
public class BlockNumberDialogFragment extends DialogFragment {

  private static final String BLOCK_DIALOG_FRAGMENT = "BlockNumberDialog";
  private static final String ARG_BLOCK_ID = "argBlockId";
  private static final String ARG_NUMBER = "argNumber";
  private static final String ARG_COUNTRY_ISO = "argCountryIso";
  private static final String ARG_DISPLAY_NUMBER = "argDisplayNumber";
  private static final String ARG_PARENT_VIEW_ID = "parentViewId";
  private String mNumber;
  private String mDisplayNumber;
  private String mCountryIso;
  private FilteredNumberAsyncQueryHandler mHandler;
  private View mParentView;
  private VisualVoicemailEnabledChecker mVoicemailEnabledChecker;
  private Callback mCallback;

  public static BlockNumberDialogFragment show(
      Integer blockId,
      String number,
      String countryIso,
      String displayNumber,
      Integer parentViewId,
      FragmentManager fragmentManager,
      Callback callback) {
    final BlockNumberDialogFragment newFragment =
        BlockNumberDialogFragment.newInstance(
            blockId, number, countryIso, displayNumber, parentViewId);

    newFragment.setCallback(callback);
    newFragment.show(fragmentManager, BlockNumberDialogFragment.BLOCK_DIALOG_FRAGMENT);
    return newFragment;
  }

  private static BlockNumberDialogFragment newInstance(
      Integer blockId,
      String number,
      String countryIso,
      String displayNumber,
      Integer parentViewId) {
    final BlockNumberDialogFragment fragment = new BlockNumberDialogFragment();
    final Bundle args = new Bundle();
    if (blockId != null) {
      args.putInt(ARG_BLOCK_ID, blockId.intValue());
    }
    if (parentViewId != null) {
      args.putInt(ARG_PARENT_VIEW_ID, parentViewId.intValue());
    }
    args.putString(ARG_NUMBER, number);
    args.putString(ARG_COUNTRY_ISO, countryIso);
    args.putString(ARG_DISPLAY_NUMBER, displayNumber);
    fragment.setArguments(args);
    return fragment;
  }

  public void setFilteredNumberAsyncQueryHandlerForTesting(
      FilteredNumberAsyncQueryHandler handler) {
    mHandler = handler;
  }

  @Override
  public Context getContext() {
    return getActivity();
  }

  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    super.onCreateDialog(savedInstanceState);
    final boolean isBlocked = getArguments().containsKey(ARG_BLOCK_ID);

    mNumber = getArguments().getString(ARG_NUMBER);
    mDisplayNumber = getArguments().getString(ARG_DISPLAY_NUMBER);
    mCountryIso = getArguments().getString(ARG_COUNTRY_ISO);

    if (TextUtils.isEmpty(mDisplayNumber)) {
      mDisplayNumber = mNumber;
    }

    mHandler = new FilteredNumberAsyncQueryHandler(getContext());
    mVoicemailEnabledChecker = new VisualVoicemailEnabledChecker(getActivity(), null);
    // Choose not to update VoicemailEnabledChecker, as checks should already been done in
    // all current use cases.
    mParentView = getActivity().findViewById(getArguments().getInt(ARG_PARENT_VIEW_ID));

    CharSequence title;
    String okText;
    String message;
    if (isBlocked) {
      title = null;
      okText = getString(R.string.unblock_number_ok);
      message =
          ContactDisplayUtils.getTtsSpannedPhoneNumber(
                  getResources(), R.string.unblock_number_confirmation_title, mDisplayNumber)
              .toString();
    } else {
      title =
          ContactDisplayUtils.getTtsSpannedPhoneNumber(
              getResources(), R.string.block_number_confirmation_title, mDisplayNumber);
      okText = getString(R.string.block_number_ok);
      if (FilteredNumberCompat.useNewFiltering(getContext())) {
        message = getString(R.string.block_number_confirmation_message_new_filtering);
      } else if (mVoicemailEnabledChecker.isVisualVoicemailEnabled()) {
        message = getString(R.string.block_number_confirmation_message_vvm);
      } else {
        message = getString(R.string.block_number_confirmation_message_no_vvm);
      }
    }

    AlertDialog.Builder builder =
        new AlertDialog.Builder(getActivity())
            .setTitle(title)
            .setMessage(message)
            .setPositiveButton(
                okText,
                new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int id) {
                    if (isBlocked) {
                      unblockNumber();
                    } else {
                      blockNumber();
                    }
                  }
                })
            .setNegativeButton(android.R.string.cancel, null);
    return builder.create();
  }

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    String e164Number = PhoneNumberUtils.formatNumberToE164(mNumber, mCountryIso);
    if (!FilteredNumbersUtil.canBlockNumber(getContext(), e164Number, mNumber)) {
      dismiss();
      Toast.makeText(
              getContext(),
              ContactDisplayUtils.getTtsSpannedPhoneNumber(
                  getResources(), R.string.invalidNumber, mDisplayNumber),
              Toast.LENGTH_SHORT)
          .show();
    }
  }

  @Override
  public void onPause() {
    // Dismiss on rotation.
    dismiss();
    mCallback = null;

    super.onPause();
  }

  public void setCallback(Callback callback) {
    mCallback = callback;
  }

  private CharSequence getBlockedMessage() {
    return ContactDisplayUtils.getTtsSpannedPhoneNumber(
        getResources(), R.string.snackbar_number_blocked, mDisplayNumber);
  }

  private CharSequence getUnblockedMessage() {
    return ContactDisplayUtils.getTtsSpannedPhoneNumber(
        getResources(), R.string.snackbar_number_unblocked, mDisplayNumber);
  }

  private int getActionTextColor() {
    return getContext().getResources().getColor(R.color.dialer_snackbar_action_text_color);
  }

  private void blockNumber() {
    final CharSequence message = getBlockedMessage();
    final CharSequence undoMessage = getUnblockedMessage();
    final Callback callback = mCallback;
    final int actionTextColor = getActionTextColor();
    final Context applicationContext = getContext().getApplicationContext();

    final OnUnblockNumberListener onUndoListener =
        new OnUnblockNumberListener() {
          @Override
          public void onUnblockComplete(int rows, ContentValues values) {
            Snackbar.make(mParentView, undoMessage, Snackbar.LENGTH_LONG).show();
            if (callback != null) {
              callback.onChangeFilteredNumberUndo();
            }
          }
        };

    final OnBlockNumberListener onBlockNumberListener =
        new OnBlockNumberListener() {
          @Override
          public void onBlockComplete(final Uri uri) {
            final View.OnClickListener undoListener =
                new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                    // Delete the newly created row on 'undo'.
                    Logger.get(applicationContext)
                        .logInteraction(InteractionEvent.Type.UNDO_BLOCK_NUMBER);
                    mHandler.unblock(onUndoListener, uri);
                  }
                };

            Snackbar.make(mParentView, message, Snackbar.LENGTH_LONG)
                .setAction(R.string.block_number_undo, undoListener)
                .setActionTextColor(actionTextColor)
                .show();

            if (callback != null) {
              callback.onFilterNumberSuccess();
            }

            if (FilteredNumbersUtil.hasRecentEmergencyCall(applicationContext)) {
              FilteredNumbersUtil.maybeNotifyCallBlockingDisabled(applicationContext);
            }
          }
        };

    mHandler.blockNumber(onBlockNumberListener, mNumber, mCountryIso);
  }

  private void unblockNumber() {
    final CharSequence message = getUnblockedMessage();
    final CharSequence undoMessage = getBlockedMessage();
    final Callback callback = mCallback;
    final int actionTextColor = getActionTextColor();
    final Context applicationContext = getContext().getApplicationContext();

    final OnBlockNumberListener onUndoListener =
        new OnBlockNumberListener() {
          @Override
          public void onBlockComplete(final Uri uri) {
            Snackbar.make(mParentView, undoMessage, Snackbar.LENGTH_LONG).show();
            if (callback != null) {
              callback.onChangeFilteredNumberUndo();
            }
          }
        };

    mHandler.unblock(
        new OnUnblockNumberListener() {
          @Override
          public void onUnblockComplete(int rows, final ContentValues values) {
            final View.OnClickListener undoListener =
                new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                    // Re-insert the row on 'undo', with a new ID.
                    Logger.get(applicationContext)
                        .logInteraction(InteractionEvent.Type.UNDO_UNBLOCK_NUMBER);
                    mHandler.blockNumber(onUndoListener, values);
                  }
                };

            Snackbar.make(mParentView, message, Snackbar.LENGTH_LONG)
                .setAction(R.string.block_number_undo, undoListener)
                .setActionTextColor(actionTextColor)
                .show();

            if (callback != null) {
              callback.onUnfilterNumberSuccess();
            }
          }
        },
        getArguments().getInt(ARG_BLOCK_ID));
  }

  /**
   * Use a callback interface to update UI after success/undo. Favor this approach over other more
   * standard paradigms because of the variety of scenarios in which the DialogFragment can be
   * invoked (by an Activity, by a fragment, by an adapter, by an adapter list item). Because of
   * this, we do NOT support retaining state on rotation, and will dismiss the dialog upon rotation
   * instead.
   */
  public interface Callback {

    /** Called when a number is successfully added to the set of filtered numbers */
    void onFilterNumberSuccess();

    /** Called when a number is successfully removed from the set of filtered numbers */
    void onUnfilterNumberSuccess();

    /** Called when the action of filtering or unfiltering a number is undone */
    void onChangeFilteredNumberUndo();
  }
}