aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/ui/BlockContactDialog.java
blob: 5a911298fa6babe54c2db62c61c3e63353bf27a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package de.pixart.messenger.ui;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TypefaceSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import de.pixart.messenger.R;
import de.pixart.messenger.entities.Blockable;
import de.pixart.messenger.entities.Conversation;

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);
        LayoutInflater inflater = (LayoutInflater) xmppActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout view = (LinearLayout) inflater.inflate(R.layout.dialog_block_contact, null);
        TextView message = view.findViewById(R.id.text);
        final CheckBox report = 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);

        String value;
        SpannableString spannable;
        if (blockable.getJid().isDomainJid() || blockable.getAccount().isBlocked(blockable.getJid().toDomainJid())) {
            builder.setTitle(isBlocked ? R.string.action_unblock_domain : R.string.action_block_domain);
            value = blockable.getJid().toDomainJid().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);
            value = blockable.getJid().toBareJid().toString();
            spannable = new SpannableString(xmppActivity.getString(isBlocked ? R.string.unblock_contact_text : R.string.block_contact_text, value));
        }
        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);
        builder.setPositiveButton(isBlocked ? R.string.unblock : R.string.block, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(final DialogInterface dialog, final int which) {
                if (isBlocked) {
                    xmppActivity.xmppConnectionService.sendUnblockRequest(blockable);
                } else {
                    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();
                    }
                }
            }
        });
        builder.create().show();
    }
}