Search in sources :

Example 1 with InvalidNumberException

use of org.whispersystems.signalservice.api.util.InvalidNumberException in project Signal-Android by WhisperSystems.

the class ContactsDatabase method getSignalRawContacts.

@NonNull
private Map<String, SignalContact> getSignalRawContacts(@NonNull Account account, @NonNull String localNumber) {
    Uri currentContactsUri = RawContacts.CONTENT_URI.buildUpon().appendQueryParameter(RawContacts.ACCOUNT_NAME, account.name).appendQueryParameter(RawContacts.ACCOUNT_TYPE, account.type).build();
    Map<String, SignalContact> signalContacts = new HashMap<>();
    Cursor cursor = null;
    try {
        String[] projection;
        if (Build.VERSION.SDK_INT >= 11) {
            projection = new String[] { BaseColumns._ID, RawContacts.SYNC1, RawContacts.SYNC4, RawContacts.CONTACT_ID, RawContacts.DISPLAY_NAME_PRIMARY, RawContacts.DISPLAY_NAME_SOURCE };
        } else {
            projection = new String[] { BaseColumns._ID, RawContacts.SYNC1, RawContacts.SYNC4, RawContacts.CONTACT_ID };
        }
        cursor = context.getContentResolver().query(currentContactsUri, projection, null, null, null);
        while (cursor != null && cursor.moveToNext()) {
            String currentNumber;
            try {
                currentNumber = PhoneNumberFormatter.formatNumber(cursor.getString(1), localNumber);
            } catch (InvalidNumberException e) {
                Log.w(TAG, e);
                currentNumber = cursor.getString(1);
            }
            long rawContactId = cursor.getLong(0);
            long contactId = cursor.getLong(3);
            String supportsVoice = cursor.getString(2);
            String rawContactDisplayName = null;
            String aggregateDisplayName = null;
            int rawContactDisplayNameSource = 0;
            if (Build.VERSION.SDK_INT >= 11) {
                rawContactDisplayName = cursor.getString(4);
                rawContactDisplayNameSource = cursor.getInt(5);
                aggregateDisplayName = getDisplayName(contactId);
            }
            signalContacts.put(currentNumber, new SignalContact(rawContactId, supportsVoice, rawContactDisplayName, aggregateDisplayName, rawContactDisplayNameSource));
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return signalContacts;
}
Also used : HashMap(java.util.HashMap) InvalidNumberException(org.whispersystems.signalservice.api.util.InvalidNumberException) Cursor(android.database.Cursor) Uri(android.net.Uri) NonNull(android.support.annotation.NonNull)

Example 2 with InvalidNumberException

use of org.whispersystems.signalservice.api.util.InvalidNumberException in project Signal-Android by WhisperSystems.

the class MmsDatabase method incrementDeliveryReceiptCount.

public void incrementDeliveryReceiptCount(SyncMessageId messageId) {
    MmsAddressDatabase addressDatabase = DatabaseFactory.getMmsAddressDatabase(context);
    SQLiteDatabase database = databaseHelper.getWritableDatabase();
    Cursor cursor = null;
    boolean found = false;
    try {
        cursor = database.query(TABLE_NAME, new String[] { ID, THREAD_ID, MESSAGE_BOX }, DATE_SENT + " = ?", new String[] { String.valueOf(messageId.getTimetamp()) }, null, null, null, null);
        while (cursor.moveToNext()) {
            if (Types.isOutgoingMessageType(cursor.getLong(cursor.getColumnIndexOrThrow(MESSAGE_BOX)))) {
                List<String> addresses = addressDatabase.getAddressesListForId(cursor.getLong(cursor.getColumnIndexOrThrow(ID)));
                for (String storedAddress : addresses) {
                    try {
                        String ourAddress = canonicalizeNumber(context, messageId.getAddress());
                        String theirAddress = canonicalizeNumberOrGroup(context, storedAddress);
                        if (ourAddress.equals(theirAddress) || GroupUtil.isEncodedGroup(theirAddress)) {
                            long id = cursor.getLong(cursor.getColumnIndexOrThrow(ID));
                            long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(THREAD_ID));
                            found = true;
                            database.execSQL("UPDATE " + TABLE_NAME + " SET " + RECEIPT_COUNT + " = " + RECEIPT_COUNT + " + 1 WHERE " + ID + " = ?", new String[] { String.valueOf(id) });
                            DatabaseFactory.getThreadDatabase(context).update(threadId, false);
                            notifyConversationListeners(threadId);
                        }
                    } catch (InvalidNumberException e) {
                        Log.w("MmsDatabase", e);
                    }
                }
            }
        }
        if (!found) {
            try {
                earlyReceiptCache.increment(messageId.getTimetamp(), canonicalizeNumber(context, messageId.getAddress()));
            } catch (InvalidNumberException e) {
                Log.w(TAG, e);
            }
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) InvalidNumberException(org.whispersystems.signalservice.api.util.InvalidNumberException) Cursor(android.database.Cursor)

Example 3 with InvalidNumberException

use of org.whispersystems.signalservice.api.util.InvalidNumberException in project Signal-Android by WhisperSystems.

the class SmsDatabase method insertMessageOutbox.

protected long insertMessageOutbox(long threadId, OutgoingTextMessage message, long type, boolean forceSms, long date) {
    if (message.isKeyExchange())
        type |= Types.KEY_EXCHANGE_BIT;
    else if (message.isSecureMessage())
        type |= (Types.SECURE_MESSAGE_BIT | Types.PUSH_MESSAGE_BIT);
    else if (message.isEndSession())
        type |= Types.END_SESSION_BIT;
    if (forceSms)
        type |= Types.MESSAGE_FORCE_SMS_BIT;
    String address = message.getRecipients().getPrimaryRecipient().getNumber();
    ContentValues contentValues = new ContentValues(6);
    contentValues.put(ADDRESS, PhoneNumberUtils.formatNumber(address));
    contentValues.put(THREAD_ID, threadId);
    contentValues.put(BODY, message.getMessageBody());
    contentValues.put(DATE_RECEIVED, System.currentTimeMillis());
    contentValues.put(DATE_SENT, date);
    contentValues.put(READ, 1);
    contentValues.put(TYPE, type);
    contentValues.put(SUBSCRIPTION_ID, message.getSubscriptionId());
    contentValues.put(EXPIRES_IN, message.getExpiresIn());
    try {
        contentValues.put(RECEIPT_COUNT, earlyReceiptCache.remove(date, canonicalizeNumber(context, address)));
    } catch (InvalidNumberException e) {
        Log.w(TAG, e);
    }
    SQLiteDatabase db = databaseHelper.getWritableDatabase();
    long messageId = db.insert(TABLE_NAME, ADDRESS, contentValues);
    DatabaseFactory.getThreadDatabase(context).update(threadId, true);
    DatabaseFactory.getThreadDatabase(context).setLastSeen(threadId);
    notifyConversationListeners(threadId);
    jobManager.add(new TrimThreadJob(context, threadId));
    return messageId;
}
Also used : ContentValues(android.content.ContentValues) TrimThreadJob(org.thoughtcrime.securesms.jobs.TrimThreadJob) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) InvalidNumberException(org.whispersystems.signalservice.api.util.InvalidNumberException)

Example 4 with InvalidNumberException

use of org.whispersystems.signalservice.api.util.InvalidNumberException in project Signal-Android by WhisperSystems.

the class SmsDatabase method incrementDeliveryReceiptCount.

public void incrementDeliveryReceiptCount(SyncMessageId messageId) {
    SQLiteDatabase database = databaseHelper.getWritableDatabase();
    Cursor cursor = null;
    boolean foundMessage = false;
    try {
        cursor = database.query(TABLE_NAME, new String[] { ID, THREAD_ID, ADDRESS, TYPE }, DATE_SENT + " = ?", new String[] { String.valueOf(messageId.getTimetamp()) }, null, null, null, null);
        while (cursor.moveToNext()) {
            if (Types.isOutgoingMessageType(cursor.getLong(cursor.getColumnIndexOrThrow(TYPE)))) {
                try {
                    String theirAddress = canonicalizeNumber(context, messageId.getAddress());
                    String ourAddress = canonicalizeNumber(context, cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS)));
                    if (ourAddress.equals(theirAddress)) {
                        long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(THREAD_ID));
                        database.execSQL("UPDATE " + TABLE_NAME + " SET " + RECEIPT_COUNT + " = " + RECEIPT_COUNT + " + 1 WHERE " + ID + " = ?", new String[] { String.valueOf(cursor.getLong(cursor.getColumnIndexOrThrow(ID))) });
                        DatabaseFactory.getThreadDatabase(context).update(threadId, false);
                        notifyConversationListeners(threadId);
                        foundMessage = true;
                    }
                } catch (InvalidNumberException e) {
                    Log.w(TAG, e);
                }
            }
        }
        if (!foundMessage) {
            try {
                earlyReceiptCache.increment(messageId.getTimetamp(), canonicalizeNumber(context, messageId.getAddress()));
            } catch (InvalidNumberException e) {
                Log.w(TAG, e);
            }
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) InvalidNumberException(org.whispersystems.signalservice.api.util.InvalidNumberException) Cursor(android.database.Cursor)

Example 5 with InvalidNumberException

use of org.whispersystems.signalservice.api.util.InvalidNumberException in project Signal-Android by WhisperSystems.

the class MultiDeviceBlockedUpdateJob method onRun.

@Override
public void onRun(MasterSecret masterSecret) throws IOException, UntrustedIdentityException {
    RecipientPreferenceDatabase database = DatabaseFactory.getRecipientPreferenceDatabase(context);
    SignalServiceMessageSender messageSender = messageSenderFactory.create();
    BlockedReader reader = database.readerForBlocked(database.getBlocked());
    List<String> blocked = new LinkedList<>();
    Recipients recipients;
    while ((recipients = reader.getNext()) != null) {
        if (recipients.isSingleRecipient()) {
            try {
                blocked.add(Util.canonicalizeNumber(context, recipients.getPrimaryRecipient().getNumber()));
            } catch (InvalidNumberException e) {
                Log.w(TAG, e);
            }
        }
    }
    messageSender.sendMessage(SignalServiceSyncMessage.forBlocked(new BlockedListMessage(blocked)));
}
Also used : RecipientPreferenceDatabase(org.thoughtcrime.securesms.database.RecipientPreferenceDatabase) Recipients(org.thoughtcrime.securesms.recipients.Recipients) InvalidNumberException(org.whispersystems.signalservice.api.util.InvalidNumberException) SignalServiceMessageSender(org.whispersystems.signalservice.api.SignalServiceMessageSender) BlockedReader(org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.BlockedReader) BlockedListMessage(org.whispersystems.signalservice.api.messages.multidevice.BlockedListMessage) LinkedList(java.util.LinkedList)

Aggregations

InvalidNumberException (org.whispersystems.signalservice.api.util.InvalidNumberException)22 Recipient (org.thoughtcrime.securesms.recipients.Recipient)9 Cursor (android.database.Cursor)6 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)6 LinkedList (java.util.LinkedList)5 DeviceContact (org.whispersystems.signalservice.api.messages.multidevice.DeviceContact)5 DeviceContactsOutputStream (org.whispersystems.signalservice.api.messages.multidevice.DeviceContactsOutputStream)5 ContentValues (android.content.ContentValues)4 VerifiedMessage (org.whispersystems.signalservice.api.messages.multidevice.VerifiedMessage)4 Uri (android.net.Uri)3 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 UndeliverableMessageException (org.thoughtcrime.securesms.transport.UndeliverableMessageException)3 SignalServiceMessageSender (org.whispersystems.signalservice.api.SignalServiceMessageSender)3 Pair (android.util.Pair)2 IOException (java.io.IOException)2 List (java.util.List)2 MmsDatabase (org.thoughtcrime.securesms.database.MmsDatabase)2 NetworkFailure (org.thoughtcrime.securesms.database.documents.NetworkFailure)2 IdentityRecord (org.thoughtcrime.securesms.database.model.IdentityRecord)2