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.net.Uri;
10import android.os.Build;
11import android.os.Bundle;
12import android.preference.PreferenceManager;
13import android.provider.ContactsContract.CommonDataKinds;
14import android.provider.ContactsContract.Contacts;
15import android.provider.ContactsContract.Intents;
16import android.text.Spannable;
17import android.text.SpannableString;
18import android.text.style.RelativeSizeSpan;
19import android.view.LayoutInflater;
20import android.view.Menu;
21import android.view.MenuItem;
22import android.view.View;
23import android.view.View.OnClickListener;
24import android.widget.ArrayAdapter;
25import android.widget.CompoundButton;
26import android.widget.CompoundButton.OnCheckedChangeListener;
27import android.widget.EditText;
28import android.widget.TextView;
29import android.widget.Toast;
30
31import androidx.annotation.NonNull;
32import androidx.appcompat.app.AlertDialog;
33import androidx.databinding.DataBindingUtil;
34
35import org.openintents.openpgp.util.OpenPgpUtils;
36
37import java.util.ArrayList;
38import java.util.Collection;
39import java.util.Collections;
40import java.util.Comparator;
41import java.util.List;
42import java.util.Map;
43import java.util.stream.Collectors;
44
45import eu.siacs.conversations.Config;
46import eu.siacs.conversations.R;
47import eu.siacs.conversations.crypto.axolotl.AxolotlService;
48import eu.siacs.conversations.crypto.axolotl.FingerprintStatus;
49import eu.siacs.conversations.crypto.axolotl.XmppAxolotlSession;
50import eu.siacs.conversations.databinding.ActivityContactDetailsBinding;
51import eu.siacs.conversations.entities.Account;
52import eu.siacs.conversations.entities.Bookmark;
53import eu.siacs.conversations.entities.Contact;
54import eu.siacs.conversations.entities.ListItem;
55import eu.siacs.conversations.services.AbstractQuickConversationsService;
56import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
57import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
58import eu.siacs.conversations.ui.adapter.MediaAdapter;
59import eu.siacs.conversations.ui.interfaces.OnMediaLoaded;
60import eu.siacs.conversations.ui.util.Attachment;
61import eu.siacs.conversations.ui.util.AvatarWorkerTask;
62import eu.siacs.conversations.ui.util.GridManager;
63import eu.siacs.conversations.ui.util.JidDialog;
64import eu.siacs.conversations.ui.util.MenuDoubleTabUtil;
65import eu.siacs.conversations.utils.AccountUtils;
66import eu.siacs.conversations.utils.Compatibility;
67import eu.siacs.conversations.utils.Emoticons;
68import eu.siacs.conversations.utils.IrregularUnicodeDetector;
69import eu.siacs.conversations.utils.PhoneNumberUtilWrapper;
70import eu.siacs.conversations.utils.UIHelper;
71import eu.siacs.conversations.utils.XmppUri;
72import eu.siacs.conversations.xml.Namespace;
73import eu.siacs.conversations.xmpp.Jid;
74import eu.siacs.conversations.xmpp.OnKeyStatusUpdated;
75import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
76import eu.siacs.conversations.xmpp.XmppConnection;
77
78public class ContactDetailsActivity extends OmemoActivity implements OnAccountUpdate, OnRosterUpdate, OnUpdateBlocklist, OnKeyStatusUpdated, OnMediaLoaded {
79 public static final String ACTION_VIEW_CONTACT = "view_contact";
80 private final int REQUEST_SYNC_CONTACTS = 0x28cf;
81 ActivityContactDetailsBinding binding;
82 private MediaAdapter mMediaAdapter;
83 protected MenuItem edit = null;
84 protected MenuItem save = null;
85
86 private Contact contact;
87 private final DialogInterface.OnClickListener removeFromRoster = new DialogInterface.OnClickListener() {
88
89 @Override
90 public void onClick(DialogInterface dialog, int which) {
91 xmppConnectionService.deleteContactOnServer(contact);
92 }
93 };
94 private final OnCheckedChangeListener mOnSendCheckedChange = new OnCheckedChangeListener() {
95
96 @Override
97 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
98 if (isChecked) {
99 if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
100 xmppConnectionService.stopPresenceUpdatesTo(contact);
101 } else {
102 contact.setOption(Contact.Options.PREEMPTIVE_GRANT);
103 }
104 } else {
105 contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
106 xmppConnectionService.sendPresencePacket(contact.getAccount(), xmppConnectionService.getPresenceGenerator().stopPresenceUpdatesTo(contact));
107 }
108 }
109 };
110 private final OnCheckedChangeListener mOnReceiveCheckedChange = new OnCheckedChangeListener() {
111
112 @Override
113 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
114 if (isChecked) {
115 xmppConnectionService.sendPresencePacket(contact.getAccount(), xmppConnectionService.getPresenceGenerator().requestPresenceUpdatesFrom(contact));
116 } else {
117 xmppConnectionService.sendPresencePacket(contact.getAccount(), xmppConnectionService.getPresenceGenerator().stopPresenceUpdatesFrom(contact));
118 }
119 }
120 };
121 private Jid accountJid;
122 private Jid contactJid;
123 private boolean showDynamicTags = false;
124 private boolean showLastSeen = false;
125 private boolean showInactiveOmemo = false;
126 private String messageFingerprint;
127
128 private void checkContactPermissionAndShowAddDialog() {
129 if (hasContactsPermission()) {
130 showAddToPhoneBookDialog();
131 } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
132 requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_SYNC_CONTACTS);
133 }
134 }
135
136 private boolean hasContactsPermission() {
137 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
138 return checkSelfPermission(Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED;
139 } else {
140 return true;
141 }
142 }
143
144 private void showAddToPhoneBookDialog() {
145 final Jid jid = contact.getJid();
146 final boolean quicksyContact = AbstractQuickConversationsService.isQuicksy()
147 && Config.QUICKSY_DOMAIN.equals(jid.getDomain())
148 && jid.getLocal() != null;
149 final String value;
150 if (quicksyContact) {
151 value = PhoneNumberUtilWrapper.toFormattedPhoneNumber(this, jid);
152 } else {
153 value = jid.toEscapedString();
154 }
155 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
156 builder.setTitle(getString(R.string.action_add_phone_book));
157 builder.setMessage(getString(R.string.add_phone_book_text, value));
158 builder.setNegativeButton(getString(R.string.cancel), null);
159 builder.setPositiveButton(getString(R.string.add), (dialog, which) -> {
160 final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
161 intent.setType(Contacts.CONTENT_ITEM_TYPE);
162 if (quicksyContact) {
163 intent.putExtra(Intents.Insert.PHONE, value);
164 } else {
165 intent.putExtra(Intents.Insert.IM_HANDLE, value);
166 intent.putExtra(Intents.Insert.IM_PROTOCOL, CommonDataKinds.Im.PROTOCOL_JABBER);
167 //TODO for modern use we want PROTOCOL_CUSTOM and an extra field with a value of 'XMPP'
168 // however we don’t have such a field and thus have to use the legacy PROTOCOL_JABBER
169 }
170 intent.putExtra("finishActivityOnSaveCompleted", true);
171 try {
172 startActivityForResult(intent, 0);
173 } catch (ActivityNotFoundException e) {
174 Toast.makeText(ContactDetailsActivity.this, R.string.no_application_found_to_view_contact, Toast.LENGTH_SHORT).show();
175 }
176 });
177 builder.create().show();
178 }
179
180 @Override
181 public void onRosterUpdate() {
182 refreshUi();
183 }
184
185 @Override
186 public void onAccountUpdate() {
187 refreshUi();
188 }
189
190 @Override
191 public void OnUpdateBlocklist(final Status status) {
192 refreshUi();
193 }
194
195 @Override
196 protected void refreshUiReal() {
197 invalidateOptionsMenu();
198 populateView();
199 }
200
201 @Override
202 protected String getShareableUri(boolean http) {
203 if (http) {
204 return "https://conversations.im/i/" + XmppUri.lameUrlEncode(contact.getJid().asBareJid().toEscapedString());
205 } else {
206 return "xmpp:" + contact.getJid().asBareJid().toEscapedString();
207 }
208 }
209
210 @Override
211 protected void onCreate(final Bundle savedInstanceState) {
212 super.onCreate(savedInstanceState);
213 showInactiveOmemo = savedInstanceState != null && savedInstanceState.getBoolean("show_inactive_omemo", false);
214 if (getIntent().getAction().equals(ACTION_VIEW_CONTACT)) {
215 try {
216 this.accountJid = Jid.ofEscaped(getIntent().getExtras().getString(EXTRA_ACCOUNT));
217 } catch (final IllegalArgumentException ignored) {
218 }
219 try {
220 this.contactJid = Jid.ofEscaped(getIntent().getExtras().getString("contact"));
221 } catch (final IllegalArgumentException ignored) {
222 }
223 }
224 this.messageFingerprint = getIntent().getStringExtra("fingerprint");
225 this.binding = DataBindingUtil.setContentView(this, R.layout.activity_contact_details);
226
227 setSupportActionBar(binding.toolbar);
228 configureActionBar(getSupportActionBar());
229 binding.showInactiveDevices.setOnClickListener(v -> {
230 showInactiveOmemo = !showInactiveOmemo;
231 populateView();
232 });
233 binding.addContactButton.setOnClickListener(v -> showAddToRosterDialog(contact));
234
235 mMediaAdapter = new MediaAdapter(this, R.dimen.media_size);
236 this.binding.media.setAdapter(mMediaAdapter);
237 GridManager.setupLayoutManager(this, this.binding.media, R.dimen.media_size);
238 }
239
240 @Override
241 public void onSaveInstanceState(final Bundle savedInstanceState) {
242 savedInstanceState.putBoolean("show_inactive_omemo", showInactiveOmemo);
243 super.onSaveInstanceState(savedInstanceState);
244 }
245
246 @Override
247 public void onStart() {
248 super.onStart();
249 final int theme = findTheme();
250 if (this.mTheme != theme) {
251 recreate();
252 } else {
253 final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
254 this.showDynamicTags = preferences.getBoolean(SettingsActivity.SHOW_DYNAMIC_TAGS, getResources().getBoolean(R.bool.show_dynamic_tags));
255 this.showLastSeen = preferences.getBoolean("last_activity", false);
256 }
257 binding.mediaWrapper.setVisibility(Compatibility.hasStoragePermission(this) ? View.VISIBLE : View.GONE);
258 mMediaAdapter.setAttachments(Collections.emptyList());
259 }
260
261 @Override
262 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
263 super.onRequestPermissionsResult(requestCode, permissions, grantResults);
264 if (grantResults.length > 0)
265 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
266 if (requestCode == REQUEST_SYNC_CONTACTS && xmppConnectionServiceBound) {
267 showAddToPhoneBookDialog();
268 xmppConnectionService.loadPhoneContacts();
269 xmppConnectionService.startContactObserver();
270 }
271 }
272 }
273
274 protected void saveEdits() {
275 binding.editTags.setVisibility(View.GONE);
276 if (edit != null) {
277 EditText text = edit.getActionView().findViewById(R.id.search_field);
278 contact.setServerName(text.getText().toString());
279 contact.setGroups(binding.editTags.getObjects().stream().map(tag -> tag.getName()).collect(Collectors.toList()));
280 ContactDetailsActivity.this.xmppConnectionService.pushContactToServer(contact);
281 populateView();
282 edit.collapseActionView();
283 }
284 if (save != null) save.setVisible(false);
285 }
286
287 @Override
288 public boolean onOptionsItemSelected(final MenuItem menuItem) {
289 if (MenuDoubleTabUtil.shouldIgnoreTap()) {
290 return false;
291 }
292 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
293 builder.setNegativeButton(getString(R.string.cancel), null);
294 switch (menuItem.getItemId()) {
295 case android.R.id.home:
296 finish();
297 break;
298 case R.id.action_share_http:
299 shareLink(true);
300 break;
301 case R.id.action_share_uri:
302 shareLink(false);
303 break;
304 case R.id.action_delete_contact:
305 builder.setTitle(getString(R.string.action_delete_contact))
306 .setMessage(JidDialog.style(this, R.string.remove_contact_text, contact.getJid().toEscapedString()))
307 .setPositiveButton(getString(R.string.delete),
308 removeFromRoster).create().show();
309 break;
310 case R.id.action_save:
311 saveEdits();
312 break;
313 case R.id.action_edit_contact:
314 Uri systemAccount = contact.getSystemAccount();
315 if (systemAccount == null) {
316 menuItem.expandActionView();
317 EditText text = menuItem.getActionView().findViewById(R.id.search_field);
318 text.setOnEditorActionListener((v, actionId, event) -> {
319 saveEdits();
320 return true;
321 });
322 text.setText(contact.getServerName());
323 text.requestFocus();
324 binding.tags.setVisibility(View.GONE);
325 binding.editTags.clearSync();
326 for (final ListItem.Tag group : contact.getGroupTags()) {
327 binding.editTags.addObjectSync(group);
328 }
329 ArrayList<ListItem.Tag> tags = new ArrayList<>();
330 for (final Account account : xmppConnectionService.getAccounts()) {
331 for (Contact contact : account.getRoster().getContacts()) {
332 tags.addAll(contact.getTags(this));
333 }
334 for (Bookmark bookmark : account.getBookmarks()) {
335 tags.addAll(bookmark.getTags(this));
336 }
337 }
338 Comparator<Map.Entry<ListItem.Tag,Integer>> sortTagsBy = Map.Entry.comparingByValue(Comparator.reverseOrder());
339 sortTagsBy = sortTagsBy.thenComparing(entry -> entry.getKey().getName());
340
341 ArrayAdapter<ListItem.Tag> adapter = new ArrayAdapter<>(
342 this,
343 android.R.layout.simple_list_item_1,
344 tags.stream()
345 .collect(Collectors.toMap((x) -> x, (t) -> 1, (c1, c2) -> c1 + c2))
346 .entrySet().stream()
347 .sorted(sortTagsBy)
348 .map(e -> e.getKey()).collect(Collectors.toList())
349 );
350 binding.editTags.setAdapter(adapter);
351 binding.editTags.setVisibility(View.VISIBLE);
352 if (save != null) save.setVisible(true);
353 } else {
354 menuItem.collapseActionView();
355 if (save != null) save.setVisible(false);
356 Intent intent = new Intent(Intent.ACTION_EDIT);
357 intent.setDataAndType(systemAccount, Contacts.CONTENT_ITEM_TYPE);
358 intent.putExtra("finishActivityOnSaveCompleted", true);
359 try {
360 startActivity(intent);
361 } catch (ActivityNotFoundException e) {
362 Toast.makeText(ContactDetailsActivity.this, R.string.no_application_found_to_view_contact, Toast.LENGTH_SHORT).show();
363 }
364
365 }
366 break;
367 case R.id.action_block:
368 BlockContactDialog.show(this, contact);
369 break;
370 case R.id.action_unblock:
371 BlockContactDialog.show(this, contact);
372 break;
373 }
374 return super.onOptionsItemSelected(menuItem);
375 }
376
377 @Override
378 public boolean onCreateOptionsMenu(final Menu menu) {
379 getMenuInflater().inflate(R.menu.contact_details, menu);
380 AccountUtils.showHideMenuItems(menu);
381 edit = menu.findItem(R.id.action_edit_contact);
382 save = menu.findItem(R.id.action_save);
383 MenuItem block = menu.findItem(R.id.action_block);
384 MenuItem unblock = menu.findItem(R.id.action_unblock);
385 MenuItem edit = menu.findItem(R.id.action_edit_contact);
386 MenuItem delete = menu.findItem(R.id.action_delete_contact);
387 if (contact == null) {
388 return true;
389 }
390 final XmppConnection connection = contact.getAccount().getXmppConnection();
391 if (connection != null && connection.getFeatures().blocking()) {
392 if (this.contact.isBlocked()) {
393 block.setVisible(false);
394 } else {
395 unblock.setVisible(false);
396 }
397 } else {
398 unblock.setVisible(false);
399 block.setVisible(false);
400 }
401 if (!contact.showInRoster()) {
402 edit.setVisible(false);
403 delete.setVisible(false);
404 }
405 return super.onCreateOptionsMenu(menu);
406 }
407
408 private void populateView() {
409 if (contact == null) {
410 return;
411 }
412 invalidateOptionsMenu();
413 setTitle(contact.getDisplayName());
414 if (contact.showInRoster()) {
415 binding.detailsSendPresence.setVisibility(View.VISIBLE);
416 binding.detailsReceivePresence.setVisibility(View.VISIBLE);
417 binding.addContactButton.setVisibility(View.GONE);
418 binding.detailsSendPresence.setOnCheckedChangeListener(null);
419 binding.detailsReceivePresence.setOnCheckedChangeListener(null);
420
421 List<String> statusMessages = contact.getPresences().getStatusMessages();
422 if (statusMessages.size() == 0) {
423 binding.statusMessage.setVisibility(View.GONE);
424 } else if (statusMessages.size() == 1) {
425 final String message = statusMessages.get(0);
426 binding.statusMessage.setVisibility(View.VISIBLE);
427 final Spannable span = new SpannableString(message);
428 if (Emoticons.isOnlyEmoji(message)) {
429 span.setSpan(new RelativeSizeSpan(2.0f), 0, message.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
430 }
431 binding.statusMessage.setText(span);
432 } else {
433 StringBuilder builder = new StringBuilder();
434 binding.statusMessage.setVisibility(View.VISIBLE);
435 int s = statusMessages.size();
436 for (int i = 0; i < s; ++i) {
437 builder.append(statusMessages.get(i));
438 if (i < s - 1) {
439 builder.append("\n");
440 }
441 }
442 binding.statusMessage.setText(builder);
443 }
444
445 if (contact.getOption(Contact.Options.FROM)) {
446 binding.detailsSendPresence.setText(R.string.send_presence_updates);
447 binding.detailsSendPresence.setChecked(true);
448 } else if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
449 binding.detailsSendPresence.setChecked(false);
450 binding.detailsSendPresence.setText(R.string.send_presence_updates);
451 } else {
452 binding.detailsSendPresence.setText(R.string.preemptively_grant);
453 binding.detailsSendPresence.setChecked(contact.getOption(Contact.Options.PREEMPTIVE_GRANT));
454 }
455 if (contact.getOption(Contact.Options.TO)) {
456 binding.detailsReceivePresence.setText(R.string.receive_presence_updates);
457 binding.detailsReceivePresence.setChecked(true);
458 } else {
459 binding.detailsReceivePresence.setText(R.string.ask_for_presence_updates);
460 binding.detailsReceivePresence.setChecked(contact.getOption(Contact.Options.ASKING));
461 }
462 if (contact.getAccount().isOnlineAndConnected()) {
463 binding.detailsReceivePresence.setEnabled(true);
464 binding.detailsSendPresence.setEnabled(true);
465 } else {
466 binding.detailsReceivePresence.setEnabled(false);
467 binding.detailsSendPresence.setEnabled(false);
468 }
469 binding.detailsSendPresence.setOnCheckedChangeListener(this.mOnSendCheckedChange);
470 binding.detailsReceivePresence.setOnCheckedChangeListener(this.mOnReceiveCheckedChange);
471 } else {
472 binding.addContactButton.setVisibility(View.VISIBLE);
473 binding.detailsSendPresence.setVisibility(View.GONE);
474 binding.detailsReceivePresence.setVisibility(View.GONE);
475 binding.statusMessage.setVisibility(View.GONE);
476 }
477
478 if (contact.isBlocked() && !this.showDynamicTags) {
479 binding.detailsLastseen.setVisibility(View.VISIBLE);
480 binding.detailsLastseen.setText(R.string.contact_blocked);
481 } else {
482 if (showLastSeen
483 && contact.getLastseen() > 0
484 && contact.getPresences().allOrNonSupport(Namespace.IDLE)) {
485 binding.detailsLastseen.setVisibility(View.VISIBLE);
486 binding.detailsLastseen.setText(UIHelper.lastseen(getApplicationContext(), contact.isActive(), contact.getLastseen()));
487 } else {
488 binding.detailsLastseen.setVisibility(View.GONE);
489 }
490 }
491
492 binding.detailsContactjid.setText(IrregularUnicodeDetector.style(this, contact.getJid()));
493 String account;
494 if (Config.DOMAIN_LOCK != null) {
495 account = contact.getAccount().getJid().getEscapedLocal();
496 } else {
497 account = contact.getAccount().getJid().asBareJid().toEscapedString();
498 }
499 binding.detailsAccount.setText(getString(R.string.using_account, account));
500 AvatarWorkerTask.loadAvatar(contact, binding.detailsContactBadge, R.dimen.avatar_on_details_screen_size);
501 binding.detailsContactBadge.setOnClickListener(this::onBadgeClick);
502
503 binding.detailsContactKeys.removeAllViews();
504 boolean hasKeys = false;
505 final LayoutInflater inflater = getLayoutInflater();
506 final AxolotlService axolotlService = contact.getAccount().getAxolotlService();
507 if (Config.supportOmemo() && axolotlService != null) {
508 final Collection<XmppAxolotlSession> sessions = axolotlService.findSessionsForContact(contact);
509 boolean anyActive = false;
510 for (XmppAxolotlSession session : sessions) {
511 anyActive = session.getTrust().isActive();
512 if (anyActive) {
513 break;
514 }
515 }
516 boolean skippedInactive = false;
517 boolean showsInactive = false;
518 for (final XmppAxolotlSession session : sessions) {
519 final FingerprintStatus trust = session.getTrust();
520 hasKeys |= !trust.isCompromised();
521 if (!trust.isActive() && anyActive) {
522 if (showInactiveOmemo) {
523 showsInactive = true;
524 } else {
525 skippedInactive = true;
526 continue;
527 }
528 }
529 if (!trust.isCompromised()) {
530 boolean highlight = session.getFingerprint().equals(messageFingerprint);
531 addFingerprintRow(binding.detailsContactKeys, session, highlight);
532 }
533 }
534 if (showsInactive || skippedInactive) {
535 binding.showInactiveDevices.setText(showsInactive ? R.string.hide_inactive_devices : R.string.show_inactive_devices);
536 binding.showInactiveDevices.setVisibility(View.VISIBLE);
537 } else {
538 binding.showInactiveDevices.setVisibility(View.GONE);
539 }
540 } else {
541 binding.showInactiveDevices.setVisibility(View.GONE);
542 }
543 binding.scanButton.setVisibility(hasKeys && isCameraFeatureAvailable() ? View.VISIBLE : View.GONE);
544 if (hasKeys) {
545 binding.scanButton.setOnClickListener((v) -> ScanActivity.scan(this));
546 }
547 if (Config.supportOpenPgp() && contact.getPgpKeyId() != 0) {
548 hasKeys = true;
549 View view = inflater.inflate(R.layout.contact_key, binding.detailsContactKeys, false);
550 TextView key = view.findViewById(R.id.key);
551 TextView keyType = view.findViewById(R.id.key_type);
552 keyType.setText(R.string.openpgp_key_id);
553 if ("pgp".equals(messageFingerprint)) {
554 keyType.setTextAppearance(this, R.style.TextAppearance_Conversations_Caption_Highlight);
555 }
556 key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
557 final OnClickListener openKey = v -> launchOpenKeyChain(contact.getPgpKeyId());
558 view.setOnClickListener(openKey);
559 key.setOnClickListener(openKey);
560 keyType.setOnClickListener(openKey);
561 binding.detailsContactKeys.addView(view);
562 }
563 binding.keysWrapper.setVisibility(hasKeys ? View.VISIBLE : View.GONE);
564
565 List<ListItem.Tag> tagList = contact.getTags(this);
566 if (tagList.size() == 0 || !this.showDynamicTags) {
567 binding.tags.setVisibility(View.GONE);
568 } else {
569 binding.tags.setVisibility(View.VISIBLE);
570 binding.tags.removeAllViewsInLayout();
571 for (final ListItem.Tag tag : tagList) {
572 final TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag, binding.tags, false);
573 tv.setText(tag.getName());
574 tv.setBackgroundColor(tag.getColor());
575 binding.tags.addView(tv);
576 }
577 }
578 }
579
580 private void onBadgeClick(View view) {
581 final Uri systemAccount = contact.getSystemAccount();
582 if (systemAccount == null) {
583 checkContactPermissionAndShowAddDialog();
584 } else {
585 final Intent intent = new Intent(Intent.ACTION_VIEW);
586 intent.setData(systemAccount);
587 try {
588 startActivity(intent);
589 } catch (final ActivityNotFoundException e) {
590 Toast.makeText(this, R.string.no_application_found_to_view_contact, Toast.LENGTH_SHORT).show();
591 }
592 }
593 }
594
595 public void onBackendConnected() {
596 if (accountJid != null && contactJid != null) {
597 Account account = xmppConnectionService.findAccountByJid(accountJid);
598 if (account == null) {
599 return;
600 }
601 this.contact = account.getRoster().getContact(contactJid);
602 if (mPendingFingerprintVerificationUri != null) {
603 processFingerprintVerification(mPendingFingerprintVerificationUri);
604 mPendingFingerprintVerificationUri = null;
605 }
606
607 if (Compatibility.hasStoragePermission(this)) {
608 final int limit = GridManager.getCurrentColumnCount(this.binding.media);
609 xmppConnectionService.getAttachments(account, contact.getJid().asBareJid(), limit, this);
610 this.binding.showMedia.setOnClickListener((v) -> MediaBrowserActivity.launch(this, contact));
611 }
612 populateView();
613 }
614 }
615
616 @Override
617 public void onKeyStatusUpdated(AxolotlService.FetchStatus report) {
618 refreshUi();
619 }
620
621 @Override
622 protected void processFingerprintVerification(XmppUri uri) {
623 if (contact != null && contact.getJid().asBareJid().equals(uri.getJid()) && uri.hasFingerprints()) {
624 if (xmppConnectionService.verifyFingerprints(contact, uri.getFingerprints())) {
625 Toast.makeText(this, R.string.verified_fingerprints, Toast.LENGTH_SHORT).show();
626 }
627 } else {
628 Toast.makeText(this, R.string.invalid_barcode, Toast.LENGTH_SHORT).show();
629 }
630 }
631
632 @Override
633 public void onMediaLoaded(List<Attachment> attachments) {
634 runOnUiThread(() -> {
635 int limit = GridManager.getCurrentColumnCount(binding.media);
636 mMediaAdapter.setAttachments(attachments.subList(0, Math.min(limit, attachments.size())));
637 binding.mediaWrapper.setVisibility(attachments.size() > 0 ? View.VISIBLE : View.GONE);
638 });
639
640 }
641}