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