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