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