2015-10-12 18:25:05 -07:00
package org.thoughtcrime.securesms.database ;
import android.content.Context ;
import android.database.Cursor ;
import android.database.sqlite.SQLiteDatabase ;
import android.database.sqlite.SQLiteOpenHelper ;
2017-03-28 12:05:30 -07:00
import android.support.annotation.NonNull ;
2017-07-26 09:59:15 -07:00
import android.support.annotation.Nullable ;
2015-10-12 18:25:05 -07:00
import org.thoughtcrime.securesms.attachments.Attachment ;
import org.thoughtcrime.securesms.attachments.DatabaseAttachment ;
2017-03-28 12:05:30 -07:00
import org.thoughtcrime.securesms.crypto.MasterSecret ;
2015-10-12 18:25:05 -07:00
2017-02-01 03:49:19 +01:00
public class MediaDatabase extends Database {
2015-10-12 18:25:05 -07:00
2017-09-25 08:32:45 -07:00
private static final String BASE_MEDIA_QUERY = " SELECT " + AttachmentDatabase . TABLE_NAME + " . " + AttachmentDatabase . ROW_ID + " AS " + AttachmentDatabase . ATTACHMENT_ID_ALIAS + " , "
2015-10-12 18:25:05 -07:00
+ AttachmentDatabase . TABLE_NAME + " . " + AttachmentDatabase . CONTENT_TYPE + " , "
+ AttachmentDatabase . TABLE_NAME + " . " + AttachmentDatabase . THUMBNAIL_ASPECT_RATIO + " , "
+ AttachmentDatabase . TABLE_NAME + " . " + AttachmentDatabase . UNIQUE_ID + " , "
+ AttachmentDatabase . TABLE_NAME + " . " + AttachmentDatabase . MMS_ID + " , "
+ AttachmentDatabase . TABLE_NAME + " . " + AttachmentDatabase . TRANSFER_STATE + " , "
+ AttachmentDatabase . TABLE_NAME + " . " + AttachmentDatabase . SIZE + " , "
2017-03-28 12:05:30 -07:00
+ AttachmentDatabase . TABLE_NAME + " . " + AttachmentDatabase . FILE_NAME + " , "
2015-10-12 18:25:05 -07:00
+ AttachmentDatabase . TABLE_NAME + " . " + AttachmentDatabase . DATA + " , "
2016-12-11 13:37:27 -08:00
+ AttachmentDatabase . TABLE_NAME + " . " + AttachmentDatabase . THUMBNAIL + " , "
2017-03-28 12:05:30 -07:00
+ AttachmentDatabase . TABLE_NAME + " . " + AttachmentDatabase . CONTENT_LOCATION + " , "
+ AttachmentDatabase . TABLE_NAME + " . " + AttachmentDatabase . CONTENT_DISPOSITION + " , "
+ AttachmentDatabase . TABLE_NAME + " . " + AttachmentDatabase . DIGEST + " , "
2017-04-22 16:29:26 -07:00
+ AttachmentDatabase . TABLE_NAME + " . " + AttachmentDatabase . FAST_PREFLIGHT_ID + " , "
2017-05-11 22:46:35 -07:00
+ AttachmentDatabase . TABLE_NAME + " . " + AttachmentDatabase . VOICE_NOTE + " , "
2017-03-28 12:05:30 -07:00
+ AttachmentDatabase . TABLE_NAME + " . " + AttachmentDatabase . NAME + " , "
2016-02-15 02:22:16 +01:00
+ MmsDatabase . TABLE_NAME + " . " + MmsDatabase . MESSAGE_BOX + " , "
+ MmsDatabase . TABLE_NAME + " . " + MmsDatabase . DATE_SENT + " , "
+ MmsDatabase . TABLE_NAME + " . " + MmsDatabase . DATE_RECEIVED + " , "
2015-10-12 18:25:05 -07:00
+ MmsDatabase . TABLE_NAME + " . " + MmsDatabase . ADDRESS + " "
+ " FROM " + AttachmentDatabase . TABLE_NAME + " LEFT JOIN " + MmsDatabase . TABLE_NAME
+ " ON " + AttachmentDatabase . TABLE_NAME + " . " + AttachmentDatabase . MMS_ID + " = " + MmsDatabase . TABLE_NAME + " . " + MmsDatabase . ID + " "
+ " WHERE " + AttachmentDatabase . MMS_ID + " IN (SELECT " + MmsSmsColumns . ID
+ " FROM " + MmsDatabase . TABLE_NAME
2017-09-25 08:32:45 -07:00
+ " WHERE " + MmsDatabase . THREAD_ID + " = ?) AND (%s) AND "
2015-11-13 00:07:05 +01:00
+ AttachmentDatabase . DATA + " IS NOT NULL "
2015-10-12 18:25:05 -07:00
+ " ORDER BY " + AttachmentDatabase . TABLE_NAME + " . " + AttachmentDatabase . ROW_ID + " DESC " ;
2017-09-25 08:32:45 -07:00
private static final String GALLERY_MEDIA_QUERY = String . format ( BASE_MEDIA_QUERY , AttachmentDatabase . CONTENT_TYPE + " LIKE 'image/%' OR " + AttachmentDatabase . CONTENT_TYPE + " LIKE 'video/%' " ) ;
2017-10-03 16:41:20 -07:00
private static final String DOCUMENT_MEDIA_QUERY = String . format ( BASE_MEDIA_QUERY , AttachmentDatabase . CONTENT_TYPE + " NOT LIKE 'image/%' AND " + AttachmentDatabase . CONTENT_TYPE + " NOT LIKE 'video/%' AND " + AttachmentDatabase . CONTENT_TYPE + " NOT LIKE 'audio/%' " ) ;
2017-09-25 08:32:45 -07:00
2017-02-01 03:49:19 +01:00
public MediaDatabase ( Context context , SQLiteOpenHelper databaseHelper ) {
2015-10-12 18:25:05 -07:00
super ( context , databaseHelper ) ;
}
2017-09-25 08:32:45 -07:00
public Cursor getGalleryMediaForThread ( long threadId ) {
SQLiteDatabase database = databaseHelper . getReadableDatabase ( ) ;
Cursor cursor = database . rawQuery ( GALLERY_MEDIA_QUERY , new String [ ] { threadId + " " } ) ;
setNotifyConverationListeners ( cursor , threadId ) ;
return cursor ;
}
public Cursor getDocumentMediaForThread ( long threadId ) {
2015-10-12 18:25:05 -07:00
SQLiteDatabase database = databaseHelper . getReadableDatabase ( ) ;
2017-09-25 08:32:45 -07:00
Cursor cursor = database . rawQuery ( DOCUMENT_MEDIA_QUERY , new String [ ] { threadId + " " } ) ;
2015-10-12 18:25:05 -07:00
setNotifyConverationListeners ( cursor , threadId ) ;
return cursor ;
}
2017-02-01 03:49:19 +01:00
public static class MediaRecord {
2017-03-28 12:05:30 -07:00
private final DatabaseAttachment attachment ;
2017-07-26 09:59:15 -07:00
private final Address address ;
2017-03-28 12:05:30 -07:00
private final long date ;
2017-07-26 09:59:15 -07:00
private MediaRecord ( DatabaseAttachment attachment , @Nullable Address address , long date ) {
2017-03-28 12:05:30 -07:00
this . attachment = attachment ;
this . address = address ;
this . date = date ;
2015-10-12 18:25:05 -07:00
}
2017-03-28 12:05:30 -07:00
public static MediaRecord from ( @NonNull Context context , @NonNull MasterSecret masterSecret , @NonNull Cursor cursor ) {
AttachmentDatabase attachmentDatabase = DatabaseFactory . getAttachmentDatabase ( context ) ;
DatabaseAttachment attachment = attachmentDatabase . getAttachment ( masterSecret , cursor ) ;
2017-07-26 09:59:15 -07:00
String serializedAddress = cursor . getString ( cursor . getColumnIndexOrThrow ( MmsDatabase . ADDRESS ) ) ;
Address address = null ;
if ( serializedAddress ! = null ) {
address = Address . fromSerialized ( serializedAddress ) ;
}
2015-10-12 18:25:05 -07:00
2016-02-15 02:22:16 +01:00
long date ;
if ( MmsDatabase . Types . isPushType ( cursor . getLong ( cursor . getColumnIndexOrThrow ( MmsDatabase . MESSAGE_BOX ) ) ) ) {
date = cursor . getLong ( cursor . getColumnIndexOrThrow ( MmsDatabase . DATE_SENT ) ) ;
} else {
date = cursor . getLong ( cursor . getColumnIndexOrThrow ( MmsDatabase . DATE_RECEIVED ) ) ;
}
2017-03-28 12:05:30 -07:00
return new MediaRecord ( attachment , address , date ) ;
2015-10-12 18:25:05 -07:00
}
public Attachment getAttachment ( ) {
2017-03-28 12:05:30 -07:00
return attachment ;
2015-10-12 18:25:05 -07:00
}
public String getContentType ( ) {
2017-03-28 12:05:30 -07:00
return attachment . getContentType ( ) ;
2015-10-12 18:25:05 -07:00
}
2017-07-26 09:59:15 -07:00
public @Nullable Address getAddress ( ) {
2015-10-12 18:25:05 -07:00
return address ;
}
public long getDate ( ) {
return date ;
}
}
}