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;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
2013-01-10 05:06:56 +00:00
|
|
|
import android.util.Log;
|
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
|
|
|
import org.thoughtcrime.securesms.crypto.MasterCipher;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.database.model.ThreadRecord;
|
2012-09-09 23:10:46 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2013-02-09 23:17:55 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
2012-09-09 23:10:46 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
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.util.InvalidMessageException;
|
2012-09-09 23:10:46 +00:00
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public class ThreadDatabase extends Database {
|
|
|
|
|
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";
|
|
|
|
public static final String RECIPIENT_IDS = "recipient_ids";
|
|
|
|
public static final String SNIPPET = "snippet";
|
|
|
|
private static final String SNIPPET_CHARSET = "snippet_cs";
|
|
|
|
public static final String READ = "read";
|
|
|
|
private static final String TYPE = "type";
|
|
|
|
private static final String ERROR = "error";
|
|
|
|
private static final String HAS_ATTACHMENT = "has_attachment";
|
|
|
|
public static final String SNIPPET_TYPE = "snippet_type";
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + ID + " INTEGER PRIMARY KEY, " +
|
|
|
|
DATE + " INTEGER DEFAULT 0, " + MESSAGE_COUNT + " INTEGER DEFAULT 0, " +
|
|
|
|
RECIPIENT_IDS + " TEXT, " + SNIPPET + " TEXT, " + SNIPPET_CHARSET + " INTEGER DEFAULT 0, " +
|
|
|
|
READ + " INTEGER DEFAULT 1, " + TYPE + " INTEGER DEFAULT 0, " + ERROR + " INTEGER DEFAULT 0, " +
|
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
|
|
|
SNIPPET_TYPE + " INTEGER DEFAULT 0, " +
|
2012-09-09 23:10:46 +00:00
|
|
|
HAS_ATTACHMENT + " 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 thread_recipient_ids_index ON " + TABLE_NAME + " (" + RECIPIENT_IDS + ");",
|
|
|
|
};
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public ThreadDatabase(Context context, SQLiteOpenHelper databaseHelper) {
|
|
|
|
super(context, databaseHelper);
|
|
|
|
}
|
|
|
|
|
|
|
|
private long[] getRecipientIds(Recipients recipients) {
|
|
|
|
Set<Long> recipientSet = new HashSet<Long>();
|
|
|
|
List<Recipient> recipientList = recipients.getRecipientsList();
|
|
|
|
|
|
|
|
for (Recipient recipient : recipientList) {
|
2012-10-01 02:56:29 +00:00
|
|
|
// String number = NumberUtil.filterNumber(recipient.getNumber());
|
2011-12-20 18:20:44 +00:00
|
|
|
String number = recipient.getNumber();
|
2012-09-09 23:10:46 +00:00
|
|
|
recipientSet.add(Long.valueOf(DatabaseFactory.getAddressDatabase(context).getCanonicalAddress(number)));
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
long[] recipientArray = new long[recipientSet.size()];
|
|
|
|
int i = 0;
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
for (Long recipientId : recipientSet) {
|
|
|
|
recipientArray[i++] = recipientId;
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
Arrays.sort(recipientArray);
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
return recipientArray;
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private String getRecipientsAsString(long[] recipientIds) {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
for (int i=0;i<recipientIds.length;i++) {
|
|
|
|
if (i != 0) sb.append(' ');
|
|
|
|
sb.append(recipientIds[i]);
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
return sb.toString();
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2013-04-26 01:59:49 +00:00
|
|
|
private long createThreadForRecipients(String recipients, int recipientCount, 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);
|
|
|
|
contentValues.put(RECIPIENT_IDS, recipients);
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (recipientCount > 1)
|
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);
|
|
|
|
}
|
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
private void updateThread(long threadId, long count, String body, long date, long type) {
|
2011-12-20 18:20:44 +00:00
|
|
|
ContentValues contentValues = new ContentValues(3);
|
|
|
|
contentValues.put(DATE, date - date % 1000);
|
|
|
|
contentValues.put(MESSAGE_COUNT, count);
|
|
|
|
contentValues.put(SNIPPET, body);
|
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);
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
db.update(TABLE_NAME, contentValues, ID + " = ?", new String[] {threadId+""});
|
|
|
|
notifyConversationListListeners();
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void deleteThread(long threadId) {
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
2012-09-09 23:10:46 +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);
|
|
|
|
|
|
|
|
if (cursor != null && cursor.getCount() > length) {
|
|
|
|
Log.w("ThreadDatabase", "Cursor count is greater than length!");
|
|
|
|
cursor.moveToPosition(cursor.getCount() - length);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
update(threadId);
|
|
|
|
notifyConversationListeners(threadId);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void setRead(long threadId) {
|
|
|
|
ContentValues contentValues = new ContentValues(1);
|
|
|
|
contentValues.put(READ, 1);
|
|
|
|
|
|
|
|
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
|
|
|
db.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {threadId+""});
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
DatabaseFactory.getSmsDatabase(context).setMessagesRead(threadId);
|
|
|
|
DatabaseFactory.getMmsDatabase(context).setMessagesRead(threadId);
|
|
|
|
notifyConversationListListeners();
|
|
|
|
}
|
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();
|
|
|
|
db.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {threadId+""});
|
|
|
|
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();
|
|
|
|
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
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public Cursor getFilteredConversationList(List<String> filter) {
|
|
|
|
if (filter == null || filter.size() == 0)
|
|
|
|
return null;
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
List<Long> recipientIds = DatabaseFactory.getAddressDatabase(context).getCanonicalAddresses(filter);
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (recipientIds == null || recipientIds.size() == 0)
|
|
|
|
return null;
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
String selection = RECIPIENT_IDS + " = ?";
|
|
|
|
String[] selectionArgs = new String[recipientIds.size()];
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
for (int i=0;i<recipientIds.size()-1;i++)
|
|
|
|
selection += (" OR " + RECIPIENT_IDS + " = ?");
|
2012-09-09 23:10:46 +00:00
|
|
|
|
|
|
|
int i= 0;
|
2011-12-20 18:20:44 +00:00
|
|
|
for (long id : recipientIds) {
|
|
|
|
selectionArgs[i++] = id+"";
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
|
|
|
Cursor cursor = db.query(TABLE_NAME, null, selection, selectionArgs, null, null, DATE + " DESC");
|
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() {
|
2011-12-20 18:20:44 +00:00
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
|
|
|
Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, DATE + " DESC");
|
|
|
|
setNotifyConverationListListeners(cursor);
|
|
|
|
return cursor;
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void deleteConversation(long threadId) {
|
|
|
|
DatabaseFactory.getSmsDatabase(context).deleteThread(threadId);
|
|
|
|
DatabaseFactory.getMmsDatabase(context).deleteThread(threadId);
|
|
|
|
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);
|
|
|
|
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();
|
2012-09-09 23:10:46 +00:00
|
|
|
deleteAllThreads();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public long getThreadIdIfExistsFor(Recipients recipients) {
|
|
|
|
long[] recipientIds = getRecipientIds(recipients);
|
|
|
|
String recipientsList = getRecipientsAsString(recipientIds);
|
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
|
|
|
String where = RECIPIENT_IDS + " = ?";
|
|
|
|
String[] recipientsArg = new String[] {recipientsList};
|
|
|
|
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
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public long getThreadIdFor(Recipients recipients) {
|
2013-04-26 01:59:49 +00:00
|
|
|
return getThreadIdFor(recipients, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getThreadIdFor(Recipients recipients, int distributionType) {
|
2011-12-20 18:20:44 +00:00
|
|
|
long[] recipientIds = getRecipientIds(recipients);
|
|
|
|
String recipientsList = getRecipientsAsString(recipientIds);
|
|
|
|
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
|
|
|
String where = RECIPIENT_IDS + " = ?";
|
|
|
|
String[] recipientsArg = new String[] {recipientsList};
|
|
|
|
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
|
2013-04-26 01:59:49 +00:00
|
|
|
return createThreadForRecipients(recipientsList, recipientIds.length, distributionType);
|
2011-12-20 18:20:44 +00:00
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
2012-10-01 02:56:29 +00:00
|
|
|
cursor.close();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2013-02-09 23:17:55 +00:00
|
|
|
public Recipients getRecipientsForThreadId(Context context, long threadId) {
|
|
|
|
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()) {
|
|
|
|
String recipientIds = cursor.getString(cursor.getColumnIndexOrThrow(RECIPIENT_IDS));
|
|
|
|
return RecipientFactory.getRecipientsForIds(context, recipientIds, false);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void update(long threadId) {
|
|
|
|
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();
|
|
|
|
return;
|
|
|
|
}
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
Cursor cursor = null;
|
2012-09-09 23:10:46 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
|
|
|
cursor = mmsSmsDatabase.getConversationSnippet(threadId);
|
2012-10-01 02:56:29 +00:00
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
updateThread(threadId, count,
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(SmsDatabase.BODY)),
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
cursor.getLong(cursor.getColumnIndexOrThrow(MmsSmsColumns.NORMALIZED_DATE_RECEIVED)),
|
|
|
|
cursor.getLong(cursor.getColumnIndexOrThrow(SmsDatabase.TYPE)));
|
2012-10-01 02:56:29 +00:00
|
|
|
} else {
|
|
|
|
deleteThread(threadId);
|
|
|
|
}
|
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
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
notifyConversationListListeners();
|
|
|
|
}
|
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
|
|
|
|
|
|
|
public Reader readerFor(Cursor cursor, MasterSecret masterSecret) {
|
|
|
|
return new Reader(cursor, masterSecret);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
public class Reader {
|
|
|
|
|
|
|
|
private final Cursor cursor;
|
|
|
|
private final MasterSecret masterSecret;
|
|
|
|
|
|
|
|
public Reader(Cursor cursor, MasterSecret masterSecret) {
|
|
|
|
this.cursor = cursor;
|
|
|
|
this.masterSecret = masterSecret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ThreadRecord getNext() {
|
|
|
|
if (cursor == null || !cursor.moveToNext())
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return getCurrent();
|
|
|
|
}
|
|
|
|
|
|
|
|
public ThreadRecord getCurrent() {
|
|
|
|
long threadId = cursor.getLong(cursor.getColumnIndexOrThrow(ThreadDatabase.ID));
|
|
|
|
String recipientId = cursor.getString(cursor.getColumnIndexOrThrow(ThreadDatabase.RECIPIENT_IDS));
|
|
|
|
Recipients recipients = RecipientFactory.getRecipientsForIds(context, recipientId, true);
|
|
|
|
|
|
|
|
String 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));
|
2013-04-26 01:59:49 +00:00
|
|
|
int distributionType = cursor.getInt(cursor.getColumnIndexOrThrow(ThreadDatabase.TYPE));
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
|
2013-04-26 01:59:49 +00:00
|
|
|
return new ThreadRecord(context, body, recipients, date, count,
|
|
|
|
read == 1, threadId, type, distributionType);
|
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 String getPlaintextBody(Cursor cursor) {
|
|
|
|
long type = cursor.getLong(cursor.getColumnIndexOrThrow(ThreadDatabase.SNIPPET_TYPE));
|
|
|
|
String ciphertextBody = cursor.getString(cursor.getColumnIndexOrThrow(SNIPPET));
|
|
|
|
|
|
|
|
if (masterSecret == null)
|
|
|
|
return ciphertextBody;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (MmsSmsColumns.Types.isSymmetricEncryption(type)) {
|
|
|
|
MasterCipher masterCipher = new MasterCipher(masterSecret);
|
|
|
|
return masterCipher.decryptBody(ciphertextBody);
|
|
|
|
} else {
|
|
|
|
return ciphertextBody;
|
|
|
|
}
|
|
|
|
} catch (InvalidMessageException e) {
|
|
|
|
Log.w("ThreadDatabase", e);
|
|
|
|
return "Error decrypting message.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String getBody(Cursor cursor) {
|
|
|
|
return cursor.getString(cursor.getColumnIndexOrThrow(SmsDatabase.BODY));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void close() {
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|