Skip to content

Commit

Permalink
Update and centralize block strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Apr 21, 2020
1 parent 6d6e017 commit b3d57ed
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 110 deletions.
157 changes: 98 additions & 59 deletions 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;

Expand All @@ -14,76 +16,113 @@
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 showBlockFor(@NonNull Context context,
@NonNull Lifecycle lifecycle,
@NonNull Recipient recipient,
@NonNull Runnable onBlock)
{
SimpleTask.run(lifecycle,
() -> buildBlockFor(context, recipient, onBlock, null),
AlertDialog.Builder::show);
}

public static void handleBlock(@NonNull Context context,
@NonNull Lifecycle lifecycle,
@NonNull RecipientId recipientId)
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()) {
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, onBlockAndDelete),
AlertDialog.Builder::show);
}

public static void handleUnblock(@NonNull Context context,
@NonNull Lifecycle lifecycle,
@NonNull RecipientId recipientId,
@Nullable Runnable postUnblock)
public static void showUnblockFor(@NonNull Context context,
@NonNull Lifecycle lifecycle,
@NonNull Recipient recipient,
@NonNull Runnable onUnblock)
{
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,
() -> buildUnblockFor(context, recipient, onUnblock),
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);
@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;
}
}
Expand Up @@ -25,6 +25,7 @@
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 {
Expand Down Expand Up @@ -106,10 +107,10 @@ public void onLoaderReset(@NonNull Loader<Cursor> loader) {
@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 {
Expand Down
Expand Up @@ -688,8 +688,13 @@ public boolean onPreferenceClick(Preference preference) {
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;
}
Expand Down
Expand Up @@ -80,6 +80,7 @@
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;
Expand Down Expand Up @@ -1680,7 +1681,7 @@ private void initializeViews() {
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 {
Expand Down Expand Up @@ -2940,20 +2941,7 @@ private void onMessageRequestBlockClicked(@NonNull MessageRequestViewModel reque
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) {
Expand All @@ -2963,18 +2951,7 @@ private void onMessageRequestUnblockClicked(@NonNull MessageRequestViewModel req
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) {
Expand Down
32 changes: 14 additions & 18 deletions app/src/main/res/values/strings.xml
Expand Up @@ -93,6 +93,20 @@
<!-- BlockedContactsActivity -->
<string name="BlockedContactsActivity_blocked_contacts">Blocked contacts</string>

<!-- BlockUnblockDialog -->
<string name="BlockUnblockDialog_block_and_leave_s">Block and leave %1$s?</string>
<string name="BlockUnblockDialog_block_s">Block %1$s?</string>
<string name="BlockUnblockDialog_you_will_no_longer_receive_messages_or_updates">You will no longer receive messages or updates from this group, and members won\'t be able to add you to this group again.</string>
<string name="BlockUnblockDialog_group_members_wont_be_able_to_add_you">Group members won\'t be able to add you to this group again.</string>
<string name="BlockUnblockDialog_group_members_will_be_able_to_add_you">Group members will be able to add you to this group again.</string>
<string name="BlockUnblockDialog_you_will_be_able_to_call_and_message_each_other">You will be able to message and call each other.</string>
<string name="BlockUnblockDialog_blocked_people_wont_be_able_to_call_you_or_send_you_messages">Blocked people won\'t be able to call you or send you messages.</string>
<string name="BlockUnblockDialog_unblock_s">Unblock %1$s?</string>
<string name="BlockUnblockDialog_unblock">Unblock</string>
<string name="BlockUnblockDialog_block">Block</string>
<string name="BlockUnblockDialog_block_and_leave">Block and Leave</string>
<string name="BlockUnblockDialog_block_and_delete">Block and Delete</string>

<!-- BucketedThreadMedia -->
<string name="BucketedThreadMedia_Today">Today</string>
<string name="BucketedThreadMedia_Yesterday">Yesterday</string>
Expand Down Expand Up @@ -243,15 +257,6 @@
<string name="ConversationActivity_sticker_pack_installed">Sticker pack installed</string>
<string name="ConversationActivity_new_say_it_with_stickers">New! Say it with stickers</string>

<string name="ConversationActivity_block_s">Block %1$s?</string>
<string name="ConversationActivity_block_and_leave_s">Block and leave %1$s?</string>
<string name="ConversationActivity_unblock_s">Unblock %1$s?</string>
<string name="ConversationActivity_you_will_be_able_to_message_and_call_each_other">You will be able to message and call each other.</string>
<string name="ConversationActivity_group_members_will_be_able_to_add_you_to_this_group_again">Group members will be able to add you to this group again.</string>
<string name="ConversationActivity_blocked_people_will_not_be_able_to_call_you_or_send_you_messages">Blocked people will not be able to call you or send you messages.</string>
<string name="ConversationActivity_you_will_leave_this_group_and_no_longer_receive_messages_or_updates">You will leave this group and no longer receive messages or updates.</string>
<string name="ConversationActivity_block">Block</string>
<string name="ConversationActivity_block_and_delete">Block and delete</string>
<string name="ConversationActivity_cancel">Cancel</string>
<string name="ConversationActivity_delete_conversation">Delete conversation?</string>
<string name="ConversationActivity_delete_and_leave_group">Delete and leave group?</string>
Expand Down Expand Up @@ -836,16 +841,7 @@
<string name="ReactionsRecipientAdapter_you">You</string>

<!-- RecipientPreferencesActivity -->
<string name="RecipientPreferenceActivity_block_this_contact_question">Block this contact?</string>
<string name="RecipientPreferenceActivity_you_will_no_longer_receive_messages_and_calls_from_this_contact">You will no longer receive messages and calls from this contact.</string>
<string name="RecipientPreferenceActivity_block_and_leave_group">Block and leave this group?</string>
<string name="RecipientPreferenceActivity_block_group">Block this group?</string>
<string name="RecipientPreferenceActivity_block_and_leave_group_description">You will no longer receive messages or updates from this group.</string>
<string name="RecipientPreferenceActivity_block">Block</string>
<string name="RecipientPreferenceActivity_unblock_this_contact_question">Unblock this contact?</string>
<string name="RecipientPreferenceActivity_you_will_once_again_be_able_to_receive_messages_and_calls_from_this_contact">You will once again be able to receive messages and calls from this contact.</string>
<string name="RecipientPreferenceActivity_unblock_this_group_question">Unblock this group?</string>
<string name="RecipientPreferenceActivity_unblock_this_group_description">Existing members will be able to add you to the group again.</string>
<string name="RecipientPreferenceActivity_error_leaving_group">Error leaving group</string>
<string name="RecipientPreferenceActivity_unblock">Unblock</string>
<string name="RecipientPreferenceActivity_enabled">Enabled</string>
Expand Down

0 comments on commit b3d57ed

Please sign in to comment.