Search in sources :

Example 1 with Avatar

use of org.thoughtcrime.securesms.contactshare.Contact.Avatar in project Signal-Android by WhisperSystems.

the class MmsDatabase method getSerializedSharedContacts.

@Nullable
private String getSerializedSharedContacts(@NonNull Map<Attachment, AttachmentId> insertedAttachmentIds, @NonNull List<Contact> contacts) {
    if (contacts.isEmpty())
        return null;
    JSONArray sharedContactJson = new JSONArray();
    for (Contact contact : contacts) {
        try {
            AttachmentId attachmentId = null;
            if (contact.getAvatarAttachment() != null) {
                attachmentId = insertedAttachmentIds.get(contact.getAvatarAttachment());
            }
            Avatar updatedAvatar = new Avatar(attachmentId, contact.getAvatarAttachment(), contact.getAvatar() != null && contact.getAvatar().isProfile());
            Contact updatedContact = new Contact(contact, updatedAvatar);
            sharedContactJson.put(new JSONObject(updatedContact.serialize()));
        } catch (JSONException | IOException e) {
            Log.w(TAG, "Failed to serialize shared contact. Skipping it.", e);
        }
    }
    return sharedContactJson.toString();
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) AttachmentId(org.thoughtcrime.securesms.attachments.AttachmentId) Avatar(org.thoughtcrime.securesms.contactshare.Contact.Avatar) Contact(org.thoughtcrime.securesms.contactshare.Contact) Nullable(androidx.annotation.Nullable)

Example 2 with Avatar

use of org.thoughtcrime.securesms.contactshare.Contact.Avatar in project Signal-Android by WhisperSystems.

the class SharedContactRepository method getContactFromSystemContacts.

@WorkerThread
@Nullable
private Contact getContactFromSystemContacts(long contactId) {
    Name name = getName(contactId);
    if (name == null) {
        Log.w(TAG, "Couldn't find a name associated with the provided contact ID.");
        return null;
    }
    List<Phone> phoneNumbers = getPhoneNumbers(contactId);
    AvatarInfo avatarInfo = getAvatarInfo(contactId, phoneNumbers);
    Avatar avatar = avatarInfo != null ? new Avatar(avatarInfo.uri, avatarInfo.isProfile) : null;
    return new Contact(name, null, phoneNumbers, getEmails(contactId), getPostalAddresses(contactId), avatar);
}
Also used : Phone(org.thoughtcrime.securesms.contactshare.Contact.Phone) Avatar(org.thoughtcrime.securesms.contactshare.Contact.Avatar) Name(org.thoughtcrime.securesms.contactshare.Contact.Name) WorkerThread(androidx.annotation.WorkerThread) Nullable(androidx.annotation.Nullable)

Example 3 with Avatar

use of org.thoughtcrime.securesms.contactshare.Contact.Avatar in project Signal-Android by WhisperSystems.

the class MmsDatabase method getSharedContacts.

private static List<Contact> getSharedContacts(@NonNull Cursor cursor, @NonNull List<DatabaseAttachment> attachments) {
    String serializedContacts = cursor.getString(cursor.getColumnIndexOrThrow(SHARED_CONTACTS));
    if (TextUtils.isEmpty(serializedContacts)) {
        return Collections.emptyList();
    }
    Map<AttachmentId, DatabaseAttachment> attachmentIdMap = new HashMap<>();
    for (DatabaseAttachment attachment : attachments) {
        attachmentIdMap.put(attachment.getAttachmentId(), attachment);
    }
    try {
        List<Contact> contacts = new LinkedList<>();
        JSONArray jsonContacts = new JSONArray(serializedContacts);
        for (int i = 0; i < jsonContacts.length(); i++) {
            Contact contact = Contact.deserialize(jsonContacts.getJSONObject(i).toString());
            if (contact.getAvatar() != null && contact.getAvatar().getAttachmentId() != null) {
                DatabaseAttachment attachment = attachmentIdMap.get(contact.getAvatar().getAttachmentId());
                Avatar updatedAvatar = new Avatar(contact.getAvatar().getAttachmentId(), attachment, contact.getAvatar().isProfile());
                contacts.add(new Contact(contact, updatedAvatar));
            } else {
                contacts.add(contact);
            }
        }
        return contacts;
    } catch (JSONException | IOException e) {
        Log.w(TAG, "Failed to parse shared contacts.", e);
    }
    return Collections.emptyList();
}
Also used : HashMap(java.util.HashMap) DatabaseAttachment(org.thoughtcrime.securesms.attachments.DatabaseAttachment) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) AttachmentId(org.thoughtcrime.securesms.attachments.AttachmentId) LinkedList(java.util.LinkedList) Avatar(org.thoughtcrime.securesms.contactshare.Contact.Avatar) Contact(org.thoughtcrime.securesms.contactshare.Contact)

Example 4 with Avatar

use of org.thoughtcrime.securesms.contactshare.Contact.Avatar in project Signal-Android by WhisperSystems.

the class ContactModelMapper method remoteToLocal.

public static Contact remoteToLocal(@NonNull SharedContact sharedContact) {
    Name name = new Name(sharedContact.getName().getDisplay().orNull(), sharedContact.getName().getGiven().orNull(), sharedContact.getName().getFamily().orNull(), sharedContact.getName().getPrefix().orNull(), sharedContact.getName().getSuffix().orNull(), sharedContact.getName().getMiddle().orNull());
    List<Phone> phoneNumbers = new LinkedList<>();
    if (sharedContact.getPhone().isPresent()) {
        for (SharedContact.Phone phone : sharedContact.getPhone().get()) {
            phoneNumbers.add(new Phone(phone.getValue(), remoteToLocalType(phone.getType()), phone.getLabel().orNull()));
        }
    }
    List<Email> emails = new LinkedList<>();
    if (sharedContact.getEmail().isPresent()) {
        for (SharedContact.Email email : sharedContact.getEmail().get()) {
            emails.add(new Email(email.getValue(), remoteToLocalType(email.getType()), email.getLabel().orNull()));
        }
    }
    List<PostalAddress> postalAddresses = new LinkedList<>();
    if (sharedContact.getAddress().isPresent()) {
        for (SharedContact.PostalAddress postalAddress : sharedContact.getAddress().get()) {
            postalAddresses.add(new PostalAddress(remoteToLocalType(postalAddress.getType()), postalAddress.getLabel().orNull(), postalAddress.getStreet().orNull(), postalAddress.getPobox().orNull(), postalAddress.getNeighborhood().orNull(), postalAddress.getCity().orNull(), postalAddress.getRegion().orNull(), postalAddress.getPostcode().orNull(), postalAddress.getCountry().orNull()));
        }
    }
    Avatar avatar = null;
    if (sharedContact.getAvatar().isPresent()) {
        Attachment attachment = PointerAttachment.forPointer(Optional.of(sharedContact.getAvatar().get().getAttachment().asPointer())).get();
        boolean isProfile = sharedContact.getAvatar().get().isProfile();
        avatar = new Avatar(null, attachment, isProfile);
    }
    return new Contact(name, sharedContact.getOrganization().orNull(), phoneNumbers, emails, postalAddresses, avatar);
}
Also used : Email(org.thoughtcrime.securesms.contactshare.Contact.Email) PointerAttachment(org.thoughtcrime.securesms.attachments.PointerAttachment) Attachment(org.thoughtcrime.securesms.attachments.Attachment) LinkedList(java.util.LinkedList) Avatar(org.thoughtcrime.securesms.contactshare.Contact.Avatar) Name(org.thoughtcrime.securesms.contactshare.Contact.Name) SharedContact(org.whispersystems.signalservice.api.messages.shared.SharedContact) PostalAddress(org.thoughtcrime.securesms.contactshare.Contact.PostalAddress) Phone(org.thoughtcrime.securesms.contactshare.Contact.Phone) SharedContact(org.whispersystems.signalservice.api.messages.shared.SharedContact)

Aggregations

Avatar (org.thoughtcrime.securesms.contactshare.Contact.Avatar)4 Nullable (androidx.annotation.Nullable)2 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2 AttachmentId (org.thoughtcrime.securesms.attachments.AttachmentId)2 Contact (org.thoughtcrime.securesms.contactshare.Contact)2 Name (org.thoughtcrime.securesms.contactshare.Contact.Name)2 Phone (org.thoughtcrime.securesms.contactshare.Contact.Phone)2 WorkerThread (androidx.annotation.WorkerThread)1 HashMap (java.util.HashMap)1 JSONObject (org.json.JSONObject)1 Attachment (org.thoughtcrime.securesms.attachments.Attachment)1 DatabaseAttachment (org.thoughtcrime.securesms.attachments.DatabaseAttachment)1 PointerAttachment (org.thoughtcrime.securesms.attachments.PointerAttachment)1 Email (org.thoughtcrime.securesms.contactshare.Contact.Email)1 PostalAddress (org.thoughtcrime.securesms.contactshare.Contact.PostalAddress)1 SharedContact (org.whispersystems.signalservice.api.messages.shared.SharedContact)1