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