anotherim/src/main/java/eu/siacs/conversations/ui/BlockContactDialog.java

64 lines
3 KiB
Java
Raw Normal View History

package eu.siacs.conversations.ui;
2018-03-29 13:08:43 +00:00
import android.databinding.DataBindingUtil;
2018-02-16 12:29:38 +00:00
import android.support.v7.app.AlertDialog;
2016-09-20 12:21:41 +00:00
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TypefaceSpan;
import android.view.View;
import android.widget.Toast;
import eu.siacs.conversations.R;
2018-03-29 13:08:43 +00:00
import eu.siacs.conversations.databinding.DialogBlockContactBinding;
import eu.siacs.conversations.entities.Blockable;
import eu.siacs.conversations.entities.Conversation;
2018-03-05 17:30:40 +00:00
import rocks.xmpp.addr.Jid;
public final class BlockContactDialog {
public static void show(final XmppActivity xmppActivity, final Blockable blockable) {
final AlertDialog.Builder builder = new AlertDialog.Builder(xmppActivity);
final boolean isBlocked = blockable.isBlocked();
builder.setNegativeButton(R.string.cancel, null);
2018-03-29 13:08:43 +00:00
DialogBlockContactBinding binding = DataBindingUtil.inflate(xmppActivity.getLayoutInflater(), R.layout.dialog_block_contact, null, false);
final boolean reporting = blockable.getAccount().getXmppConnection().getFeatures().spamReporting();
2018-03-29 13:08:43 +00:00
binding.reportSpam.setVisibility(!isBlocked && reporting ? View.VISIBLE : View.GONE);
builder.setView(binding.getRoot());
2016-09-20 12:21:41 +00:00
String value;
SpannableString spannable;
2018-03-05 17:30:40 +00:00
if (blockable.getJid().getLocal() == null || blockable.getAccount().isBlocked(Jid.ofDomain(blockable.getJid().getDomain()))) {
builder.setTitle(isBlocked ? R.string.action_unblock_domain : R.string.action_block_domain);
2018-03-05 17:30:40 +00:00
value = Jid.ofDomain(blockable.getJid().getDomain()).toString();
spannable = new SpannableString(xmppActivity.getString(isBlocked ? R.string.unblock_domain_text : R.string.block_domain_text, value));
} else {
int resBlockAction = blockable instanceof Conversation && ((Conversation) blockable).isWithStranger() ? R.string.block_stranger : R.string.action_block_contact;
builder.setTitle(isBlocked ? R.string.action_unblock_contact : resBlockAction);
2018-03-05 17:30:40 +00:00
value = blockable.getJid().asBareJid().toString();
spannable = new SpannableString(xmppActivity.getString(isBlocked ? R.string.unblock_contact_text : R.string.block_contact_text, value));
}
2016-09-20 12:21:41 +00:00
int start = spannable.toString().indexOf(value);
if (start >= 0) {
2018-03-29 13:08:43 +00:00
spannable.setSpan(new TypefaceSpan("monospace"), start, start + value.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
2016-09-20 12:21:41 +00:00
}
2018-03-29 13:08:43 +00:00
binding.text.setText(spannable);
builder.setPositiveButton(isBlocked ? R.string.unblock : R.string.block, (dialog, which) -> {
if (isBlocked) {
xmppActivity.xmppConnectionService.sendUnblockRequest(blockable);
} else {
boolean toastShown = false;
if (xmppActivity.xmppConnectionService.sendBlockRequest(blockable, binding.reportSpam.isChecked())) {
Toast.makeText(xmppActivity, R.string.corresponding_conversations_closed, Toast.LENGTH_SHORT).show();
toastShown = true;
}
if (xmppActivity instanceof ContactDetailsActivity) {
if (!toastShown) {
Toast.makeText(xmppActivity, R.string.contact_blocked_past_tense, Toast.LENGTH_SHORT).show();
}
2018-03-29 13:08:43 +00:00
xmppActivity.finish();
}
}
});
builder.create().show();
}
}