ContactDetailsActivity.java

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