Home | History | Annotate | Download | only in filterednumber
      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.filterednumber;
     18 
     19 import com.google.common.base.Preconditions;
     20 
     21 import android.app.AlertDialog;
     22 import android.app.Dialog;
     23 import android.app.DialogFragment;
     24 import android.content.DialogInterface;
     25 import android.content.DialogInterface.OnShowListener;
     26 import android.os.Bundle;
     27 import android.view.View;
     28 import com.android.dialer.R;
     29 import com.android.dialer.filterednumber.BlockedNumbersMigrator.Listener;
     30 
     31 /**
     32  * Dialog fragment shown to users when they need to migrate to use
     33  * {@link android.provider.BlockedNumberContract} for blocking.
     34  */
     35 public class MigrateBlockedNumbersDialogFragment extends DialogFragment {
     36 
     37     private BlockedNumbersMigrator mBlockedNumbersMigrator;
     38     private BlockedNumbersMigrator.Listener mMigrationListener;
     39 
     40     /**
     41      * Creates a new MigrateBlockedNumbersDialogFragment.
     42      *
     43      * @param blockedNumbersMigrator The {@link BlockedNumbersMigrator} which will be used to
     44      * migrate the numbers.
     45      * @param migrationListener The {@link BlockedNumbersMigrator.Listener} to call when the
     46      * migration is complete.
     47      * @return The new MigrateBlockedNumbersDialogFragment.
     48      * @throws NullPointerException if blockedNumbersMigrator or migrationListener are {@code null}.
     49      */
     50     public static DialogFragment newInstance(BlockedNumbersMigrator blockedNumbersMigrator,
     51             BlockedNumbersMigrator.Listener migrationListener) {
     52         MigrateBlockedNumbersDialogFragment fragment = new MigrateBlockedNumbersDialogFragment();
     53         fragment.mBlockedNumbersMigrator = Preconditions.checkNotNull(blockedNumbersMigrator);
     54         fragment.mMigrationListener = Preconditions.checkNotNull(migrationListener);
     55         return fragment;
     56     }
     57 
     58     @Override
     59     public Dialog onCreateDialog(Bundle savedInstanceState) {
     60         super.onCreateDialog(savedInstanceState);
     61         AlertDialog dialog = 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(new OnShowListener() {
     70             @Override
     71             public void onShow(DialogInterface dialog) {
     72                 final AlertDialog alertDialog = (AlertDialog) dialog;
     73                 alertDialog.getButton(AlertDialog.BUTTON_POSITIVE)
     74                         .setOnClickListener(newPositiveButtonOnClickListener(alertDialog));
     75             }
     76         });
     77         return dialog;
     78     }
     79 
     80     /*
     81      * Creates a new View.OnClickListener to be used as the positive button in this dialog. The
     82      * OnClickListener will grey out the dialog's positive and negative buttons while the migration
     83      * is underway, and close the dialog once the migrate is complete.
     84      */
     85     private View.OnClickListener newPositiveButtonOnClickListener(final AlertDialog alertDialog) {
     86         return new View.OnClickListener() {
     87             @Override
     88             public void onClick(View v) {
     89                 alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
     90                 alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setEnabled(false);
     91                 mBlockedNumbersMigrator.migrate(new Listener() {
     92                     @Override
     93                     public void onComplete() {
     94                         alertDialog.dismiss();
     95                         mMigrationListener.onComplete();
     96                     }
     97                 });
     98             }
     99         };
    100     }
    101 
    102     @Override
    103     public void onPause() {
    104         // The dialog is dismissed and state is cleaned up onPause, i.e. rotation.
    105         dismiss();
    106         mBlockedNumbersMigrator = null;
    107         mMigrationListener = null;
    108         super.onPause();
    109     }
    110 }
    111