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 ;
import android.content.Context ;
import android.database.Cursor ;
import android.database.sqlite.SQLiteDatabase ;
import android.database.sqlite.SQLiteOpenHelper ;
import android.database.sqlite.SQLiteQueryBuilder ;
import android.util.Log ;
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
2012-10-29 23:51:42 +00:00
public static final String TRANSPORT = " transport_type " ;
public static final String GROUP_SIZE = " group_size " ;
public static final String SMS_GROUP_SENT_COUNT = " sms_group_sent_count " ;
public static final String SMS_GROUP_SEND_FAILED_COUNT = " sms_group_sent_failed_count " ;
public static final String MMS_GROUP_SENT_COUNT = " mms_group_sent_count " ;
public static final String MMS_GROUP_SEND_FAILED_COUNT = " mms_group_sent_failed_count " ;
2011-12-20 18:20:44 +00:00
2013-01-06 21:13:14 +00:00
public static final String DATE_SENT = " date_sent " ;
public static final String DATE_RECEIVED = " date_received " ;
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
2012-10-29 23:51:42 +00:00
public Cursor getCollatedGroupConversation ( long threadId ) {
String smsCaseSecurity = " CASE " + SmsDatabase . TYPE + " " +
" WHEN " + SmsDatabase . Types . SENT_TYPE + " THEN 1 " +
" WHEN " + SmsDatabase . Types . SENT_PENDING + " THEN 1 " +
" WHEN " + SmsDatabase . Types . ENCRYPTED_OUTBOX_TYPE + " THEN 1 " +
" WHEN " + SmsDatabase . Types . FAILED_TYPE + " THEN 1 " +
" WHEN " + SmsDatabase . Types . ENCRYPTING_TYPE + " THEN 2 " +
" WHEN " + SmsDatabase . Types . SECURE_SENT_TYPE + " THEN 2 " +
" ELSE 0 END " ;
String mmsCaseSecurity = " CASE " + MmsDatabase . MESSAGE_BOX + " " +
" WHEN " + MmsDatabase . Types . MESSAGE_BOX_OUTBOX + " THEN 'insecure' " +
" WHEN " + MmsDatabase . Types . MESSAGE_BOX_SENT + " THEN 'insecure' " +
" WHEN " + MmsDatabase . Types . MESSAGE_BOX_SENT_FAILED + " THEN 'insecure' " +
" WHEN " + MmsDatabase . Types . MESSAGE_BOX_SECURE_OUTBOX + " THEN 'secure' " +
" WHEN " + MmsDatabase . Types . MESSAGE_BOX_SECURE_SENT + " THEN 'secure' " +
" ELSE 0 END " ;
String mmsGroupSentCount = " SUM(CASE " + MmsDatabase . MESSAGE_BOX + " " +
" WHEN " + MmsDatabase . Types . MESSAGE_BOX_SENT + " THEN 1 " +
" WHEN " + MmsDatabase . Types . MESSAGE_BOX_SECURE_SENT + " THEN 1 " +
" ELSE 0 END) " ;
String smsGroupSentCount = " SUM(CASE " + SmsDatabase . TYPE + " " +
" WHEN " + SmsDatabase . Types . SENT_TYPE + " THEN 1 " +
" WHEN " + SmsDatabase . Types . SECURE_SENT_TYPE + " THEN 1 " +
" ELSE 0 END) " ;
String mmsGroupSentFailedCount = " SUM(CASE " + MmsDatabase . MESSAGE_BOX + " " +
" WHEN " + MmsDatabase . Types . MESSAGE_BOX_SENT_FAILED + " THEN 1 " +
" ELSE 0 END) " ;
String smsGroupSentFailedCount = " SUM(CASE " + SmsDatabase . TYPE + " " +
" WHEN " + SmsDatabase . Types . FAILED_TYPE + " THEN 1 " +
" ELSE 0 END) " ;
2013-02-04 19:03:19 +00:00
String [ ] projection = { " _id " , " body " , " type " , " address " , " subject " , " status " , " normalized_date_sent AS date_sent " , " normalized_date_received AS date_received " , " m_type " , " msg_box " , " transport_type " , " COUNT(_id) AS group_size " , mmsGroupSentCount + " AS mms_group_sent_count " , mmsGroupSentFailedCount + " AS mms_group_sent_failed_count " , smsGroupSentCount + " AS sms_group_sent_count " , smsGroupSentFailedCount + " AS sms_group_sent_failed_count " , smsCaseSecurity + " AS sms_collate " , mmsCaseSecurity + " AS mms_collate " } ;
2013-01-06 21:13:14 +00:00
String order = " normalized_date_received ASC " ;
2012-10-29 23:51:42 +00:00
String selection = " thread_id = " + threadId ;
2013-01-06 21:13:14 +00:00
String groupBy = " normalized_date_sent / 1000, sms_collate, mms_collate " ;
2012-10-29 23:51:42 +00:00
Cursor cursor = queryTables ( projection , selection , order , groupBy , null ) ;
setNotifyConverationListeners ( cursor , threadId ) ;
return cursor ;
}
2011-12-20 18:20:44 +00:00
public Cursor getConversation ( long threadId ) {
2013-01-06 21:13:14 +00:00
String [ ] projection = { " _id " , " body " , " type " , " address " , " subject " ,
" normalized_date_sent AS date_sent " ,
" normalized_date_received AS date_received " ,
2013-01-07 05:38:36 +00:00
" m_type " , " msg_box " , " status " , " transport_type " } ;
2013-01-06 21:13:14 +00:00
String order = " normalized_date_received ASC " ;
2011-12-20 18:20:44 +00:00
String selection = " thread_id = " + threadId ;
2012-10-01 02:56:29 +00:00
2012-10-29 23:51:42 +00:00
Cursor cursor = queryTables ( projection , selection , order , null , 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 ) {
2013-01-06 21:13:14 +00:00
String [ ] projection = { " _id " , " body " , " type " , " address " , " subject " ,
" normalized_date_sent AS date_sent " ,
" normalized_date_received AS date_received " ,
" m_type " , " msg_box " , " transport_type " } ;
String order = " normalized_date_received DESC " ;
2011-12-20 18:20:44 +00:00
String selection = " thread_id = " + threadId ;
2012-10-01 02:56:29 +00:00
2012-10-29 23:51:42 +00:00
Cursor cursor = queryTables ( projection , selection , order , null , " 1 " ) ;
2011-12-20 18:20:44 +00:00
return cursor ;
}
2012-10-01 02:56:29 +00:00
2011-12-20 18:20:44 +00:00
public Cursor getUnread ( ) {
2013-01-06 21:13:14 +00:00
String [ ] projection = { " _id " , " body " , " read " , " type " , " address " , " subject " , " thread_id " ,
" normalized_date_sent AS date_sent " ,
" normalized_date_received AS date_received " ,
" m_type " , " msg_box " , " transport_type " } ;
String order = " normalized_date_received ASC " ;
2011-12-20 18:20:44 +00:00
String selection = " read = 0 " ;
2012-10-01 02:56:29 +00:00
2012-10-29 23:51:42 +00:00
Cursor cursor = queryTables ( projection , selection , order , null , null ) ;
2012-10-01 02:56:29 +00:00
return cursor ;
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 ;
}
2012-10-29 23:51:42 +00:00
private Cursor queryTables ( String [ ] projection , String selection , String order , String groupBy , String limit ) {
2013-01-07 05:38:36 +00:00
String [ ] mmsProjection = { " date * 1000 AS normalized_date_sent " , " date_received * 1000 AS normalized_date_received " , " _id " , " body " , " read " , " thread_id " , " type " , " address " , " subject " , " date " , " m_type " , " msg_box " , " status " , " transport_type " } ;
String [ ] smsProjection = { " date_sent * 1 AS normalized_date_sent " , " date * 1 AS normalized_date_received " , " _id " , " body " , " read " , " thread_id " , " type " , " address " , " subject " , " date " , " m_type " , " msg_box " , " status " , " transport_type " } ;
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 ) ;
mmsQueryBuilder . setTables ( MmsDatabase . TABLE_NAME ) ;
smsQueryBuilder . setTables ( SmsDatabase . TABLE_NAME ) ;
2012-10-01 02:56:29 +00:00
2011-12-20 18:20:44 +00:00
Set < String > mmsColumnsPresent = new HashSet < String > ( ) ;
mmsColumnsPresent . add ( " _id " ) ;
mmsColumnsPresent . add ( " m_type " ) ;
mmsColumnsPresent . add ( " msg_box " ) ;
mmsColumnsPresent . add ( " date " ) ;
2013-01-06 21:13:14 +00:00
mmsColumnsPresent . add ( " date_received " ) ;
2011-12-20 18:20:44 +00:00
mmsColumnsPresent . add ( " read " ) ;
mmsColumnsPresent . add ( " thread_id " ) ;
2012-10-01 02:56:29 +00:00
2011-12-20 18:20:44 +00:00
Set < String > smsColumnsPresent = new HashSet < String > ( ) ;
smsColumnsPresent . add ( " _id " ) ;
smsColumnsPresent . add ( " body " ) ;
smsColumnsPresent . add ( " type " ) ;
smsColumnsPresent . add ( " address " ) ;
smsColumnsPresent . add ( " subject " ) ;
2013-01-06 21:13:14 +00:00
smsColumnsPresent . add ( " date_sent " ) ;
2011-12-20 18:20:44 +00:00
smsColumnsPresent . add ( " date " ) ;
smsColumnsPresent . add ( " read " ) ;
smsColumnsPresent . add ( " thread_id " ) ;
2013-01-07 05:38:36 +00:00
smsColumnsPresent . add ( " status " ) ;
2011-12-20 18:20:44 +00:00
2013-01-06 21:13:14 +00:00
String mmsSubQuery = mmsQueryBuilder . buildUnionSubQuery ( " transport_type " , mmsProjection , mmsColumnsPresent , 2 , " mms " , selection , null , null , null ) ;
String smsSubQuery = smsQueryBuilder . buildUnionSubQuery ( " transport_type " , smsProjection , smsColumnsPresent , 2 , " sms " , selection , null , null , null ) ;
2011-12-20 18:20:44 +00:00
2012-10-01 02:56:29 +00:00
SQLiteQueryBuilder unionQueryBuilder = new SQLiteQueryBuilder ( ) ;
2011-12-20 18:20:44 +00:00
String unionQuery = unionQueryBuilder . buildUnionQuery ( new String [ ] { smsSubQuery , mmsSubQuery } , order , null ) ;
SQLiteQueryBuilder outerQueryBuilder = new SQLiteQueryBuilder ( ) ;
outerQueryBuilder . setTables ( " ( " + unionQuery + " ) " ) ;
2012-10-01 02:56:29 +00:00
2012-10-29 23:51:42 +00:00
String query = outerQueryBuilder . buildQuery ( projection , null , null , groupBy , null , null , limit ) ;
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 ( ) ;
Cursor cursor = db . rawQuery ( query , null ) ;
return cursor ;
2012-10-01 02:56:29 +00:00
2011-12-20 18:20:44 +00:00
}
2012-10-01 02:56:29 +00:00
2011-12-20 18:20:44 +00:00
}