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(
527 contact, binding.detailsAvatar, R.dimen.avatar_on_details_screen_size);
528 binding.detailsAvatar.setOnClickListener(this::onAvatarClicked);
529 if (QuickConversationsService.isContactListIntegration(this)) {
530 if (contact.getSystemAccount() == null) {
531 binding.addAddressBook.setText(R.string.save_to_contact);
532 } else {
533 binding.addAddressBook.setText(R.string.show_in_contacts);
534 }
535 binding.addAddressBook.setVisibility(View.VISIBLE);
536 binding.addAddressBook.setOnClickListener(this::onAddToAddressBookClick);
537 } else {
538 binding.addAddressBook.setVisibility(View.GONE);
539 }
540
541 binding.detailsContactKeys.removeAllViews();
542 boolean hasKeys = false;
543 final LayoutInflater inflater = getLayoutInflater();
544 final AxolotlService axolotlService = contact.getAccount().getAxolotlService();
545 if (Config.supportOmemo() && axolotlService != null) {
546 final Collection<XmppAxolotlSession> sessions =
547 axolotlService.findSessionsForContact(contact);
548 boolean anyActive = false;
549 for (XmppAxolotlSession session : sessions) {
550 anyActive = session.getTrust().isActive();
551 if (anyActive) {
552 break;
553 }
554 }
555 boolean skippedInactive = false;
556 boolean showsInactive = false;
557 boolean showUnverifiedWarning = false;
558 for (final XmppAxolotlSession session : sessions) {
559 final FingerprintStatus trust = session.getTrust();
560 hasKeys |= !trust.isCompromised();
561 if (!trust.isActive() && anyActive) {
562 if (showInactiveOmemo) {
563 showsInactive = true;
564 } else {
565 skippedInactive = true;
566 continue;
567 }
568 }
569 if (!trust.isCompromised()) {
570 boolean highlight = session.getFingerprint().equals(messageFingerprint);
571 addFingerprintRow(binding.detailsContactKeys, session, highlight);
572 }
573 if (trust.isUnverified()) {
574 showUnverifiedWarning = true;
575 }
576 }
577 binding.unverifiedWarning.setVisibility(
578 showUnverifiedWarning ? View.VISIBLE : View.GONE);
579 if (showsInactive || skippedInactive) {
580 binding.showInactiveDevices.setText(
581 showsInactive
582 ? R.string.hide_inactive_devices
583 : R.string.show_inactive_devices);
584 binding.showInactiveDevices.setVisibility(View.VISIBLE);
585 } else {
586 binding.showInactiveDevices.setVisibility(View.GONE);
587 }
588 } else {
589 binding.showInactiveDevices.setVisibility(View.GONE);
590 }
591 final boolean isCameraFeatureAvailable = isCameraFeatureAvailable();
592 binding.scanButton.setVisibility(
593 hasKeys && isCameraFeatureAvailable ? View.VISIBLE : View.GONE);
594 if (hasKeys) {
595 binding.scanButton.setOnClickListener((v) -> ScanActivity.scan(this));
596 }
597 if (Config.supportOpenPgp() && contact.getPgpKeyId() != 0) {
598 hasKeys = true;
599 View view =
600 inflater.inflate(
601 R.layout.item_device_fingerprint, binding.detailsContactKeys, false);
602 TextView key = view.findViewById(R.id.key);
603 TextView keyType = view.findViewById(R.id.key_type);
604 keyType.setText(R.string.openpgp_key_id);
605 if ("pgp".equals(messageFingerprint)) {
606 keyType.setTextColor(
607 MaterialColors.getColor(
608 keyType, com.google.android.material.R.attr.colorPrimaryVariant));
609 }
610 key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
611 final OnClickListener openKey = v -> launchOpenKeyChain(contact.getPgpKeyId());
612 view.setOnClickListener(openKey);
613 key.setOnClickListener(openKey);
614 keyType.setOnClickListener(openKey);
615 binding.detailsContactKeys.addView(view);
616 }
617 binding.keysWrapper.setVisibility(hasKeys ? View.VISIBLE : View.GONE);
618
619 final List<ListItem.Tag> tagList = contact.getTags(this);
620 final boolean hasMetaTags =
621 contact.isBlocked() || contact.getShownStatus() != Presence.Availability.OFFLINE;
622 if ((tagList.isEmpty() && !hasMetaTags) || !this.showDynamicTags) {
623 binding.tags.setVisibility(View.GONE);
624 } else {
625 binding.tags.setVisibility(View.VISIBLE);
626 binding.tags.removeViews(1, binding.tags.getChildCount() - 1);
627 final ImmutableList.Builder<Integer> viewIdBuilder = new ImmutableList.Builder<>();
628 for (final ListItem.Tag tag : tagList) {
629 final String name = tag.getName();
630 final TextView tv =
631 (TextView) inflater.inflate(R.layout.item_tag, binding.tags, false);
632 tv.setText(name);
633 tv.setBackgroundTintList(
634 ColorStateList.valueOf(
635 MaterialColors.harmonizeWithPrimary(
636 this, XEP0392Helper.rgbFromNick(name))));
637 final int id = ViewCompat.generateViewId();
638 tv.setId(id);
639 viewIdBuilder.add(id);
640 binding.tags.addView(tv);
641 }
642 if (contact.isBlocked()) {
643 final TextView tv =
644 (TextView) inflater.inflate(R.layout.item_tag, binding.tags, false);
645 tv.setText(R.string.blocked);
646 tv.setBackgroundTintList(
647 ColorStateList.valueOf(
648 MaterialColors.harmonizeWithPrimary(
649 tv.getContext(),
650 ContextCompat.getColor(
651 tv.getContext(), R.color.gray_800))));
652 final int id = ViewCompat.generateViewId();
653 tv.setId(id);
654 viewIdBuilder.add(id);
655 binding.tags.addView(tv);
656 } else {
657 final Presence.Availability status = contact.getShownStatus();
658 if (status != Presence.Availability.OFFLINE) {
659 final TextView tv =
660 (TextView) inflater.inflate(R.layout.item_tag, binding.tags, false);
661 UIHelper.setStatus(tv, status);
662 final int id = ViewCompat.generateViewId();
663 tv.setId(id);
664 viewIdBuilder.add(id);
665 binding.tags.addView(tv);
666 }
667 }
668 binding.flowWidget.setReferencedIds(Ints.toArray(viewIdBuilder.build()));
669 }
670 }
671
672 private void onDetailsAccountClicked(final View view) {
673 final var contact = this.contact;
674 if (contact == null) {
675 return;
676 }
677 switchToAccount(contact.getAccount());
678 }
679
680 private void onAvatarClicked(final View view) {
681 final var contact = this.contact;
682 if (contact == null) {
683 return;
684 }
685 final var avatar = contact.getAvatar();
686 if (avatar == null) {
687 return;
688 }
689 final var intent = new Intent(this, ViewProfilePictureActivity.class);
690 intent.setData(Uri.fromParts("avatar", avatar, null));
691 intent.putExtra(ViewProfilePictureActivity.EXTRA_DISPLAY_NAME, contact.getDisplayName());
692 startActivity(intent);
693 }
694
695 private void onAddToAddressBookClick(final View view) {
696 if (QuickConversationsService.isContactListIntegration(this)) {
697 final Uri systemAccount = contact.getSystemAccount();
698 if (systemAccount == null) {
699 checkContactPermissionAndShowAddDialog();
700 } else {
701 final Intent intent = new Intent(Intent.ACTION_VIEW);
702 intent.setData(systemAccount);
703 try {
704 startActivity(intent);
705 } catch (final ActivityNotFoundException e) {
706 Toast.makeText(
707 this,
708 R.string.no_application_found_to_view_contact,
709 Toast.LENGTH_SHORT)
710 .show();
711 }
712 }
713 } else {
714 Toast.makeText(
715 this,
716 R.string.contact_list_integration_not_available,
717 Toast.LENGTH_SHORT)
718 .show();
719 }
720 }
721
722 public void onBackendConnected() {
723 if (accountJid != null && contactJid != null) {
724 Account account = xmppConnectionService.findAccountByJid(accountJid);
725 if (account == null) {
726 return;
727 }
728 this.contact = account.getRoster().getContact(contactJid);
729 if (mPendingFingerprintVerificationUri != null) {
730 processFingerprintVerification(mPendingFingerprintVerificationUri);
731 mPendingFingerprintVerificationUri = null;
732 }
733
734 if (Compatibility.hasStoragePermission(this)) {
735 final int limit = GridManager.getCurrentColumnCount(this.binding.media);
736 xmppConnectionService.getAttachments(
737 account, contact.getJid().asBareJid(), limit, this);
738 this.binding.showMedia.setOnClickListener(
739 (v) -> MediaBrowserActivity.launch(this, contact));
740 }
741 populateView();
742 }
743 }
744
745 @Override
746 public void onKeyStatusUpdated(AxolotlService.FetchStatus report) {
747 refreshUi();
748 }
749
750 @Override
751 protected void processFingerprintVerification(XmppUri uri) {
752 if (contact != null
753 && contact.getJid().asBareJid().equals(uri.getJid())
754 && uri.hasFingerprints()) {
755 if (xmppConnectionService.verifyFingerprints(contact, uri.getFingerprints())) {
756 Toast.makeText(this, R.string.verified_fingerprints, Toast.LENGTH_SHORT).show();
757 }
758 } else {
759 Toast.makeText(this, R.string.invalid_barcode, Toast.LENGTH_SHORT).show();
760 }
761 }
762
763 @Override
764 public void onMediaLoaded(List<Attachment> attachments) {
765 runOnUiThread(
766 () -> {
767 int limit = GridManager.getCurrentColumnCount(binding.media);
768 mMediaAdapter.setAttachments(
769 attachments.subList(0, Math.min(limit, attachments.size())));
770 binding.mediaWrapper.setVisibility(
771 attachments.size() > 0 ? View.VISIBLE : View.GONE);
772 });
773 }
774}