From 6dfdbbbd85d702c638188ab6be39df4d6621f2d1 Mon Sep 17 00:00:00 2001 From: Android Dialer Date: Tue, 17 Apr 2018 18:24:01 -0700 Subject: Add spam blocking promotion after showing spam dialog in after-call notification Test: N/A PiperOrigin-RevId: 193290689 Change-Id: Idff99e2f67d5161d09b7ea3c0818ed92d2286b61 --- .../app/calllog/BlockReportSpamListener.java | 14 +++- .../promo/SpamBlockingPromoDialogFragment.java | 20 +++++- .../dialer/spam/promo/SpamBlockingPromoHelper.java | 81 ++++++++++++++-------- .../incallui/spam/SpamNotificationActivity.java | 50 ++++++++++--- 4 files changed, 122 insertions(+), 43 deletions(-) (limited to 'java') diff --git a/java/com/android/dialer/app/calllog/BlockReportSpamListener.java b/java/com/android/dialer/app/calllog/BlockReportSpamListener.java index d52ac3241..551791850 100644 --- a/java/com/android/dialer/app/calllog/BlockReportSpamListener.java +++ b/java/com/android/dialer/app/calllog/BlockReportSpamListener.java @@ -97,7 +97,7 @@ public class BlockReportSpamListener implements CallLogListItemViewHolder.OnClic countryIso); if (isSpamChecked) { - spamBlockingPromoHelper.showSpamBlockingPromoDialog(rootView, fragmentManager); + showSpamBlockingPromoDialog(); } }, null) @@ -136,8 +136,7 @@ public class BlockReportSpamListener implements CallLogListItemViewHolder.OnClic }, number, countryIso); - - spamBlockingPromoHelper.showSpamBlockingPromoDialog(rootView, fragmentManager); + showSpamBlockingPromoDialog(); }, null) .show(fragmentManager, BlockReportSpamDialogs.BLOCK_DIALOG_TAG); @@ -205,4 +204,13 @@ public class BlockReportSpamListener implements CallLogListItemViewHolder.OnClic null) .show(fragmentManager, BlockReportSpamDialogs.NOT_SPAM_DIALOG_TAG); } + + private void showSpamBlockingPromoDialog() { + if (spamBlockingPromoHelper.shouldShowSpamBlockingPromo()) { + spamBlockingPromoHelper.showSpamBlockingPromoDialog( + fragmentManager, + success -> spamBlockingPromoHelper.showModifySettingOnCompleteSnackbar(rootView, success), + null /* onDissmissListener */); + } + } } diff --git a/java/com/android/dialer/spam/promo/SpamBlockingPromoDialogFragment.java b/java/com/android/dialer/spam/promo/SpamBlockingPromoDialogFragment.java index 0ea17d395..ea54728a6 100644 --- a/java/com/android/dialer/spam/promo/SpamBlockingPromoDialogFragment.java +++ b/java/com/android/dialer/spam/promo/SpamBlockingPromoDialogFragment.java @@ -19,7 +19,9 @@ package com.android.dialer.spam.promo; 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; /** Dialog for spam blocking on-boarding promotion. */ public class SpamBlockingPromoDialogFragment extends DialogFragment { @@ -29,16 +31,32 @@ public class SpamBlockingPromoDialogFragment extends DialogFragment { /** Called when dialog positive button is pressed. */ protected OnEnableListener positiveListener; - public static DialogFragment newInstance(OnEnableListener positiveListener) { + /** Called when the dialog is dismissed. */ + @Nullable protected DialogInterface.OnDismissListener dismissListener; + + public static DialogFragment newInstance( + OnEnableListener positiveListener, + @Nullable DialogInterface.OnDismissListener dismissListener) { SpamBlockingPromoDialogFragment fragment = new SpamBlockingPromoDialogFragment(); fragment.positiveListener = positiveListener; + fragment.dismissListener = dismissListener; return fragment; } + @Override + public void onDismiss(DialogInterface dialog) { + if (dismissListener != null) { + dismissListener.onDismiss(dialog); + } + super.onDismiss(dialog); + } + @Override public void onPause() { // The dialog is dismissed onPause, i.e. rotation. dismiss(); + dismissListener = null; + positiveListener = null; super.onPause(); } diff --git a/java/com/android/dialer/spam/promo/SpamBlockingPromoHelper.java b/java/com/android/dialer/spam/promo/SpamBlockingPromoHelper.java index 56143ce7f..b5bdd74b7 100644 --- a/java/com/android/dialer/spam/promo/SpamBlockingPromoHelper.java +++ b/java/com/android/dialer/spam/promo/SpamBlockingPromoHelper.java @@ -18,14 +18,16 @@ package com.android.dialer.spam.promo; import android.app.FragmentManager; import android.content.Context; +import android.content.DialogInterface.OnDismissListener; import android.preference.PreferenceManager; -import android.support.annotation.VisibleForTesting; import android.support.design.widget.Snackbar; import android.view.View; +import android.widget.Toast; import com.android.dialer.configprovider.ConfigProviderBindings; import com.android.dialer.logging.DialerImpression; import com.android.dialer.logging.Logger; import com.android.dialer.spam.SpamSettings; +import com.android.dialer.spam.SpamSettings.ModifySettingListener; /** Helper class for showing spam blocking on-boarding promotions. */ public class SpamBlockingPromoHelper { @@ -42,25 +44,6 @@ public class SpamBlockingPromoHelper { this.spamSettings = spamSettings; } - /** Shows a spam blocking promo dialog with on complete snackbar if all the prerequisites meet. */ - public void showSpamBlockingPromoDialog(View view, FragmentManager fragmentManager) { - if (!shouldShowSpamBlockingPromo()) { - return; - } - - updateLastShowSpamTimestamp(); - Logger.get(context).logImpression(DialerImpression.Type.SPAM_BLOCKING_CALL_LOG_PROMO_SHOWN); - SpamBlockingPromoDialogFragment.newInstance( - () -> { - Logger.get(context) - .logImpression( - DialerImpression.Type.SPAM_BLOCKING_ENABLED_THROUGH_CALL_LOG_PROMO); - spamSettings.modifySpamBlockingSetting( - true, success -> showModifySettingOnCompleteSnackbar(view, success)); - }) - .show(fragmentManager, SpamBlockingPromoDialogFragment.SPAM_BLOCKING_PROMO_DIALOG_TAG); - } - /** * Returns true if we should show a spam blocking promo. * @@ -70,8 +53,7 @@ public class SpamBlockingPromoHelper { * * @return true if we should show a spam blocking promo. */ - @VisibleForTesting - boolean shouldShowSpamBlockingPromo() { + public boolean shouldShowSpamBlockingPromo() { if (!ConfigProviderBindings.get(context).getBoolean(ENABLE_SPAM_BLOCKING_PROMO, false) || !spamSettings.isSpamEnabled() || !spamSettings.isSpamBlockingEnabledByFlag() @@ -88,6 +70,40 @@ public class SpamBlockingPromoHelper { return lastShowMillis == 0 || System.currentTimeMillis() - lastShowMillis > showPeriodMillis; } + /** + * Shows a spam blocking promo dialog. + * + * @param fragmentManager the fragment manager to show the dialog. + * @param modifySettingListener the listener called after spam blocking setting is modified. + * @param onDismissListener the listener called when the dialog is dismissed. + */ + public void showSpamBlockingPromoDialog( + FragmentManager fragmentManager, + ModifySettingListener modifySettingListener, + OnDismissListener onDismissListener) { + updateLastShowSpamTimestamp(); + Logger.get(context).logImpression(DialerImpression.Type.SPAM_BLOCKING_CALL_LOG_PROMO_SHOWN); + SpamBlockingPromoDialogFragment.newInstance( + () -> { + Logger.get(context) + .logImpression( + DialerImpression.Type.SPAM_BLOCKING_ENABLED_THROUGH_CALL_LOG_PROMO); + spamSettings.modifySpamBlockingSetting( + true, + success -> { + if (!success) { + Logger.get(context) + .logImpression( + DialerImpression.Type + .SPAM_BLOCKING_MODIFY_FAILURE_THROUGH_CALL_LOG_PROMO); + } + modifySettingListener.onComplete(success); + }); + }, + onDismissListener) + .show(fragmentManager, SpamBlockingPromoDialogFragment.SPAM_BLOCKING_PROMO_DIALOG_TAG); + } + private void updateLastShowSpamTimestamp() { PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()) .edit() @@ -96,16 +112,12 @@ public class SpamBlockingPromoHelper { } /** - * Shows a modify setting on complete snackbar and a link to redirect to setting page + * Shows a modify setting on complete snackbar and a link to redirect to setting page. * - * @param view the view to attach on-complete notice snackbar - * @param success whether the modify setting operation succceeds + * @param view the view to attach on-complete notice snackbar. + * @param success whether the modify setting operation succceeds. */ - private void showModifySettingOnCompleteSnackbar(View view, boolean success) { - if (!success) { - Logger.get(context) - .logImpression(DialerImpression.Type.SPAM_BLOCKING_MODIFY_FAILURE_THROUGH_CALL_LOG_PROMO); - } + public void showModifySettingOnCompleteSnackbar(View view, boolean success) { String snackBarText = success ? context.getString(R.string.spam_blocking_settings_enable_complete_text) @@ -118,4 +130,13 @@ public class SpamBlockingPromoHelper { context.getResources().getColor(R.color.dialer_snackbar_action_text_color)) .show(); } + + /** Shows a modify setting on complete toast message. */ + public void showModifySettingOnCompleteToast(boolean success) { + String toastText = + success + ? context.getString(R.string.spam_blocking_settings_enable_complete_text) + : context.getString(R.string.spam_blocking_settings_enable_error_text); + Toast.makeText(context, toastText, Toast.LENGTH_LONG).show(); + } } diff --git a/java/com/android/incallui/spam/SpamNotificationActivity.java b/java/com/android/incallui/spam/SpamNotificationActivity.java index cb5f8e514..c04a071a1 100644 --- a/java/com/android/incallui/spam/SpamNotificationActivity.java +++ b/java/com/android/incallui/spam/SpamNotificationActivity.java @@ -25,6 +25,7 @@ import android.content.Intent; import android.os.Bundle; import android.provider.CallLog; import android.provider.ContactsContract; +import android.support.annotation.Nullable; import android.support.v4.app.FragmentActivity; import android.telephony.PhoneNumberUtils; import com.android.dialer.blocking.BlockedNumbersMigrator; @@ -42,6 +43,7 @@ import com.android.dialer.logging.ReportingLocation; import com.android.dialer.notification.DialerNotificationManager; import com.android.dialer.phonenumberutil.PhoneNumberHelper; import com.android.dialer.spam.SpamComponent; +import com.android.dialer.spam.promo.SpamBlockingPromoHelper; import com.android.incallui.call.DialerCall; /** Creates the after call notification dialogs. */ @@ -58,6 +60,12 @@ public class SpamNotificationActivity extends FragmentActivity { static final String ACTION_MARK_NUMBER_AS_NOT_SPAM = "com.android.incallui.spam.ACTION_MARK_NUMBER_AS_NOT_SPAM"; + static final String ACTION_ENABLE_SPAM_BLOCKING = + "com.android.incallui.spam.ACTION_ENABLE_SPAM_BLOCKING"; + + static final String ACTION_SHOW_SPAM_BLOCKING_PROMO_DIALOG = + "com.android.incallui.spam.ACTION_SHOW_SPAM_BLOCKING_PROMO_DIALOG"; + private static final String TAG = "SpamNotifications"; private static final String EXTRA_NOTIFICATION_TAG = "notification_tag"; private static final String EXTRA_NOTIFICATION_ID = "notification_id"; @@ -85,7 +93,11 @@ public class SpamNotificationActivity extends FragmentActivity { * @return Intent intent that starts this activity. */ public static Intent createActivityIntent( - Context context, DialerCall call, String action, String notificationTag, int notificationId) { + Context context, + @Nullable DialerCall call, + String action, + String notificationTag, + int notificationId) { Intent intent = new Intent(context, SpamNotificationActivity.class); intent.setAction(action); // This ensures only one activity of this kind exists at a time. @@ -93,7 +105,10 @@ public class SpamNotificationActivity extends FragmentActivity { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra(EXTRA_NOTIFICATION_TAG, notificationTag); intent.putExtra(EXTRA_NOTIFICATION_ID, notificationId); - intent.putExtra(EXTRA_CALL_INFO, newCallInfoBundle(call)); + + if (call != null) { + intent.putExtra(EXTRA_CALL_INFO, newCallInfoBundle(call)); + } return intent; } @@ -179,6 +194,9 @@ public class SpamNotificationActivity extends FragmentActivity { showNonSpamDialog(); } break; + case ACTION_SHOW_SPAM_BLOCKING_PROMO_DIALOG: + showSpamBlockingPromoDialog(); + break; default: // fall out } } @@ -230,8 +248,7 @@ public class SpamNotificationActivity extends FragmentActivity { new BlockReportSpamDialogs.OnSpamDialogClickListener() { @Override public void onClick(boolean isSpamChecked) { - blockReportNumberAndFinish( - number, isSpamChecked, contactLookupResultType); + blockReportNumber(number, isSpamChecked, contactLookupResultType); } }, dismissListener) @@ -239,7 +256,7 @@ public class SpamNotificationActivity extends FragmentActivity { } }); } else { - blockReportNumberAndFinish(number, true, contactLookupResultType); + blockReportNumber(number, true, contactLookupResultType); } } @@ -271,7 +288,7 @@ public class SpamNotificationActivity extends FragmentActivity { } /** Block and report the number as spam. */ - private void blockReportNumberAndFinish( + private void blockReportNumber( String number, boolean reportAsSpam, ContactLookupResult.Type contactLookupResultType) { if (reportAsSpam) { logCallImpression(DialerImpression.Type.SPAM_AFTER_CALL_NOTIFICATION_MARKED_NUMBER_AS_SPAM); @@ -287,8 +304,6 @@ public class SpamNotificationActivity extends FragmentActivity { logCallImpression(DialerImpression.Type.SPAM_AFTER_CALL_NOTIFICATION_BLOCK_NUMBER); filteredNumberAsyncQueryHandler.blockNumber(null, number, getCountryIso()); - // TODO: DialerCall finish() after block/reporting async tasks complete (a bug) - finish(); } /** Report the number as not spam. */ @@ -326,7 +341,9 @@ public class SpamNotificationActivity extends FragmentActivity { } private Bundle getCallInfo() { - return getIntent().getBundleExtra(EXTRA_CALL_INFO); + return getIntent().hasExtra(EXTRA_CALL_INFO) + ? getIntent().getBundleExtra(EXTRA_CALL_INFO) + : new Bundle(); } /** Dialog that displays "Not spam", "Block/report spam" and "Dismiss". */ @@ -400,6 +417,7 @@ public class SpamNotificationActivity extends FragmentActivity { dismiss(); spamNotificationActivity.maybeShowBlockReportSpamDialog( number, contactLookupResultType); + spamNotificationActivity.showSpamBlockingPromoDialog(); } }) .setNegativeButton( @@ -502,4 +520,18 @@ public class SpamNotificationActivity extends FragmentActivity { .create(); } } + + private void showSpamBlockingPromoDialog() { + SpamBlockingPromoHelper spamBlockingPromoHelper = + new SpamBlockingPromoHelper( + getApplicationContext(), SpamComponent.get(this).spamSettings()); + if (!spamBlockingPromoHelper.shouldShowSpamBlockingPromo()) { + finish(); + } else { + spamBlockingPromoHelper.showSpamBlockingPromoDialog( + getFragmentManager(), + success -> spamBlockingPromoHelper.showModifySettingOnCompleteToast(success), + dialog -> finish()); + } + } } -- cgit v1.2.3