diff options
Diffstat (limited to 'java/com/android/dialer/blockreportspam/ShowBlockReportSpamDialogReceiver.java')
-rw-r--r-- | java/com/android/dialer/blockreportspam/ShowBlockReportSpamDialogReceiver.java | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/java/com/android/dialer/blockreportspam/ShowBlockReportSpamDialogReceiver.java b/java/com/android/dialer/blockreportspam/ShowBlockReportSpamDialogReceiver.java index 6b8f81908..02aa9943b 100644 --- a/java/com/android/dialer/blockreportspam/ShowBlockReportSpamDialogReceiver.java +++ b/java/com/android/dialer/blockreportspam/ShowBlockReportSpamDialogReceiver.java @@ -21,19 +21,24 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.support.annotation.Nullable; import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler; import com.android.dialer.blockreportspam.BlockReportSpamDialogs.DialogFragmentForBlockingNumber; import com.android.dialer.blockreportspam.BlockReportSpamDialogs.DialogFragmentForBlockingNumberAndOptionallyReportingAsSpam; import com.android.dialer.blockreportspam.BlockReportSpamDialogs.DialogFragmentForReportingNotSpam; +import com.android.dialer.blockreportspam.BlockReportSpamDialogs.DialogFragmentForUnblockingNumber; import com.android.dialer.blockreportspam.BlockReportSpamDialogs.OnConfirmListener; import com.android.dialer.blockreportspam.BlockReportSpamDialogs.OnSpamDialogClickListener; import com.android.dialer.common.Assert; import com.android.dialer.common.LogUtil; +import com.android.dialer.common.concurrent.DialerExecutor.Worker; +import com.android.dialer.common.concurrent.DialerExecutorComponent; import com.android.dialer.logging.DialerImpression; import com.android.dialer.logging.Logger; import com.android.dialer.protos.ProtoParsers; import com.android.dialer.spam.Spam; import com.android.dialer.spam.SpamComponent; +import com.google.auto.value.AutoValue; /** * A {@link BroadcastReceiver} that shows an appropriate dialog upon receiving notifications from @@ -45,6 +50,7 @@ public final class ShowBlockReportSpamDialogReceiver extends BroadcastReceiver { static final String ACTION_SHOW_DIALOG_TO_BLOCK_NUMBER_AND_OPTIONALLY_REPORT_SPAM = "show_dialog_to_block_number_and_optionally_report_spam"; static final String ACTION_SHOW_DIALOG_TO_REPORT_NOT_SPAM = "show_dialog_to_report_not_spam"; + static final String ACTION_SHOW_DIALOG_TO_UNBLOCK_NUMBER = "show_dialog_to_unblock_number"; static final String EXTRA_DIALOG_INFO = "dialog_info"; /** {@link FragmentManager} needed to show a {@link android.app.DialogFragment}. */ @@ -56,6 +62,7 @@ public final class ShowBlockReportSpamDialogReceiver extends BroadcastReceiver { intentFilter.addAction(ACTION_SHOW_DIALOG_TO_BLOCK_NUMBER_AND_OPTIONALLY_REPORT_SPAM); intentFilter.addAction(ACTION_SHOW_DIALOG_TO_BLOCK_NUMBER); intentFilter.addAction(ACTION_SHOW_DIALOG_TO_REPORT_NOT_SPAM); + intentFilter.addAction(ACTION_SHOW_DIALOG_TO_UNBLOCK_NUMBER); return intentFilter; } @@ -79,6 +86,9 @@ public final class ShowBlockReportSpamDialogReceiver extends BroadcastReceiver { case ACTION_SHOW_DIALOG_TO_REPORT_NOT_SPAM: showDialogToReportNotSpam(context, intent); break; + case ACTION_SHOW_DIALOG_TO_UNBLOCK_NUMBER: + showDialogToUnblockNumber(context, intent); + break; default: throw new IllegalStateException("Unsupported action: " + action); } @@ -197,4 +207,103 @@ public final class ShowBlockReportSpamDialogReceiver extends BroadcastReceiver { dialogInfo.getNormalizedNumber(), onConfirmListener, /* dismissListener = */ null) .show(fragmentManager, BlockReportSpamDialogs.NOT_SPAM_DIALOG_TAG); } + + private void showDialogToUnblockNumber(Context context, Intent intent) { + LogUtil.enterBlock("ShowBlockReportSpamDialogReceiver.showDialogToUnblockNumber"); + + Assert.checkArgument(intent.hasExtra(EXTRA_DIALOG_INFO)); + BlockReportSpamDialogInfo dialogInfo = + ProtoParsers.getTrusted( + intent, EXTRA_DIALOG_INFO, BlockReportSpamDialogInfo.getDefaultInstance()); + + FilteredNumberAsyncQueryHandler filteredNumberAsyncQueryHandler = + new FilteredNumberAsyncQueryHandler(context); + + // Set up the positive listener for the dialog. + OnConfirmListener onConfirmListener = + () -> { + LogUtil.i("ShowBlockReportSpamDialogReceiver.showDialogToUnblockNumber", "confirmed"); + + DialerExecutorComponent.get(context) + .dialerExecutorFactory() + .createNonUiTaskBuilder( + new GetIdForBlockedNumberWorker(filteredNumberAsyncQueryHandler)) + .onSuccess( + idForBlockedNumber -> { + LogUtil.i( + "ShowBlockReportSpamDialogReceiver.showDialogToUnblockNumber", + "ID for the blocked number retrieved"); + if (idForBlockedNumber == null) { + throw new IllegalStateException("ID for a blocked number is null."); + } + + LogUtil.i( + "ShowBlockReportSpamDialogReceiver.showDialogToUnblockNumber", + "unblocking number"); + filteredNumberAsyncQueryHandler.unblock( + (rows, values) -> + Logger.get(context) + .logImpression(DialerImpression.Type.USER_ACTION_UNBLOCKED_NUMBER), + idForBlockedNumber); + }) + .onFailure( + throwable -> { + throw new RuntimeException(throwable); + }) + .build() + .executeSerial( + NumberInfo.newBuilder() + .setNormalizedNumber(dialogInfo.getNormalizedNumber()) + .setCountryIso(dialogInfo.getCountryIso()) + .build()); + }; + + // Create & show the dialog. + DialogFragmentForUnblockingNumber.newInstance( + dialogInfo.getNormalizedNumber(), onConfirmListener, /* dismissListener = */ null) + .show(fragmentManager, BlockReportSpamDialogs.UNBLOCK_DIALOG_TAG); + } + + /** A {@link Worker} that retrieves the ID of a blocked number from the database. */ + private static final class GetIdForBlockedNumberWorker implements Worker<NumberInfo, Integer> { + + private final FilteredNumberAsyncQueryHandler filteredNumberAsyncQueryHandler; + + GetIdForBlockedNumberWorker(FilteredNumberAsyncQueryHandler filteredNumberAsyncQueryHandler) { + this.filteredNumberAsyncQueryHandler = filteredNumberAsyncQueryHandler; + } + + @Nullable + @Override + public Integer doInBackground(NumberInfo input) throws Throwable { + LogUtil.enterBlock("GetIdForBlockedNumberWorker.doInBackground"); + + return filteredNumberAsyncQueryHandler.getBlockedIdSynchronous( + input.getNormalizedNumber(), input.getCountryIso()); + } + } + + /** + * Contains information about a number and serves as the input to {@link + * GetIdForBlockedNumberWorker}. + */ + @AutoValue + abstract static class NumberInfo { + static Builder newBuilder() { + return new AutoValue_ShowBlockReportSpamDialogReceiver_NumberInfo.Builder(); + } + + abstract String getNormalizedNumber(); + + abstract String getCountryIso(); + + @AutoValue.Builder + abstract static class Builder { + abstract Builder setNormalizedNumber(String normalizedNumber); + + abstract Builder setCountryIso(String countryIso); + + abstract NumberInfo build(); + } + } } |