1package eu.siacs.conversations.ui;
  2
  3import android.app.Activity;
  4import android.app.Dialog;
  5import android.content.Context;
  6import android.content.DialogInterface;
  7import android.os.Bundle;
  8import android.text.Editable;
  9import android.text.TextUtils;
 10import android.text.TextWatcher;
 11import android.view.View;
 12import android.widget.AdapterView;
 13import android.widget.Button;
 14import androidx.annotation.NonNull;
 15import androidx.appcompat.app.AlertDialog;
 16import androidx.databinding.DataBindingUtil;
 17import androidx.fragment.app.DialogFragment;
 18import com.google.android.material.dialog.MaterialAlertDialogBuilder;
 19import eu.siacs.conversations.R;
 20import eu.siacs.conversations.databinding.DialogCreatePublicChannelBinding;
 21import eu.siacs.conversations.entities.Account;
 22import eu.siacs.conversations.services.XmppConnectionService;
 23import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
 24import eu.siacs.conversations.ui.interfaces.OnBackendConnected;
 25import eu.siacs.conversations.ui.util.DelayedHintHelper;
 26import eu.siacs.conversations.utils.CryptoHelper;
 27import eu.siacs.conversations.xmpp.Jid;
 28import eu.siacs.conversations.xmpp.XmppConnection;
 29import java.util.ArrayList;
 30import java.util.Collection;
 31import java.util.List;
 32
 33public class CreatePublicChannelDialog extends DialogFragment implements OnBackendConnected {
 34
 35    private static final char[] FORBIDDEN =
 36            new char[] {'\u0022', '&', '\'', '/', ':', '<', '>', '@'};
 37
 38    private static final String ACCOUNTS_LIST_KEY = "activated_accounts_list";
 39    private CreatePublicChannelDialogListener mListener;
 40    private KnownHostsAdapter knownHostsAdapter;
 41    private boolean jidWasModified = false;
 42    private boolean nameEntered = false;
 43    private boolean skipTetxWatcher = false;
 44
 45    public static CreatePublicChannelDialog newInstance(final List<String> accounts) {
 46        CreatePublicChannelDialog dialog = new CreatePublicChannelDialog();
 47        Bundle bundle = new Bundle();
 48        bundle.putStringArrayList(ACCOUNTS_LIST_KEY, (ArrayList<String>) accounts);
 49        dialog.setArguments(bundle);
 50        return dialog;
 51    }
 52
 53    @Override
 54    public void onActivityCreated(Bundle savedInstanceState) {
 55        super.onActivityCreated(savedInstanceState);
 56        setRetainInstance(true);
 57    }
 58
 59    @NonNull
 60    @Override
 61    public Dialog onCreateDialog(Bundle savedInstanceState) {
 62        jidWasModified =
 63                savedInstanceState != null
 64                        && savedInstanceState.getBoolean("jid_was_modified_false", false);
 65        nameEntered =
 66                savedInstanceState != null && savedInstanceState.getBoolean("name_entered", false);
 67        final MaterialAlertDialogBuilder builder =
 68                new MaterialAlertDialogBuilder(requireActivity());
 69        builder.setTitle(R.string.create_public_channel);
 70        final DialogCreatePublicChannelBinding binding =
 71                DataBindingUtil.inflate(
 72                        getActivity().getLayoutInflater(),
 73                        R.layout.dialog_create_public_channel,
 74                        null,
 75                        false);
 76        binding.account.setOnItemSelectedListener(
 77                new AdapterView.OnItemSelectedListener() {
 78                    @Override
 79                    public void onItemSelected(
 80                            AdapterView<?> parent, View view, int position, long id) {
 81                        updateJidSuggestion(binding);
 82                    }
 83
 84                    @Override
 85                    public void onNothingSelected(AdapterView<?> parent) {}
 86                });
 87        binding.jid.addTextChangedListener(
 88                new TextWatcher() {
 89                    @Override
 90                    public void beforeTextChanged(
 91                            CharSequence s, int start, int count, int after) {}
 92
 93                    @Override
 94                    public void onTextChanged(CharSequence s, int start, int before, int count) {}
 95
 96                    @Override
 97                    public void afterTextChanged(Editable s) {
 98                        if (skipTetxWatcher) {
 99                            return;
100                        }
101                        if (jidWasModified) {
102                            jidWasModified = !TextUtils.isEmpty(s);
103                        } else {
104                            jidWasModified = !s.toString().equals(getJidSuggestion(binding));
105                        }
106                    }
107                });
108        updateInputs(binding, false);
109        ArrayList<String> mActivatedAccounts = getArguments().getStringArrayList(ACCOUNTS_LIST_KEY);
110        StartConversationActivity.populateAccountSpinner(
111                getActivity(), mActivatedAccounts, binding.account);
112        builder.setView(binding.getRoot());
113        builder.setPositiveButton(nameEntered ? R.string.create : R.string.next, null);
114        builder.setNegativeButton(nameEntered ? R.string.back : R.string.cancel, null);
115        DelayedHintHelper.setHint(R.string.channel_bare_jid_example, binding.jid);
116        this.knownHostsAdapter = new KnownHostsAdapter(getActivity(), R.layout.item_autocomplete);
117        binding.jid.setAdapter(knownHostsAdapter);
118        final AlertDialog dialog = builder.create();
119        binding.groupChatName.setOnEditorActionListener(
120                (v, actionId, event) -> {
121                    submit(dialog, binding);
122                    return true;
123                });
124        dialog.setOnShowListener(
125                dialogInterface -> {
126                    dialog.getButton(DialogInterface.BUTTON_NEGATIVE)
127                            .setOnClickListener(v -> goBack(dialog, binding));
128                    dialog.getButton(DialogInterface.BUTTON_POSITIVE)
129                            .setOnClickListener(v -> submit(dialog, binding));
130                });
131        return dialog;
132    }
133
134    private void updateJidSuggestion(final DialogCreatePublicChannelBinding binding) {
135        if (jidWasModified) {
136            return;
137        }
138        String jid = getJidSuggestion(binding);
139        skipTetxWatcher = true;
140        binding.jid.setText(jid);
141        skipTetxWatcher = false;
142    }
143
144    @Override
145    public void onSaveInstanceState(Bundle outState) {
146        outState.putBoolean("jid_was_modified", jidWasModified);
147        outState.putBoolean("name_entered", nameEntered);
148        super.onSaveInstanceState(outState);
149    }
150
151    private static String getJidSuggestion(final DialogCreatePublicChannelBinding binding) {
152        final Account account =
153                StartConversationActivity.getSelectedAccount(
154                        binding.getRoot().getContext(), binding.account);
155        final XmppConnection connection = account == null ? null : account.getXmppConnection();
156        if (connection == null) {
157            return "";
158        }
159        final Editable nameText = binding.groupChatName.getText();
160        final String name = nameText == null ? "" : nameText.toString().trim();
161        final String domain = connection.getMucServer();
162        if (domain == null) {
163            return "";
164        }
165        final String localpart = clean(name);
166        if (TextUtils.isEmpty(localpart)) {
167            return "";
168        } else {
169            try {
170                return Jid.of(localpart, domain, null).toString();
171            } catch (IllegalArgumentException e) {
172                return Jid.of(CryptoHelper.pronounceable(), domain, null).toString();
173            }
174        }
175    }
176
177    private static String clean(String name) {
178        for (char c : FORBIDDEN) {
179            name = name.replace(String.valueOf(c), "");
180        }
181        return name.replaceAll("\\s+", "-");
182    }
183
184    private void goBack(AlertDialog dialog, DialogCreatePublicChannelBinding binding) {
185        if (nameEntered) {
186            nameEntered = false;
187            updateInputs(binding, true);
188            updateButtons(dialog);
189        } else {
190            dialog.dismiss();
191        }
192    }
193
194    private void submit(AlertDialog dialog, DialogCreatePublicChannelBinding binding) {
195        final Context context = binding.getRoot().getContext();
196        final Editable nameText = binding.groupChatName.getText();
197        final String name = nameText == null ? "" : nameText.toString().trim();
198        final Editable addressText = binding.jid.getText();
199        final String address = addressText == null ? "" : addressText.toString().trim();
200        if (nameEntered) {
201            binding.nameLayout.setError(null);
202            if (address.isEmpty()) {
203                binding.xmppAddressLayout.setError(
204                        context.getText(R.string.please_enter_xmpp_address));
205            } else {
206                final Jid jid;
207                try {
208                    jid = Jid.ofUserInput(address);
209                } catch (final IllegalArgumentException e) {
210                    binding.xmppAddressLayout.setError(context.getText(R.string.invalid_jid));
211                    return;
212                }
213                final Account account =
214                        StartConversationActivity.getSelectedAccount(context, binding.account);
215                if (account == null) {
216                    return;
217                }
218                final XmppConnectionService service =
219                        ((XmppActivity) context).xmppConnectionService;
220                if (service != null && service.findFirstMuc(jid) != null) {
221                    binding.xmppAddressLayout.setError(
222                            context.getString(R.string.channel_already_exists));
223                    return;
224                }
225                mListener.onCreatePublicChannel(account, name, jid);
226                dialog.dismiss();
227            }
228        } else {
229            binding.xmppAddressLayout.setError(null);
230            if (name.isEmpty()) {
231                binding.nameLayout.setError(context.getText(R.string.please_enter_name));
232            } else if (StartConversationActivity.isValidJid(name)) {
233                binding.nameLayout.setError(context.getText(R.string.this_is_an_xmpp_address));
234            } else {
235                binding.nameLayout.setError(null);
236                nameEntered = true;
237                updateInputs(binding, true);
238                updateButtons(dialog);
239                binding.jid.setText("");
240                binding.jid.append(getJidSuggestion(binding));
241            }
242        }
243    }
244
245    private void updateInputs(
246            final DialogCreatePublicChannelBinding binding, final boolean requestFocus) {
247        binding.xmppAddressLayout.setVisibility(nameEntered ? View.VISIBLE : View.GONE);
248        binding.nameLayout.setVisibility(nameEntered ? View.GONE : View.VISIBLE);
249        if (!requestFocus) {
250            return;
251        }
252        if (nameEntered) {
253            binding.xmppAddressLayout.requestFocus();
254        } else {
255            binding.nameLayout.requestFocus();
256        }
257    }
258
259    private void updateButtons(AlertDialog dialog) {
260        final Button positive = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
261        final Button negative = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
262        positive.setText(nameEntered ? R.string.create : R.string.next);
263        negative.setText(nameEntered ? R.string.back : R.string.cancel);
264    }
265
266    @Override
267    public void onBackendConnected() {
268        refreshKnownHosts();
269    }
270
271    private void refreshKnownHosts() {
272        Activity activity = getActivity();
273        if (activity instanceof XmppActivity) {
274            Collection<String> hosts =
275                    ((XmppActivity) activity).xmppConnectionService.getKnownConferenceHosts();
276            this.knownHostsAdapter.refresh(hosts);
277        }
278    }
279
280    public interface CreatePublicChannelDialogListener {
281        void onCreatePublicChannel(Account account, String name, Jid address);
282    }
283
284    @Override
285    public void onAttach(@NonNull Context context) {
286        super.onAttach(context);
287        try {
288            mListener = (CreatePublicChannelDialogListener) context;
289        } catch (ClassCastException e) {
290            throw new ClassCastException(
291                    context.toString() + " must implement CreateConferenceDialogListener");
292        }
293    }
294
295    @Override
296    public void onStart() {
297        super.onStart();
298        final Activity activity = getActivity();
299        if (activity instanceof XmppActivity
300                && ((XmppActivity) activity).xmppConnectionService != null) {
301            refreshKnownHosts();
302        }
303    }
304
305    @Override
306    public void onDestroyView() {
307        Dialog dialog = getDialog();
308        if (dialog != null && getRetainInstance()) {
309            dialog.setDismissMessage(null);
310        }
311        super.onDestroyView();
312    }
313}