From b3d57edb24bc3a76d020df22501eced735776eeb Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Thu, 16 Apr 2020 11:30:51 -0400 Subject: [PATCH] Update and centralize block strings. --- .../securesms/BlockUnblockDialog.java | 159 +++++++++++------- .../securesms/BlockedContactsActivity.java | 9 +- .../RecipientPreferenceActivity.java | 9 +- .../conversation/ConversationActivity.java | 31 +--- app/src/main/res/values/strings.xml | 32 ++-- 5 files changed, 129 insertions(+), 111 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/BlockUnblockDialog.java b/app/src/main/java/org/thoughtcrime/securesms/BlockUnblockDialog.java index 4b14463440..d0eaf699f7 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/BlockUnblockDialog.java +++ b/app/src/main/java/org/thoughtcrime/securesms/BlockUnblockDialog.java @@ -1,9 +1,11 @@ package org.thoughtcrime.securesms; import android.content.Context; +import android.content.res.Resources; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.annotation.WorkerThread; import androidx.appcompat.app.AlertDialog; import androidx.lifecycle.Lifecycle; @@ -14,76 +16,113 @@ import org.thoughtcrime.securesms.recipients.RecipientUtil; import org.thoughtcrime.securesms.util.concurrent.SignalExecutors; import org.thoughtcrime.securesms.util.concurrent.SimpleTask; +/** + * This should be used whenever we want to prompt the user to block/unblock a recipient. + */ public final class BlockUnblockDialog { - private BlockUnblockDialog() { - } + private BlockUnblockDialog() { } - public static void handleBlock(@NonNull Context context, - @NonNull Lifecycle lifecycle, - @NonNull RecipientId recipientId) + public static void showBlockFor(@NonNull Context context, + @NonNull Lifecycle lifecycle, + @NonNull Recipient recipient, + @NonNull Runnable onBlock) { - SimpleTask.run( - lifecycle, - () -> { - AlertDialog.Builder builder = new AlertDialog.Builder(context); - Recipient resolved = Recipient.resolved(recipientId); - - if (resolved.isGroup()) { - if (DatabaseFactory.getGroupDatabase(context).isActive(resolved.requireGroupId())) { - builder.setTitle(R.string.RecipientPreferenceActivity_block_and_leave_group); - } else { - builder.setTitle(R.string.RecipientPreferenceActivity_block_group); - } - builder.setMessage(R.string.RecipientPreferenceActivity_block_and_leave_group_description); - } else { - builder.setTitle(R.string.RecipientPreferenceActivity_block_this_contact_question) - .setMessage(R.string.RecipientPreferenceActivity_you_will_no_longer_receive_messages_and_calls_from_this_contact); - } - - return builder.setCancelable(true) - .setNegativeButton(android.R.string.cancel, null) - .setPositiveButton(R.string.RecipientPreferenceActivity_block, (dialog, which) -> setBlocked(context, resolved, true)); - }, - AlertDialog.Builder::show); + SimpleTask.run(lifecycle, + () -> buildBlockFor(context, recipient, onBlock, null), + AlertDialog.Builder::show); } - public static void handleUnblock(@NonNull Context context, - @NonNull Lifecycle lifecycle, - @NonNull RecipientId recipientId, - @Nullable Runnable postUnblock) + public static void showBlockAndDeleteFor(@NonNull Context context, + @NonNull Lifecycle lifecycle, + @NonNull Recipient recipient, + @NonNull Runnable onBlock, + @NonNull Runnable onBlockAndDelete) { - SimpleTask.run( - lifecycle, - () -> { - AlertDialog.Builder builder = new AlertDialog.Builder(context); - Recipient resolved = Recipient.resolved(recipientId); - - if (resolved.isGroup()) { - builder.setTitle(R.string.RecipientPreferenceActivity_unblock_this_group_question) - .setMessage(R.string.RecipientPreferenceActivity_unblock_this_group_description); - } else { - builder.setTitle(R.string.RecipientPreferenceActivity_unblock_this_contact_question) - .setMessage(R.string.RecipientPreferenceActivity_you_will_once_again_be_able_to_receive_messages_and_calls_from_this_contact); - } - - return builder.setCancelable(true) - .setNegativeButton(android.R.string.cancel, null) - .setPositiveButton(R.string.RecipientPreferenceActivity_unblock, (dialog, which) -> { - setBlocked(context, resolved, false); - if (postUnblock != null) postUnblock.run(); - }); - }, - AlertDialog.Builder::show); + SimpleTask.run(lifecycle, + () -> buildBlockFor(context, recipient, onBlock, onBlockAndDelete), + AlertDialog.Builder::show); } - private static void setBlocked(@NonNull final Context context, final Recipient recipient, final boolean blocked) { - SignalExecutors.BOUNDED.execute(() -> { - if (blocked) { - RecipientUtil.block(context, recipient); + public static void showUnblockFor(@NonNull Context context, + @NonNull Lifecycle lifecycle, + @NonNull Recipient recipient, + @NonNull Runnable onUnblock) + { + SimpleTask.run(lifecycle, + () -> buildUnblockFor(context, recipient, onUnblock), + AlertDialog.Builder::show); + } + + @WorkerThread + private static AlertDialog.Builder buildBlockFor(@NonNull Context context, + @NonNull Recipient recipient, + @NonNull Runnable onBlock, + @Nullable Runnable onBlockAndDelete) + { + recipient = recipient.resolve(); + + AlertDialog.Builder builder = new AlertDialog.Builder(context); + Resources resources = context.getResources(); + + if (recipient.isGroup()) { + if (DatabaseFactory.getGroupDatabase(context).isActive(recipient.requireGroupId())) { + builder.setTitle(resources.getString(R.string.BlockUnblockDialog_block_and_leave_s, recipient.getDisplayName(context))); + builder.setMessage(R.string.BlockUnblockDialog_you_will_no_longer_receive_messages_or_updates); + builder.setPositiveButton(R.string.BlockUnblockDialog_block_and_leave, ((dialog, which) -> onBlock.run())); + builder.setNegativeButton(android.R.string.cancel, null); } else { - RecipientUtil.unblock(context, recipient); + builder.setTitle(resources.getString(R.string.BlockUnblockDialog_block_s, recipient.getDisplayName(context))); + builder.setMessage(R.string.BlockUnblockDialog_group_members_wont_be_able_to_add_you); + builder.setPositiveButton(R.string.RecipientPreferenceActivity_block, ((dialog, which) -> onBlock.run())); + builder.setNegativeButton(android.R.string.cancel, null); } - }); + } else { + builder.setTitle(resources.getString(R.string.BlockUnblockDialog_block_s, recipient.getDisplayName(context))); + builder.setMessage(R.string.BlockUnblockDialog_blocked_people_wont_be_able_to_call_you_or_send_you_messages); + + if (onBlockAndDelete != null) { + builder.setNeutralButton(android.R.string.cancel, null); + builder.setPositiveButton(R.string.BlockUnblockDialog_block_and_delete, (d, w) -> onBlockAndDelete.run()); + builder.setNegativeButton(R.string.BlockUnblockDialog_block, (d, w) -> onBlock.run()); + } else { + builder.setPositiveButton(R.string.BlockUnblockDialog_block, ((dialog, which) -> onBlock.run())); + builder.setNegativeButton(android.R.string.cancel, null); + } + } + + return builder; + } + + @WorkerThread + private static AlertDialog.Builder buildUnblockFor(@NonNull Context context, + @NonNull Recipient recipient, + @NonNull Runnable onUnblock) + { + recipient = recipient.resolve(); + + AlertDialog.Builder builder = new AlertDialog.Builder(context); + Resources resources = context.getResources(); + + if (recipient.isGroup()) { + if (DatabaseFactory.getGroupDatabase(context).isActive(recipient.requireGroupId())) { + builder.setTitle(resources.getString(R.string.BlockUnblockDialog_unblock_s, recipient.getDisplayName(context))); + builder.setMessage(R.string.BlockUnblockDialog_group_members_will_be_able_to_add_you); + builder.setPositiveButton(R.string.RecipientPreferenceActivity_unblock, ((dialog, which) -> onUnblock.run())); + builder.setNegativeButton(android.R.string.cancel, null); + } else { + builder.setTitle(resources.getString(R.string.BlockUnblockDialog_unblock_s, recipient.getDisplayName(context))); + builder.setMessage(R.string.BlockUnblockDialog_group_members_will_be_able_to_add_you); + builder.setPositiveButton(R.string.RecipientPreferenceActivity_unblock, ((dialog, which) -> onUnblock.run())); + builder.setNegativeButton(android.R.string.cancel, null); + } + } else { + builder.setTitle(resources.getString(R.string.BlockUnblockDialog_unblock_s, recipient.getDisplayName(context))); + builder.setMessage(R.string.BlockUnblockDialog_you_will_be_able_to_call_and_message_each_other); + builder.setPositiveButton(R.string.RecipientPreferenceActivity_unblock, ((dialog, which) -> onUnblock.run())); + builder.setNegativeButton(android.R.string.cancel, null); + } + + return builder; } } diff --git a/app/src/main/java/org/thoughtcrime/securesms/BlockedContactsActivity.java b/app/src/main/java/org/thoughtcrime/securesms/BlockedContactsActivity.java index 0adbf798ff..a9e3a37d2d 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/BlockedContactsActivity.java +++ b/app/src/main/java/org/thoughtcrime/securesms/BlockedContactsActivity.java @@ -25,6 +25,7 @@ import org.thoughtcrime.securesms.preferences.BlockedContactListItem; import org.thoughtcrime.securesms.recipients.LiveRecipient; import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.recipients.RecipientId; +import org.thoughtcrime.securesms.recipients.RecipientUtil; import org.thoughtcrime.securesms.util.DynamicTheme; public class BlockedContactsActivity extends PassphraseRequiredActionBarActivity { @@ -106,10 +107,10 @@ public class BlockedContactsActivity extends PassphraseRequiredActionBarActivity @Override public void onItemClick(AdapterView parent, View view, int position, long id) { Recipient recipient = ((BlockedContactListItem)view).getRecipient(); - BlockUnblockDialog.handleUnblock(requireContext(), - getLifecycle(), - recipient.getId(), - () -> LoaderManager.getInstance(this).restartLoader(0, null, this)); + BlockUnblockDialog.showUnblockFor(requireContext(), getLifecycle(), recipient, () -> { + RecipientUtil.unblock(requireContext(), recipient); + LoaderManager.getInstance(this).restartLoader(0, null, this); + }); } private static class BlockedContactAdapter extends CursorAdapter { diff --git a/app/src/main/java/org/thoughtcrime/securesms/RecipientPreferenceActivity.java b/app/src/main/java/org/thoughtcrime/securesms/RecipientPreferenceActivity.java index 91cdf47052..35ae4cd578 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/RecipientPreferenceActivity.java +++ b/app/src/main/java/org/thoughtcrime/securesms/RecipientPreferenceActivity.java @@ -688,8 +688,13 @@ public class RecipientPreferenceActivity extends PassphraseRequiredActionBarActi private class BlockClickedListener implements Preference.OnPreferenceClickListener { @Override public boolean onPreferenceClick(Preference preference) { - if (recipient.get().isBlocked()) BlockUnblockDialog.handleUnblock(preference.getContext(), getLifecycle(), recipient.getId(), null); - else BlockUnblockDialog.handleBlock(preference.getContext(), getLifecycle(), recipient.getId()); + Context context = preference.getContext(); + + if (recipient.get().isBlocked()) { + BlockUnblockDialog.showUnblockFor(context, getLifecycle(), recipient.get(), () -> RecipientUtil.unblock(context, recipient.get())); + } else { + BlockUnblockDialog.showBlockFor(context, getLifecycle(), recipient.get(), () -> RecipientUtil.block(context, recipient.get())); + } return true; } diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationActivity.java b/app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationActivity.java index 657b46977b..e59dc05408 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationActivity.java +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationActivity.java @@ -80,6 +80,7 @@ import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import org.thoughtcrime.securesms.ApplicationContext; +import org.thoughtcrime.securesms.BlockUnblockDialog; import org.thoughtcrime.securesms.ExpirationDialog; import org.thoughtcrime.securesms.GroupCreateActivity; import org.thoughtcrime.securesms.GroupMembersDialog; @@ -1680,7 +1681,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity composeText.setOnClickListener(composeKeyPressedListener); composeText.setOnFocusChangeListener(composeKeyPressedListener); - if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA) && Camera.getNumberOfCameras() > 0) { + if (Camera.getNumberOfCameras() > 0) { quickCameraToggle.setVisibility(View.VISIBLE); quickCameraToggle.setOnClickListener(new QuickCameraToggleListener()); } else { @@ -2940,20 +2941,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity return; } - AlertDialog.Builder builder = new AlertDialog.Builder(this) - .setNeutralButton(R.string.ConversationActivity_cancel, (d, w) -> d.dismiss()) - .setPositiveButton(R.string.ConversationActivity_block_and_delete, (d, w) -> requestModel.onBlockAndDelete()) - .setNegativeButton(R.string.ConversationActivity_block, (d, w) -> requestModel.onBlock()); - - if (recipient.isGroup()) { - builder.setTitle(getString(R.string.ConversationActivity_block_and_leave_s, recipient.getDisplayName(this))); - builder.setMessage(R.string.ConversationActivity_you_will_leave_this_group_and_no_longer_receive_messages_or_updates); - } else { - builder.setTitle(getString(R.string.ConversationActivity_block_s, recipient.getDisplayName(this))); - builder.setMessage(R.string.ConversationActivity_blocked_people_will_not_be_able_to_call_you_or_send_you_messages); - } - - builder.show(); + BlockUnblockDialog.showBlockAndDeleteFor(this, getLifecycle(), recipient, requestModel::onBlock, requestModel::onBlockAndDelete); } private void onMessageRequestUnblockClicked(@NonNull MessageRequestViewModel requestModel) { @@ -2963,18 +2951,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity return; } - AlertDialog.Builder builder = new AlertDialog.Builder(this) - .setTitle(getString(R.string.ConversationActivity_unblock_s, recipient.getDisplayName(this))) - .setNeutralButton(R.string.ConversationActivity_cancel, (d, w) -> d.dismiss()) - .setNegativeButton(R.string.ConversationActivity_unblock, (d, w) -> requestModel.onUnblock()); - - if (recipient.isGroup()) { - builder.setMessage(R.string.ConversationActivity_group_members_will_be_able_to_add_you_to_this_group_again); - } else { - builder.setMessage(R.string.ConversationActivity_you_will_be_able_to_message_and_call_each_other); - } - - builder.show(); + BlockUnblockDialog.showUnblockFor(this, getLifecycle(), recipient, requestModel::onUnblock); } private void presentMessageRequestDisplayState(@NonNull MessageRequestViewModel.DisplayState displayState) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 71402358c6..4f1f6500a3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -93,6 +93,20 @@ Blocked contacts + + Block and leave %1$s? + Block %1$s? + You will no longer receive messages or updates from this group, and members won\'t be able to add you to this group again. + Group members won\'t be able to add you to this group again. + Group members will be able to add you to this group again. + You will be able to message and call each other. + Blocked people won\'t be able to call you or send you messages. + Unblock %1$s? + Unblock + Block + Block and Leave + Block and Delete + Today Yesterday @@ -243,15 +257,6 @@ Sticker pack installed New! Say it with stickers - Block %1$s? - Block and leave %1$s? - Unblock %1$s? - You will be able to message and call each other. - Group members will be able to add you to this group again. - Blocked people will not be able to call you or send you messages. - You will leave this group and no longer receive messages or updates. - Block - Block and delete Cancel Delete conversation? Delete and leave group? @@ -836,16 +841,7 @@ You - Block this contact? - You will no longer receive messages and calls from this contact. - Block and leave this group? - Block this group? - You will no longer receive messages or updates from this group. Block - Unblock this contact? - You will once again be able to receive messages and calls from this contact. - Unblock this group? - Existing members will be able to add you to the group again. Error leaving group Unblock Enabled