1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.dialer.blocking;
18 
19 import android.app.AlertDialog;
20 import android.app.Dialog;
21 import android.app.DialogFragment;
22 import android.content.DialogInterface;
23 import android.content.DialogInterface.OnShowListener;
24 import android.os.Bundle;
25 import android.view.View;
26 import com.android.dialer.blocking.BlockedNumbersMigrator.Listener;
27 import java.util.Objects;
28 
29 /**
30  * Dialog fragment shown to users when they need to migrate to use {@link
31  * android.provider.BlockedNumberContract} for blocking.
32  */
33 public class MigrateBlockedNumbersDialogFragment extends DialogFragment {
34 
35   private BlockedNumbersMigrator mBlockedNumbersMigrator;
36   private BlockedNumbersMigrator.Listener mMigrationListener;
37 
38   /**
39    * Creates a new MigrateBlockedNumbersDialogFragment.
40    *
41    * @param blockedNumbersMigrator The {@link BlockedNumbersMigrator} which will be used to migrate
42    *     the numbers.
43    * @param migrationListener The {@link BlockedNumbersMigrator.Listener} to call when the migration
44    *     is complete.
45    * @return The new MigrateBlockedNumbersDialogFragment.
46    * @throws NullPointerException if blockedNumbersMigrator or migrationListener are {@code null}.
47    */
newInstance( BlockedNumbersMigrator blockedNumbersMigrator, BlockedNumbersMigrator.Listener migrationListener)48   public static DialogFragment newInstance(
49       BlockedNumbersMigrator blockedNumbersMigrator,
50       BlockedNumbersMigrator.Listener migrationListener) {
51     MigrateBlockedNumbersDialogFragment fragment = new MigrateBlockedNumbersDialogFragment();
52     fragment.mBlockedNumbersMigrator = Objects.requireNonNull(blockedNumbersMigrator);
53     fragment.mMigrationListener = Objects.requireNonNull(migrationListener);
54     return fragment;
55   }
56 
57   @Override
onCreateDialog(Bundle savedInstanceState)58   public Dialog onCreateDialog(Bundle savedInstanceState) {
59     super.onCreateDialog(savedInstanceState);
60     AlertDialog dialog =
61         new AlertDialog.Builder(getActivity())
62             .setTitle(R.string.migrate_blocked_numbers_dialog_title)
63             .setMessage(R.string.migrate_blocked_numbers_dialog_message)
64             .setPositiveButton(R.string.migrate_blocked_numbers_dialog_allow_button, null)
65             .setNegativeButton(R.string.migrate_blocked_numbers_dialog_cancel_button, null)
66             .create();
67     // The Dialog's buttons aren't available until show is called, so an OnShowListener
68     // is used to set the positive button callback.
69     dialog.setOnShowListener(
70         new OnShowListener() {
71           @Override
72           public void onShow(DialogInterface dialog) {
73             final AlertDialog alertDialog = (AlertDialog) dialog;
74             alertDialog
75                 .getButton(AlertDialog.BUTTON_POSITIVE)
76                 .setOnClickListener(newPositiveButtonOnClickListener(alertDialog));
77           }
78         });
79     return dialog;
80   }
81 
82   /*
83    * Creates a new View.OnClickListener to be used as the positive button in this dialog. The
84    * OnClickListener will grey out the dialog's positive and negative buttons while the migration
85    * is underway, and close the dialog once the migrate is complete.
86    */
newPositiveButtonOnClickListener(final AlertDialog alertDialog)87   private View.OnClickListener newPositiveButtonOnClickListener(final AlertDialog alertDialog) {
88     return new View.OnClickListener() {
89       @Override
90       public void onClick(View v) {
91         alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
92         alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setEnabled(false);
93         mBlockedNumbersMigrator.migrate(
94             new Listener() {
95               @Override
96               public void onComplete() {
97                 alertDialog.dismiss();
98                 mMigrationListener.onComplete();
99               }
100             });
101       }
102     };
103   }
104 
105   @Override
106   public void onPause() {
107     // The dialog is dismissed and state is cleaned up onPause, i.e. rotation.
108     dismiss();
109     mBlockedNumbersMigrator = null;
110     mMigrationListener = null;
111     super.onPause();
112   }
113 }
114