Adding block numbers migration dialog

+ When the user attempts to block a number and they haven't migrated
to the framework blocking implementation, they should be shown a
dialog that asks them to migrate. This CL introduces the Dialog that
is shown and updates the Call log and Call details to open it.
+ As part one of the change, the Dialog is shown every time the user
attempts to block or unblock a number (when the feature is enabled).
A later CL will complete this migration step to ensure that the
dialog is only shown until migration is finished.

Bug: 26664600
Change-Id: Ia4c2d504f8d98679b90d232058eb5ee6ea9b38f1
diff --git a/src/com/android/dialer/CallDetailActivity.java b/src/com/android/dialer/CallDetailActivity.java
index e6a29fb..d2cda92 100644
--- a/src/com/android/dialer/CallDetailActivity.java
+++ b/src/com/android/dialer/CallDetailActivity.java
@@ -54,10 +54,12 @@
 import com.android.dialer.calllog.CallTypeHelper;
 import com.android.dialer.calllog.ContactInfoHelper;
 import com.android.dialer.calllog.PhoneAccountUtils;
+import com.android.dialer.compat.FilteredNumberCompat;
 import com.android.dialer.database.FilteredNumberAsyncQueryHandler;
 import com.android.dialer.database.FilteredNumberAsyncQueryHandler.OnCheckBlockedListener;
 import com.android.dialer.filterednumber.BlockNumberDialogFragment;
 import com.android.dialer.filterednumber.FilteredNumbersUtil;
+import com.android.dialer.filterednumber.MigrateBlockedNumbersDialogFragment;
 import com.android.dialer.logging.InteractionEvent;
 import com.android.dialer.logging.Logger;
 import com.android.dialer.util.DialerUtils;
@@ -372,14 +374,9 @@
     public void onClick(View view) {
         int resId = view.getId();
         if (resId == R.id.call_detail_action_block) {
-            BlockNumberDialogFragment.show(
-                    mBlockedNumberId,
-                    mNumber,
-                    mDetails.countryIso,
-                    mDisplayNumber,
-                    R.id.call_detail,
-                    getFragmentManager(),
-                    this);
+            FilteredNumberCompat
+                    .showBlockNumberDialogFlow(mBlockedNumberId, mNumber, mDetails.countryIso,
+                            mDisplayNumber, R.id.call_detail, getFragmentManager(), this);
         } else if (resId == R.id.call_detail_action_copy) {
             ClipboardUtils.copyText(mContext, null, mNumber, true);
         } else if (resId == R.id.call_detail_action_edit_before_call) {
diff --git a/src/com/android/dialer/calllog/CallLogListItemViewHolder.java b/src/com/android/dialer/calllog/CallLogListItemViewHolder.java
index 392672f..de9bf77 100644
--- a/src/com/android/dialer/calllog/CallLogListItemViewHolder.java
+++ b/src/com/android/dialer/calllog/CallLogListItemViewHolder.java
@@ -51,9 +51,11 @@
 import com.android.dialer.DialtactsActivity;
 import com.android.dialer.R;
 import com.android.dialer.calllog.calllogcache.CallLogCache;
+import com.android.dialer.compat.FilteredNumberCompat;
 import com.android.dialer.database.FilteredNumberAsyncQueryHandler;
 import com.android.dialer.filterednumber.BlockNumberDialogFragment;
 import com.android.dialer.filterednumber.FilteredNumbersUtil;
+import com.android.dialer.filterednumber.MigrateBlockedNumbersDialogFragment;
 import com.android.dialer.logging.Logger;
 import com.android.dialer.logging.ScreenEvent;
 import com.android.dialer.service.ExtendedBlockingButtonRenderer;
@@ -370,14 +372,11 @@
     public boolean onMenuItemClick(MenuItem item) {
         int resId = item.getItemId();
         if (resId == R.id.context_menu_block_number) {
-            BlockNumberDialogFragment.show(
-                    blockId,
-                    number,
-                    countryIso,
-                    displayNumber,
-                    R.id.floating_action_button_container,
-                    ((Activity) mContext).getFragmentManager(),
-                    mFilteredNumberDialogCallback);
+            FilteredNumberCompat
+                    .showBlockNumberDialogFlow(blockId, number, countryIso, displayNumber,
+                            R.id.floating_action_button_container,
+                            ((Activity) mContext).getFragmentManager(),
+                            mFilteredNumberDialogCallback);
             return true;
         } else if (resId == R.id.context_menu_copy_to_clipboard) {
             ClipboardUtils.copyText(mContext, null, number, true);
diff --git a/src/com/android/dialer/compat/FilteredNumberCompat.java b/src/com/android/dialer/compat/FilteredNumberCompat.java
index 3ad45e8..743bf28 100644
--- a/src/com/android/dialer/compat/FilteredNumberCompat.java
+++ b/src/com/android/dialer/compat/FilteredNumberCompat.java
@@ -18,9 +18,9 @@
 
 import com.google.common.base.Preconditions;
 
+import android.app.FragmentManager;
 import android.content.ContentUris;
 import android.content.ContentValues;
-import android.content.Context;
 import android.net.Uri;
 import android.preference.PreferenceManager;
 import android.support.annotation.Nullable;
@@ -33,6 +33,10 @@
 import com.android.dialer.database.FilteredNumberContract.FilteredNumberColumns;
 import com.android.dialer.database.FilteredNumberContract.FilteredNumberSources;
 import com.android.dialer.database.FilteredNumberContract.FilteredNumberTypes;
+import com.android.dialer.filterednumber.BlockNumberDialogFragment;
+import com.android.dialer.filterednumber.BlockNumberDialogFragment.Callback;
+import com.android.dialer.filterednumber.MigrateBlockedNumbersDialogFragment;
+import com.android.dialer.filterednumber.MigrateBlockedNumbersDialogFragment.Listener;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -222,4 +226,41 @@
         }
         return contentValues;
     }
+
+    /**
+     * Shows the flow of {@link android.app.DialogFragment}s for blocking or unblocking numbers.
+     *
+     * @param blockId The id into the blocked numbers database.
+     * @param number The number to block or unblock.
+     * @param countryIso The countryIso used to format the given number.
+     * @param displayNumber The form of the number to block, suitable for displaying.
+     * @param parentViewId The id for the containing view of the Dialog.
+     * @param fragmentManager The {@link FragmentManager} used to show fragments.
+     * @param callback (optional) The {@link Callback} to call when the block or unblock operation
+     * is complete.
+     */
+    public static void showBlockNumberDialogFlow(final Integer blockId, final String number,
+            final String countryIso, final String displayNumber, final Integer parentViewId,
+            final FragmentManager fragmentManager, @Nullable final Callback callback) {
+        // If the user is blocking a number and isn't using the framework solution when they
+        // should be, show the migration dialog
+        if (shouldShowMigrationDialog(blockId == null)) {
+            MigrateBlockedNumbersDialogFragment.newInstance(new Listener() {
+                @Override
+                public void onComplete() {
+                    BlockNumberDialogFragment
+                            .show(null, number, countryIso, displayNumber, parentViewId,
+                                    fragmentManager, callback);
+                }
+            }).show(fragmentManager, "MigrateBlockedNumbers");
+            return;
+        }
+        BlockNumberDialogFragment
+                .show(blockId, number, countryIso, displayNumber, parentViewId, fragmentManager,
+                        callback);
+    }
+
+    private static boolean shouldShowMigrationDialog(boolean isBlocking) {
+        return isBlocking && canUseNewFiltering() && !hasMigratedToNewBlocking();
+    }
 }
diff --git a/src/com/android/dialer/filterednumber/MigrateBlockedNumbersDialogFragment.java b/src/com/android/dialer/filterednumber/MigrateBlockedNumbersDialogFragment.java
new file mode 100644
index 0000000..3783519
--- /dev/null
+++ b/src/com/android/dialer/filterednumber/MigrateBlockedNumbersDialogFragment.java
@@ -0,0 +1,97 @@
+/*
+ * 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.filterednumber;
+
+import com.google.common.base.Preconditions;
+
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.app.DialogFragment;
+import android.content.DialogInterface;
+import android.content.DialogInterface.OnClickListener;
+import android.os.Bundle;
+import android.support.annotation.Nullable;
+
+import com.android.dialer.R;
+
+/**
+ * Dialog fragment shown to users when they need to migrate to use
+ * {@link android.provider.BlockedNumberContract} for blocking.
+ */
+public class MigrateBlockedNumbersDialogFragment extends DialogFragment {
+
+    /**
+     * Listener for the operation to migrate from
+     * {@link com.android.dialer.database.FilteredNumberContract} blocking to
+     * {@link android.provider.BlockedNumberContract} blocking.
+     */
+    public interface Listener {
+
+        /**
+         * Callback called when the migration operation is finished.
+         */
+        void onComplete();
+    }
+
+    private Listener mMigrationListener;
+
+    /**
+     * Creates a new MigrateBlockedNumbersDialogFragment.
+     *
+     * @param migrationListener The {@link Listener} to call when the
+     * migration is complete.
+     * @return the new MigrateBlockedNumbersDialogFragment.
+     * @throws NullPointerException if migrationListener is {@code null}.
+     */
+    public static DialogFragment newInstance(Listener migrationListener) {
+        MigrateBlockedNumbersDialogFragment fragment = new MigrateBlockedNumbersDialogFragment();
+        fragment.mMigrationListener = Preconditions.checkNotNull(migrationListener);
+        return fragment;
+    }
+
+    @Override
+    public Dialog onCreateDialog(Bundle savedInstanceState) {
+        super.onCreateDialog(savedInstanceState);
+        return new AlertDialog.Builder(getActivity())
+                .setTitle(R.string.migrate_blocked_numbers_dialog_title)
+                .setMessage(R.string.migrate_blocked_numbers_dialog_message)
+                .setPositiveButton(R.string.migrate_blocked_numbers_dialog_allow_button,
+                        newPositiveButtonOnClickListener())
+                .setNegativeButton(R.string.migrate_blocked_numbers_dialog_cancel_button, null)
+                .create();
+    }
+
+    private DialogInterface.OnClickListener newPositiveButtonOnClickListener() {
+        return new OnClickListener() {
+            @Override
+            public void onClick(DialogInterface dialog, int which) {
+                // TODO(maxwelb): Perform actual migration, call
+                // FilteredNumberCompat#hasMigratedToNewBlocking
+
+                mMigrationListener.onComplete();
+            }
+        };
+    }
+
+    @Override
+    public void onPause() {
+        // The dialog is dismissed and state is cleaned up onPause, i.e. rotation.
+        dismiss();
+        mMigrationListener = null;
+        super.onPause();
+    }
+}