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