2012-10-01 02:56:29 +00:00
/ * *
2011-12-20 18:20:44 +00:00
* Copyright ( C ) 2011 Whisper Systems
2012-10-01 02:56:29 +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-10-01 02:56:29 +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 ;
2015-10-21 22:32:19 +00:00
import android.annotation.TargetApi ;
2011-12-20 18:20:44 +00:00
import android.content.Context ;
import android.database.Cursor ;
import android.database.sqlite.SQLiteDatabase ;
import android.database.sqlite.SQLiteOpenHelper ;
import android.database.sqlite.SQLiteQueryBuilder ;
2015-10-21 22:32:19 +00:00
import android.os.Build ;
2015-07-20 22:05:56 +00:00
import android.support.annotation.NonNull ;
import android.support.annotation.Nullable ;
2011-12-20 18:20:44 +00:00
import android.util.Log ;
2014-11-03 23:16:04 +00:00
import org.thoughtcrime.securesms.crypto.MasterSecret ;
2015-01-15 21:35:35 +00:00
import org.thoughtcrime.securesms.database.model.MessageRecord ;
2015-07-20 22:05:56 +00:00
import org.whispersystems.libaxolotl.util.guava.Optional ;
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
2012-10-01 02:56:29 +00:00
import java.util.HashSet ;
import java.util.Set ;
2011-12-20 18:20:44 +00:00
public class MmsSmsDatabase extends Database {
2012-10-01 02:56:29 +00:00
2015-10-21 22:32:19 +00:00
private static final String TAG = MmsSmsDatabase . class . getSimpleName ( ) ;
2013-04-26 01:59:49 +00:00
public static final String TRANSPORT = " transport_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
public static final String MMS_TRANSPORT = " mms " ;
public static final String SMS_TRANSPORT = " sms " ;
2013-01-06 21:13:14 +00:00
2015-10-21 22:32:19 +00:00
private static final String [ ] PROJECTION = { MmsSmsColumns . ID , SmsDatabase . BODY , SmsDatabase . TYPE ,
MmsSmsColumns . THREAD_ID ,
SmsDatabase . ADDRESS , SmsDatabase . ADDRESS_DEVICE_ID , SmsDatabase . SUBJECT ,
MmsSmsColumns . NORMALIZED_DATE_SENT ,
MmsSmsColumns . NORMALIZED_DATE_RECEIVED ,
MmsDatabase . MESSAGE_TYPE , MmsDatabase . MESSAGE_BOX ,
SmsDatabase . STATUS , MmsDatabase . PART_COUNT ,
MmsDatabase . CONTENT_LOCATION , MmsDatabase . TRANSACTION_ID ,
MmsDatabase . MESSAGE_SIZE , MmsDatabase . EXPIRY ,
MmsDatabase . STATUS , MmsSmsColumns . RECEIPT_COUNT ,
MmsSmsColumns . MISMATCHED_IDENTITIES ,
MmsDatabase . NETWORK_FAILURE , TRANSPORT ,
AttachmentDatabase . ATTACHMENT_ID_ALIAS ,
AttachmentDatabase . UNIQUE_ID ,
AttachmentDatabase . MMS_ID ,
AttachmentDatabase . SIZE ,
AttachmentDatabase . DATA ,
AttachmentDatabase . CONTENT_TYPE ,
AttachmentDatabase . CONTENT_LOCATION ,
AttachmentDatabase . CONTENT_DISPOSITION ,
AttachmentDatabase . NAME ,
AttachmentDatabase . TRANSFER_STATE } ;
2011-12-20 18:20:44 +00:00
public MmsSmsDatabase ( Context context , SQLiteOpenHelper databaseHelper ) {
super ( context , databaseHelper ) ;
}
2012-10-01 02:56:29 +00:00
2015-08-04 19:29:26 +00:00
public Cursor getConversation ( long threadId , long limit ) {
2015-10-21 22:32:19 +00:00
String order = MmsSmsColumns . NORMALIZED_DATE_RECEIVED + " DESC " ;
String selection = MmsSmsColumns . THREAD_ID + " = " + threadId ;
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
2015-10-21 22:32:19 +00:00
Cursor cursor = queryTables ( PROJECTION , selection , order , limit > 0 ? String . valueOf ( limit ) : null ) ;
2015-01-15 21:35:35 +00:00
setNotifyConverationListeners ( cursor , threadId ) ;
return cursor ;
}
2015-08-04 19:29:26 +00:00
public Cursor getConversation ( long threadId ) {
return getConversation ( threadId , 0 ) ;
}
2015-01-15 21:35:35 +00:00
public Cursor getIdentityConflictMessagesForThread ( long threadId ) {
String order = MmsSmsColumns . NORMALIZED_DATE_RECEIVED + " ASC " ;
String selection = MmsSmsColumns . THREAD_ID + " = " + threadId + " AND " + MmsSmsColumns . MISMATCHED_IDENTITIES + " IS NOT NULL " ;
2015-10-21 22:32:19 +00:00
Cursor cursor = queryTables ( PROJECTION , selection , order , null ) ;
2011-12-20 18:20:44 +00:00
setNotifyConverationListeners ( cursor , threadId ) ;
return cursor ;
}
2012-10-01 02:56:29 +00:00
2011-12-20 18:20:44 +00:00
public Cursor getConversationSnippet ( long threadId ) {
2015-10-21 22:32:19 +00:00
String order = MmsSmsColumns . NORMALIZED_DATE_RECEIVED + " DESC " ;
String selection = MmsSmsColumns . THREAD_ID + " = " + threadId ;
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
2015-10-21 22:32:19 +00:00
return queryTables ( PROJECTION , selection , order , " 1 " ) ;
2011-12-20 18:20:44 +00:00
}
2012-10-01 02:56:29 +00:00
2011-12-20 18:20:44 +00:00
public Cursor getUnread ( ) {
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
String order = MmsSmsColumns . NORMALIZED_DATE_RECEIVED + " ASC " ;
String selection = MmsSmsColumns . READ + " = 0 " ;
2015-10-21 22:32:19 +00:00
return queryTables ( PROJECTION , selection , order , null ) ;
2011-12-20 18:20:44 +00:00
}
2012-10-01 02:56:29 +00:00
2011-12-20 18:20:44 +00:00
public int getConversationCount ( long threadId ) {
int count = DatabaseFactory . getSmsDatabase ( context ) . getMessageCountForThread ( threadId ) ;
count + = DatabaseFactory . getMmsDatabase ( context ) . getMessageCountForThread ( threadId ) ;
2012-10-01 02:56:29 +00:00
2011-12-20 18:20:44 +00:00
return count ;
}
2014-07-25 22:14:29 +00:00
public void incrementDeliveryReceiptCount ( String address , long timestamp ) {
DatabaseFactory . getSmsDatabase ( context ) . incrementDeliveryReceiptCount ( address , timestamp ) ;
DatabaseFactory . getMmsDatabase ( context ) . incrementDeliveryReceiptCount ( address , timestamp ) ;
}
2015-10-21 22:32:19 +00:00
private Cursor queryTables ( String [ ] projection , String selection , String order , String limit ) {
2015-08-04 20:37:22 +00:00
String [ ] mmsProjection = { MmsDatabase . DATE_SENT + " AS " + MmsSmsColumns . NORMALIZED_DATE_SENT ,
MmsDatabase . DATE_RECEIVED + " AS " + MmsSmsColumns . NORMALIZED_DATE_RECEIVED ,
2015-10-21 22:32:19 +00:00
MmsDatabase . TABLE_NAME + " . " + MmsDatabase . ID + " AS " + MmsSmsColumns . ID ,
AttachmentDatabase . TABLE_NAME + " . " + AttachmentDatabase . ROW_ID + " AS " + AttachmentDatabase . ATTACHMENT_ID_ALIAS ,
SmsDatabase . BODY , MmsSmsColumns . READ , MmsSmsColumns . THREAD_ID ,
2014-02-03 03:38:06 +00:00
SmsDatabase . TYPE , SmsDatabase . ADDRESS , SmsDatabase . ADDRESS_DEVICE_ID , SmsDatabase . SUBJECT , MmsDatabase . MESSAGE_TYPE ,
2013-05-05 19:51:36 +00:00
MmsDatabase . MESSAGE_BOX , SmsDatabase . STATUS , MmsDatabase . PART_COUNT ,
MmsDatabase . CONTENT_LOCATION , MmsDatabase . TRANSACTION_ID ,
MmsDatabase . MESSAGE_SIZE , MmsDatabase . EXPIRY , MmsDatabase . STATUS ,
2015-01-15 21:35:35 +00:00
MmsSmsColumns . RECEIPT_COUNT , MmsSmsColumns . MISMATCHED_IDENTITIES ,
2015-10-21 22:32:19 +00:00
MmsDatabase . NETWORK_FAILURE , TRANSPORT ,
AttachmentDatabase . UNIQUE_ID ,
AttachmentDatabase . MMS_ID ,
AttachmentDatabase . SIZE ,
AttachmentDatabase . DATA ,
AttachmentDatabase . CONTENT_TYPE ,
AttachmentDatabase . CONTENT_LOCATION ,
AttachmentDatabase . CONTENT_DISPOSITION ,
AttachmentDatabase . NAME ,
AttachmentDatabase . TRANSFER_STATE } ;
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
2015-08-04 20:37:22 +00:00
String [ ] smsProjection = { SmsDatabase . DATE_SENT + " AS " + MmsSmsColumns . NORMALIZED_DATE_SENT ,
SmsDatabase . DATE_RECEIVED + " AS " + MmsSmsColumns . NORMALIZED_DATE_RECEIVED ,
2015-10-21 22:32:19 +00:00
MmsSmsColumns . ID , " NULL AS " + AttachmentDatabase . ATTACHMENT_ID_ALIAS ,
SmsDatabase . BODY , MmsSmsColumns . READ , MmsSmsColumns . THREAD_ID ,
2014-02-03 03:38:06 +00:00
SmsDatabase . TYPE , SmsDatabase . ADDRESS , SmsDatabase . ADDRESS_DEVICE_ID , SmsDatabase . SUBJECT , MmsDatabase . MESSAGE_TYPE ,
2013-05-05 19:51:36 +00:00
MmsDatabase . MESSAGE_BOX , SmsDatabase . STATUS , MmsDatabase . PART_COUNT ,
MmsDatabase . CONTENT_LOCATION , MmsDatabase . TRANSACTION_ID ,
MmsDatabase . MESSAGE_SIZE , MmsDatabase . EXPIRY , MmsDatabase . STATUS ,
2015-01-15 21:35:35 +00:00
MmsSmsColumns . RECEIPT_COUNT , MmsSmsColumns . MISMATCHED_IDENTITIES ,
2015-10-21 22:32:19 +00:00
MmsDatabase . NETWORK_FAILURE , TRANSPORT ,
AttachmentDatabase . UNIQUE_ID ,
AttachmentDatabase . MMS_ID ,
AttachmentDatabase . SIZE ,
AttachmentDatabase . DATA ,
AttachmentDatabase . CONTENT_TYPE ,
AttachmentDatabase . CONTENT_LOCATION ,
AttachmentDatabase . CONTENT_DISPOSITION ,
AttachmentDatabase . NAME ,
AttachmentDatabase . TRANSFER_STATE } ;
2012-10-01 02:56:29 +00:00
2011-12-20 18:20:44 +00:00
SQLiteQueryBuilder mmsQueryBuilder = new SQLiteQueryBuilder ( ) ;
SQLiteQueryBuilder smsQueryBuilder = new SQLiteQueryBuilder ( ) ;
mmsQueryBuilder . setDistinct ( true ) ;
smsQueryBuilder . setDistinct ( true ) ;
2015-10-21 22:32:19 +00:00
mmsQueryBuilder . setTables ( MmsDatabase . TABLE_NAME + " LEFT OUTER JOIN " + AttachmentDatabase . TABLE_NAME + " ON ( " + MmsDatabase . TABLE_NAME + " . " + MmsDatabase . ID + " = " + AttachmentDatabase . TABLE_NAME + " . " + AttachmentDatabase . MMS_ID + " ) " ) ;
2011-12-20 18:20:44 +00:00
smsQueryBuilder . setTables ( SmsDatabase . TABLE_NAME ) ;
2012-10-01 02:56:29 +00:00
2015-10-21 22:32:19 +00:00
Set < String > mmsColumnsPresent = new HashSet < > ( ) ;
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
mmsColumnsPresent . add ( MmsSmsColumns . ID ) ;
2013-05-05 19:51:36 +00:00
mmsColumnsPresent . add ( MmsSmsColumns . READ ) ;
mmsColumnsPresent . add ( MmsSmsColumns . THREAD_ID ) ;
mmsColumnsPresent . add ( MmsSmsColumns . BODY ) ;
mmsColumnsPresent . add ( MmsSmsColumns . ADDRESS ) ;
2014-02-03 03:38:06 +00:00
mmsColumnsPresent . add ( MmsSmsColumns . ADDRESS_DEVICE_ID ) ;
2014-07-25 22:14:29 +00:00
mmsColumnsPresent . add ( MmsSmsColumns . RECEIPT_COUNT ) ;
2015-01-15 21:35:35 +00:00
mmsColumnsPresent . add ( MmsSmsColumns . MISMATCHED_IDENTITIES ) ;
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
mmsColumnsPresent . add ( MmsDatabase . MESSAGE_TYPE ) ;
mmsColumnsPresent . add ( MmsDatabase . MESSAGE_BOX ) ;
mmsColumnsPresent . add ( MmsDatabase . DATE_SENT ) ;
mmsColumnsPresent . add ( MmsDatabase . DATE_RECEIVED ) ;
2013-04-26 18:23:43 +00:00
mmsColumnsPresent . add ( MmsDatabase . PART_COUNT ) ;
2013-05-05 19:51:36 +00:00
mmsColumnsPresent . add ( MmsDatabase . CONTENT_LOCATION ) ;
mmsColumnsPresent . add ( MmsDatabase . TRANSACTION_ID ) ;
mmsColumnsPresent . add ( MmsDatabase . MESSAGE_SIZE ) ;
mmsColumnsPresent . add ( MmsDatabase . EXPIRY ) ;
mmsColumnsPresent . add ( MmsDatabase . STATUS ) ;
2015-01-15 21:35:35 +00:00
mmsColumnsPresent . add ( MmsDatabase . NETWORK_FAILURE ) ;
2012-10-01 02:56:29 +00:00
2015-10-21 22:32:19 +00:00
mmsColumnsPresent . add ( AttachmentDatabase . ROW_ID ) ;
mmsColumnsPresent . add ( AttachmentDatabase . UNIQUE_ID ) ;
mmsColumnsPresent . add ( AttachmentDatabase . SIZE ) ;
mmsColumnsPresent . add ( AttachmentDatabase . CONTENT_TYPE ) ;
mmsColumnsPresent . add ( AttachmentDatabase . CONTENT_LOCATION ) ;
mmsColumnsPresent . add ( AttachmentDatabase . CONTENT_DISPOSITION ) ;
mmsColumnsPresent . add ( AttachmentDatabase . NAME ) ;
mmsColumnsPresent . add ( AttachmentDatabase . TRANSFER_STATE ) ;
Set < String > smsColumnsPresent = new HashSet < > ( ) ;
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
smsColumnsPresent . add ( MmsSmsColumns . ID ) ;
2013-04-26 18:23:43 +00:00
smsColumnsPresent . add ( MmsSmsColumns . BODY ) ;
2013-05-05 19:51:36 +00:00
smsColumnsPresent . add ( MmsSmsColumns . ADDRESS ) ;
2014-02-03 03:38:06 +00:00
smsColumnsPresent . add ( MmsSmsColumns . ADDRESS_DEVICE_ID ) ;
2013-05-05 19:51:36 +00:00
smsColumnsPresent . add ( MmsSmsColumns . READ ) ;
smsColumnsPresent . add ( MmsSmsColumns . THREAD_ID ) ;
2014-07-25 22:14:29 +00:00
smsColumnsPresent . add ( MmsSmsColumns . RECEIPT_COUNT ) ;
2015-01-15 21:35:35 +00:00
smsColumnsPresent . add ( MmsSmsColumns . MISMATCHED_IDENTITIES ) ;
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
smsColumnsPresent . add ( SmsDatabase . TYPE ) ;
smsColumnsPresent . add ( SmsDatabase . SUBJECT ) ;
smsColumnsPresent . add ( SmsDatabase . DATE_SENT ) ;
smsColumnsPresent . add ( SmsDatabase . DATE_RECEIVED ) ;
smsColumnsPresent . add ( SmsDatabase . STATUS ) ;
2015-10-21 22:32:19 +00:00
String mmsSubQuery = mmsQueryBuilder . buildUnionSubQuery ( TRANSPORT , mmsProjection , mmsColumnsPresent , 3 , MMS_TRANSPORT , selection , null , null , null ) ;
String smsSubQuery = smsQueryBuilder . buildUnionSubQuery ( TRANSPORT , smsProjection , smsColumnsPresent , 3 , SMS_TRANSPORT , selection , null , null , null ) ;
2011-12-20 18:20:44 +00:00
2012-10-01 02:56:29 +00:00
SQLiteQueryBuilder unionQueryBuilder = new SQLiteQueryBuilder ( ) ;
2015-10-21 22:32:19 +00:00
String unionQuery = unionQueryBuilder . buildUnionQuery ( new String [ ] { smsSubQuery , mmsSubQuery } , order , limit ) ;
2011-12-20 18:20:44 +00:00
SQLiteQueryBuilder outerQueryBuilder = new SQLiteQueryBuilder ( ) ;
2015-10-24 21:38:03 +00:00
outerQueryBuilder . setDistinct ( true ) ;
2011-12-20 18:20:44 +00:00
outerQueryBuilder . setTables ( " ( " + unionQuery + " ) " ) ;
2012-10-01 02:56:29 +00:00
2015-10-21 22:32:19 +00:00
String query = outerQueryBuilder . buildQuery ( projection , null , null , null , null , null , null ) ;
2012-10-01 02:56:29 +00:00
2011-12-20 18:20:44 +00:00
Log . w ( " MmsSmsDatabase " , " Executing query: " + query ) ;
SQLiteDatabase db = databaseHelper . getReadableDatabase ( ) ;
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
return db . rawQuery ( query , null ) ;
}
2015-07-20 22:05:56 +00:00
public Reader readerFor ( @NonNull Cursor cursor , @Nullable MasterSecret masterSecret ) {
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
return new Reader ( cursor , masterSecret ) ;
}
2012-10-01 02:56:29 +00:00
2015-07-20 22:05:56 +00:00
public Reader readerFor ( @NonNull 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
return new Reader ( cursor ) ;
2011-12-20 18:20:44 +00:00
}
2012-10-01 02:56:29 +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 {
2015-07-20 22:05:56 +00:00
private final Cursor cursor ;
private final Optional < MasterSecret > masterSecret ;
private EncryptingSmsDatabase . Reader smsReader ;
private MmsDatabase . Reader mmsReader ;
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-07-20 22:05:56 +00:00
public Reader ( Cursor cursor , @Nullable MasterSecret masterSecret ) {
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
this . cursor = cursor ;
2015-07-20 22:05:56 +00:00
this . masterSecret = Optional . fromNullable ( masterSecret ) ;
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 ( Cursor cursor ) {
2015-07-20 22:05:56 +00:00
this ( cursor , null ) ;
}
private EncryptingSmsDatabase . Reader getSmsReader ( ) {
if ( smsReader = = null ) {
if ( masterSecret . isPresent ( ) ) smsReader = DatabaseFactory . getEncryptingSmsDatabase ( context ) . readerFor ( masterSecret . get ( ) , cursor ) ;
else smsReader = DatabaseFactory . getSmsDatabase ( context ) . readerFor ( cursor ) ;
}
return smsReader ;
}
private MmsDatabase . Reader getMmsReader ( ) {
if ( mmsReader = = null ) {
mmsReader = DatabaseFactory . getMmsDatabase ( context ) . readerFor ( masterSecret . orNull ( ) , cursor ) ;
}
return mmsReader ;
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
}
public MessageRecord getNext ( ) {
if ( cursor = = null | | ! cursor . moveToNext ( ) )
return null ;
return getCurrent ( ) ;
}
public MessageRecord getCurrent ( ) {
String type = cursor . getString ( cursor . getColumnIndexOrThrow ( TRANSPORT ) ) ;
2015-10-21 22:32:19 +00:00
Log . w ( " MmsSmsDatabase " , " Type: " + type ) ;
if ( MmsSmsDatabase . MMS_TRANSPORT . equals ( type ) ) return getMmsReader ( ) . getCurrent ( ) ;
else if ( MmsSmsDatabase . SMS_TRANSPORT . equals ( type ) ) return getSmsReader ( ) . getCurrent ( ) ;
else throw new AssertionError ( " Bad type: " + 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 18:23:43 +00:00
public void close ( ) {
cursor . close ( ) ;
}
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
}
2011-12-20 18:20:44 +00:00
}