2012-09-08 03:03:23 +00:00
|
|
|
/**
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-09-08 03:03:23 +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-08 03:03:23 +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;
|
|
|
|
|
2012-09-08 03:03:23 +00:00
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
2015-03-27 17:26:17 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2015-10-13 01:25:05 +00:00
|
|
|
import android.support.annotation.Nullable;
|
2014-11-12 19:15:05 +00:00
|
|
|
import android.text.TextUtils;
|
2012-09-08 03:03:23 +00:00
|
|
|
import android.util.Log;
|
2013-04-26 18:23:43 +00:00
|
|
|
import android.util.Pair;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2017-09-30 15:45:45 +00:00
|
|
|
import com.annimon.stream.Stream;
|
2017-05-08 22:32:59 +00:00
|
|
|
import com.google.android.mms.pdu_alt.NotificationInd;
|
|
|
|
import com.google.android.mms.pdu_alt.PduHeaders;
|
2014-03-07 21:05:35 +00:00
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
import net.sqlcipher.database.SQLiteDatabase;
|
|
|
|
|
2014-11-25 06:50:32 +00:00
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
2015-10-13 01:25:05 +00:00
|
|
|
import org.thoughtcrime.securesms.attachments.Attachment;
|
|
|
|
import org.thoughtcrime.securesms.attachments.DatabaseAttachment;
|
2017-01-20 23:26:17 +00:00
|
|
|
import org.thoughtcrime.securesms.attachments.MmsNotificationAttachment;
|
2015-01-15 21:35:35 +00:00
|
|
|
import org.thoughtcrime.securesms.database.documents.IdentityKeyMismatch;
|
|
|
|
import org.thoughtcrime.securesms.database.documents.IdentityKeyMismatchList;
|
2015-10-13 01:25:05 +00:00
|
|
|
import org.thoughtcrime.securesms.database.documents.NetworkFailure;
|
|
|
|
import org.thoughtcrime.securesms.database.documents.NetworkFailureList;
|
2018-01-25 03:17:44 +00:00
|
|
|
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper;
|
2013-04-30 18:14:01 +00:00
|
|
|
import org.thoughtcrime.securesms.database.model.DisplayRecord;
|
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.MediaMmsMessageRecord;
|
|
|
|
import org.thoughtcrime.securesms.database.model.MessageRecord;
|
|
|
|
import org.thoughtcrime.securesms.database.model.NotificationMmsMessageRecord;
|
2014-11-25 06:50:32 +00:00
|
|
|
import org.thoughtcrime.securesms.jobs.TrimThreadJob;
|
2013-07-19 00:42:45 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.IncomingMediaMessage;
|
2017-07-26 16:59:15 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.MmsException;
|
2016-08-16 03:23:56 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.OutgoingExpirationUpdateMessage;
|
2014-11-12 19:15:05 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.OutgoingGroupMediaMessage;
|
|
|
|
import org.thoughtcrime.securesms.mms.OutgoingMediaMessage;
|
2015-10-13 01:25:05 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.OutgoingSecureMediaMessage;
|
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.mms.SlideDeck;
|
2017-08-01 15:56:00 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
|
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;
|
2014-11-12 19:15:05 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2014-11-25 06:50:32 +00:00
|
|
|
import org.whispersystems.jobqueue.JobManager;
|
2016-03-23 17:34:41 +00:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2011-12-20 18:20:44 +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.NoSuchAlgorithmException;
|
|
|
|
import java.security.SecureRandom;
|
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 java.util.HashSet;
|
2015-01-15 21:35:35 +00:00
|
|
|
import java.util.LinkedList;
|
2013-09-09 01:19:05 +00:00
|
|
|
import java.util.List;
|
2017-10-02 21:54:55 +00:00
|
|
|
import java.util.Map;
|
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 java.util.Set;
|
|
|
|
|
2015-01-15 21:35:35 +00:00
|
|
|
public class MmsDatabase extends MessagingDatabase {
|
|
|
|
|
|
|
|
private static final String TAG = MmsDatabase.class.getSimpleName();
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public static final String TABLE_NAME = "mms";
|
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_SENT = "date";
|
|
|
|
static final String DATE_RECEIVED = "date_received";
|
2011-12-20 18:20:44 +00:00
|
|
|
public static final String MESSAGE_BOX = "msg_box";
|
2013-05-05 19:51:36 +00:00
|
|
|
static final String CONTENT_LOCATION = "ct_l";
|
|
|
|
static final String EXPIRY = "exp";
|
2011-12-20 18:20:44 +00:00
|
|
|
public static final String MESSAGE_TYPE = "m_type";
|
2013-05-05 19:51:36 +00:00
|
|
|
static final String MESSAGE_SIZE = "m_size";
|
|
|
|
static final String STATUS = "st";
|
|
|
|
static final String TRANSACTION_ID = "tr_id";
|
2013-04-26 18:23:43 +00:00
|
|
|
static final String PART_COUNT = "part_count";
|
2015-01-15 21:35:35 +00:00
|
|
|
static final String NETWORK_FAILURE = "network_failures";
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + ID + " INTEGER PRIMARY KEY, " +
|
2013-01-07 02:47:20 +00:00
|
|
|
THREAD_ID + " INTEGER, " + DATE_SENT + " INTEGER, " + DATE_RECEIVED + " INTEGER, " + MESSAGE_BOX + " INTEGER, " +
|
2015-10-13 01:25:05 +00:00
|
|
|
READ + " INTEGER DEFAULT 0, " + "m_id" + " TEXT, " + "sub" + " TEXT, " +
|
|
|
|
"sub_cs" + " INTEGER, " + BODY + " TEXT, " + PART_COUNT + " INTEGER, " +
|
|
|
|
"ct_t" + " TEXT, " + CONTENT_LOCATION + " TEXT, " + ADDRESS + " TEXT, " +
|
2014-02-03 03:38:06 +00:00
|
|
|
ADDRESS_DEVICE_ID + " INTEGER, " +
|
2015-10-13 01:25:05 +00:00
|
|
|
EXPIRY + " INTEGER, " + "m_cls" + " TEXT, " + MESSAGE_TYPE + " INTEGER, " +
|
|
|
|
"v" + " INTEGER, " + MESSAGE_SIZE + " INTEGER, " + "pri" + " INTEGER, " +
|
|
|
|
"rr" + " INTEGER, " + "rpt_a" + " INTEGER, " + "resp_st" + " INTEGER, " +
|
|
|
|
STATUS + " INTEGER, " + TRANSACTION_ID + " TEXT, " + "retr_st" + " INTEGER, " +
|
|
|
|
"retr_txt" + " TEXT, " + "retr_txt_cs" + " INTEGER, " + "read_status" + " INTEGER, " +
|
|
|
|
"ct_cls" + " INTEGER, " + "resp_txt" + " TEXT, " + "d_tm" + " INTEGER, " +
|
2017-09-16 05:38:53 +00:00
|
|
|
DELIVERY_RECEIPT_COUNT + " INTEGER DEFAULT 0, " + MISMATCHED_IDENTITIES + " TEXT DEFAULT NULL, " +
|
2016-02-06 00:10:33 +00:00
|
|
|
NETWORK_FAILURE + " TEXT DEFAULT NULL," + "d_rpt" + " INTEGER, " +
|
2016-08-16 03:23:56 +00:00
|
|
|
SUBSCRIPTION_ID + " INTEGER DEFAULT -1, " + EXPIRES_IN + " INTEGER DEFAULT 0, " +
|
2017-09-16 05:38:53 +00:00
|
|
|
EXPIRE_STARTED + " INTEGER DEFAULT 0, " + NOTIFIED + " INTEGER DEFAULT 0, " +
|
|
|
|
READ_RECEIPT_COUNT + " INTEGER DEFAULT 0);";
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2012-10-30 00:41:06 +00:00
|
|
|
public static final String[] CREATE_INDEXS = {
|
|
|
|
"CREATE INDEX IF NOT EXISTS mms_thread_id_index ON " + TABLE_NAME + " (" + THREAD_ID + ");",
|
|
|
|
"CREATE INDEX IF NOT EXISTS mms_read_index ON " + TABLE_NAME + " (" + READ + ");",
|
2017-03-09 01:38:55 +00:00
|
|
|
"CREATE INDEX IF NOT EXISTS mms_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 mms_message_box_index ON " + TABLE_NAME + " (" + MESSAGE_BOX + ");",
|
2015-08-04 20:37:22 +00:00
|
|
|
"CREATE INDEX IF NOT EXISTS mms_date_sent_index ON " + TABLE_NAME + " (" + DATE_SENT + ");",
|
|
|
|
"CREATE INDEX IF NOT EXISTS mms_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[] MMS_PROJECTION = new String[] {
|
2015-10-24 16:40:04 +00:00
|
|
|
MmsDatabase.TABLE_NAME + "." + ID + " AS " + ID,
|
|
|
|
THREAD_ID, DATE_SENT + " AS " + NORMALIZED_DATE_SENT,
|
2015-08-04 20:37:22 +00:00
|
|
|
DATE_RECEIVED + " AS " + NORMALIZED_DATE_RECEIVED,
|
2015-10-13 01:25:05 +00:00
|
|
|
MESSAGE_BOX, READ,
|
|
|
|
CONTENT_LOCATION, EXPIRY, MESSAGE_TYPE,
|
|
|
|
MESSAGE_SIZE, STATUS, TRANSACTION_ID,
|
|
|
|
BODY, PART_COUNT, ADDRESS, ADDRESS_DEVICE_ID,
|
2017-09-30 15:45:45 +00:00
|
|
|
DELIVERY_RECEIPT_COUNT, READ_RECEIPT_COUNT, MISMATCHED_IDENTITIES, NETWORK_FAILURE, SUBSCRIPTION_ID,
|
2017-03-09 01:38:55 +00:00
|
|
|
EXPIRES_IN, EXPIRE_STARTED, NOTIFIED,
|
2015-10-24 16:40:04 +00:00
|
|
|
AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.ROW_ID + " AS " + AttachmentDatabase.ATTACHMENT_ID_ALIAS,
|
|
|
|
AttachmentDatabase.UNIQUE_ID,
|
|
|
|
AttachmentDatabase.MMS_ID,
|
|
|
|
AttachmentDatabase.SIZE,
|
2017-03-28 19:05:30 +00:00
|
|
|
AttachmentDatabase.FILE_NAME,
|
2015-10-24 16:40:04 +00:00
|
|
|
AttachmentDatabase.DATA,
|
2016-12-11 21:37:27 +00:00
|
|
|
AttachmentDatabase.THUMBNAIL,
|
2015-10-24 16:40:04 +00:00
|
|
|
AttachmentDatabase.CONTENT_TYPE,
|
|
|
|
AttachmentDatabase.CONTENT_LOCATION,
|
2017-02-26 18:06:27 +00:00
|
|
|
AttachmentDatabase.DIGEST,
|
2017-04-22 23:29:26 +00:00
|
|
|
AttachmentDatabase.FAST_PREFLIGHT_ID,
|
2017-05-12 05:46:35 +00:00
|
|
|
AttachmentDatabase.VOICE_NOTE,
|
2015-10-24 16:40:04 +00:00
|
|
|
AttachmentDatabase.CONTENT_DISPOSITION,
|
|
|
|
AttachmentDatabase.NAME,
|
|
|
|
AttachmentDatabase.TRANSFER_STATE
|
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
|
|
|
};
|
|
|
|
|
2015-10-24 16:40:04 +00:00
|
|
|
private static final String RAW_ID_WHERE = TABLE_NAME + "._id = ?";
|
2013-04-26 18:23:43 +00:00
|
|
|
|
2017-09-16 05:38:53 +00:00
|
|
|
private final EarlyReceiptCache earlyDeliveryReceiptCache = new EarlyReceiptCache();
|
|
|
|
private final EarlyReceiptCache earlyReadReceiptCache = new EarlyReceiptCache();
|
|
|
|
|
2014-11-25 06:50:32 +00:00
|
|
|
private final JobManager jobManager;
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public MmsDatabase(Context context, SQLCipherOpenHelper databaseHelper) {
|
2011-12-20 18:20:44 +00:00
|
|
|
super(context, databaseHelper);
|
2014-11-25 06:50:32 +00:00
|
|
|
this.jobManager = ApplicationContext.getInstance(context).getJobManager();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2015-01-15 21:35:35 +00:00
|
|
|
@Override
|
|
|
|
protected String getTableName() {
|
|
|
|
return TABLE_NAME;
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public int getMessageCountForThread(long threadId) {
|
2012-09-08 03:03:23 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
2011-12-20 18:20:44 +00:00
|
|
|
Cursor cursor = null;
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
|
|
|
cursor = db.query(TABLE_NAME, new String[] {"COUNT(*)"}, THREAD_ID + " = ?", new String[] {threadId+""}, null, null, null);
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (cursor != null && cursor.moveToFirst())
|
|
|
|
return cursor.getInt(0);
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2015-01-15 21:35:35 +00:00
|
|
|
public void addFailures(long messageId, List<NetworkFailure> failure) {
|
|
|
|
try {
|
|
|
|
addToDocument(messageId, NETWORK_FAILURE, failure, NetworkFailureList.class);
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeFailure(long messageId, NetworkFailure failure) {
|
|
|
|
try {
|
|
|
|
removeFromDocument(messageId, NETWORK_FAILURE, failure, NetworkFailureList.class);
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-30 15:45:45 +00:00
|
|
|
public void incrementReceiptCount(SyncMessageId messageId, long timestamp, boolean deliveryReceipt, boolean readReceipt) {
|
2017-08-01 15:56:00 +00:00
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
Cursor cursor = null;
|
|
|
|
boolean found = false;
|
2014-07-25 22:14:29 +00:00
|
|
|
|
|
|
|
try {
|
2017-08-01 15:56:00 +00:00
|
|
|
cursor = database.query(TABLE_NAME, new String[] {ID, THREAD_ID, MESSAGE_BOX, ADDRESS}, DATE_SENT + " = ?", new String[] {String.valueOf(messageId.getTimetamp())}, null, null, null, null);
|
2014-07-25 22:14:29 +00:00
|
|
|
|
|
|
|
while (cursor.moveToNext()) {
|
2014-10-21 04:06:34 +00:00
|
|
|
if (Types.isOutgoingMessageType(cursor.getLong(cursor.getColumnIndexOrThrow(MESSAGE_BOX)))) {
|
2017-08-01 15:56:00 +00:00
|
|
|
Address theirAddress = Address.fromSerialized(cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS)));
|
2017-09-16 05:38:53 +00:00
|
|
|
Address ourAddress = messageId.getAddress();
|
|
|
|
String columnName = deliveryReceipt ? DELIVERY_RECEIPT_COUNT : READ_RECEIPT_COUNT;
|
2014-10-21 04:06:34 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
if (ourAddress.equals(theirAddress) || theirAddress.isGroup()) {
|
|
|
|
long id = cursor.getLong(cursor.getColumnIndexOrThrow(ID));
|
|
|
|
long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(THREAD_ID));
|
2017-09-30 15:45:45 +00:00
|
|
|
int status = deliveryReceipt ? GroupReceiptDatabase.STATUS_DELIVERED : GroupReceiptDatabase.STATUS_READ;
|
2014-10-21 04:06:34 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
found = true;
|
2015-12-09 04:32:54 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
database.execSQL("UPDATE " + TABLE_NAME + " SET " +
|
2017-09-16 05:38:53 +00:00
|
|
|
columnName + " = " + columnName + " + 1 WHERE " + ID + " = ?",
|
2017-08-01 15:56:00 +00:00
|
|
|
new String[] {String.valueOf(id)});
|
2014-10-21 04:06:34 +00:00
|
|
|
|
2017-09-30 15:45:45 +00:00
|
|
|
DatabaseFactory.getGroupReceiptDatabase(context).update(ourAddress, id, status, timestamp);
|
2017-08-01 15:56:00 +00:00
|
|
|
DatabaseFactory.getThreadDatabase(context).update(threadId, false);
|
|
|
|
notifyConversationListeners(threadId);
|
2014-07-25 22:14:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-09 04:32:54 +00:00
|
|
|
|
|
|
|
if (!found) {
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
Cursor cursor = null;
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
|
|
|
cursor = db.rawQuery(sql, sqlArgs);
|
|
|
|
if (cursor != null && cursor.moveToFirst())
|
|
|
|
return cursor.getLong(0);
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2015-02-16 05:53:59 +00:00
|
|
|
private long getThreadIdFor(IncomingMediaMessage retrieved) throws RecipientFormattingException, MmsException {
|
2014-01-14 08:26:43 +00:00
|
|
|
if (retrieved.getGroupId() != null) {
|
2017-08-22 01:32:38 +00:00
|
|
|
Recipient groupRecipients = Recipient.from(context, retrieved.getGroupId(), true);
|
2014-02-14 23:59:57 +00:00
|
|
|
return DatabaseFactory.getThreadDatabase(context).getThreadIdFor(groupRecipients);
|
2015-10-28 17:24:55 +00:00
|
|
|
} else {
|
2017-08-22 01:32:38 +00:00
|
|
|
Recipient sender = Recipient.from(context, retrieved.getFrom(), true);
|
2017-08-01 15:56:00 +00:00
|
|
|
return DatabaseFactory.getThreadDatabase(context).getThreadIdFor(sender);
|
2015-10-28 17:24:55 +00:00
|
|
|
}
|
2013-04-26 01:59:49 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 17:26:17 +00:00
|
|
|
private long getThreadIdFor(@NonNull NotificationInd notification) {
|
|
|
|
String fromString = notification.getFrom() != null && notification.getFrom().getTextString() != null
|
|
|
|
? Util.toIsoString(notification.getFrom().getTextString())
|
|
|
|
: "";
|
2017-08-22 01:32:38 +00:00
|
|
|
Recipient recipient = Recipient.from(context, Address.fromExternal(context, fromString), false);
|
2017-08-01 15:56:00 +00:00
|
|
|
return DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2015-10-24 16:40:04 +00:00
|
|
|
private Cursor rawQuery(@NonNull String where, @Nullable String[] arguments) {
|
|
|
|
SQLiteDatabase database = databaseHelper.getReadableDatabase();
|
2015-10-25 00:51:17 +00:00
|
|
|
return database.rawQuery("SELECT " + Util.join(MMS_PROJECTION, ",") +
|
2015-10-24 16:40:04 +00:00
|
|
|
" FROM " + MmsDatabase.TABLE_NAME + " LEFT OUTER JOIN " + AttachmentDatabase.TABLE_NAME +
|
|
|
|
" ON (" + MmsDatabase.TABLE_NAME + "." + MmsDatabase.ID + " = " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.MMS_ID + ")" +
|
|
|
|
" WHERE " + where, arguments);
|
|
|
|
}
|
|
|
|
|
2015-01-15 21:35:35 +00:00
|
|
|
public Cursor getMessage(long messageId) {
|
2015-10-24 16:40:04 +00:00
|
|
|
Cursor cursor = rawQuery(RAW_ID_WHERE, new String[] {messageId + ""});
|
2015-01-15 21:35:35 +00:00
|
|
|
setNotifyConverationListeners(cursor, getThreadIdForMessage(messageId));
|
|
|
|
return cursor;
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public Reader getExpireStartedMessages() {
|
2016-08-16 03:23:56 +00:00
|
|
|
String where = EXPIRE_STARTED + " > 0";
|
2018-01-25 03:17:44 +00:00
|
|
|
return readerFor(rawQuery(where, null));
|
2016-08-16 03:23:56 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public Reader getDecryptInProgressMessages() {
|
2015-10-24 16:40:04 +00:00
|
|
|
String where = MESSAGE_BOX + " & " + (Types.ENCRYPTION_ASYMMETRIC_BIT) + " != 0";
|
2018-01-25 03:17:44 +00:00
|
|
|
return readerFor(rawQuery(where, null));
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-03 19:47:28 +00:00
|
|
|
private void updateMailboxBitmask(long id, long maskOff, long maskOn, Optional<Long> 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
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
db.execSQL("UPDATE " + TABLE_NAME +
|
2015-01-15 21:35:35 +00:00
|
|
|
" SET " + MESSAGE_BOX + " = (" + MESSAGE_BOX + " & " + (Types.TOTAL_MASK - maskOff) + " | " + maskOn + " )" +
|
|
|
|
" WHERE " + ID + " = ?", new String[] {id + ""});
|
2015-11-24 15:06:41 +00:00
|
|
|
|
2015-12-03 19:47:28 +00:00
|
|
|
if (threadId.isPresent()) {
|
|
|
|
DatabaseFactory.getThreadDatabase(context).update(threadId.get(), false);
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2014-02-16 02:31:25 +00:00
|
|
|
public void markAsOutbox(long messageId) {
|
2015-12-03 19:47:28 +00:00
|
|
|
long threadId = getThreadIdForMessage(messageId);
|
|
|
|
updateMailboxBitmask(messageId, Types.BASE_TYPE_MASK, Types.BASE_OUTBOX_TYPE, Optional.of(threadId));
|
2014-03-01 22:17:55 +00:00
|
|
|
}
|
|
|
|
|
2014-04-03 23:20:23 +00:00
|
|
|
public void markAsForcedSms(long messageId) {
|
2015-12-03 19:47:28 +00:00
|
|
|
long threadId = getThreadIdForMessage(messageId);
|
|
|
|
updateMailboxBitmask(messageId, Types.PUSH_MESSAGE_BIT, Types.MESSAGE_FORCE_SMS_BIT, Optional.of(threadId));
|
|
|
|
notifyConversationListeners(threadId);
|
2014-03-01 22:17:55 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 01:47:24 +00:00
|
|
|
public void markAsPendingInsecureSmsFallback(long messageId) {
|
2015-12-03 19:47:28 +00:00
|
|
|
long threadId = getThreadIdForMessage(messageId);
|
|
|
|
updateMailboxBitmask(messageId, Types.BASE_TYPE_MASK, Types.BASE_PENDING_INSECURE_SMS_FALLBACK, Optional.of(threadId));
|
|
|
|
notifyConversationListeners(threadId);
|
2014-02-16 02:31:25 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 17:49:01 +00:00
|
|
|
// public void markAsSending(long messageId) {
|
|
|
|
// long threadId = getThreadIdForMessage(messageId);
|
|
|
|
// updateMailboxBitmask(messageId, Types.BASE_TYPE_MASK, Types.BASE_SENDING_TYPE, Optional.of(threadId));
|
|
|
|
// notifyConversationListeners(threadId);
|
|
|
|
// }
|
2013-10-13 10:53:41 +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 void markAsSentFailed(long messageId) {
|
2015-12-03 19:47:28 +00:00
|
|
|
long threadId = getThreadIdForMessage(messageId);
|
|
|
|
updateMailboxBitmask(messageId, Types.BASE_TYPE_MASK, Types.BASE_SENT_FAILED_TYPE, Optional.of(threadId));
|
|
|
|
notifyConversationListeners(threadId);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2013-07-17 02:52:02 +00:00
|
|
|
|
2017-10-02 21:54:55 +00:00
|
|
|
@Override
|
2016-12-21 17:49:01 +00:00
|
|
|
public void markAsSent(long messageId, boolean secure) {
|
2015-12-03 19:47:28 +00:00
|
|
|
long threadId = getThreadIdForMessage(messageId);
|
2016-12-21 17:49:01 +00:00
|
|
|
updateMailboxBitmask(messageId, Types.BASE_TYPE_MASK, Types.BASE_SENT_TYPE | (secure ? Types.PUSH_MESSAGE_BIT | Types.SECURE_MESSAGE_BIT : 0), Optional.of(threadId));
|
2015-12-03 19:47:28 +00:00
|
|
|
notifyConversationListeners(threadId);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void markDownloadState(long messageId, long state) {
|
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
ContentValues contentValues = new ContentValues();
|
|
|
|
contentValues.put(STATUS, state);
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2013-04-30 18:14:01 +00:00
|
|
|
database.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {messageId + ""});
|
2012-09-08 03:03:23 +00:00
|
|
|
notifyConversationListeners(getThreadIdForMessage(messageId));
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void markAsNoSession(long messageId, long threadId) {
|
2015-12-03 19:47:28 +00:00
|
|
|
updateMailboxBitmask(messageId, Types.ENCRYPTION_MASK, Types.ENCRYPTION_REMOTE_NO_SESSION_BIT, Optional.of(threadId));
|
2012-09-08 03:03:23 +00:00
|
|
|
notifyConversationListeners(threadId);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2016-12-21 17:49:01 +00:00
|
|
|
// public void markAsSecure(long messageId) {
|
|
|
|
// updateMailboxBitmask(messageId, 0, Types.SECURE_MESSAGE_BIT, Optional.<Long>absent());
|
|
|
|
// }
|
2013-11-19 18:13:24 +00:00
|
|
|
|
2014-04-01 01:47:24 +00:00
|
|
|
public void markAsInsecure(long messageId) {
|
2015-12-03 19:47:28 +00:00
|
|
|
updateMailboxBitmask(messageId, Types.SECURE_MESSAGE_BIT, 0, Optional.<Long>absent());
|
2014-04-01 01:47:24 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 17:49:01 +00:00
|
|
|
// public void markAsPush(long messageId) {
|
|
|
|
// updateMailboxBitmask(messageId, 0, Types.PUSH_MESSAGE_BIT, Optional.<Long>absent());
|
|
|
|
// }
|
2014-02-21 07:00:38 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void markAsDecryptFailed(long messageId, long threadId) {
|
2015-12-03 19:47:28 +00:00
|
|
|
updateMailboxBitmask(messageId, Types.ENCRYPTION_MASK, Types.ENCRYPTION_REMOTE_FAILED_BIT, Optional.of(threadId));
|
2012-09-08 03:03:23 +00:00
|
|
|
notifyConversationListeners(threadId);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2014-03-19 19:37:46 +00:00
|
|
|
public void markAsDecryptDuplicate(long messageId, long threadId) {
|
2015-12-03 19:47:28 +00:00
|
|
|
updateMailboxBitmask(messageId, Types.ENCRYPTION_MASK, Types.ENCRYPTION_REMOTE_DUPLICATE_BIT, Optional.of(threadId));
|
2014-03-19 19:37:46 +00:00
|
|
|
notifyConversationListeners(threadId);
|
|
|
|
}
|
|
|
|
|
2014-04-10 03:02:46 +00:00
|
|
|
public void markAsLegacyVersion(long messageId, long threadId) {
|
2015-12-03 19:47:28 +00:00
|
|
|
updateMailboxBitmask(messageId, Types.ENCRYPTION_MASK, Types.ENCRYPTION_REMOTE_LEGACY_BIT, Optional.of(threadId));
|
2014-04-10 03:02:46 +00:00
|
|
|
notifyConversationListeners(threadId);
|
|
|
|
}
|
|
|
|
|
2017-10-02 21:54:55 +00:00
|
|
|
@Override
|
2016-08-16 03:23:56 +00:00
|
|
|
public void markExpireStarted(long messageId) {
|
|
|
|
markExpireStarted(messageId, System.currentTimeMillis());
|
|
|
|
}
|
|
|
|
|
2017-10-02 21:54:55 +00:00
|
|
|
@Override
|
2016-08-16 03:23:56 +00:00
|
|
|
public void markExpireStarted(long messageId, long startedTimestamp) {
|
|
|
|
ContentValues contentValues = new ContentValues();
|
|
|
|
contentValues.put(EXPIRE_STARTED, startedTimestamp);
|
|
|
|
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
db.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {String.valueOf(messageId)});
|
|
|
|
|
|
|
|
long threadId = getThreadIdForMessage(messageId);
|
|
|
|
notifyConversationListeners(threadId);
|
|
|
|
}
|
|
|
|
|
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)});
|
|
|
|
}
|
|
|
|
|
2017-10-09 01:09:46 +00:00
|
|
|
|
2016-10-10 18:13:37 +00:00
|
|
|
public List<MarkedMessageInfo> setMessagesRead(long threadId) {
|
2017-10-09 01:09:46 +00:00
|
|
|
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> result = new LinkedList<>();
|
|
|
|
Cursor cursor = null;
|
2016-02-20 01:07:41 +00:00
|
|
|
|
|
|
|
database.beginTransaction();
|
|
|
|
|
|
|
|
try {
|
2017-10-09 01:09:46 +00:00
|
|
|
cursor = database.query(TABLE_NAME, new String[] {ID, ADDRESS, DATE_SENT, MESSAGE_BOX, EXPIRES_IN, EXPIRE_STARTED}, where, arguments, null, null, null);
|
2016-02-20 01:07:41 +00:00
|
|
|
|
|
|
|
while(cursor != null && cursor.moveToNext()) {
|
2016-10-10 18:13:37 +00:00
|
|
|
if (Types.isSecureType(cursor.getLong(3))) {
|
2017-07-26 16:59:15 +00:00
|
|
|
SyncMessageId syncMessageId = new SyncMessageId(Address.fromSerialized(cursor.getString(1)), cursor.getLong(2));
|
2016-10-10 18:13:37 +00:00
|
|
|
ExpirationInfo expirationInfo = new ExpirationInfo(cursor.getLong(0), cursor.getLong(4), cursor.getLong(5), true);
|
|
|
|
|
|
|
|
result.add(new MarkedMessageInfo(syncMessageId, expirationInfo));
|
2016-02-20 01:07:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ContentValues contentValues = new ContentValues();
|
|
|
|
contentValues.put(READ, 1);
|
|
|
|
|
2017-10-09 01:09:46 +00:00
|
|
|
database.update(TABLE_NAME, contentValues, where, arguments);
|
2016-02-20 01:07:41 +00:00
|
|
|
database.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) cursor.close();
|
|
|
|
database.endTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-10-02 19:08:30 +00:00
|
|
|
public List<Pair<Long, Long>> setTimestampRead(SyncMessageId messageId, long expireStarted) {
|
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
List<Pair<Long, Long>> expiring = new LinkedList<>();
|
|
|
|
Cursor cursor = null;
|
2016-02-20 01:07:41 +00:00
|
|
|
|
|
|
|
try {
|
2017-08-01 15:56:00 +00:00
|
|
|
cursor = database.query(TABLE_NAME, new String[] {ID, THREAD_ID, MESSAGE_BOX, EXPIRES_IN, ADDRESS}, DATE_SENT + " = ?", new String[] {String.valueOf(messageId.getTimetamp())}, null, null, null, null);
|
2016-02-20 01:07:41 +00:00
|
|
|
|
|
|
|
while (cursor.moveToNext()) {
|
2017-08-01 15:56:00 +00:00
|
|
|
Address theirAddress = Address.fromSerialized(cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS)));
|
|
|
|
Address ourAddress = messageId.getAddress();
|
2016-02-20 01:07:41 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
if (ourAddress.equals(theirAddress) || theirAddress.isGroup()) {
|
|
|
|
long id = cursor.getLong(cursor.getColumnIndexOrThrow(ID));
|
|
|
|
long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(THREAD_ID));
|
|
|
|
long expiresIn = cursor.getLong(cursor.getColumnIndexOrThrow(EXPIRES_IN));
|
2016-02-20 01:07:41 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(READ, 1);
|
2016-10-02 19:08:30 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
if (expiresIn > 0) {
|
|
|
|
values.put(EXPIRE_STARTED, expireStarted);
|
|
|
|
expiring.add(new Pair<>(id, expiresIn));
|
|
|
|
}
|
2016-10-02 19:08:30 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
database.update(TABLE_NAME, values, ID_WHERE, new String[]{String.valueOf(id)});
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2017-08-01 15:56:00 +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;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public void updateMessageBody(long messageId, String body) {
|
|
|
|
long type = 0;
|
2015-07-07 00:36:49 +00:00
|
|
|
|
|
|
|
updateMessageBodyAndType(messageId, body, Types.ENCRYPTION_MASK, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Pair<Long, Long> updateMessageBodyAndType(long messageId, String body, long maskOff, long maskOn) {
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
db.execSQL("UPDATE " + TABLE_NAME + " SET " + BODY + " = ?, " +
|
|
|
|
MESSAGE_BOX + " = (" + MESSAGE_BOX + " & " + (Types.TOTAL_MASK - maskOff) + " | " + maskOn + ") " +
|
|
|
|
"WHERE " + ID + " = ?",
|
|
|
|
new String[] {body, messageId + ""});
|
|
|
|
|
|
|
|
long threadId = getThreadIdForMessage(messageId);
|
|
|
|
|
2015-11-23 23:07:41 +00:00
|
|
|
DatabaseFactory.getThreadDatabase(context).update(threadId, true);
|
2015-07-07 00:36:49 +00:00
|
|
|
notifyConversationListeners(threadId);
|
|
|
|
notifyConversationListListeners();
|
|
|
|
|
|
|
|
return new Pair<>(messageId, threadId);
|
|
|
|
}
|
|
|
|
|
2017-05-08 22:32:59 +00:00
|
|
|
public Optional<MmsNotificationInfo> getNotification(long messageId) {
|
2015-10-24 16:40:04 +00:00
|
|
|
Cursor cursor = null;
|
2014-11-03 23:16:04 +00:00
|
|
|
|
|
|
|
try {
|
2015-10-24 16:40:04 +00:00
|
|
|
cursor = rawQuery(RAW_ID_WHERE, new String[] {String.valueOf(messageId)});
|
2014-11-03 23:16:04 +00:00
|
|
|
|
|
|
|
if (cursor != null && cursor.moveToNext()) {
|
2017-09-08 18:19:57 +00:00
|
|
|
return Optional.of(new MmsNotificationInfo(cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS)),
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(CONTENT_LOCATION)),
|
2017-05-08 22:32:59 +00:00
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(TRANSACTION_ID)),
|
|
|
|
cursor.getInt(cursor.getColumnIndexOrThrow(SUBSCRIPTION_ID))));
|
2014-11-03 23:16:04 +00:00
|
|
|
} else {
|
|
|
|
return Optional.absent();
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public OutgoingMediaMessage getOutgoingMessage(long messageId)
|
2014-11-08 19:35:58 +00:00
|
|
|
throws MmsException, NoSuchMessageException
|
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
|
|
|
{
|
2015-10-13 01:25:05 +00:00
|
|
|
AttachmentDatabase attachmentDatabase = DatabaseFactory.getAttachmentDatabase(context);
|
|
|
|
Cursor cursor = null;
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
2015-10-24 16:40:04 +00:00
|
|
|
cursor = rawQuery(RAW_ID_WHERE, new String[] {String.valueOf(messageId)});
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2014-11-08 19:35:58 +00:00
|
|
|
if (cursor != null && cursor.moveToNext()) {
|
2017-08-01 15:56:00 +00:00
|
|
|
long outboxType = cursor.getLong(cursor.getColumnIndexOrThrow(MESSAGE_BOX));
|
2018-01-25 03:17:44 +00:00
|
|
|
String body = cursor.getString(cursor.getColumnIndexOrThrow(BODY));
|
2017-08-01 15:56:00 +00:00
|
|
|
long timestamp = cursor.getLong(cursor.getColumnIndexOrThrow(NORMALIZED_DATE_SENT));
|
|
|
|
int subscriptionId = cursor.getInt(cursor.getColumnIndexOrThrow(SUBSCRIPTION_ID));
|
|
|
|
long expiresIn = cursor.getLong(cursor.getColumnIndexOrThrow(EXPIRES_IN));
|
2018-01-25 03:17:44 +00:00
|
|
|
List<Attachment> attachments = new LinkedList<>(attachmentDatabase.getAttachmentsForMessage(messageId));
|
2017-08-01 15:56:00 +00:00
|
|
|
String address = cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS));
|
|
|
|
long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(THREAD_ID));
|
|
|
|
int distributionType = DatabaseFactory.getThreadDatabase(context).getDistributionType(threadId);
|
|
|
|
|
2017-08-22 01:32:38 +00:00
|
|
|
Recipient recipient = Recipient.from(context, Address.fromSerialized(address), false);
|
2015-10-13 01:25:05 +00:00
|
|
|
|
|
|
|
if (body != null && (Types.isGroupQuit(outboxType) || Types.isGroupUpdate(outboxType))) {
|
2017-08-01 15:56:00 +00:00
|
|
|
return new OutgoingGroupMediaMessage(recipient, body, attachments, timestamp, 0);
|
2016-08-16 03:23:56 +00:00
|
|
|
} else if (Types.isExpirationTimerUpdate(outboxType)) {
|
2017-08-01 15:56:00 +00:00
|
|
|
return new OutgoingExpirationUpdateMessage(recipient, timestamp, expiresIn);
|
2015-10-13 01:25:05 +00:00
|
|
|
}
|
2013-05-05 19:51:36 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
OutgoingMediaMessage message = new OutgoingMediaMessage(recipient, body, attachments, timestamp, subscriptionId, expiresIn, distributionType);
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
if (Types.isSecureType(outboxType)) {
|
|
|
|
return new OutgoingSecureMediaMessage(message);
|
2013-05-05 19:51:36 +00:00
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
return message;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
2014-11-08 19:35:58 +00:00
|
|
|
throw new NoSuchMessageException("No record found for id: " + messageId);
|
2015-10-13 01:25:05 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
throw new MmsException(e);
|
2011-12-20 18:20:44 +00:00
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
2012-09-08 03:03:23 +00:00
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public long copyMessageInbox(long messageId) throws MmsException {
|
2014-11-08 19:35:58 +00:00
|
|
|
try {
|
2018-01-25 03:17:44 +00:00
|
|
|
OutgoingMediaMessage request = getOutgoingMessage(messageId);
|
2015-10-13 01:25:05 +00:00
|
|
|
ContentValues contentValues = new ContentValues();
|
2017-08-01 15:56:00 +00:00
|
|
|
contentValues.put(ADDRESS, request.getRecipient().getAddress().serialize());
|
2015-10-13 01:25:05 +00:00
|
|
|
contentValues.put(DATE_SENT, request.getSentTimeMillis());
|
2014-11-08 19:35:58 +00:00
|
|
|
contentValues.put(MESSAGE_BOX, Types.BASE_INBOX_TYPE | Types.SECURE_MESSAGE_BIT | Types.ENCRYPTION_SYMMETRIC_BIT);
|
|
|
|
contentValues.put(THREAD_ID, getThreadIdForMessage(messageId));
|
|
|
|
contentValues.put(READ, 1);
|
|
|
|
contentValues.put(DATE_RECEIVED, contentValues.getAsLong(DATE_SENT));
|
2016-08-16 03:23:56 +00:00
|
|
|
contentValues.put(EXPIRES_IN, request.getExpiresIn());
|
2014-11-08 19:35:58 +00:00
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
List<Attachment> attachments = new LinkedList<>();
|
|
|
|
|
|
|
|
for (Attachment attachment : request.getAttachments()) {
|
|
|
|
DatabaseAttachment databaseAttachment = (DatabaseAttachment)attachment;
|
|
|
|
attachments.add(new DatabaseAttachment(databaseAttachment.getAttachmentId(),
|
|
|
|
databaseAttachment.getMmsId(),
|
|
|
|
databaseAttachment.hasData(),
|
2016-12-11 21:37:27 +00:00
|
|
|
databaseAttachment.hasThumbnail(),
|
2015-10-13 01:25:05 +00:00
|
|
|
databaseAttachment.getContentType(),
|
|
|
|
AttachmentDatabase.TRANSFER_PROGRESS_DONE,
|
|
|
|
databaseAttachment.getSize(),
|
2017-03-28 19:05:30 +00:00
|
|
|
databaseAttachment.getFileName(),
|
2015-10-13 01:25:05 +00:00
|
|
|
databaseAttachment.getLocation(),
|
|
|
|
databaseAttachment.getKey(),
|
2017-02-26 18:06:27 +00:00
|
|
|
databaseAttachment.getRelay(),
|
2017-04-22 23:29:26 +00:00
|
|
|
databaseAttachment.getDigest(),
|
2017-05-12 05:46:35 +00:00
|
|
|
databaseAttachment.getFastPreflightId(),
|
|
|
|
databaseAttachment.isVoiceNote()));
|
2015-09-04 17:56:59 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
return insertMediaMessage(request.getBody(),
|
2015-10-13 01:25:05 +00:00
|
|
|
attachments,
|
2017-05-25 15:40:43 +00:00
|
|
|
contentValues,
|
|
|
|
null);
|
2014-11-08 19:35:58 +00:00
|
|
|
} catch (NoSuchMessageException e) {
|
|
|
|
throw new MmsException(e);
|
|
|
|
}
|
2014-10-23 01:28:03 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
private Optional<InsertResult> insertMessageInbox(IncomingMediaMessage retrieved,
|
2017-01-22 21:52:36 +00:00
|
|
|
String contentLocation,
|
|
|
|
long threadId, long mailbox)
|
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
|
|
|
throws MmsException
|
|
|
|
{
|
2013-07-19 00:42:45 +00:00
|
|
|
if (threadId == -1 || retrieved.isGroupMessage()) {
|
2013-04-26 01:59:49 +00:00
|
|
|
try {
|
|
|
|
threadId = getThreadIdFor(retrieved);
|
|
|
|
} catch (RecipientFormattingException e) {
|
|
|
|
Log.w("MmsDatabase", e);
|
2013-07-19 00:42:45 +00:00
|
|
|
if (threadId == -1)
|
|
|
|
throw new MmsException(e);
|
2013-04-26 01:59:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
ContentValues contentValues = new ContentValues();
|
|
|
|
|
|
|
|
contentValues.put(DATE_SENT, retrieved.getSentTimeMillis());
|
2017-08-01 15:56:00 +00:00
|
|
|
contentValues.put(ADDRESS, retrieved.getFrom().serialize());
|
2015-10-13 01:25:05 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
contentValues.put(MESSAGE_BOX, mailbox);
|
2015-10-13 01:25:05 +00:00
|
|
|
contentValues.put(MESSAGE_TYPE, PduHeaders.MESSAGE_TYPE_RETRIEVE_CONF);
|
2011-12-20 18:20:44 +00:00
|
|
|
contentValues.put(THREAD_ID, threadId);
|
|
|
|
contentValues.put(CONTENT_LOCATION, contentLocation);
|
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.put(STATUS, Status.DOWNLOAD_INITIALIZED);
|
2015-08-04 20:37:22 +00:00
|
|
|
contentValues.put(DATE_RECEIVED, generatePduCompatTimestamp());
|
2015-10-24 16:40:04 +00:00
|
|
|
contentValues.put(PART_COUNT, retrieved.getAttachments().size());
|
2016-02-06 00:10:33 +00:00
|
|
|
contentValues.put(SUBSCRIPTION_ID, retrieved.getSubscriptionId());
|
2016-08-16 03:23:56 +00:00
|
|
|
contentValues.put(EXPIRES_IN, retrieved.getExpiresIn());
|
2016-10-10 17:00:11 +00:00
|
|
|
contentValues.put(READ, retrieved.isExpirationUpdate() ? 1 : 0);
|
2013-01-06 21:13:14 +00:00
|
|
|
|
2013-07-19 00:42:45 +00:00
|
|
|
if (!contentValues.containsKey(DATE_SENT)) {
|
2013-01-06 21:13:14 +00:00
|
|
|
contentValues.put(DATE_SENT, contentValues.getAsLong(DATE_RECEIVED));
|
2013-07-19 00:42:45 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2017-01-22 21:52:36 +00:00
|
|
|
if (retrieved.isPushMessage() && isDuplicate(retrieved, threadId)) {
|
|
|
|
Log.w(TAG, "Ignoring duplicate media message (" + retrieved.getSentTimeMillis() + ")");
|
|
|
|
return Optional.absent();
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
long messageId = insertMediaMessage(retrieved.getBody(), retrieved.getAttachments(), contentValues, null);
|
2013-12-07 19:00:20 +00:00
|
|
|
|
2016-08-16 03:23:56 +00:00
|
|
|
if (!Types.isExpirationTimerUpdate(mailbox)) {
|
2017-11-14 02:01:05 +00:00
|
|
|
DatabaseFactory.getThreadDatabase(context).incrementUnread(threadId, 1);
|
2016-08-16 03:23:56 +00:00
|
|
|
DatabaseFactory.getThreadDatabase(context).update(threadId, true);
|
|
|
|
}
|
|
|
|
|
2013-05-05 20:14:23 +00:00
|
|
|
notifyConversationListeners(threadId);
|
2014-11-25 06:50:32 +00:00
|
|
|
jobManager.add(new TrimThreadJob(context, threadId));
|
2013-04-26 18:23:43 +00:00
|
|
|
|
2017-01-22 21:52:36 +00:00
|
|
|
return Optional.of(new InsertResult(messageId, threadId));
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public Optional<InsertResult> insertMessageInbox(IncomingMediaMessage retrieved,
|
2017-01-22 21:52:36 +00:00
|
|
|
String contentLocation, long 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
|
|
|
throws MmsException
|
|
|
|
{
|
2015-07-07 00:36:49 +00:00
|
|
|
long type = Types.BASE_INBOX_TYPE;
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2015-07-07 00:36:49 +00:00
|
|
|
if (retrieved.isPushMessage()) {
|
|
|
|
type |= Types.PUSH_MESSAGE_BIT;
|
|
|
|
}
|
|
|
|
|
2016-08-16 03:23:56 +00:00
|
|
|
if (retrieved.isExpirationUpdate()) {
|
|
|
|
type |= Types.EXPIRATION_TIMER_UPDATE_BIT;
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
return insertMessageInbox(retrieved, contentLocation, threadId, type);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public Optional<InsertResult> insertSecureDecryptedMessageInbox(IncomingMediaMessage retrieved, long 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
|
|
|
throws MmsException
|
|
|
|
{
|
2015-07-07 00:36:49 +00:00
|
|
|
long type = Types.BASE_INBOX_TYPE | Types.SECURE_MESSAGE_BIT;
|
|
|
|
|
|
|
|
if (retrieved.isPushMessage()) {
|
|
|
|
type |= Types.PUSH_MESSAGE_BIT;
|
|
|
|
}
|
|
|
|
|
2016-08-16 03:23:56 +00:00
|
|
|
if (retrieved.isExpirationUpdate()) {
|
|
|
|
type |= Types.EXPIRATION_TIMER_UPDATE_BIT;
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
return insertMessageInbox(retrieved, "", threadId, type);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2016-02-06 00:10:33 +00:00
|
|
|
public Pair<Long, Long> insertMessageInbox(@NonNull NotificationInd notification, int subscriptionId) {
|
2017-08-01 15:56:00 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
2015-10-13 01:25:05 +00:00
|
|
|
long threadId = getThreadIdFor(notification);
|
|
|
|
ContentValues contentValues = new ContentValues();
|
|
|
|
ContentValuesBuilder contentBuilder = new ContentValuesBuilder(contentValues);
|
|
|
|
|
2017-05-08 22:32:59 +00:00
|
|
|
Log.w(TAG, "Message received type: " + notification.getMessageType());
|
2015-03-27 17:26:17 +00:00
|
|
|
|
|
|
|
|
2017-05-08 22:32:59 +00:00
|
|
|
contentBuilder.add(CONTENT_LOCATION, notification.getContentLocation());
|
|
|
|
contentBuilder.add(DATE_SENT, System.currentTimeMillis());
|
|
|
|
contentBuilder.add(EXPIRY, notification.getExpiry());
|
|
|
|
contentBuilder.add(MESSAGE_SIZE, notification.getMessageSize());
|
|
|
|
contentBuilder.add(TRANSACTION_ID, notification.getTransactionId());
|
|
|
|
contentBuilder.add(MESSAGE_TYPE, notification.getMessageType());
|
2015-10-13 01:25:05 +00:00
|
|
|
|
2017-05-08 22:32:59 +00:00
|
|
|
if (notification.getFrom() != null) {
|
2017-08-01 15:56:00 +00:00
|
|
|
contentValues.put(ADDRESS, Address.fromExternal(context, Util.toIsoString(notification.getFrom().getTextString())).serialize());
|
2015-10-13 01:25:05 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 17:26:17 +00:00
|
|
|
contentValues.put(MESSAGE_BOX, Types.BASE_INBOX_TYPE);
|
|
|
|
contentValues.put(THREAD_ID, threadId);
|
|
|
|
contentValues.put(STATUS, Status.DOWNLOAD_INITIALIZED);
|
2015-08-04 20:37:22 +00:00
|
|
|
contentValues.put(DATE_RECEIVED, generatePduCompatTimestamp());
|
2015-03-27 17:26:17 +00:00
|
|
|
contentValues.put(READ, Util.isDefaultSmsProvider(context) ? 0 : 1);
|
2016-02-06 00:10:33 +00:00
|
|
|
contentValues.put(SUBSCRIPTION_ID, subscriptionId);
|
2015-03-27 17:26:17 +00:00
|
|
|
|
|
|
|
if (!contentValues.containsKey(DATE_SENT))
|
|
|
|
contentValues.put(DATE_SENT, contentValues.getAsLong(DATE_RECEIVED));
|
|
|
|
|
|
|
|
long messageId = db.insert(TABLE_NAME, null, contentValues);
|
2017-01-29 21:16:10 +00:00
|
|
|
|
2015-03-27 17:26:17 +00:00
|
|
|
return new Pair<>(messageId, threadId);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2013-04-26 18:23:43 +00:00
|
|
|
public void markIncomingNotificationReceived(long threadId) {
|
|
|
|
notifyConversationListeners(threadId);
|
2015-11-23 23:07:41 +00:00
|
|
|
DatabaseFactory.getThreadDatabase(context).update(threadId, true);
|
2013-12-07 19:00:20 +00:00
|
|
|
|
2014-01-07 02:52:18 +00:00
|
|
|
if (org.thoughtcrime.securesms.util.Util.isDefaultSmsProvider(context)) {
|
2017-11-14 02:01:05 +00:00
|
|
|
DatabaseFactory.getThreadDatabase(context).incrementUnread(threadId, 1);
|
2013-12-07 19:00:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 06:50:32 +00:00
|
|
|
jobManager.add(new TrimThreadJob(context, threadId));
|
2013-04-26 18:23:43 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public long insertMessageOutbox(@NonNull OutgoingMediaMessage message,
|
2017-04-22 23:29:26 +00:00
|
|
|
long threadId, boolean forceSms,
|
2017-10-02 21:54:55 +00:00
|
|
|
@Nullable SmsDatabase.InsertListener insertListener)
|
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
|
|
|
throws MmsException
|
|
|
|
{
|
2016-12-21 17:49:01 +00:00
|
|
|
long type = Types.BASE_SENDING_TYPE;
|
2015-07-07 00:36:49 +00:00
|
|
|
|
2016-12-21 17:49:01 +00:00
|
|
|
if (message.isSecure()) type |= (Types.SECURE_MESSAGE_BIT | Types.PUSH_MESSAGE_BIT);
|
2014-06-11 22:31:59 +00:00
|
|
|
if (forceSms) type |= Types.MESSAGE_FORCE_SMS_BIT;
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2014-02-20 05:06:54 +00:00
|
|
|
if (message.isGroup()) {
|
2014-02-22 01:51:25 +00:00
|
|
|
if (((OutgoingGroupMediaMessage)message).isGroupUpdate()) type |= Types.GROUP_UPDATE_BIT;
|
2014-02-20 05:06:54 +00:00
|
|
|
else if (((OutgoingGroupMediaMessage)message).isGroupQuit()) type |= Types.GROUP_QUIT_BIT;
|
|
|
|
}
|
|
|
|
|
2016-08-16 03:23:56 +00:00
|
|
|
if (message.isExpirationUpdate()) {
|
|
|
|
type |= Types.EXPIRATION_TIMER_UPDATE_BIT;
|
|
|
|
}
|
|
|
|
|
2017-10-02 21:54:55 +00:00
|
|
|
Map<Address, Long> earlyDeliveryReceipts = earlyDeliveryReceiptCache.remove(message.getSentTimeMillis());
|
|
|
|
Map<Address, Long> earlyReadReceipts = earlyReadReceiptCache.remove(message.getSentTimeMillis());
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
ContentValues contentValues = new ContentValues();
|
|
|
|
contentValues.put(DATE_SENT, message.getSentTimeMillis());
|
|
|
|
contentValues.put(MESSAGE_TYPE, PduHeaders.MESSAGE_TYPE_SEND_REQ);
|
2014-02-20 05:06:54 +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.put(MESSAGE_BOX, type);
|
2011-12-20 18:20:44 +00:00
|
|
|
contentValues.put(THREAD_ID, threadId);
|
|
|
|
contentValues.put(READ, 1);
|
2015-10-16 17:07:50 +00:00
|
|
|
contentValues.put(DATE_RECEIVED, System.currentTimeMillis());
|
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-08-01 15:56:00 +00:00
|
|
|
contentValues.put(ADDRESS, message.getRecipient().getAddress().serialize());
|
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
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
long messageId = insertMediaMessage(message.getBody(), message.getAttachments(), contentValues, insertListener);
|
2017-04-22 23:29:26 +00:00
|
|
|
|
2017-09-30 15:45:45 +00:00
|
|
|
if (message.getRecipient().getAddress().isGroup()) {
|
|
|
|
List<Recipient> members = DatabaseFactory.getGroupDatabase(context).getGroupMembers(message.getRecipient().getAddress().toGroupString(), false);
|
|
|
|
GroupReceiptDatabase receiptDatabase = DatabaseFactory.getGroupReceiptDatabase(context);
|
|
|
|
|
|
|
|
receiptDatabase.insert(Stream.of(members).map(Recipient::getAddress).toList(),
|
|
|
|
messageId, GroupReceiptDatabase.STATUS_UNDELIVERED, message.getSentTimeMillis());
|
2017-10-02 21:54:55 +00:00
|
|
|
|
|
|
|
for (Address address : earlyDeliveryReceipts.keySet()) receiptDatabase.update(address, messageId, GroupReceiptDatabase.STATUS_DELIVERED, -1);
|
|
|
|
for (Address address : earlyReadReceipts.keySet()) receiptDatabase.update(address, messageId, GroupReceiptDatabase.STATUS_READ, -1);
|
2017-09-30 15:45:45 +00:00
|
|
|
}
|
|
|
|
|
2017-02-26 18:49:48 +00:00
|
|
|
DatabaseFactory.getThreadDatabase(context).setLastSeen(threadId);
|
2017-08-19 00:28:56 +00:00
|
|
|
DatabaseFactory.getThreadDatabase(context).setHasSent(threadId, true);
|
2014-11-25 06:50:32 +00:00
|
|
|
jobManager.add(new TrimThreadJob(context, threadId));
|
2013-01-10 05:06:56 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
return messageId;
|
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
private long insertMediaMessage(@Nullable String body,
|
2015-10-13 01:25:05 +00:00
|
|
|
@NonNull List<Attachment> attachments,
|
2017-05-25 15:40:43 +00:00
|
|
|
@NonNull ContentValues contentValues,
|
|
|
|
@Nullable SmsDatabase.InsertListener insertListener)
|
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
|
|
|
throws MmsException
|
|
|
|
{
|
2018-01-25 03:17:44 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
AttachmentDatabase partsDatabase = DatabaseFactory.getAttachmentDatabase(context);
|
2013-04-26 18:23:43 +00:00
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
contentValues.put(BODY, body);
|
2015-10-13 01:25:05 +00:00
|
|
|
contentValues.put(PART_COUNT, attachments.size());
|
2013-04-26 18:23:43 +00:00
|
|
|
|
2015-04-28 05:16:37 +00:00
|
|
|
db.beginTransaction();
|
|
|
|
try {
|
|
|
|
long messageId = db.insert(TABLE_NAME, null, contentValues);
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
partsDatabase.insertAttachmentsForMessage(messageId, attachments);
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2015-04-28 05:16:37 +00:00
|
|
|
db.setTransactionSuccessful();
|
|
|
|
return messageId;
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2017-05-25 15:40:43 +00:00
|
|
|
if (insertListener != null) {
|
|
|
|
insertListener.onComplete();
|
|
|
|
}
|
|
|
|
|
2015-10-16 20:59:40 +00:00
|
|
|
notifyConversationListeners(contentValues.getAsLong(THREAD_ID));
|
2015-11-23 23:07:41 +00:00
|
|
|
DatabaseFactory.getThreadDatabase(context).update(contentValues.getAsLong(THREAD_ID), true);
|
2015-10-16 20:59:40 +00:00
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2015-03-31 20:36:04 +00:00
|
|
|
public boolean delete(long messageId) {
|
2015-10-13 01:25:05 +00:00
|
|
|
long threadId = getThreadIdForMessage(messageId);
|
|
|
|
AttachmentDatabase attachmentDatabase = DatabaseFactory.getAttachmentDatabase(context);
|
|
|
|
attachmentDatabase.deleteAttachmentsForMessage(messageId);
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2017-09-30 15:45:45 +00:00
|
|
|
GroupReceiptDatabase groupReceiptDatabase = DatabaseFactory.getGroupReceiptDatabase(context);
|
|
|
|
groupReceiptDatabase.deleteRowsForMessage(messageId);
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
database.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-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void deleteThread(long threadId) {
|
2015-10-13 01:25:05 +00:00
|
|
|
Set<Long> singleThreadSet = new HashSet<>();
|
2011-12-20 18:20:44 +00:00
|
|
|
singleThreadSet.add(threadId);
|
|
|
|
deleteThreads(singleThreadSet);
|
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2017-01-22 21:52:36 +00:00
|
|
|
private boolean isDuplicate(IncomingMediaMessage message, long threadId) {
|
|
|
|
SQLiteDatabase database = databaseHelper.getReadableDatabase();
|
|
|
|
Cursor cursor = database.query(TABLE_NAME, null, DATE_SENT + " = ? AND " + ADDRESS + " = ? AND " + THREAD_ID + " = ?",
|
2017-08-01 15:56:00 +00:00
|
|
|
new String[]{String.valueOf(message.getSentTimeMillis()), message.getFrom().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 deleteThreads(Set<Long> threadIds) {
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
String where = "";
|
|
|
|
Cursor cursor = null;
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
for (long threadId : threadIds) {
|
|
|
|
where += THREAD_ID + " = '" + threadId + "' OR ";
|
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
where = where.substring(0, where.length() - 4);
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
|
|
|
cursor = db.query(TABLE_NAME, new String[] {ID}, where, null, null, null, null);
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
while (cursor != null && cursor.moveToNext()) {
|
|
|
|
delete(cursor.getLong(0));
|
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
2012-09-08 03:03:23 +00:00
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 05:06:56 +00:00
|
|
|
/*package*/void deleteMessagesInThreadBeforeDate(long threadId, long date) {
|
|
|
|
Cursor cursor = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
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
|
|
|
String where = THREAD_ID + " = ? AND (CASE (" + MESSAGE_BOX + " & " + Types.BASE_TYPE_MASK + ") ";
|
2013-01-10 05:06:56 +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
|
|
|
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)");
|
|
|
|
|
|
|
|
Log.w("MmsDatabase", "Executing trim query: " + where);
|
|
|
|
cursor = db.query(TABLE_NAME, new String[] {ID}, where, new String[] {threadId+""}, null, null, null);
|
|
|
|
|
|
|
|
while (cursor != null && cursor.moveToNext()) {
|
|
|
|
Log.w("MmsDatabase", "Trimming: " + cursor.getLong(0));
|
|
|
|
delete(cursor.getLong(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void deleteAllThreads() {
|
2015-10-13 01:25:05 +00:00
|
|
|
DatabaseFactory.getAttachmentDatabase(context).deleteAllAttachments();
|
2017-09-30 15:45:45 +00:00
|
|
|
DatabaseFactory.getGroupReceiptDatabase(context).deleteAllRows();
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
SQLiteDatabase database = databaseHelper.getWritableDatabase();
|
|
|
|
database.delete(TABLE_NAME, null, null);
|
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2012-09-30 18:46:45 +00:00
|
|
|
public Cursor getCarrierMmsInformation(String apn) {
|
2012-11-21 03:09:46 +00:00
|
|
|
Uri uri = Uri.withAppendedPath(Uri.parse("content://telephony/carriers"), "current");
|
2014-11-12 19:15:05 +00:00
|
|
|
String selection = TextUtils.isEmpty(apn) ? null : "apn = ?";
|
|
|
|
String[] selectionArgs = TextUtils.isEmpty(apn) ? null : new String[] {apn.trim()};
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2013-03-17 18:19:36 +00:00
|
|
|
try {
|
|
|
|
return context.getContentResolver().query(uri, null, selection, selectionArgs, null);
|
|
|
|
} catch (NullPointerException npe) {
|
|
|
|
// NOTE - This is dumb, but on some devices there's an NPE in the Android framework
|
|
|
|
// for the provider of this call, which gets rethrown back to here through a binder
|
|
|
|
// call.
|
|
|
|
throw new IllegalArgumentException(npe);
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public Reader readerFor(Cursor cursor) {
|
|
|
|
return new Reader(cursor);
|
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-08 03:03:23 +00:00
|
|
|
|
2017-04-22 23:29:26 +00:00
|
|
|
public OutgoingMessageReader readerFor(OutgoingMediaMessage message, long threadId) {
|
|
|
|
return new OutgoingMessageReader(message, 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
|
|
|
public static class Status {
|
2011-12-20 18:20:44 +00:00
|
|
|
public static final int DOWNLOAD_INITIALIZED = 1;
|
|
|
|
public static final int DOWNLOAD_NO_CONNECTIVITY = 2;
|
|
|
|
public static final int DOWNLOAD_CONNECTING = 3;
|
2012-10-01 02:56:29 +00:00
|
|
|
public static final int DOWNLOAD_SOFT_FAILURE = 4;
|
|
|
|
public static final int DOWNLOAD_HARD_FAILURE = 5;
|
2013-09-16 07:55:01 +00:00
|
|
|
public static final int DOWNLOAD_APN_UNAVAILABLE = 6;
|
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-08 03:03:23 +00:00
|
|
|
|
2017-05-08 22:32:59 +00:00
|
|
|
public static class MmsNotificationInfo {
|
2017-09-08 18:19:57 +00:00
|
|
|
private final Address from;
|
|
|
|
private final String contentLocation;
|
|
|
|
private final String transactionId;
|
|
|
|
private final int subscriptionId;
|
2017-05-08 22:32:59 +00:00
|
|
|
|
2017-09-08 18:19:57 +00:00
|
|
|
MmsNotificationInfo(@Nullable String from, String contentLocation, String transactionId, int subscriptionId) {
|
|
|
|
this.from = from == null ? null : Address.fromSerialized(from);
|
2017-05-08 22:32:59 +00:00
|
|
|
this.contentLocation = contentLocation;
|
|
|
|
this.transactionId = transactionId;
|
|
|
|
this.subscriptionId = subscriptionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getContentLocation() {
|
|
|
|
return contentLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getTransactionId() {
|
|
|
|
return transactionId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getSubscriptionId() {
|
|
|
|
return subscriptionId;
|
|
|
|
}
|
2017-09-08 18:19:57 +00:00
|
|
|
|
|
|
|
public @Nullable Address getFrom() {
|
|
|
|
return from;
|
|
|
|
}
|
2017-05-08 22:32:59 +00:00
|
|
|
}
|
|
|
|
|
2017-04-22 23:29:26 +00:00
|
|
|
public class OutgoingMessageReader {
|
|
|
|
|
|
|
|
private final OutgoingMediaMessage message;
|
|
|
|
private final long id;
|
|
|
|
private final long threadId;
|
|
|
|
|
|
|
|
public OutgoingMessageReader(OutgoingMediaMessage message, long threadId) {
|
|
|
|
try {
|
|
|
|
this.message = message;
|
|
|
|
this.id = SecureRandom.getInstance("SHA1PRNG").nextLong();
|
|
|
|
this.threadId = threadId;
|
|
|
|
} catch (NoSuchAlgorithmException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public MessageRecord getCurrent() {
|
|
|
|
SlideDeck slideDeck = new SlideDeck(context, message.getAttachments());
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
return new MediaMmsMessageRecord(context, id, message.getRecipient(), message.getRecipient(),
|
2017-04-22 23:29:26 +00:00
|
|
|
1, System.currentTimeMillis(), System.currentTimeMillis(),
|
|
|
|
0, threadId, new DisplayRecord.Body(message.getBody(), true),
|
|
|
|
slideDeck, slideDeck.getSlides().size(),
|
|
|
|
message.isSecure() ? MmsSmsColumns.Types.getOutgoingEncryptedMessageType() : MmsSmsColumns.Types.getOutgoingSmsMessageType(),
|
|
|
|
new LinkedList<IdentityKeyMismatch>(),
|
|
|
|
new LinkedList<NetworkFailure>(),
|
|
|
|
message.getSubscriptionId(),
|
|
|
|
message.getExpiresIn(),
|
2017-09-16 05:38:53 +00:00
|
|
|
System.currentTimeMillis(), 0);
|
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 {
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
private final Cursor cursor;
|
2013-04-26 18:23:43 +00:00
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
public Reader(Cursor cursor) {
|
|
|
|
this.cursor = cursor;
|
2011-12-20 18:20:44 +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 MessageRecord getNext() {
|
|
|
|
if (cursor == null || !cursor.moveToNext())
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return getCurrent();
|
2011-12-20 18:20:44 +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 MessageRecord getCurrent() {
|
|
|
|
long mmsType = cursor.getLong(cursor.getColumnIndexOrThrow(MmsDatabase.MESSAGE_TYPE));
|
2012-09-08 03:03:23 +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
|
|
|
if (mmsType == PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND) {
|
|
|
|
return getNotificationMmsMessageRecord(cursor);
|
|
|
|
} else {
|
|
|
|
return getMediaMmsMessageRecord(cursor);
|
2011-12-20 18:20:44 +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
|
|
|
}
|
2012-09-08 03:03:23 +00:00
|
|
|
|
2013-05-05 19:51:36 +00:00
|
|
|
private NotificationMmsMessageRecord getNotificationMmsMessageRecord(Cursor cursor) {
|
2017-09-16 05:38:53 +00:00
|
|
|
long id = cursor.getLong(cursor.getColumnIndexOrThrow(MmsDatabase.ID));
|
|
|
|
long dateSent = cursor.getLong(cursor.getColumnIndexOrThrow(MmsDatabase.NORMALIZED_DATE_SENT));
|
|
|
|
long dateReceived = cursor.getLong(cursor.getColumnIndexOrThrow(MmsDatabase.NORMALIZED_DATE_RECEIVED));
|
|
|
|
long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(MmsDatabase.THREAD_ID));
|
|
|
|
long mailbox = cursor.getLong(cursor.getColumnIndexOrThrow(MmsDatabase.MESSAGE_BOX));
|
|
|
|
String address = cursor.getString(cursor.getColumnIndexOrThrow(MmsDatabase.ADDRESS));
|
|
|
|
int addressDeviceId = cursor.getInt(cursor.getColumnIndexOrThrow(MmsDatabase.ADDRESS_DEVICE_ID));
|
|
|
|
Recipient recipient = getRecipientFor(address);
|
|
|
|
|
|
|
|
String contentLocation = cursor.getString(cursor.getColumnIndexOrThrow(MmsDatabase.CONTENT_LOCATION));
|
|
|
|
String transactionId = cursor.getString(cursor.getColumnIndexOrThrow(MmsDatabase.TRANSACTION_ID));
|
|
|
|
long messageSize = cursor.getLong(cursor.getColumnIndexOrThrow(MmsDatabase.MESSAGE_SIZE));
|
|
|
|
long expiry = cursor.getLong(cursor.getColumnIndexOrThrow(MmsDatabase.EXPIRY));
|
|
|
|
int status = cursor.getInt(cursor.getColumnIndexOrThrow(MmsDatabase.STATUS));
|
|
|
|
int deliveryReceiptCount = cursor.getInt(cursor.getColumnIndexOrThrow(MmsDatabase.DELIVERY_RECEIPT_COUNT));
|
|
|
|
int readReceiptCount = cursor.getInt(cursor.getColumnIndexOrThrow(MmsDatabase.READ_RECEIPT_COUNT));
|
|
|
|
int subscriptionId = cursor.getInt(cursor.getColumnIndexOrThrow(MmsDatabase.SUBSCRIPTION_ID));
|
|
|
|
|
|
|
|
if (!TextSecurePreferences.isReadReceiptsEnabled(context)) {
|
|
|
|
readReceiptCount = 0;
|
|
|
|
}
|
2013-05-05 19:51:36 +00:00
|
|
|
|
|
|
|
byte[]contentLocationBytes = null;
|
|
|
|
byte[]transactionIdBytes = null;
|
|
|
|
|
2014-11-12 19:15:05 +00:00
|
|
|
if (!TextUtils.isEmpty(contentLocation))
|
2013-07-10 02:48:33 +00:00
|
|
|
contentLocationBytes = org.thoughtcrime.securesms.util.Util.toIsoBytes(contentLocation);
|
2013-05-05 19:51:36 +00:00
|
|
|
|
2014-11-12 19:15:05 +00:00
|
|
|
if (!TextUtils.isEmpty(transactionId))
|
2013-07-10 02:48:33 +00:00
|
|
|
transactionIdBytes = org.thoughtcrime.securesms.util.Util.toIsoBytes(transactionId);
|
2013-05-05 19:51:36 +00:00
|
|
|
|
2017-01-20 23:26:17 +00:00
|
|
|
SlideDeck slideDeck = new SlideDeck(context, new MmsNotificationAttachment(status, messageSize));
|
|
|
|
|
2013-05-05 19:51:36 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
return new NotificationMmsMessageRecord(context, id, recipient, recipient,
|
2017-09-16 05:38:53 +00:00
|
|
|
addressDeviceId, dateSent, dateReceived, deliveryReceiptCount, threadId,
|
2014-02-03 03:38:06 +00:00
|
|
|
contentLocationBytes, messageSize, expiry, status,
|
2017-09-16 05:38:53 +00:00
|
|
|
transactionIdBytes, mailbox, subscriptionId, slideDeck,
|
|
|
|
readReceiptCount);
|
2013-05-05 19:51: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
|
|
|
private MediaMmsMessageRecord getMediaMmsMessageRecord(Cursor cursor) {
|
2017-09-16 05:38:53 +00:00
|
|
|
long id = cursor.getLong(cursor.getColumnIndexOrThrow(MmsDatabase.ID));
|
|
|
|
long dateSent = cursor.getLong(cursor.getColumnIndexOrThrow(MmsDatabase.NORMALIZED_DATE_SENT));
|
|
|
|
long dateReceived = cursor.getLong(cursor.getColumnIndexOrThrow(MmsDatabase.NORMALIZED_DATE_RECEIVED));
|
|
|
|
long box = cursor.getLong(cursor.getColumnIndexOrThrow(MmsDatabase.MESSAGE_BOX));
|
|
|
|
long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(MmsDatabase.THREAD_ID));
|
|
|
|
String address = cursor.getString(cursor.getColumnIndexOrThrow(MmsDatabase.ADDRESS));
|
|
|
|
int addressDeviceId = cursor.getInt(cursor.getColumnIndexOrThrow(MmsDatabase.ADDRESS_DEVICE_ID));
|
|
|
|
int deliveryReceiptCount = cursor.getInt(cursor.getColumnIndexOrThrow(MmsDatabase.DELIVERY_RECEIPT_COUNT));
|
|
|
|
int readReceiptCount = cursor.getInt(cursor.getColumnIndexOrThrow(MmsDatabase.READ_RECEIPT_COUNT));
|
|
|
|
DisplayRecord.Body body = getBody(cursor);
|
|
|
|
int partCount = cursor.getInt(cursor.getColumnIndexOrThrow(MmsDatabase.PART_COUNT));
|
|
|
|
String mismatchDocument = cursor.getString(cursor.getColumnIndexOrThrow(MmsDatabase.MISMATCHED_IDENTITIES));
|
|
|
|
String networkDocument = cursor.getString(cursor.getColumnIndexOrThrow(MmsDatabase.NETWORK_FAILURE));
|
|
|
|
int subscriptionId = cursor.getInt(cursor.getColumnIndexOrThrow(MmsDatabase.SUBSCRIPTION_ID));
|
|
|
|
long expiresIn = cursor.getLong(cursor.getColumnIndexOrThrow(MmsDatabase.EXPIRES_IN));
|
|
|
|
long expireStarted = cursor.getLong(cursor.getColumnIndexOrThrow(MmsDatabase.EXPIRE_STARTED));
|
|
|
|
|
|
|
|
if (!TextSecurePreferences.isReadReceiptsEnabled(context)) {
|
|
|
|
readReceiptCount = 0;
|
|
|
|
}
|
2015-01-15 21:35:35 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
Recipient recipient = getRecipientFor(address);
|
2015-01-15 21:35:35 +00:00
|
|
|
List<IdentityKeyMismatch> mismatches = getMismatchedIdentities(mismatchDocument);
|
|
|
|
List<NetworkFailure> networkFailures = getFailures(networkDocument);
|
2015-10-21 22:32:19 +00:00
|
|
|
SlideDeck slideDeck = getSlideDeck(cursor);
|
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-08-01 15:56:00 +00:00
|
|
|
return new MediaMmsMessageRecord(context, id, recipient, recipient,
|
2017-09-16 05:38:53 +00:00
|
|
|
addressDeviceId, dateSent, dateReceived, deliveryReceiptCount,
|
2016-02-06 00:10:33 +00:00
|
|
|
threadId, body, slideDeck, partCount, box, mismatches,
|
2017-09-16 05:38:53 +00:00
|
|
|
networkFailures, subscriptionId, expiresIn, expireStarted,
|
|
|
|
readReceiptCount);
|
2013-04-26 18:23:43 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
private Recipient getRecipientFor(String serialized) {
|
2017-07-26 16:59:15 +00:00
|
|
|
Address address;
|
2013-05-05 19:51:36 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
if (TextUtils.isEmpty(serialized) || "insert-address-token".equals(serialized)) {
|
|
|
|
address = Address.UNKNOWN;
|
|
|
|
} else {
|
|
|
|
address = Address.fromSerialized(serialized);
|
2013-05-05 19:51:36 +00:00
|
|
|
|
|
|
|
}
|
2017-08-22 01:32:38 +00:00
|
|
|
return Recipient.from(context, address, true);
|
2013-05-05 19:51:36 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 21:35:35 +00:00
|
|
|
private List<IdentityKeyMismatch> getMismatchedIdentities(String document) {
|
|
|
|
if (!TextUtils.isEmpty(document)) {
|
|
|
|
try {
|
|
|
|
return JsonUtils.fromJson(document, IdentityKeyMismatchList.class).getList();
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new LinkedList<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<NetworkFailure> getFailures(String document) {
|
|
|
|
if (!TextUtils.isEmpty(document)) {
|
|
|
|
try {
|
|
|
|
return JsonUtils.fromJson(document, NetworkFailureList.class).getList();
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.w(TAG, ioe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new LinkedList<>();
|
|
|
|
}
|
|
|
|
|
2013-04-30 18:14:01 +00:00
|
|
|
private DisplayRecord.Body getBody(Cursor cursor) {
|
2018-01-25 03:17:44 +00:00
|
|
|
String body = cursor.getString(cursor.getColumnIndexOrThrow(MmsDatabase.BODY));
|
|
|
|
return new DisplayRecord.Body(body == null ? "" : body, true);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
2015-10-21 22:32:19 +00:00
|
|
|
private SlideDeck getSlideDeck(@NonNull Cursor cursor) {
|
2018-01-25 03:17:44 +00:00
|
|
|
Attachment attachment = DatabaseFactory.getAttachmentDatabase(context).getAttachment(cursor);
|
2015-10-21 22:32:19 +00:00
|
|
|
return new SlideDeck(context, attachment);
|
2013-05-06 19:22:03 +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 void close() {
|
|
|
|
cursor.close();
|
2011-12-20 18:20:44 +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
|
|
|
|
2015-08-04 20:37:22 +00:00
|
|
|
private long generatePduCompatTimestamp() {
|
|
|
|
final long time = System.currentTimeMillis();
|
|
|
|
return time - (time % 1000);
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|