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;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.attachments.Attachment;
|
|
|
|
import org.thoughtcrime.securesms.attachments.AttachmentId;
|
|
|
|
import org.thoughtcrime.securesms.attachments.DatabaseAttachment;
|
|
|
|
|
|
|
|
public class ImageDatabase extends Database {
|
|
|
|
|
|
|
|
private final static String IMAGES_QUERY = "SELECT " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.ROW_ID + ", "
|
|
|
|
+ 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 + ", "
|
|
|
|
+ AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.DATA + ", "
|
2016-12-11 13:37:27 -08:00
|
|
|
+ AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.THUMBNAIL + ", "
|
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
|
|
|
|
+ " WHERE " + MmsDatabase.THREAD_ID + " = ?) AND "
|
2015-11-13 00:07:05 +01:00
|
|
|
+ AttachmentDatabase.CONTENT_TYPE + " LIKE 'image/%' AND "
|
|
|
|
+ AttachmentDatabase.DATA + " IS NOT NULL "
|
2015-10-12 18:25:05 -07:00
|
|
|
+ "ORDER BY " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.ROW_ID + " DESC";
|
|
|
|
|
|
|
|
public ImageDatabase(Context context, SQLiteOpenHelper databaseHelper) {
|
|
|
|
super(context, databaseHelper);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Cursor getImagesForThread(long threadId) {
|
|
|
|
SQLiteDatabase database = databaseHelper.getReadableDatabase();
|
|
|
|
Cursor cursor = database.rawQuery(IMAGES_QUERY, new String[]{threadId+""});
|
|
|
|
setNotifyConverationListeners(cursor, threadId);
|
|
|
|
return cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class ImageRecord {
|
|
|
|
private final AttachmentId attachmentId;
|
|
|
|
private final long mmsId;
|
|
|
|
private final boolean hasData;
|
2016-12-11 13:37:27 -08:00
|
|
|
private final boolean hasThumbnail;
|
2015-10-12 18:25:05 -07:00
|
|
|
private final String contentType;
|
|
|
|
private final String address;
|
|
|
|
private final long date;
|
|
|
|
private final int transferState;
|
|
|
|
private final long size;
|
|
|
|
|
2016-12-11 13:37:27 -08:00
|
|
|
private ImageRecord(AttachmentId attachmentId, long mmsId,
|
|
|
|
boolean hasData, boolean hasThumbnail,
|
2015-10-12 18:25:05 -07:00
|
|
|
String contentType, String address, long date,
|
|
|
|
int transferState, long size)
|
|
|
|
{
|
|
|
|
this.attachmentId = attachmentId;
|
|
|
|
this.mmsId = mmsId;
|
|
|
|
this.hasData = hasData;
|
2016-12-11 13:37:27 -08:00
|
|
|
this.hasThumbnail = hasThumbnail;
|
2015-10-12 18:25:05 -07:00
|
|
|
this.contentType = contentType;
|
|
|
|
this.address = address;
|
|
|
|
this.date = date;
|
|
|
|
this.transferState = transferState;
|
|
|
|
this.size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ImageRecord from(Cursor cursor) {
|
|
|
|
AttachmentId attachmentId = new AttachmentId(cursor.getLong(cursor.getColumnIndexOrThrow(AttachmentDatabase.ROW_ID)),
|
|
|
|
cursor.getLong(cursor.getColumnIndexOrThrow(AttachmentDatabase.UNIQUE_ID)));
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
return new ImageRecord(attachmentId,
|
|
|
|
cursor.getLong(cursor.getColumnIndexOrThrow(AttachmentDatabase.MMS_ID)),
|
|
|
|
!cursor.isNull(cursor.getColumnIndexOrThrow(AttachmentDatabase.DATA)),
|
2016-12-11 13:37:27 -08:00
|
|
|
!cursor.isNull(cursor.getColumnIndexOrThrow(AttachmentDatabase.THUMBNAIL)),
|
2015-10-12 18:25:05 -07:00
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(AttachmentDatabase.CONTENT_TYPE)),
|
|
|
|
cursor.getString(cursor.getColumnIndexOrThrow(MmsDatabase.ADDRESS)),
|
2016-02-15 02:22:16 +01:00
|
|
|
date,
|
2015-10-12 18:25:05 -07:00
|
|
|
cursor.getInt(cursor.getColumnIndexOrThrow(AttachmentDatabase.TRANSFER_STATE)),
|
|
|
|
cursor.getLong(cursor.getColumnIndexOrThrow(AttachmentDatabase.SIZE)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public Attachment getAttachment() {
|
2016-12-11 13:37:27 -08:00
|
|
|
return new DatabaseAttachment(attachmentId, mmsId, hasData, hasThumbnail, contentType, transferState, size, null, null, null);
|
2015-10-12 18:25:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getContentType() {
|
|
|
|
return contentType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getAddress() {
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getDate() {
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|