VCardManager.java

 1package eu.siacs.conversations.xmpp.manager;
 2
 3import android.content.Context;
 4import com.google.common.util.concurrent.Futures;
 5import com.google.common.util.concurrent.ListenableFuture;
 6import com.google.common.util.concurrent.MoreExecutors;
 7import eu.siacs.conversations.xmpp.Jid;
 8import eu.siacs.conversations.xmpp.XmppConnection;
 9import im.conversations.android.xmpp.model.stanza.Iq;
10import im.conversations.android.xmpp.model.vcard.VCard;
11
12public class VCardManager extends AbstractManager {
13
14    public VCardManager(final Context context, final XmppConnection connection) {
15        super(context, connection);
16    }
17
18    public ListenableFuture<VCard> retrieve(final Jid address) {
19        final var iq = new Iq(Iq.Type.GET, new VCard());
20        iq.setTo(address);
21        return Futures.transform(
22                this.connection.sendIqPacket(iq),
23                result -> {
24                    final var vCard = result.getExtension(VCard.class);
25                    if (vCard == null) {
26                        throw new IllegalStateException("Result did not include vCard");
27                    }
28                    return vCard;
29                },
30                MoreExecutors.directExecutor());
31    }
32
33    public ListenableFuture<byte[]> retrievePhoto(final Jid address) {
34        final var vCardFuture = retrieve(address);
35        return Futures.transform(
36                vCardFuture,
37                vCard -> {
38                    final var photo = vCard.getPhoto();
39                    if (photo == null) {
40                        throw new IllegalStateException(
41                                String.format("No photo in vCard of %s", address));
42                    }
43                    final var binaryValue = photo.getBinaryValue();
44                    if (binaryValue == null) {
45                        throw new IllegalStateException(
46                                String.format("Photo has no binary value in vCard of %s", address));
47                    }
48                    return binaryValue.asBytes();
49                },
50                MoreExecutors.directExecutor());
51    }
52}