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