mirror of
https://github.com/oxen-io/session-android.git
synced 2025-12-03 08:52:20 +00:00
Add sticker support.
No sticker packs are available for use yet, but we now have the latent ability to send and receive.
This commit is contained in:
@@ -28,7 +28,6 @@ import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.text.TextUtils;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
import android.util.Pair;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
@@ -45,9 +44,11 @@ import org.thoughtcrime.securesms.crypto.ClassicDecryptingPartInputStream;
|
||||
import org.thoughtcrime.securesms.crypto.ModernDecryptingPartInputStream;
|
||||
import org.thoughtcrime.securesms.crypto.ModernEncryptingPartOutputStream;
|
||||
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
import org.thoughtcrime.securesms.mms.MediaStream;
|
||||
import org.thoughtcrime.securesms.mms.MmsException;
|
||||
import org.thoughtcrime.securesms.mms.PartAuthority;
|
||||
import org.thoughtcrime.securesms.stickers.StickerLocator;
|
||||
import org.thoughtcrime.securesms.util.BitmapDecodingException;
|
||||
import org.thoughtcrime.securesms.util.BitmapUtil;
|
||||
import org.thoughtcrime.securesms.util.JsonUtils;
|
||||
@@ -93,6 +94,9 @@ public class AttachmentDatabase extends Database {
|
||||
static final String DIGEST = "digest";
|
||||
static final String VOICE_NOTE = "voice_note";
|
||||
static final String QUOTE = "quote";
|
||||
public static final String STICKER_PACK_ID = "sticker_pack_id";
|
||||
public static final String STICKER_PACK_KEY = "sticker_pack_key";
|
||||
static final String STICKER_ID = "sticker_id";
|
||||
static final String FAST_PREFLIGHT_ID = "fast_preflight_id";
|
||||
public static final String DATA_RANDOM = "data_random";
|
||||
private static final String THUMBNAIL_RANDOM = "thumbnail_random";
|
||||
@@ -115,7 +119,7 @@ public class AttachmentDatabase extends Database {
|
||||
SIZE, FILE_NAME, THUMBNAIL, THUMBNAIL_ASPECT_RATIO,
|
||||
UNIQUE_ID, DIGEST, FAST_PREFLIGHT_ID, VOICE_NOTE,
|
||||
QUOTE, DATA_RANDOM, THUMBNAIL_RANDOM, WIDTH, HEIGHT,
|
||||
CAPTION };
|
||||
CAPTION, STICKER_PACK_ID, STICKER_PACK_KEY, STICKER_ID};
|
||||
|
||||
public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + ROW_ID + " INTEGER PRIMARY KEY, " +
|
||||
MMS_ID + " INTEGER, " + "seq" + " INTEGER DEFAULT 0, " +
|
||||
@@ -128,11 +132,13 @@ public class AttachmentDatabase extends Database {
|
||||
UNIQUE_ID + " INTEGER NOT NULL, " + DIGEST + " BLOB, " + FAST_PREFLIGHT_ID + " TEXT, " +
|
||||
VOICE_NOTE + " INTEGER DEFAULT 0, " + DATA_RANDOM + " BLOB, " + THUMBNAIL_RANDOM + " BLOB, " +
|
||||
QUOTE + " INTEGER DEFAULT 0, " + WIDTH + " INTEGER DEFAULT 0, " + HEIGHT + " INTEGER DEFAULT 0, " +
|
||||
CAPTION + " TEXT DEFAULT NULL);";
|
||||
CAPTION + " TEXT DEFAULT NULL, " + STICKER_PACK_ID + " TEXT DEFAULT NULL, " +
|
||||
STICKER_PACK_KEY + " DEFAULT NULL, " + STICKER_ID + " INTEGER DEFAULT -1);";
|
||||
|
||||
public static final String[] CREATE_INDEXS = {
|
||||
"CREATE INDEX IF NOT EXISTS part_mms_id_index ON " + TABLE_NAME + " (" + MMS_ID + ");",
|
||||
"CREATE INDEX IF NOT EXISTS pending_push_index ON " + TABLE_NAME + " (" + TRANSFER_STATE + ");",
|
||||
"CREATE INDEX IF NOT EXISTS part_sticker_pack_id_index ON " + TABLE_NAME + " (" + STICKER_PACK_ID + ");",
|
||||
};
|
||||
|
||||
private final ExecutorService thumbnailExecutor = Util.newSingleThreadedLifoExecutor();
|
||||
@@ -176,6 +182,15 @@ public class AttachmentDatabase extends Database {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean containsStickerPackId(@NonNull String stickerPackId) {
|
||||
String selection = STICKER_PACK_ID + " = ?";
|
||||
String[] args = new String[] { stickerPackId };
|
||||
|
||||
try (Cursor cursor = databaseHelper.getReadableDatabase().query(TABLE_NAME, null, selection, args, null, null, "1")) {
|
||||
return cursor != null && cursor.moveToFirst();
|
||||
}
|
||||
}
|
||||
|
||||
public void setTransferProgressFailed(AttachmentId attachmentId, long mmsId)
|
||||
throws MmsException
|
||||
{
|
||||
@@ -435,7 +450,8 @@ public class AttachmentDatabase extends Database {
|
||||
mediaStream.getWidth(),
|
||||
mediaStream.getHeight(),
|
||||
databaseAttachment.isQuote(),
|
||||
databaseAttachment.getCaption());
|
||||
databaseAttachment.getCaption(),
|
||||
databaseAttachment.getSticker());
|
||||
}
|
||||
|
||||
|
||||
@@ -477,6 +493,26 @@ public class AttachmentDatabase extends Database {
|
||||
notifyConversationListeners(DatabaseFactory.getMmsDatabase(context).getThreadIdForMessage(messageId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns (pack_id, pack_key) pairs that are referenced in attachments but not in the stickers
|
||||
* database.
|
||||
*/
|
||||
public @Nullable Cursor getUnavailableStickerPacks() {
|
||||
String query = "SELECT DISTINCT " + STICKER_PACK_ID + ", " + STICKER_PACK_KEY + " FROM " + TABLE_NAME + " WHERE " + STICKER_PACK_ID + " NOT IN (" +
|
||||
"SELECT DISTINCT " + StickerDatabase.PACK_ID + " FROM " + StickerDatabase.TABLE_NAME +
|
||||
")";
|
||||
|
||||
return databaseHelper.getReadableDatabase().rawQuery(query, null);
|
||||
}
|
||||
|
||||
public boolean hasStickerAttachments() {
|
||||
String selection = STICKER_PACK_ID + " NOT NULL";
|
||||
|
||||
try (Cursor cursor = databaseHelper.getReadableDatabase().query(TABLE_NAME, null, selection, null, null, null, null, "1")) {
|
||||
return cursor != null && cursor.moveToFirst();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
@VisibleForTesting
|
||||
protected @Nullable InputStream getDataStream(AttachmentId attachmentId, String dataType, long offset)
|
||||
@@ -609,7 +645,12 @@ public class AttachmentDatabase extends Database {
|
||||
object.getInt(WIDTH),
|
||||
object.getInt(HEIGHT),
|
||||
object.getInt(QUOTE) == 1,
|
||||
object.getString(CAPTION)));
|
||||
object.getString(CAPTION),
|
||||
object.getInt(STICKER_ID) >= 0
|
||||
? new StickerLocator(object.getString(STICKER_PACK_ID),
|
||||
object.getString(STICKER_PACK_KEY),
|
||||
object.getInt(STICKER_ID))
|
||||
: null));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -633,7 +674,12 @@ public class AttachmentDatabase extends Database {
|
||||
cursor.getInt(cursor.getColumnIndexOrThrow(WIDTH)),
|
||||
cursor.getInt(cursor.getColumnIndexOrThrow(HEIGHT)),
|
||||
cursor.getInt(cursor.getColumnIndexOrThrow(QUOTE)) == 1,
|
||||
cursor.getString(cursor.getColumnIndexOrThrow(CAPTION))));
|
||||
cursor.getString(cursor.getColumnIndexOrThrow(CAPTION)),
|
||||
cursor.getInt(cursor.getColumnIndexOrThrow(STICKER_ID)) >= 0
|
||||
? new StickerLocator(cursor.getString(cursor.getColumnIndexOrThrow(STICKER_PACK_ID)),
|
||||
cursor.getString(cursor.getColumnIndexOrThrow(STICKER_PACK_KEY)),
|
||||
cursor.getInt(cursor.getColumnIndexOrThrow(STICKER_ID)))
|
||||
: null));
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
throw new AssertionError(e);
|
||||
@@ -673,12 +719,19 @@ public class AttachmentDatabase extends Database {
|
||||
contentValues.put(QUOTE, quote);
|
||||
contentValues.put(CAPTION, attachment.getCaption());
|
||||
|
||||
if (attachment.isSticker()) {
|
||||
contentValues.put(STICKER_PACK_ID, attachment.getSticker().getPackId());
|
||||
contentValues.put(STICKER_PACK_KEY, attachment.getSticker().getPackKey());
|
||||
contentValues.put(STICKER_ID, attachment.getSticker().getStickerId());
|
||||
}
|
||||
|
||||
if (dataInfo != null) {
|
||||
contentValues.put(DATA, dataInfo.file.getAbsolutePath());
|
||||
contentValues.put(SIZE, dataInfo.length);
|
||||
contentValues.put(DATA_RANDOM, dataInfo.random);
|
||||
}
|
||||
|
||||
boolean notifyPacks = attachment.isSticker() && !hasStickerAttachments();
|
||||
long rowId = database.insert(TABLE_NAME, null, contentValues);
|
||||
AttachmentId attachmentId = new AttachmentId(rowId, uniqueId);
|
||||
Uri thumbnailUri = attachment.getThumbnailUri();
|
||||
@@ -713,6 +766,10 @@ public class AttachmentDatabase extends Database {
|
||||
}
|
||||
}
|
||||
|
||||
if (notifyPacks) {
|
||||
notifyStickerPackListeners();
|
||||
}
|
||||
|
||||
return attachmentId;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,12 +2,10 @@ package org.thoughtcrime.securesms.database;
|
||||
|
||||
import android.database.ContentObserver;
|
||||
import android.database.Cursor;
|
||||
import android.database.DataSetObserver;
|
||||
import android.database.MatrixCursor;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@@ -23,7 +21,7 @@ import java.util.ListIterator;
|
||||
*
|
||||
* Given that this is cursor-backed, it is effectively immutable.
|
||||
*/
|
||||
public class CursorList<T> implements List<T>, Closeable {
|
||||
public class CursorList<T> implements List<T>, ObservableContent {
|
||||
|
||||
private final Cursor cursor;
|
||||
private final ModelBuilder<T> modelBuilder;
|
||||
@@ -185,10 +183,12 @@ public class CursorList<T> implements List<T>, Closeable {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerContentObserver(@NonNull ContentObserver observer) {
|
||||
cursor.registerContentObserver(observer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregisterContentObserver(@NonNull ContentObserver observer) {
|
||||
cursor.unregisterContentObserver(observer);
|
||||
}
|
||||
|
||||
@@ -178,9 +178,10 @@ public abstract class CursorRecyclerViewAdapter<VH extends RecyclerView.ViewHold
|
||||
|
||||
@Override
|
||||
public final long getItemId(int position) {
|
||||
if (isHeaderPosition(position)) return HEADER_ID;
|
||||
if (isFooterPosition(position)) return FOOTER_ID;
|
||||
if (isFastAccessPosition(position)) return getFastAccessItemId(position);
|
||||
if (isHeaderPosition(position)) return HEADER_ID;
|
||||
else if (isFooterPosition(position)) return FOOTER_ID;
|
||||
else if (isFastAccessPosition(position)) return getFastAccessItemId(position);
|
||||
|
||||
long itemId = getItemId(getCursorAtPositionOrThrow(position));
|
||||
return itemId <= Long.MIN_VALUE + 1 ? itemId + 2 : itemId;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ package org.thoughtcrime.securesms.database;
|
||||
import android.content.Context;
|
||||
import android.database.ContentObserver;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper;
|
||||
@@ -51,6 +50,14 @@ public abstract class Database {
|
||||
context.getContentResolver().notifyChange(DatabaseContentProviders.ConversationList.CONTENT_URI, null);
|
||||
}
|
||||
|
||||
protected void notifyStickerListeners() {
|
||||
context.getContentResolver().notifyChange(DatabaseContentProviders.Sticker.CONTENT_URI, null);
|
||||
}
|
||||
|
||||
protected void notifyStickerPackListeners() {
|
||||
context.getContentResolver().notifyChange(DatabaseContentProviders.StickerPack.CONTENT_URI, null);
|
||||
}
|
||||
|
||||
protected void setNotifyConverationListeners(Cursor cursor, long threadId) {
|
||||
cursor.setNotificationUri(context.getContentResolver(), DatabaseContentProviders.Conversation.getUriForThread(threadId));
|
||||
}
|
||||
@@ -59,6 +66,14 @@ public abstract class Database {
|
||||
cursor.setNotificationUri(context.getContentResolver(), DatabaseContentProviders.ConversationList.CONTENT_URI);
|
||||
}
|
||||
|
||||
protected void setNotifyStickerListeners(Cursor cursor) {
|
||||
cursor.setNotificationUri(context.getContentResolver(), DatabaseContentProviders.Sticker.CONTENT_URI);
|
||||
}
|
||||
|
||||
protected void setNotifyStickerPackListeners(Cursor cursor) {
|
||||
cursor.setNotificationUri(context.getContentResolver(), DatabaseContentProviders.StickerPack.CONTENT_URI);
|
||||
}
|
||||
|
||||
protected void registerAttachmentListeners(@NonNull ContentObserver observer) {
|
||||
context.getContentResolver().registerContentObserver(DatabaseContentProviders.Attachment.CONTENT_URI,
|
||||
true,
|
||||
|
||||
@@ -29,6 +29,14 @@ public class DatabaseContentProviders {
|
||||
public static final Uri CONTENT_URI = Uri.parse("content://org.thoughtcrime.securesms.database.attachment");
|
||||
}
|
||||
|
||||
public static class Sticker extends NoopContentProvider {
|
||||
public static final Uri CONTENT_URI = Uri.parse("content://org.thoughtcrime.securesms.database.sticker");
|
||||
}
|
||||
|
||||
public static class StickerPack extends NoopContentProvider {
|
||||
public static final Uri CONTENT_URI = Uri.parse("content://org.thoughtcrime.securesms.database.stickerpack");
|
||||
}
|
||||
|
||||
private static abstract class NoopContentProvider extends ContentProvider {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -58,6 +58,7 @@ public class DatabaseFactory {
|
||||
private final SessionDatabase sessionDatabase;
|
||||
private final SearchDatabase searchDatabase;
|
||||
private final JobDatabase jobDatabase;
|
||||
private final StickerDatabase stickerDatabase;
|
||||
|
||||
public static DatabaseFactory getInstance(Context context) {
|
||||
synchronized (lock) {
|
||||
@@ -140,6 +141,10 @@ public class DatabaseFactory {
|
||||
return getInstance(context).jobDatabase;
|
||||
}
|
||||
|
||||
public static StickerDatabase getStickerDatabase(Context context) {
|
||||
return getInstance(context).stickerDatabase;
|
||||
}
|
||||
|
||||
public static SQLiteDatabase getBackupDatabase(Context context) {
|
||||
return getInstance(context).databaseHelper.getReadableDatabase();
|
||||
}
|
||||
@@ -174,6 +179,7 @@ public class DatabaseFactory {
|
||||
this.sessionDatabase = new SessionDatabase(context, databaseHelper);
|
||||
this.searchDatabase = new SearchDatabase(context, databaseHelper);
|
||||
this.jobDatabase = new JobDatabase(context, databaseHelper);
|
||||
this.stickerDatabase = new StickerDatabase(context, databaseHelper, attachmentSecret);
|
||||
}
|
||||
|
||||
public void onApplicationLevelUpgrade(@NonNull Context context, @NonNull MasterSecret masterSecret,
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.thoughtcrime.securesms.database;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.ContentObservable;
|
||||
import android.database.ContentObserver;
|
||||
import android.database.Cursor;
|
||||
import android.support.annotation.NonNull;
|
||||
@@ -9,7 +8,6 @@ import android.support.annotation.Nullable;
|
||||
|
||||
import net.sqlcipher.database.SQLiteDatabase;
|
||||
|
||||
import org.thoughtcrime.securesms.attachments.Attachment;
|
||||
import org.thoughtcrime.securesms.attachments.DatabaseAttachment;
|
||||
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper;
|
||||
|
||||
@@ -35,6 +33,9 @@ public class MediaDatabase extends Database {
|
||||
+ AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.WIDTH + ", "
|
||||
+ AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.HEIGHT + ", "
|
||||
+ AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.QUOTE + ", "
|
||||
+ AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.STICKER_PACK_ID + ", "
|
||||
+ AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.STICKER_PACK_KEY + ", "
|
||||
+ AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.STICKER_ID + ", "
|
||||
+ AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.CAPTION + ", "
|
||||
+ AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.NAME + ", "
|
||||
+ MmsDatabase.TABLE_NAME + "." + MmsDatabase.MESSAGE_BOX + ", "
|
||||
@@ -47,7 +48,8 @@ public class MediaDatabase extends Database {
|
||||
+ " FROM " + MmsDatabase.TABLE_NAME
|
||||
+ " WHERE " + MmsDatabase.THREAD_ID + " = ?) AND (%s) AND "
|
||||
+ AttachmentDatabase.DATA + " IS NOT NULL AND "
|
||||
+ AttachmentDatabase.QUOTE + " = 0 "
|
||||
+ AttachmentDatabase.QUOTE + " = 0 AND "
|
||||
+ AttachmentDatabase.STICKER_PACK_ID + " IS NULL "
|
||||
+ "ORDER BY " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.ROW_ID + " DESC";
|
||||
|
||||
private static final String GALLERY_MEDIA_QUERY = String.format(BASE_MEDIA_QUERY, AttachmentDatabase.CONTENT_TYPE + " LIKE 'image/%' OR " + AttachmentDatabase.CONTENT_TYPE + " LIKE 'video/%'");
|
||||
|
||||
@@ -134,7 +134,7 @@ public class MmsDatabase extends MessagingDatabase {
|
||||
"CREATE INDEX IF NOT EXISTS mms_read_and_notified_and_thread_id_index ON " + TABLE_NAME + "(" + READ + "," + NOTIFIED + "," + THREAD_ID + ");",
|
||||
"CREATE INDEX IF NOT EXISTS mms_message_box_index ON " + TABLE_NAME + " (" + MESSAGE_BOX + ");",
|
||||
"CREATE INDEX IF NOT EXISTS mms_date_sent_index ON " + TABLE_NAME + " (" + DATE_SENT + ");",
|
||||
"CREATE INDEX IF NOT EXISTS mms_thread_date_index ON " + TABLE_NAME + " (" + THREAD_ID + ", " + DATE_RECEIVED + ");"
|
||||
"CREATE INDEX IF NOT EXISTS mms_thread_date_index ON " + TABLE_NAME + " (" + THREAD_ID + ", " + DATE_RECEIVED + ");",
|
||||
};
|
||||
|
||||
private static final String[] MMS_PROJECTION = new String[] {
|
||||
@@ -166,7 +166,10 @@ public class MmsDatabase extends MessagingDatabase {
|
||||
"'" + AttachmentDatabase.CONTENT_DISPOSITION + "', " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.CONTENT_DISPOSITION + ", " +
|
||||
"'" + AttachmentDatabase.NAME + "', " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.NAME + ", " +
|
||||
"'" + AttachmentDatabase.TRANSFER_STATE + "', " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.TRANSFER_STATE + ", " +
|
||||
"'" + AttachmentDatabase.CAPTION + "', " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.CAPTION +
|
||||
"'" + AttachmentDatabase.CAPTION + "', " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.CAPTION + ", " +
|
||||
"'" + AttachmentDatabase.STICKER_PACK_ID + "', " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.STICKER_PACK_ID+ ", " +
|
||||
"'" + AttachmentDatabase.STICKER_PACK_KEY + "', " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.STICKER_PACK_KEY + ", " +
|
||||
"'" + AttachmentDatabase.STICKER_ID + "', " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.STICKER_ID +
|
||||
")) AS " + AttachmentDatabase.ATTACHMENT_JSON_ALIAS,
|
||||
};
|
||||
|
||||
@@ -758,7 +761,8 @@ public class MmsDatabase extends MessagingDatabase {
|
||||
databaseAttachment.getWidth(),
|
||||
databaseAttachment.getHeight(),
|
||||
databaseAttachment.isQuote(),
|
||||
databaseAttachment.getCaption()));
|
||||
databaseAttachment.getCaption(),
|
||||
databaseAttachment.getSticker()));
|
||||
}
|
||||
|
||||
return insertMediaMessage(request.getBody(),
|
||||
@@ -1065,6 +1069,8 @@ public class MmsDatabase extends MessagingDatabase {
|
||||
database.delete(TABLE_NAME, ID_WHERE, new String[] {messageId+""});
|
||||
boolean threadDeleted = DatabaseFactory.getThreadDatabase(context).update(threadId, false);
|
||||
notifyConversationListeners(threadId);
|
||||
notifyStickerListeners();
|
||||
notifyStickerPackListeners();
|
||||
return threadDeleted;
|
||||
}
|
||||
|
||||
@@ -1220,6 +1226,18 @@ public class MmsDatabase extends MessagingDatabase {
|
||||
}
|
||||
}
|
||||
|
||||
public void beginTransaction() {
|
||||
databaseHelper.getWritableDatabase().beginTransaction();
|
||||
}
|
||||
|
||||
public void setTransactionSuccessful() {
|
||||
databaseHelper.getWritableDatabase().setTransactionSuccessful();
|
||||
}
|
||||
|
||||
public void endTransaction() {
|
||||
databaseHelper.getWritableDatabase().endTransaction();
|
||||
}
|
||||
|
||||
public Reader readerFor(Cursor cursor) {
|
||||
return new Reader(cursor);
|
||||
}
|
||||
@@ -1448,8 +1466,10 @@ public class MmsDatabase extends MessagingDatabase {
|
||||
}
|
||||
|
||||
private SlideDeck getSlideDeck(@NonNull List<DatabaseAttachment> attachments) {
|
||||
List<? extends Attachment> messageAttachmnets = Stream.of(attachments).filterNot(Attachment::isQuote).toList();
|
||||
return new SlideDeck(context, messageAttachmnets);
|
||||
List<? extends Attachment> messageAttachments = Stream.of(attachments)
|
||||
.filterNot(Attachment::isQuote)
|
||||
.toList();
|
||||
return new SlideDeck(context, messageAttachments);
|
||||
}
|
||||
|
||||
private @Nullable Quote getQuote(@NonNull Cursor cursor) {
|
||||
|
||||
@@ -248,7 +248,10 @@ public class MmsSmsDatabase extends Database {
|
||||
"'" + AttachmentDatabase.CONTENT_DISPOSITION + "', " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.CONTENT_DISPOSITION + ", " +
|
||||
"'" + AttachmentDatabase.NAME + "', " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.NAME + ", " +
|
||||
"'" + AttachmentDatabase.TRANSFER_STATE + "', " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.TRANSFER_STATE + ", " +
|
||||
"'" + AttachmentDatabase.CAPTION + "', " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.CAPTION +
|
||||
"'" + AttachmentDatabase.CAPTION + "', " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.CAPTION + ", " +
|
||||
"'" + AttachmentDatabase.STICKER_PACK_ID + "', " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.STICKER_PACK_ID + ", " +
|
||||
"'" + AttachmentDatabase.STICKER_PACK_KEY + "', " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.STICKER_PACK_KEY + ", " +
|
||||
"'" + AttachmentDatabase.STICKER_ID + "', " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.STICKER_ID +
|
||||
")) AS " + AttachmentDatabase.ATTACHMENT_JSON_ALIAS,
|
||||
SmsDatabase.BODY, MmsSmsColumns.READ, MmsSmsColumns.THREAD_ID,
|
||||
SmsDatabase.TYPE, SmsDatabase.ADDRESS, SmsDatabase.ADDRESS_DEVICE_ID, SmsDatabase.SUBJECT, MmsDatabase.MESSAGE_TYPE,
|
||||
@@ -349,6 +352,9 @@ public class MmsSmsDatabase extends Database {
|
||||
mmsColumnsPresent.add(AttachmentDatabase.WIDTH);
|
||||
mmsColumnsPresent.add(AttachmentDatabase.HEIGHT);
|
||||
mmsColumnsPresent.add(AttachmentDatabase.QUOTE);
|
||||
mmsColumnsPresent.add(AttachmentDatabase.STICKER_PACK_ID);
|
||||
mmsColumnsPresent.add(AttachmentDatabase.STICKER_PACK_KEY);
|
||||
mmsColumnsPresent.add(AttachmentDatabase.STICKER_ID);
|
||||
mmsColumnsPresent.add(AttachmentDatabase.CAPTION);
|
||||
mmsColumnsPresent.add(AttachmentDatabase.CONTENT_DISPOSITION);
|
||||
mmsColumnsPresent.add(AttachmentDatabase.NAME);
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.thoughtcrime.securesms.database;
|
||||
|
||||
import android.database.ContentObserver;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import java.io.Closeable;
|
||||
|
||||
public interface ObservableContent extends Closeable {
|
||||
void registerContentObserver(@NonNull ContentObserver observer);
|
||||
void unregisterContentObserver(@NonNull ContentObserver observer);
|
||||
}
|
||||
481
src/org/thoughtcrime/securesms/database/StickerDatabase.java
Normal file
481
src/org/thoughtcrime/securesms/database/StickerDatabase.java
Normal file
@@ -0,0 +1,481 @@
|
||||
package org.thoughtcrime.securesms.database;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Pair;
|
||||
|
||||
import net.sqlcipher.database.SQLiteDatabase;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
import org.thoughtcrime.securesms.crypto.AttachmentSecret;
|
||||
import org.thoughtcrime.securesms.crypto.ModernDecryptingPartInputStream;
|
||||
import org.thoughtcrime.securesms.crypto.ModernEncryptingPartOutputStream;
|
||||
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper;
|
||||
import org.thoughtcrime.securesms.database.model.IncomingSticker;
|
||||
import org.thoughtcrime.securesms.database.model.StickerPackRecord;
|
||||
import org.thoughtcrime.securesms.database.model.StickerRecord;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri;
|
||||
import org.thoughtcrime.securesms.stickers.BlessedPacks;
|
||||
import org.thoughtcrime.securesms.stickers.StickerPackInstallEvent;
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class StickerDatabase extends Database {
|
||||
|
||||
private static final String TAG = Log.tag(StickerDatabase.class);
|
||||
|
||||
public static final String TABLE_NAME = "sticker";
|
||||
public static final String _ID = "_id";
|
||||
static final String PACK_ID = "pack_id";
|
||||
private static final String PACK_KEY = "pack_key";
|
||||
private static final String PACK_TITLE = "pack_title";
|
||||
private static final String PACK_AUTHOR = "pack_author";
|
||||
private static final String STICKER_ID = "sticker_id";
|
||||
private static final String EMOJI = "emoji";
|
||||
private static final String COVER = "cover";
|
||||
private static final String INSTALLED = "installed";
|
||||
private static final String LAST_USED = "last_used";
|
||||
public static final String FILE_PATH = "file_path";
|
||||
public static final String FILE_LENGTH = "file_length";
|
||||
public static final String FILE_RANDOM = "file_random";
|
||||
|
||||
public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
|
||||
PACK_ID + " TEXT NOT NULL, " +
|
||||
PACK_KEY + " TEXT NOT NULL, " +
|
||||
PACK_TITLE + " TEXT NOT NULL, " +
|
||||
PACK_AUTHOR + " TEXT NOT NULL, " +
|
||||
STICKER_ID + " INTEGER, " +
|
||||
COVER + " INTEGER, " +
|
||||
EMOJI + " TEXT NOT NULL, " +
|
||||
LAST_USED + " INTEGER, " +
|
||||
INSTALLED + " INTEGER," +
|
||||
FILE_PATH + " TEXT NOT NULL, " +
|
||||
FILE_LENGTH + " INTEGER, " +
|
||||
FILE_RANDOM + " BLOB, " +
|
||||
"UNIQUE(" + PACK_ID + ", " + STICKER_ID + ", " + COVER + ") ON CONFLICT IGNORE)";
|
||||
|
||||
public static final String[] CREATE_INDEXES = {
|
||||
"CREATE INDEX IF NOT EXISTS sticker_pack_id_index ON " + TABLE_NAME + " (" + PACK_ID + ");",
|
||||
"CREATE INDEX IF NOT EXISTS sticker_sticker_id_index ON " + TABLE_NAME + " (" + STICKER_ID + ");"
|
||||
};
|
||||
|
||||
private static final String DIRECTORY = "stickers";
|
||||
|
||||
private final AttachmentSecret attachmentSecret;
|
||||
|
||||
public StickerDatabase(Context context, SQLCipherOpenHelper databaseHelper, AttachmentSecret attachmentSecret) {
|
||||
super(context, databaseHelper);
|
||||
this.attachmentSecret = attachmentSecret;
|
||||
}
|
||||
|
||||
public void insertSticker(@NonNull IncomingSticker sticker, @NonNull InputStream dataStream) throws IOException {
|
||||
FileInfo fileInfo = saveStickerImage(dataStream);
|
||||
ContentValues contentValues = new ContentValues();
|
||||
|
||||
contentValues.put(PACK_ID, sticker.getPackId());
|
||||
contentValues.put(PACK_KEY, sticker.getPackKey());
|
||||
contentValues.put(PACK_TITLE, sticker.getPackTitle());
|
||||
contentValues.put(PACK_AUTHOR, sticker.getPackAuthor());
|
||||
contentValues.put(STICKER_ID, sticker.getStickerId());
|
||||
contentValues.put(EMOJI, sticker.getEmoji());
|
||||
contentValues.put(COVER, sticker.isCover() ? 1 : 0);
|
||||
contentValues.put(INSTALLED, sticker.isInstalled() ? 1 : 0);
|
||||
contentValues.put(FILE_PATH, fileInfo.getFile().getAbsolutePath());
|
||||
contentValues.put(FILE_LENGTH, fileInfo.getLength());
|
||||
contentValues.put(FILE_RANDOM, fileInfo.getRandom());
|
||||
|
||||
long id = databaseHelper.getWritableDatabase().insert(TABLE_NAME, null, contentValues);
|
||||
|
||||
if (id > 0) {
|
||||
notifyStickerListeners();
|
||||
|
||||
if (sticker.isCover()) {
|
||||
notifyStickerPackListeners();
|
||||
|
||||
if (sticker.isInstalled()) {
|
||||
broadcastInstallEvent(sticker.getPackId());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public @Nullable StickerRecord getSticker(@NonNull String packId, int stickerId, boolean isCover) {
|
||||
String selection = PACK_ID + " = ? AND " + STICKER_ID + " = ? AND " + COVER + " = ?";
|
||||
String[] args = new String[] { packId, String.valueOf(stickerId), String.valueOf(isCover ? 1 : 0) };
|
||||
|
||||
try (Cursor cursor = databaseHelper.getReadableDatabase().query(TABLE_NAME, null, selection, args, null, null, "1")) {
|
||||
return new StickerRecordReader(cursor).getNext();
|
||||
}
|
||||
}
|
||||
|
||||
public @Nullable StickerPackRecord getStickerPack(@NonNull String packId) {
|
||||
String query = PACK_ID + " = ? AND " + COVER + " = ?";
|
||||
String[] args = new String[] { packId, "1" };
|
||||
|
||||
try (Cursor cursor = databaseHelper.getReadableDatabase().query(TABLE_NAME, null, query, args, null, null, null, "1")) {
|
||||
return new StickerPackRecordReader(cursor).getNext();
|
||||
}
|
||||
}
|
||||
|
||||
public @Nullable Cursor getInstalledStickerPacks() {
|
||||
String selection = COVER + " = ? AND " + INSTALLED + " = ?";
|
||||
String[] args = new String[] { "1", "1" };
|
||||
Cursor cursor = databaseHelper.getReadableDatabase().query(TABLE_NAME, null, selection, args, null, null, null);
|
||||
|
||||
setNotifyStickerPackListeners(cursor);
|
||||
return cursor;
|
||||
}
|
||||
|
||||
public @Nullable Cursor getStickersByEmoji(@NonNull String emoji) {
|
||||
String selection = EMOJI + " LIKE ? AND " + COVER + " = ?";
|
||||
String[] args = new String[] { "%"+emoji+"%", "0" };
|
||||
|
||||
Cursor cursor = databaseHelper.getReadableDatabase().query(TABLE_NAME, null, selection, args, null, null, null);
|
||||
setNotifyStickerListeners(cursor);
|
||||
|
||||
return cursor;
|
||||
}
|
||||
|
||||
public @Nullable Cursor getAllStickerPacks() {
|
||||
return getAllStickerPacks(null);
|
||||
}
|
||||
|
||||
public @Nullable Cursor getAllStickerPacks(@Nullable String limit) {
|
||||
String query = COVER + " = ?";
|
||||
String[] args = new String[] { "1" };
|
||||
Cursor cursor = databaseHelper.getReadableDatabase().query(TABLE_NAME, null, query, args, null, null, null, limit);
|
||||
setNotifyStickerPackListeners(cursor);
|
||||
|
||||
return cursor;
|
||||
}
|
||||
|
||||
public @Nullable Cursor getStickersForPack(@NonNull String packId) {
|
||||
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
||||
String selection = PACK_ID + " = ? AND " + COVER + " = ?";
|
||||
String[] args = new String[] { packId, "0" };
|
||||
|
||||
Cursor cursor = db.query(TABLE_NAME, null, selection, args, null, null, null);
|
||||
setNotifyStickerListeners(cursor);
|
||||
|
||||
return cursor;
|
||||
}
|
||||
|
||||
public @Nullable Cursor getRecentlyUsedStickers(int limit) {
|
||||
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
||||
String selection = LAST_USED + " > ? AND " + COVER + " = ?";
|
||||
String[] args = new String[] { "0", "0" };
|
||||
|
||||
Cursor cursor = db.query(TABLE_NAME, null, selection, args, null, null, LAST_USED + " DESC", String.valueOf(limit));
|
||||
setNotifyStickerListeners(cursor);
|
||||
|
||||
return cursor;
|
||||
}
|
||||
|
||||
public @Nullable InputStream getStickerStream(long rowId) throws IOException {
|
||||
String selection = _ID + " = ?";
|
||||
String[] args = new String[] { String.valueOf(rowId) };
|
||||
|
||||
try (Cursor cursor = databaseHelper.getReadableDatabase().query(TABLE_NAME, null, selection, args, null, null, null)) {
|
||||
if (cursor != null && cursor.moveToNext()) {
|
||||
String path = cursor.getString(cursor.getColumnIndexOrThrow(FILE_PATH));
|
||||
byte[] random = cursor.getBlob(cursor.getColumnIndexOrThrow(FILE_RANDOM));
|
||||
|
||||
if (path != null) {
|
||||
return ModernDecryptingPartInputStream.createFor(attachmentSecret, random, new File(path), 0);
|
||||
} else {
|
||||
Log.w(TAG, "getStickerStream("+rowId+") - No sticker data");
|
||||
}
|
||||
} else {
|
||||
Log.i(TAG, "getStickerStream("+rowId+") - Sticker not found.");
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isPackInstalled(@NonNull String packId) {
|
||||
StickerPackRecord record = getStickerPack(packId);
|
||||
|
||||
return (record != null && record.isInstalled());
|
||||
}
|
||||
|
||||
public boolean isPackAvailableAsReference(@NonNull String packId) {
|
||||
return getStickerPack(packId) != null;
|
||||
}
|
||||
|
||||
public void updateStickerLastUsedTime(long rowId, long lastUsed) {
|
||||
String selection = _ID + " = ?";
|
||||
String[] args = new String[] { String.valueOf(rowId) };
|
||||
ContentValues values = new ContentValues();
|
||||
|
||||
values.put(LAST_USED, lastUsed);
|
||||
|
||||
databaseHelper.getWritableDatabase().update(TABLE_NAME, values, selection, args);
|
||||
|
||||
notifyStickerListeners();
|
||||
notifyStickerPackListeners();
|
||||
}
|
||||
|
||||
public void markPackAsInstalled(@NonNull String packKey) {
|
||||
updatePackInstalled(databaseHelper.getWritableDatabase(), packKey, true);
|
||||
notifyStickerPackListeners();
|
||||
}
|
||||
|
||||
public void deleteOrphanedPacks() {
|
||||
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
||||
String query = "SELECT " + PACK_ID + " FROM " + TABLE_NAME + " WHERE " + INSTALLED + " = ? AND " +
|
||||
PACK_ID + " NOT IN (" +
|
||||
"SELECT DISTINCT " + AttachmentDatabase.STICKER_PACK_ID + " FROM " + AttachmentDatabase.TABLE_NAME + " " +
|
||||
"WHERE " + AttachmentDatabase.STICKER_PACK_ID + " NOT NULL" +
|
||||
")";
|
||||
String[] args = new String[] { "0" };
|
||||
|
||||
db.beginTransaction();
|
||||
|
||||
try {
|
||||
boolean performedDelete = false;
|
||||
|
||||
try (Cursor cursor = db.rawQuery(query, args)) {
|
||||
while (cursor != null && cursor.moveToNext()) {
|
||||
String packId = cursor.getString(cursor.getColumnIndexOrThrow(PACK_ID));
|
||||
|
||||
if (!BlessedPacks.contains(packId)) {
|
||||
deletePack(db, packId);
|
||||
performedDelete = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
db.setTransactionSuccessful();
|
||||
|
||||
if (performedDelete) {
|
||||
notifyStickerPackListeners();
|
||||
notifyStickerListeners();
|
||||
}
|
||||
} finally {
|
||||
db.endTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
public void uninstallPack(@NonNull String packId) {
|
||||
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
||||
|
||||
db.beginTransaction();
|
||||
try {
|
||||
|
||||
updatePackInstalled(db, packId, false);
|
||||
deleteStickersInPackExceptCover(db, packId);
|
||||
|
||||
db.setTransactionSuccessful();
|
||||
notifyStickerPackListeners();
|
||||
notifyStickerListeners();
|
||||
} finally {
|
||||
db.endTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
private void updatePackInstalled(@NonNull SQLiteDatabase db, @NonNull String packId, boolean installed) {
|
||||
StickerPackRecord existing = getStickerPack(packId);
|
||||
|
||||
if (existing != null && existing.isInstalled() == installed) {
|
||||
return;
|
||||
}
|
||||
|
||||
String selection = PACK_ID + " = ?";
|
||||
String[] args = new String[]{ packId };
|
||||
ContentValues values = new ContentValues(1);
|
||||
|
||||
values.put(INSTALLED, installed ? 1 : 0);
|
||||
db.update(TABLE_NAME, values, selection, args);
|
||||
|
||||
if (installed) {
|
||||
broadcastInstallEvent(packId);
|
||||
}
|
||||
}
|
||||
|
||||
private FileInfo saveStickerImage(@NonNull InputStream inputStream) throws IOException {
|
||||
File partsDirectory = context.getDir(DIRECTORY, Context.MODE_PRIVATE);
|
||||
File file = File.createTempFile("sticker", ".mms", partsDirectory);
|
||||
Pair<byte[], OutputStream> out = ModernEncryptingPartOutputStream.createFor(attachmentSecret, file, false);
|
||||
long length = Util.copy(inputStream, out.second);
|
||||
|
||||
return new FileInfo(file, length, out.first);
|
||||
}
|
||||
|
||||
private void deleteSticker(@NonNull SQLiteDatabase db, long rowId, @Nullable String filePath) {
|
||||
String selection = _ID + " = ?";
|
||||
String[] args = new String[] { String.valueOf(rowId) };
|
||||
|
||||
db.delete(TABLE_NAME, selection, args);
|
||||
|
||||
if (!TextUtils.isEmpty(filePath)) {
|
||||
new File(filePath).delete();
|
||||
}
|
||||
}
|
||||
|
||||
private void deletePack(@NonNull SQLiteDatabase db, @NonNull String packId) {
|
||||
String selection = PACK_ID + " = ?";
|
||||
String[] args = new String[] { packId };
|
||||
|
||||
db.delete(TABLE_NAME, selection, args);
|
||||
|
||||
deleteStickersInPack(db, packId);
|
||||
}
|
||||
|
||||
private void deleteStickersInPack(@NonNull SQLiteDatabase db, @NonNull String packId) {
|
||||
String selection = PACK_ID + " = ?";
|
||||
String[] args = new String[] { packId };
|
||||
|
||||
db.beginTransaction();
|
||||
|
||||
try {
|
||||
try (Cursor cursor = db.query(TABLE_NAME, null, selection, args, null, null, null)) {
|
||||
while (cursor != null && cursor.moveToNext()) {
|
||||
String filePath = cursor.getString(cursor.getColumnIndexOrThrow(FILE_PATH));
|
||||
long rowId = cursor.getLong(cursor.getColumnIndexOrThrow(_ID));
|
||||
|
||||
deleteSticker(db, rowId, filePath);
|
||||
}
|
||||
}
|
||||
|
||||
db.setTransactionSuccessful();
|
||||
} finally {
|
||||
db.endTransaction();
|
||||
}
|
||||
|
||||
db.delete(TABLE_NAME, selection, args);
|
||||
}
|
||||
|
||||
private void deleteStickersInPackExceptCover(@NonNull SQLiteDatabase db, @NonNull String packId) {
|
||||
String selection = PACK_ID + " = ? AND " + COVER + " = ?";
|
||||
String[] args = new String[] { packId, "0" };
|
||||
|
||||
db.beginTransaction();
|
||||
|
||||
try {
|
||||
try (Cursor cursor = db.query(TABLE_NAME, null, selection, args, null, null, null)) {
|
||||
while (cursor != null && cursor.moveToNext()) {
|
||||
long rowId = cursor.getLong(cursor.getColumnIndexOrThrow(_ID));
|
||||
String filePath = cursor.getString(cursor.getColumnIndexOrThrow(FILE_PATH));
|
||||
|
||||
deleteSticker(db, rowId, filePath);
|
||||
}
|
||||
}
|
||||
|
||||
db.setTransactionSuccessful();
|
||||
} finally {
|
||||
db.endTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
private void broadcastInstallEvent(@NonNull String packId) {
|
||||
StickerPackRecord pack = getStickerPack(packId);
|
||||
|
||||
if (pack != null) {
|
||||
EventBus.getDefault().postSticky(new StickerPackInstallEvent(new DecryptableUri(pack.getCover().getUri())));
|
||||
}
|
||||
}
|
||||
|
||||
private static final class FileInfo {
|
||||
private final File file;
|
||||
private final long length;
|
||||
private final byte[] random;
|
||||
|
||||
private FileInfo(@NonNull File file, long length, @NonNull byte[] random) {
|
||||
this.file = file;
|
||||
this.length = length;
|
||||
this.random = random;
|
||||
}
|
||||
|
||||
public File getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public long getLength() {
|
||||
return length;
|
||||
}
|
||||
|
||||
public byte[] getRandom() {
|
||||
return random;
|
||||
}
|
||||
}
|
||||
|
||||
public static final class StickerRecordReader implements Closeable {
|
||||
|
||||
private final Cursor cursor;
|
||||
|
||||
public StickerRecordReader(@Nullable Cursor cursor) {
|
||||
this.cursor = cursor;
|
||||
}
|
||||
|
||||
public @Nullable StickerRecord getNext() {
|
||||
if (cursor == null || !cursor.moveToNext()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return getCurrent();
|
||||
}
|
||||
|
||||
public @NonNull StickerRecord getCurrent() {
|
||||
return new StickerRecord(cursor.getLong(cursor.getColumnIndexOrThrow(_ID)),
|
||||
cursor.getString(cursor.getColumnIndexOrThrow(PACK_ID)),
|
||||
cursor.getString(cursor.getColumnIndexOrThrow(PACK_KEY)),
|
||||
cursor.getInt(cursor.getColumnIndexOrThrow(STICKER_ID)),
|
||||
cursor.getString(cursor.getColumnIndexOrThrow(EMOJI)),
|
||||
cursor.getLong(cursor.getColumnIndexOrThrow(FILE_LENGTH)),
|
||||
cursor.getInt(cursor.getColumnIndexOrThrow(COVER)) == 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static final class StickerPackRecordReader implements Closeable {
|
||||
|
||||
private final Cursor cursor;
|
||||
|
||||
public StickerPackRecordReader(@Nullable Cursor cursor) {
|
||||
this.cursor = cursor;
|
||||
}
|
||||
|
||||
public @Nullable StickerPackRecord getNext() {
|
||||
if (cursor == null || !cursor.moveToNext()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return getCurrent();
|
||||
}
|
||||
|
||||
public @NonNull StickerPackRecord getCurrent() {
|
||||
StickerRecord cover = new StickerRecordReader(cursor).getCurrent();
|
||||
|
||||
return new StickerPackRecord(cursor.getString(cursor.getColumnIndexOrThrow(PACK_ID)),
|
||||
cursor.getString(cursor.getColumnIndexOrThrow(PACK_KEY)),
|
||||
cursor.getString(cursor.getColumnIndexOrThrow(PACK_TITLE)),
|
||||
cursor.getString(cursor.getColumnIndexOrThrow(PACK_AUTHOR)),
|
||||
cover,
|
||||
cursor.getInt(cursor.getColumnIndexOrThrow(INSTALLED)) == 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,6 @@ import android.database.MergeCursor;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
|
||||
import com.annimon.stream.Stream;
|
||||
|
||||
@@ -40,6 +39,7 @@ import org.thoughtcrime.securesms.database.model.MediaMmsMessageRecord;
|
||||
import org.thoughtcrime.securesms.database.model.MessageRecord;
|
||||
import org.thoughtcrime.securesms.database.model.MmsMessageRecord;
|
||||
import org.thoughtcrime.securesms.database.model.ThreadRecord;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
import org.thoughtcrime.securesms.mms.Slide;
|
||||
import org.thoughtcrime.securesms.mms.SlideDeck;
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
@@ -601,7 +601,11 @@ public class ThreadDatabase extends Database {
|
||||
SlideDeck slideDeck = ((MediaMmsMessageRecord)record).getSlideDeck();
|
||||
Slide thumbnail = slideDeck.getThumbnailSlide();
|
||||
|
||||
return thumbnail != null ? thumbnail.getThumbnailUri() : null;
|
||||
if (thumbnail != null) {
|
||||
return thumbnail.getThumbnailUri();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private @NonNull String createQuery(@NonNull String where, int limit) {
|
||||
|
||||
@@ -9,10 +9,6 @@ import android.os.SystemClock;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import org.thoughtcrime.securesms.database.Address;
|
||||
import org.thoughtcrime.securesms.database.JobDatabase;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
|
||||
import net.sqlcipher.database.SQLiteDatabase;
|
||||
import net.sqlcipher.database.SQLiteDatabaseHook;
|
||||
import net.sqlcipher.database.SQLiteOpenHelper;
|
||||
@@ -20,11 +16,13 @@ import net.sqlcipher.database.SQLiteOpenHelper;
|
||||
import org.thoughtcrime.securesms.ApplicationContext;
|
||||
import org.thoughtcrime.securesms.crypto.DatabaseSecret;
|
||||
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
||||
import org.thoughtcrime.securesms.database.Address;
|
||||
import org.thoughtcrime.securesms.database.AttachmentDatabase;
|
||||
import org.thoughtcrime.securesms.database.DraftDatabase;
|
||||
import org.thoughtcrime.securesms.database.GroupDatabase;
|
||||
import org.thoughtcrime.securesms.database.GroupReceiptDatabase;
|
||||
import org.thoughtcrime.securesms.database.IdentityDatabase;
|
||||
import org.thoughtcrime.securesms.database.JobDatabase;
|
||||
import org.thoughtcrime.securesms.database.MmsDatabase;
|
||||
import org.thoughtcrime.securesms.database.OneTimePreKeyDatabase;
|
||||
import org.thoughtcrime.securesms.database.PushDatabase;
|
||||
@@ -33,8 +31,10 @@ import org.thoughtcrime.securesms.database.SearchDatabase;
|
||||
import org.thoughtcrime.securesms.database.SessionDatabase;
|
||||
import org.thoughtcrime.securesms.database.SignedPreKeyDatabase;
|
||||
import org.thoughtcrime.securesms.database.SmsDatabase;
|
||||
import org.thoughtcrime.securesms.database.StickerDatabase;
|
||||
import org.thoughtcrime.securesms.database.ThreadDatabase;
|
||||
import org.thoughtcrime.securesms.jobs.RefreshPreKeysJob;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
import org.thoughtcrime.securesms.notifications.NotificationChannels;
|
||||
import org.thoughtcrime.securesms.service.KeyCachingService;
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
||||
@@ -65,8 +65,9 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
|
||||
private static final int SELF_ATTACHMENT_CLEANUP = 18;
|
||||
private static final int RECIPIENT_FORCE_SMS_SELECTION = 19;
|
||||
private static final int JOBMANAGER_STRIKES_BACK = 20;
|
||||
private static final int STICKERS = 21;
|
||||
|
||||
private static final int DATABASE_VERSION = 20;
|
||||
private static final int DATABASE_VERSION = 21;
|
||||
private static final String DATABASE_NAME = "signal.db";
|
||||
|
||||
private final Context context;
|
||||
@@ -106,12 +107,9 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
|
||||
db.execSQL(OneTimePreKeyDatabase.CREATE_TABLE);
|
||||
db.execSQL(SignedPreKeyDatabase.CREATE_TABLE);
|
||||
db.execSQL(SessionDatabase.CREATE_TABLE);
|
||||
for (String sql : SearchDatabase.CREATE_TABLE) {
|
||||
db.execSQL(sql);
|
||||
}
|
||||
for (String sql : JobDatabase.CREATE_TABLE) {
|
||||
db.execSQL(sql);
|
||||
}
|
||||
db.execSQL(StickerDatabase.CREATE_TABLE);
|
||||
executeStatements(db, SearchDatabase.CREATE_TABLE);
|
||||
executeStatements(db, JobDatabase.CREATE_TABLE);
|
||||
|
||||
executeStatements(db, SmsDatabase.CREATE_INDEXS);
|
||||
executeStatements(db, MmsDatabase.CREATE_INDEXS);
|
||||
@@ -120,6 +118,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
|
||||
executeStatements(db, DraftDatabase.CREATE_INDEXS);
|
||||
executeStatements(db, GroupDatabase.CREATE_INDEXS);
|
||||
executeStatements(db, GroupReceiptDatabase.CREATE_INDEXES);
|
||||
executeStatements(db, StickerDatabase.CREATE_INDEXES);
|
||||
|
||||
if (context.getDatabasePath(ClassicOpenHelper.NAME).exists()) {
|
||||
ClassicOpenHelper legacyHelper = new ClassicOpenHelper(context);
|
||||
@@ -438,6 +437,31 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
|
||||
"UNIQUE(job_spec_id, depends_on_job_spec_id))");
|
||||
}
|
||||
|
||||
if (oldVersion < STICKERS) {
|
||||
db.execSQL("CREATE TABLE sticker (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
|
||||
"pack_id TEXT NOT NULL, " +
|
||||
"pack_key TEXT NOT NULL, " +
|
||||
"pack_title TEXT NOT NULL, " +
|
||||
"pack_author TEXT NOT NULL, " +
|
||||
"sticker_id INTEGER, " +
|
||||
"cover INTEGER, " +
|
||||
"emoji TEXT NOT NULL, " +
|
||||
"last_used INTEGER, " +
|
||||
"installed INTEGER," +
|
||||
"file_path TEXT NOT NULL, " +
|
||||
"file_length INTEGER, " +
|
||||
"file_random BLOB, " +
|
||||
"UNIQUE(pack_id, sticker_id, cover) ON CONFLICT IGNORE)");
|
||||
|
||||
db.execSQL("CREATE INDEX IF NOT EXISTS sticker_pack_id_index ON sticker (pack_id);");
|
||||
db.execSQL("CREATE INDEX IF NOT EXISTS sticker_sticker_id_index ON sticker (sticker_id);");
|
||||
|
||||
db.execSQL("ALTER TABLE part ADD COLUMN sticker_pack_id TEXT");
|
||||
db.execSQL("ALTER TABLE part ADD COLUMN sticker_pack_key TEXT");
|
||||
db.execSQL("ALTER TABLE part ADD COLUMN sticker_id INTEGER DEFAULT -1");
|
||||
db.execSQL("CREATE INDEX IF NOT EXISTS part_sticker_pack_id_index ON part (sticker_pack_id)");
|
||||
}
|
||||
|
||||
db.setTransactionSuccessful();
|
||||
} finally {
|
||||
db.endTransaction();
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package org.thoughtcrime.securesms.database.model;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
public class IncomingSticker {
|
||||
|
||||
private final String packKey;
|
||||
private final String packId;
|
||||
private final String packTitle;
|
||||
private final String packAuthor;
|
||||
private final int stickerId;
|
||||
private final String emoji;
|
||||
private final boolean isCover;
|
||||
private final boolean isInstalled;
|
||||
|
||||
public IncomingSticker(@NonNull String packId,
|
||||
@NonNull String packKey,
|
||||
@NonNull String packTitle,
|
||||
@NonNull String packAuthor,
|
||||
int stickerId,
|
||||
@NonNull String emoji,
|
||||
boolean isCover,
|
||||
boolean isInstalled)
|
||||
{
|
||||
this.packId = packId;
|
||||
this.packKey = packKey;
|
||||
this.packTitle = packTitle;
|
||||
this.packAuthor = packAuthor;
|
||||
this.stickerId = stickerId;
|
||||
this.emoji = emoji;
|
||||
this.isCover = isCover;
|
||||
this.isInstalled = isInstalled;
|
||||
}
|
||||
|
||||
public @NonNull String getPackKey() {
|
||||
return packKey;
|
||||
}
|
||||
|
||||
public @NonNull String getPackId() {
|
||||
return packId;
|
||||
}
|
||||
|
||||
public @NonNull String getPackTitle() {
|
||||
return packTitle;
|
||||
}
|
||||
|
||||
public @NonNull String getPackAuthor() {
|
||||
return packAuthor;
|
||||
}
|
||||
|
||||
public int getStickerId() {
|
||||
return stickerId;
|
||||
}
|
||||
|
||||
public @NonNull String getEmoji() {
|
||||
return emoji;
|
||||
}
|
||||
|
||||
public boolean isCover() {
|
||||
return isCover;
|
||||
}
|
||||
|
||||
public boolean isInstalled() {
|
||||
return isInstalled;
|
||||
}
|
||||
}
|
||||
@@ -55,8 +55,8 @@ public class MediaMmsMessageRecord extends MmsMessageRecord {
|
||||
List<IdentityKeyMismatch> mismatches,
|
||||
List<NetworkFailure> failures, int subscriptionId,
|
||||
long expiresIn, long expireStarted, int readReceiptCount,
|
||||
@Nullable Quote quote, @Nullable List<Contact> contacts,
|
||||
@Nullable List<LinkPreview> linkPreviews, boolean unidentified)
|
||||
@Nullable Quote quote, @NonNull List<Contact> contacts,
|
||||
@NonNull List<LinkPreview> linkPreviews, boolean unidentified)
|
||||
{
|
||||
super(id, body, conversationRecipient, individualRecipient, recipientDeviceId, dateSent,
|
||||
dateReceived, threadId, Status.STATUS_NONE, deliveryReceiptCount, mailbox, mismatches, failures,
|
||||
|
||||
40
src/org/thoughtcrime/securesms/database/model/Sticker.java
Normal file
40
src/org/thoughtcrime/securesms/database/model/Sticker.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package org.thoughtcrime.securesms.database.model;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import org.thoughtcrime.securesms.attachments.Attachment;
|
||||
|
||||
public class Sticker {
|
||||
|
||||
private final String packId;
|
||||
private final String packKey;
|
||||
private final int stickerId;
|
||||
private final Attachment attachment;
|
||||
|
||||
public Sticker(@NonNull String packId,
|
||||
@NonNull String packKey,
|
||||
int stickerId,
|
||||
@NonNull Attachment attachment)
|
||||
{
|
||||
this.packId = packId;
|
||||
this.packKey = packKey;
|
||||
this.stickerId = stickerId;
|
||||
this.attachment = attachment;
|
||||
}
|
||||
|
||||
public @NonNull String getPackId() {
|
||||
return packId;
|
||||
}
|
||||
|
||||
public @NonNull String getPackKey() {
|
||||
return packKey;
|
||||
}
|
||||
|
||||
public int getStickerId() {
|
||||
return stickerId;
|
||||
}
|
||||
|
||||
public @NonNull Attachment getAttachment() {
|
||||
return attachment;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package org.thoughtcrime.securesms.database.model;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import org.whispersystems.libsignal.util.guava.Optional;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Represents a record for a sticker pack in the {@link org.thoughtcrime.securesms.database.StickerDatabase}.
|
||||
*/
|
||||
public final class StickerPackRecord {
|
||||
|
||||
private final String packId;
|
||||
private final String packKey;
|
||||
private final Optional<String> title;
|
||||
private final Optional<String> author;
|
||||
private final StickerRecord cover;
|
||||
private final boolean installed;
|
||||
|
||||
public StickerPackRecord(@NonNull String packId,
|
||||
@NonNull String packKey,
|
||||
@NonNull String title,
|
||||
@NonNull String author,
|
||||
@NonNull StickerRecord cover,
|
||||
boolean installed)
|
||||
{
|
||||
this.packId = packId;
|
||||
this.packKey = packKey;
|
||||
this.title = TextUtils.isEmpty(title) ? Optional.absent() : Optional.of(title);
|
||||
this.author = TextUtils.isEmpty(author) ? Optional.absent() : Optional.of(author);
|
||||
this.cover = cover;
|
||||
this.installed = installed;
|
||||
}
|
||||
|
||||
public @NonNull String getPackId() {
|
||||
return packId;
|
||||
}
|
||||
|
||||
public @NonNull String getPackKey() {
|
||||
return packKey;
|
||||
}
|
||||
|
||||
public @NonNull Optional<String> getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public @NonNull Optional<String> getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public @NonNull StickerRecord getCover() {
|
||||
return cover;
|
||||
}
|
||||
|
||||
public boolean isInstalled() {
|
||||
return installed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
StickerPackRecord record = (StickerPackRecord) o;
|
||||
return installed == record.installed &&
|
||||
packId.equals(record.packId) &&
|
||||
packKey.equals(record.packKey) &&
|
||||
title.equals(record.title) &&
|
||||
author.equals(record.author) &&
|
||||
cover.equals(record.cover);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(packId, packKey, title, author, cover, installed);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package org.thoughtcrime.securesms.database.model;
|
||||
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import org.thoughtcrime.securesms.mms.PartAuthority;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Represents a record for a sticker pack in the {@link org.thoughtcrime.securesms.database.StickerDatabase}.
|
||||
*/
|
||||
public final class StickerRecord {
|
||||
|
||||
private final long rowId;
|
||||
private final String packId;
|
||||
private final String packKey;
|
||||
private final int stickerId;
|
||||
private final String emoji;
|
||||
private final long size;
|
||||
private final boolean isCover;
|
||||
|
||||
public StickerRecord(long rowId,
|
||||
@NonNull String packId,
|
||||
@NonNull String packKey,
|
||||
int stickerId,
|
||||
@NonNull String emoji,
|
||||
long size,
|
||||
boolean isCover)
|
||||
{
|
||||
this.rowId = rowId;
|
||||
this.packId = packId;
|
||||
this.packKey = packKey;
|
||||
this.stickerId = stickerId;
|
||||
this.emoji = emoji;
|
||||
this.size = size;
|
||||
this.isCover = isCover;
|
||||
}
|
||||
|
||||
public long getRowId() {
|
||||
return rowId;
|
||||
}
|
||||
|
||||
public @NonNull String getPackId() {
|
||||
return packId;
|
||||
}
|
||||
|
||||
public @NonNull String getPackKey() {
|
||||
return packKey;
|
||||
}
|
||||
|
||||
public int getStickerId() {
|
||||
return stickerId;
|
||||
}
|
||||
|
||||
public @NonNull Uri getUri() {
|
||||
return PartAuthority.getStickerUri(rowId);
|
||||
}
|
||||
|
||||
public @NonNull String getEmoji() {
|
||||
return emoji;
|
||||
}
|
||||
|
||||
public long getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public boolean isCover() {
|
||||
return isCover;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
StickerRecord that = (StickerRecord) o;
|
||||
return rowId == that.rowId &&
|
||||
stickerId == that.stickerId &&
|
||||
size == that.size &&
|
||||
isCover == that.isCover &&
|
||||
packId.equals(that.packId) &&
|
||||
packKey.equals(that.packKey) &&
|
||||
emoji.equals(that.emoji);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(rowId, packId, packKey, stickerId, emoji, size, isCover);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user