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