aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/ui/CreatePublicChannelDialog.java
blob: 02e44698e72fbd7c5c34d365c62cd524b86c4f66 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package de.pixart.messenger.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.DialogFragment;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import de.pixart.messenger.R;
import de.pixart.messenger.databinding.CreatePublicChannelDialogBinding;
import de.pixart.messenger.entities.Account;
import de.pixart.messenger.services.XmppConnectionService;
import de.pixart.messenger.ui.adapter.KnownHostsAdapter;
import de.pixart.messenger.ui.interfaces.OnBackendConnected;
import de.pixart.messenger.ui.util.DelayedHintHelper;
import de.pixart.messenger.utils.CryptoHelper;
import de.pixart.messenger.xmpp.XmppConnection;
import rocks.xmpp.addr.Jid;

public class CreatePublicChannelDialog extends DialogFragment implements OnBackendConnected {

    private static final char[] FORBIDDEN = new char[]{'\u0022', '&', '\'', '/', ':', '<', '>', '@'};

    private static final String ACCOUNTS_LIST_KEY = "activated_accounts_list";
    private static final String MULTIPLE_ACCOUNTS = "multiple_accounts_enabled";
    private CreatePublicChannelDialogListener mListener;
    private KnownHostsAdapter knownHostsAdapter;
    private boolean jidWasModified = false;
    private boolean nameEntered = false;
    private boolean skipTetxWatcher = false;
    private static final SecureRandom RANDOM = new SecureRandom();

    public static CreatePublicChannelDialog newInstance(List<String> accounts) {
        CreatePublicChannelDialog dialog = new CreatePublicChannelDialog();
        Bundle bundle = new Bundle();
        bundle.putStringArrayList(ACCOUNTS_LIST_KEY, (ArrayList<String>) accounts);
        dialog.setArguments(bundle);
        return dialog;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setRetainInstance(true);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        jidWasModified = savedInstanceState != null && savedInstanceState.getBoolean("jid_was_modified_false", false);
        nameEntered = savedInstanceState != null && savedInstanceState.getBoolean("name_entered", false);
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.create_public_channel);
        final CreatePublicChannelDialogBinding binding = DataBindingUtil.inflate(getActivity().getLayoutInflater(), R.layout.create_public_channel_dialog, null, false);
        if (getArguments().getBoolean(MULTIPLE_ACCOUNTS)) {
            binding.yourAccount.setVisibility(View.VISIBLE);
            binding.account.setVisibility(View.VISIBLE);
        } else {
            binding.yourAccount.setVisibility(View.GONE);
            binding.account.setVisibility(View.GONE);
        }
        binding.account.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                updateJidSuggestion(binding);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        binding.jid.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (skipTetxWatcher) {
                    return;
                }
                if (jidWasModified) {
                    jidWasModified = !TextUtils.isEmpty(s);
                } else {
                    jidWasModified = !s.toString().equals(getJidSuggestion(binding));
                }
            }
        });
        updateInputs(binding, false);
        ArrayList<String> mActivatedAccounts = getArguments().getStringArrayList(ACCOUNTS_LIST_KEY);
        StartConversationActivity.populateAccountSpinner(getActivity(), mActivatedAccounts, binding.account);
        builder.setView(binding.getRoot());
        builder.setPositiveButton(nameEntered ? R.string.create : R.string.next, null);
        builder.setNegativeButton(nameEntered ? R.string.back : R.string.cancel, null);
        DelayedHintHelper.setHint(R.string.channel_bare_jid_example, binding.jid);
        this.knownHostsAdapter = new KnownHostsAdapter(getActivity(), R.layout.simple_list_item);
        binding.jid.setAdapter(knownHostsAdapter);
        final AlertDialog dialog = builder.create();
        binding.groupChatName.setOnEditorActionListener((v, actionId, event) -> {
            submit(dialog, binding);
            return true;
        });
        dialog.setOnShowListener(dialogInterface -> {
            dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setOnClickListener(v -> goBack(dialog, binding));
            dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(v -> submit(dialog, binding));
        });
        return dialog;
    }

    private void updateJidSuggestion(CreatePublicChannelDialogBinding binding) {
        if (jidWasModified) {
            return;
        }
        String jid = getJidSuggestion(binding);
        skipTetxWatcher = true;
        binding.jid.setText(jid);
        skipTetxWatcher = false;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putBoolean("jid_was_modified", jidWasModified);
        outState.putBoolean("name_entered", nameEntered);
        super.onSaveInstanceState(outState);
    }

    private static String getJidSuggestion(CreatePublicChannelDialogBinding binding) {
        final Account account = StartConversationActivity.getSelectedAccount(binding.getRoot().getContext(), binding.account);
        final XmppConnection connection = account == null ? null : account.getXmppConnection();
        if (connection == null) {
            return "";
        }
        final Editable nameText = binding.groupChatName.getText();
        final String name = nameText == null ? "" : nameText.toString().trim();
        final String domain = connection.getMucServer();
        if (domain == null) {
            return "";
        }
        final String localpart = clean(name);
        if (TextUtils.isEmpty(localpart)) {
            return "";
        } else {
            try {
                return Jid.of(localpart, domain, null).toEscapedString();
            } catch (IllegalArgumentException e) {
                return Jid.of(CryptoHelper.pronounceable(RANDOM), domain, null).toEscapedString();
            }
        }
    }

    private static String clean(String name) {
        for (char c : FORBIDDEN) {
            name = name.replace(String.valueOf(c), "");
        }
        return name.replaceAll("\\s+", "-");
    }

    private void goBack(AlertDialog dialog, CreatePublicChannelDialogBinding binding) {
        if (nameEntered) {
            nameEntered = false;
            updateInputs(binding, true);
            updateButtons(dialog);
        } else {
            dialog.dismiss();
        }
    }

    private void submit(AlertDialog dialog, CreatePublicChannelDialogBinding binding) {
        final Context context = binding.getRoot().getContext();
        final Editable nameText = binding.groupChatName.getText();
        final String name = nameText == null ? "" : nameText.toString().trim();
        final Editable addressText = binding.jid.getText();
        final String address = addressText == null ? "" : addressText.toString().trim();
        if (nameEntered) {
            binding.nameLayout.setError(null);
            if (address.isEmpty()) {
                binding.xmppAddressLayout.setError(context.getText(R.string.please_enter_xmpp_address));
            } else {
                final Jid jid;
                try {
                    jid = Jid.ofEscaped(address);
                } catch (IllegalArgumentException e) {
                    binding.xmppAddressLayout.setError(context.getText(R.string.invalid_jid));
                    return;
                }
                final Account account = StartConversationActivity.getSelectedAccount(context, binding.account);
                if (account == null) {
                    return;
                }
                final XmppConnectionService service = ((XmppActivity) context).xmppConnectionService;
                if (service != null && service.findFirstMuc(jid) != null) {
                    binding.xmppAddressLayout.setError(context.getString(R.string.channel_already_exists));
                    return;
                }
                mListener.onCreatePublicChannel(account, name, jid);
                dialog.dismiss();
            }
        } else {
            binding.xmppAddressLayout.setError(null);
            if (name.isEmpty()) {
                binding.nameLayout.setError(context.getText(R.string.please_enter_name));
            } else if (StartConversationActivity.isValidJid(name)) {
                binding.nameLayout.setError(context.getText(R.string.this_is_an_xmpp_address));
            } else {
                binding.nameLayout.setError(null);
                nameEntered = true;
                updateInputs(binding, true);
                updateButtons(dialog);
                binding.jid.setText("");
                binding.jid.append(getJidSuggestion(binding));
            }
        }
    }


    private void updateInputs(CreatePublicChannelDialogBinding binding, boolean requestFocus) {
        binding.xmppAddressLayout.setVisibility(nameEntered ? View.VISIBLE : View.GONE);
        binding.nameLayout.setVisibility(nameEntered ? View.GONE : View.VISIBLE);
        if (!requestFocus) {
            return;
        }
        if (nameEntered) {
            binding.xmppAddressLayout.requestFocus();
        } else {
            binding.nameLayout.requestFocus();
        }
    }

    private void updateButtons(AlertDialog dialog) {
        final Button positive = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
        final Button negative = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
        positive.setText(nameEntered ? R.string.create : R.string.next);
        negative.setText(nameEntered ? R.string.back : R.string.cancel);
    }

    @Override
    public void onBackendConnected() {
        refreshKnownHosts();
    }

    private void refreshKnownHosts() {
        Activity activity = getActivity();
        if (activity instanceof XmppActivity) {
            Collection<String> hosts = ((XmppActivity) activity).xmppConnectionService.getKnownConferenceHosts();
            this.knownHostsAdapter.refresh(hosts);
        }
    }

    public interface CreatePublicChannelDialogListener {
        void onCreatePublicChannel(Account account, String name, Jid address);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            mListener = (CreatePublicChannelDialogListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString()
                    + " must implement CreateConferenceDialogListener");
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        final Activity activity = getActivity();
        if (activity instanceof XmppActivity && ((XmppActivity) activity).xmppConnectionService != null) {
            refreshKnownHosts();
        }
    }

    @Override
    public void onDestroyView() {
        Dialog dialog = getDialog();
        if (dialog != null && getRetainInstance()) {
            dialog.setDismissMessage(null);
        }
        super.onDestroyView();
    }
}