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 @Deprecated
34 public class MigrateBlockedNumbersDialogFragment extends DialogFragment {
35 
36   private BlockedNumbersMigrator blockedNumbersMigrator;
37   private BlockedNumbersMigrator.Listener migrationListener;
38 
39   /**
40    * Creates a new MigrateBlockedNumbersDialogFragment.
41    *
42    * @param blockedNumbersMigrator The {@link BlockedNumbersMigrator} which will be used to migrate
43    *     the numbers.
44    * @param migrationListener The {@link BlockedNumbersMigrator.Listener} to call when the migration
45    *     is complete.
46    * @return The new MigrateBlockedNumbersDialogFragment.
47    * @throws NullPointerException if blockedNumbersMigrator or migrationListener are {@code null}.
48    */
newInstance( BlockedNumbersMigrator blockedNumbersMigrator, BlockedNumbersMigrator.Listener migrationListener)49   public static DialogFragment newInstance(
50       BlockedNumbersMigrator blockedNumbersMigrator,
51       BlockedNumbersMigrator.Listener migrationListener) {
52     MigrateBlockedNumbersDialogFragment fragment = new MigrateBlockedNumbersDialogFragment();
53     fragment.blockedNumbersMigrator = Objects.requireNonNull(blockedNumbersMigrator);
54     fragment.migrationListener = Objects.requireNonNull(migrationListener);
55     return fragment;
56   }
57 
58   @Override
onCreateDialog(Bundle savedInstanceState)59   public Dialog onCreateDialog(Bundle savedInstanceState) {
60     super.onCreateDialog(savedInstanceState);
61     AlertDialog dialog =
62         new AlertDialog.Builder(getActivity())
63             .setTitle(R.string.migrate_blocked_numbers_dialog_title)
64             .setMessage(R.string.migrate_blocked_numbers_dialog_message)
65             .setPositiveButton(R.string.migrate_blocked_numbers_dialog_allow_button, null)
66             .setNegativeButton(R.string.migrate_blocked_numbers_dialog_cancel_button, null)
67             .create();
68     // The Dialog's buttons aren't available until show is called, so an OnShowListener
69     // is used to set the positive button callback.
70     dialog.setOnShowListener(
71         new OnShowListener() {
72           @Override
73           public void onShow(DialogInterface dialog) {
74             final AlertDialog alertDialog = (AlertDialog) dialog;
75             alertDialog
76                 .getButton(AlertDialog.BUTTON_POSITIVE)
77                 .setOnClickListener(newPositiveButtonOnClickListener(alertDialog));
78           }
79         });
80     return dialog;
81   }
82 
83   /*
84    * Creates a new View.OnClickListener to be used as the positive button in this dialog. The
85    * OnClickListener will grey out the dialog's positive and negative buttons while the migration
86    * is underway, and close the dialog once the migrate is complete.
87    */
newPositiveButtonOnClickListener(final AlertDialog alertDialog)88   private View.OnClickListener newPositiveButtonOnClickListener(final AlertDialog alertDialog) {
89     return new View.OnClickListener() {
90       @Override
91       public void onClick(View v) {
92         alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
93         alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setEnabled(false);
94         blockedNumbersMigrator.migrate(
95             new Listener() {
96               @Override
97               public void onComplete() {
98                 alertDialog.dismiss();
99                 migrationListener.onComplete();
100               }
101             });
102       }
103     };
104   }
105 
106   @Override
107   public void onPause() {
108     // The dialog is dismissed and state is cleaned up onPause, i.e. rotation.
109     dismiss();
110     blockedNumbersMigrator = null;
111     migrationListener = null;
112     super.onPause();
113   }
114 }
115