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