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