2017-11-14 02:01:05 +00:00
|
|
|
/*
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2017-11-14 02:01:05 +00:00
|
|
|
* Copyright (C) 2013 - 2017 Open Whisper Systems
|
2012-09-09 23:10:46 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2012-09-09 23:10:46 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms.database;
|
|
|
|
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
2020-08-19 00:06:26 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2014-11-12 19:15:05 +00:00
|
|
|
import android.text.TextUtils;
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
import android.util.Pair;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2017-10-02 21:54:55 +00:00
|
|
|
import com.annimon.stream.Stream;
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
import net.sqlcipher.database.SQLiteDatabase;
|
|
|
|
import net.sqlcipher.database.SQLiteStatement;
|
|
|
|
|
2014-11-25 06:50:32 +00:00
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
2015-01-15 21:35:35 +00:00
|
|
|
import org.thoughtcrime.securesms.database.documents.IdentityKeyMismatch;
|
|
|
|
import org.thoughtcrime.securesms.database.documents.IdentityKeyMismatchList;
|
2018-01-25 03:17:44 +00:00
|
|
|
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper;
|
2017-04-22 23:29:26 +00:00
|
|
|
import org.thoughtcrime.securesms.database.model.MessageRecord;
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
import org.thoughtcrime.securesms.database.model.SmsMessageRecord;
|
2014-11-25 06:50:32 +00:00
|
|
|
import org.thoughtcrime.securesms.jobs.TrimThreadJob;
|
2018-05-22 09:13:10 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2017-08-01 15:56:00 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2014-02-20 05:06:54 +00:00
|
|
|
import org.thoughtcrime.securesms.sms.IncomingGroupMessage;
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
import org.thoughtcrime.securesms.sms.IncomingTextMessage;
|
|
|
|
import org.thoughtcrime.securesms.sms.OutgoingTextMessage;
|
2015-01-15 21:35:35 +00:00
|
|
|
import org.thoughtcrime.securesms.util.JsonUtils;
|
2017-09-16 05:38:53 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2017-01-22 21:52:36 +00:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2015-01-15 21:35:35 +00:00
|
|
|
import java.io.IOException;
|
2017-04-22 23:29:26 +00:00
|
|
|
import java.security.SecureRandom;
|
2019-10-29 00:23:30 +00:00
|
|
|
import java.util.HashSet;
|
2015-01-15 21:35:35 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2017-10-02 21:54:55 +00:00
|
|
|
import java.util.Map;
|
2012-09-09 23:10:46 +00:00
|
|
|
import java.util.Set;
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
/**
|
|
|
|
* Database for storage of SMS messages.
|
2012-09-09 23:10:46 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* @author Moxie Marlinspike
|
|
|
|
*/
|
2015-01-15 21:35:35 +00:00
|
|
|
public class SmsDatabase extends MessagingDatabase {
|
|
|
|
|
|
|
|
private static final String TAG = SmsDatabase.class.getSimpleName();
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public static final String TABLE_NAME = "sms";
|
|
|
|
public static final String PERSON = "person";
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
static final String DATE_RECEIVED = "date";
|
|
|
|
static final String DATE_SENT = "date_sent";
|
2011-12-20 18:20:44 +00:00
|
|
|
public static final String PROTOCOL = "protocol";
|
|
|
|
public static final String STATUS = "status";
|
|
|
|
public static final String TYPE = "type";
|
|
|
|
public static final String REPLY_PATH_PRESENT = "reply_path_present";
|
|
|
|
public static final String SUBJECT = "subject";
|
|
|
|
public static final String SERVICE_CENTER = "service_center";
|
2012-09-09 23:10:46 +00:00
|
|
|
|
|
|
|
public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + ID + " integer PRIMARY KEY, " +
|
2014-02-03 03:38:06 +00:00
|
|
|
THREAD_ID + " INTEGER, " + ADDRESS + " TEXT, " + ADDRESS_DEVICE_ID + " INTEGER DEFAULT 1, " + PERSON + " INTEGER, " +
|
|
|
|
DATE_RECEIVED + " INTEGER, " + DATE_SENT + " INTEGER, " + PROTOCOL + " INTEGER, " + READ + " INTEGER DEFAULT 0, " +
|
2013-01-06 21:13:14 +00:00
|
|
|
STATUS + " INTEGER DEFAULT -1," + TYPE + " INTEGER, " + REPLY_PATH_PRESENT + " INTEGER, " +
|
2017-09-16 05:38:53 +00:00
|
|
|
DELIVERY_RECEIPT_COUNT + " INTEGER DEFAULT 0," + SUBJECT + " TEXT, " + BODY + " TEXT, " +
|
2016-08-16 03:23:56 +00:00
|
|
|
MISMATCHED_IDENTITIES + " TEXT DEFAULT NULL, " + SERVICE_CENTER + " TEXT, " + SUBSCRIPTION_ID + " INTEGER DEFAULT -1, " +
|
2017-09-16 05:38:53 +00:00
|
|
|
EXPIRES_IN + " INTEGER DEFAULT 0, " + EXPIRE_STARTED + " INTEGER DEFAULT 0, " + NOTIFIED + " DEFAULT 0, " +
|
2019-06-17 00:54:38 +00:00
|
|
|
READ_RECEIPT_COUNT + " INTEGER DEFAULT 0, " + UNIDENTIFIED + " INTEGER DEFAULT 0);";
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2012-10-30 00:41:06 +00:00
|
|
|
public static final String[] CREATE_INDEXS = {
|
|
|
|
"CREATE INDEX IF NOT EXISTS sms_thread_id_index ON " + TABLE_NAME + " (" + THREAD_ID + ");",
|
|
|
|
"CREATE INDEX IF NOT EXISTS sms_read_index ON " + TABLE_NAME + " (" + READ + ");",
|
2017-03-09 01:38:55 +00:00
|
|
|
"CREATE INDEX IF NOT EXISTS sms_read_and_notified_and_thread_id_index ON " + TABLE_NAME + "(" + READ + "," + NOTIFIED + "," + THREAD_ID + ");",
|
2014-07-25 22:14:29 +00:00
|
|
|
"CREATE INDEX IF NOT EXISTS sms_type_index ON " + TABLE_NAME + " (" + TYPE + ");",
|
2015-08-04 20:37:22 +00:00
|
|
|
"CREATE INDEX IF NOT EXISTS sms_date_sent_index ON " + TABLE_NAME + " (" + DATE_SENT + ");",
|
|
|
|
"CREATE INDEX IF NOT EXISTS sms_thread_date_index ON " + TABLE_NAME + " (" + THREAD_ID + ", " + DATE_RECEIVED + ");"
|
2012-10-30 00:41:06 +00:00
|
|
|
};
|
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
private static final String[] MESSAGE_PROJECTION = new String[] {
|
2014-02-03 03:38:06 +00:00
|
|
|
ID, THREAD_ID, ADDRESS, ADDRESS_DEVICE_ID, PERSON,
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
DATE_RECEIVED + " AS " + NORMALIZED_DATE_RECEIVED,
|
|
|
|
DATE_SENT + " AS " + NORMALIZED_DATE_SENT,
|
|
|
|
PROTOCOL, READ, STATUS, TYPE,
|
2017-09-16 05:38:53 +00:00
|
|
|
REPLY_PATH_PRESENT, SUBJECT, BODY, SERVICE_CENTER, DELIVERY_RECEIPT_COUNT,
|
2017-03-09 01:38:55 +00:00
|
|
|
MISMATCHED_IDENTITIES, SUBSCRIPTION_ID, EXPIRES_IN, EXPIRE_STARTED,
|
2018-05-22 09:13:10 +00:00
|
|
|
NOTIFIED, READ_RECEIPT_COUNT, UNIDENTIFIED
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
};
|
|
|
|
|
2017-09-16 05:38:53 +00:00
|
|
|
private static final EarlyReceiptCache earlyDeliveryReceiptCache = new EarlyReceiptCache();
|
|
|
|
private static final EarlyReceiptCache earlyReadReceiptCache = new EarlyReceiptCache();
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public SmsDatabase(Context context, SQLCipherOpenHelper databaseHelper) {
|
2011-12-20 18:20:44 +00:00
|
|
|
super(context, databaseHelper);
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2015-01-15 21:35:35 +00:00
|
|
|
protected String getTableName() {
|
|
|
|
return TABLE_NAME;
|
|
|
|
}
|
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
private void updateTypeBitmask(long id, long maskOff, long maskOn) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i("MessageDatabase", "Updating ID: " + id + " to base type: " + maskOn);
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
db.execSQL("UPDATE " + TABLE_NAME +
|
|
|
|
" SET " + TYPE + " = (" + TYPE + " & " + (Types.TOTAL_MASK - maskOff) + " | " + maskOn + " )" +
|
|
|
|
" WHERE " + ID + " = ?", new String[] {id+""});
|
2012-09-09 23:10:46 +00:00
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
long threadId = getThreadIdForMessage(id);
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2015-11-23 23:07:41 +00:00
|
|
|
DatabaseFactory.getThreadDatabase(context).update(threadId, false);
|
2011-12-20 18:20:44 +00:00
|
|
|
notifyConversationListeners(threadId);
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public long getThreadIdForMessage(long id) {
|
|
|
|
String sql = "SELECT " + THREAD_ID + " FROM " + TABLE_NAME + " WHERE " + ID + " = ?";
|
|
|
|
String[] sqlArgs = new String[] {id+""};
|
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
Cursor cursor = null;
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
|
|
|
cursor = db.rawQuery(sql, sqlArgs);
|
|
|
|
if (cursor != null && cursor.moveToFirst())
|
2012-10-01 02:56:29 +00:00
|
|
|
return cursor.getLong(0);
|
2011-12-20 18:20:44 +00:00
|
|
|
else
|
2012-10-01 02:56:29 +00:00
|
|
|
return -1;
|
2011-12-20 18:20:44 +00:00
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
2012-10-01 02:56:29 +00:00
|
|
|
cursor.close();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2013-06-25 04:02:30 +00:00
|
|
|
public int getMessageCount() {
|
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
|
|
|
Cursor cursor = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
cursor = db.query(TABLE_NAME, new String[] {"COUNT(*)"}, null, null, null, null, null);
|
|
|
|
|
|
|
|
if (cursor != null && cursor.moveToFirst()) return cursor.getInt(0);
|
|
|
|
else return 0;
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public int getMessageCountForThread(long threadId) {
|
2012-09-09 23:10:46 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
2011-12-20 18:20:44 +00:00
|
|
|
Cursor cursor = null;
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
cursor = db.query(TABLE_NAME, new String[] {"COUNT(*)"}, THREAD_ID + " = ?",
|
|
|
|
new String[] {threadId+""}, null, null, null);
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (cursor != null && cursor.moveToFirst())
|
2012-10-01 02:56:29 +00:00
|
|
|
return cursor.getInt(0);
|
2011-12-20 18:20:44 +00:00
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
2012-10-01 02:56:29 +00:00
|
|
|
cursor.close();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2019-07-01 06:49:42 +00:00
|
|
|
public long getIDForMessageAtIndex(long threadID, int index) {
|
2019-06-27 06:03:05 +00:00
|
|
|
SQLiteDatabase database = databaseHelper.getReadableDatabase();
|
|
|
|
Cursor cursor = null;
|
|
|
|
try {
|
|
|
|
cursor = database.query(TABLE_NAME, null, THREAD_ID + " = ?", new String[] { threadID + "" }, null, null, null);
|
2019-07-01 06:49:42 +00:00
|
|
|
if (cursor != null && cursor.moveToPosition(index)) {
|
2019-06-27 06:03:05 +00:00
|
|
|
return cursor.getLong(0);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) {
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-10-29 00:23:30 +00:00
|
|
|
public Set<Long> getAllMessageIDs(long threadID) {
|
|
|
|
SQLiteDatabase database = databaseHelper.getReadableDatabase();
|
|
|
|
Cursor cursor = null;
|
|
|
|
Set<Long> messageIDs = new HashSet<>();
|
|
|
|
try {
|
|
|
|
cursor = database.query(TABLE_NAME, null, THREAD_ID + " = ?", new String[] { threadID + "" }, null, null, null);
|
|
|
|
while (cursor != null && cursor.moveToNext()) {
|
|
|
|
messageIDs.add(cursor.getLong(0));
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) {
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return messageIDs;
|
|
|
|
}
|
|
|
|
|
2015-01-15 21:35:35 +00:00
|
|
|
public void markAsEndSession(long id) {
|
|
|
|
updateTypeBitmask(id, Types.KEY_EXCHANGE_MASK, Types.END_SESSION_BIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void markAsPreKeyBundle(long id) {
|
|
|
|
updateTypeBitmask(id, Types.KEY_EXCHANGE_MASK, Types.KEY_EXCHANGE_BIT | Types.KEY_EXCHANGE_BUNDLE_BIT);
|
|
|
|
}
|
|
|
|
|
2013-11-26 01:00:20 +00:00
|
|
|
public void markAsInvalidVersionKeyExchange(long id) {
|
|
|
|
updateTypeBitmask(id, 0, Types.KEY_EXCHANGE_INVALID_VERSION_BIT);
|
|
|
|
}
|
|
|
|
|
2013-11-18 21:16:18 +00:00
|
|
|
public void markAsSecure(long id) {
|
|
|
|
updateTypeBitmask(id, 0, Types.SECURE_MESSAGE_BIT);
|
|
|
|
}
|
|
|
|
|
2014-04-01 01:47:24 +00:00
|
|
|
public void markAsInsecure(long id) {
|
|
|
|
updateTypeBitmask(id, Types.SECURE_MESSAGE_BIT, 0);
|
|
|
|
}
|
|
|
|
|
2014-02-21 07:00:38 +00:00
|
|
|
public void markAsPush(long id) {
|
|
|
|
updateTypeBitmask(id, 0, Types.PUSH_MESSAGE_BIT);
|
|
|
|
}
|
|
|
|
|
2014-03-01 22:17:55 +00:00
|
|
|
public void markAsForcedSms(long id) {
|
2015-06-03 22:51:27 +00:00
|
|
|
updateTypeBitmask(id, Types.PUSH_MESSAGE_BIT, Types.MESSAGE_FORCE_SMS_BIT);
|
2014-03-01 22:17:55 +00:00
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void markAsDecryptFailed(long id) {
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
updateTypeBitmask(id, Types.ENCRYPTION_MASK, Types.ENCRYPTION_REMOTE_FAILED_BIT);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2014-03-19 19:37:46 +00:00
|
|
|
public void markAsDecryptDuplicate(long id) {
|
|
|
|
updateTypeBitmask(id, Types.ENCRYPTION_MASK, Types.ENCRYPTION_REMOTE_DUPLICATE_BIT);
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void markAsNoSession(long id) {
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
updateTypeBitmask(id, Types.ENCRYPTION_MASK, Types.ENCRYPTION_REMOTE_NO_SESSION_BIT);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2020-05-12 23:07:51 +00:00
|
|
|
public void markAsSentLokiSessionRestorationRequest(long id) {
|
2019-12-05 01:59:37 +00:00
|
|
|
updateTypeBitmask(id, Types.ENCRYPTION_MASK, Types.ENCRYPTION_LOKI_SESSION_RESTORE_SENT_BIT);
|
|
|
|
}
|
|
|
|
|
2014-04-10 22:20:43 +00:00
|
|
|
public void markAsLegacyVersion(long id) {
|
|
|
|
updateTypeBitmask(id, Types.ENCRYPTION_MASK, Types.ENCRYPTION_REMOTE_LEGACY_BIT);
|
|
|
|
}
|
|
|
|
|
2013-05-06 02:11:11 +00:00
|
|
|
public void markAsOutbox(long id) {
|
|
|
|
updateTypeBitmask(id, Types.BASE_TYPE_MASK, Types.BASE_OUTBOX_TYPE);
|
|
|
|
}
|
|
|
|
|
2014-04-01 01:47:24 +00:00
|
|
|
public void markAsPendingInsecureSmsFallback(long id) {
|
|
|
|
updateTypeBitmask(id, Types.BASE_TYPE_MASK, Types.BASE_PENDING_INSECURE_SMS_FALLBACK);
|
2014-03-01 22:17:55 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 21:54:55 +00:00
|
|
|
@Override
|
2016-12-21 17:49:01 +00:00
|
|
|
public void markAsSent(long id, boolean isSecure) {
|
|
|
|
updateTypeBitmask(id, Types.BASE_TYPE_MASK, Types.BASE_SENT_TYPE | (isSecure ? Types.PUSH_MESSAGE_BIT | Types.SECURE_MESSAGE_BIT : 0));
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2018-10-11 23:45:22 +00:00
|
|
|
public void markAsSending(long id) {
|
|
|
|
updateTypeBitmask(id, Types.BASE_TYPE_MASK, Types.BASE_SENDING_TYPE);
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:37:40 +00:00
|
|
|
public void markAsMissedCall(long id) {
|
|
|
|
updateTypeBitmask(id, Types.TOTAL_MASK, Types.MISSED_CALL_TYPE);
|
|
|
|
}
|
|
|
|
|
2018-10-11 23:45:22 +00:00
|
|
|
@Override
|
2018-05-22 09:13:10 +00:00
|
|
|
public void markUnidentified(long id, boolean unidentified) {
|
|
|
|
ContentValues contentValues = new ContentValues(1);
|
|
|
|
contentValues.put(UNIDENTIFIED, unidentified ? 1 : 0);
|
|
|
|
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
db.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {String.valueOf(id)});
|
|
|
|
}
|
|
|
|
|
2017-10-02 21:54:55 +00:00
|
|
|
@Override
|
2016-08-16 03:23:56 +00:00
|
|
|
public void markExpireStarted(long id) {
|
|
|
|
markExpireStarted(id, System.currentTimeMillis());
|
|
|
|
}
|
|
|
|
|
2017-10-02 21:54:55 +00:00
|
|
|
@Override
|
2016-08-16 03:23:56 +00:00
|
|
|
public void markExpireStarted(long id, long startedAtTimestamp) {
|
|
|
|
ContentValues contentValues = new ContentValues();
|
|
|
|
contentValues.put(EXPIRE_STARTED, startedAtTimestamp);
|
|
|
|
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
db.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {String.valueOf(id)});
|
|
|
|
|
|
|
|
long threadId = getThreadIdForMessage(id);
|
|
|
|
|
|
|
|
DatabaseFactory.getThreadDatabase(context).update(threadId, false);
|
|
|
|
notifyConversationListeners(threadId);
|
|
|
|
}
|
|
|
|
|
2013-01-07 05:38:36 +00:00
|
|
|
public void markStatus(long id, int status) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i("MessageDatabase", "Updating ID: " + id + " to status: " + status);
|
2013-01-07 05:38:36 +00:00
|
|
|
ContentValues contentValues = new ContentValues();
|
|
|
|
contentValues.put(STATUS, status);
|
|
|
|
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
db.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {id+""});
|
2015-12-09 18:53:19 +00:00
|
|
|
|
|
|
|
long threadId = getThreadIdForMessage(id);
|
|
|
|
DatabaseFactory.getThreadDatabase(context).update(threadId, false);
|
|
|
|
notifyConversationListeners(threadId);
|
2013-01-07 05:38:36 +00:00
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void markAsSentFailed(long id) {
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
updateTypeBitmask(id, Types.BASE_TYPE_MASK, Types.BASE_SENT_FAILED_TYPE);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2017-03-09 01:38:55 +00:00
|
|
|
public void markAsNotified(long id) {
|
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
ContentValues contentValues = new ContentValues();
|
|
|
|
|
|
|
|
contentValues.put(NOTIFIED, 1);
|
|
|
|
|
|
|
|
database.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {String.valueOf(id)});
|
|
|
|
}
|
|
|
|
|
2020-01-16 03:35:51 +00:00
|
|
|
public boolean isOutgoingMessage(long timestamp) {
|
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
Cursor cursor = null;
|
|
|
|
boolean isOutgoing = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
cursor = database.query(TABLE_NAME, new String[] { ID, THREAD_ID, ADDRESS, TYPE },
|
2020-05-21 23:53:04 +00:00
|
|
|
DATE_SENT + " = ?", new String[] { String.valueOf(timestamp) },
|
|
|
|
null, null, null, null);
|
2020-01-16 03:35:51 +00:00
|
|
|
|
|
|
|
while (cursor.moveToNext()) {
|
|
|
|
if (Types.isOutgoingMessageType(cursor.getLong(cursor.getColumnIndexOrThrow(TYPE)))) {
|
2020-05-21 23:53:04 +00:00
|
|
|
isOutgoing = true;
|
2020-01-16 03:35:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) cursor.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return isOutgoing;
|
|
|
|
}
|
|
|
|
|
2017-09-16 05:38:53 +00:00
|
|
|
public void incrementReceiptCount(SyncMessageId messageId, boolean deliveryReceipt, boolean readReceipt) {
|
2015-12-09 04:32:54 +00:00
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
Cursor cursor = null;
|
|
|
|
boolean foundMessage = false;
|
2014-07-25 22:14:29 +00:00
|
|
|
|
|
|
|
try {
|
2014-10-21 04:06:34 +00:00
|
|
|
cursor = database.query(TABLE_NAME, new String[] {ID, THREAD_ID, ADDRESS, TYPE},
|
2016-02-20 01:07:41 +00:00
|
|
|
DATE_SENT + " = ?", new String[] {String.valueOf(messageId.getTimetamp())},
|
2014-07-25 22:14:29 +00:00
|
|
|
null, null, null, null);
|
|
|
|
|
|
|
|
while (cursor.moveToNext()) {
|
2014-10-21 04:06:34 +00:00
|
|
|
if (Types.isOutgoingMessageType(cursor.getLong(cursor.getColumnIndexOrThrow(TYPE)))) {
|
2017-07-26 16:59:15 +00:00
|
|
|
Address theirAddress = messageId.getAddress();
|
|
|
|
Address ourAddress = Address.fromSerialized(cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS)));
|
2017-09-16 05:38:53 +00:00
|
|
|
String columnName = deliveryReceipt ? DELIVERY_RECEIPT_COUNT : READ_RECEIPT_COUNT;
|
2017-07-26 16:59:15 +00:00
|
|
|
|
|
|
|
if (ourAddress.equals(theirAddress)) {
|
|
|
|
long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(THREAD_ID));
|
|
|
|
|
|
|
|
database.execSQL("UPDATE " + TABLE_NAME +
|
2017-09-16 05:38:53 +00:00
|
|
|
" SET " + columnName + " = " + columnName + " + 1 WHERE " +
|
2017-07-26 16:59:15 +00:00
|
|
|
ID + " = ?",
|
|
|
|
new String[] {String.valueOf(cursor.getLong(cursor.getColumnIndexOrThrow(ID)))});
|
|
|
|
|
|
|
|
DatabaseFactory.getThreadDatabase(context).update(threadId, false);
|
|
|
|
notifyConversationListeners(threadId);
|
|
|
|
foundMessage = true;
|
2014-07-25 22:14:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-09 04:32:54 +00:00
|
|
|
|
|
|
|
if (!foundMessage) {
|
2017-09-16 05:38:53 +00:00
|
|
|
if (deliveryReceipt) earlyDeliveryReceiptCache.increment(messageId.getTimetamp(), messageId.getAddress());
|
|
|
|
if (readReceipt) earlyReadReceiptCache.increment(messageId.getTimetamp(), messageId.getAddress());
|
2015-12-09 04:32:54 +00:00
|
|
|
}
|
|
|
|
|
2014-07-25 22:14:29 +00:00
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-28 22:51:27 +00:00
|
|
|
public List<Pair<Long, Long>> setTimestampRead(SyncMessageId messageId, long proposedExpireStarted) {
|
2016-10-02 19:08:30 +00:00
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
List<Pair<Long, Long>> expiring = new LinkedList<>();
|
|
|
|
Cursor cursor = null;
|
2016-02-20 01:07:41 +00:00
|
|
|
|
|
|
|
try {
|
2018-06-28 22:51:27 +00:00
|
|
|
cursor = database.query(TABLE_NAME, new String[] {ID, THREAD_ID, ADDRESS, TYPE, EXPIRES_IN, EXPIRE_STARTED},
|
2016-02-20 01:07:41 +00:00
|
|
|
DATE_SENT + " = ?", new String[] {String.valueOf(messageId.getTimetamp())},
|
|
|
|
null, null, null, null);
|
|
|
|
|
|
|
|
while (cursor.moveToNext()) {
|
2017-07-26 16:59:15 +00:00
|
|
|
Address theirAddress = messageId.getAddress();
|
|
|
|
Address ourAddress = Address.fromSerialized(cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS)));
|
2016-02-20 01:07:41 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
if (ourAddress.equals(theirAddress)) {
|
2018-06-28 22:51:27 +00:00
|
|
|
long id = cursor.getLong(cursor.getColumnIndexOrThrow(ID));
|
|
|
|
long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(THREAD_ID));
|
|
|
|
long expiresIn = cursor.getLong(cursor.getColumnIndexOrThrow(EXPIRES_IN));
|
|
|
|
long expireStarted = cursor.getLong(cursor.getColumnIndexOrThrow(EXPIRE_STARTED));
|
|
|
|
|
|
|
|
expireStarted = expireStarted > 0 ? Math.min(proposedExpireStarted, expireStarted) : proposedExpireStarted;
|
2016-02-20 01:07:41 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
ContentValues contentValues = new ContentValues();
|
|
|
|
contentValues.put(READ, 1);
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
if (expiresIn > 0) {
|
|
|
|
contentValues.put(EXPIRE_STARTED, expireStarted);
|
|
|
|
expiring.add(new Pair<>(id, expiresIn));
|
|
|
|
}
|
2016-10-02 19:08:30 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
database.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {cursor.getLong(cursor.getColumnIndexOrThrow(ID)) + ""});
|
2016-02-20 01:07:41 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
DatabaseFactory.getThreadDatabase(context).updateReadState(threadId);
|
|
|
|
DatabaseFactory.getThreadDatabase(context).setLastSeen(threadId);
|
|
|
|
notifyConversationListeners(threadId);
|
2016-02-20 01:07:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) cursor.close();
|
|
|
|
}
|
2016-10-02 19:08:30 +00:00
|
|
|
|
|
|
|
return expiring;
|
2013-05-06 20:59:40 +00:00
|
|
|
}
|
|
|
|
|
2017-10-09 01:09:46 +00:00
|
|
|
public List<MarkedMessageInfo> setMessagesRead(long threadId) {
|
|
|
|
return setMessagesRead(THREAD_ID + " = ? AND " + READ + " = 0", new String[] {String.valueOf(threadId)});
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<MarkedMessageInfo> setAllMessagesRead() {
|
|
|
|
return setMessagesRead(READ + " = 0", null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<MarkedMessageInfo> setMessagesRead(String where, String[] arguments) {
|
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
List<MarkedMessageInfo> results = new LinkedList<>();
|
|
|
|
Cursor cursor = null;
|
|
|
|
|
|
|
|
database.beginTransaction();
|
|
|
|
try {
|
|
|
|
cursor = database.query(TABLE_NAME, new String[] {ID, ADDRESS, DATE_SENT, TYPE, EXPIRES_IN, EXPIRE_STARTED}, where, arguments, null, null, null);
|
|
|
|
|
|
|
|
while (cursor != null && cursor.moveToNext()) {
|
|
|
|
if (Types.isSecureType(cursor.getLong(3))) {
|
|
|
|
SyncMessageId syncMessageId = new SyncMessageId(Address.fromSerialized(cursor.getString(1)), cursor.getLong(2));
|
|
|
|
ExpirationInfo expirationInfo = new ExpirationInfo(cursor.getLong(0), cursor.getLong(4), cursor.getLong(5), false);
|
|
|
|
|
|
|
|
results.add(new MarkedMessageInfo(syncMessageId, expirationInfo));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ContentValues contentValues = new ContentValues();
|
|
|
|
contentValues.put(READ, 1);
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2017-10-09 01:09:46 +00:00
|
|
|
database.update(TABLE_NAME, contentValues, where, arguments);
|
|
|
|
database.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) cursor.close();
|
|
|
|
database.endTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public Pair<Long, Long> updateBundleMessageBody(long messageId, String body) {
|
|
|
|
long type = Types.BASE_INBOX_TYPE | Types.SECURE_MESSAGE_BIT | Types.PUSH_MESSAGE_BIT;
|
|
|
|
return updateMessageBodyAndType(messageId, body, Types.TOTAL_MASK, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateMessageBody(long messageId, String body) {
|
|
|
|
long type = 0;
|
|
|
|
updateMessageBodyAndType(messageId, body, Types.ENCRYPTION_MASK, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Pair<Long, Long> updateMessageBodyAndType(long messageId, String body, long maskOff, long maskOn) {
|
2011-12-20 18:20:44 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
db.execSQL("UPDATE " + TABLE_NAME + " SET " + BODY + " = ?, " +
|
2015-01-15 21:35:35 +00:00
|
|
|
TYPE + " = (" + TYPE + " & " + (Types.TOTAL_MASK - maskOff) + " | " + maskOn + ") " +
|
|
|
|
"WHERE " + ID + " = ?",
|
|
|
|
new String[] {body, messageId + ""});
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
|
|
|
|
long threadId = getThreadIdForMessage(messageId);
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2015-11-23 23:07:41 +00:00
|
|
|
DatabaseFactory.getThreadDatabase(context).update(threadId, true);
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
notifyConversationListeners(threadId);
|
2011-12-20 18:20:44 +00:00
|
|
|
notifyConversationListListeners();
|
2015-05-18 22:16:27 +00:00
|
|
|
|
|
|
|
return new Pair<>(messageId, threadId);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2014-10-23 01:28:03 +00:00
|
|
|
public Pair<Long, Long> copyMessageInbox(long messageId) {
|
2018-01-25 03:17:44 +00:00
|
|
|
try {
|
|
|
|
SmsMessageRecord record = getMessage(messageId);
|
2014-11-25 06:50:32 +00:00
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
ContentValues contentValues = new ContentValues();
|
|
|
|
contentValues.put(TYPE, (record.getType() & ~Types.BASE_TYPE_MASK) | Types.BASE_INBOX_TYPE);
|
|
|
|
contentValues.put(ADDRESS, record.getIndividualRecipient().getAddress().serialize());
|
|
|
|
contentValues.put(ADDRESS_DEVICE_ID, record.getRecipientDeviceId());
|
|
|
|
contentValues.put(DATE_RECEIVED, System.currentTimeMillis());
|
|
|
|
contentValues.put(DATE_SENT, record.getDateSent());
|
|
|
|
contentValues.put(PROTOCOL, 31337);
|
|
|
|
contentValues.put(READ, 0);
|
2018-02-02 02:29:09 +00:00
|
|
|
contentValues.put(BODY, record.getBody());
|
2018-01-25 03:17:44 +00:00
|
|
|
contentValues.put(THREAD_ID, record.getThreadId());
|
|
|
|
contentValues.put(EXPIRES_IN, record.getExpiresIn());
|
|
|
|
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
long newMessageId = db.insert(TABLE_NAME, null, contentValues);
|
|
|
|
|
|
|
|
DatabaseFactory.getThreadDatabase(context).update(record.getThreadId(), true);
|
|
|
|
notifyConversationListeners(record.getThreadId());
|
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
ApplicationContext.getInstance(context).getJobManager().add(new TrimThreadJob(record.getThreadId()));
|
2018-01-25 03:17:44 +00:00
|
|
|
|
|
|
|
return new Pair<>(newMessageId, record.getThreadId());
|
|
|
|
} catch (NoSuchMessageException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
2014-10-23 01:28:03 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
public @NonNull Pair<Long, Long> insertReceivedCall(@NonNull Address address) {
|
|
|
|
return insertCallLog(address, Types.INCOMING_CALL_TYPE, false);
|
2015-09-21 17:54:23 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
public @NonNull Pair<Long, Long> insertOutgoingCall(@NonNull Address address) {
|
|
|
|
return insertCallLog(address, Types.OUTGOING_CALL_TYPE, false);
|
2015-09-21 17:54:23 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
public @NonNull Pair<Long, Long> insertMissedCall(@NonNull Address address) {
|
|
|
|
return insertCallLog(address, Types.MISSED_CALL_TYPE, true);
|
2015-09-21 17:54:23 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
private @NonNull Pair<Long, Long> insertCallLog(@NonNull Address address, long type, boolean unread) {
|
2017-08-22 01:32:38 +00:00
|
|
|
Recipient recipient = Recipient.from(context, address, true);
|
2017-08-01 15:56:00 +00:00
|
|
|
long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient);
|
2015-09-21 17:54:23 +00:00
|
|
|
|
|
|
|
ContentValues values = new ContentValues(6);
|
2017-07-26 16:59:15 +00:00
|
|
|
values.put(ADDRESS, address.serialize());
|
2015-09-21 17:54:23 +00:00
|
|
|
values.put(ADDRESS_DEVICE_ID, 1);
|
|
|
|
values.put(DATE_RECEIVED, System.currentTimeMillis());
|
|
|
|
values.put(DATE_SENT, System.currentTimeMillis());
|
|
|
|
values.put(READ, unread ? 0 : 1);
|
|
|
|
values.put(TYPE, type);
|
|
|
|
values.put(THREAD_ID, threadId);
|
|
|
|
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
long messageId = db.insert(TABLE_NAME, null, values);
|
|
|
|
|
2015-11-23 23:07:41 +00:00
|
|
|
DatabaseFactory.getThreadDatabase(context).update(threadId, true);
|
2015-09-21 17:54:23 +00:00
|
|
|
notifyConversationListeners(threadId);
|
2019-03-28 15:56:35 +00:00
|
|
|
ApplicationContext.getInstance(context).getJobManager().add(new TrimThreadJob(threadId));
|
2015-09-21 17:54:23 +00:00
|
|
|
|
|
|
|
if (unread) {
|
2017-11-14 02:01:05 +00:00
|
|
|
DatabaseFactory.getThreadDatabase(context).incrementUnread(threadId, 1);
|
2015-09-21 17:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new Pair<>(messageId, threadId);
|
|
|
|
}
|
|
|
|
|
2017-01-22 21:52:36 +00:00
|
|
|
protected Optional<InsertResult> insertMessageInbox(IncomingTextMessage message, long type) {
|
2015-10-27 19:18:02 +00:00
|
|
|
if (message.isJoined()) {
|
|
|
|
type = (type & (Types.TOTAL_MASK - Types.BASE_TYPE_MASK)) | Types.JOINED_TYPE;
|
|
|
|
} else if (message.isPreKeyBundle()) {
|
2015-03-11 21:23:45 +00:00
|
|
|
type |= Types.KEY_EXCHANGE_BIT | Types.KEY_EXCHANGE_BUNDLE_BIT;
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
} else if (message.isSecureMessage()) {
|
|
|
|
type |= Types.SECURE_MESSAGE_BIT;
|
2014-02-20 05:06:54 +00:00
|
|
|
} else if (message.isGroup()) {
|
|
|
|
type |= Types.SECURE_MESSAGE_BIT;
|
2014-02-22 01:51:25 +00:00
|
|
|
if (((IncomingGroupMessage)message).isUpdate()) type |= Types.GROUP_UPDATE_BIT;
|
2014-02-20 05:06:54 +00:00
|
|
|
else if (((IncomingGroupMessage)message).isQuit()) type |= Types.GROUP_QUIT_BIT;
|
2014-02-19 21:46:49 +00:00
|
|
|
} else if (message.isEndSession()) {
|
|
|
|
type |= Types.SECURE_MESSAGE_BIT;
|
2014-02-20 05:06:54 +00:00
|
|
|
type |= Types.END_SESSION_BIT;
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2016-11-09 17:37:40 +00:00
|
|
|
if (message.isPush()) type |= Types.PUSH_MESSAGE_BIT;
|
|
|
|
if (message.isIdentityUpdate()) type |= Types.KEY_EXCHANGE_IDENTITY_UPDATE_BIT;
|
|
|
|
if (message.isContentPreKeyBundle()) type |= Types.KEY_EXCHANGE_CONTENT_FORMAT;
|
2014-02-21 07:00:38 +00:00
|
|
|
|
2017-06-07 01:03:09 +00:00
|
|
|
if (message.isIdentityVerified()) type |= Types.KEY_EXCHANGE_IDENTITY_VERIFIED_BIT;
|
|
|
|
else if (message.isIdentityDefault()) type |= Types.KEY_EXCHANGE_IDENTITY_DEFAULT_BIT;
|
|
|
|
|
2017-08-22 01:32:38 +00:00
|
|
|
Recipient recipient = Recipient.from(context, message.getSender(), true);
|
2014-02-03 03:38:06 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
Recipient groupRecipient;
|
2014-02-03 03:38:06 +00:00
|
|
|
|
2015-03-03 19:44:49 +00:00
|
|
|
if (message.getGroupId() == null) {
|
2017-08-01 15:56:00 +00:00
|
|
|
groupRecipient = null;
|
2015-03-03 19:44:49 +00:00
|
|
|
} else {
|
2017-08-22 01:32:38 +00:00
|
|
|
groupRecipient = Recipient.from(context, message.getGroupId(), true);
|
2014-02-03 03:38:06 +00:00
|
|
|
}
|
|
|
|
|
2016-08-30 00:49:49 +00:00
|
|
|
boolean unread = (org.thoughtcrime.securesms.util.Util.isDefaultSmsProvider(context) ||
|
|
|
|
message.isSecureMessage() || message.isGroup() || message.isPreKeyBundle()) &&
|
2017-06-07 01:03:09 +00:00
|
|
|
!message.isIdentityUpdate() && !message.isIdentityDefault() && !message.isIdentityVerified();
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2014-01-14 08:26:43 +00:00
|
|
|
long threadId;
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
if (groupRecipient == null) threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient);
|
|
|
|
else threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(groupRecipient);
|
2014-01-14 08:26:43 +00:00
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
ContentValues values = new ContentValues(6);
|
2017-07-26 16:59:15 +00:00
|
|
|
values.put(ADDRESS, message.getSender().serialize());
|
2014-02-03 03:38:06 +00:00
|
|
|
values.put(ADDRESS_DEVICE_ID, message.getSenderDeviceId());
|
2019-09-18 04:43:02 +00:00
|
|
|
values.put(DATE_RECEIVED, message.getSentTimestampMillis()); // Loki - This is important due to how we handle GIFs
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
values.put(DATE_SENT, message.getSentTimestampMillis());
|
|
|
|
values.put(PROTOCOL, message.getProtocol());
|
2013-12-07 19:00:20 +00:00
|
|
|
values.put(READ, unread ? 0 : 1);
|
2016-02-06 00:10:33 +00:00
|
|
|
values.put(SUBSCRIPTION_ID, message.getSubscriptionId());
|
2016-08-16 03:23:56 +00:00
|
|
|
values.put(EXPIRES_IN, message.getExpiresIn());
|
2018-05-22 09:13:10 +00:00
|
|
|
values.put(UNIDENTIFIED, message.isUnidentified());
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
|
2014-11-12 19:15:05 +00:00
|
|
|
if (!TextUtils.isEmpty(message.getPseudoSubject()))
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
values.put(SUBJECT, message.getPseudoSubject());
|
|
|
|
|
|
|
|
values.put(REPLY_PATH_PRESENT, message.isReplyPathPresent());
|
|
|
|
values.put(SERVICE_CENTER, message.getServiceCenterAddress());
|
|
|
|
values.put(BODY, message.getMessageBody());
|
|
|
|
values.put(TYPE, type);
|
|
|
|
values.put(THREAD_ID, threadId);
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2017-01-22 21:52:36 +00:00
|
|
|
if (message.isPush() && isDuplicate(message, threadId)) {
|
|
|
|
Log.w(TAG, "Duplicate message (" + message.getSentTimestampMillis() + "), ignoring...");
|
|
|
|
return Optional.absent();
|
|
|
|
} else {
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
long messageId = db.insert(TABLE_NAME, null, values);
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2017-01-22 21:52:36 +00:00
|
|
|
if (unread) {
|
2017-11-14 02:01:05 +00:00
|
|
|
DatabaseFactory.getThreadDatabase(context).incrementUnread(threadId, 1);
|
2017-01-22 21:52:36 +00:00
|
|
|
}
|
2013-12-07 19:00:20 +00:00
|
|
|
|
2017-06-07 01:03:09 +00:00
|
|
|
if (!message.isIdentityUpdate() && !message.isIdentityVerified() && !message.isIdentityDefault()) {
|
2017-01-22 21:52:36 +00:00
|
|
|
DatabaseFactory.getThreadDatabase(context).update(threadId, true);
|
|
|
|
}
|
2016-08-30 00:49:49 +00:00
|
|
|
|
2017-02-18 04:43:24 +00:00
|
|
|
if (message.getSubscriptionId() != -1) {
|
2017-08-22 01:47:37 +00:00
|
|
|
DatabaseFactory.getRecipientDatabase(context).setDefaultSubscriptionId(recipient, message.getSubscriptionId());
|
2017-02-18 04:43:24 +00:00
|
|
|
}
|
|
|
|
|
2017-01-22 21:52:36 +00:00
|
|
|
notifyConversationListeners(threadId);
|
2017-06-19 18:20:15 +00:00
|
|
|
|
|
|
|
if (!message.isIdentityUpdate() && !message.isIdentityVerified() && !message.isIdentityDefault()) {
|
2019-03-28 15:56:35 +00:00
|
|
|
ApplicationContext.getInstance(context).getJobManager().add(new TrimThreadJob(threadId));
|
2017-06-19 18:20:15 +00:00
|
|
|
}
|
2013-01-10 05:06:56 +00:00
|
|
|
|
2017-01-22 21:52:36 +00:00
|
|
|
return Optional.of(new InsertResult(messageId, threadId));
|
|
|
|
}
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
}
|
|
|
|
|
2017-01-22 21:52:36 +00:00
|
|
|
public Optional<InsertResult> insertMessageInbox(IncomingTextMessage message) {
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
return insertMessageInbox(message, Types.BASE_INBOX_TYPE);
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public long insertMessageOutbox(long threadId, OutgoingTextMessage message,
|
|
|
|
boolean forceSms, long date, InsertListener insertListener)
|
2014-06-11 22:31:59 +00:00
|
|
|
{
|
2018-01-25 03:17:44 +00:00
|
|
|
long type = Types.BASE_SENDING_TYPE;
|
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
if (message.isKeyExchange()) type |= Types.KEY_EXCHANGE_BIT;
|
2016-12-21 17:49:01 +00:00
|
|
|
else if (message.isSecureMessage()) type |= (Types.SECURE_MESSAGE_BIT | Types.PUSH_MESSAGE_BIT);
|
2014-02-19 21:46:49 +00:00
|
|
|
else if (message.isEndSession()) type |= Types.END_SESSION_BIT;
|
2014-06-11 22:31:59 +00:00
|
|
|
if (forceSms) type |= Types.MESSAGE_FORCE_SMS_BIT;
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
|
2017-06-07 01:03:09 +00:00
|
|
|
if (message.isIdentityVerified()) type |= Types.KEY_EXCHANGE_IDENTITY_VERIFIED_BIT;
|
|
|
|
else if (message.isIdentityDefault()) type |= Types.KEY_EXCHANGE_IDENTITY_DEFAULT_BIT;
|
|
|
|
|
2017-10-02 21:54:55 +00:00
|
|
|
Address address = message.getRecipient().getAddress();
|
|
|
|
Map<Address, Long> earlyDeliveryReceipts = earlyDeliveryReceiptCache.remove(date);
|
|
|
|
Map<Address, Long> earlyReadReceipts = earlyReadReceiptCache.remove(date);
|
2015-12-09 04:32:54 +00:00
|
|
|
|
2014-11-08 19:35:58 +00:00
|
|
|
ContentValues contentValues = new ContentValues(6);
|
2017-07-26 16:59:15 +00:00
|
|
|
contentValues.put(ADDRESS, address.serialize());
|
2014-11-08 19:35:58 +00:00
|
|
|
contentValues.put(THREAD_ID, threadId);
|
|
|
|
contentValues.put(BODY, message.getMessageBody());
|
2015-10-16 17:07:50 +00:00
|
|
|
contentValues.put(DATE_RECEIVED, System.currentTimeMillis());
|
2014-11-08 19:35:58 +00:00
|
|
|
contentValues.put(DATE_SENT, date);
|
|
|
|
contentValues.put(READ, 1);
|
|
|
|
contentValues.put(TYPE, type);
|
2016-02-06 00:10:33 +00:00
|
|
|
contentValues.put(SUBSCRIPTION_ID, message.getSubscriptionId());
|
2016-08-16 03:23:56 +00:00
|
|
|
contentValues.put(EXPIRES_IN, message.getExpiresIn());
|
2017-10-02 21:54:55 +00:00
|
|
|
contentValues.put(DELIVERY_RECEIPT_COUNT, Stream.of(earlyDeliveryReceipts.values()).mapToLong(Long::longValue).sum());
|
|
|
|
contentValues.put(READ_RECEIPT_COUNT, Stream.of(earlyReadReceipts.values()).mapToLong(Long::longValue).sum());
|
2015-12-09 04:32:54 +00:00
|
|
|
|
2014-11-08 19:35:58 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
long messageId = db.insert(TABLE_NAME, ADDRESS, contentValues);
|
|
|
|
|
2017-04-22 23:29:26 +00:00
|
|
|
if (insertListener != null) {
|
|
|
|
insertListener.onComplete();
|
|
|
|
}
|
|
|
|
|
2017-06-19 18:20:15 +00:00
|
|
|
if (!message.isIdentityVerified() && !message.isIdentityDefault()) {
|
|
|
|
DatabaseFactory.getThreadDatabase(context).update(threadId, true);
|
|
|
|
DatabaseFactory.getThreadDatabase(context).setLastSeen(threadId);
|
|
|
|
}
|
|
|
|
|
2017-08-19 00:28:56 +00:00
|
|
|
DatabaseFactory.getThreadDatabase(context).setHasSent(threadId, true);
|
|
|
|
|
2014-11-08 19:35:58 +00:00
|
|
|
notifyConversationListeners(threadId);
|
2017-06-19 18:20:15 +00:00
|
|
|
|
|
|
|
if (!message.isIdentityVerified() && !message.isIdentityDefault()) {
|
2019-03-28 15:56:35 +00:00
|
|
|
ApplicationContext.getInstance(context).getJobManager().add(new TrimThreadJob(threadId));
|
2017-06-19 18:20:15 +00:00
|
|
|
}
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
|
2014-11-08 19:35:58 +00:00
|
|
|
return messageId;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 04:02:30 +00:00
|
|
|
Cursor getMessages(int skip, int limit) {
|
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
|
|
|
return db.query(TABLE_NAME, MESSAGE_PROJECTION, null, null, null, null, ID, skip + "," + limit);
|
|
|
|
}
|
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
Cursor getOutgoingMessages() {
|
|
|
|
String outgoingSelection = TYPE + " & " + Types.BASE_TYPE_MASK + " = " + Types.BASE_OUTBOX_TYPE;
|
2011-12-20 18:20:44 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
return db.query(TABLE_NAME, MESSAGE_PROJECTION, outgoingSelection, null, null, null, null);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2016-08-16 03:23:56 +00:00
|
|
|
public Cursor getExpirationStartedMessages() {
|
|
|
|
String where = EXPIRE_STARTED + " > 0";
|
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
|
|
|
return db.query(TABLE_NAME, MESSAGE_PROJECTION, where, null, null, null, null);
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public SmsMessageRecord getMessage(long messageId) throws NoSuchMessageException {
|
2015-01-15 21:35:35 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
2018-01-25 03:17:44 +00:00
|
|
|
Cursor cursor = db.query(TABLE_NAME, MESSAGE_PROJECTION, ID_WHERE, new String[]{messageId + ""}, null, null, null);
|
|
|
|
Reader reader = new Reader(cursor);
|
|
|
|
SmsMessageRecord record = reader.getNext();
|
|
|
|
|
|
|
|
reader.close();
|
|
|
|
|
|
|
|
if (record == null) throw new NoSuchMessageException("No message for ID: " + messageId);
|
|
|
|
else return record;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Cursor getMessageCursor(long messageId) {
|
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
2018-10-11 23:45:22 +00:00
|
|
|
Cursor cursor = db.query(TABLE_NAME, MESSAGE_PROJECTION, ID_WHERE, new String[] {messageId + ""}, null, null, null);
|
|
|
|
setNotifyConverationListeners(cursor, getThreadIdForMessage(messageId));
|
|
|
|
return cursor;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2015-03-31 20:36:04 +00:00
|
|
|
public boolean deleteMessage(long messageId) {
|
2018-08-02 13:25:33 +00:00
|
|
|
Log.i("MessageDatabase", "Deleting: " + messageId);
|
2011-12-20 18:20:44 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
long threadId = getThreadIdForMessage(messageId);
|
|
|
|
db.delete(TABLE_NAME, ID_WHERE, new String[] {messageId+""});
|
2015-11-23 23:07:41 +00:00
|
|
|
boolean threadDeleted = DatabaseFactory.getThreadDatabase(context).update(threadId, false);
|
2011-12-20 18:20:44 +00:00
|
|
|
notifyConversationListeners(threadId);
|
2015-03-31 20:36:04 +00:00
|
|
|
return threadDeleted;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2018-02-26 22:02:12 +00:00
|
|
|
public void ensureMigration() {
|
|
|
|
databaseHelper.getWritableDatabase();
|
|
|
|
}
|
|
|
|
|
2017-01-22 21:52:36 +00:00
|
|
|
private boolean isDuplicate(IncomingTextMessage message, long threadId) {
|
|
|
|
SQLiteDatabase database = databaseHelper.getReadableDatabase();
|
|
|
|
Cursor cursor = database.query(TABLE_NAME, null, DATE_SENT + " = ? AND " + ADDRESS + " = ? AND " + THREAD_ID + " = ?",
|
2017-07-26 16:59:15 +00:00
|
|
|
new String[]{String.valueOf(message.getSentTimestampMillis()), message.getSender().serialize(), String.valueOf(threadId)},
|
2017-01-22 21:52:36 +00:00
|
|
|
null, null, null, "1");
|
|
|
|
|
|
|
|
try {
|
|
|
|
return cursor != null && cursor.moveToFirst();
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) cursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
/*package */void deleteThread(long threadId) {
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
db.delete(TABLE_NAME, THREAD_ID + " = ?", new String[] {threadId+""});
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2013-01-10 05:06:56 +00:00
|
|
|
/*package*/void deleteMessagesInThreadBeforeDate(long threadId, long date) {
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
String where = THREAD_ID + " = ? AND (CASE " + TYPE;
|
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
for (long outgoingType : Types.OUTGOING_MESSAGE_TYPES) {
|
2013-01-10 05:06:56 +00:00
|
|
|
where += " WHEN " + outgoingType + " THEN " + DATE_SENT + " < " + date;
|
|
|
|
}
|
|
|
|
|
|
|
|
where += (" ELSE " + DATE_RECEIVED + " < " + date + " END)");
|
|
|
|
|
2015-01-15 21:35:35 +00:00
|
|
|
db.delete(TABLE_NAME, where, new String[] {threadId + ""});
|
2013-01-10 05:06:56 +00:00
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
/*package*/ void deleteThreads(Set<Long> threadIds) {
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
String where = "";
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
for (long threadId : threadIds) {
|
|
|
|
where += THREAD_ID + " = '" + threadId + "' OR ";
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
where = where.substring(0, where.length() - 4);
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
db.delete(TABLE_NAME, where, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*package */ void deleteAllThreads() {
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
2012-09-09 23:10:46 +00:00
|
|
|
db.delete(TABLE_NAME, null, null);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
/*package*/ SQLiteDatabase beginTransaction() {
|
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
database.beginTransaction();
|
|
|
|
return database;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*package*/ void endTransaction(SQLiteDatabase database) {
|
|
|
|
database.setTransactionSuccessful();
|
|
|
|
database.endTransaction();
|
2012-09-09 23:10:46 +00:00
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
/*package*/ SQLiteStatement createInsertStatement(SQLiteDatabase database) {
|
2013-01-06 21:13:14 +00:00
|
|
|
return database.compileStatement("INSERT INTO " + TABLE_NAME + " (" + ADDRESS + ", " +
|
|
|
|
PERSON + ", " +
|
|
|
|
DATE_SENT + ", " +
|
|
|
|
DATE_RECEIVED + ", " +
|
|
|
|
PROTOCOL + ", " +
|
|
|
|
READ + ", " +
|
|
|
|
STATUS + ", " +
|
|
|
|
TYPE + ", " +
|
|
|
|
REPLY_PATH_PRESENT + ", " +
|
|
|
|
SUBJECT + ", " +
|
|
|
|
BODY + ", " +
|
|
|
|
SERVICE_CENTER +
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
", " + THREAD_ID + ") " +
|
2013-01-06 21:13:14 +00:00
|
|
|
" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2013-01-07 05:38:36 +00:00
|
|
|
public static class Status {
|
|
|
|
public static final int STATUS_NONE = -1;
|
2014-02-21 00:14:58 +00:00
|
|
|
public static final int STATUS_COMPLETE = 0;
|
|
|
|
public static final int STATUS_PENDING = 0x20;
|
|
|
|
public static final int STATUS_FAILED = 0x40;
|
2013-01-07 05:38:36 +00:00
|
|
|
}
|
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
public Reader readerFor(Cursor cursor) {
|
|
|
|
return new Reader(cursor);
|
|
|
|
}
|
|
|
|
|
2017-04-22 23:29:26 +00:00
|
|
|
public OutgoingMessageReader readerFor(OutgoingTextMessage message, long threadId) {
|
|
|
|
return new OutgoingMessageReader(message, threadId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public class OutgoingMessageReader {
|
|
|
|
|
|
|
|
private final OutgoingTextMessage message;
|
|
|
|
private final long id;
|
|
|
|
private final long threadId;
|
|
|
|
|
|
|
|
public OutgoingMessageReader(OutgoingTextMessage message, long threadId) {
|
2019-03-15 22:31:52 +00:00
|
|
|
this.message = message;
|
|
|
|
this.threadId = threadId;
|
|
|
|
this.id = new SecureRandom().nextLong();
|
2017-04-22 23:29:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public MessageRecord getCurrent() {
|
2019-03-13 21:28:16 +00:00
|
|
|
return new SmsMessageRecord(id, message.getMessageBody(),
|
2017-08-01 15:56:00 +00:00
|
|
|
message.getRecipient(), message.getRecipient(),
|
2017-04-22 23:29:26 +00:00
|
|
|
1, System.currentTimeMillis(), System.currentTimeMillis(),
|
|
|
|
0, message.isSecureMessage() ? MmsSmsColumns.Types.getOutgoingEncryptedMessageType() : MmsSmsColumns.Types.getOutgoingSmsMessageType(),
|
|
|
|
threadId, 0, new LinkedList<IdentityKeyMismatch>(),
|
|
|
|
message.getSubscriptionId(), message.getExpiresIn(),
|
2020-07-15 04:26:20 +00:00
|
|
|
System.currentTimeMillis(), 0, false);
|
2017-04-22 23:29:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
public class Reader {
|
|
|
|
|
|
|
|
private final Cursor cursor;
|
|
|
|
|
|
|
|
public Reader(Cursor cursor) {
|
|
|
|
this.cursor = cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
public SmsMessageRecord getNext() {
|
|
|
|
if (cursor == null || !cursor.moveToNext())
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return getCurrent();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2013-06-25 04:02:30 +00:00
|
|
|
public int getCount() {
|
|
|
|
if (cursor == null) return 0;
|
|
|
|
else return cursor.getCount();
|
|
|
|
}
|
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
public SmsMessageRecord getCurrent() {
|
2017-09-16 05:38:53 +00:00
|
|
|
long messageId = cursor.getLong(cursor.getColumnIndexOrThrow(SmsDatabase.ID));
|
|
|
|
Address address = Address.fromSerialized(cursor.getString(cursor.getColumnIndexOrThrow(SmsDatabase.ADDRESS)));
|
|
|
|
int addressDeviceId = cursor.getInt(cursor.getColumnIndexOrThrow(SmsDatabase.ADDRESS_DEVICE_ID));
|
|
|
|
long type = cursor.getLong(cursor.getColumnIndexOrThrow(SmsDatabase.TYPE));
|
|
|
|
long dateReceived = cursor.getLong(cursor.getColumnIndexOrThrow(SmsDatabase.NORMALIZED_DATE_RECEIVED));
|
|
|
|
long dateSent = cursor.getLong(cursor.getColumnIndexOrThrow(SmsDatabase.NORMALIZED_DATE_SENT));
|
|
|
|
long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(SmsDatabase.THREAD_ID));
|
|
|
|
int status = cursor.getInt(cursor.getColumnIndexOrThrow(SmsDatabase.STATUS));
|
|
|
|
int deliveryReceiptCount = cursor.getInt(cursor.getColumnIndexOrThrow(SmsDatabase.DELIVERY_RECEIPT_COUNT));
|
|
|
|
int readReceiptCount = cursor.getInt(cursor.getColumnIndexOrThrow(SmsDatabase.READ_RECEIPT_COUNT));
|
|
|
|
String mismatchDocument = cursor.getString(cursor.getColumnIndexOrThrow(SmsDatabase.MISMATCHED_IDENTITIES));
|
|
|
|
int subscriptionId = cursor.getInt(cursor.getColumnIndexOrThrow(SmsDatabase.SUBSCRIPTION_ID));
|
|
|
|
long expiresIn = cursor.getLong(cursor.getColumnIndexOrThrow(SmsDatabase.EXPIRES_IN));
|
|
|
|
long expireStarted = cursor.getLong(cursor.getColumnIndexOrThrow(SmsDatabase.EXPIRE_STARTED));
|
2018-02-02 02:29:09 +00:00
|
|
|
String body = cursor.getString(cursor.getColumnIndexOrThrow(SmsDatabase.BODY));
|
2018-05-22 09:13:10 +00:00
|
|
|
boolean unidentified = cursor.getInt(cursor.getColumnIndexOrThrow(SmsDatabase.UNIDENTIFIED)) == 1;
|
2017-09-16 05:38:53 +00:00
|
|
|
|
|
|
|
if (!TextSecurePreferences.isReadReceiptsEnabled(context)) {
|
|
|
|
readReceiptCount = 0;
|
|
|
|
}
|
2015-01-15 21:35:35 +00:00
|
|
|
|
|
|
|
List<IdentityKeyMismatch> mismatches = getMismatches(mismatchDocument);
|
2017-08-22 01:32:38 +00:00
|
|
|
Recipient recipient = Recipient.from(context, address, true);
|
2015-01-15 21:35:35 +00:00
|
|
|
|
2019-03-13 21:28:16 +00:00
|
|
|
return new SmsMessageRecord(messageId, body, recipient,
|
2017-08-01 15:56:00 +00:00
|
|
|
recipient,
|
2015-01-15 21:35:35 +00:00
|
|
|
addressDeviceId,
|
2017-09-16 05:38:53 +00:00
|
|
|
dateSent, dateReceived, deliveryReceiptCount, type,
|
2016-08-16 03:23:56 +00:00
|
|
|
threadId, status, mismatches, subscriptionId,
|
2020-07-15 04:26:20 +00:00
|
|
|
expiresIn, expireStarted, readReceiptCount, unidentified);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2015-01-15 21:35:35 +00:00
|
|
|
private List<IdentityKeyMismatch> getMismatches(String document) {
|
|
|
|
try {
|
|
|
|
if (!TextUtils.isEmpty(document)) {
|
|
|
|
return JsonUtils.fromJson(document, IdentityKeyMismatchList.class).getList();
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new LinkedList<>();
|
|
|
|
}
|
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
public void close() {
|
|
|
|
cursor.close();
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2017-01-22 21:52:36 +00:00
|
|
|
|
2017-04-22 23:29:26 +00:00
|
|
|
public interface InsertListener {
|
|
|
|
public void onComplete();
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|