Search in sources :

Example 6 with SignalServiceAttachmentStream

use of org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream in project Signal-Android by signalapp.

the class MultiDeviceProfileKeyUpdateJob method onRun.

@Override
public void onRun(MasterSecret masterSecret) throws IOException, UntrustedIdentityException {
    if (!TextSecurePreferences.isMultiDevice(getContext())) {
        Log.w(TAG, "Not multi device...");
        return;
    }
    Optional<byte[]> profileKey = Optional.of(ProfileKeyUtil.getProfileKey(getContext()));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DeviceContactsOutputStream out = new DeviceContactsOutputStream(baos);
    out.write(new DeviceContact(TextSecurePreferences.getLocalNumber(getContext()), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), profileKey, false, Optional.absent()));
    out.close();
    SignalServiceAttachmentStream attachmentStream = SignalServiceAttachment.newStreamBuilder().withStream(new ByteArrayInputStream(baos.toByteArray())).withContentType("application/octet-stream").withLength(baos.toByteArray().length).build();
    SignalServiceSyncMessage syncMessage = SignalServiceSyncMessage.forContacts(new ContactsMessage(attachmentStream, false));
    messageSender.sendMessage(syncMessage);
}
Also used : DeviceContact(org.whispersystems.signalservice.api.messages.multidevice.DeviceContact) ByteArrayInputStream(java.io.ByteArrayInputStream) DeviceContactsOutputStream(org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsOutputStream) ContactsMessage(org.whispersystems.signalservice.api.messages.multidevice.ContactsMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SignalServiceSyncMessage(org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage) SignalServiceAttachmentStream(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream)

Example 7 with SignalServiceAttachmentStream

use of org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream in project libsignal-service-java by signalapp.

the class DeviceContactsInputStream method read.

public DeviceContact read() throws IOException {
    long detailsLength = readRawVarint32();
    byte[] detailsSerialized = new byte[(int) detailsLength];
    Util.readFully(in, detailsSerialized);
    SignalServiceProtos.ContactDetails details = SignalServiceProtos.ContactDetails.parseFrom(detailsSerialized);
    String number = details.getNumber();
    Optional<String> name = Optional.fromNullable(details.getName());
    Optional<SignalServiceAttachmentStream> avatar = Optional.absent();
    Optional<String> color = details.hasColor() ? Optional.of(details.getColor()) : Optional.<String>absent();
    Optional<VerifiedMessage> verified = Optional.absent();
    Optional<byte[]> profileKey = Optional.absent();
    boolean blocked = false;
    Optional<Integer> expireTimer = Optional.absent();
    if (details.hasAvatar()) {
        long avatarLength = details.getAvatar().getLength();
        InputStream avatarStream = new LimitedInputStream(in, avatarLength);
        String avatarContentType = details.getAvatar().getContentType();
        avatar = Optional.of(new SignalServiceAttachmentStream(avatarStream, avatarContentType, avatarLength, Optional.<String>absent(), false, null));
    }
    if (details.hasVerified()) {
        try {
            String destination = details.getVerified().getDestination();
            IdentityKey identityKey = new IdentityKey(details.getVerified().getIdentityKey().toByteArray(), 0);
            VerifiedMessage.VerifiedState state;
            switch(details.getVerified().getState()) {
                case VERIFIED:
                    state = VerifiedMessage.VerifiedState.VERIFIED;
                    break;
                case UNVERIFIED:
                    state = VerifiedMessage.VerifiedState.UNVERIFIED;
                    break;
                case DEFAULT:
                    state = VerifiedMessage.VerifiedState.DEFAULT;
                    break;
                default:
                    throw new InvalidMessageException("Unknown state: " + details.getVerified().getState());
            }
            verified = Optional.of(new VerifiedMessage(destination, identityKey, state, System.currentTimeMillis()));
        } catch (InvalidKeyException | InvalidMessageException e) {
            Log.w(TAG, e);
            verified = Optional.absent();
        }
    }
    if (details.hasProfileKey()) {
        profileKey = Optional.fromNullable(details.getProfileKey().toByteArray());
    }
    if (details.hasExpireTimer() && details.getExpireTimer() > 0) {
        expireTimer = Optional.of(details.getExpireTimer());
    }
    blocked = details.getBlocked();
    return new DeviceContact(number, name, avatar, color, verified, profileKey, blocked, expireTimer);
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) IdentityKey(org.whispersystems.libsignal.IdentityKey) InputStream(java.io.InputStream) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) SignalServiceAttachmentStream(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream) SignalServiceProtos(org.whispersystems.signalservice.internal.push.SignalServiceProtos)

Aggregations

SignalServiceAttachmentStream (org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream)7 FileInputStream (java.io.FileInputStream)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)2 ContactsMessage (org.whispersystems.signalservice.api.messages.multidevice.ContactsMessage)2 PushNetworkException (org.whispersystems.signalservice.api.push.exceptions.PushNetworkException)2 SignalServiceProtos (org.whispersystems.signalservice.internal.push.SignalServiceProtos)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IdentityKey (org.whispersystems.libsignal.IdentityKey)1 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)1 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)1 DeviceContact (org.whispersystems.signalservice.api.messages.multidevice.DeviceContact)1 DeviceContactsOutputStream (org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsOutputStream)1 SignalServiceSyncMessage (org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage)1