Skip to content

Commit

Permalink
Sort search results in inverted timestamp order (fix #1223)
Browse files Browse the repository at this point in the history
Signed-off-by: Daniele Ricci <[email protected]>
  • Loading branch information
daniele-athome committed Sep 14, 2018
1 parent 20d668e commit 66fafa0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
26 changes: 25 additions & 1 deletion app/src/main/java/org/kontalk/provider/MessagesProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public class MessagesProvider extends ContentProvider {
@VisibleForTesting
static class DatabaseHelper extends SQLiteOpenHelper {
@VisibleForTesting
static final int DATABASE_VERSION = 18;
static final int DATABASE_VERSION = 19;
@VisibleForTesting
static final String DATABASE_NAME = "messages.db";

Expand Down Expand Up @@ -234,7 +234,9 @@ static class DatabaseHelper extends SQLiteOpenHelper {
/** This table will contain every text message to speed-up full text searches. */
private static final String SCHEMA_FULLTEXT =
"CREATE VIRTUAL TABLE " + TABLE_FULLTEXT + " USING fts3 (" +
"msg_id INTEGER PRIMARY KEY," +
"thread_id INTEGER NOT NULL, " +
"timestamp INTEGER NOT NULL," +
"content TEXT" +
")";

Expand Down Expand Up @@ -433,6 +435,21 @@ static class DatabaseHelper extends SQLiteOpenHelper {
"ALTER TABLE threads ADD COLUMN archived INTEGER NOT NULL DEFAULT 0",
};

private static final String[] SCHEMA_UPGRADE_V18 = {
"CREATE VIRTUAL TABLE fulltext_timestamp USING fts3 (" +
"msg_id INTEGER PRIMARY KEY," +
"thread_id INTEGER NOT NULL," +
"timestamp INTEGER NOT NULL," +
"content TEXT)",
"INSERT INTO fulltext_timestamp" +
" SELECT _id, thread_id, timestamp, CAST(body_content AS TEXT)" +
" FROM messages" +
" WHERE body_mime = 'text/plain' AND" +
" encrypted = 0",
"DROP TABLE fulltext",
"ALTER TABLE fulltext_timestamp RENAME TO fulltext",
};

/** If true, fail all operations. */
private boolean mLocked;

Expand Down Expand Up @@ -513,6 +530,11 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(sql);
}
// fall through
case 18:
for (String sql : SCHEMA_UPGRADE_V18) {
db.execSQL(sql);
}
// fall through
}
}

Expand Down Expand Up @@ -1738,7 +1760,9 @@ public static void unlockForImport(Context context) {
threadsProjectionMap.put(Groups.MEMBERSHIP, Groups.MEMBERSHIP);

fulltextProjectionMap = new HashMap<>();
fulltextProjectionMap.put(Fulltext._ID, Fulltext._ID);
fulltextProjectionMap.put(Fulltext.THREAD_ID, Fulltext.THREAD_ID);
fulltextProjectionMap.put(Fulltext.TIMESTAMP, Fulltext.TIMESTAMP);
fulltextProjectionMap.put(Fulltext.CONTENT, Fulltext.CONTENT);

groupsProjectionMap = new HashMap<>();
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/org/kontalk/provider/MyMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ private Fulltext() {}
public static final Uri CONTENT_URI = Uri.parse("content://"
+ MessagesProvider.AUTHORITY + "/fulltext");

public static final String _ID = "rowid";
public static final String _ID = "msg_id";
public static final String THREAD_ID = "thread_id";
public static final String TIMESTAMP = "timestamp";
public static final String CONTENT = "content";

public static final String DEFAULT_SORT_ORDER = "timestamp DESC";
}

private static final String ITEM_TYPE = BuildConfig.APPLICATION_ID + ".message";
Expand Down

0 comments on commit 66fafa0

Please sign in to comment.