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:" + Uri.encode(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 edit.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
406 @Override
407 public boolean onMenuItemActionCollapse(MenuItem item) {
408 binding.editTags.setVisibility(View.GONE);
409 if (save != null) save.setVisible(false);
410 populateView();
411 return true;
412 }
413
414 @Override
415 public boolean onMenuItemActionExpand(MenuItem item) { return true; }
416 });
417 return super.onCreateOptionsMenu(menu);
418 }
419
420 private void populateView() {
421 if (contact == null) {
422 return;
423 }
424 if (binding.editTags.getVisibility() != View.GONE) return;
425 invalidateOptionsMenu();
426 setTitle(contact.getDisplayName());
427 if (contact.showInRoster()) {
428 binding.detailsSendPresence.setVisibility(View.VISIBLE);
429 binding.detailsReceivePresence.setVisibility(View.VISIBLE);
430 binding.addContactButton.setVisibility(View.GONE);
431 binding.detailsSendPresence.setOnCheckedChangeListener(null);
432 binding.detailsReceivePresence.setOnCheckedChangeListener(null);
433
434 List<String> statusMessages = contact.getPresences().getStatusMessages();
435 if (statusMessages.size() == 0) {
436 binding.statusMessage.setVisibility(View.GONE);
437 } else if (statusMessages.size() == 1) {
438 final String message = statusMessages.get(0);
439 binding.statusMessage.setVisibility(View.VISIBLE);
440 final Spannable span = new SpannableString(message);
441 if (Emoticons.isOnlyEmoji(message)) {
442 span.setSpan(new RelativeSizeSpan(2.0f), 0, message.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
443 }
444 binding.statusMessage.setText(span);
445 } else {
446 StringBuilder builder = new StringBuilder();
447 binding.statusMessage.setVisibility(View.VISIBLE);
448 int s = statusMessages.size();
449 for (int i = 0; i < s; ++i) {
450 builder.append(statusMessages.get(i));
451 if (i < s - 1) {
452 builder.append("\n");
453 }
454 }
455 binding.statusMessage.setText(builder);
456 }
457
458 if (contact.getOption(Contact.Options.FROM)) {
459 binding.detailsSendPresence.setText(R.string.send_presence_updates);
460 binding.detailsSendPresence.setChecked(true);
461 } else if (contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
462 binding.detailsSendPresence.setChecked(false);
463 binding.detailsSendPresence.setText(R.string.send_presence_updates);
464 } else {
465 binding.detailsSendPresence.setText(R.string.preemptively_grant);
466 binding.detailsSendPresence.setChecked(contact.getOption(Contact.Options.PREEMPTIVE_GRANT));
467 }
468 if (contact.getOption(Contact.Options.TO)) {
469 binding.detailsReceivePresence.setText(R.string.receive_presence_updates);
470 binding.detailsReceivePresence.setChecked(true);
471 } else {
472 binding.detailsReceivePresence.setText(R.string.ask_for_presence_updates);
473 binding.detailsReceivePresence.setChecked(contact.getOption(Contact.Options.ASKING));
474 }
475 if (contact.getAccount().isOnlineAndConnected()) {
476 binding.detailsReceivePresence.setEnabled(true);
477 binding.detailsSendPresence.setEnabled(true);
478 } else {
479 binding.detailsReceivePresence.setEnabled(false);
480 binding.detailsSendPresence.setEnabled(false);
481 }
482 binding.detailsSendPresence.setOnCheckedChangeListener(this.mOnSendCheckedChange);
483 binding.detailsReceivePresence.setOnCheckedChangeListener(this.mOnReceiveCheckedChange);
484 } else {
485 binding.addContactButton.setVisibility(View.VISIBLE);
486 binding.detailsSendPresence.setVisibility(View.GONE);
487 binding.detailsReceivePresence.setVisibility(View.GONE);
488 binding.statusMessage.setVisibility(View.GONE);
489 }
490
491 if (contact.isBlocked() && !this.showDynamicTags) {
492 binding.detailsLastseen.setVisibility(View.VISIBLE);
493 binding.detailsLastseen.setText(R.string.contact_blocked);
494 } else {
495 if (showLastSeen
496 && contact.getLastseen() > 0
497 && contact.getPresences().allOrNonSupport(Namespace.IDLE)) {
498 binding.detailsLastseen.setVisibility(View.VISIBLE);
499 binding.detailsLastseen.setText(UIHelper.lastseen(getApplicationContext(), contact.isActive(), contact.getLastseen()));
500 } else {
501 binding.detailsLastseen.setVisibility(View.GONE);
502 }
503 }
504
505 binding.detailsContactjid.setText(IrregularUnicodeDetector.style(this, contact.getJid()));
506 String account;
507 if (Config.DOMAIN_LOCK != null) {
508 account = contact.getAccount().getJid().getEscapedLocal();
509 } else {
510 account = contact.getAccount().getJid().asBareJid().toEscapedString();
511 }
512 binding.detailsAccount.setText(getString(R.string.using_account, account));
513 AvatarWorkerTask.loadAvatar(contact, binding.detailsContactBadge, R.dimen.avatar_on_details_screen_size);
514 binding.detailsContactBadge.setOnClickListener(this::onBadgeClick);
515
516 binding.detailsContactKeys.removeAllViews();
517 boolean hasKeys = false;
518 final LayoutInflater inflater = getLayoutInflater();
519 final AxolotlService axolotlService = contact.getAccount().getAxolotlService();
520 if (Config.supportOmemo() && axolotlService != null) {
521 final Collection<XmppAxolotlSession> sessions = axolotlService.findSessionsForContact(contact);
522 boolean anyActive = false;
523 for (XmppAxolotlSession session : sessions) {
524 anyActive = session.getTrust().isActive();
525 if (anyActive) {
526 break;
527 }
528 }
529 boolean skippedInactive = false;
530 boolean showsInactive = false;
531 for (final XmppAxolotlSession session : sessions) {
532 final FingerprintStatus trust = session.getTrust();
533 hasKeys |= !trust.isCompromised();
534 if (!trust.isActive() && anyActive) {
535 if (showInactiveOmemo) {
536 showsInactive = true;
537 } else {
538 skippedInactive = true;
539 continue;
540 }
541 }
542 if (!trust.isCompromised()) {
543 boolean highlight = session.getFingerprint().equals(messageFingerprint);
544 addFingerprintRow(binding.detailsContactKeys, session, highlight);
545 }
546 }
547 if (showsInactive || skippedInactive) {
548 binding.showInactiveDevices.setText(showsInactive ? R.string.hide_inactive_devices : R.string.show_inactive_devices);
549 binding.showInactiveDevices.setVisibility(View.VISIBLE);
550 } else {
551 binding.showInactiveDevices.setVisibility(View.GONE);
552 }
553 } else {
554 binding.showInactiveDevices.setVisibility(View.GONE);
555 }
556 binding.scanButton.setVisibility(hasKeys && isCameraFeatureAvailable() ? View.VISIBLE : View.GONE);
557 if (hasKeys) {
558 binding.scanButton.setOnClickListener((v) -> ScanActivity.scan(this));
559 }
560 if (Config.supportOpenPgp() && contact.getPgpKeyId() != 0) {
561 hasKeys = true;
562 View view = inflater.inflate(R.layout.contact_key, binding.detailsContactKeys, false);
563 TextView key = view.findViewById(R.id.key);
564 TextView keyType = view.findViewById(R.id.key_type);
565 keyType.setText(R.string.openpgp_key_id);
566 if ("pgp".equals(messageFingerprint)) {
567 keyType.setTextAppearance(this, R.style.TextAppearance_Conversations_Caption_Highlight);
568 }
569 key.setText(OpenPgpUtils.convertKeyIdToHex(contact.getPgpKeyId()));
570 final OnClickListener openKey = v -> launchOpenKeyChain(contact.getPgpKeyId());
571 view.setOnClickListener(openKey);
572 key.setOnClickListener(openKey);
573 keyType.setOnClickListener(openKey);
574 binding.detailsContactKeys.addView(view);
575 }
576 binding.keysWrapper.setVisibility(hasKeys ? View.VISIBLE : View.GONE);
577
578 List<ListItem.Tag> tagList = contact.getTags(this);
579 if (tagList.size() == 0 || !this.showDynamicTags) {
580 binding.tags.setVisibility(View.GONE);
581 } else {
582 binding.tags.setVisibility(View.VISIBLE);
583 binding.tags.removeAllViewsInLayout();
584 for (final ListItem.Tag tag : tagList) {
585 final TextView tv = (TextView) inflater.inflate(R.layout.list_item_tag, binding.tags, false);
586 tv.setText(tag.getName());
587 tv.setBackgroundColor(tag.getColor());
588 binding.tags.addView(tv);
589 }
590 }
591 }
592
593 private void onBadgeClick(View view) {
594 final Uri systemAccount = contact.getSystemAccount();
595 if (systemAccount == null) {
596 checkContactPermissionAndShowAddDialog();
597 } else {
598 final Intent intent = new Intent(Intent.ACTION_VIEW);
599 intent.setData(systemAccount);
600 try {
601 startActivity(intent);
602 } catch (final ActivityNotFoundException e) {
603 Toast.makeText(this, R.string.no_application_found_to_view_contact, Toast.LENGTH_SHORT).show();
604 }
605 }
606 }
607
608 public void onBackendConnected() {
609 if (accountJid != null && contactJid != null) {
610 Account account = xmppConnectionService.findAccountByJid(accountJid);
611 if (account == null) {
612 return;
613 }
614 this.contact = account.getRoster().getContact(contactJid);
615 if (mPendingFingerprintVerificationUri != null) {
616 processFingerprintVerification(mPendingFingerprintVerificationUri);
617 mPendingFingerprintVerificationUri = null;
618 }
619
620 if (Compatibility.hasStoragePermission(this)) {
621 final int limit = GridManager.getCurrentColumnCount(this.binding.media);
622 xmppConnectionService.getAttachments(account, contact.getJid().asBareJid(), limit, this);
623 this.binding.showMedia.setOnClickListener((v) -> MediaBrowserActivity.launch(this, contact));
624 }
625 populateView();
626 }
627 }
628
629 @Override
630 public void onKeyStatusUpdated(AxolotlService.FetchStatus report) {
631 refreshUi();
632 }
633
634 @Override
635 protected void processFingerprintVerification(XmppUri uri) {
636 if (contact != null && contact.getJid().asBareJid().equals(uri.getJid()) && uri.hasFingerprints()) {
637 if (xmppConnectionService.verifyFingerprints(contact, uri.getFingerprints())) {
638 Toast.makeText(this, R.string.verified_fingerprints, Toast.LENGTH_SHORT).show();
639 }
640 } else {
641 Toast.makeText(this, R.string.invalid_barcode, Toast.LENGTH_SHORT).show();
642 }
643 }
644
645 @Override
646 public void onMediaLoaded(List<Attachment> attachments) {
647 runOnUiThread(() -> {
648 int limit = GridManager.getCurrentColumnCount(binding.media);
649 mMediaAdapter.setAttachments(attachments.subList(0, Math.min(limit, attachments.size())));
650 binding.mediaWrapper.setVisibility(attachments.size() > 0 ? View.VISIBLE : View.GONE);
651 });
652
653 }
654}