Search in sources :

Example 1 with VCardBuilder

use of org.kde.kdeconnect.Helpers.ContactsHelper.VCardBuilder in project kdeconnect-android by KDE.

the class ContactsPlugin method handleRequestVCardsByUIDs.

private boolean handleRequestVCardsByUIDs(NetworkPacket np) {
    if (!np.has("uids")) {
        Log.e("ContactsPlugin", "handleRequestNamesByUIDs received a malformed packet with no uids key");
        return false;
    }
    List<String> uIDsAsStrings = np.getStringList("uids");
    // Convert to Collection<uIDs> to call getVCardsForContactIDs
    Set<uID> uIDs = new HashSet<>(uIDsAsStrings.size());
    for (String uID : uIDsAsStrings) {
        uIDs.add(new uID(uID));
    }
    Map<uID, VCardBuilder> uIDsToVCards = ContactsHelper.getVCardsForContactIDs(context, uIDs);
    // ContactsHelper.getVCardsForContactIDs(..) is allowed to reply without
    // some of the requested uIDs if they were not in the database, so update our list
    uIDsAsStrings = new ArrayList<>(uIDsToVCards.size());
    NetworkPacket reply = new NetworkPacket(PACKET_TYPE_CONTACTS_RESPONSE_VCARDS);
    // Add the vcards to the packet
    for (uID uID : uIDsToVCards.keySet()) {
        VCardBuilder vcard = uIDsToVCards.get(uID);
        try {
            vcard = this.addVCardMetadata(vcard, uID);
            // Store this as a valid uID
            uIDsAsStrings.add(uID.toString());
            // Add the uid -> vcard pairing to the packet
            reply.set(uID.toString(), vcard.toString());
        } catch (ContactsHelper.ContactNotFoundException e) {
            e.printStackTrace();
        }
    }
    // Add the valid uIDs to the packet
    reply.set("uids", uIDsAsStrings);
    device.sendPacket(reply);
    return true;
}
Also used : ContactsHelper.uID(org.kde.kdeconnect.Helpers.ContactsHelper.uID) ContactsHelper(org.kde.kdeconnect.Helpers.ContactsHelper) VCardBuilder(org.kde.kdeconnect.Helpers.ContactsHelper.VCardBuilder) ContactNotFoundException(org.kde.kdeconnect.Helpers.ContactsHelper.ContactNotFoundException) NetworkPacket(org.kde.kdeconnect.NetworkPacket) HashSet(java.util.HashSet)

Aggregations

HashSet (java.util.HashSet)1 ContactsHelper (org.kde.kdeconnect.Helpers.ContactsHelper)1 ContactNotFoundException (org.kde.kdeconnect.Helpers.ContactsHelper.ContactNotFoundException)1 VCardBuilder (org.kde.kdeconnect.Helpers.ContactsHelper.VCardBuilder)1 ContactsHelper.uID (org.kde.kdeconnect.Helpers.ContactsHelper.uID)1 NetworkPacket (org.kde.kdeconnect.NetworkPacket)1