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