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