1package eu.siacs.conversations.ui;
2
3import android.app.Activity;
4import android.app.PendingIntent;
5import android.content.Context;
6import android.content.Intent;
7import android.content.SharedPreferences;
8import android.graphics.PorterDuff;
9import android.net.Uri;
10import android.os.Bundle;
11import android.preference.PreferenceManager;
12import android.text.Editable;
13import android.text.SpannableStringBuilder;
14import android.text.TextWatcher;
15import android.text.method.LinkMovementMethod;
16import android.view.LayoutInflater;
17import android.view.Menu;
18import android.view.MenuItem;
19import android.view.View;
20import android.view.View.OnClickListener;
21import android.view.ViewGroup;
22import android.widget.ArrayAdapter;
23import android.widget.TextView;
24import android.widget.Toast;
25
26import androidx.annotation.NonNull;
27import androidx.appcompat.app.AlertDialog;
28import androidx.databinding.DataBindingUtil;
29
30import com.cheogram.android.Util;
31
32import java.util.ArrayList;
33import java.util.Collections;
34import java.util.Comparator;
35import java.util.List;
36import java.util.Map;
37import java.util.concurrent.atomic.AtomicInteger;
38import java.util.stream.Collectors;
39
40import eu.siacs.conversations.Config;
41import eu.siacs.conversations.R;
42import eu.siacs.conversations.databinding.ActivityMucDetailsBinding;
43import eu.siacs.conversations.databinding.ThreadRowBinding;
44import eu.siacs.conversations.entities.Account;
45import eu.siacs.conversations.entities.Bookmark;
46import eu.siacs.conversations.entities.Contact;
47import eu.siacs.conversations.entities.Conversation;
48import eu.siacs.conversations.entities.ListItem;
49import eu.siacs.conversations.entities.MucOptions;
50import eu.siacs.conversations.entities.MucOptions.User;
51import eu.siacs.conversations.services.XmppConnectionService;
52import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdate;
53import eu.siacs.conversations.services.XmppConnectionService.OnMucRosterUpdate;
54import eu.siacs.conversations.ui.adapter.MediaAdapter;
55import eu.siacs.conversations.ui.adapter.UserPreviewAdapter;
56import eu.siacs.conversations.ui.interfaces.OnMediaLoaded;
57import eu.siacs.conversations.ui.util.Attachment;
58import eu.siacs.conversations.ui.util.AvatarWorkerTask;
59import eu.siacs.conversations.ui.util.GridManager;
60import eu.siacs.conversations.ui.util.MenuDoubleTabUtil;
61import eu.siacs.conversations.ui.util.MucConfiguration;
62import eu.siacs.conversations.ui.util.MucDetailsContextMenuHelper;
63import eu.siacs.conversations.ui.util.MyLinkify;
64import eu.siacs.conversations.ui.util.SoftKeyboardUtils;
65import eu.siacs.conversations.utils.AccountUtils;
66import eu.siacs.conversations.utils.Compatibility;
67import eu.siacs.conversations.utils.StringUtils;
68import eu.siacs.conversations.utils.StylingHelper;
69import eu.siacs.conversations.utils.UIHelper;
70import eu.siacs.conversations.utils.XmppUri;
71import eu.siacs.conversations.xmpp.Jid;
72import eu.siacs.conversations.xmpp.XmppConnection;
73import me.drakeet.support.toast.ToastCompat;
74
75import static eu.siacs.conversations.entities.Bookmark.printableValue;
76import static eu.siacs.conversations.utils.StringUtils.changed;
77
78public class ConferenceDetailsActivity extends XmppActivity implements OnConversationUpdate, OnMucRosterUpdate, XmppConnectionService.OnAffiliationChanged, XmppConnectionService.OnConfigurationPushed, XmppConnectionService.OnRoomDestroy, TextWatcher, OnMediaLoaded {
79 public static final String ACTION_VIEW_MUC = "view_muc";
80
81 private Conversation mConversation;
82 private ActivityMucDetailsBinding binding;
83 private MediaAdapter mMediaAdapter;
84 private UserPreviewAdapter mUserPreviewAdapter;
85 private String uuid = null;
86
87 private boolean mAdvancedMode = false;
88 private boolean showDynamicTags = true;
89
90 private final UiCallback<Conversation> renameCallback = new UiCallback<Conversation>() {
91 @Override
92 public void success(Conversation object) {
93 displayToast(getString(R.string.your_nick_has_been_changed));
94 runOnUiThread(() -> {
95 updateView();
96 });
97
98 }
99
100 @Override
101 public void error(final int errorCode, Conversation object) {
102 displayToast(getString(errorCode));
103 }
104
105 @Override
106 public void userInputRequired(PendingIntent pi, Conversation object) {
107
108 }
109 };
110
111 public static void open(final Activity activity, final Conversation conversation) {
112 Intent intent = new Intent(activity, ConferenceDetailsActivity.class);
113 intent.setAction(ConferenceDetailsActivity.ACTION_VIEW_MUC);
114 intent.putExtra("uuid", conversation.getUuid());
115 activity.startActivity(intent);
116 }
117
118 private final OnClickListener mNotifyStatusClickListener = new OnClickListener() {
119 @Override
120 public void onClick(View v) {
121 AlertDialog.Builder builder = new AlertDialog.Builder(ConferenceDetailsActivity.this);
122 builder.setTitle(R.string.pref_notification_settings);
123 String[] choices = {
124 getString(R.string.notify_on_all_messages),
125 getString(R.string.notify_only_when_highlighted),
126 getString(R.string.notify_only_when_highlighted_or_replied),
127 getString(R.string.notify_never)
128 };
129 final AtomicInteger choice;
130 if (mConversation.getLongAttribute(Conversation.ATTRIBUTE_MUTED_TILL, 0) == Long.MAX_VALUE) {
131 choice = new AtomicInteger(3);
132 } else {
133 choice = new AtomicInteger(mConversation.alwaysNotify() ? 0 : (mConversation.notifyReplies() ? 2 : 1));
134 }
135 builder.setSingleChoiceItems(choices, choice.get(), (dialog, which) -> choice.set(which));
136 builder.setNegativeButton(R.string.cancel, null);
137 builder.setPositiveButton(R.string.ok, (dialog, which) -> {
138 if (choice.get() == 3) {
139 mConversation.setMutedTill(Long.MAX_VALUE);
140 } else {
141 mConversation.setMutedTill(0);
142 mConversation.setAttribute(Conversation.ATTRIBUTE_ALWAYS_NOTIFY, String.valueOf(choice.get() == 0));
143 mConversation.setAttribute(Conversation.ATTRIBUTE_NOTIFY_REPLIES, String.valueOf(choice.get() == 2));
144 }
145 xmppConnectionService.updateConversation(mConversation);
146 updateView();
147 });
148 builder.create().show();
149 }
150 };
151
152 private final OnClickListener mChangeConferenceSettings = new OnClickListener() {
153 @Override
154 public void onClick(View v) {
155 final MucOptions mucOptions = mConversation.getMucOptions();
156 AlertDialog.Builder builder = new AlertDialog.Builder(ConferenceDetailsActivity.this);
157 MucConfiguration configuration = MucConfiguration.get(ConferenceDetailsActivity.this, mAdvancedMode, mucOptions);
158 builder.setTitle(configuration.title);
159 final boolean[] values = configuration.values;
160 builder.setMultiChoiceItems(configuration.names, values, (dialog, which, isChecked) -> values[which] = isChecked);
161 builder.setNegativeButton(R.string.cancel, null);
162 builder.setPositiveButton(R.string.confirm, (dialog, which) -> {
163 final Bundle options = configuration.toBundle(values);
164 options.putString("muc#roomconfig_persistentroom", "1");
165 options.putString("{http://prosody.im/protocol/muc}roomconfig_allowmemberinvites", options.getString("muc#roomconfig_allowinvites"));
166 xmppConnectionService.pushConferenceConfiguration(mConversation,
167 options,
168 ConferenceDetailsActivity.this);
169 });
170 builder.create().show();
171 }
172 };
173
174
175 @Override
176 public void onConversationUpdate() {
177 refreshUi();
178 }
179
180 @Override
181 public void onMucRosterUpdate() {
182 refreshUi();
183 }
184
185 @Override
186 protected void refreshUiReal() {
187 updateView();
188 }
189
190 @Override
191 protected void onCreate(Bundle savedInstanceState) {
192 super.onCreate(savedInstanceState);
193 this.binding = DataBindingUtil.setContentView(this, R.layout.activity_muc_details);
194 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
195 showDynamicTags = preferences.getBoolean(SettingsActivity.SHOW_DYNAMIC_TAGS, getResources().getBoolean(R.bool.show_dynamic_tags));
196 this.binding.changeConferenceButton.setOnClickListener(this.mChangeConferenceSettings);
197 setSupportActionBar(binding.toolbar);
198 configureActionBar(getSupportActionBar());
199 this.binding.editNickButton.setOnClickListener(v -> quickEdit(mConversation.getMucOptions().getActualNick(),
200 R.string.nickname,
201 value -> {
202 if (xmppConnectionService.renameInMuc(mConversation, value, renameCallback)) {
203 return null;
204 } else {
205 return getString(R.string.invalid_muc_nick);
206 }
207 }));
208 this.mAdvancedMode = getPreferences().getBoolean("advanced_muc_mode", false);
209 this.binding.mucInfoMore.setVisibility(this.mAdvancedMode ? View.VISIBLE : View.GONE);
210 this.binding.notificationStatusButton.setOnClickListener(this.mNotifyStatusClickListener);
211 this.binding.yourPhoto.setOnClickListener(v -> {
212 final MucOptions mucOptions = mConversation.getMucOptions();
213 if (!mucOptions.hasVCards()) {
214 Toast.makeText(this, R.string.host_does_not_support_group_chat_avatars, Toast.LENGTH_SHORT).show();
215 return;
216 }
217 if (!mucOptions.getSelf().getAffiliation().ranks(MucOptions.Affiliation.OWNER)) {
218 Toast.makeText(this, R.string.only_the_owner_can_change_group_chat_avatar, Toast.LENGTH_SHORT).show();
219 return;
220 }
221 final Intent intent = new Intent(this, PublishGroupChatProfilePictureActivity.class);
222 intent.putExtra("uuid", mConversation.getUuid());
223 startActivity(intent);
224 });
225 this.binding.editMucNameButton.setOnClickListener(this::onMucEditButtonClicked);
226 this.binding.mucEditTitle.addTextChangedListener(this);
227 this.binding.mucEditSubject.addTextChangedListener(this);
228 this.binding.mucEditSubject.addTextChangedListener(new StylingHelper.MessageEditorStyler(this.binding.mucEditSubject));
229 this.binding.editTags.addTextChangedListener(this);
230 this.mMediaAdapter = new MediaAdapter(this, R.dimen.media_size);
231 this.mUserPreviewAdapter = new UserPreviewAdapter();
232 this.binding.media.setAdapter(mMediaAdapter);
233 this.binding.users.setAdapter(mUserPreviewAdapter);
234 GridManager.setupLayoutManager(this, this.binding.media, R.dimen.media_size);
235 GridManager.setupLayoutManager(this, this.binding.users, R.dimen.media_size);
236 this.binding.recentThreads.setOnItemClickListener((a0, v, pos, a3) -> {
237 final Conversation.Thread thread = (Conversation.Thread) binding.recentThreads.getAdapter().getItem(pos);
238 switchToConversation(mConversation, null, false, null, false, true, null, thread.getThreadId());
239 });
240 this.binding.invite.setOnClickListener(v -> inviteToConversation(mConversation));
241 this.binding.showUsers.setOnClickListener(v -> {
242 Intent intent = new Intent(this, MucUsersActivity.class);
243 intent.putExtra("uuid", mConversation.getUuid());
244 startActivity(intent);
245 });
246 this.binding.relatedMucs.setOnClickListener(v -> {
247 final Intent intent = new Intent(this, ChannelDiscoveryActivity.class);
248 intent.putExtra("services", new String[]{ mConversation.getJid().getDomain().toEscapedString(), mConversation.getAccount().getJid().toEscapedString() });
249 startActivity(intent);
250 });
251 }
252
253 @Override
254 protected void onStart() {
255 super.onStart();
256 final int theme = findTheme();
257 if (this.mTheme != theme) {
258 recreate();
259 }
260 binding.mediaWrapper.setVisibility(Compatibility.hasStoragePermission(this) ? View.VISIBLE : View.GONE);
261 }
262
263 @Override
264 public boolean onOptionsItemSelected(MenuItem menuItem) {
265 if (MenuDoubleTabUtil.shouldIgnoreTap()) {
266 return false;
267 }
268 switch (menuItem.getItemId()) {
269 case android.R.id.home:
270 finish();
271 break;
272 case R.id.action_share_http:
273 shareLink(true);
274 break;
275 case R.id.action_share_uri:
276 shareLink(false);
277 break;
278 case R.id.action_save_as_bookmark:
279 saveAsBookmark();
280 break;
281 case R.id.action_delete_bookmark:
282 deleteBookmark();
283 break;
284 case R.id.action_destroy_room:
285 destroyRoom();
286 break;
287 case R.id.action_advanced_mode:
288 this.mAdvancedMode = !menuItem.isChecked();
289 menuItem.setChecked(this.mAdvancedMode);
290 getPreferences().edit().putBoolean("advanced_muc_mode", mAdvancedMode).apply();
291 final boolean online = mConversation != null && mConversation.getMucOptions().online();
292 this.binding.mucInfoMore.setVisibility(this.mAdvancedMode && online ? View.VISIBLE : View.GONE);
293 invalidateOptionsMenu();
294 updateView();
295 break;
296 }
297 return super.onOptionsItemSelected(menuItem);
298 }
299
300 @Override
301 public boolean onContextItemSelected(MenuItem item) {
302 final User user = mUserPreviewAdapter.getSelectedUser();
303 if (user == null) {
304 Toast.makeText(this, R.string.unable_to_perform_this_action, Toast.LENGTH_SHORT).show();
305 return true;
306 }
307 if (!MucDetailsContextMenuHelper.onContextItemSelected(item, mUserPreviewAdapter.getSelectedUser(), this)) {
308 return super.onContextItemSelected(item);
309 }
310 return true;
311 }
312
313 public void onMucEditButtonClicked(View v) {
314 if (this.binding.mucEditor.getVisibility() == View.GONE) {
315 final MucOptions mucOptions = mConversation.getMucOptions();
316 this.binding.mucEditor.setVisibility(View.VISIBLE);
317 this.binding.mucDisplay.setVisibility(View.GONE);
318 this.binding.editMucNameButton.setImageResource(getThemeResource(R.attr.icon_cancel, R.drawable.ic_cancel_black_24dp));
319 final String name = mucOptions.getName();
320 this.binding.mucEditTitle.setText("");
321 final boolean owner = mucOptions.getSelf().getAffiliation().ranks(MucOptions.Affiliation.OWNER);
322 if (owner || printableValue(name)) {
323 this.binding.mucEditTitle.setVisibility(View.VISIBLE);
324 if (name != null) {
325 this.binding.mucEditTitle.append(name);
326 }
327 } else {
328 this.binding.mucEditTitle.setVisibility(View.GONE);
329 }
330 this.binding.mucEditTitle.setEnabled(owner);
331 final String subject = mucOptions.getSubject();
332 this.binding.mucEditSubject.setText("");
333 if (subject != null) {
334 this.binding.mucEditSubject.append(subject);
335 }
336 this.binding.mucEditSubject.setEnabled(mucOptions.canChangeSubject());
337 if (!owner) {
338 this.binding.mucEditSubject.requestFocus();
339 }
340
341 final Bookmark bookmark = mConversation.getBookmark();
342 if (bookmark != null && mConversation.getAccount().getXmppConnection().getFeatures().bookmarks2() && showDynamicTags) {
343 for (final ListItem.Tag group : bookmark.getGroupTags()) {
344 binding.editTags.addObjectSync(group);
345 }
346 ArrayList<ListItem.Tag> tags = new ArrayList<>();
347 for (final Account account : xmppConnectionService.getAccounts()) {
348 for (Contact contact : account.getRoster().getContacts()) {
349 tags.addAll(contact.getTags(this));
350 }
351 for (Bookmark bmark : account.getBookmarks()) {
352 tags.addAll(bmark.getTags(this));
353 }
354 }
355 Comparator<Map.Entry<ListItem.Tag,Integer>> sortTagsBy = Map.Entry.comparingByValue(Comparator.reverseOrder());
356 sortTagsBy = sortTagsBy.thenComparing(entry -> entry.getKey().getName());
357
358 ArrayAdapter<ListItem.Tag> adapter = new ArrayAdapter<>(
359 this,
360 android.R.layout.simple_list_item_1,
361 tags.stream()
362 .collect(Collectors.toMap((x) -> x, (t) -> 1, (c1, c2) -> c1 + c2))
363 .entrySet().stream()
364 .sorted(sortTagsBy)
365 .map(e -> e.getKey()).collect(Collectors.toList())
366 );
367 binding.editTags.setAdapter(adapter);
368 this.binding.editTags.setVisibility(View.VISIBLE);
369 } else {
370 this.binding.editTags.setVisibility(View.GONE);
371 }
372 } else {
373 String subject = this.binding.mucEditSubject.isEnabled() ? this.binding.mucEditSubject.getEditableText().toString().trim() : null;
374 String name = this.binding.mucEditTitle.isEnabled() ? this.binding.mucEditTitle.getEditableText().toString().trim() : null;
375 onMucInfoUpdated(subject, name);
376
377 final Bookmark bookmark = mConversation.getBookmark();
378 if (bookmark != null && mConversation.getAccount().getXmppConnection().getFeatures().bookmarks2()) {
379 bookmark.setGroups(binding.editTags.getObjects().stream().map(tag -> tag.getName()).collect(Collectors.toList()));
380 xmppConnectionService.createBookmark(bookmark.getAccount(), bookmark);
381 }
382
383 SoftKeyboardUtils.hideSoftKeyboard(this);
384 hideEditor();
385 updateView();
386 }
387 }
388
389 private void hideEditor() {
390 this.binding.mucEditor.setVisibility(View.GONE);
391 this.binding.mucDisplay.setVisibility(View.VISIBLE);
392 this.binding.editMucNameButton.setImageResource(getThemeResource(R.attr.icon_edit_body, R.drawable.ic_edit_black_24dp));
393 }
394
395 private void onMucInfoUpdated(String subject, String name) {
396 final MucOptions mucOptions = mConversation.getMucOptions();
397 if (mucOptions.canChangeSubject() && changed(mucOptions.getSubject(), subject)) {
398 xmppConnectionService.pushSubjectToConference(mConversation, subject);
399 }
400 if (mucOptions.getSelf().getAffiliation().ranks(MucOptions.Affiliation.OWNER) && changed(mucOptions.getName(), name)) {
401 Bundle options = new Bundle();
402 options.putString("muc#roomconfig_persistentroom", "1");
403 options.putString("muc#roomconfig_roomname", StringUtils.nullOnEmpty(name));
404 xmppConnectionService.pushConferenceConfiguration(mConversation, options, this);
405 }
406 }
407
408
409 @Override
410 protected String getShareableUri(boolean http) {
411 if (mConversation != null) {
412 if (http) {
413 return "https://conversations.im/j/" + XmppUri.lameUrlEncode(mConversation.getJid().asBareJid().toEscapedString());
414 } else {
415 return "xmpp:" + Uri.encode(mConversation.getJid().asBareJid().toEscapedString(), "@/+") + "?join";
416 }
417 } else {
418 return null;
419 }
420 }
421
422 @Override
423 public boolean onPrepareOptionsMenu(Menu menu) {
424 MenuItem menuItemSaveBookmark = menu.findItem(R.id.action_save_as_bookmark);
425 MenuItem menuItemDeleteBookmark = menu.findItem(R.id.action_delete_bookmark);
426 MenuItem menuItemAdvancedMode = menu.findItem(R.id.action_advanced_mode);
427 MenuItem menuItemDestroyRoom = menu.findItem(R.id.action_destroy_room);
428 menuItemAdvancedMode.setChecked(mAdvancedMode);
429 if (mConversation == null) {
430 return true;
431 }
432 if (mConversation.getBookmark() != null) {
433 menuItemSaveBookmark.setVisible(false);
434 menuItemDeleteBookmark.setVisible(true);
435 } else {
436 menuItemDeleteBookmark.setVisible(false);
437 menuItemSaveBookmark.setVisible(true);
438 }
439 menuItemDestroyRoom.setVisible(mConversation.getMucOptions().getSelf().getAffiliation().ranks(MucOptions.Affiliation.OWNER));
440 return true;
441 }
442
443 @Override
444 public boolean onCreateOptionsMenu(Menu menu) {
445 final boolean groupChat = mConversation != null && mConversation.isPrivateAndNonAnonymous();
446 getMenuInflater().inflate(R.menu.muc_details, menu);
447 final MenuItem share = menu.findItem(R.id.action_share);
448 share.setVisible(!groupChat);
449 final MenuItem destroy = menu.findItem(R.id.action_destroy_room);
450 destroy.setTitle(groupChat ? R.string.destroy_room : R.string.destroy_channel);
451 AccountUtils.showHideMenuItems(menu);
452 return super.onCreateOptionsMenu(menu);
453 }
454
455 @Override
456 public void onMediaLoaded(List<Attachment> attachments) {
457 runOnUiThread(() -> {
458 int limit = GridManager.getCurrentColumnCount(binding.media);
459 mMediaAdapter.setAttachments(attachments.subList(0, Math.min(limit, attachments.size())));
460 binding.mediaWrapper.setVisibility(attachments.size() > 0 ? View.VISIBLE : View.GONE);
461 });
462
463 }
464
465
466 protected void saveAsBookmark() {
467 xmppConnectionService.saveConversationAsBookmark(mConversation, mConversation.getMucOptions().getName());
468 }
469
470 protected void deleteBookmark() {
471 final Account account = mConversation.getAccount();
472 final Bookmark bookmark = mConversation.getBookmark();
473 bookmark.setConversation(null);
474 xmppConnectionService.deleteBookmark(account, bookmark);
475 updateView();
476 }
477
478 protected void destroyRoom() {
479 final boolean groupChat = mConversation != null && mConversation.isPrivateAndNonAnonymous();
480 AlertDialog.Builder builder = new AlertDialog.Builder(this);
481 builder.setTitle(groupChat ? R.string.destroy_room : R.string.destroy_channel);
482 builder.setMessage(groupChat ? R.string.destroy_room_dialog : R.string.destroy_channel_dialog);
483 builder.setPositiveButton(R.string.ok, (dialog, which) -> {
484 xmppConnectionService.destroyRoom(mConversation, ConferenceDetailsActivity.this);
485 });
486 builder.setNegativeButton(R.string.cancel, null);
487 final AlertDialog dialog = builder.create();
488 dialog.setCanceledOnTouchOutside(false);
489 dialog.show();
490 }
491
492 @Override
493 void onBackendConnected() {
494 if (mPendingConferenceInvite != null) {
495 mPendingConferenceInvite.execute(this);
496 mPendingConferenceInvite = null;
497 }
498 if (getIntent().getAction().equals(ACTION_VIEW_MUC)) {
499 this.uuid = getIntent().getExtras().getString("uuid");
500 }
501 if (uuid != null) {
502 this.mConversation = xmppConnectionService.findConversationByUuid(uuid);
503 if (this.mConversation != null) {
504 if (Compatibility.hasStoragePermission(this)) {
505 final int limit = GridManager.getCurrentColumnCount(this.binding.media);
506 xmppConnectionService.getAttachments(this.mConversation, limit, this);
507 this.binding.showMedia.setOnClickListener((v) -> MediaBrowserActivity.launch(this, mConversation));
508 }
509 updateView();
510 }
511 }
512 }
513
514 @Override
515 public void onBackPressed() {
516 if (this.binding.mucEditor.getVisibility() == View.VISIBLE) {
517 hideEditor();
518 } else {
519 super.onBackPressed();
520 }
521 }
522
523 private void updateView() {
524 invalidateOptionsMenu();
525 if (mConversation == null) {
526 return;
527 }
528 final MucOptions mucOptions = mConversation.getMucOptions();
529 final User self = mucOptions.getSelf();
530 String account;
531 if (Config.DOMAIN_LOCK != null) {
532 account = mConversation.getAccount().getJid().getEscapedLocal();
533 } else {
534 account = mConversation.getAccount().getJid().asBareJid().toEscapedString();
535 }
536 setTitle(mucOptions.isPrivateAndNonAnonymous() ? R.string.action_muc_details : R.string.channel_details);
537 final Bookmark bookmark = mConversation.getBookmark();
538 final XmppConnection connection = mConversation.getAccount().getXmppConnection();
539 this.binding.editMucNameButton.setVisibility((self.getAffiliation().ranks(MucOptions.Affiliation.OWNER) || mucOptions.canChangeSubject() || (bookmark != null && connection != null && connection.getFeatures().bookmarks2())) ? View.VISIBLE : View.GONE);
540 this.binding.detailsAccount.setText(getString(R.string.using_account, account));
541 this.binding.truejid.setVisibility(View.GONE);
542 if (mConversation.isPrivateAndNonAnonymous()) {
543 this.binding.jid.setText(getString(R.string.hosted_on, mConversation.getJid().getDomain()));
544 this.binding.truejid.setText(mConversation.getJid().asBareJid().toEscapedString());
545 if (mAdvancedMode) this.binding.truejid.setVisibility(View.VISIBLE);
546 } else {
547 this.binding.jid.setText(mConversation.getJid().asBareJid().toEscapedString());
548 }
549 AvatarWorkerTask.loadAvatar(mConversation, binding.yourPhoto, R.dimen.avatar_on_details_screen_size);
550 String roomName = mucOptions.getName();
551 String subject = mucOptions.getSubject();
552 final boolean hasTitle;
553 if (printableValue(roomName)) {
554 this.binding.mucTitle.setText(roomName);
555 this.binding.mucTitle.setVisibility(View.VISIBLE);
556 hasTitle = true;
557 } else if (!printableValue(subject)) {
558 this.binding.mucTitle.setText(mConversation.getName());
559 hasTitle = true;
560 this.binding.mucTitle.setVisibility(View.VISIBLE);
561 } else {
562 hasTitle = false;
563 this.binding.mucTitle.setVisibility(View.GONE);
564 }
565 if (printableValue(subject)) {
566 SpannableStringBuilder spannable = new SpannableStringBuilder(subject);
567 StylingHelper.format(spannable, this.binding.mucSubject.getCurrentTextColor());
568 MyLinkify.addLinks(spannable, false);
569 this.binding.mucSubject.setText(spannable);
570 this.binding.mucSubject.setTextAppearance(this, subject.length() > (hasTitle ? 128 : 196) ? R.style.TextAppearance_Conversations_Body1_Linkified : R.style.TextAppearance_Conversations_Subhead);
571 this.binding.mucSubject.setAutoLinkMask(0);
572 this.binding.mucSubject.setVisibility(View.VISIBLE);
573 this.binding.mucSubject.setMovementMethod(LinkMovementMethod.getInstance());
574 } else {
575 this.binding.mucSubject.setVisibility(View.GONE);
576 }
577 this.binding.mucYourNick.setText(mucOptions.getActualNick());
578 if (mucOptions.online()) {
579 this.binding.usersWrapper.setVisibility(View.VISIBLE);
580 this.binding.mucInfoMore.setVisibility(this.mAdvancedMode ? View.VISIBLE : View.GONE);
581 this.binding.mucRole.setVisibility(View.VISIBLE);
582 this.binding.mucRole.setText(getStatus(self));
583 if (mucOptions.getSelf().getAffiliation().ranks(MucOptions.Affiliation.OWNER)) {
584 this.binding.mucSettings.setVisibility(View.VISIBLE);
585 this.binding.mucConferenceType.setText(MucConfiguration.describe(this, mucOptions));
586 } else if (!mucOptions.isPrivateAndNonAnonymous() && mucOptions.nonanonymous()) {
587 this.binding.mucSettings.setVisibility(View.VISIBLE);
588 this.binding.mucConferenceType.setText(R.string.group_chat_will_make_your_jabber_id_public);
589 } else {
590 this.binding.mucSettings.setVisibility(View.GONE);
591 }
592 if (mucOptions.mamSupport()) {
593 this.binding.mucInfoMam.setText(R.string.server_info_available);
594 } else {
595 this.binding.mucInfoMam.setText(R.string.server_info_unavailable);
596 }
597 if (self.getAffiliation().ranks(MucOptions.Affiliation.OWNER)) {
598 this.binding.changeConferenceButton.setVisibility(View.VISIBLE);
599 } else {
600 this.binding.changeConferenceButton.setVisibility(View.INVISIBLE);
601 }
602 } else {
603 this.binding.usersWrapper.setVisibility(View.GONE);
604 this.binding.mucInfoMore.setVisibility(View.GONE);
605 this.binding.mucSettings.setVisibility(View.GONE);
606 }
607
608 int ic_notifications = getThemeResource(R.attr.icon_notifications, R.drawable.ic_notifications_black_24dp);
609 int ic_notifications_off = getThemeResource(R.attr.icon_notifications_off, R.drawable.ic_notifications_off_black_24dp);
610 int ic_notifications_paused = getThemeResource(R.attr.icon_notifications_paused, R.drawable.ic_notifications_paused_black_24dp);
611 int ic_notifications_none = getThemeResource(R.attr.icon_notifications_none, R.drawable.ic_notifications_none_black_24dp);
612
613 long mutedTill = mConversation.getLongAttribute(Conversation.ATTRIBUTE_MUTED_TILL, 0);
614 if (mutedTill == Long.MAX_VALUE) {
615 this.binding.notificationStatusText.setText(R.string.notify_never);
616 this.binding.notificationStatusButton.setImageResource(ic_notifications_off);
617 } else if (System.currentTimeMillis() < mutedTill) {
618 this.binding.notificationStatusText.setText(R.string.notify_paused);
619 this.binding.notificationStatusButton.setImageResource(ic_notifications_paused);
620 } else if (mConversation.alwaysNotify()) {
621 this.binding.notificationStatusText.setText(R.string.notify_on_all_messages);
622 this.binding.notificationStatusButton.setImageResource(ic_notifications);
623 } else if (mConversation.notifyReplies()) {
624 this.binding.notificationStatusText.setText(R.string.notify_only_when_highlighted_or_replied);
625 this.binding.notificationStatusButton.setImageResource(ic_notifications_none);
626 } else {
627 this.binding.notificationStatusText.setText(R.string.notify_only_when_highlighted);
628 this.binding.notificationStatusButton.setImageResource(ic_notifications_none);
629 }
630 final List<User> users = mucOptions.getUsers();
631 Collections.sort(users, (a, b) -> {
632 if (b.getAffiliation().outranks(a.getAffiliation())) {
633 return 1;
634 } else if (a.getAffiliation().outranks(b.getAffiliation())) {
635 return -1;
636 } else {
637 if (a.getAvatar() != null && b.getAvatar() == null) {
638 return -1;
639 } else if (a.getAvatar() == null && b.getAvatar() != null) {
640 return 1;
641 } else {
642 return a.getComparableName().compareToIgnoreCase(b.getComparableName());
643 }
644 }
645 });
646 this.mUserPreviewAdapter.submitList(MucOptions.sub(users, GridManager.getCurrentColumnCount(binding.users)));
647 this.binding.invite.setVisibility(mucOptions.canInvite() ? View.VISIBLE : View.GONE);
648 this.binding.showUsers.setVisibility(users.size() > 0 ? View.VISIBLE : View.GONE);
649 this.binding.showUsers.setText(getResources().getQuantityString(R.plurals.view_users, users.size(), users.size()));
650 this.binding.usersWrapper.setVisibility(users.size() > 0 || mucOptions.canInvite() ? View.VISIBLE : View.GONE);
651 if (users.size() == 0) {
652 this.binding.noUsersHints.setText(mucOptions.isPrivateAndNonAnonymous() ? R.string.no_users_hint_group_chat : R.string.no_users_hint_channel);
653 this.binding.noUsersHints.setVisibility(View.VISIBLE);
654 } else {
655 this.binding.noUsersHints.setVisibility(View.GONE);
656 }
657
658 if (bookmark == null) {
659 binding.tags.setVisibility(View.GONE);
660 return;
661 }
662
663 final List<Conversation.Thread> recentThreads = mConversation.recentThreads();
664 if (recentThreads.isEmpty()) {
665 this.binding.recentThreadsWrapper.setVisibility(View.GONE);
666 } else {
667 final ThreadAdapter threads = new ThreadAdapter();
668 threads.addAll(recentThreads);
669 this.binding.recentThreads.setAdapter(threads);
670 this.binding.recentThreadsWrapper.setVisibility(View.VISIBLE);
671 Util.justifyListViewHeightBasedOnChildren(binding.recentThreads);
672 }
673
674 List<ListItem.Tag> tagList = bookmark.getTags(this);
675 if (tagList.size() == 0 || !showDynamicTags) {
676 binding.tags.setVisibility(View.GONE);
677 } else {
678 final LayoutInflater inflater = getLayoutInflater();
679 binding.tags.setVisibility(View.VISIBLE);
680 binding.tags.removeAllViewsInLayout();
681 for (final ListItem.Tag tag : tagList) {
682 final TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag, binding.tags, false);
683 tv.setText(tag.getName());
684 tv.getBackground().mutate().setColorFilter(tag.getColor(), PorterDuff.Mode.SRC_IN);
685 binding.tags.addView(tv);
686 }
687 }
688 }
689
690 public static String getStatus(Context context, User user, final boolean advanced) {
691 if (advanced) {
692 return String.format("%s (%s)", context.getString(user.getAffiliation().getResId()), context.getString(user.getRole().getResId()));
693 } else {
694 return context.getString(user.getAffiliation().getResId());
695 }
696 }
697
698 private String getStatus(User user) {
699 return getStatus(this, user, mAdvancedMode);
700 }
701
702 @Override
703 public void onAffiliationChangedSuccessful(Jid jid) {
704 refreshUi();
705 }
706
707 @Override
708 public void onAffiliationChangeFailed(Jid jid, int resId) {
709 displayToast(getString(resId, jid.asBareJid().toEscapedString()));
710 }
711
712 @Override
713 public void onRoomDestroySucceeded() {
714 finish();
715 }
716
717 @Override
718 public void onRoomDestroyFailed() {
719 final boolean groupChat = mConversation != null && mConversation.isPrivateAndNonAnonymous();
720 displayToast(getString(groupChat ? R.string.could_not_destroy_room : R.string.could_not_destroy_channel));
721 }
722
723 @Override
724 public void onPushSucceeded() {
725 displayToast(getString(R.string.modified_conference_options));
726 }
727
728 @Override
729 public void onPushFailed() {
730 displayToast(getString(R.string.could_not_modify_conference_options));
731 }
732
733 private void displayToast(final String msg) {
734 runOnUiThread(() -> {
735 if (isFinishing()) {
736 return;
737 }
738 ToastCompat.makeText(this, msg, Toast.LENGTH_SHORT).show();
739 });
740 }
741
742 @Override
743 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
744
745 }
746
747 @Override
748 public void onTextChanged(CharSequence s, int start, int before, int count) {
749
750 }
751
752 @Override
753 public void afterTextChanged(Editable s) {
754 if (mConversation == null) {
755 return;
756 }
757 final MucOptions mucOptions = mConversation.getMucOptions();
758 if (this.binding.mucEditor.getVisibility() == View.VISIBLE) {
759 boolean subjectChanged = changed(binding.mucEditSubject.getEditableText().toString(), mucOptions.getSubject());
760 boolean nameChanged = changed(binding.mucEditTitle.getEditableText().toString(), mucOptions.getName());
761 final Bookmark bookmark = mConversation.getBookmark();
762 if (subjectChanged || nameChanged || (bookmark != null && mConversation.getAccount().getXmppConnection().getFeatures().bookmarks2())) {
763 this.binding.editMucNameButton.setImageResource(getThemeResource(R.attr.icon_save, R.drawable.ic_save_black_24dp));
764 } else {
765 this.binding.editMucNameButton.setImageResource(getThemeResource(R.attr.icon_cancel, R.drawable.ic_cancel_black_24dp));
766 }
767 }
768 }
769
770 class ThreadAdapter extends ArrayAdapter<Conversation.Thread> {
771 ThreadAdapter() { super(ConferenceDetailsActivity.this, 0); }
772
773 @Override
774 public View getView(int position, View view, @NonNull ViewGroup parent) {
775 final ThreadRowBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.thread_row, parent, false);
776 final Conversation.Thread item = getItem(position);
777
778 binding.threadIdenticon.setColor(UIHelper.getColorForName(item.getThreadId()));
779 binding.threadIdenticon.setHash(UIHelper.identiconHash(item.getThreadId()));
780
781 binding.threadSubject.setText(item.getDisplay());
782
783 return binding.getRoot();
784 }
785 }
786}