1package eu.siacs.conversations.ui;
2
3import android.Manifest;
4import android.content.ActivityNotFoundException;
5import android.content.DialogInterface;
6import android.content.Intent;
7import android.content.SharedPreferences;
8import android.content.pm.PackageManager;
9import android.content.res.ColorStateList;
10import android.net.Uri;
11import android.os.Build;
12import android.os.Bundle;
13import android.preference.PreferenceManager;
14import android.provider.ContactsContract.CommonDataKinds;
15import android.provider.ContactsContract.Contacts;
16import android.provider.ContactsContract.Intents;
17import android.provider.Settings;
18import android.text.Spannable;
19import android.text.SpannableString;
20import android.text.style.RelativeSizeSpan;
21import android.view.LayoutInflater;
22import android.view.Menu;
23import android.view.MenuItem;
24import android.view.View;
25import android.view.View.OnClickListener;
26import android.widget.CompoundButton;
27import android.widget.CompoundButton.OnCheckedChangeListener;
28import android.widget.TextView;
29import android.widget.Toast;
30import androidx.annotation.NonNull;
31import androidx.core.content.ContextCompat;
32import androidx.core.view.ViewCompat;
33import androidx.databinding.DataBindingUtil;
34import com.google.android.material.color.MaterialColors;
35import com.google.android.material.dialog.MaterialAlertDialogBuilder;
36import com.google.common.base.Joiner;
37import com.google.common.collect.ImmutableList;
38import com.google.common.collect.Iterables;
39import com.google.common.primitives.Ints;
40import eu.siacs.conversations.AppSettings;
41import eu.siacs.conversations.Config;
42import eu.siacs.conversations.R;
43import eu.siacs.conversations.crypto.axolotl.AxolotlService;
44import eu.siacs.conversations.crypto.axolotl.FingerprintStatus;
45import eu.siacs.conversations.crypto.axolotl.XmppAxolotlSession;
46import eu.siacs.conversations.databinding.ActivityContactDetailsBinding;
47import eu.siacs.conversations.entities.Account;
48import eu.siacs.conversations.entities.Contact;
49import eu.siacs.conversations.entities.ListItem;
50import eu.siacs.conversations.services.AbstractQuickConversationsService;
51import eu.siacs.conversations.services.QuickConversationsService;
52import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
53import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
54import eu.siacs.conversations.ui.adapter.MediaAdapter;
55import eu.siacs.conversations.ui.interfaces.OnMediaLoaded;
56import eu.siacs.conversations.ui.util.Attachment;
57import eu.siacs.conversations.ui.util.AvatarWorkerTask;
58import eu.siacs.conversations.ui.util.GridManager;
59import eu.siacs.conversations.ui.util.JidDialog;
60import eu.siacs.conversations.ui.util.MenuDoubleTabUtil;
61import eu.siacs.conversations.utils.AccountUtils;
62import eu.siacs.conversations.utils.Compatibility;
63import eu.siacs.conversations.utils.Emoticons;
64import eu.siacs.conversations.utils.IrregularUnicodeDetector;
65import eu.siacs.conversations.utils.PhoneNumberUtilWrapper;
66import eu.siacs.conversations.utils.UIHelper;
67import eu.siacs.conversations.utils.XEP0392Helper;
68import eu.siacs.conversations.utils.XmppUri;
69import eu.siacs.conversations.xml.Namespace;
70import eu.siacs.conversations.xmpp.Jid;
71import eu.siacs.conversations.xmpp.OnKeyStatusUpdated;
72import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
73import eu.siacs.conversations.xmpp.XmppConnection;
74import eu.siacs.conversations.xmpp.manager.PresenceManager;
75import eu.siacs.conversations.xmpp.manager.RosterManager;
76import im.conversations.android.xmpp.model.stanza.Presence;
77import java.util.Collection;
78import java.util.Collections;
79import java.util.List;
80import org.openintents.openpgp.util.OpenPgpUtils;
81
82public class ContactDetailsActivity extends OmemoActivity
83 implements OnAccountUpdate,
84 OnRosterUpdate,
85 OnUpdateBlocklist,
86 OnKeyStatusUpdated,
87 OnMediaLoaded {
88 public static final String ACTION_VIEW_CONTACT = "view_contact";
89 private final int REQUEST_SYNC_CONTACTS = 0x28cf;
90 ActivityContactDetailsBinding binding;
91 private MediaAdapter mMediaAdapter;
92
93 private Contact contact;
94 private final DialogInterface.OnClickListener removeFromRoster =
95 new DialogInterface.OnClickListener() {
96
97 @Override
98 public void onClick(DialogInterface dialog, int which) {
99 xmppConnectionService.deleteContactOnServer(contact);
100 }
101 };
102 private final OnCheckedChangeListener mOnSendCheckedChange =
103 new OnCheckedChangeListener() {
104
105 @Override
106 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
107 if (isChecked) {
108 if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
109 xmppConnectionService.stopPresenceUpdatesTo(contact);
110 } else {
111 contact.setOption(Contact.Options.PREEMPTIVE_GRANT);
112 }
113 } else {
114 contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
115 final var connection = contact.getAccount().getXmppConnection();
116 connection
117 .getManager(PresenceManager.class)
118 .unsubscribed(contact.getJid().asBareJid());
119 }
120 }
121 };
122 private final OnCheckedChangeListener mOnReceiveCheckedChange =
123 new OnCheckedChangeListener() {
124
125 @Override
126 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
127 final var connection = contact.getAccount().getXmppConnection();
128 if (isChecked) {
129 connection
130 .getManager(PresenceManager.class)
131 .subscribe(contact.getJid().asBareJid());
132 } else {
133 connection
134 .getManager(PresenceManager.class)
135 .unsubscribe(contact.getJid().asBareJid());
136 }
137 }
138 };
139 private Jid accountJid;
140 private Jid contactJid;
141 private boolean showDynamicTags = false;
142 private boolean showLastSeen = false;
143 private boolean showInactiveOmemo = false;
144 private String messageFingerprint;
145
146 private void checkContactPermissionAndShowAddDialog() {
147 if (hasContactsPermission()) {
148 showAddToPhoneBookDialog();
149 } else if (QuickConversationsService.isContactListIntegration(this)) {
150 requestPermissions(
151 new String[] {Manifest.permission.READ_CONTACTS}, REQUEST_SYNC_CONTACTS);
152 }
153 }
154
155 private boolean hasContactsPermission() {
156 if (QuickConversationsService.isContactListIntegration(this)) {
157 return checkSelfPermission(Manifest.permission.READ_CONTACTS)
158 == PackageManager.PERMISSION_GRANTED;
159 } else {
160 return true;
161 }
162 }
163
164 private void showAddToPhoneBookDialog() {
165 final Jid jid = contact.getJid();
166 final boolean quicksyContact =
167 AbstractQuickConversationsService.isQuicksy()
168 && Config.QUICKSY_DOMAIN.equals(jid.getDomain())
169 && jid.getLocal() != null;
170 final String value;
171 if (quicksyContact) {
172 value = PhoneNumberUtilWrapper.toFormattedPhoneNumber(this, jid);
173 } else {
174 value = jid.toString();
175 }
176 final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this);
177 builder.setTitle(getString(R.string.save_to_contact));
178 builder.setMessage(getString(R.string.add_phone_book_text, value));
179 builder.setNegativeButton(getString(R.string.cancel), null);
180 builder.setPositiveButton(
181 getString(R.string.add),
182 (dialog, which) -> {
183 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
184 intent.setType(Contacts.CONTENT_ITEM_TYPE);
185 if (quicksyContact) {
186 intent.putExtra(Intents.Insert.PHONE, value);
187 } else {
188 intent.putExtra(Intents.Insert.IM_HANDLE, value);
189 intent.putExtra(
190 Intents.Insert.IM_PROTOCOL, CommonDataKinds.Im.PROTOCOL_JABBER);
191 // TODO for modern use we want PROTOCOL_CUSTOM and an extra field with a
192 // value of 'XMPP'
193 // however we don’t have such a field and thus have to use the legacy
194 // PROTOCOL_JABBER
195 }
196 intent.putExtra("finishActivityOnSaveCompleted", true);
197 try {
198 startActivityForResult(intent, 0);
199 } catch (ActivityNotFoundException e) {
200 Toast.makeText(
201 ContactDetailsActivity.this,
202 R.string.no_application_found_to_view_contact,
203 Toast.LENGTH_SHORT)
204 .show();
205 }
206 });
207 builder.create().show();
208 }
209
210 @Override
211 public void onRosterUpdate() {
212 refreshUi();
213 }
214
215 @Override
216 public void onAccountUpdate() {
217 refreshUi();
218 }
219
220 @Override
221 public void OnUpdateBlocklist(final Status status) {
222 refreshUi();
223 }
224
225 @Override
226 protected void refreshUiReal() {
227 invalidateOptionsMenu();
228 populateView();
229 }
230
231 @Override
232 protected String getShareableUri(boolean http) {
233 if (http) {
234 return "https://conversations.im/i/"
235 + XmppUri.lameUrlEncode(contact.getJid().asBareJid().toString());
236 } else {
237 return "xmpp:" + contact.getJid().asBareJid().toString();
238 }
239 }
240
241 @Override
242 protected void onCreate(final Bundle savedInstanceState) {
243 super.onCreate(savedInstanceState);
244 showInactiveOmemo =
245 savedInstanceState != null
246 && savedInstanceState.getBoolean("show_inactive_omemo", false);
247 if (getIntent().getAction().equals(ACTION_VIEW_CONTACT)) {
248 try {
249 this.accountJid = Jid.of(getIntent().getExtras().getString(EXTRA_ACCOUNT));
250 } catch (final IllegalArgumentException ignored) {
251 }
252 try {
253 this.contactJid = Jid.of(getIntent().getExtras().getString("contact"));
254 } catch (final IllegalArgumentException ignored) {
255 }
256 }
257 this.messageFingerprint = getIntent().getStringExtra("fingerprint");
258 this.binding = DataBindingUtil.setContentView(this, R.layout.activity_contact_details);
259 Activities.setStatusAndNavigationBarColors(this, binding.getRoot());
260
261 setSupportActionBar(binding.toolbar);
262 configureActionBar(getSupportActionBar());
263 binding.showInactiveDevices.setOnClickListener(
264 v -> {
265 showInactiveOmemo = !showInactiveOmemo;
266 populateView();
267 });
268 binding.addContactButton.setOnClickListener(v -> showAddToRosterDialog(contact));
269
270 mMediaAdapter = new MediaAdapter(this, R.dimen.media_size);
271 this.binding.media.setAdapter(mMediaAdapter);
272 GridManager.setupLayoutManager(this, this.binding.media, R.dimen.media_size);
273 }
274
275 @Override
276 public void onSaveInstanceState(final Bundle savedInstanceState) {
277 savedInstanceState.putBoolean("show_inactive_omemo", showInactiveOmemo);
278 super.onSaveInstanceState(savedInstanceState);
279 }
280
281 @Override
282 public void onStart() {
283 super.onStart();
284 final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
285 this.showDynamicTags = preferences.getBoolean(AppSettings.SHOW_DYNAMIC_TAGS, false);
286 this.showLastSeen = preferences.getBoolean("last_activity", false);
287 binding.mediaWrapper.setVisibility(
288 Compatibility.hasStoragePermission(this) ? View.VISIBLE : View.GONE);
289 mMediaAdapter.setAttachments(Collections.emptyList());
290 }
291
292 @Override
293 public void onRequestPermissionsResult(
294 int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
295 // TODO check for Camera / Scan permission
296 super.onRequestPermissionsResult(requestCode, permissions, grantResults);
297 if (grantResults.length == 0) {
298 return;
299 }
300 if (requestCode == REQUEST_SYNC_CONTACTS && xmppConnectionServiceBound) {
301 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
302 showAddToPhoneBookDialog();
303 xmppConnectionService.loadPhoneContacts();
304 xmppConnectionService.startContactObserver();
305 } else {
306 showRedirectToAppSettings();
307 }
308 }
309 }
310
311 private void showRedirectToAppSettings() {
312 final var dialogBuilder = new MaterialAlertDialogBuilder(this);
313 dialogBuilder.setTitle(R.string.save_to_contact);
314 dialogBuilder.setMessage(
315 getString(R.string.no_contacts_permission, getString(R.string.app_name)));
316 dialogBuilder.setPositiveButton(
317 R.string.continue_btn,
318 (d, w) -> {
319 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
320 Uri uri = Uri.fromParts("package", getPackageName(), null);
321 intent.setData(uri);
322 startActivity(intent);
323 });
324 dialogBuilder.setNegativeButton(R.string.cancel, null);
325 dialogBuilder.create().show();
326 }
327
328 @Override
329 public boolean onOptionsItemSelected(final MenuItem menuItem) {
330 if (MenuDoubleTabUtil.shouldIgnoreTap()) {
331 return false;
332 }
333 switch (menuItem.getItemId()) {
334 case android.R.id.home:
335 finish();
336 break;
337 case R.id.action_share_http:
338 shareLink(true);
339 break;
340 case R.id.action_share_uri:
341 shareLink(false);
342 break;
343 case R.id.action_delete_contact:
344 final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this);
345 builder.setNegativeButton(getString(R.string.cancel), null);
346 builder.setTitle(getString(R.string.action_delete_contact))
347 .setMessage(
348 JidDialog.style(
349 this,
350 R.string.remove_contact_text,
351 contact.getJid().toString()))
352 .setPositiveButton(getString(R.string.delete), removeFromRoster)
353 .create()
354 .show();
355 break;
356 case R.id.action_edit_contact:
357 final Uri systemAccount = contact.getSystemAccount();
358 if (systemAccount == null) {
359 quickEdit(
360 contact.getServerName(),
361 R.string.contact_name,
362 value -> {
363 contact.setServerName(value);
364 final var connection = contact.getAccount().getXmppConnection();
365 connection
366 .getManager(RosterManager.class)
367 .addRosterItem(contact, null);
368 populateView();
369 return null;
370 },
371 true);
372 } else {
373 Intent intent = new Intent(Intent.ACTION_EDIT);
374 intent.setDataAndType(systemAccount, Contacts.CONTENT_ITEM_TYPE);
375 intent.putExtra("finishActivityOnSaveCompleted", true);
376 try {
377 startActivity(intent);
378 } catch (ActivityNotFoundException e) {
379 Toast.makeText(
380 ContactDetailsActivity.this,
381 R.string.no_application_found_to_view_contact,
382 Toast.LENGTH_SHORT)
383 .show();
384 }
385 }
386 break;
387 case R.id.action_block, R.id.action_unblock:
388 BlockContactDialog.show(this, contact);
389 break;
390 case R.id.action_custom_notifications:
391 configureCustomNotifications(contact);
392 break;
393 }
394 return super.onOptionsItemSelected(menuItem);
395 }
396
397 private void configureCustomNotifications(final Contact contact) {
398 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
399 return;
400 }
401 final var shortcut = xmppConnectionService.getShortcutService().getShortcutInfo(contact);
402 configureCustomNotification(shortcut);
403 }
404
405 @Override
406 public boolean onCreateOptionsMenu(final Menu menu) {
407 getMenuInflater().inflate(R.menu.contact_details, menu);
408 AccountUtils.showHideMenuItems(menu);
409 final MenuItem block = menu.findItem(R.id.action_block);
410 final MenuItem unblock = menu.findItem(R.id.action_unblock);
411 final MenuItem edit = menu.findItem(R.id.action_edit_contact);
412 final MenuItem delete = menu.findItem(R.id.action_delete_contact);
413 final MenuItem customNotifications = menu.findItem(R.id.action_custom_notifications);
414 customNotifications.setVisible(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R);
415 if (contact == null) {
416 return true;
417 }
418 final XmppConnection connection = contact.getAccount().getXmppConnection();
419 if (connection != null && connection.getFeatures().blocking()) {
420 if (this.contact.isBlocked()) {
421 block.setVisible(false);
422 } else {
423 unblock.setVisible(false);
424 }
425 } else {
426 unblock.setVisible(false);
427 block.setVisible(false);
428 }
429 if (!contact.showInRoster()) {
430 edit.setVisible(false);
431 delete.setVisible(false);
432 }
433 return super.onCreateOptionsMenu(menu);
434 }
435
436 private void populateView() {
437 if (contact == null) {
438 return;
439 }
440 invalidateOptionsMenu();
441 setTitle(contact.getDisplayName());
442 if (contact.showInRoster()) {
443 binding.detailsSendPresence.setVisibility(View.VISIBLE);
444 binding.detailsReceivePresence.setVisibility(View.VISIBLE);
445 binding.addContactButton.setVisibility(View.GONE);
446 binding.detailsSendPresence.setOnCheckedChangeListener(null);
447 binding.detailsReceivePresence.setOnCheckedChangeListener(null);
448
449 Collection<String> statusMessages = contact.getPresences().getStatusMessages();
450 if (statusMessages.isEmpty()) {
451 binding.statusMessage.setVisibility(View.GONE);
452 } else if (statusMessages.size() == 1) {
453 final String message = Iterables.getOnlyElement(statusMessages);
454 binding.statusMessage.setVisibility(View.VISIBLE);
455 final Spannable span = new SpannableString(message);
456 if (Emoticons.isOnlyEmoji(message)) {
457 span.setSpan(
458 new RelativeSizeSpan(2.0f),
459 0,
460 message.length(),
461 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
462 }
463 binding.statusMessage.setText(span);
464 } else {
465 binding.statusMessage.setText(Joiner.on('\n').join(statusMessages));
466 }
467
468 if (contact.getOption(Contact.Options.FROM)) {
469 binding.detailsSendPresence.setText(R.string.send_presence_updates);
470 binding.detailsSendPresence.setChecked(true);
471 } else if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
472 binding.detailsSendPresence.setChecked(false);
473 binding.detailsSendPresence.setText(R.string.send_presence_updates);
474 } else {
475 binding.detailsSendPresence.setText(R.string.preemptively_grant);
476 binding.detailsSendPresence.setChecked(
477 contact.getOption(Contact.Options.PREEMPTIVE_GRANT));
478 }
479 if (contact.getOption(Contact.Options.TO)) {
480 binding.detailsReceivePresence.setText(R.string.receive_presence_updates);
481 binding.detailsReceivePresence.setChecked(true);
482 } else {
483 binding.detailsReceivePresence.setText(R.string.ask_for_presence_updates);
484 binding.detailsReceivePresence.setChecked(
485 contact.getOption(Contact.Options.ASKING));
486 }
487 if (contact.getAccount().isOnlineAndConnected()) {
488 binding.detailsReceivePresence.setEnabled(true);
489 binding.detailsSendPresence.setEnabled(true);
490 } else {
491 binding.detailsReceivePresence.setEnabled(false);
492 binding.detailsSendPresence.setEnabled(false);
493 }
494 binding.detailsSendPresence.setOnCheckedChangeListener(this.mOnSendCheckedChange);
495 binding.detailsReceivePresence.setOnCheckedChangeListener(this.mOnReceiveCheckedChange);
496 } else {
497 binding.addContactButton.setVisibility(View.VISIBLE);
498 binding.detailsSendPresence.setVisibility(View.GONE);
499 binding.detailsReceivePresence.setVisibility(View.GONE);
500 binding.statusMessage.setVisibility(View.GONE);
501 }
502
503 if (contact.isBlocked() && !this.showDynamicTags) {
504 binding.detailsLastSeen.setVisibility(View.VISIBLE);
505 binding.detailsLastSeen.setText(R.string.contact_blocked);
506 } else {
507 if (showLastSeen
508 && contact.getLastseen() > 0
509 && contact.getPresences().allOrNonSupport(Namespace.IDLE)) {
510 binding.detailsLastSeen.setVisibility(View.VISIBLE);
511 binding.detailsLastSeen.setText(
512 UIHelper.lastseen(
513 getApplicationContext(),
514 contact.isActive(),
515 contact.getLastseen()));
516 } else {
517 binding.detailsLastSeen.setVisibility(View.GONE);
518 }
519 }
520
521 binding.detailsContactXmppAddress.setText(
522 IrregularUnicodeDetector.style(this, contact.getJid()));
523 final String account = contact.getAccount().getJid().asBareJid().toString();
524 binding.detailsAccount.setOnClickListener(this::onDetailsAccountClicked);
525 binding.detailsAccount.setText(getString(R.string.using_account, account));
526 AvatarWorkerTask.loadAvatar(contact, binding.detailsAvatar, R.dimen.publish_avatar_size);
527 binding.detailsAvatar.setOnClickListener(this::onAvatarClicked);
528 if (QuickConversationsService.isContactListIntegration(this)) {
529 if (contact.getSystemAccount() == null) {
530 binding.addAddressBook.setText(R.string.save_to_contact);
531 } else {
532 binding.addAddressBook.setText(R.string.show_in_contacts);
533 }
534 binding.addAddressBook.setVisibility(View.VISIBLE);
535 binding.addAddressBook.setOnClickListener(this::onAddToAddressBookClick);
536 } else {
537 binding.addAddressBook.setVisibility(View.GONE);
538 }
539
540 binding.detailsContactKeys.removeAllViews();
541 boolean hasKeys = false;
542 final LayoutInflater inflater = getLayoutInflater();
543 final AxolotlService axolotlService = contact.getAccount().getAxolotlService();
544 if (Config.supportOmemo() && axolotlService != null) {
545 final Collection<XmppAxolotlSession> sessions =
546 axolotlService.findSessionsForContact(contact);
547 boolean anyActive = false;
548 for (XmppAxolotlSession session : sessions) {
549 anyActive = session.getTrust().isActive();
550 if (anyActive) {
551 break;
552 }
553 }
554 boolean skippedInactive = false;
555 boolean showsInactive = false;
556 boolean showUnverifiedWarning = false;
557 for (final XmppAxolotlSession session : sessions) {
558 final FingerprintStatus trust = session.getTrust();
559 hasKeys |= !trust.isCompromised();
560 if (!trust.isActive() && anyActive) {
561 if (showInactiveOmemo) {
562 showsInactive = true;
563 } else {
564 skippedInactive = true;
565 continue;
566 }
567 }
568 if (!trust.isCompromised()) {
569 boolean highlight = session.getFingerprint().equals(messageFingerprint);
570 addFingerprintRow(binding.detailsContactKeys, session, highlight);
571 }
572 if (trust.isUnverified()) {
573 showUnverifiedWarning = true;
574 }
575 }
576 binding.unverifiedWarning.setVisibility(
577 showUnverifiedWarning ? View.VISIBLE : View.GONE);
578 if (showsInactive || skippedInactive) {
579 binding.showInactiveDevices.setText(
580 showsInactive
581 ? R.string.hide_inactive_devices
582 : R.string.show_inactive_devices);
583 binding.showInactiveDevices.setVisibility(View.VISIBLE);
584 } else {
585 binding.showInactiveDevices.setVisibility(View.GONE);
586 }
587 } else {
588 binding.showInactiveDevices.setVisibility(View.GONE);
589 }
590 final boolean isCameraFeatureAvailable = isCameraFeatureAvailable();
591 binding.scanButton.setVisibility(
592 hasKeys && isCameraFeatureAvailable ? View.VISIBLE : View.GONE);
593 if (hasKeys) {
594 binding.scanButton.setOnClickListener((v) -> ScanActivity.scan(this));
595 }
596 if (Config.supportOpenPgp() && contact.getPgpKeyId() != 0) {
597 hasKeys = true;
598 View view =
599 inflater.inflate(
600 R.layout.item_device_fingerprint, binding.detailsContactKeys, false);
601 TextView key = view.findViewById(R.id.key);
602 TextView keyType = view.findViewById(R.id.key_type);
603 keyType.setText(R.string.openpgp_key_id);
604 if ("pgp".equals(messageFingerprint)) {
605 keyType.setTextColor(
606 MaterialColors.getColor(
607 keyType, com.google.android.material.R.attr.colorPrimaryVariant));
608 }
609 key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
610 final OnClickListener openKey = v -> launchOpenKeyChain(contact.getPgpKeyId());
611 view.setOnClickListener(openKey);
612 key.setOnClickListener(openKey);
613 keyType.setOnClickListener(openKey);
614 binding.detailsContactKeys.addView(view);
615 }
616 binding.keysWrapper.setVisibility(hasKeys ? View.VISIBLE : View.GONE);
617
618 final List<ListItem.Tag> tagList = contact.getTags(this);
619 final boolean hasMetaTags =
620 contact.isBlocked() || contact.getShownStatus() != Presence.Availability.OFFLINE;
621 if ((tagList.isEmpty() && !hasMetaTags) || !this.showDynamicTags) {
622 binding.tags.setVisibility(View.GONE);
623 } else {
624 binding.tags.setVisibility(View.VISIBLE);
625 binding.tags.removeViews(1, binding.tags.getChildCount() - 1);
626 final ImmutableList.Builder<Integer> viewIdBuilder = new ImmutableList.Builder<>();
627 for (final ListItem.Tag tag : tagList) {
628 final String name = tag.getName();
629 final TextView tv =
630 (TextView) inflater.inflate(R.layout.item_tag, binding.tags, false);
631 tv.setText(name);
632 tv.setBackgroundTintList(
633 ColorStateList.valueOf(
634 MaterialColors.harmonizeWithPrimary(
635 this, XEP0392Helper.rgbFromNick(name))));
636 final int id = ViewCompat.generateViewId();
637 tv.setId(id);
638 viewIdBuilder.add(id);
639 binding.tags.addView(tv);
640 }
641 if (contact.isBlocked()) {
642 final TextView tv =
643 (TextView) inflater.inflate(R.layout.item_tag, binding.tags, false);
644 tv.setText(R.string.blocked);
645 tv.setBackgroundTintList(
646 ColorStateList.valueOf(
647 MaterialColors.harmonizeWithPrimary(
648 tv.getContext(),
649 ContextCompat.getColor(
650 tv.getContext(), R.color.gray_800))));
651 final int id = ViewCompat.generateViewId();
652 tv.setId(id);
653 viewIdBuilder.add(id);
654 binding.tags.addView(tv);
655 } else {
656 final Presence.Availability status = contact.getShownStatus();
657 if (status != Presence.Availability.OFFLINE) {
658 final TextView tv =
659 (TextView) inflater.inflate(R.layout.item_tag, binding.tags, false);
660 UIHelper.setStatus(tv, status);
661 final int id = ViewCompat.generateViewId();
662 tv.setId(id);
663 viewIdBuilder.add(id);
664 binding.tags.addView(tv);
665 }
666 }
667 binding.flowWidget.setReferencedIds(Ints.toArray(viewIdBuilder.build()));
668 }
669 }
670
671 private void onDetailsAccountClicked(final View view) {
672 final var contact = this.contact;
673 if (contact == null) {
674 return;
675 }
676 switchToAccount(contact.getAccount());
677 }
678
679 private void onAvatarClicked(final View view) {
680 final var contact = this.contact;
681 if (contact == null) {
682 return;
683 }
684 final var avatar = contact.getAvatar();
685 if (avatar == null) {
686 return;
687 }
688 final var intent = new Intent(this, ViewProfilePictureActivity.class);
689 intent.setData(Uri.fromParts("avatar", avatar, null));
690 intent.putExtra(ViewProfilePictureActivity.EXTRA_DISPLAY_NAME, contact.getDisplayName());
691 startActivity(intent);
692 }
693
694 private void onAddToAddressBookClick(final View view) {
695 if (QuickConversationsService.isContactListIntegration(this)) {
696 final Uri systemAccount = contact.getSystemAccount();
697 if (systemAccount == null) {
698 checkContactPermissionAndShowAddDialog();
699 } else {
700 final Intent intent = new Intent(Intent.ACTION_VIEW);
701 intent.setData(systemAccount);
702 try {
703 startActivity(intent);
704 } catch (final ActivityNotFoundException e) {
705 Toast.makeText(
706 this,
707 R.string.no_application_found_to_view_contact,
708 Toast.LENGTH_SHORT)
709 .show();
710 }
711 }
712 } else {
713 Toast.makeText(
714 this,
715 R.string.contact_list_integration_not_available,
716 Toast.LENGTH_SHORT)
717 .show();
718 }
719 }
720
721 public void onBackendConnected() {
722 if (accountJid != null && contactJid != null) {
723 Account account = xmppConnectionService.findAccountByJid(accountJid);
724 if (account == null) {
725 return;
726 }
727 this.contact = account.getRoster().getContact(contactJid);
728 if (mPendingFingerprintVerificationUri != null) {
729 processFingerprintVerification(mPendingFingerprintVerificationUri);
730 mPendingFingerprintVerificationUri = null;
731 }
732
733 if (Compatibility.hasStoragePermission(this)) {
734 final int limit = GridManager.getCurrentColumnCount(this.binding.media);
735 xmppConnectionService.getAttachments(
736 account, contact.getJid().asBareJid(), limit, this);
737 this.binding.showMedia.setOnClickListener(
738 (v) -> MediaBrowserActivity.launch(this, contact));
739 }
740 populateView();
741 }
742 }
743
744 @Override
745 public void onKeyStatusUpdated(AxolotlService.FetchStatus report) {
746 refreshUi();
747 }
748
749 @Override
750 protected void processFingerprintVerification(XmppUri uri) {
751 if (contact != null
752 && contact.getJid().asBareJid().equals(uri.getJid())
753 && uri.hasFingerprints()) {
754 if (xmppConnectionService.verifyFingerprints(contact, uri.getFingerprints())) {
755 Toast.makeText(this, R.string.verified_fingerprints, Toast.LENGTH_SHORT).show();
756 }
757 } else {
758 Toast.makeText(this, R.string.invalid_barcode, Toast.LENGTH_SHORT).show();
759 }
760 }
761
762 @Override
763 public void onMediaLoaded(List<Attachment> attachments) {
764 runOnUiThread(
765 () -> {
766 int limit = GridManager.getCurrentColumnCount(binding.media);
767 mMediaAdapter.setAttachments(
768 attachments.subList(0, Math.min(limit, attachments.size())));
769 binding.mediaWrapper.setVisibility(
770 attachments.size() > 0 ? View.VISIBLE : View.GONE);
771 });
772 }
773}