diff options
author | Sarmad Hashmi <mhashmi@google.com> | 2016-04-13 15:10:20 -0700 |
---|---|---|
committer | Sarmad Hashmi <mhashmi@google.com> | 2016-04-15 16:23:32 -0700 |
commit | b86e29447c861ad90874969d806a9bd1f8dd7348 (patch) | |
tree | 1b2c37b5d671907f3128eafab980a49ee6430c47 /src/com/android | |
parent | eaad81bcb32aad730604e5b329c10b1f601dedc1 (diff) |
Refactor block/report dialogs and put into utility class.
+Put all the dialog creation code in one place so they can be re-used
+This is mainly for the after call notification because the same dialogs
will be shown in an invisible activity
+Create listener for determining whether a user checked the spam
checkbox
+Change to DialogFragments for easy reusing of dialogs
Change-Id: I67e13bc0e61952299a4770c9184543ef8b63728a
Diffstat (limited to 'src/com/android')
3 files changed, 370 insertions, 113 deletions
diff --git a/src/com/android/dialer/calllog/BlockReportSpamListener.java b/src/com/android/dialer/calllog/BlockReportSpamListener.java index bda4ee538..62b9b9311 100644 --- a/src/com/android/dialer/calllog/BlockReportSpamListener.java +++ b/src/com/android/dialer/calllog/BlockReportSpamListener.java @@ -1,32 +1,30 @@ package com.android.dialer.calllog; -import android.app.AlertDialog; +import android.app.Activity; +import android.app.FragmentManager; import android.content.ContentValues; -import android.content.Context; import android.content.DialogInterface; import android.net.Uri; import android.support.v7.widget.RecyclerView; -import android.view.View; -import android.widget.CheckBox; +import com.android.dialer.util.BlockReportSpamDialogs; import com.android.dialer.database.FilteredNumberAsyncQueryHandler; import com.android.dialer.service.ExtendedCallInfoService; -import com.android.dialer.R; /** * Listener to show dialogs for block and report spam actions. */ public class BlockReportSpamListener implements CallLogListItemViewHolder.OnClickListener { - private final Context mContext; + private final FragmentManager mFragmentManager; private final RecyclerView.Adapter mAdapter; private final ExtendedCallInfoService mExtendedCallInfoService; private final FilteredNumberAsyncQueryHandler mFilteredNumberAsyncQueryHandler; - public BlockReportSpamListener(Context context, RecyclerView.Adapter adapter, + public BlockReportSpamListener(FragmentManager fragmentManager, RecyclerView.Adapter adapter, ExtendedCallInfoService extendedCallInfoService, FilteredNumberAsyncQueryHandler filteredNumberAsyncQueryHandler) { - mContext = context; + mFragmentManager = fragmentManager; mAdapter = adapter; mExtendedCallInfoService = extendedCallInfoService; mFilteredNumberAsyncQueryHandler = filteredNumberAsyncQueryHandler; @@ -35,124 +33,88 @@ public class BlockReportSpamListener implements CallLogListItemViewHolder.OnClic @Override public void onBlockReportSpam(String displayNumber, final String number, final String countryIso, final int callType) { - final View dialogView = View.inflate(mContext, R.layout.block_report_spam_dialog, null); - - AlertDialog.Builder alertDialogBuilder = createDialogBuilder(); - alertDialogBuilder - .setView(dialogView) - .setTitle(mContext.getString( - R.string.block_report_number_alert_title, displayNumber)) - .setPositiveButton(mContext.getString(R.string.block_number_ok), - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - CheckBox isSpamCheckbox = (CheckBox) dialogView - .findViewById(R.id.report_number_as_spam_action); - if (isSpamCheckbox.isChecked()) { - mExtendedCallInfoService.reportSpam( - number, countryIso, callType); - } - mFilteredNumberAsyncQueryHandler.blockNumber( - new FilteredNumberAsyncQueryHandler.OnBlockNumberListener() { - @Override - public void onBlockComplete(Uri uri) { - mAdapter.notifyDataSetChanged(); - } - }, - number, - countryIso); - } - }); - alertDialogBuilder.show(); + BlockReportSpamDialogs.BlockReportSpamDialogFragment.newInstance( + displayNumber, + false, + new BlockReportSpamDialogs.OnSpamDialogClickListener() { + @Override + public void onClick(boolean isSpamChecked) { + if (isSpamChecked) { + mExtendedCallInfoService.reportSpam( + number, countryIso, callType); + } + mFilteredNumberAsyncQueryHandler.blockNumber( + new FilteredNumberAsyncQueryHandler.OnBlockNumberListener() { + @Override + public void onBlockComplete(Uri uri) { + mAdapter.notifyDataSetChanged(); + } + }, + number, + countryIso); + } + }, null) + .show(mFragmentManager, BlockReportSpamDialogs.BLOCK_REPORT_SPAM_DIALOG_TAG); } @Override public void onBlock(String displayNumber, final String number, final String countryIso, final int callType) { - AlertDialog.Builder alertDialogBuilder = createDialogBuilder(); - alertDialogBuilder - .setTitle(mContext.getString( - R.string.block_report_number_alert_title, displayNumber)) - .setMessage(R.string.block_number_alert_details) - .setPositiveButton(mContext.getString(R.string.block_number_ok), - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - mExtendedCallInfoService.reportSpam(number, countryIso, callType); - mFilteredNumberAsyncQueryHandler.blockNumber( - new FilteredNumberAsyncQueryHandler.OnBlockNumberListener() { - @Override - public void onBlockComplete(Uri uri) { - mAdapter.notifyDataSetChanged(); - } - }, - number, - countryIso); - } - }); - alertDialogBuilder.show(); + BlockReportSpamDialogs.BlockDialogFragment.newInstance(displayNumber, + new BlockReportSpamDialogs.OnConfirmListener() { + @Override + public void onClick() { + mExtendedCallInfoService.reportSpam(number, countryIso, callType); + mFilteredNumberAsyncQueryHandler.blockNumber( + new FilteredNumberAsyncQueryHandler.OnBlockNumberListener() { + @Override + public void onBlockComplete(Uri uri) { + mAdapter.notifyDataSetChanged(); + } + }, + number, + countryIso); + } + }, null) + .show(mFragmentManager, BlockReportSpamDialogs.BLOCK_DIALOG_TAG); } @Override public void onUnblock(String displayNumber, final String number, final String countryIso, final Integer blockId, final boolean isSpam, final int callType) { - AlertDialog.Builder alertDialogBuilder = createDialogBuilder(); - if (isSpam) { - alertDialogBuilder.setMessage(R.string.unblock_number_alert_details); - } - alertDialogBuilder - .setTitle(mContext.getString( - R.string.unblock_report_number_alert_title, displayNumber)) - .setPositiveButton(R.string.unblock_number_ok, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - if (isSpam) { - mExtendedCallInfoService.reportNotSpam( - number, countryIso, callType); - } - mFilteredNumberAsyncQueryHandler.unblock( - new FilteredNumberAsyncQueryHandler.OnUnblockNumberListener() { - @Override - public void onUnblockComplete(int rows, ContentValues values) { - mAdapter.notifyDataSetChanged(); - } - }, - blockId); - } - }); - alertDialogBuilder.show(); + BlockReportSpamDialogs.UnblockDialogFragment.newInstance(displayNumber, isSpam, + new BlockReportSpamDialogs.OnConfirmListener() { + @Override + public void onClick() { + if (isSpam) { + mExtendedCallInfoService.reportNotSpam( + number, countryIso, callType); + } + mFilteredNumberAsyncQueryHandler.unblock( + new FilteredNumberAsyncQueryHandler.OnUnblockNumberListener() { + @Override + public void onUnblockComplete(int rows, ContentValues values) { + mAdapter.notifyDataSetChanged(); + } + }, + blockId); + } + }, null) + .show(mFragmentManager, BlockReportSpamDialogs.UNBLOCK_DIALOG_TAG); } @Override public void onReportNotSpam(String displayNumber, final String number, final String countryIso, final int callType) { - AlertDialog.Builder alertDialogBuilder = createDialogBuilder(); - alertDialogBuilder - .setTitle(mContext.getString( - R.string.report_not_spam_alert_title, displayNumber)) - .setMessage(R.string.report_not_spam_alert_details) - .setPositiveButton(R.string.report_not_spam_alert_button, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - mExtendedCallInfoService.reportNotSpam( - number, countryIso, callType); - mAdapter.notifyDataSetChanged(); - } - }); - alertDialogBuilder.show(); - } - - private AlertDialog.Builder createDialogBuilder() { - return new AlertDialog.Builder(mContext) - .setCancelable(true) - .setNegativeButton(mContext.getString(android.R.string.cancel), - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - dialog.cancel(); - } - }); + BlockReportSpamDialogs.ReportNotSpamDialogFragment.newInstance(displayNumber, + new BlockReportSpamDialogs.OnConfirmListener() { + @Override + public void onClick() { + mExtendedCallInfoService.reportNotSpam( + number, countryIso, callType); + mAdapter.notifyDataSetChanged(); + } + }, null) + .show(mFragmentManager, BlockReportSpamDialogs.NOT_SPAM_DIALOG_TAG); } } diff --git a/src/com/android/dialer/calllog/CallLogAdapter.java b/src/com/android/dialer/calllog/CallLogAdapter.java index 36726e456..9cde0b65d 100644 --- a/src/com/android/dialer/calllog/CallLogAdapter.java +++ b/src/com/android/dialer/calllog/CallLogAdapter.java @@ -21,6 +21,7 @@ import com.android.dialer.service.ExtendedCallInfoService; import com.android.dialerbind.ObjectFactory; import com.google.common.annotations.VisibleForTesting; +import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; @@ -317,7 +318,8 @@ public class CallLogAdapter extends GroupingListAdapter maybeShowVoicemailPromoCard(); mExtendedCallInfoService = ObjectFactory.newExtendedCallInfoService(context); - mBlockReportSpamListener = new BlockReportSpamListener(mContext, this, + mBlockReportSpamListener = new BlockReportSpamListener( + ((Activity) mContext).getFragmentManager(), this, mExtendedCallInfoService, mFilteredNumberAsyncQueryHandler); setHasStableIds(true); } diff --git a/src/com/android/dialer/util/BlockReportSpamDialogs.java b/src/com/android/dialer/util/BlockReportSpamDialogs.java new file mode 100644 index 000000000..45b2f4228 --- /dev/null +++ b/src/com/android/dialer/util/BlockReportSpamDialogs.java @@ -0,0 +1,293 @@ +/* + * 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.util; + +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; +import android.app.DialogFragment; +import android.content.DialogInterface; +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.view.View; +import android.widget.CheckBox; +import android.widget.CompoundButton; + +import com.android.dialer.R; + +/** + * Helper class for creating block/report dialog fragments. + */ +public class BlockReportSpamDialogs { + public static final String BLOCK_REPORT_SPAM_DIALOG_TAG = "BlockReportSpamDialog"; + public static final String BLOCK_DIALOG_TAG = "BlockDialog"; + public static final String UNBLOCK_DIALOG_TAG = "UnblockDialog"; + public static final String NOT_SPAM_DIALOG_TAG = "NotSpamDialog"; + + /** + * Listener passed to block/report spam dialog for positive click in + * {@link BlockReportSpamDialogFragment}. + */ + public interface OnSpamDialogClickListener { + /** + * Called when user clicks on positive button in block/report spam dialog. + * @param isSpamChecked Whether the spam checkbox is checked. + */ + void onClick(boolean isSpamChecked); + } + + /** + * Listener passed to all dialogs except the block/report spam dialog for positive click. + */ + public interface OnConfirmListener { + /** + * Called when user clicks on positive button in the dialog. + */ + void onClick(); + } + + /** + * Contains the common attributes between all block/unblock/report dialog fragments. + */ + private static class CommonDialogsFragment extends DialogFragment { + /** + * The number to display in the dialog title. + */ + protected String mDisplayNumber; + + /** + * Called when dialog positive button is pressed. + */ + protected OnConfirmListener mPositiveListener; + + /** + * Called when dialog is dismissed. + */ + @Nullable + protected DialogInterface.OnDismissListener mDismissListener; + + @Override + public void onDismiss(DialogInterface dialog) { + if (mDismissListener != null) { + mDismissListener.onDismiss(dialog); + } + super.onDismiss(dialog); + } + + @Override + public void onPause() { + // The dialog is dismissed onPause, i.e. rotation. + dismiss(); + mDismissListener = null; + mPositiveListener = null; + mDisplayNumber = null; + super.onPause(); + } + } + + /** + * Dialog for block/report spam with the mark as spam checkbox. + */ + public static class BlockReportSpamDialogFragment extends CommonDialogsFragment { + /** + * Called when dialog positive button is pressed. + */ + private OnSpamDialogClickListener mPositiveListener; + + /** + * Whether the mark as spam checkbox is checked before displaying the dialog. + */ + private boolean mSpamChecked; + + public static DialogFragment newInstance(String displayNumber, + boolean spamChecked, + OnSpamDialogClickListener positiveListener, + @Nullable DialogInterface.OnDismissListener + dismissListener) { + BlockReportSpamDialogFragment fragment = new BlockReportSpamDialogFragment(); + fragment.mSpamChecked = spamChecked; + fragment.mDisplayNumber = displayNumber; + fragment.mPositiveListener = positiveListener; + fragment.mDismissListener = dismissListener; + return fragment; + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + super.onCreateDialog(savedInstanceState); + View dialogView = View.inflate(getActivity(), R.layout.block_report_spam_dialog, null); + final CheckBox isSpamCheckbox = + (CheckBox) dialogView.findViewById(R.id.report_number_as_spam_action); + // Listen for changes on the checkbox and update if orientation changes + isSpamCheckbox.setChecked(mSpamChecked); + isSpamCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + mSpamChecked = isChecked; + } + }); + + AlertDialog.Builder alertDialogBuilder = createDialogBuilder(getActivity(), this); + Dialog dialog = alertDialogBuilder + .setView(dialogView) + .setTitle(getString(R.string.block_report_number_alert_title, mDisplayNumber)) + .setPositiveButton(R.string.block_number_ok, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dismiss(); + mPositiveListener.onClick(isSpamCheckbox.isChecked()); + } + }).create(); + dialog.setCanceledOnTouchOutside(true); + return dialog; + } + } + + /** + * Dialog for blocking a number. + */ + public static class BlockDialogFragment extends CommonDialogsFragment { + public static DialogFragment newInstance(String displayNumber, + OnConfirmListener positiveListener, + @Nullable DialogInterface.OnDismissListener + dismissListener) { + BlockDialogFragment fragment = new BlockDialogFragment(); + fragment.mDisplayNumber = displayNumber; + fragment.mPositiveListener = positiveListener; + fragment.mDismissListener = dismissListener; + return fragment; + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + super.onCreateDialog(savedInstanceState); + // Return the newly created dialog + AlertDialog.Builder alertDialogBuilder = createDialogBuilder(getActivity(), this); + Dialog dialog = alertDialogBuilder + .setTitle(getString(R.string.block_report_number_alert_title, mDisplayNumber)) + .setPositiveButton(R.string.block_number_ok, + createGenericOnClickListener(this, mPositiveListener)) + .create(); + dialog.setCanceledOnTouchOutside(true); + return dialog; + } + } + + /** + * Dialog for unblocking a number. + */ + public static class UnblockDialogFragment extends CommonDialogsFragment { + /** + * Whether or not the number is spam. + */ + private boolean mIsSpam; + + public static DialogFragment newInstance(String displayNumber, + boolean isSpam, + OnConfirmListener positiveListener, + @Nullable DialogInterface.OnDismissListener + dismissListener) { + UnblockDialogFragment fragment = new UnblockDialogFragment(); + fragment.mDisplayNumber = displayNumber; + fragment.mIsSpam = isSpam; + fragment.mPositiveListener = positiveListener; + fragment.mDismissListener = dismissListener; + return fragment; + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + super.onCreateDialog(savedInstanceState); + // Return the newly created dialog + AlertDialog.Builder alertDialogBuilder = createDialogBuilder(getActivity(), this); + if (mIsSpam) { + alertDialogBuilder.setMessage(R.string.unblock_number_alert_details); + } + Dialog dialog = alertDialogBuilder + .setTitle(getString(R.string.unblock_report_number_alert_title, mDisplayNumber)) + .setPositiveButton(R.string.unblock_number_ok, + createGenericOnClickListener(this, mPositiveListener)) + .create(); + dialog.setCanceledOnTouchOutside(true); + return dialog; + } + } + + /** + * Dialog for reporting a number as not spam. + */ + public static class ReportNotSpamDialogFragment extends CommonDialogsFragment { + public static DialogFragment newInstance(String displayNumber, + OnConfirmListener positiveListener, + @Nullable DialogInterface.OnDismissListener + dismissListener) { + ReportNotSpamDialogFragment fragment = new ReportNotSpamDialogFragment(); + fragment.mDisplayNumber = displayNumber; + fragment.mPositiveListener = positiveListener; + fragment.mDismissListener = dismissListener; + return fragment; + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + super.onCreateDialog(savedInstanceState); + // Return the newly created dialog + AlertDialog.Builder alertDialogBuilder = createDialogBuilder(getActivity(), this); + Dialog dialog = alertDialogBuilder + .setTitle(getString(R.string.report_not_spam_alert_title, mDisplayNumber)) + .setMessage(R.string.report_not_spam_alert_details) + .setPositiveButton(R.string.report_not_spam_alert_button, + createGenericOnClickListener(this, mPositiveListener)) + .create(); + dialog.setCanceledOnTouchOutside(true); + return dialog; + } + } + + /** + * Creates a dialog with the default cancel button listener (dismisses dialog). + */ + private static AlertDialog.Builder createDialogBuilder(Activity activity, + final DialogFragment fragment) { + return new AlertDialog.Builder(activity) + .setCancelable(true) + .setNegativeButton(android.R.string.cancel, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + fragment.dismiss(); + } + }); + } + + /** + * Creates a generic click listener which dismisses the fragment and then calls the actual + * listener. + */ + private static DialogInterface.OnClickListener createGenericOnClickListener( + final DialogFragment fragment, + final OnConfirmListener listener) { + return new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + fragment.dismiss(); + listener.onClick(); + } + }; + } +} |