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