2012-09-09 23:10:46 +00:00
|
|
|
/**
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-09-09 23:10:46 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2012-09-09 23:10:46 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms.database;
|
|
|
|
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
2015-04-02 17:02:19 +00:00
|
|
|
import android.database.MergeCursor;
|
2011-12-20 18:20:44 +00:00
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
2015-10-16 20:59:40 +00:00
|
|
|
import android.net.Uri;
|
2015-04-13 15:57:20 +00:00
|
|
|
import android.support.annotation.Nullable;
|
2014-11-12 19:15:05 +00:00
|
|
|
import android.text.TextUtils;
|
2013-01-10 05:06:56 +00:00
|
|
|
import android.util.Log;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2017-08-07 04:43:11 +00:00
|
|
|
import com.annimon.stream.Stream;
|
|
|
|
|
2014-12-17 19:21:04 +00:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2014-11-12 19:15:05 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterCipher;
|
2017-08-07 23:47:38 +00:00
|
|
|
import org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord;
|
2016-10-10 18:13:37 +00:00
|
|
|
import org.thoughtcrime.securesms.database.MessagingDatabase.MarkedMessageInfo;
|
2017-08-07 04:43:11 +00:00
|
|
|
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.RecipientsPreferences;
|
2013-04-30 18:14:01 +00:00
|
|
|
import org.thoughtcrime.securesms.database.model.DisplayRecord;
|
2015-10-16 20:59:40 +00:00
|
|
|
import org.thoughtcrime.securesms.database.model.MediaMmsMessageRecord;
|
2013-04-26 18:23:43 +00:00
|
|
|
import org.thoughtcrime.securesms.database.model.MessageRecord;
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
import org.thoughtcrime.securesms.database.model.ThreadRecord;
|
2015-10-16 20:59:40 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.Slide;
|
|
|
|
import org.thoughtcrime.securesms.mms.SlideDeck;
|
2017-08-01 15:56:00 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2017-08-02 18:04:10 +00:00
|
|
|
import org.thoughtcrime.securesms.util.DelimiterUtil;
|
2015-04-02 17:02:19 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2016-03-23 17:34:41 +00:00
|
|
|
import org.whispersystems.libsignal.InvalidMessageException;
|
2017-08-19 00:28:56 +00:00
|
|
|
import org.whispersystems.libsignal.util.Pair;
|
2017-08-07 23:47:38 +00:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2015-04-02 17:02:19 +00:00
|
|
|
import java.util.LinkedList;
|
2012-09-09 23:10:46 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public class ThreadDatabase extends Database {
|
|
|
|
|
2015-10-16 20:59:40 +00:00
|
|
|
private static final String TAG = ThreadDatabase.class.getSimpleName();
|
|
|
|
|
2013-04-26 01:59:49 +00:00
|
|
|
static final String TABLE_NAME = "thread";
|
|
|
|
public static final String ID = "_id";
|
|
|
|
public static final String DATE = "date";
|
|
|
|
public static final String MESSAGE_COUNT = "message_count";
|
2017-08-01 15:56:00 +00:00
|
|
|
public static final String ADDRESS = "recipient_ids";
|
2013-04-26 01:59:49 +00:00
|
|
|
public static final String SNIPPET = "snippet";
|
|
|
|
private static final String SNIPPET_CHARSET = "snippet_cs";
|
|
|
|
public static final String READ = "read";
|
2015-11-23 23:07:41 +00:00
|
|
|
public static final String TYPE = "type";
|
2013-04-26 01:59:49 +00:00
|
|
|
private static final String ERROR = "error";
|
|
|
|
public static final String SNIPPET_TYPE = "snippet_type";
|
2015-11-23 23:07:41 +00:00
|
|
|
public static final String SNIPPET_URI = "snippet_uri";
|
|
|
|
public static final String ARCHIVED = "archived";
|
2015-11-24 15:06:41 +00:00
|
|
|
public static final String STATUS = "status";
|
|
|
|
public static final String RECEIPT_COUNT = "delivery_receipt_count";
|
2016-09-02 03:43:59 +00:00
|
|
|
public static final String EXPIRES_IN = "expires_in";
|
2017-02-14 06:35:47 +00:00
|
|
|
public static final String LAST_SEEN = "last_seen";
|
2017-08-19 00:28:56 +00:00
|
|
|
private static final String HAS_SENT = "has_sent";
|
2015-11-24 15:06:41 +00:00
|
|
|
|
|
|
|
public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" +
|
|
|
|
ID + " INTEGER PRIMARY KEY, " + DATE + " INTEGER DEFAULT 0, " +
|
2017-08-01 15:56:00 +00:00
|
|
|
MESSAGE_COUNT + " INTEGER DEFAULT 0, " + ADDRESS + " TEXT, " + SNIPPET + " TEXT, " +
|
2015-11-24 15:06:41 +00:00
|
|
|
SNIPPET_CHARSET + " INTEGER DEFAULT 0, " + READ + " INTEGER DEFAULT 1, " +
|
|
|
|
TYPE + " INTEGER DEFAULT 0, " + ERROR + " INTEGER DEFAULT 0, " +
|
|
|
|
SNIPPET_TYPE + " INTEGER DEFAULT 0, " + SNIPPET_URI + " TEXT DEFAULT NULL, " +
|
|
|
|
ARCHIVED + " INTEGER DEFAULT 0, " + STATUS + " INTEGER DEFAULT 0, " +
|
2017-02-14 06:35:47 +00:00
|
|
|
RECEIPT_COUNT + " INTEGER DEFAULT 0, " + EXPIRES_IN + " INTEGER DEFAULT 0, " +
|
2017-08-19 00:28:56 +00:00
|
|
|
LAST_SEEN + " INTEGER DEFAULT 0, " + HAS_SENT + " 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 = {
|
2017-08-01 15:56:00 +00:00
|
|
|
"CREATE INDEX IF NOT EXISTS thread_recipient_ids_index ON " + TABLE_NAME + " (" + ADDRESS + ");",
|
2017-05-20 01:01:40 +00:00
|
|
|
"CREATE INDEX IF NOT EXISTS archived_count_index ON " + TABLE_NAME + " (" + ARCHIVED + ", " + MESSAGE_COUNT + ");",
|
2012-10-30 00:41:06 +00:00
|
|
|
};
|
|
|
|
|
2017-08-07 04:43:11 +00:00
|
|
|
private static final String[] THREAD_PROJECTION = {
|
|
|
|
ID, DATE, MESSAGE_COUNT, ADDRESS, SNIPPET, SNIPPET_CHARSET, READ, TYPE, ERROR, SNIPPET_TYPE,
|
|
|
|
SNIPPET_URI, ARCHIVED, STATUS, RECEIPT_COUNT, EXPIRES_IN, LAST_SEEN
|
|
|
|
};
|
|
|
|
|
|
|
|
private static final List<String> TYPED_THREAD_PROJECTION = Stream.of(THREAD_PROJECTION)
|
|
|
|
.map(columnName -> TABLE_NAME + "." + columnName)
|
|
|
|
.toList();
|
|
|
|
|
2017-08-07 23:47:38 +00:00
|
|
|
private static final List<String> COMBINED_THREAD_RECIPIENT_GROUP_PROJECTION = Stream.concat(Stream.concat(Stream.of(TYPED_THREAD_PROJECTION),
|
|
|
|
Stream.of(RecipientPreferenceDatabase.TYPED_RECIPIENT_PROJECTION)),
|
|
|
|
Stream.of(GroupDatabase.TYPED_GROUP_PROJECTION))
|
|
|
|
.toList();
|
2017-08-07 04:43:11 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public ThreadDatabase(Context context, SQLiteOpenHelper databaseHelper) {
|
|
|
|
super(context, databaseHelper);
|
|
|
|
}
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
private long createThreadForRecipient(Address address, boolean group, int distributionType) {
|
2011-12-20 18:20:44 +00:00
|
|
|
ContentValues contentValues = new ContentValues(4);
|
|
|
|
long date = System.currentTimeMillis();
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
contentValues.put(DATE, date - date % 1000);
|
2017-08-01 15:56:00 +00:00
|
|
|
contentValues.put(ADDRESS, address.serialize());
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
if (group)
|
2013-04-26 01:59:49 +00:00
|
|
|
contentValues.put(TYPE, distributionType);
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
contentValues.put(MESSAGE_COUNT, 0);
|
|
|
|
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
return db.insert(TABLE_NAME, null, contentValues);
|
|
|
|
}
|
|
|
|
|
2015-11-24 15:06:41 +00:00
|
|
|
private void updateThread(long threadId, long count, String body, @Nullable Uri attachment,
|
2016-08-16 03:23:56 +00:00
|
|
|
long date, int status, int receiptCount, long type, boolean unarchive,
|
|
|
|
long expiresIn)
|
2014-02-14 23:59:57 +00:00
|
|
|
{
|
2015-11-24 15:06:41 +00:00
|
|
|
ContentValues contentValues = new ContentValues(7);
|
2011-12-20 18:20:44 +00:00
|
|
|
contentValues.put(DATE, date - date % 1000);
|
|
|
|
contentValues.put(MESSAGE_COUNT, count);
|
|
|
|
contentValues.put(SNIPPET, body);
|
2015-10-16 20:59:40 +00:00
|
|
|
contentValues.put(SNIPPET_URI, attachment == null ? null : attachment.toString());
|
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(SNIPPET_TYPE, type);
|
2015-11-24 15:06:41 +00:00
|
|
|
contentValues.put(STATUS, status);
|
|
|
|
contentValues.put(RECEIPT_COUNT, receiptCount);
|
2016-08-16 03:23:56 +00:00
|
|
|
contentValues.put(EXPIRES_IN, expiresIn);
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2015-11-23 23:07:41 +00:00
|
|
|
if (unarchive) {
|
|
|
|
contentValues.put(ARCHIVED, 0);
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
2013-04-26 18:23:43 +00:00
|
|
|
db.update(TABLE_NAME, contentValues, ID + " = ?", new String[] {threadId + ""});
|
2011-12-20 18:20:44 +00:00
|
|
|
notifyConversationListListeners();
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2015-11-23 23:07:41 +00:00
|
|
|
public void updateSnippet(long threadId, String snippet, @Nullable Uri attachment, long date, long type, boolean unarchive) {
|
2015-11-24 15:06:41 +00:00
|
|
|
ContentValues contentValues = new ContentValues(4);
|
2015-04-06 20:44:18 +00:00
|
|
|
|
|
|
|
contentValues.put(DATE, date - date % 1000);
|
2014-12-12 01:13:01 +00:00
|
|
|
contentValues.put(SNIPPET, snippet);
|
|
|
|
contentValues.put(SNIPPET_TYPE, type);
|
2015-10-16 20:59:40 +00:00
|
|
|
contentValues.put(SNIPPET_URI, attachment == null ? null : attachment.toString());
|
2015-11-23 23:07:41 +00:00
|
|
|
|
|
|
|
if (unarchive) {
|
|
|
|
contentValues.put(ARCHIVED, 0);
|
|
|
|
}
|
|
|
|
|
2014-12-12 01:13:01 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
db.update(TABLE_NAME, contentValues, ID + " = ?", new String[] {threadId + ""});
|
|
|
|
notifyConversationListListeners();
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void deleteThread(long threadId) {
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
2015-10-16 20:59:40 +00:00
|
|
|
db.delete(TABLE_NAME, ID_WHERE, new String[] {threadId + ""});
|
2011-12-20 18:20:44 +00:00
|
|
|
notifyConversationListListeners();
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void deleteThreads(Set<Long> threadIds) {
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
String where = "";
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
for (long threadId : threadIds) {
|
|
|
|
where += ID + " = '" + threadId + "' OR ";
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
where = where.substring(0, where.length() - 4);
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
db.delete(TABLE_NAME, where, null);
|
|
|
|
notifyConversationListListeners();
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void deleteAllThreads() {
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
2012-09-09 23:10:46 +00:00
|
|
|
db.delete(TABLE_NAME, null, null);
|
|
|
|
notifyConversationListListeners();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2013-01-10 05:06:56 +00:00
|
|
|
public void trimAllThreads(int length, ProgressListener listener) {
|
|
|
|
Cursor cursor = null;
|
|
|
|
int threadCount = 0;
|
|
|
|
int complete = 0;
|
|
|
|
|
|
|
|
try {
|
|
|
|
cursor = this.getConversationList();
|
|
|
|
|
|
|
|
if (cursor != null)
|
|
|
|
threadCount = cursor.getCount();
|
|
|
|
|
|
|
|
while (cursor != null && cursor.moveToNext()) {
|
|
|
|
long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(ID));
|
|
|
|
trimThread(threadId, length);
|
|
|
|
|
|
|
|
listener.onProgress(++complete, threadCount);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void trimThread(long threadId, int length) {
|
|
|
|
Log.w("ThreadDatabase", "Trimming thread: " + threadId + " to: " + length);
|
|
|
|
Cursor cursor = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
cursor = DatabaseFactory.getMmsSmsDatabase(context).getConversation(threadId);
|
|
|
|
|
2015-09-28 23:39:39 +00:00
|
|
|
if (cursor != null && length > 0 && cursor.getCount() > length) {
|
2013-01-10 05:06:56 +00:00
|
|
|
Log.w("ThreadDatabase", "Cursor count is greater than length!");
|
2015-09-28 23:39:39 +00:00
|
|
|
cursor.moveToPosition(length - 1);
|
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
|
|
|
long lastTweetDate = cursor.getLong(cursor.getColumnIndexOrThrow(MmsSmsColumns.NORMALIZED_DATE_RECEIVED));
|
2013-01-10 05:06:56 +00:00
|
|
|
|
|
|
|
Log.w("ThreadDatabase", "Cut off tweet date: " + lastTweetDate);
|
|
|
|
|
|
|
|
DatabaseFactory.getSmsDatabase(context).deleteMessagesInThreadBeforeDate(threadId, lastTweetDate);
|
|
|
|
DatabaseFactory.getMmsDatabase(context).deleteMessagesInThreadBeforeDate(threadId, lastTweetDate);
|
|
|
|
|
2015-11-23 23:07:41 +00:00
|
|
|
update(threadId, false);
|
2013-01-10 05:06:56 +00:00
|
|
|
notifyConversationListeners(threadId);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-06 20:59:40 +00:00
|
|
|
public void setAllThreadsRead() {
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
ContentValues contentValues = new ContentValues(1);
|
|
|
|
contentValues.put(READ, 1);
|
|
|
|
|
|
|
|
db.update(TABLE_NAME, contentValues, null, null);
|
|
|
|
|
|
|
|
DatabaseFactory.getSmsDatabase(context).setAllMessagesRead();
|
|
|
|
DatabaseFactory.getMmsDatabase(context).setAllMessagesRead();
|
|
|
|
notifyConversationListListeners();
|
|
|
|
}
|
|
|
|
|
2017-02-22 23:05:35 +00:00
|
|
|
public List<MarkedMessageInfo> setRead(long threadId, boolean lastSeen) {
|
2011-12-20 18:20:44 +00:00
|
|
|
ContentValues contentValues = new ContentValues(1);
|
|
|
|
contentValues.put(READ, 1);
|
|
|
|
|
2017-02-22 23:05:35 +00:00
|
|
|
if (lastSeen) {
|
|
|
|
contentValues.put(LAST_SEEN, System.currentTimeMillis());
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
db.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {threadId+""});
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2016-10-10 18:13:37 +00:00
|
|
|
final List<MarkedMessageInfo> smsRecords = DatabaseFactory.getSmsDatabase(context).setMessagesRead(threadId);
|
|
|
|
final List<MarkedMessageInfo> mmsRecords = DatabaseFactory.getMmsDatabase(context).setMessagesRead(threadId);
|
2016-02-20 01:07:41 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
notifyConversationListListeners();
|
2016-02-20 01:07:41 +00:00
|
|
|
|
2016-10-10 18:13:37 +00:00
|
|
|
return new LinkedList<MarkedMessageInfo>() {{
|
2016-02-20 01:07:41 +00:00
|
|
|
addAll(smsRecords);
|
|
|
|
addAll(mmsRecords);
|
|
|
|
}};
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void setUnread(long threadId) {
|
|
|
|
ContentValues contentValues = new ContentValues(1);
|
2013-04-26 01:59:49 +00:00
|
|
|
contentValues.put(READ, 0);
|
|
|
|
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
2015-10-16 20:59:40 +00:00
|
|
|
db.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {threadId + ""});
|
2013-04-26 01:59:49 +00:00
|
|
|
notifyConversationListListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setDistributionType(long threadId, int distributionType) {
|
|
|
|
ContentValues contentValues = new ContentValues(1);
|
|
|
|
contentValues.put(TYPE, distributionType);
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
2015-10-16 20:59:40 +00:00
|
|
|
db.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {threadId + ""});
|
2012-09-09 23:10:46 +00:00
|
|
|
notifyConversationListListeners();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
public int getDistributionType(long threadId) {
|
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
|
|
|
Cursor cursor = db.query(TABLE_NAME, new String[]{TYPE}, ID_WHERE, new String[]{String.valueOf(threadId)}, null, null, null);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (cursor != null && cursor.moveToNext()) {
|
|
|
|
return cursor.getInt(cursor.getColumnIndexOrThrow(TYPE));
|
|
|
|
}
|
|
|
|
|
|
|
|
return DistributionTypes.DEFAULT;
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) cursor.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
public Cursor getFilteredConversationList(List<Address> filter) {
|
2011-12-20 18:20:44 +00:00
|
|
|
if (filter == null || filter.size() == 0)
|
|
|
|
return null;
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
|
|
|
List<List<Address>> partitionedAddresses = Util.partition(filter, 900);
|
|
|
|
List<Cursor> cursors = new LinkedList<>();
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
for (List<Address> addresses : partitionedAddresses) {
|
2017-08-01 15:56:00 +00:00
|
|
|
String selection = ADDRESS + " = ?";
|
2017-07-26 16:59:15 +00:00
|
|
|
String[] selectionArgs = new String[addresses.size()];
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
for (int i=0;i<addresses.size()-1;i++)
|
2017-08-01 15:56:00 +00:00
|
|
|
selection += (" OR " + ADDRESS + " = ?");
|
2015-04-02 17:02:19 +00:00
|
|
|
|
|
|
|
int i= 0;
|
2017-07-26 16:59:15 +00:00
|
|
|
for (Address address : addresses) {
|
2017-08-02 18:04:10 +00:00
|
|
|
selectionArgs[i++] = DelimiterUtil.escape(address.serialize(), ' ');
|
2015-04-02 17:02:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cursors.add(db.query(TABLE_NAME, null, selection, selectionArgs, null, null, DATE + " DESC"));
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2015-04-02 17:02:19 +00:00
|
|
|
Cursor cursor = cursors.size() > 1 ? new MergeCursor(cursors.toArray(new Cursor[cursors.size()])) : cursors.get(0);
|
2012-09-09 23:10:46 +00:00
|
|
|
setNotifyConverationListListeners(cursor);
|
2011-12-20 18:20:44 +00:00
|
|
|
return cursor;
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
|
|
|
public Cursor getConversationList() {
|
2017-08-07 04:43:11 +00:00
|
|
|
return getConversationList("0");
|
2015-11-23 23:07:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Cursor getArchivedConversationList() {
|
2017-08-07 04:43:11 +00:00
|
|
|
return getConversationList("1");
|
|
|
|
}
|
|
|
|
|
|
|
|
private Cursor getConversationList(String archived) {
|
2017-08-07 23:47:38 +00:00
|
|
|
String projection = Util.join(COMBINED_THREAD_RECIPIENT_GROUP_PROJECTION, ",");
|
2017-08-07 04:43:11 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
|
|
|
Cursor cursor = db.rawQuery("SELECT " + projection + " FROM " + TABLE_NAME +
|
|
|
|
" LEFT OUTER JOIN " + RecipientPreferenceDatabase.TABLE_NAME +
|
2017-08-07 23:47:38 +00:00
|
|
|
" ON " + TABLE_NAME + "." + ADDRESS + " = " + RecipientPreferenceDatabase.TABLE_NAME + "." + RecipientPreferenceDatabase.ADDRESS +
|
|
|
|
" LEFT OUTER JOIN " + GroupDatabase.TABLE_NAME +
|
|
|
|
" ON " + TABLE_NAME + "." + ADDRESS + " = " + GroupDatabase.TABLE_NAME + "." + GroupDatabase.GROUP_ID +
|
2017-08-07 04:43:11 +00:00
|
|
|
" WHERE " + ARCHIVED + " = ? AND " + MESSAGE_COUNT + " != 0" +
|
|
|
|
" ORDER BY " + TABLE_NAME + "." + DATE + " DESC",
|
|
|
|
new String[] {archived});
|
2015-11-23 23:07:41 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
setNotifyConverationListListeners(cursor);
|
2015-11-23 23:07:41 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
return cursor;
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2017-01-12 03:54:19 +00:00
|
|
|
public Cursor getDirectShareList() {
|
2017-08-07 04:43:11 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
2017-08-07 23:47:38 +00:00
|
|
|
String projection = Util.join(COMBINED_THREAD_RECIPIENT_GROUP_PROJECTION, ",");
|
2017-08-07 04:43:11 +00:00
|
|
|
|
|
|
|
return db.rawQuery("SELECT " + projection + " FROM " + TABLE_NAME +
|
|
|
|
" LEFT OUTER JOIN " + RecipientPreferenceDatabase.TABLE_NAME +
|
|
|
|
" ON " + TABLE_NAME + "." + ADDRESS + " = " + RecipientPreferenceDatabase.TABLE_NAME + "." + ADDRESS +
|
|
|
|
" ORDER BY " + TABLE_NAME + "." + DATE + " DESC",
|
|
|
|
null);
|
2017-01-12 03:54:19 +00:00
|
|
|
}
|
|
|
|
|
2015-11-23 23:07:41 +00:00
|
|
|
public int getArchivedConversationListCount() {
|
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
|
|
|
Cursor cursor = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
cursor = db.query(TABLE_NAME, new String[] {"COUNT(*)"}, ARCHIVED + " = ?",
|
|
|
|
new String[] {"1"}, null, null, null);
|
|
|
|
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
return cursor.getInt(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) cursor.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void archiveConversation(long threadId) {
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
ContentValues contentValues = new ContentValues(1);
|
|
|
|
contentValues.put(ARCHIVED, 1);
|
|
|
|
|
|
|
|
db.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {threadId + ""});
|
|
|
|
notifyConversationListListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void unarchiveConversation(long threadId) {
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
ContentValues contentValues = new ContentValues(1);
|
|
|
|
contentValues.put(ARCHIVED, 0);
|
|
|
|
|
|
|
|
db.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {threadId + ""});
|
|
|
|
notifyConversationListListeners();
|
|
|
|
}
|
|
|
|
|
2017-02-14 06:35:47 +00:00
|
|
|
public void setLastSeen(long threadId) {
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
ContentValues contentValues = new ContentValues(1);
|
|
|
|
contentValues.put(LAST_SEEN, System.currentTimeMillis());
|
|
|
|
|
|
|
|
db.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {String.valueOf(threadId)});
|
|
|
|
notifyConversationListListeners();
|
|
|
|
}
|
|
|
|
|
2017-08-19 00:28:56 +00:00
|
|
|
public Pair<Long, Boolean> getLastSeenAndHasSent(long threadId) {
|
2017-02-14 06:35:47 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
2017-08-19 00:28:56 +00:00
|
|
|
Cursor cursor = db.query(TABLE_NAME, new String[]{LAST_SEEN, HAS_SENT}, ID_WHERE, new String[]{String.valueOf(threadId)}, null, null, null);
|
2017-02-14 06:35:47 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
2017-08-19 00:28:56 +00:00
|
|
|
return new Pair<>(cursor.getLong(0), cursor.getLong(1) == 1);
|
2017-02-14 06:35:47 +00:00
|
|
|
}
|
|
|
|
|
2017-08-19 00:28:56 +00:00
|
|
|
return new Pair<>(-1L, false);
|
2017-02-14 06:35:47 +00:00
|
|
|
} finally {
|
|
|
|
if (cursor != null) cursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void deleteConversation(long threadId) {
|
|
|
|
DatabaseFactory.getSmsDatabase(context).deleteThread(threadId);
|
|
|
|
DatabaseFactory.getMmsDatabase(context).deleteThread(threadId);
|
2015-01-03 23:25:35 +00:00
|
|
|
DatabaseFactory.getDraftDatabase(context).clearDrafts(threadId);
|
2011-12-20 18:20:44 +00:00
|
|
|
deleteThread(threadId);
|
|
|
|
notifyConversationListeners(threadId);
|
|
|
|
notifyConversationListListeners();
|
2012-09-09 23:10:46 +00:00
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void deleteConversations(Set<Long> selectedConversations) {
|
|
|
|
DatabaseFactory.getSmsDatabase(context).deleteThreads(selectedConversations);
|
|
|
|
DatabaseFactory.getMmsDatabase(context).deleteThreads(selectedConversations);
|
2015-01-03 23:25:35 +00:00
|
|
|
DatabaseFactory.getDraftDatabase(context).clearDrafts(selectedConversations);
|
2011-12-20 18:20:44 +00:00
|
|
|
deleteThreads(selectedConversations);
|
|
|
|
notifyConversationListeners(selectedConversations);
|
|
|
|
notifyConversationListListeners();
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void deleteAllConversations() {
|
|
|
|
DatabaseFactory.getSmsDatabase(context).deleteAllThreads();
|
|
|
|
DatabaseFactory.getMmsDatabase(context).deleteAllThreads();
|
2015-01-03 23:25:35 +00:00
|
|
|
DatabaseFactory.getDraftDatabase(context).clearAllDrafts();
|
2012-09-09 23:10:46 +00:00
|
|
|
deleteAllThreads();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
public long getThreadIdIfExistsFor(Recipient recipient) {
|
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
|
|
|
String where = ADDRESS + " = ?";
|
|
|
|
String[] recipientsArg = new String[] {recipient.getAddress().serialize()};
|
|
|
|
Cursor cursor = null;
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
|
|
|
cursor = db.query(TABLE_NAME, new String[]{ID}, where, recipientsArg, null, null, null);
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (cursor != null && cursor.moveToFirst())
|
2012-10-01 02:56:29 +00:00
|
|
|
return cursor.getLong(cursor.getColumnIndexOrThrow(ID));
|
2011-12-20 18:20:44 +00:00
|
|
|
else
|
2012-10-01 02:56:29 +00:00
|
|
|
return -1L;
|
2011-12-20 18:20:44 +00:00
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
2012-10-01 02:56:29 +00:00
|
|
|
cursor.close();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
public long getThreadIdFor(Recipient recipient) {
|
|
|
|
return getThreadIdFor(recipient, DistributionTypes.DEFAULT);
|
2013-04-26 01:59:49 +00:00
|
|
|
}
|
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
public long getThreadIdFor(Recipient recipient, int distributionType) {
|
2017-07-26 16:59:15 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
2017-08-01 15:56:00 +00:00
|
|
|
String where = ADDRESS + " = ?";
|
|
|
|
String[] recipientsArg = new String[]{recipient.getAddress().serialize()};
|
2017-07-26 16:59:15 +00:00
|
|
|
Cursor cursor = null;
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
|
|
|
cursor = db.query(TABLE_NAME, new String[]{ID}, where, recipientsArg, null, null, null);
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
2012-10-01 02:56:29 +00:00
|
|
|
return cursor.getLong(cursor.getColumnIndexOrThrow(ID));
|
2017-07-26 16:59:15 +00:00
|
|
|
} else {
|
2017-08-01 15:56:00 +00:00
|
|
|
return createThreadForRecipient(recipient.getAddress(), recipient.isGroupRecipient(), distributionType);
|
2017-07-26 16:59:15 +00:00
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
2012-10-01 02:56:29 +00:00
|
|
|
cursor.close();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2017-08-01 15:56:00 +00:00
|
|
|
public @Nullable Recipient getRecipientForThreadId(long threadId) {
|
2013-02-09 23:17:55 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
|
|
|
Cursor cursor = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
cursor = db.query(TABLE_NAME, null, ID + " = ?", new String[] {threadId+""}, null, null, null);
|
|
|
|
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
2017-08-01 15:56:00 +00:00
|
|
|
Address address = Address.fromSerialized(cursor.getString(cursor.getColumnIndexOrThrow(ADDRESS)));
|
2017-08-22 01:32:38 +00:00
|
|
|
return Recipient.from(context, address, false);
|
2013-02-09 23:17:55 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-08-19 00:28:56 +00:00
|
|
|
public void setHasSent(long threadId, boolean hasSent) {
|
|
|
|
ContentValues contentValues = new ContentValues(1);
|
|
|
|
contentValues.put(HAS_SENT, hasSent ? 1 : 0);
|
|
|
|
|
|
|
|
databaseHelper.getWritableDatabase().update(TABLE_NAME, contentValues, ID_WHERE,
|
|
|
|
new String[] {String.valueOf(threadId)});
|
|
|
|
|
|
|
|
notifyConversationListeners(threadId);
|
|
|
|
}
|
|
|
|
|
2016-02-20 01:07:41 +00:00
|
|
|
public void updateReadState(long threadId) {
|
|
|
|
int unreadCount = DatabaseFactory.getMmsSmsDatabase(context).getUnreadCount(threadId);
|
|
|
|
|
|
|
|
ContentValues contentValues = new ContentValues();
|
|
|
|
contentValues.put(READ, unreadCount == 0);
|
|
|
|
|
|
|
|
databaseHelper.getWritableDatabase().update(TABLE_NAME, contentValues,ID_WHERE,
|
|
|
|
new String[] {String.valueOf(threadId)});
|
|
|
|
|
|
|
|
notifyConversationListListeners();
|
|
|
|
}
|
|
|
|
|
2015-11-23 23:07:41 +00:00
|
|
|
public boolean update(long threadId, boolean unarchive) {
|
2011-12-20 18:20:44 +00:00
|
|
|
MmsSmsDatabase mmsSmsDatabase = DatabaseFactory.getMmsSmsDatabase(context);
|
|
|
|
long count = mmsSmsDatabase.getConversationCount(threadId);
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (count == 0) {
|
|
|
|
deleteThread(threadId);
|
|
|
|
notifyConversationListListeners();
|
2015-03-31 20:36:04 +00:00
|
|
|
return true;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2013-04-26 18:23:43 +00:00
|
|
|
MmsSmsDatabase.Reader reader = null;
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
2014-06-12 15:59:54 +00:00
|
|
|
reader = mmsSmsDatabase.readerFor(mmsSmsDatabase.getConversationSnippet(threadId));
|
|
|
|
MessageRecord record;
|
2013-04-26 18:23:43 +00:00
|
|
|
|
|
|
|
if (reader != null && (record = reader.getNext()) != null) {
|
2015-11-24 15:06:41 +00:00
|
|
|
updateThread(threadId, count, record.getBody().getBody(), getAttachmentUriFor(record),
|
2015-07-06 22:14:57 +00:00
|
|
|
record.getTimestamp(), record.getDeliveryStatus(), record.getReceiptCount(),
|
2016-08-16 03:23:56 +00:00
|
|
|
record.getType(), unarchive, record.getExpiresIn());
|
2015-03-31 20:36:04 +00:00
|
|
|
notifyConversationListListeners();
|
|
|
|
return false;
|
2012-10-01 02:56:29 +00:00
|
|
|
} else {
|
|
|
|
deleteThread(threadId);
|
2015-03-31 20:36:04 +00:00
|
|
|
notifyConversationListListeners();
|
|
|
|
return true;
|
2012-10-01 02:56:29 +00:00
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
} finally {
|
2013-04-26 18:23:43 +00:00
|
|
|
if (reader != null)
|
|
|
|
reader.close();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-10 05:06:56 +00:00
|
|
|
|
2015-10-16 20:59:40 +00:00
|
|
|
private @Nullable Uri getAttachmentUriFor(MessageRecord record) {
|
2015-11-04 23:39:01 +00:00
|
|
|
if (!record.isMms() || record.isMmsNotification() || record.isGroupAction()) return null;
|
2015-10-16 20:59:40 +00:00
|
|
|
|
|
|
|
SlideDeck slideDeck = ((MediaMmsMessageRecord)record).getSlideDeck();
|
|
|
|
Slide thumbnail = slideDeck.getThumbnailSlide();
|
|
|
|
|
|
|
|
return thumbnail != null ? thumbnail.getThumbnailUri() : null;
|
|
|
|
}
|
|
|
|
|
2013-01-10 05:06:56 +00:00
|
|
|
public static interface ProgressListener {
|
|
|
|
public void onProgress(int complete, int total);
|
|
|
|
}
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
|
2014-03-16 21:36:21 +00:00
|
|
|
public Reader readerFor(Cursor cursor, MasterCipher masterCipher) {
|
|
|
|
return new Reader(cursor, masterCipher);
|
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
|
|
|
}
|
|
|
|
|
2013-04-26 01:59:49 +00:00
|
|
|
public static class DistributionTypes {
|
|
|
|
public static final int DEFAULT = 2;
|
|
|
|
public static final int BROADCAST = 1;
|
|
|
|
public static final int CONVERSATION = 2;
|
2015-11-23 23:07:41 +00:00
|
|
|
public static final int ARCHIVE = 3;
|
2013-04-26 01:59:49 +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 {
|
|
|
|
|
2014-03-16 21:36:21 +00:00
|
|
|
private final Cursor cursor;
|
2013-04-26 18:23:43 +00:00
|
|
|
private final MasterCipher masterCipher;
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
|
2014-03-16 21:36:21 +00:00
|
|
|
public Reader(Cursor cursor, MasterCipher masterCipher) {
|
|
|
|
this.cursor = cursor;
|
|
|
|
this.masterCipher = masterCipher;
|
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 ThreadRecord getNext() {
|
|
|
|
if (cursor == null || !cursor.moveToNext())
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return getCurrent();
|
|
|
|
}
|
|
|
|
|
|
|
|
public ThreadRecord getCurrent() {
|
2017-08-16 04:03:31 +00:00
|
|
|
long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(ThreadDatabase.ID));
|
|
|
|
int distributionType = cursor.getInt(cursor.getColumnIndexOrThrow(ThreadDatabase.TYPE));
|
|
|
|
Address address = Address.fromSerialized(cursor.getString(cursor.getColumnIndexOrThrow(ThreadDatabase.ADDRESS)));
|
|
|
|
|
|
|
|
Optional<RecipientsPreferences> preferences;
|
|
|
|
Optional<GroupRecord> groupRecord;
|
|
|
|
|
|
|
|
if (distributionType != DistributionTypes.ARCHIVE) {
|
|
|
|
preferences = DatabaseFactory.getRecipientPreferenceDatabase(context).getRecipientPreferences(cursor);
|
|
|
|
groupRecord = DatabaseFactory.getGroupDatabase(context).getGroup(cursor);
|
|
|
|
} else {
|
|
|
|
preferences = Optional.absent();
|
|
|
|
groupRecord = Optional.absent();
|
|
|
|
}
|
|
|
|
|
2017-08-22 01:32:38 +00:00
|
|
|
Recipient recipient = Recipient.from(context, address, preferences, groupRecord, true);
|
2017-08-16 04:03:31 +00:00
|
|
|
DisplayRecord.Body body = getPlaintextBody(cursor);
|
|
|
|
long date = cursor.getLong(cursor.getColumnIndexOrThrow(ThreadDatabase.DATE));
|
|
|
|
long count = cursor.getLong(cursor.getColumnIndexOrThrow(ThreadDatabase.MESSAGE_COUNT));
|
|
|
|
long read = cursor.getLong(cursor.getColumnIndexOrThrow(ThreadDatabase.READ));
|
|
|
|
long type = cursor.getLong(cursor.getColumnIndexOrThrow(ThreadDatabase.SNIPPET_TYPE));
|
|
|
|
boolean archived = cursor.getInt(cursor.getColumnIndex(ThreadDatabase.ARCHIVED)) != 0;
|
|
|
|
int status = cursor.getInt(cursor.getColumnIndexOrThrow(ThreadDatabase.STATUS));
|
|
|
|
int receiptCount = cursor.getInt(cursor.getColumnIndexOrThrow(ThreadDatabase.RECEIPT_COUNT));
|
|
|
|
long expiresIn = cursor.getLong(cursor.getColumnIndexOrThrow(ThreadDatabase.EXPIRES_IN));
|
|
|
|
long lastSeen = cursor.getLong(cursor.getColumnIndexOrThrow(ThreadDatabase.LAST_SEEN));
|
|
|
|
Uri snippetUri = getSnippetUri(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 ThreadRecord(context, body, snippetUri, recipient, date, count, read == 1,
|
2016-08-16 03:23:56 +00:00
|
|
|
threadId, receiptCount, status, type, distributionType, archived,
|
2017-02-14 06:35:47 +00:00
|
|
|
expiresIn, lastSeen);
|
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
|
|
|
}
|
|
|
|
|
2013-04-30 18:14:01 +00:00
|
|
|
private DisplayRecord.Body getPlaintextBody(Cursor 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
|
|
|
try {
|
2013-04-30 18:14:01 +00:00
|
|
|
long type = cursor.getLong(cursor.getColumnIndexOrThrow(ThreadDatabase.SNIPPET_TYPE));
|
|
|
|
String body = cursor.getString(cursor.getColumnIndexOrThrow(SNIPPET));
|
|
|
|
|
2014-11-12 19:15:05 +00:00
|
|
|
if (!TextUtils.isEmpty(body) && masterCipher != null && MmsSmsColumns.Types.isSymmetricEncryption(type)) {
|
2013-04-30 18:14:01 +00:00
|
|
|
return new DisplayRecord.Body(masterCipher.decryptBody(body), true);
|
2014-11-12 19:15:05 +00:00
|
|
|
} else if (!TextUtils.isEmpty(body) && masterCipher == null && MmsSmsColumns.Types.isSymmetricEncryption(type)) {
|
2013-04-30 18:14:01 +00:00
|
|
|
return new DisplayRecord.Body(body, false);
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
} else {
|
2013-04-30 18:14:01 +00:00
|
|
|
return new DisplayRecord.Body(body, true);
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
}
|
|
|
|
} catch (InvalidMessageException e) {
|
|
|
|
Log.w("ThreadDatabase", e);
|
2014-12-17 19:21:04 +00:00
|
|
|
return new DisplayRecord.Body(context.getString(R.string.ThreadDatabase_error_decrypting_message), true);
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-16 20:59:40 +00:00
|
|
|
private @Nullable Uri getSnippetUri(Cursor cursor) {
|
|
|
|
if (cursor.isNull(cursor.getColumnIndexOrThrow(ThreadDatabase.SNIPPET_URI))) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return Uri.parse(cursor.getString(cursor.getColumnIndexOrThrow(ThreadDatabase.SNIPPET_URI)));
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|