Search in sources :

Example 1 with DeviceContact

use of org.whispersystems.signalservice.api.messages.multidevice.DeviceContact in project Signal-Android by WhisperSystems.

the class MultiDeviceContactUpdateJob method generateSingleContactUpdate.

private void generateSingleContactUpdate(long recipientId) throws IOException, UntrustedIdentityException, NetworkException {
    SignalServiceMessageSender messageSender = messageSenderFactory.create();
    File contactDataFile = createTempFile("multidevice-contact-update");
    try {
        DeviceContactsOutputStream out = new DeviceContactsOutputStream(new FileOutputStream(contactDataFile));
        Recipient recipient = RecipientFactory.getRecipientForId(context, recipientId, false);
        out.write(new DeviceContact(Util.canonicalizeNumber(context, recipient.getNumber()), Optional.fromNullable(recipient.getName()), getAvatar(recipient.getContactUri()), Optional.fromNullable(recipient.getColor().serialize())));
        out.close();
        sendUpdate(messageSender, contactDataFile);
    } catch (InvalidNumberException e) {
        Log.w(TAG, e);
    } finally {
        if (contactDataFile != null)
            contactDataFile.delete();
    }
}
Also used : DeviceContact(org.whispersystems.signalservice.api.messages.multidevice.DeviceContact) DeviceContactsOutputStream(org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsOutputStream) InvalidNumberException(org.whispersystems.signalservice.api.util.InvalidNumberException) FileOutputStream(java.io.FileOutputStream) SignalServiceMessageSender(org.whispersystems.signalservice.api.SignalServiceMessageSender) Recipient(org.thoughtcrime.securesms.recipients.Recipient) File(java.io.File)

Example 2 with DeviceContact

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

the class MultiDeviceContactUpdateJob method generateSingleContactUpdate.

private void generateSingleContactUpdate(@NonNull Address address) throws IOException, UntrustedIdentityException, NetworkException {
    File contactDataFile = createTempFile("multidevice-contact-update");
    try {
        DeviceContactsOutputStream out = new DeviceContactsOutputStream(new FileOutputStream(contactDataFile));
        Recipient recipient = Recipient.from(context, address, false);
        Optional<IdentityDatabase.IdentityRecord> identityRecord = DatabaseFactory.getIdentityDatabase(context).getIdentity(address);
        Optional<VerifiedMessage> verifiedMessage = getVerifiedMessage(recipient, identityRecord);
        out.write(new DeviceContact(address.toPhoneString(), Optional.fromNullable(recipient.getName()), getAvatar(recipient.getContactUri()), Optional.fromNullable(recipient.getColor().serialize()), verifiedMessage, Optional.fromNullable(recipient.getProfileKey()), recipient.isBlocked(), recipient.getExpireMessages() > 0 ? Optional.of(recipient.getExpireMessages()) : Optional.absent()));
        out.close();
        sendUpdate(messageSender, contactDataFile, false);
    } catch (InvalidNumberException e) {
        Log.w(TAG, e);
    } finally {
        if (contactDataFile != null)
            contactDataFile.delete();
    }
}
Also used : DeviceContact(org.whispersystems.signalservice.api.messages.multidevice.DeviceContact) DeviceContactsOutputStream(org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsOutputStream) InvalidNumberException(org.whispersystems.signalservice.api.util.InvalidNumberException) FileOutputStream(java.io.FileOutputStream) Recipient(org.thoughtcrime.securesms.recipients.Recipient) VerifiedMessage(org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage) File(java.io.File)

Example 3 with DeviceContact

use of org.whispersystems.signalservice.api.messages.multidevice.DeviceContact in project Signal-Android by WhisperSystems.

the class MultiDeviceContactUpdateJob method generateFullContactUpdate.

private void generateFullContactUpdate() throws IOException, UntrustedIdentityException, NetworkException {
    boolean isAppVisible = ApplicationDependencies.getAppForegroundObserver().isForegrounded();
    long timeSinceLastSync = System.currentTimeMillis() - TextSecurePreferences.getLastFullContactSyncTime(context);
    Log.d(TAG, "Requesting a full contact sync. forced = " + forceSync + ", appVisible = " + isAppVisible + ", timeSinceLastSync = " + timeSinceLastSync + " ms");
    if (!forceSync && !isAppVisible && timeSinceLastSync < FULL_SYNC_TIME) {
        Log.i(TAG, "App is backgrounded and the last contact sync was too soon (" + timeSinceLastSync + " ms ago). Marking that we need a sync. Skipping multi-device contact update...");
        TextSecurePreferences.setNeedsFullContactSync(context, true);
        return;
    }
    TextSecurePreferences.setLastFullContactSyncTime(context, System.currentTimeMillis());
    TextSecurePreferences.setNeedsFullContactSync(context, false);
    WriteDetails writeDetails = createTempFile();
    try {
        DeviceContactsOutputStream out = new DeviceContactsOutputStream(writeDetails.outputStream);
        List<Recipient> recipients = SignalDatabase.recipients().getRecipientsForMultiDeviceSync();
        Map<RecipientId, Integer> inboxPositions = SignalDatabase.threads().getInboxPositions();
        Set<RecipientId> archived = SignalDatabase.threads().getArchivedRecipients();
        for (Recipient recipient : recipients) {
            Optional<IdentityRecord> identity = ApplicationDependencies.getProtocolStore().aci().identities().getIdentityRecord(recipient.getId());
            Optional<VerifiedMessage> verified = getVerifiedMessage(recipient, identity);
            Optional<String> name = Optional.fromNullable(recipient.isSystemContact() ? recipient.getDisplayName(context) : recipient.getGroupName(context));
            Optional<ProfileKey> profileKey = ProfileKeyUtil.profileKeyOptional(recipient.getProfileKey());
            boolean blocked = recipient.isBlocked();
            Optional<Integer> expireTimer = recipient.getExpiresInSeconds() > 0 ? Optional.of(recipient.getExpiresInSeconds()) : Optional.absent();
            Optional<Integer> inboxPosition = Optional.fromNullable(inboxPositions.get(recipient.getId()));
            out.write(new DeviceContact(RecipientUtil.toSignalServiceAddress(context, recipient), name, getAvatar(recipient.getId(), recipient.getContactUri()), Optional.of(ChatColorsMapper.getMaterialColor(recipient.getChatColors()).serialize()), verified, profileKey, blocked, expireTimer, inboxPosition, archived.contains(recipient.getId())));
        }
        Recipient self = Recipient.self();
        byte[] profileKey = self.getProfileKey();
        if (profileKey != null) {
            out.write(new DeviceContact(RecipientUtil.toSignalServiceAddress(context, self), Optional.absent(), Optional.absent(), Optional.of(ChatColorsMapper.getMaterialColor(self.getChatColors()).serialize()), Optional.absent(), ProfileKeyUtil.profileKeyOptionalOrThrow(self.getProfileKey()), false, self.getExpiresInSeconds() > 0 ? Optional.of(self.getExpiresInSeconds()) : Optional.absent(), Optional.fromNullable(inboxPositions.get(self.getId())), archived.contains(self.getId())));
        }
        out.close();
        long length = BlobProvider.getInstance().calculateFileSize(context, writeDetails.uri);
        sendUpdate(ApplicationDependencies.getSignalServiceMessageSender(), BlobProvider.getInstance().getStream(context, writeDetails.uri), length, true);
    } catch (InvalidNumberException e) {
        Log.w(TAG, e);
    } finally {
        BlobProvider.getInstance().delete(context, writeDetails.uri);
    }
}
Also used : DeviceContact(org.whispersystems.signalservice.api.messages.multidevice.DeviceContact) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) DeviceContactsOutputStream(org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsOutputStream) InvalidNumberException(org.whispersystems.signalservice.api.util.InvalidNumberException) IdentityRecord(org.thoughtcrime.securesms.database.model.IdentityRecord) Recipient(org.thoughtcrime.securesms.recipients.Recipient) ProfileKey(org.signal.zkgroup.profiles.ProfileKey) VerifiedMessage(org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage)

Example 4 with DeviceContact

use of org.whispersystems.signalservice.api.messages.multidevice.DeviceContact 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 5 with DeviceContact

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

the class MultiDeviceContactUpdateJob method generateFullContactUpdate.

private void generateFullContactUpdate() throws IOException, UntrustedIdentityException, NetworkException {
    if (!Permissions.hasAny(context, Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS)) {
        Log.w(TAG, "No contact permissions, skipping multi-device contact update...");
        return;
    }
    File contactDataFile = createTempFile("multidevice-contact-update");
    try {
        DeviceContactsOutputStream out = new DeviceContactsOutputStream(new FileOutputStream(contactDataFile));
        Collection<ContactData> contacts = ContactAccessor.getInstance().getContactsWithPush(context);
        for (ContactData contactData : contacts) {
            Uri contactUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactData.id));
            Address address = Address.fromExternal(context, contactData.numbers.get(0).number);
            Recipient recipient = Recipient.from(context, address, false);
            Optional<IdentityDatabase.IdentityRecord> identity = DatabaseFactory.getIdentityDatabase(context).getIdentity(address);
            Optional<VerifiedMessage> verified = getVerifiedMessage(recipient, identity);
            Optional<String> name = Optional.fromNullable(contactData.name);
            Optional<String> color = Optional.of(recipient.getColor().serialize());
            Optional<byte[]> profileKey = Optional.fromNullable(recipient.getProfileKey());
            boolean blocked = recipient.isBlocked();
            Optional<Integer> expireTimer = recipient.getExpireMessages() > 0 ? Optional.of(recipient.getExpireMessages()) : Optional.absent();
            out.write(new DeviceContact(address.toPhoneString(), name, getAvatar(contactUri), color, verified, profileKey, blocked, expireTimer));
        }
        if (ProfileKeyUtil.hasProfileKey(context)) {
            out.write(new DeviceContact(TextSecurePreferences.getLocalNumber(context), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), Optional.of(ProfileKeyUtil.getProfileKey(context)), false, Optional.absent()));
        }
        out.close();
        sendUpdate(messageSender, contactDataFile, true);
    } catch (InvalidNumberException e) {
        Log.w(TAG, e);
    } finally {
        if (contactDataFile != null)
            contactDataFile.delete();
    }
}
Also used : DeviceContact(org.whispersystems.signalservice.api.messages.multidevice.DeviceContact) Address(org.thoughtcrime.securesms.database.Address) DeviceContactsOutputStream(org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsOutputStream) InvalidNumberException(org.whispersystems.signalservice.api.util.InvalidNumberException) Recipient(org.thoughtcrime.securesms.recipients.Recipient) Uri(android.net.Uri) FileOutputStream(java.io.FileOutputStream) ContactData(org.thoughtcrime.securesms.contacts.ContactAccessor.ContactData) VerifiedMessage(org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage) File(java.io.File)

Aggregations

DeviceContact (org.whispersystems.signalservice.api.messages.multidevice.DeviceContact)7 DeviceContactsOutputStream (org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsOutputStream)7 Recipient (org.thoughtcrime.securesms.recipients.Recipient)5 InvalidNumberException (org.whispersystems.signalservice.api.util.InvalidNumberException)5 VerifiedMessage (org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage)4 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ProfileKey (org.signal.zkgroup.profiles.ProfileKey)2 IdentityRecord (org.thoughtcrime.securesms.database.model.IdentityRecord)2 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)2 SignalServiceMessageSender (org.whispersystems.signalservice.api.SignalServiceMessageSender)2 SignalServiceAttachmentStream (org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream)2 ContactsMessage (org.whispersystems.signalservice.api.messages.multidevice.ContactsMessage)2 SignalServiceSyncMessage (org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage)2 Uri (android.net.Uri)1 ContactData (org.thoughtcrime.securesms.contacts.ContactAccessor.ContactData)1 Address (org.thoughtcrime.securesms.database.Address)1 NotPushRegisteredException (org.thoughtcrime.securesms.net.NotPushRegisteredException)1