2014-12-21 20:43:58 +00:00
|
|
|
package eu.siacs.conversations.ui;
|
|
|
|
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.DialogInterface;
|
2016-09-20 12:21:41 +00:00
|
|
|
import android.text.SpannableString;
|
|
|
|
import android.text.Spanned;
|
|
|
|
import android.text.style.TypefaceSpan;
|
2016-09-18 21:21:05 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.CheckBox;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.TextView;
|
2017-03-06 15:53:54 +00:00
|
|
|
import android.widget.Toast;
|
2014-12-21 20:43:58 +00:00
|
|
|
|
|
|
|
import eu.siacs.conversations.R;
|
|
|
|
import eu.siacs.conversations.entities.Blockable;
|
2017-03-20 11:55:48 +00:00
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
2014-12-21 20:43:58 +00:00
|
|
|
|
|
|
|
public final class BlockContactDialog {
|
2017-03-06 15:53:54 +00:00
|
|
|
public static void show(final XmppActivity xmppActivity,
|
2014-12-21 20:43:58 +00:00
|
|
|
final Blockable blockable) {
|
2017-03-06 15:53:54 +00:00
|
|
|
final AlertDialog.Builder builder = new AlertDialog.Builder(xmppActivity);
|
2014-12-21 20:43:58 +00:00
|
|
|
final boolean isBlocked = blockable.isBlocked();
|
|
|
|
builder.setNegativeButton(R.string.cancel, null);
|
2017-03-06 15:53:54 +00:00
|
|
|
LayoutInflater inflater = (LayoutInflater) xmppActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
2016-09-18 21:21:05 +00:00
|
|
|
LinearLayout view = (LinearLayout) inflater.inflate(R.layout.dialog_block_contact,null);
|
|
|
|
TextView message = (TextView) view.findViewById(R.id.text);
|
|
|
|
final CheckBox report = (CheckBox) view.findViewById(R.id.report_spam);
|
|
|
|
final boolean reporting = blockable.getAccount().getXmppConnection().getFeatures().spamReporting();
|
|
|
|
report.setVisibility(!isBlocked && reporting ? View.VISIBLE : View.GONE);
|
|
|
|
builder.setView(view);
|
2014-12-21 20:43:58 +00:00
|
|
|
|
2016-09-20 12:21:41 +00:00
|
|
|
String value;
|
|
|
|
SpannableString spannable;
|
2014-12-21 20:43:58 +00:00
|
|
|
if (blockable.getJid().isDomainJid() || blockable.getAccount().isBlocked(blockable.getJid().toDomainJid())) {
|
|
|
|
builder.setTitle(isBlocked ? R.string.action_unblock_domain : R.string.action_block_domain);
|
2016-09-20 12:21:41 +00:00
|
|
|
value = blockable.getJid().toDomainJid().toString();
|
2017-03-06 15:53:54 +00:00
|
|
|
spannable = new SpannableString(xmppActivity.getString(isBlocked ? R.string.unblock_domain_text : R.string.block_domain_text, value));
|
2014-12-21 20:43:58 +00:00
|
|
|
} else {
|
2017-03-20 11:55:48 +00:00
|
|
|
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);
|
2016-09-20 12:21:41 +00:00
|
|
|
value = blockable.getJid().toBareJid().toString();
|
2017-03-06 15:53:54 +00:00
|
|
|
spannable = new SpannableString(xmppActivity.getString(isBlocked ? R.string.unblock_contact_text : R.string.block_contact_text, value));
|
2014-12-21 20:43:58 +00:00
|
|
|
}
|
2016-09-20 12:21:41 +00:00
|
|
|
int start = spannable.toString().indexOf(value);
|
|
|
|
if (start >= 0) {
|
|
|
|
spannable.setSpan(new TypefaceSpan("monospace"),start,start + value.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
}
|
|
|
|
message.setText(spannable);
|
2014-12-21 20:43:58 +00:00
|
|
|
builder.setPositiveButton(isBlocked ? R.string.unblock : R.string.block, new DialogInterface.OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(final DialogInterface dialog, final int which) {
|
|
|
|
if (isBlocked) {
|
2017-03-06 15:53:54 +00:00
|
|
|
xmppActivity.xmppConnectionService.sendUnblockRequest(blockable);
|
2014-12-21 20:43:58 +00:00
|
|
|
} else {
|
2017-03-06 15:53:54 +00:00
|
|
|
boolean toastShown = false;
|
|
|
|
if (xmppActivity.xmppConnectionService.sendBlockRequest(blockable, report.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();
|
|
|
|
}
|
|
|
|
xmppActivity.finish();
|
|
|
|
}
|
2014-12-21 20:43:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
builder.create().show();
|
|
|
|
}
|
|
|
|
}
|