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