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