Search in sources :

Example 31 with Address

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

the class RemoteReplyReceiver method onReceive.

@SuppressLint("StaticFieldLeak")
@Override
public void onReceive(final Context context, Intent intent) {
    if (!REPLY_ACTION.equals(intent.getAction()))
        return;
    Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
    if (remoteInput == null)
        return;
    final Address address = intent.getParcelableExtra(ADDRESS_EXTRA);
    final CharSequence responseText = remoteInput.getCharSequence(MessageNotifier.EXTRA_REMOTE_REPLY);
    if (responseText != null) {
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                long threadId;
                Recipient recipient = Recipient.from(context, address, false);
                int subscriptionId = recipient.getDefaultSubscriptionId().or(-1);
                long expiresIn = recipient.getExpireMessages() * 1000L;
                if (recipient.isGroupRecipient()) {
                    OutgoingMediaMessage reply = new OutgoingMediaMessage(recipient, responseText.toString(), new LinkedList<>(), System.currentTimeMillis(), subscriptionId, expiresIn, 0);
                    threadId = MessageSender.send(context, reply, -1, false, null);
                } else {
                    OutgoingTextMessage reply = new OutgoingTextMessage(recipient, responseText.toString(), expiresIn, subscriptionId);
                    threadId = MessageSender.send(context, reply, -1, false, null);
                }
                List<MarkedMessageInfo> messageIds = DatabaseFactory.getThreadDatabase(context).setRead(threadId, true);
                MessageNotifier.updateNotification(context);
                MarkReadReceiver.process(context, messageIds);
                return null;
            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }
}
Also used : OutgoingTextMessage(org.thoughtcrime.securesms.sms.OutgoingTextMessage) Address(org.thoughtcrime.securesms.database.Address) Bundle(android.os.Bundle) Recipient(org.thoughtcrime.securesms.recipients.Recipient) OutgoingMediaMessage(org.thoughtcrime.securesms.mms.OutgoingMediaMessage) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList) SuppressLint(android.annotation.SuppressLint)

Example 32 with Address

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

the class RecipientProvider method getGroupRecipientDetails.

@NonNull
private RecipientDetails getGroupRecipientDetails(Context context, Address groupId, Optional<GroupRecord> groupRecord, Optional<RecipientSettings> settings, boolean asynchronous) {
    if (!groupRecord.isPresent()) {
        groupRecord = DatabaseFactory.getGroupDatabase(context).getGroup(groupId.toGroupString());
    }
    if (!settings.isPresent()) {
        settings = DatabaseFactory.getRecipientDatabase(context).getRecipientSettings(groupId);
    }
    if (groupRecord.isPresent()) {
        String title = groupRecord.get().getTitle();
        List<Address> memberAddresses = groupRecord.get().getMembers();
        List<Recipient> members = new LinkedList<>();
        Long avatarId = null;
        for (Address memberAddress : memberAddresses) {
            members.add(getRecipient(context, memberAddress, Optional.absent(), Optional.absent(), asynchronous));
        }
        if (!groupId.isMmsGroup() && title == null) {
            title = context.getString(R.string.RecipientProvider_unnamed_group);
        }
        if (groupRecord.get().getAvatar() != null && groupRecord.get().getAvatar().length > 0) {
            avatarId = groupRecord.get().getAvatarId();
        }
        return new RecipientDetails(title, avatarId, false, settings.orNull(), members);
    }
    return new RecipientDetails(context.getString(R.string.RecipientProvider_unnamed_group), null, false, settings.orNull(), null);
}
Also used : Address(org.thoughtcrime.securesms.database.Address) LinkedList(java.util.LinkedList) NonNull(android.support.annotation.NonNull)

Example 33 with Address

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

the class SessionStoreMigrationHelper method migrateSessions.

static void migrateSessions(Context context, SQLiteDatabase database) {
    File directory = new File(context.getFilesDir(), SESSIONS_DIRECTORY_V2);
    if (directory.exists()) {
        File[] sessionFiles = directory.listFiles();
        if (sessionFiles != null) {
            for (File sessionFile : sessionFiles) {
                try {
                    String[] parts = sessionFile.getName().split("[.]");
                    Address address = Address.fromSerialized(parts[0]);
                    int deviceId;
                    if (parts.length > 1)
                        deviceId = Integer.parseInt(parts[1]);
                    else
                        deviceId = SignalServiceAddress.DEFAULT_DEVICE_ID;
                    FileInputStream in = new FileInputStream(sessionFile);
                    int versionMarker = readInteger(in);
                    if (versionMarker > CURRENT_VERSION) {
                        throw new AssertionError("Unknown version: " + versionMarker + ", " + sessionFile.getAbsolutePath());
                    }
                    byte[] serialized = readBlob(in);
                    in.close();
                    if (versionMarker < PLAINTEXT_VERSION) {
                        throw new AssertionError("Not plaintext: " + versionMarker + ", " + sessionFile.getAbsolutePath());
                    }
                    SessionRecord sessionRecord;
                    if (versionMarker == SINGLE_STATE_VERSION) {
                        Log.w(TAG, "Migrating single state version: " + sessionFile.getAbsolutePath());
                        SessionStructure sessionStructure = SessionStructure.parseFrom(serialized);
                        SessionState sessionState = new SessionState(sessionStructure);
                        sessionRecord = new SessionRecord(sessionState);
                    } else if (versionMarker >= ARCHIVE_STATES_VERSION) {
                        Log.w(TAG, "Migrating session: " + sessionFile.getAbsolutePath());
                        sessionRecord = new SessionRecord(serialized);
                    } else {
                        throw new AssertionError("Unknown version: " + versionMarker + ", " + sessionFile.getAbsolutePath());
                    }
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(SessionDatabase.ADDRESS, address.serialize());
                    contentValues.put(SessionDatabase.DEVICE, deviceId);
                    contentValues.put(SessionDatabase.RECORD, sessionRecord.serialize());
                    database.insert(SessionDatabase.TABLE_NAME, null, contentValues);
                } catch (NumberFormatException | IOException e) {
                    Log.w(TAG, e);
                }
            }
        }
    }
}
Also used : SessionStructure(org.whispersystems.libsignal.state.StorageProtos.SessionStructure) ContentValues(android.content.ContentValues) SessionState(org.whispersystems.libsignal.state.SessionState) Address(org.thoughtcrime.securesms.database.Address) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) File(java.io.File) SessionRecord(org.whispersystems.libsignal.state.SessionRecord)

Example 34 with Address

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

the class PushDecryptJob method handleUntrustedIdentityMessage.

private void handleUntrustedIdentityMessage(@NonNull SignalServiceEnvelope envelope, @NonNull Optional<Long> smsMessageId) {
    try {
        SmsDatabase database = DatabaseFactory.getSmsDatabase(context);
        Address sourceAddress = Address.fromExternal(context, envelope.getSource());
        byte[] serialized = envelope.hasLegacyMessage() ? envelope.getLegacyMessage() : envelope.getContent();
        PreKeySignalMessage whisperMessage = new PreKeySignalMessage(serialized);
        IdentityKey identityKey = whisperMessage.getIdentityKey();
        String encoded = Base64.encodeBytes(serialized);
        IncomingTextMessage textMessage = new IncomingTextMessage(sourceAddress, envelope.getSourceDevice(), envelope.getTimestamp(), encoded, Optional.absent(), 0);
        if (!smsMessageId.isPresent()) {
            IncomingPreKeyBundleMessage bundleMessage = new IncomingPreKeyBundleMessage(textMessage, encoded, envelope.hasLegacyMessage());
            Optional<InsertResult> insertResult = database.insertMessageInbox(bundleMessage);
            if (insertResult.isPresent()) {
                database.setMismatchedIdentity(insertResult.get().getMessageId(), sourceAddress, identityKey);
                MessageNotifier.updateNotification(context, insertResult.get().getThreadId());
            }
        } else {
            database.updateMessageBody(smsMessageId.get(), encoded);
            database.markAsPreKeyBundle(smsMessageId.get());
            database.setMismatchedIdentity(smsMessageId.get(), sourceAddress, identityKey);
        }
    } catch (InvalidMessageException | InvalidVersionException e) {
        throw new AssertionError(e);
    }
}
Also used : InsertResult(org.thoughtcrime.securesms.database.MessagingDatabase.InsertResult) InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) IdentityKey(org.whispersystems.libsignal.IdentityKey) Address(org.thoughtcrime.securesms.database.Address) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) InvalidVersionException(org.whispersystems.libsignal.InvalidVersionException) IncomingPreKeyBundleMessage(org.thoughtcrime.securesms.sms.IncomingPreKeyBundleMessage) PreKeySignalMessage(org.whispersystems.libsignal.protocol.PreKeySignalMessage) IncomingTextMessage(org.thoughtcrime.securesms.sms.IncomingTextMessage) SmsDatabase(org.thoughtcrime.securesms.database.SmsDatabase)

Example 35 with Address

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

the class PushDecryptJob method handleProfileKey.

private void handleProfileKey(@NonNull SignalServiceEnvelope envelope, @NonNull SignalServiceDataMessage message) {
    RecipientDatabase database = DatabaseFactory.getRecipientDatabase(context);
    Address sourceAddress = Address.fromExternal(context, envelope.getSource());
    Recipient recipient = Recipient.from(context, sourceAddress, false);
    if (recipient.getProfileKey() == null || !MessageDigest.isEqual(recipient.getProfileKey(), message.getProfileKey().get())) {
        database.setProfileKey(recipient, message.getProfileKey().get());
        ApplicationContext.getInstance(context).getJobManager().add(new RetrieveProfileJob(context, recipient));
    }
}
Also used : RecipientDatabase(org.thoughtcrime.securesms.database.RecipientDatabase) Address(org.thoughtcrime.securesms.database.Address) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) Recipient(org.thoughtcrime.securesms.recipients.Recipient)

Aggregations

Address (org.thoughtcrime.securesms.database.Address)40 Recipient (org.thoughtcrime.securesms.recipients.Recipient)13 LinkedList (java.util.LinkedList)10 Uri (android.net.Uri)9 GroupDatabase (org.thoughtcrime.securesms.database.GroupDatabase)7 Cursor (android.database.Cursor)6 ByteString (com.google.protobuf.ByteString)6 IOException (java.io.IOException)6 File (java.io.File)5 GroupContext (org.whispersystems.signalservice.internal.push.SignalServiceProtos.GroupContext)5 NonNull (android.support.annotation.NonNull)4 Attachment (org.thoughtcrime.securesms.attachments.Attachment)4 InsertResult (org.thoughtcrime.securesms.database.MessagingDatabase.InsertResult)4 SignalServiceAttachment (org.whispersystems.signalservice.api.messages.SignalServiceAttachment)4 SignalServiceAddress (org.whispersystems.signalservice.api.push.SignalServiceAddress)4 SuppressLint (android.annotation.SuppressLint)3 Nullable (android.support.annotation.Nullable)3 HashSet (java.util.HashSet)3 List (java.util.List)3 ContentValues (android.content.ContentValues)2