Search in sources :

Example 16 with IdentityRecord

use of org.thoughtcrime.securesms.database.model.IdentityRecord in project Signal-Android by WhisperSystems.

the class IdentityDatabase method setVerified.

public void setVerified(@NonNull String addressName, @NonNull RecipientId recipientId, IdentityKey identityKey, VerifiedStatus verifiedStatus) {
    SQLiteDatabase database = databaseHelper.getSignalWritableDatabase();
    String query = ADDRESS + " = ? AND " + IDENTITY_KEY + " = ?";
    String[] args = SqlUtil.buildArgs(addressName, Base64.encodeBytes(identityKey.serialize()));
    ContentValues contentValues = new ContentValues(1);
    contentValues.put(VERIFIED, verifiedStatus.toInt());
    int updated = database.update(TABLE_NAME, contentValues, query, args);
    if (updated > 0) {
        Optional<IdentityRecord> record = getIdentityRecord(addressName);
        if (record.isPresent())
            EventBus.getDefault().post(record.get());
        SignalDatabase.recipients().markNeedsSync(recipientId);
    }
}
Also used : ContentValues(android.content.ContentValues) IdentityRecord(org.thoughtcrime.securesms.database.model.IdentityRecord)

Example 17 with IdentityRecord

use of org.thoughtcrime.securesms.database.model.IdentityRecord in project Signal-Android by WhisperSystems.

the class IdentityDatabase method saveIdentityInternal.

private void saveIdentityInternal(@NonNull String addressName, @NonNull RecipientId recipientId, IdentityKey identityKey, VerifiedStatus verifiedStatus, boolean firstUse, long timestamp, boolean nonBlockingApproval) {
    SQLiteDatabase database = databaseHelper.getSignalWritableDatabase();
    String identityKeyString = Base64.encodeBytes(identityKey.serialize());
    ContentValues contentValues = new ContentValues();
    contentValues.put(ADDRESS, addressName);
    contentValues.put(IDENTITY_KEY, identityKeyString);
    contentValues.put(TIMESTAMP, timestamp);
    contentValues.put(VERIFIED, verifiedStatus.toInt());
    contentValues.put(NONBLOCKING_APPROVAL, nonBlockingApproval ? 1 : 0);
    contentValues.put(FIRST_USE, firstUse ? 1 : 0);
    database.replace(TABLE_NAME, null, contentValues);
    EventBus.getDefault().post(new IdentityRecord(recipientId, identityKey, verifiedStatus, firstUse, timestamp, nonBlockingApproval));
}
Also used : ContentValues(android.content.ContentValues) IdentityRecord(org.thoughtcrime.securesms.database.model.IdentityRecord)

Example 18 with IdentityRecord

use of org.thoughtcrime.securesms.database.model.IdentityRecord in project Signal-Android by signalapp.

the class IdentityDatabase method getIdentityRecord.

@NonNull
private static IdentityRecord getIdentityRecord(@NonNull Cursor cursor) throws IOException, InvalidKeyException {
    String addressName = CursorUtil.requireString(cursor, ADDRESS);
    String serializedIdentity = CursorUtil.requireString(cursor, IDENTITY_KEY);
    long timestamp = CursorUtil.requireLong(cursor, TIMESTAMP);
    int verifiedStatus = CursorUtil.requireInt(cursor, VERIFIED);
    boolean nonblockingApproval = CursorUtil.requireBoolean(cursor, NONBLOCKING_APPROVAL);
    boolean firstUse = CursorUtil.requireBoolean(cursor, FIRST_USE);
    IdentityKey identity = new IdentityKey(Base64.decode(serializedIdentity), 0);
    return new IdentityRecord(RecipientId.fromExternalPush(addressName), identity, VerifiedStatus.forState(verifiedStatus), firstUse, timestamp, nonblockingApproval);
}
Also used : IdentityKey(org.whispersystems.libsignal.IdentityKey) IdentityRecord(org.thoughtcrime.securesms.database.model.IdentityRecord) NonNull(androidx.annotation.NonNull)

Example 19 with IdentityRecord

use of org.thoughtcrime.securesms.database.model.IdentityRecord in project Signal-Android by signalapp.

the class SignalBaseIdentityKeyStore method getIdentityRecords.

@NonNull
public IdentityRecordList getIdentityRecords(@NonNull List<Recipient> recipients) {
    List<String> addressNames = recipients.stream().filter(Recipient::hasServiceId).map(Recipient::requireServiceId).map(ServiceId::toString).collect(Collectors.toList());
    if (addressNames.isEmpty()) {
        return IdentityRecordList.EMPTY;
    }
    List<IdentityRecord> records = new ArrayList<>(recipients.size());
    for (Recipient recipient : recipients) {
        if (recipient.hasServiceId()) {
            IdentityStoreRecord record = cache.get(recipient.requireServiceId().toString());
            if (record != null) {
                records.add(record.toIdentityRecord(recipient.getId()));
            }
        } else {
            Log.w(TAG, "[getIdentityRecords] No serviceId for " + recipient.getId());
        }
    }
    return new IdentityRecordList(records);
}
Also used : IdentityRecordList(org.thoughtcrime.securesms.database.identity.IdentityRecordList) ArrayList(java.util.ArrayList) IdentityRecord(org.thoughtcrime.securesms.database.model.IdentityRecord) Recipient(org.thoughtcrime.securesms.recipients.Recipient) IdentityStoreRecord(org.thoughtcrime.securesms.database.model.IdentityStoreRecord) NonNull(androidx.annotation.NonNull)

Example 20 with IdentityRecord

use of org.thoughtcrime.securesms.database.model.IdentityRecord in project Signal-Android by signalapp.

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)

Aggregations

IdentityRecord (org.thoughtcrime.securesms.database.model.IdentityRecord)20 Recipient (org.thoughtcrime.securesms.recipients.Recipient)10 SignalIdentityKeyStore (org.thoughtcrime.securesms.crypto.storage.SignalIdentityKeyStore)6 SignalSessionLock (org.whispersystems.signalservice.api.SignalSessionLock)6 ContentValues (android.content.ContentValues)4 NonNull (androidx.annotation.NonNull)4 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)4 DeviceContact (org.whispersystems.signalservice.api.messages.multidevice.DeviceContact)4 DeviceContactsOutputStream (org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsOutputStream)4 VerifiedMessage (org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage)4 InvalidNumberException (org.whispersystems.signalservice.api.util.InvalidNumberException)4 WorkerThread (androidx.annotation.WorkerThread)2 ArrayList (java.util.ArrayList)2 ProfileKey (org.signal.zkgroup.profiles.ProfileKey)2 IdentityRecordList (org.thoughtcrime.securesms.database.identity.IdentityRecordList)2 IdentityStoreRecord (org.thoughtcrime.securesms.database.model.IdentityStoreRecord)2 LiveRecipient (org.thoughtcrime.securesms.recipients.LiveRecipient)2 SingleLiveEvent (org.thoughtcrime.securesms.util.SingleLiveEvent)2 IdentityKey (org.whispersystems.libsignal.IdentityKey)2