ConferenceDetailsActivity.java

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