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();
}
}
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();
}
}
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);
}
}
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);
}
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();
}
}
Aggregations