mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-21 00:59:49 +01:00
Add Emoji Search, Sticker Search, and GIF Keyboard.
Co-authored-by: Alex Hart <alex@signal.org> Co-authored-by: Cody Henthorne <cody@signal.org> Co-authored-by: Greyson Parrelli<greyson@signal.org>
This commit is contained in:
committed by
Cody Henthorne
parent
66c3b1388a
commit
08e86b8c82
@@ -60,11 +60,12 @@ public class DatabaseFactory {
|
||||
private final SessionDatabase sessionDatabase;
|
||||
private final SearchDatabase searchDatabase;
|
||||
private final StickerDatabase stickerDatabase;
|
||||
private final UnknownStorageIdDatabase storageIdDatabase ;
|
||||
private final UnknownStorageIdDatabase storageIdDatabase;
|
||||
private final RemappedRecordsDatabase remappedRecordsDatabase;
|
||||
private final MentionDatabase mentionDatabase;
|
||||
private final PaymentDatabase paymentDatabase;
|
||||
private final ChatColorsDatabase chatColorsDatabase;
|
||||
private final EmojiSearchDatabase emojiSearchDatabase;
|
||||
|
||||
public static DatabaseFactory getInstance(Context context) {
|
||||
if (instance == null) {
|
||||
@@ -171,6 +172,10 @@ public class DatabaseFactory {
|
||||
return getInstance(context).paymentDatabase;
|
||||
}
|
||||
|
||||
public static EmojiSearchDatabase getEmojiSearchDatabase(Context context) {
|
||||
return getInstance(context).emojiSearchDatabase;
|
||||
}
|
||||
|
||||
public static SQLiteDatabase getBackupDatabase(Context context) {
|
||||
return getInstance(context).databaseHelper.getReadableDatabase().getSqlCipherDatabase();
|
||||
}
|
||||
@@ -229,6 +234,7 @@ public class DatabaseFactory {
|
||||
this.mentionDatabase = new MentionDatabase(context, databaseHelper);
|
||||
this.paymentDatabase = new PaymentDatabase(context, databaseHelper);
|
||||
this.chatColorsDatabase = new ChatColorsDatabase(context, databaseHelper);
|
||||
this.emojiSearchDatabase = new EmojiSearchDatabase(context, databaseHelper);
|
||||
}
|
||||
|
||||
public void onApplicationLevelUpgrade(@NonNull Context context, @NonNull MasterSecret masterSecret,
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package org.thoughtcrime.securesms.database;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.annimon.stream.Stream;
|
||||
|
||||
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper;
|
||||
import org.thoughtcrime.securesms.database.model.EmojiSearchData;
|
||||
import org.thoughtcrime.securesms.util.CursorUtil;
|
||||
import org.thoughtcrime.securesms.util.FtsUtil;
|
||||
import org.thoughtcrime.securesms.util.SqlUtil;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Contains all info necessary for full-text search of emoji tags.
|
||||
*/
|
||||
public class EmojiSearchDatabase extends Database {
|
||||
|
||||
public static final String TABLE_NAME = "emoji_search";
|
||||
|
||||
public static final String LABEL = "label";
|
||||
public static final String EMOJI = "emoji";
|
||||
|
||||
public static final String CREATE_TABLE = "CREATE VIRTUAL TABLE " + TABLE_NAME + " USING fts5(" + LABEL + ", " + EMOJI + " UNINDEXED)";
|
||||
|
||||
public EmojiSearchDatabase(@NonNull Context context, @NonNull SQLCipherOpenHelper databaseHelper) {
|
||||
super(context, databaseHelper);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param query A search query. Doesn't need any special formatted -- it'll be sanitized.
|
||||
* @return A list of emoji that are related to the search term, ordered by relevance.
|
||||
*/
|
||||
public @NonNull List<String> query(@NonNull String query, int limit) {
|
||||
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
||||
String matchString = FtsUtil.createPrefixMatchString(query);
|
||||
List<String> results = new LinkedList<>();
|
||||
|
||||
if (TextUtils.isEmpty(matchString)) {
|
||||
return results;
|
||||
}
|
||||
|
||||
String[] projection = new String[] { EMOJI };
|
||||
String selection = LABEL + " MATCH (?)";
|
||||
String[] args = SqlUtil.buildArgs(matchString);
|
||||
|
||||
try (Cursor cursor = db.query(true, TABLE_NAME, projection, selection, args, null, null,"rank", String.valueOf(limit))) {
|
||||
while (cursor.moveToNext()) {
|
||||
results.add(CursorUtil.requireString(cursor, EMOJI));
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the content of the current search index and replaces it with the new one.
|
||||
*/
|
||||
public void setSearchIndex(@NonNull List<EmojiSearchData> searchIndex) {
|
||||
SQLiteDatabase db = databaseHelper.getReadableDatabase();
|
||||
|
||||
db.beginTransaction();
|
||||
try {
|
||||
db.delete(TABLE_NAME, null, null);
|
||||
|
||||
for (EmojiSearchData searchData : searchIndex) {
|
||||
for (String label : searchData.getTags()) {
|
||||
ContentValues values = new ContentValues(2);
|
||||
values.put(LABEL, label);
|
||||
values.put(EMOJI, searchData.getEmoji());
|
||||
db.insert(TABLE_NAME, null, values);
|
||||
}
|
||||
}
|
||||
db.setTransactionSuccessful();
|
||||
} finally {
|
||||
db.endTransaction();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.thoughtcrime.securesms.crypto.MasterSecret;
|
||||
import org.thoughtcrime.securesms.database.AttachmentDatabase;
|
||||
import org.thoughtcrime.securesms.database.ChatColorsDatabase;
|
||||
import org.thoughtcrime.securesms.database.DraftDatabase;
|
||||
import org.thoughtcrime.securesms.database.EmojiSearchDatabase;
|
||||
import org.thoughtcrime.securesms.database.GroupDatabase;
|
||||
import org.thoughtcrime.securesms.database.GroupReceiptDatabase;
|
||||
import org.thoughtcrime.securesms.database.IdentityDatabase;
|
||||
@@ -190,8 +191,9 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper implements SignalDatab
|
||||
private static final int SERVER_GUID = 99;
|
||||
private static final int CHAT_COLORS = 100;
|
||||
private static final int AVATAR_COLORS = 101;
|
||||
private static final int EMOJI_SEARCH = 102;
|
||||
|
||||
private static final int DATABASE_VERSION = 101;
|
||||
private static final int DATABASE_VERSION = 102;
|
||||
private static final String DATABASE_NAME = "signal.db";
|
||||
|
||||
private final Context context;
|
||||
@@ -224,6 +226,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper implements SignalDatab
|
||||
db.execSQL(MentionDatabase.CREATE_TABLE);
|
||||
db.execSQL(PaymentDatabase.CREATE_TABLE);
|
||||
db.execSQL(ChatColorsDatabase.CREATE_TABLE);
|
||||
db.execSQL(EmojiSearchDatabase.CREATE_TABLE);
|
||||
executeStatements(db, SearchDatabase.CREATE_TABLE);
|
||||
executeStatements(db, RemappedRecordsDatabase.CREATE_TABLE);
|
||||
|
||||
@@ -1506,6 +1509,10 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper implements SignalDatab
|
||||
}
|
||||
}
|
||||
|
||||
if (oldVersion < EMOJI_SEARCH) {
|
||||
db.execSQL("CREATE VIRTUAL TABLE emoji_search USING fts5(label, emoji UNINDEXED)");
|
||||
}
|
||||
|
||||
db.setTransactionSuccessful();
|
||||
} finally {
|
||||
db.endTransaction();
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package org.thoughtcrime.securesms.database.model;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Ties together an emoji with it's associated search tags.
|
||||
*/
|
||||
public final class EmojiSearchData {
|
||||
@JsonProperty
|
||||
private String emoji;
|
||||
|
||||
@JsonProperty
|
||||
private List<String> tags;
|
||||
|
||||
public EmojiSearchData() {}
|
||||
|
||||
public @NonNull String getEmoji() {
|
||||
return emoji;
|
||||
}
|
||||
|
||||
public @NonNull List<String> getTags() {
|
||||
return tags;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user