ConferenceDetailsActivity.java

  1package eu.siacs.conversations.ui;
  2
  3import android.app.PendingIntent;
  4import android.content.Context;
  5import android.content.Intent;
  6import android.content.IntentSender.SendIntentException;
  7import android.content.res.Resources;
  8import android.databinding.DataBindingUtil;
  9import android.graphics.Bitmap;
 10import android.graphics.drawable.BitmapDrawable;
 11import android.graphics.drawable.Drawable;
 12import android.os.AsyncTask;
 13import android.os.Bundle;
 14import android.support.v4.content.ContextCompat;
 15import android.support.v7.app.AlertDialog;
 16import android.support.v7.widget.Toolbar;
 17import android.text.Editable;
 18import android.text.SpannableStringBuilder;
 19import android.text.TextWatcher;
 20import android.view.ContextMenu;
 21import android.view.LayoutInflater;
 22import android.view.Menu;
 23import android.view.MenuItem;
 24import android.view.View;
 25import android.view.View.OnClickListener;
 26import android.widget.ImageView;
 27import android.widget.Toast;
 28
 29import org.openintents.openpgp.util.OpenPgpUtils;
 30
 31import java.lang.ref.WeakReference;
 32import java.util.ArrayList;
 33import java.util.Collections;
 34import java.util.List;
 35import java.util.concurrent.RejectedExecutionException;
 36import java.util.concurrent.atomic.AtomicInteger;
 37
 38import eu.siacs.conversations.Config;
 39import eu.siacs.conversations.R;
 40import eu.siacs.conversations.crypto.PgpEngine;
 41import eu.siacs.conversations.databinding.ActivityMucDetailsBinding;
 42import eu.siacs.conversations.databinding.ContactBinding;
 43import eu.siacs.conversations.entities.Account;
 44import eu.siacs.conversations.entities.Bookmark;
 45import eu.siacs.conversations.entities.Contact;
 46import eu.siacs.conversations.entities.Conversation;
 47import eu.siacs.conversations.entities.MucOptions;
 48import eu.siacs.conversations.entities.MucOptions.User;
 49import eu.siacs.conversations.services.XmppConnectionService;
 50import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdate;
 51import eu.siacs.conversations.services.XmppConnectionService.OnMucRosterUpdate;
 52import eu.siacs.conversations.ui.adapter.MediaAdapter;
 53import eu.siacs.conversations.ui.interfaces.OnMediaLoaded;
 54import eu.siacs.conversations.ui.util.Attachment;
 55import eu.siacs.conversations.ui.util.GridManager;
 56import eu.siacs.conversations.ui.util.MenuDoubleTabUtil;
 57import eu.siacs.conversations.ui.util.MucDetailsContextMenuHelper;
 58import eu.siacs.conversations.ui.util.MyLinkify;
 59import eu.siacs.conversations.ui.util.SoftKeyboardUtils;
 60import eu.siacs.conversations.utils.EmojiWrapper;
 61import eu.siacs.conversations.utils.StringUtils;
 62import eu.siacs.conversations.utils.StylingHelper;
 63import eu.siacs.conversations.utils.UIHelper;
 64import eu.siacs.conversations.utils.XmppUri;
 65import rocks.xmpp.addr.Jid;
 66
 67import static eu.siacs.conversations.entities.Bookmark.printableValue;
 68import static eu.siacs.conversations.utils.StringUtils.changed;
 69
 70public class ConferenceDetailsActivity extends XmppActivity implements OnConversationUpdate, OnMucRosterUpdate, XmppConnectionService.OnAffiliationChanged, XmppConnectionService.OnRoleChanged, XmppConnectionService.OnConfigurationPushed, TextWatcher, OnMediaLoaded {
 71    public static final String ACTION_VIEW_MUC = "view_muc";
 72
 73    private static final float INACTIVE_ALPHA = 0.4684f; //compromise between dark and light theme
 74
 75    private Conversation mConversation;
 76    private OnClickListener inviteListener = new OnClickListener() {
 77
 78        @Override
 79        public void onClick(View v) {
 80            inviteToConversation(mConversation);
 81        }
 82    };
 83    private ActivityMucDetailsBinding binding;
 84    private MediaAdapter mMediaAdapter;
 85    private String uuid = null;
 86    private User mSelectedUser = null;
 87
 88    private boolean mAdvancedMode = false;
 89
 90    private UiCallback<Conversation> renameCallback = new UiCallback<Conversation>() {
 91        @Override
 92        public void success(Conversation object) {
 93            runOnUiThread(() -> {
 94                Toast.makeText(ConferenceDetailsActivity.this, getString(R.string.your_nick_has_been_changed), Toast.LENGTH_SHORT).show();
 95                updateView();
 96            });
 97
 98        }
 99
100        @Override
101        public void error(final int errorCode, Conversation object) {
102            runOnUiThread(() -> Toast.makeText(ConferenceDetailsActivity.this, getString(errorCode), Toast.LENGTH_SHORT).show());
103        }
104
105        @Override
106        public void userInputRequried(PendingIntent pi, Conversation object) {
107
108        }
109    };
110
111    private 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_never)
120            };
121            final AtomicInteger choice;
122            if (mConversation.getLongAttribute(Conversation.ATTRIBUTE_MUTED_TILL, 0) == Long.MAX_VALUE) {
123                choice = new AtomicInteger(2);
124            } else {
125                choice = new AtomicInteger(mConversation.alwaysNotify() ? 0 : 1);
126            }
127            builder.setSingleChoiceItems(choices, choice.get(), (dialog, which) -> choice.set(which));
128            builder.setNegativeButton(R.string.cancel, null);
129            builder.setPositiveButton(R.string.ok, (dialog, which) -> {
130                if (choice.get() == 2) {
131                    mConversation.setMutedTill(Long.MAX_VALUE);
132                } else {
133                    mConversation.setMutedTill(0);
134                    mConversation.setAttribute(Conversation.ATTRIBUTE_ALWAYS_NOTIFY, String.valueOf(choice.get() == 0));
135                }
136                xmppConnectionService.updateConversation(mConversation);
137                updateView();
138            });
139            builder.create().show();
140        }
141    };
142
143    private OnClickListener mChangeConferenceSettings = new OnClickListener() {
144        @Override
145        public void onClick(View v) {
146            final MucOptions mucOptions = mConversation.getMucOptions();
147            AlertDialog.Builder builder = new AlertDialog.Builder(ConferenceDetailsActivity.this);
148            builder.setTitle(R.string.conference_options);
149            final String[] options;
150            final boolean[] values;
151            if (mAdvancedMode) {
152                options = new String[]{
153                        getString(R.string.members_only),
154                        getString(R.string.moderated),
155                        getString(R.string.non_anonymous)
156                };
157                values = new boolean[]{
158                        mucOptions.membersOnly(),
159                        mucOptions.moderated(),
160                        mucOptions.nonanonymous()
161                };
162            } else {
163                options = new String[]{
164                        getString(R.string.members_only),
165                        getString(R.string.non_anonymous)
166                };
167                values = new boolean[]{
168                        mucOptions.membersOnly(),
169                        mucOptions.nonanonymous()
170                };
171            }
172            builder.setMultiChoiceItems(options, values, (dialog, which, isChecked) -> values[which] = isChecked);
173            builder.setNegativeButton(R.string.cancel, null);
174            builder.setPositiveButton(R.string.confirm, (dialog, which) -> {
175                if (!mucOptions.membersOnly() && values[0]) {
176                    xmppConnectionService.changeAffiliationsInConference(mConversation,
177                            MucOptions.Affiliation.NONE,
178                            MucOptions.Affiliation.MEMBER);
179                }
180                Bundle options1 = new Bundle();
181                options1.putString("muc#roomconfig_membersonly", values[0] ? "1" : "0");
182                if (values.length == 2) {
183                    options1.putString("muc#roomconfig_whois", values[1] ? "anyone" : "moderators");
184                } else if (values.length == 3) {
185                    options1.putString("muc#roomconfig_moderatedroom", values[1] ? "1" : "0");
186                    options1.putString("muc#roomconfig_whois", values[2] ? "anyone" : "moderators");
187                }
188                options1.putString("muc#roomconfig_persistentroom", "1");
189                final boolean whois = values.length == 2 ? values[1] : values[2];
190                if (values[0] == whois) {
191                    options1.putString("muc#roomconfig_publicroom", whois ? "0" : "1");
192                }
193                xmppConnectionService.pushConferenceConfiguration(mConversation,
194                        options1,
195                        ConferenceDetailsActivity.this);
196            });
197            builder.create().show();
198        }
199    };
200
201    public static boolean cancelPotentialWork(User user, ImageView imageView) {
202        final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
203
204        if (bitmapWorkerTask != null) {
205            final User old = bitmapWorkerTask.o;
206            if (old == null || user != old) {
207                bitmapWorkerTask.cancel(true);
208            } else {
209                return false;
210            }
211        }
212        return true;
213    }
214
215    private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
216        if (imageView != null) {
217            final Drawable drawable = imageView.getDrawable();
218            if (drawable instanceof AsyncDrawable) {
219                final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
220                return asyncDrawable.getBitmapWorkerTask();
221            }
222        }
223        return null;
224    }
225
226
227    @Override
228    public void onConversationUpdate() {
229        refreshUi();
230    }
231
232    @Override
233    public void onMucRosterUpdate() {
234        refreshUi();
235    }
236
237    @Override
238    protected void refreshUiReal() {
239        updateView();
240    }
241
242    @Override
243    protected void onCreate(Bundle savedInstanceState) {
244        super.onCreate(savedInstanceState);
245        this.binding = DataBindingUtil.setContentView(this, R.layout.activity_muc_details);
246        this.binding.mucMoreDetails.setVisibility(View.GONE);
247        this.binding.changeConferenceButton.setOnClickListener(this.mChangeConferenceSettings);
248        this.binding.invite.setOnClickListener(inviteListener);
249        setSupportActionBar((Toolbar) binding.toolbar);
250        configureActionBar(getSupportActionBar());
251        this.binding.editNickButton.setOnClickListener(v -> quickEdit(mConversation.getMucOptions().getActualNick(),
252                R.string.nickname,
253                value -> {
254                    if (xmppConnectionService.renameInMuc(mConversation, value, renameCallback)) {
255                        return null;
256                    } else {
257                        return getString(R.string.invalid_muc_nick);
258                    }
259                }));
260        this.mAdvancedMode = getPreferences().getBoolean("advanced_muc_mode", false);
261        this.binding.mucInfoMore.setVisibility(this.mAdvancedMode ? View.VISIBLE : View.GONE);
262        this.binding.notificationStatusButton.setOnClickListener(this.mNotifyStatusClickListener);
263        this.binding.yourPhoto.setOnClickListener(v -> {
264            final MucOptions mucOptions = mConversation.getMucOptions();
265            if (!mucOptions.hasVCards()) {
266                Toast.makeText(this, R.string.host_does_not_support_group_chat_avatars, Toast.LENGTH_SHORT).show();
267                return;
268            }
269            if (!mucOptions.getSelf().getAffiliation().ranks(MucOptions.Affiliation.OWNER)) {
270                Toast.makeText(this, R.string.only_the_owner_can_change_group_chat_avatar, Toast.LENGTH_SHORT).show();
271                return;
272            }
273            final Intent intent = new Intent(this, PublishGroupChatProfilePictureActivity.class);
274            intent.putExtra("uuid", mConversation.getUuid());
275            startActivity(intent);
276        });
277        this.binding.editMucNameButton.setOnClickListener(this::onMucEditButtonClicked);
278        this.binding.mucEditTitle.addTextChangedListener(this);
279        this.binding.mucEditSubject.addTextChangedListener(this);
280        this.binding.mucEditSubject.addTextChangedListener(new StylingHelper.MessageEditorStyler(this.binding.mucEditSubject));
281        mMediaAdapter = new MediaAdapter(this,R.dimen.media_size);
282        this.binding.media.setAdapter(mMediaAdapter);
283        GridManager.setupLayoutManager(this, this.binding.media, R.dimen.media_size);
284    }
285
286    @Override
287    protected void onStart() {
288        super.onStart();
289        final int theme = findTheme();
290        if (this.mTheme != theme) {
291            recreate();
292        }
293    }
294
295    @Override
296    public boolean onOptionsItemSelected(MenuItem menuItem) {
297        if (MenuDoubleTabUtil.shouldIgnoreTap()) {
298            return false;
299        }
300        switch (menuItem.getItemId()) {
301            case android.R.id.home:
302                finish();
303                break;
304            case R.id.action_share_http:
305                shareLink(true);
306                break;
307            case R.id.action_share_uri:
308                shareLink(false);
309                break;
310            case R.id.action_save_as_bookmark:
311                saveAsBookmark();
312                break;
313            case R.id.action_delete_bookmark:
314                deleteBookmark();
315                break;
316            case R.id.action_advanced_mode:
317                this.mAdvancedMode = !menuItem.isChecked();
318                menuItem.setChecked(this.mAdvancedMode);
319                getPreferences().edit().putBoolean("advanced_muc_mode", mAdvancedMode).apply();
320                final boolean online = mConversation != null && mConversation.getMucOptions().online();
321                this.binding.mucInfoMore.setVisibility(this.mAdvancedMode && online ? View.VISIBLE : View.GONE);
322                invalidateOptionsMenu();
323                updateView();
324                break;
325        }
326        return super.onOptionsItemSelected(menuItem);
327    }
328
329    public void onMucEditButtonClicked(View v) {
330        if (this.binding.mucEditor.getVisibility() == View.GONE) {
331            final MucOptions mucOptions = mConversation.getMucOptions();
332            this.binding.mucEditor.setVisibility(View.VISIBLE);
333            this.binding.mucDisplay.setVisibility(View.GONE);
334            this.binding.editMucNameButton.setImageResource(getThemeResource(R.attr.icon_cancel, R.drawable.ic_cancel_black_24dp));
335            final String name = mucOptions.getName();
336            this.binding.mucEditTitle.setText("");
337            final boolean owner = mucOptions.getSelf().getAffiliation().ranks(MucOptions.Affiliation.OWNER);
338            if (owner || printableValue(name)) {
339                this.binding.mucEditTitle.setVisibility(View.VISIBLE);
340                if (name != null) {
341                    this.binding.mucEditTitle.append(name);
342                }
343            } else {
344                this.binding.mucEditTitle.setVisibility(View.GONE);
345            }
346            this.binding.mucEditTitle.setEnabled(owner);
347            final String subject = mucOptions.getSubject();
348            this.binding.mucEditSubject.setText("");
349            if (subject != null) {
350                this.binding.mucEditSubject.append(subject);
351            }
352            this.binding.mucEditSubject.setEnabled(mucOptions.canChangeSubject());
353            if (!owner) {
354                this.binding.mucEditSubject.requestFocus();
355            }
356        } else {
357            String subject = this.binding.mucEditSubject.isEnabled() ? this.binding.mucEditSubject.getEditableText().toString().trim() : null;
358            String name = this.binding.mucEditTitle.isEnabled() ? this.binding.mucEditTitle.getEditableText().toString().trim() : null;
359            onMucInfoUpdated(subject, name);
360            SoftKeyboardUtils.hideSoftKeyboard(this);
361            hideEditor();
362        }
363    }
364
365    private void hideEditor() {
366        this.binding.mucEditor.setVisibility(View.GONE);
367        this.binding.mucDisplay.setVisibility(View.VISIBLE);
368        this.binding.editMucNameButton.setImageResource(getThemeResource(R.attr.icon_edit_body, R.drawable.ic_edit_black_24dp));
369    }
370
371    private void onMucInfoUpdated(String subject, String name) {
372        final MucOptions mucOptions = mConversation.getMucOptions();
373        if (mucOptions.canChangeSubject() && changed(mucOptions.getSubject(), subject)) {
374            xmppConnectionService.pushSubjectToConference(mConversation, subject);
375        }
376        if (mucOptions.getSelf().getAffiliation().ranks(MucOptions.Affiliation.OWNER) && changed(mucOptions.getName(), name)) {
377            Bundle options = new Bundle();
378            options.putString("muc#roomconfig_persistentroom", "1");
379            options.putString("muc#roomconfig_roomname", StringUtils.nullOnEmpty(name));
380            xmppConnectionService.pushConferenceConfiguration(mConversation, options, this);
381        }
382    }
383
384
385    @Override
386    protected String getShareableUri(boolean http) {
387        if (mConversation != null) {
388            if (http) {
389                return "https://conversations.im/j/" + XmppUri.lameUrlEncode(mConversation.getJid().asBareJid().toEscapedString());
390            } else {
391                return "xmpp:" + mConversation.getJid().asBareJid() + "?join";
392            }
393        } else {
394            return null;
395        }
396    }
397
398    @Override
399    public boolean onPrepareOptionsMenu(Menu menu) {
400        MenuItem menuItemSaveBookmark = menu.findItem(R.id.action_save_as_bookmark);
401        MenuItem menuItemDeleteBookmark = menu.findItem(R.id.action_delete_bookmark);
402        MenuItem menuItemAdvancedMode = menu.findItem(R.id.action_advanced_mode);
403        menuItemAdvancedMode.setChecked(mAdvancedMode);
404        if (mConversation == null) {
405            return true;
406        }
407        if (mConversation.getBookmark() != null) {
408            menuItemSaveBookmark.setVisible(false);
409            menuItemDeleteBookmark.setVisible(true);
410        } else {
411            menuItemDeleteBookmark.setVisible(false);
412            menuItemSaveBookmark.setVisible(true);
413        }
414        return true;
415    }
416
417    @Override
418    public boolean onCreateOptionsMenu(Menu menu) {
419        getMenuInflater().inflate(R.menu.muc_details, menu);
420        return super.onCreateOptionsMenu(menu);
421    }
422
423    @Override
424    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
425        Object tag = v.getTag();
426        if (tag instanceof User) {
427            getMenuInflater().inflate(R.menu.muc_details_context, menu);
428            final User user = (User) tag;
429            this.mSelectedUser = user;
430            String name;
431            final Contact contact = user.getContact();
432            if (contact != null && contact.showInRoster()) {
433                name = contact.getDisplayName();
434            } else if (user.getRealJid() != null) {
435                name = user.getRealJid().asBareJid().toString();
436            } else {
437                name = user.getName();
438            }
439            menu.setHeaderTitle(name);
440            MucDetailsContextMenuHelper.configureMucDetailsContextMenu(this, menu, mConversation, user);
441        }
442        super.onCreateContextMenu(menu, v, menuInfo);
443    }
444
445    @Override
446    public boolean onContextItemSelected(MenuItem item) {
447        if (!MucDetailsContextMenuHelper.onContextItemSelected(item, mSelectedUser, mConversation, this)) {
448            return super.onContextItemSelected(item);
449        }
450        return true;
451    }
452
453    @Override
454    public void onMediaLoaded(List<Attachment> attachments) {
455        runOnUiThread(() -> {
456            int limit = GridManager.getCurrentColumnCount(binding.media);
457            mMediaAdapter.setAttachments(attachments.subList(0, Math.min(limit,attachments.size())));
458            binding.mediaWrapper.setVisibility(attachments.size() > 0 ? View.VISIBLE : View.GONE);
459        });
460
461    }
462
463
464    protected void saveAsBookmark() {
465        xmppConnectionService.saveConversationAsBookmark(mConversation, mConversation.getMucOptions().getName());
466    }
467
468    protected void deleteBookmark() {
469        Account account = mConversation.getAccount();
470        Bookmark bookmark = mConversation.getBookmark();
471        account.getBookmarks().remove(bookmark);
472        bookmark.setConversation(null);
473        xmppConnectionService.pushBookmarks(account);
474        updateView();
475    }
476
477    @Override
478    void onBackendConnected() {
479        if (mPendingConferenceInvite != null) {
480            mPendingConferenceInvite.execute(this);
481            mPendingConferenceInvite = null;
482        }
483        if (getIntent().getAction().equals(ACTION_VIEW_MUC)) {
484            this.uuid = getIntent().getExtras().getString("uuid");
485        }
486        if (uuid != null) {
487            this.mConversation = xmppConnectionService.findConversationByUuid(uuid);
488            if (this.mConversation != null) {
489                final int limit = GridManager.getCurrentColumnCount(this.binding.media);
490                xmppConnectionService.getAttachments(this.mConversation, limit, this);
491                this.binding.showMedia.setOnClickListener((v)->MediaBrowserActivity.launch(this,mConversation));
492                updateView();
493            }
494        }
495    }
496
497    @Override
498    public void onBackPressed() {
499        if (this.binding.mucEditor.getVisibility() == View.VISIBLE) {
500            hideEditor();
501        } else {
502            super.onBackPressed();
503        }
504    }
505
506    private void updateView() {
507        invalidateOptionsMenu();
508        final MucOptions mucOptions = mConversation.getMucOptions();
509        final User self = mucOptions.getSelf();
510        String account;
511        if (Config.DOMAIN_LOCK != null) {
512            account = mConversation.getAccount().getJid().getLocal();
513        } else {
514            account = mConversation.getAccount().getJid().asBareJid().toString();
515        }
516        this.binding.editMucNameButton.setVisibility((self.getAffiliation().ranks(MucOptions.Affiliation.OWNER) || mucOptions.canChangeSubject()) ? View.VISIBLE : View.GONE);
517        this.binding.detailsAccount.setText(getString(R.string.using_account, account));
518        this.binding.jid.setText(mConversation.getJid().asBareJid().toEscapedString());
519        this.binding.yourPhoto.setImageBitmap(avatarService().get(mConversation,(int) getResources().getDimension(R.dimen.avatar_on_details_screen_size)));
520        String roomName = mucOptions.getName();
521        String subject = mucOptions.getSubject();
522        final boolean hasTitle;
523        if (printableValue(roomName)) {
524            this.binding.mucTitle.setText(EmojiWrapper.transform(roomName));
525            this.binding.mucTitle.setVisibility(View.VISIBLE);
526            hasTitle = true;
527        } else if (!printableValue(subject)) {
528            this.binding.mucTitle.setText(EmojiWrapper.transform(mConversation.getName()));
529            hasTitle = true;
530            this.binding.mucTitle.setVisibility(View.VISIBLE);
531        } else {
532            hasTitle = false;
533            this.binding.mucTitle.setVisibility(View.GONE);
534        }
535        if (printableValue(subject)) {
536            SpannableStringBuilder spannable = new SpannableStringBuilder(subject);
537            StylingHelper.format(spannable, this.binding.mucSubject.getCurrentTextColor());
538            MyLinkify.addLinks(spannable, false);
539            this.binding.mucSubject.setText(EmojiWrapper.transform(spannable));
540            this.binding.mucSubject.setTextAppearance(this,subject.length() > (hasTitle ? 128 : 196) ? R.style.TextAppearance_Conversations_Body1_Linkified : R.style.TextAppearance_Conversations_Subhead);
541            this.binding.mucSubject.setAutoLinkMask(0);
542            this.binding.mucSubject.setVisibility(View.VISIBLE);
543        } else {
544            this.binding.mucSubject.setVisibility(View.GONE);
545        }
546        this.binding.mucYourNick.setText(mucOptions.getActualNick());
547        if (mucOptions.online()) {
548            this.binding.mucMoreDetails.setVisibility(View.VISIBLE);
549            this.binding.mucSettings.setVisibility(View.VISIBLE);
550            this.binding.mucInfoMore.setVisibility(this.mAdvancedMode ? View.VISIBLE : View.GONE);
551            this.binding.mucRole.setVisibility(View.VISIBLE);
552            this.binding.mucRole.setText(getStatus(self));
553            if (mucOptions.membersOnly()) {
554                this.binding.mucConferenceType.setText(R.string.private_conference);
555            } else {
556                this.binding.mucConferenceType.setText(R.string.public_conference);
557            }
558            if (mucOptions.mamSupport()) {
559                this.binding.mucInfoMam.setText(R.string.server_info_available);
560            } else {
561                this.binding.mucInfoMam.setText(R.string.server_info_unavailable);
562            }
563            if (self.getAffiliation().ranks(MucOptions.Affiliation.OWNER)) {
564                this.binding.changeConferenceButton.setVisibility(View.VISIBLE);
565            } else {
566                this.binding.changeConferenceButton.setVisibility(View.INVISIBLE);
567            }
568        } else {
569            this.binding.mucMoreDetails.setVisibility(View.GONE);
570            this.binding.mucInfoMore.setVisibility(View.GONE);
571            this.binding.mucSettings.setVisibility(View.GONE);
572        }
573
574        int ic_notifications = getThemeResource(R.attr.icon_notifications, R.drawable.ic_notifications_black_24dp);
575        int ic_notifications_off = getThemeResource(R.attr.icon_notifications_off, R.drawable.ic_notifications_off_black_24dp);
576        int ic_notifications_paused = getThemeResource(R.attr.icon_notifications_paused, R.drawable.ic_notifications_paused_black_24dp);
577        int ic_notifications_none = getThemeResource(R.attr.icon_notifications_none, R.drawable.ic_notifications_none_black_24dp);
578
579        long mutedTill = mConversation.getLongAttribute(Conversation.ATTRIBUTE_MUTED_TILL, 0);
580        if (mutedTill == Long.MAX_VALUE) {
581            this.binding.notificationStatusText.setText(R.string.notify_never);
582            this.binding.notificationStatusButton.setImageResource(ic_notifications_off);
583        } else if (System.currentTimeMillis() < mutedTill) {
584            this.binding.notificationStatusText.setText(R.string.notify_paused);
585            this.binding.notificationStatusButton.setImageResource(ic_notifications_paused);
586        } else if (mConversation.alwaysNotify()) {
587            this.binding.notificationStatusText.setText(R.string.notify_on_all_messages);
588            this.binding.notificationStatusButton.setImageResource(ic_notifications);
589        } else {
590            this.binding.notificationStatusText.setText(R.string.notify_only_when_highlighted);
591            this.binding.notificationStatusButton.setImageResource(ic_notifications_none);
592        }
593
594        final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
595        this.binding.mucMembers.removeAllViews();
596        if (inflater == null) {
597            return;
598        }
599        final ArrayList<User> users = mucOptions.getUsers();
600        Collections.sort(users);
601        for (final User user : users) {
602            ContactBinding binding = DataBindingUtil.inflate(inflater, R.layout.contact, this.binding.mucMembers, false);
603            this.setListItemBackgroundOnView(binding.getRoot());
604            binding.getRoot().setOnClickListener(view1 -> highlightInMuc(mConversation, user.getName()));
605            registerForContextMenu(binding.getRoot());
606            binding.getRoot().setTag(user);
607            if (mAdvancedMode && user.getPgpKeyId() != 0) {
608                binding.key.setVisibility(View.VISIBLE);
609                binding.key.setOnClickListener(v -> viewPgpKey(user));
610                binding.key.setText(OpenPgpUtils.convertKeyIdToHex(user.getPgpKeyId()));
611            }
612            Contact contact = user.getContact();
613            String name = user.getName();
614            if (contact != null) {
615                binding.contactDisplayName.setText(contact.getDisplayName());
616                binding.contactJid.setText((name != null ? name + " \u2022 " : "") + getStatus(user));
617            } else {
618                binding.contactDisplayName.setText(name == null ? "" : name);
619                binding.contactJid.setText(getStatus(user));
620
621            }
622            loadAvatar(user, binding.contactPhoto);
623            if (user.getRole() == MucOptions.Role.NONE) {
624                binding.contactJid.setAlpha(INACTIVE_ALPHA);
625                binding.key.setAlpha(INACTIVE_ALPHA);
626                binding.contactDisplayName.setAlpha(INACTIVE_ALPHA);
627                binding.contactPhoto.setAlpha(INACTIVE_ALPHA);
628            }
629            this.binding.mucMembers.addView(binding.getRoot());
630            if (mConversation.getMucOptions().canInvite()) {
631                this.binding.invite.setVisibility(View.VISIBLE);
632            } else {
633                this.binding.invite.setVisibility(View.GONE);
634            }
635        }
636    }
637
638    private String getStatus(User user) {
639        if (mAdvancedMode) {
640            return getString(user.getAffiliation().getResId()) +
641                    " (" + getString(user.getRole().getResId()) + ')';
642        } else {
643            return getString(user.getAffiliation().getResId());
644        }
645    }
646
647    private void viewPgpKey(User user) {
648        PgpEngine pgp = xmppConnectionService.getPgpEngine();
649        if (pgp != null) {
650            PendingIntent intent = pgp.getIntentForKey(user.getPgpKeyId());
651            if (intent != null) {
652                try {
653                    startIntentSenderForResult(intent.getIntentSender(), 0, null, 0, 0, 0);
654                } catch (SendIntentException ignored) {
655
656                }
657            }
658        }
659    }
660
661    @Override
662    public void onAffiliationChangedSuccessful(Jid jid) {
663        refreshUi();
664    }
665
666    @Override
667    public void onAffiliationChangeFailed(Jid jid, int resId) {
668        displayToast(getString(resId, jid.asBareJid().toString()));
669    }
670
671    @Override
672    public void onRoleChangedSuccessful(String nick) {
673
674    }
675
676    @Override
677    public void onRoleChangeFailed(String nick, int resId) {
678        displayToast(getString(resId, nick));
679    }
680
681    @Override
682    public void onPushSucceeded() {
683        displayToast(getString(R.string.modified_conference_options));
684    }
685
686    @Override
687    public void onPushFailed() {
688        displayToast(getString(R.string.could_not_modify_conference_options));
689    }
690
691    private void displayToast(final String msg) {
692        runOnUiThread(() -> Toast.makeText(ConferenceDetailsActivity.this, msg, Toast.LENGTH_SHORT).show());
693    }
694
695    public void loadAvatar(User user, ImageView imageView) {
696        if (cancelPotentialWork(user, imageView)) {
697            final Bitmap bm = avatarService().get(user, getPixel(48), true);
698            if (bm != null) {
699                cancelPotentialWork(user, imageView);
700                imageView.setImageBitmap(bm);
701                imageView.setBackgroundColor(0x00000000);
702            } else {
703                String seed = user.getRealJid() != null ? user.getRealJid().asBareJid().toString() : null;
704                imageView.setBackgroundColor(UIHelper.getColorForName(seed == null ? user.getName() : seed));
705                imageView.setImageDrawable(null);
706                final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
707                final AsyncDrawable asyncDrawable = new AsyncDrawable(getResources(), null, task);
708                imageView.setImageDrawable(asyncDrawable);
709                try {
710                    task.execute(user);
711                } catch (final RejectedExecutionException ignored) {
712                }
713            }
714        }
715    }
716
717    @Override
718    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
719
720    }
721
722    @Override
723    public void onTextChanged(CharSequence s, int start, int before, int count) {
724
725    }
726
727    @Override
728    public void afterTextChanged(Editable s) {
729        if (mConversation == null) {
730            return;
731        }
732        final MucOptions mucOptions = mConversation.getMucOptions();
733        if (this.binding.mucEditor.getVisibility() == View.VISIBLE) {
734            boolean subjectChanged = changed(binding.mucEditSubject.getEditableText().toString(), mucOptions.getSubject());
735            boolean nameChanged = changed(binding.mucEditTitle.getEditableText().toString(), mucOptions.getName());
736            if (subjectChanged || nameChanged) {
737                this.binding.editMucNameButton.setImageResource(getThemeResource(R.attr.icon_save, R.drawable.ic_save_black_24dp));
738            } else {
739                this.binding.editMucNameButton.setImageResource(getThemeResource(R.attr.icon_cancel, R.drawable.ic_cancel_black_24dp));
740            }
741        }
742    }
743
744    static class AsyncDrawable extends BitmapDrawable {
745        private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
746
747        AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
748            super(res, bitmap);
749            bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask);
750        }
751
752        BitmapWorkerTask getBitmapWorkerTask() {
753            return bitmapWorkerTaskReference.get();
754        }
755    }
756
757    class BitmapWorkerTask extends AsyncTask<User, Void, Bitmap> {
758        private final WeakReference<ImageView> imageViewReference;
759        private User o = null;
760
761        private BitmapWorkerTask(ImageView imageView) {
762            imageViewReference = new WeakReference<>(imageView);
763        }
764
765        @Override
766        protected Bitmap doInBackground(User... params) {
767            this.o = params[0];
768            if (imageViewReference.get() == null) {
769                return null;
770            }
771            return avatarService().get(this.o, getPixel(48), isCancelled());
772        }
773
774        @Override
775        protected void onPostExecute(Bitmap bitmap) {
776            if (bitmap != null && !isCancelled()) {
777                final ImageView imageView = imageViewReference.get();
778                if (imageView != null) {
779                    imageView.setImageBitmap(bitmap);
780                    imageView.setBackgroundColor(0x00000000);
781                }
782            }
783        }
784    }
785
786}