session-android/src/org/thoughtcrime/securesms/database/SmsDatabase.java

395 lines
16 KiB
Java
Raw Normal View History

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;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
2012-09-09 23:10:46 +00:00
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.sms.TextMessage;
import org.thoughtcrime.securesms.util.Trimmer;
2012-09-09 23:10:46 +00:00
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
2011-12-20 18:20:44 +00:00
/**
* Database for storage of SMS messages.
2012-09-09 23:10:46 +00:00
*
2011-12-20 18:20:44 +00:00
* @author Moxie Marlinspike
*/
public class SmsDatabase extends Database {
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
public static final String TABLE_NAME = "sms";
public static final String ID = "_id";
public static final String THREAD_ID = "thread_id";
public static final String ADDRESS = "address";
public static final String PERSON = "person";
public static final String DATE_RECEIVED = "date";
public static final String DATE_SENT = "date_sent";
2011-12-20 18:20:44 +00:00
public static final String PROTOCOL = "protocol";
public static final String READ = "read";
public static final String STATUS = "status";
public static final String TYPE = "type";
public static final String REPLY_PATH_PRESENT = "reply_path_present";
public static final String SUBJECT = "subject";
public static final String BODY = "body";
public static final String SERVICE_CENTER = "service_center";
2012-09-09 23:10:46 +00:00
public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + ID + " integer PRIMARY KEY, " +
THREAD_ID + " INTEGER, " + ADDRESS + " TEXT, " + PERSON + " INTEGER, " + DATE_RECEIVED + " INTEGER, " +
DATE_SENT + " INTEGER, " + PROTOCOL + " INTEGER, " + READ + " INTEGER DEFAULT 0, " +
STATUS + " INTEGER DEFAULT -1," + TYPE + " INTEGER, " + REPLY_PATH_PRESENT + " INTEGER, " +
SUBJECT + " TEXT, " + BODY + " TEXT, " + SERVICE_CENTER + " TEXT);";
2012-09-09 23:10:46 +00:00
public static final String[] CREATE_INDEXS = {
"CREATE INDEX IF NOT EXISTS sms_thread_id_index ON " + TABLE_NAME + " (" + THREAD_ID + ");",
"CREATE INDEX IF NOT EXISTS sms_read_index ON " + TABLE_NAME + " (" + READ + ");",
"CREATE INDEX IF NOT EXISTS sms_read_and_thread_id_index ON " + TABLE_NAME + "(" + READ + "," + THREAD_ID + ");",
"CREATE INDEX IF NOT EXISTS sms_type_index ON " + TABLE_NAME + " (" + TYPE + ");"
};
2011-12-20 18:20:44 +00:00
public SmsDatabase(Context context, SQLiteOpenHelper databaseHelper) {
super(context, databaseHelper);
}
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
private void updateType(long id, long type) {
Log.w("MessageDatabase", "Updating ID: " + id + " to type: " + type);
ContentValues contentValues = new ContentValues();
contentValues.put(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_WHERE, new String[] {id+""});
notifyConversationListeners(getThreadIdForMessage(id));
}
2012-09-09 23:10:46 +00:00
private long insertMessageReceived(TextMessage message, String body, long type, long timeSent) {
2012-09-09 23:10:46 +00:00
List<Recipient> recipientList = new ArrayList<Recipient>(1);
recipientList.add(new Recipient(null, message.getSender(), null, null));
2011-12-20 18:20:44 +00:00
Recipients recipients = new Recipients(recipientList);
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients);
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
ContentValues values = new ContentValues(6);
values.put(ADDRESS, message.getSender());
values.put(DATE_RECEIVED, Long.valueOf(System.currentTimeMillis()));
values.put(DATE_SENT, timeSent);
values.put(PROTOCOL, message.getProtocol());
2011-12-20 18:20:44 +00:00
values.put(READ, Integer.valueOf(0));
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
if (message.getPseudoSubject().length() > 0)
values.put(SUBJECT, message.getPseudoSubject());
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
values.put(REPLY_PATH_PRESENT, message.isReplyPathPresent() ? 1 : 0);
values.put(SERVICE_CENTER, message.getServiceCenterAddress());
values.put(BODY, body);
values.put(TYPE, type);
values.put(THREAD_ID, threadId);
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
SQLiteDatabase db = databaseHelper.getWritableDatabase();
long messageId = db.insert(TABLE_NAME, null, values);
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
DatabaseFactory.getThreadDatabase(context).setUnread(threadId);
DatabaseFactory.getThreadDatabase(context).update(threadId);
notifyConversationListeners(threadId);
Trimmer.trimThread(context, threadId);
2011-12-20 18:20:44 +00:00
return messageId;
}
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
public long getThreadIdForMessage(long id) {
String sql = "SELECT " + THREAD_ID + " FROM " + TABLE_NAME + " WHERE " + ID + " = ?";
String[] sqlArgs = new String[] {id+""};
SQLiteDatabase db = databaseHelper.getReadableDatabase();
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
Cursor cursor = null;
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
try {
cursor = db.rawQuery(sql, sqlArgs);
if (cursor != null && cursor.moveToFirst())
2012-10-01 02:56:29 +00:00
return cursor.getLong(0);
2011-12-20 18:20:44 +00:00
else
2012-10-01 02:56:29 +00:00
return -1;
2011-12-20 18:20:44 +00:00
} finally {
if (cursor != null)
2012-10-01 02:56:29 +00:00
cursor.close();
2011-12-20 18:20:44 +00:00
}
}
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
public int getMessageCountForThread(long threadId) {
2012-09-09 23:10:46 +00:00
SQLiteDatabase db = databaseHelper.getReadableDatabase();
2011-12-20 18:20:44 +00:00
Cursor cursor = null;
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
try {
cursor = db.query(TABLE_NAME, new String[] {"COUNT(*)"}, THREAD_ID + " = ?", new String[] {threadId+""}, null, null, null);
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
if (cursor != null && cursor.moveToFirst())
2012-10-01 02:56:29 +00:00
return cursor.getInt(0);
2011-12-20 18:20:44 +00:00
} finally {
if (cursor != null)
2012-10-01 02:56:29 +00:00
cursor.close();
2011-12-20 18:20:44 +00:00
}
return 0;
}
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
public void markAsDecryptFailed(long id) {
updateType(id, Types.FAILED_DECRYPT_TYPE);
}
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
public void markAsNoSession(long id) {
updateType(id, Types.NO_SESSION_TYPE);
}
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
public void markAsDecrypting(long id) {
updateType(id, Types.DECRYPT_IN_PROGRESS_TYPE);
}
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
public void markAsSent(long id, long type) {
if (type == Types.ENCRYPTING_TYPE)
updateType(id, Types.SECURE_SENT_TYPE);
else
updateType(id, Types.SENT_TYPE);
}
2012-09-09 23:10:46 +00:00
public void markStatus(long id, int status) {
Log.w("MessageDatabase", "Updating ID: " + id + " to status: " + status);
ContentValues contentValues = new ContentValues();
contentValues.put(STATUS, status);
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {id+""});
notifyConversationListeners(getThreadIdForMessage(id));
}
2011-12-20 18:20:44 +00:00
public void markAsSentFailed(long id) {
updateType(id, Types.FAILED_TYPE);
}
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
public void setMessagesRead(long threadId) {
SQLiteDatabase database = databaseHelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(READ, 1);
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
long start = System.currentTimeMillis();
database.update(TABLE_NAME, contentValues, THREAD_ID + " = ? AND " + READ + " = 0", new String[] {threadId+""});
long end = System.currentTimeMillis();
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
Log.w("SmsDatabase", "setMessagesRead time: " + (end-start));
}
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
public void updateMessageBodyAndType(long messageId, String body, long type) {
ContentValues contentValues = new ContentValues();
contentValues.put(BODY, body);
contentValues.put(TYPE, type);
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {messageId+""});
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
DatabaseFactory.getThreadDatabase(context).update(getThreadIdForMessage(messageId));
notifyConversationListeners(getThreadIdForMessage(messageId));
notifyConversationListListeners();
}
2012-09-09 23:10:46 +00:00
public long insertSecureMessageReceived(TextMessage message, String body) {
return insertMessageReceived(message, body, Types.DECRYPT_IN_PROGRESS_TYPE,
message.getSentTimestampMillis());
2011-12-20 18:20:44 +00:00
}
2012-09-09 23:10:46 +00:00
public long insertMessageReceived(TextMessage message, String body) {
return insertMessageReceived(message, body, Types.INBOX_TYPE, message.getSentTimestampMillis());
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 insertMessageSent(String address, long threadId, String body, long date, long type) {
ContentValues contentValues = new ContentValues(6);
2012-10-01 02:56:29 +00:00
// contentValues.put(ADDRESS, NumberUtil.filterNumber(address));
2011-12-20 18:20:44 +00:00
contentValues.put(ADDRESS, address);
contentValues.put(THREAD_ID, threadId);
contentValues.put(BODY, body);
contentValues.put(DATE_RECEIVED, date);
contentValues.put(DATE_SENT, date);
2011-12-20 18:20:44 +00:00
contentValues.put(READ, 1);
contentValues.put(TYPE, type);
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
SQLiteDatabase db = databaseHelper.getWritableDatabase();
long messageId = db.insert(TABLE_NAME, ADDRESS, contentValues);
DatabaseFactory.getThreadDatabase(context).update(threadId);
notifyConversationListeners(threadId);
Trimmer.trimThread(context, threadId);
2011-12-20 18:20:44 +00:00
return messageId;
}
public Cursor getOutgoingMessages() {
String outgoingSelection = "(" + TYPE + " = " + Types.ENCRYPTING_TYPE + " OR " + TYPE + " = " + Types.ENCRYPTED_OUTBOX_TYPE + ")";
SQLiteDatabase db = databaseHelper.getReadableDatabase();
return db.query(TABLE_NAME, null, outgoingSelection, null, null, null, null);
}
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
public Cursor getDecryptInProgressMessages() {
String where = TYPE + " = " + Types.DECRYPT_IN_PROGRESS_TYPE;
SQLiteDatabase db = databaseHelper.getReadableDatabase();
return db.query(TABLE_NAME, null, where, null, null, null, null);
}
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
public Cursor getEncryptedRogueMessages(Recipient recipient) {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
2012-09-09 23:10:46 +00:00
String selection = TYPE + " = " + Types.NO_SESSION_TYPE + " AND PHONE_NUMBERS_EQUAL(" + ADDRESS + ", ?)";
2011-12-20 18:20:44 +00:00
String[] args = {recipient.getNumber()};
2012-09-09 23:10:46 +00:00
return db.query(TABLE_NAME, null, selection, args, null, null, null);
2011-12-20 18:20:44 +00:00
}
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
public Cursor getMessage(long messageId) {
SQLiteDatabase db = databaseHelper.getReadableDatabase();
return db.query(TABLE_NAME, null, ID_WHERE, new String[] {messageId+""}, null, null, null);
}
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
public void deleteMessage(long messageId) {
Log.w("MessageDatabase", "Deleting: " + messageId);
SQLiteDatabase db = databaseHelper.getWritableDatabase();
long threadId = getThreadIdForMessage(messageId);
db.delete(TABLE_NAME, ID_WHERE, new String[] {messageId+""});
DatabaseFactory.getThreadDatabase(context).update(threadId);
notifyConversationListeners(threadId);
}
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
/*package */void deleteThread(long threadId) {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.delete(TABLE_NAME, THREAD_ID + " = ?", new String[] {threadId+""});
}
2012-09-09 23:10:46 +00:00
/*package*/void deleteMessagesInThreadBeforeDate(long threadId, long date) {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
String where = THREAD_ID + " = ? AND (CASE " + TYPE;
for (int outgoingType : Types.OUTGOING_MESSAGE_TYPES) {
where += " WHEN " + outgoingType + " THEN " + DATE_SENT + " < " + date;
}
where += (" ELSE " + DATE_RECEIVED + " < " + date + " END)");
db.delete(TABLE_NAME, where, new String[] {threadId+""});
}
2011-12-20 18:20:44 +00:00
/*package*/ void deleteThreads(Set<Long> threadIds) {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
String where = "";
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
for (long threadId : threadIds) {
where += THREAD_ID + " = '" + threadId + "' OR ";
}
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
where = where.substring(0, where.length() - 4);
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
db.delete(TABLE_NAME, where, null);
}
/*package */ void deleteAllThreads() {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
2012-09-09 23:10:46 +00:00
db.delete(TABLE_NAME, null, null);
2011-12-20 18:20:44 +00:00
}
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
/*package*/ SQLiteDatabase beginTransaction() {
SQLiteDatabase database = databaseHelper.getWritableDatabase();
database.beginTransaction();
return database;
}
/*package*/ void endTransaction(SQLiteDatabase database) {
database.setTransactionSuccessful();
database.endTransaction();
2012-09-09 23:10:46 +00:00
}
2011-12-20 18:20:44 +00:00
/*package*/ void insertRaw(SQLiteDatabase database, ContentValues contentValues) {
database.insert(TABLE_NAME, null, contentValues);
}
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
/*package*/ SQLiteStatement createInsertStatement(SQLiteDatabase database) {
return database.compileStatement("INSERT INTO " + TABLE_NAME + " (" + ADDRESS + ", " +
PERSON + ", " +
DATE_SENT + ", " +
DATE_RECEIVED + ", " +
PROTOCOL + ", " +
READ + ", " +
STATUS + ", " +
TYPE + ", " +
REPLY_PATH_PRESENT + ", " +
SUBJECT + ", " +
BODY + ", " +
SERVICE_CENTER +
", THREAD_ID) " +
" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
2011-12-20 18:20:44 +00:00
}
2012-09-09 23:10:46 +00:00
public static class Status {
public static final int STATUS_NONE = -1;
public static final int STATUS_COMPLETE = 0;
public static final int STATUS_PENDING = 32;
public static final int STATUS_FAILED = 64;
}
2011-12-20 18:20:44 +00:00
public static class Types {
public static final int INBOX_TYPE = 1;
public static final int SENT_TYPE = 2;
public static final int SENT_PENDING = 4;
public static final int FAILED_TYPE = 5;
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
public static final int ENCRYPTING_TYPE = 42; // Messages are stored local encrypted and need async encryption and delivery.
public static final int ENCRYPTED_OUTBOX_TYPE = 43; // Messages are stored local encrypted and need delivery.
public static final int SECURE_SENT_TYPE = 44; // Messages were sent with async encryption.
public static final int SECURE_RECEIVED_TYPE = 45; // Messages were received with async decryption.
public static final int FAILED_DECRYPT_TYPE = 46; // Messages were received with async encryption and failed to decrypt.
public static final int DECRYPT_IN_PROGRESS_TYPE = 47; // Messages are in the process of being asymmetricaly decrypted.
public static final int NO_SESSION_TYPE = 48; // Messages were received with async encryption but there is no session yet.
2012-09-09 23:10:46 +00:00
public static final int[] OUTGOING_MESSAGE_TYPES = {SENT_TYPE, SENT_PENDING, ENCRYPTING_TYPE,
ENCRYPTED_OUTBOX_TYPE, SECURE_SENT_TYPE,
FAILED_TYPE};
2011-12-20 18:20:44 +00:00
public static boolean isFailedMessageType(long type) {
return type == FAILED_TYPE;
}
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
public static boolean isOutgoingMessageType(long type) {
for (int outgoingType : OUTGOING_MESSAGE_TYPES) {
if (type == outgoingType)
return true;
}
return false;
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 static boolean isPendingMessageType(long type) {
return type == SENT_PENDING || type == ENCRYPTING_TYPE || type == ENCRYPTED_OUTBOX_TYPE;
}
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
public static boolean isSecureType(long type) {
return type == SECURE_SENT_TYPE || type == ENCRYPTING_TYPE || type == SECURE_RECEIVED_TYPE;
}
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
}
2012-09-09 23:10:46 +00:00
2011-12-20 18:20:44 +00:00
}