1package eu.siacs.conversations.persistance;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.DatabaseUtils;
7import android.database.sqlite.SQLiteDatabase;
8import android.database.sqlite.SQLiteOpenHelper;
9import android.os.Environment;
10import android.os.SystemClock;
11import android.util.Base64;
12import android.util.Log;
13
14import com.google.common.base.Stopwatch;
15
16import org.json.JSONException;
17import org.json.JSONObject;
18import org.whispersystems.libsignal.IdentityKey;
19import org.whispersystems.libsignal.IdentityKeyPair;
20import org.whispersystems.libsignal.InvalidKeyException;
21import org.whispersystems.libsignal.SignalProtocolAddress;
22import org.whispersystems.libsignal.state.PreKeyRecord;
23import org.whispersystems.libsignal.state.SessionRecord;
24import org.whispersystems.libsignal.state.SignedPreKeyRecord;
25
26import java.io.ByteArrayInputStream;
27import java.io.File;
28import java.io.IOException;
29import java.security.cert.CertificateEncodingException;
30import java.security.cert.CertificateException;
31import java.security.cert.CertificateFactory;
32import java.security.cert.X509Certificate;
33import java.util.ArrayList;
34import java.util.HashMap;
35import java.util.HashSet;
36import java.util.List;
37import java.util.Map;
38import java.util.Set;
39import java.util.UUID;
40import java.util.concurrent.CopyOnWriteArrayList;
41
42import io.ipfs.cid.Cid;
43
44import eu.siacs.conversations.Config;
45import eu.siacs.conversations.crypto.axolotl.AxolotlService;
46import eu.siacs.conversations.crypto.axolotl.FingerprintStatus;
47import eu.siacs.conversations.crypto.axolotl.SQLiteAxolotlStore;
48import eu.siacs.conversations.entities.Account;
49import eu.siacs.conversations.entities.Contact;
50import eu.siacs.conversations.entities.Conversation;
51import eu.siacs.conversations.entities.DownloadableFile;
52import eu.siacs.conversations.entities.Message;
53import eu.siacs.conversations.entities.PresenceTemplate;
54import eu.siacs.conversations.entities.Roster;
55import eu.siacs.conversations.entities.ServiceDiscoveryResult;
56import eu.siacs.conversations.services.QuickConversationsService;
57import eu.siacs.conversations.services.ShortcutService;
58import eu.siacs.conversations.utils.CryptoHelper;
59import eu.siacs.conversations.utils.CursorUtils;
60import eu.siacs.conversations.utils.FtsUtils;
61import eu.siacs.conversations.utils.MimeUtils;
62import eu.siacs.conversations.utils.Resolver;
63import eu.siacs.conversations.xmpp.InvalidJid;
64import eu.siacs.conversations.xmpp.Jid;
65import eu.siacs.conversations.xmpp.mam.MamReference;
66
67public class DatabaseBackend extends SQLiteOpenHelper {
68
69 private static final String DATABASE_NAME = "history";
70 private static final int DATABASE_VERSION = 51;
71
72 private static boolean requiresMessageIndexRebuild = false;
73 private static DatabaseBackend instance = null;
74 private static final String CREATE_CONTATCS_STATEMENT = "create table "
75 + Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, "
76 + Contact.SERVERNAME + " TEXT, " + Contact.SYSTEMNAME + " TEXT,"
77 + Contact.PRESENCE_NAME + " TEXT,"
78 + Contact.JID + " TEXT," + Contact.KEYS + " TEXT,"
79 + Contact.PHOTOURI + " TEXT," + Contact.OPTIONS + " NUMBER,"
80 + Contact.SYSTEMACCOUNT + " NUMBER, " + Contact.AVATAR + " TEXT, "
81 + Contact.LAST_PRESENCE + " TEXT, " + Contact.LAST_TIME + " NUMBER, "
82 + Contact.RTP_CAPABILITY + " TEXT,"
83 + Contact.GROUPS + " TEXT, FOREIGN KEY(" + Contact.ACCOUNT + ") REFERENCES "
84 + Account.TABLENAME + "(" + Account.UUID
85 + ") ON DELETE CASCADE, UNIQUE(" + Contact.ACCOUNT + ", "
86 + Contact.JID + ") ON CONFLICT REPLACE);";
87
88 private static final String CREATE_DISCOVERY_RESULTS_STATEMENT = "create table "
89 + ServiceDiscoveryResult.TABLENAME + "("
90 + ServiceDiscoveryResult.HASH + " TEXT, "
91 + ServiceDiscoveryResult.VER + " TEXT, "
92 + ServiceDiscoveryResult.RESULT + " TEXT, "
93 + "UNIQUE(" + ServiceDiscoveryResult.HASH + ", "
94 + ServiceDiscoveryResult.VER + ") ON CONFLICT REPLACE);";
95
96 private static final String CREATE_PRESENCE_TEMPLATES_STATEMENT = "CREATE TABLE "
97 + PresenceTemplate.TABELNAME + "("
98 + PresenceTemplate.UUID + " TEXT, "
99 + PresenceTemplate.LAST_USED + " NUMBER,"
100 + PresenceTemplate.MESSAGE + " TEXT,"
101 + PresenceTemplate.STATUS + " TEXT,"
102 + "UNIQUE(" + PresenceTemplate.MESSAGE + "," + PresenceTemplate.STATUS + ") ON CONFLICT REPLACE);";
103
104 private static final String CREATE_PREKEYS_STATEMENT = "CREATE TABLE "
105 + SQLiteAxolotlStore.PREKEY_TABLENAME + "("
106 + SQLiteAxolotlStore.ACCOUNT + " TEXT, "
107 + SQLiteAxolotlStore.ID + " INTEGER, "
108 + SQLiteAxolotlStore.KEY + " TEXT, FOREIGN KEY("
109 + SQLiteAxolotlStore.ACCOUNT
110 + ") REFERENCES " + Account.TABLENAME + "(" + Account.UUID + ") ON DELETE CASCADE, "
111 + "UNIQUE( " + SQLiteAxolotlStore.ACCOUNT + ", "
112 + SQLiteAxolotlStore.ID
113 + ") ON CONFLICT REPLACE"
114 + ");";
115
116 private static final String CREATE_SIGNED_PREKEYS_STATEMENT = "CREATE TABLE "
117 + SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME + "("
118 + SQLiteAxolotlStore.ACCOUNT + " TEXT, "
119 + SQLiteAxolotlStore.ID + " INTEGER, "
120 + SQLiteAxolotlStore.KEY + " TEXT, FOREIGN KEY("
121 + SQLiteAxolotlStore.ACCOUNT
122 + ") REFERENCES " + Account.TABLENAME + "(" + Account.UUID + ") ON DELETE CASCADE, "
123 + "UNIQUE( " + SQLiteAxolotlStore.ACCOUNT + ", "
124 + SQLiteAxolotlStore.ID
125 + ") ON CONFLICT REPLACE" +
126 ");";
127
128 private static final String CREATE_SESSIONS_STATEMENT = "CREATE TABLE "
129 + SQLiteAxolotlStore.SESSION_TABLENAME + "("
130 + SQLiteAxolotlStore.ACCOUNT + " TEXT, "
131 + SQLiteAxolotlStore.NAME + " TEXT, "
132 + SQLiteAxolotlStore.DEVICE_ID + " INTEGER, "
133 + SQLiteAxolotlStore.KEY + " TEXT, FOREIGN KEY("
134 + SQLiteAxolotlStore.ACCOUNT
135 + ") REFERENCES " + Account.TABLENAME + "(" + Account.UUID + ") ON DELETE CASCADE, "
136 + "UNIQUE( " + SQLiteAxolotlStore.ACCOUNT + ", "
137 + SQLiteAxolotlStore.NAME + ", "
138 + SQLiteAxolotlStore.DEVICE_ID
139 + ") ON CONFLICT REPLACE"
140 + ");";
141
142 private static final String CREATE_IDENTITIES_STATEMENT = "CREATE TABLE "
143 + SQLiteAxolotlStore.IDENTITIES_TABLENAME + "("
144 + SQLiteAxolotlStore.ACCOUNT + " TEXT, "
145 + SQLiteAxolotlStore.NAME + " TEXT, "
146 + SQLiteAxolotlStore.OWN + " INTEGER, "
147 + SQLiteAxolotlStore.FINGERPRINT + " TEXT, "
148 + SQLiteAxolotlStore.CERTIFICATE + " BLOB, "
149 + SQLiteAxolotlStore.TRUST + " TEXT, "
150 + SQLiteAxolotlStore.ACTIVE + " NUMBER, "
151 + SQLiteAxolotlStore.LAST_ACTIVATION + " NUMBER,"
152 + SQLiteAxolotlStore.KEY + " TEXT, FOREIGN KEY("
153 + SQLiteAxolotlStore.ACCOUNT
154 + ") REFERENCES " + Account.TABLENAME + "(" + Account.UUID + ") ON DELETE CASCADE, "
155 + "UNIQUE( " + SQLiteAxolotlStore.ACCOUNT + ", "
156 + SQLiteAxolotlStore.NAME + ", "
157 + SQLiteAxolotlStore.FINGERPRINT
158 + ") ON CONFLICT IGNORE"
159 + ");";
160
161 private static final String RESOLVER_RESULTS_TABLENAME = "resolver_results";
162
163 private static final String CREATE_RESOLVER_RESULTS_TABLE = "create table " + RESOLVER_RESULTS_TABLENAME + "("
164 + Resolver.Result.DOMAIN + " TEXT,"
165 + Resolver.Result.HOSTNAME + " TEXT,"
166 + Resolver.Result.IP + " BLOB,"
167 + Resolver.Result.PRIORITY + " NUMBER,"
168 + Resolver.Result.DIRECT_TLS + " NUMBER,"
169 + Resolver.Result.AUTHENTICATED + " NUMBER,"
170 + Resolver.Result.PORT + " NUMBER,"
171 + "UNIQUE(" + Resolver.Result.DOMAIN + ") ON CONFLICT REPLACE"
172 + ");";
173
174 private static final String CREATE_MESSAGE_TIME_INDEX = "CREATE INDEX message_time_index ON " + Message.TABLENAME + "(" + Message.TIME_SENT + ")";
175 private static final String CREATE_MESSAGE_CONVERSATION_INDEX = "CREATE INDEX message_conversation_index ON " + Message.TABLENAME + "(" + Message.CONVERSATION + ")";
176 private static final String CREATE_MESSAGE_DELETED_INDEX = "CREATE INDEX message_deleted_index ON " + Message.TABLENAME + "(" + Message.DELETED + ")";
177 private static final String CREATE_MESSAGE_RELATIVE_FILE_PATH_INDEX = "CREATE INDEX message_file_path_index ON " + Message.TABLENAME + "(" + Message.RELATIVE_FILE_PATH + ")";
178 private static final String CREATE_MESSAGE_TYPE_INDEX = "CREATE INDEX message_type_index ON " + Message.TABLENAME + "(" + Message.TYPE + ")";
179
180 private static final String CREATE_MESSAGE_INDEX_TABLE = "CREATE VIRTUAL TABLE messages_index USING fts4 (uuid,body,notindexed=\"uuid\",content=\"" + Message.TABLENAME + "\",tokenize='unicode61')";
181 private static final String CREATE_MESSAGE_INSERT_TRIGGER = "CREATE TRIGGER after_message_insert AFTER INSERT ON " + Message.TABLENAME + " BEGIN INSERT INTO messages_index(rowid,uuid,body) VALUES(NEW.rowid,NEW.uuid,NEW.body); END;";
182 private static final String CREATE_MESSAGE_UPDATE_TRIGGER = "CREATE TRIGGER after_message_update UPDATE OF uuid,body ON " + Message.TABLENAME + " BEGIN UPDATE messages_index SET body=NEW.body,uuid=NEW.uuid WHERE rowid=OLD.rowid; END;";
183 private static final String CREATE_MESSAGE_DELETE_TRIGGER = "CREATE TRIGGER after_message_delete AFTER DELETE ON " + Message.TABLENAME + " BEGIN DELETE FROM messages_index WHERE rowid=OLD.rowid; END;";
184 private static final String COPY_PREEXISTING_ENTRIES = "INSERT INTO messages_index(messages_index) VALUES('rebuild');";
185
186 protected Context context;
187
188 private DatabaseBackend(Context context) {
189 super(context, DATABASE_NAME, null, DATABASE_VERSION);
190 this.context = context;
191 }
192
193 private static ContentValues createFingerprintStatusContentValues(FingerprintStatus.Trust trust, boolean active) {
194 ContentValues values = new ContentValues();
195 values.put(SQLiteAxolotlStore.TRUST, trust.toString());
196 values.put(SQLiteAxolotlStore.ACTIVE, active ? 1 : 0);
197 return values;
198 }
199
200 public static boolean requiresMessageIndexRebuild() {
201 return requiresMessageIndexRebuild;
202 }
203
204 public void rebuildMessagesIndex() {
205 final SQLiteDatabase db = getWritableDatabase();
206 final Stopwatch stopwatch = Stopwatch.createStarted();
207 db.execSQL(COPY_PREEXISTING_ENTRIES);
208 Log.d(Config.LOGTAG,"rebuilt message index in "+ stopwatch.stop().toString());
209 }
210
211 public static synchronized DatabaseBackend getInstance(Context context) {
212 if (instance == null) {
213 instance = new DatabaseBackend(context);
214 }
215 return instance;
216 }
217
218 protected void cheogramMigrate(SQLiteDatabase db) {
219 db.beginTransaction();
220
221 try {
222 Cursor cursor = db.rawQuery("PRAGMA cheogram.user_version", null);
223 cursor.moveToNext();
224 int cheogramVersion = cursor.getInt(0);
225 cursor.close();
226
227 if(cheogramVersion < 1) {
228 // No cross-DB foreign keys unfortunately
229 db.execSQL(
230 "CREATE TABLE cheogram." + Message.TABLENAME + "(" +
231 Message.UUID + " TEXT PRIMARY KEY, " +
232 "subject TEXT" +
233 ")"
234 );
235 db.execSQL("PRAGMA cheogram.user_version = 1");
236 }
237
238 if(cheogramVersion < 2) {
239 db.execSQL(
240 "ALTER TABLE cheogram." + Message.TABLENAME + " " +
241 "ADD COLUMN oobUri TEXT"
242 );
243 db.execSQL(
244 "ALTER TABLE cheogram." + Message.TABLENAME + " " +
245 "ADD COLUMN fileParams TEXT"
246 );
247 db.execSQL("PRAGMA cheogram.user_version = 2");
248 }
249
250 if(cheogramVersion < 3) {
251 db.execSQL(
252 "ALTER TABLE cheogram." + Message.TABLENAME + " " +
253 "ADD COLUMN payloads TEXT"
254 );
255 db.execSQL("PRAGMA cheogram.user_version = 3");
256 }
257
258 if(cheogramVersion < 4) {
259 db.execSQL(
260 "CREATE TABLE cheogram.cids (" +
261 "cid TEXT NOT NULL PRIMARY KEY," +
262 "path TEXT NOT NULL" +
263 ")"
264 );
265 db.execSQL("PRAGMA cheogram.user_version = 4");
266 }
267
268 if(cheogramVersion < 5) {
269 db.execSQL(
270 "ALTER TABLE cheogram." + Message.TABLENAME + " " +
271 "ADD COLUMN timeReceived NUMBER"
272 );
273 db.execSQL("CREATE INDEX cheogram.message_time_received_index ON " + Message.TABLENAME + " (timeReceived)");
274 db.execSQL("PRAGMA cheogram.user_version = 5");
275 }
276
277 if(cheogramVersion < 6) {
278 db.execSQL(
279 "CREATE TABLE cheogram.blocked_media (" +
280 "cid TEXT NOT NULL PRIMARY KEY" +
281 ")"
282 );
283 db.execSQL("PRAGMA cheogram.user_version = 6");
284 }
285
286 if(cheogramVersion < 7) {
287 db.execSQL(
288 "ALTER TABLE cheogram.cids " +
289 "ADD COLUMN url TEXT"
290 );
291 db.execSQL("PRAGMA cheogram.user_version = 7");
292 }
293
294 db.setTransactionSuccessful();
295 } finally {
296 db.endTransaction();
297 }
298 }
299
300 @Override
301 public void onConfigure(SQLiteDatabase db) {
302 db.execSQL("PRAGMA foreign_keys=ON");
303 db.rawQuery("PRAGMA secure_delete=ON", null).close();
304 db.execSQL("ATTACH DATABASE ? AS cheogram", new Object[]{context.getDatabasePath("cheogram").getPath()});
305 cheogramMigrate(db);
306 }
307
308 @Override
309 public void onCreate(SQLiteDatabase db) {
310 db.execSQL("create table " + Account.TABLENAME + "(" + Account.UUID + " TEXT PRIMARY KEY,"
311 + Account.USERNAME + " TEXT,"
312 + Account.SERVER + " TEXT,"
313 + Account.PASSWORD + " TEXT,"
314 + Account.DISPLAY_NAME + " TEXT, "
315 + Account.STATUS + " TEXT,"
316 + Account.STATUS_MESSAGE + " TEXT,"
317 + Account.ROSTERVERSION + " TEXT,"
318 + Account.OPTIONS + " NUMBER, "
319 + Account.AVATAR + " TEXT, "
320 + Account.KEYS + " TEXT, "
321 + Account.HOSTNAME + " TEXT, "
322 + Account.RESOURCE + " TEXT,"
323 + Account.PINNED_MECHANISM + " TEXT,"
324 + Account.PINNED_CHANNEL_BINDING + " TEXT,"
325 + Account.FAST_MECHANISM + " TEXT,"
326 + Account.FAST_TOKEN + " TEXT,"
327 + Account.PORT + " NUMBER DEFAULT 5222)");
328 db.execSQL("create table " + Conversation.TABLENAME + " ("
329 + Conversation.UUID + " TEXT PRIMARY KEY, " + Conversation.NAME
330 + " TEXT, " + Conversation.CONTACT + " TEXT, "
331 + Conversation.ACCOUNT + " TEXT, " + Conversation.CONTACTJID
332 + " TEXT, " + Conversation.CREATED + " NUMBER, "
333 + Conversation.STATUS + " NUMBER, " + Conversation.MODE
334 + " NUMBER, " + Conversation.ATTRIBUTES + " TEXT, FOREIGN KEY("
335 + Conversation.ACCOUNT + ") REFERENCES " + Account.TABLENAME
336 + "(" + Account.UUID + ") ON DELETE CASCADE);");
337 db.execSQL("create table " + Message.TABLENAME + "( " + Message.UUID
338 + " TEXT PRIMARY KEY, " + Message.CONVERSATION + " TEXT, "
339 + Message.TIME_SENT + " NUMBER, " + Message.COUNTERPART
340 + " TEXT, " + Message.TRUE_COUNTERPART + " TEXT,"
341 + Message.BODY + " TEXT, " + Message.ENCRYPTION + " NUMBER, "
342 + Message.STATUS + " NUMBER," + Message.TYPE + " NUMBER, "
343 + Message.RELATIVE_FILE_PATH + " TEXT, "
344 + Message.SERVER_MSG_ID + " TEXT, "
345 + Message.FINGERPRINT + " TEXT, "
346 + Message.CARBON + " INTEGER, "
347 + Message.EDITED + " TEXT, "
348 + Message.READ + " NUMBER DEFAULT 1, "
349 + Message.OOB + " INTEGER, "
350 + Message.ERROR_MESSAGE + " TEXT,"
351 + Message.READ_BY_MARKERS + " TEXT,"
352 + Message.MARKABLE + " NUMBER DEFAULT 0,"
353 + Message.DELETED + " NUMBER DEFAULT 0,"
354 + Message.BODY_LANGUAGE + " TEXT,"
355 + Message.REMOTE_MSG_ID + " TEXT, FOREIGN KEY("
356 + Message.CONVERSATION + ") REFERENCES "
357 + Conversation.TABLENAME + "(" + Conversation.UUID
358 + ") ON DELETE CASCADE);");
359 db.execSQL(CREATE_MESSAGE_TIME_INDEX);
360 db.execSQL(CREATE_MESSAGE_CONVERSATION_INDEX);
361 db.execSQL(CREATE_MESSAGE_DELETED_INDEX);
362 db.execSQL(CREATE_MESSAGE_RELATIVE_FILE_PATH_INDEX);
363 db.execSQL(CREATE_MESSAGE_TYPE_INDEX);
364 db.execSQL(CREATE_CONTATCS_STATEMENT);
365 db.execSQL(CREATE_DISCOVERY_RESULTS_STATEMENT);
366 db.execSQL(CREATE_SESSIONS_STATEMENT);
367 db.execSQL(CREATE_PREKEYS_STATEMENT);
368 db.execSQL(CREATE_SIGNED_PREKEYS_STATEMENT);
369 db.execSQL(CREATE_IDENTITIES_STATEMENT);
370 db.execSQL(CREATE_PRESENCE_TEMPLATES_STATEMENT);
371 db.execSQL(CREATE_RESOLVER_RESULTS_TABLE);
372 db.execSQL(CREATE_MESSAGE_INDEX_TABLE);
373 db.execSQL(CREATE_MESSAGE_INSERT_TRIGGER);
374 db.execSQL(CREATE_MESSAGE_UPDATE_TRIGGER);
375 db.execSQL(CREATE_MESSAGE_DELETE_TRIGGER);
376 }
377
378 @Override
379 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
380 if (oldVersion < 2 && newVersion >= 2) {
381 db.execSQL("update " + Account.TABLENAME + " set "
382 + Account.OPTIONS + " = " + Account.OPTIONS + " | 8");
383 }
384 if (oldVersion < 3 && newVersion >= 3) {
385 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
386 + Message.TYPE + " NUMBER");
387 }
388 if (oldVersion < 5 && newVersion >= 5) {
389 db.execSQL("DROP TABLE " + Contact.TABLENAME);
390 db.execSQL(CREATE_CONTATCS_STATEMENT);
391 db.execSQL("UPDATE " + Account.TABLENAME + " SET "
392 + Account.ROSTERVERSION + " = NULL");
393 }
394 if (oldVersion < 6 && newVersion >= 6) {
395 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
396 + Message.TRUE_COUNTERPART + " TEXT");
397 }
398 if (oldVersion < 7 && newVersion >= 7) {
399 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
400 + Message.REMOTE_MSG_ID + " TEXT");
401 db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN "
402 + Contact.AVATAR + " TEXT");
403 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN "
404 + Account.AVATAR + " TEXT");
405 }
406 if (oldVersion < 8 && newVersion >= 8) {
407 db.execSQL("ALTER TABLE " + Conversation.TABLENAME + " ADD COLUMN "
408 + Conversation.ATTRIBUTES + " TEXT");
409 }
410 if (oldVersion < 9 && newVersion >= 9) {
411 db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN "
412 + Contact.LAST_TIME + " NUMBER");
413 db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN "
414 + Contact.LAST_PRESENCE + " TEXT");
415 }
416 if (oldVersion < 10 && newVersion >= 10) {
417 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
418 + Message.RELATIVE_FILE_PATH + " TEXT");
419 }
420 if (oldVersion < 11 && newVersion >= 11) {
421 db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN "
422 + Contact.GROUPS + " TEXT");
423 db.execSQL("delete from " + Contact.TABLENAME);
424 db.execSQL("update " + Account.TABLENAME + " set " + Account.ROSTERVERSION + " = NULL");
425 }
426 if (oldVersion < 12 && newVersion >= 12) {
427 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
428 + Message.SERVER_MSG_ID + " TEXT");
429 }
430 if (oldVersion < 13 && newVersion >= 13) {
431 db.execSQL("delete from " + Contact.TABLENAME);
432 db.execSQL("update " + Account.TABLENAME + " set " + Account.ROSTERVERSION + " = NULL");
433 }
434 if (oldVersion < 14 && newVersion >= 14) {
435 canonicalizeJids(db);
436 }
437 if (oldVersion < 15 && newVersion >= 15) {
438 recreateAxolotlDb(db);
439 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
440 + Message.FINGERPRINT + " TEXT");
441 }
442 if (oldVersion < 16 && newVersion >= 16) {
443 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
444 + Message.CARBON + " INTEGER");
445 }
446 if (oldVersion < 19 && newVersion >= 19) {
447 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.DISPLAY_NAME + " TEXT");
448 }
449 if (oldVersion < 20 && newVersion >= 20) {
450 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.HOSTNAME + " TEXT");
451 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.PORT + " NUMBER DEFAULT 5222");
452 }
453 if (oldVersion < 26 && newVersion >= 26) {
454 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.STATUS + " TEXT");
455 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.STATUS_MESSAGE + " TEXT");
456 }
457 if (oldVersion < 40 && newVersion >= 40) {
458 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.RESOURCE + " TEXT");
459 }
460 /* Any migrations that alter the Account table need to happen BEFORE this migration, as it
461 * depends on account de-serialization.
462 */
463 if (oldVersion < 17 && newVersion >= 17 && newVersion < 31) {
464 List<Account> accounts = getAccounts(db);
465 for (Account account : accounts) {
466 String ownDeviceIdString = account.getKey(SQLiteAxolotlStore.JSONKEY_REGISTRATION_ID);
467 if (ownDeviceIdString == null) {
468 continue;
469 }
470 int ownDeviceId = Integer.valueOf(ownDeviceIdString);
471 SignalProtocolAddress ownAddress = new SignalProtocolAddress(account.getJid().asBareJid().toString(), ownDeviceId);
472 deleteSession(db, account, ownAddress);
473 IdentityKeyPair identityKeyPair = loadOwnIdentityKeyPair(db, account);
474 if (identityKeyPair != null) {
475 String[] selectionArgs = {
476 account.getUuid(),
477 CryptoHelper.bytesToHex(identityKeyPair.getPublicKey().serialize())
478 };
479 ContentValues values = new ContentValues();
480 values.put(SQLiteAxolotlStore.TRUSTED, 2);
481 db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, values,
482 SQLiteAxolotlStore.ACCOUNT + " = ? AND "
483 + SQLiteAxolotlStore.FINGERPRINT + " = ? ",
484 selectionArgs);
485 } else {
486 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": could not load own identity key pair");
487 }
488 }
489 }
490 if (oldVersion < 18 && newVersion >= 18) {
491 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.READ + " NUMBER DEFAULT 1");
492 }
493
494 if (oldVersion < 21 && newVersion >= 21) {
495 List<Account> accounts = getAccounts(db);
496 for (Account account : accounts) {
497 account.unsetPgpSignature();
498 db.update(Account.TABLENAME, account.getContentValues(), Account.UUID
499 + "=?", new String[]{account.getUuid()});
500 }
501 }
502
503 if (oldVersion >= 15 && oldVersion < 22 && newVersion >= 22) {
504 db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.CERTIFICATE);
505 }
506
507 if (oldVersion < 23 && newVersion >= 23) {
508 db.execSQL(CREATE_DISCOVERY_RESULTS_STATEMENT);
509 }
510
511 if (oldVersion < 24 && newVersion >= 24) {
512 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.EDITED + " TEXT");
513 }
514
515 if (oldVersion < 25 && newVersion >= 25) {
516 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.OOB + " INTEGER");
517 }
518
519 if (oldVersion < 26 && newVersion >= 26) {
520 db.execSQL(CREATE_PRESENCE_TEMPLATES_STATEMENT);
521 }
522
523 if (oldVersion < 27 && newVersion >= 27) {
524 db.execSQL("DELETE FROM " + ServiceDiscoveryResult.TABLENAME);
525 }
526
527 if (oldVersion < 28 && newVersion >= 28) {
528 canonicalizeJids(db);
529 }
530
531 if (oldVersion < 29 && newVersion >= 29) {
532 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.ERROR_MESSAGE + " TEXT");
533 }
534 if (oldVersion >= 15 && oldVersion < 31 && newVersion >= 31) {
535 db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.TRUST + " TEXT");
536 db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.ACTIVE + " NUMBER");
537 HashMap<Integer, ContentValues> migration = new HashMap<>();
538 migration.put(0, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, true));
539 migration.put(1, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, true));
540 migration.put(2, createFingerprintStatusContentValues(FingerprintStatus.Trust.UNTRUSTED, true));
541 migration.put(3, createFingerprintStatusContentValues(FingerprintStatus.Trust.COMPROMISED, false));
542 migration.put(4, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, false));
543 migration.put(5, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, false));
544 migration.put(6, createFingerprintStatusContentValues(FingerprintStatus.Trust.UNTRUSTED, false));
545 migration.put(7, createFingerprintStatusContentValues(FingerprintStatus.Trust.VERIFIED_X509, true));
546 migration.put(8, createFingerprintStatusContentValues(FingerprintStatus.Trust.VERIFIED_X509, false));
547 for (Map.Entry<Integer, ContentValues> entry : migration.entrySet()) {
548 String whereClause = SQLiteAxolotlStore.TRUSTED + "=?";
549 String[] where = {String.valueOf(entry.getKey())};
550 db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, entry.getValue(), whereClause, where);
551 }
552
553 }
554 if (oldVersion >= 15 && oldVersion < 32 && newVersion >= 32) {
555 db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.LAST_ACTIVATION + " NUMBER");
556 ContentValues defaults = new ContentValues();
557 defaults.put(SQLiteAxolotlStore.LAST_ACTIVATION, System.currentTimeMillis());
558 db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, defaults, null, null);
559 }
560 if (oldVersion >= 15 && oldVersion < 33 && newVersion >= 33) {
561 String whereClause = SQLiteAxolotlStore.OWN + "=1";
562 db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, createFingerprintStatusContentValues(FingerprintStatus.Trust.VERIFIED, true), whereClause, null);
563 }
564
565 if (oldVersion < 34 && newVersion >= 34) {
566 db.execSQL(CREATE_MESSAGE_TIME_INDEX);
567
568 final File oldPicturesDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/Conversations/");
569 final File oldFilesDirectory = new File(Environment.getExternalStorageDirectory() + "/Conversations/");
570 final File newFilesDirectory = new File(Environment.getExternalStorageDirectory() + "/Conversations/Media/Conversations Files/");
571 final File newVideosDirectory = new File(Environment.getExternalStorageDirectory() + "/Conversations/Media/Conversations Videos/");
572 if (oldPicturesDirectory.exists() && oldPicturesDirectory.isDirectory()) {
573 final File newPicturesDirectory = new File(Environment.getExternalStorageDirectory() + "/Conversations/Media/Conversations Images/");
574 newPicturesDirectory.getParentFile().mkdirs();
575 if (oldPicturesDirectory.renameTo(newPicturesDirectory)) {
576 Log.d(Config.LOGTAG, "moved " + oldPicturesDirectory.getAbsolutePath() + " to " + newPicturesDirectory.getAbsolutePath());
577 }
578 }
579 if (oldFilesDirectory.exists() && oldFilesDirectory.isDirectory()) {
580 newFilesDirectory.mkdirs();
581 newVideosDirectory.mkdirs();
582 final File[] files = oldFilesDirectory.listFiles();
583 if (files == null) {
584 return;
585 }
586 for (File file : files) {
587 if (file.getName().equals(".nomedia")) {
588 if (file.delete()) {
589 Log.d(Config.LOGTAG, "deleted nomedia file in " + oldFilesDirectory.getAbsolutePath());
590 }
591 } else if (file.isFile()) {
592 final String name = file.getName();
593 boolean isVideo = false;
594 int start = name.lastIndexOf('.') + 1;
595 if (start < name.length()) {
596 String mime = MimeUtils.guessMimeTypeFromExtension(name.substring(start));
597 isVideo = mime != null && mime.startsWith("video/");
598 }
599 File dst = new File((isVideo ? newVideosDirectory : newFilesDirectory).getAbsolutePath() + "/" + file.getName());
600 if (file.renameTo(dst)) {
601 Log.d(Config.LOGTAG, "moved " + file + " to " + dst);
602 }
603 }
604 }
605 }
606 }
607 if (oldVersion < 35 && newVersion >= 35) {
608 db.execSQL(CREATE_MESSAGE_CONVERSATION_INDEX);
609 }
610 if (oldVersion < 36 && newVersion >= 36) {
611 List<Account> accounts = getAccounts(db);
612 for (Account account : accounts) {
613 account.setOption(Account.OPTION_REQUIRES_ACCESS_MODE_CHANGE, true);
614 account.setOption(Account.OPTION_LOGGED_IN_SUCCESSFULLY, false);
615 db.update(Account.TABLENAME, account.getContentValues(), Account.UUID
616 + "=?", new String[]{account.getUuid()});
617 }
618 }
619
620 if (oldVersion < 37 && newVersion >= 37) {
621 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.READ_BY_MARKERS + " TEXT");
622 }
623
624 if (oldVersion < 38 && newVersion >= 38) {
625 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.MARKABLE + " NUMBER DEFAULT 0");
626 }
627
628 if (oldVersion < 39 && newVersion >= 39) {
629 db.execSQL(CREATE_RESOLVER_RESULTS_TABLE);
630 }
631
632 if (QuickConversationsService.isQuicksy() && oldVersion < 43 && newVersion >= 43) {
633 List<Account> accounts = getAccounts(db);
634 for (Account account : accounts) {
635 account.setOption(Account.OPTION_MAGIC_CREATE, true);
636 db.update(Account.TABLENAME, account.getContentValues(), Account.UUID
637 + "=?", new String[]{account.getUuid()});
638 }
639 }
640
641 if (oldVersion < 44 && newVersion >= 44) {
642 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.DELETED + " NUMBER DEFAULT 0");
643 db.execSQL(CREATE_MESSAGE_DELETED_INDEX);
644 db.execSQL(CREATE_MESSAGE_RELATIVE_FILE_PATH_INDEX);
645 db.execSQL(CREATE_MESSAGE_TYPE_INDEX);
646 }
647
648 if (oldVersion < 45 && newVersion >= 45) {
649 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.BODY_LANGUAGE);
650 }
651
652 if (oldVersion < 46 && newVersion >= 46) {
653 final long start = SystemClock.elapsedRealtime();
654 db.rawQuery("PRAGMA secure_delete = FALSE", null).close();
655 db.execSQL("update " + Message.TABLENAME + " set " + Message.EDITED + "=NULL");
656 db.rawQuery("PRAGMA secure_delete=ON", null).close();
657 final long diff = SystemClock.elapsedRealtime() - start;
658 Log.d(Config.LOGTAG, "deleted old edit information in " + diff + "ms");
659 }
660 if (oldVersion < 47 && newVersion >= 47) {
661 db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN " + Contact.PRESENCE_NAME + " TEXT");
662 }
663 if (oldVersion < 48 && newVersion >= 48) {
664 db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN " + Contact.RTP_CAPABILITY + " TEXT");
665 }
666 if (oldVersion < 49 && newVersion >= 49) {
667 db.beginTransaction();
668 db.execSQL("DROP TRIGGER IF EXISTS after_message_insert;");
669 db.execSQL("DROP TRIGGER IF EXISTS after_message_update;");
670 db.execSQL("DROP TRIGGER IF EXISTS after_message_delete;");
671 db.execSQL("DROP TABLE IF EXISTS messages_index;");
672 // a hack that should not be necessary, but
673 // there was at least one occurence when SQLite failed at this
674 db.execSQL("DROP TABLE IF EXISTS messages_index_docsize;");
675 db.execSQL("DROP TABLE IF EXISTS messages_index_segdir;");
676 db.execSQL("DROP TABLE IF EXISTS messages_index_segments;");
677 db.execSQL("DROP TABLE IF EXISTS messages_index_stat;");
678 db.execSQL(CREATE_MESSAGE_INDEX_TABLE);
679 db.execSQL(CREATE_MESSAGE_INSERT_TRIGGER);
680 db.execSQL(CREATE_MESSAGE_UPDATE_TRIGGER);
681 db.execSQL(CREATE_MESSAGE_DELETE_TRIGGER);
682 db.setTransactionSuccessful();
683 db.endTransaction();
684 requiresMessageIndexRebuild = true;
685 }
686 if (oldVersion < 50 && newVersion >= 50) {
687 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.PINNED_MECHANISM + " TEXT");
688 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.PINNED_CHANNEL_BINDING + " TEXT");
689 }
690 if (oldVersion < 51 && newVersion >= 51) {
691 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.FAST_MECHANISM + " TEXT");
692 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.FAST_TOKEN + " TEXT");
693 }
694 }
695
696 private void canonicalizeJids(SQLiteDatabase db) {
697 // migrate db to new, canonicalized JID domainpart representation
698
699 // Conversation table
700 Cursor cursor = db.rawQuery("select * from " + Conversation.TABLENAME, new String[0]);
701 while (cursor.moveToNext()) {
702 String newJid;
703 try {
704 newJid = Jid.of(cursor.getString(cursor.getColumnIndex(Conversation.CONTACTJID))).toString();
705 } catch (IllegalArgumentException ignored) {
706 Log.e(Config.LOGTAG, "Failed to migrate Conversation CONTACTJID "
707 + cursor.getString(cursor.getColumnIndex(Conversation.CONTACTJID))
708 + ": " + ignored + ". Skipping...");
709 continue;
710 }
711
712 final String[] updateArgs = {
713 newJid,
714 cursor.getString(cursor.getColumnIndex(Conversation.UUID)),
715 };
716 db.execSQL("update " + Conversation.TABLENAME
717 + " set " + Conversation.CONTACTJID + " = ? "
718 + " where " + Conversation.UUID + " = ?", updateArgs);
719 }
720 cursor.close();
721
722 // Contact table
723 cursor = db.rawQuery("select * from " + Contact.TABLENAME, new String[0]);
724 while (cursor.moveToNext()) {
725 String newJid;
726 try {
727 newJid = Jid.of(cursor.getString(cursor.getColumnIndex(Contact.JID))).toString();
728 } catch (final IllegalArgumentException e) {
729 Log.e(Config.LOGTAG, "Failed to migrate Contact JID "
730 + cursor.getString(cursor.getColumnIndex(Contact.JID))
731 + ": Skipping...", e);
732 continue;
733 }
734
735 final String[] updateArgs = {
736 newJid,
737 cursor.getString(cursor.getColumnIndex(Contact.ACCOUNT)),
738 cursor.getString(cursor.getColumnIndex(Contact.JID)),
739 };
740 db.execSQL("update " + Contact.TABLENAME
741 + " set " + Contact.JID + " = ? "
742 + " where " + Contact.ACCOUNT + " = ? "
743 + " AND " + Contact.JID + " = ?", updateArgs);
744 }
745 cursor.close();
746
747 // Account table
748 cursor = db.rawQuery("select * from " + Account.TABLENAME, new String[0]);
749 while (cursor.moveToNext()) {
750 String newServer;
751 try {
752 newServer = Jid.of(
753 cursor.getString(cursor.getColumnIndex(Account.USERNAME)),
754 cursor.getString(cursor.getColumnIndex(Account.SERVER)),
755 null
756 ).getDomain().toEscapedString();
757 } catch (IllegalArgumentException ignored) {
758 Log.e(Config.LOGTAG, "Failed to migrate Account SERVER "
759 + cursor.getString(cursor.getColumnIndex(Account.SERVER))
760 + ": " + ignored + ". Skipping...");
761 continue;
762 }
763
764 String[] updateArgs = {
765 newServer,
766 cursor.getString(cursor.getColumnIndex(Account.UUID)),
767 };
768 db.execSQL("update " + Account.TABLENAME
769 + " set " + Account.SERVER + " = ? "
770 + " where " + Account.UUID + " = ?", updateArgs);
771 }
772 cursor.close();
773 }
774
775 public DownloadableFile getFileForCid(Cid cid) {
776 SQLiteDatabase db = this.getReadableDatabase();
777 Cursor cursor = db.query("cheogram.cids", new String[]{"path"}, "cid=?", new String[]{cid.toString()}, null, null, null);
778 DownloadableFile f = null;
779 if (cursor.moveToNext()) {
780 f = new DownloadableFile(cursor.getString(0));
781 }
782 cursor.close();
783 return f;
784 }
785
786 public String getUrlForCid(Cid cid) {
787 SQLiteDatabase db = this.getReadableDatabase();
788 Cursor cursor = db.query("cheogram.cids", new String[]{"url"}, "cid=?", new String[]{cid.toString()}, null, null, null);
789 String url = null;
790 if (cursor.moveToNext()) {
791 url = cursor.getString(0);
792 }
793 cursor.close();
794 return url;
795 }
796
797 public void saveCid(Cid cid, File file) {
798 saveCid(cid, file, null);
799 }
800
801 public void saveCid(Cid cid, File file, String url) {
802 SQLiteDatabase db = this.getWritableDatabase();
803 ContentValues cv = new ContentValues();
804 cv.put("cid", cid.toString());
805 cv.put("path", file.getAbsolutePath());
806 cv.put("url", url);
807 db.insertWithOnConflict("cheogram.cids", null, cv, SQLiteDatabase.CONFLICT_REPLACE);
808 }
809
810 public void blockMedia(Cid cid) {
811 SQLiteDatabase db = this.getWritableDatabase();
812 ContentValues cv = new ContentValues();
813 cv.put("cid", cid.toString());
814 db.insertWithOnConflict("cheogram.blocked_media", null, cv, SQLiteDatabase.CONFLICT_REPLACE);
815 }
816
817 public boolean isBlockedMedia(Cid cid) {
818 SQLiteDatabase db = this.getReadableDatabase();
819 Cursor cursor = db.query("cheogram.blocked_media", new String[]{"count(*)"}, "cid=?", new String[]{cid.toString()}, null, null, null);
820 boolean is = false;
821 if (cursor.moveToNext()) {
822 is = cursor.getInt(0) > 0;
823 }
824 cursor.close();
825 return is;
826 }
827
828 public void clearBlockedMedia() {
829 SQLiteDatabase db = this.getWritableDatabase();
830 db.execSQL("DELETE FROM cheogram.blocked_media");
831 }
832
833 public void createConversation(Conversation conversation) {
834 SQLiteDatabase db = this.getWritableDatabase();
835 db.insert(Conversation.TABLENAME, null, conversation.getContentValues());
836 }
837
838 public void createMessage(Message message) {
839 SQLiteDatabase db = this.getWritableDatabase();
840 db.insert(Message.TABLENAME, null, message.getContentValues());
841 db.insert("cheogram." + Message.TABLENAME, null, message.getCheogramContentValues());
842 }
843
844 public void createAccount(Account account) {
845 SQLiteDatabase db = this.getWritableDatabase();
846 db.insert(Account.TABLENAME, null, account.getContentValues());
847 }
848
849 public void insertDiscoveryResult(ServiceDiscoveryResult result) {
850 SQLiteDatabase db = this.getWritableDatabase();
851 db.insert(ServiceDiscoveryResult.TABLENAME, null, result.getContentValues());
852 }
853
854 public ServiceDiscoveryResult findDiscoveryResult(final String hash, final String ver) {
855 SQLiteDatabase db = this.getReadableDatabase();
856 String[] selectionArgs = {hash, ver};
857 Cursor cursor = db.query(ServiceDiscoveryResult.TABLENAME, null,
858 ServiceDiscoveryResult.HASH + "=? AND " + ServiceDiscoveryResult.VER + "=?",
859 selectionArgs, null, null, null);
860 if (cursor.getCount() == 0) {
861 cursor.close();
862 return null;
863 }
864 cursor.moveToFirst();
865
866 ServiceDiscoveryResult result = null;
867 try {
868 result = new ServiceDiscoveryResult(cursor);
869 } catch (JSONException e) { /* result is still null */ }
870
871 cursor.close();
872 return result;
873 }
874
875 public void saveResolverResult(String domain, Resolver.Result result) {
876 SQLiteDatabase db = this.getWritableDatabase();
877 ContentValues contentValues = result.toContentValues();
878 contentValues.put(Resolver.Result.DOMAIN, domain);
879 db.insert(RESOLVER_RESULTS_TABLENAME, null, contentValues);
880 }
881
882 public synchronized Resolver.Result findResolverResult(String domain) {
883 SQLiteDatabase db = this.getReadableDatabase();
884 String where = Resolver.Result.DOMAIN + "=?";
885 String[] whereArgs = {domain};
886 final Cursor cursor = db.query(RESOLVER_RESULTS_TABLENAME, null, where, whereArgs, null, null, null);
887 Resolver.Result result = null;
888 if (cursor != null) {
889 try {
890 if (cursor.moveToFirst()) {
891 result = Resolver.Result.fromCursor(cursor);
892 }
893 } catch (Exception e) {
894 Log.d(Config.LOGTAG, "unable to find cached resolver result in database " + e.getMessage());
895 return null;
896 } finally {
897 cursor.close();
898 }
899 }
900 return result;
901 }
902
903 public void insertPresenceTemplate(PresenceTemplate template) {
904 SQLiteDatabase db = this.getWritableDatabase();
905 String whereToDelete = PresenceTemplate.MESSAGE + "=?";
906 String[] whereToDeleteArgs = {template.getStatusMessage()};
907 db.delete(PresenceTemplate.TABELNAME, whereToDelete, whereToDeleteArgs);
908 db.delete(PresenceTemplate.TABELNAME, PresenceTemplate.UUID + " not in (select " + PresenceTemplate.UUID + " from " + PresenceTemplate.TABELNAME + " order by " + PresenceTemplate.LAST_USED + " desc limit 9)", null);
909 db.insert(PresenceTemplate.TABELNAME, null, template.getContentValues());
910 }
911
912 public List<PresenceTemplate> getPresenceTemplates() {
913 ArrayList<PresenceTemplate> templates = new ArrayList<>();
914 SQLiteDatabase db = this.getReadableDatabase();
915 Cursor cursor = db.query(PresenceTemplate.TABELNAME, null, null, null, null, null, PresenceTemplate.LAST_USED + " desc");
916 while (cursor.moveToNext()) {
917 templates.add(PresenceTemplate.fromCursor(cursor));
918 }
919 cursor.close();
920 return templates;
921 }
922
923 public CopyOnWriteArrayList<Conversation> getConversations(int status) {
924 CopyOnWriteArrayList<Conversation> list = new CopyOnWriteArrayList<>();
925 SQLiteDatabase db = this.getReadableDatabase();
926 String[] selectionArgs = {Integer.toString(status)};
927 Cursor cursor = db.rawQuery("select * from " + Conversation.TABLENAME
928 + " where " + Conversation.STATUS + " = ? and " + Conversation.CONTACTJID + " is not null order by "
929 + Conversation.CREATED + " desc", selectionArgs);
930 while (cursor.moveToNext()) {
931 final Conversation conversation = Conversation.fromCursor(cursor);
932 if (conversation.getJid() instanceof InvalidJid) {
933 continue;
934 }
935 list.add(conversation);
936 }
937 cursor.close();
938 return list;
939 }
940
941 public ArrayList<Message> getMessages(Conversation conversations, int limit) {
942 return getMessages(conversations, limit, -1);
943 }
944
945 public ArrayList<Message> getMessages(Conversation conversation, int limit, long timestamp) {
946 ArrayList<Message> list = new ArrayList<>();
947 SQLiteDatabase db = this.getReadableDatabase();
948 Cursor cursor;
949 if (timestamp == -1) {
950 String[] selectionArgs = {conversation.getUuid()};
951 cursor = db.rawQuery(
952 "SELECT * FROM " + Message.TABLENAME + " " +
953 "LEFT JOIN cheogram." + Message.TABLENAME +
954 " USING (" + Message.UUID + ")" +
955 "WHERE " + Message.CONVERSATION + "=? " +
956 "ORDER BY " + Message.TIME_SENT + " DESC " +
957 "LIMIT " + String.valueOf(limit),
958 selectionArgs
959 );
960 } else {
961 String[] selectionArgs = {conversation.getUuid(),
962 Long.toString(timestamp)};
963 cursor = db.rawQuery(
964 "SELECT * FROM " + Message.TABLENAME + " " +
965 "LEFT JOIN cheogram." + Message.TABLENAME +
966 " USING (" + Message.UUID + ")" +
967 "WHERE " + Message.CONVERSATION + "=? AND " +
968 Message.TIME_SENT + "<? " +
969 "ORDER BY " + Message.TIME_SENT + " DESC " +
970 "LIMIT " + String.valueOf(limit),
971 selectionArgs
972 );
973 }
974 CursorUtils.upgradeCursorWindowSize(cursor);
975 while (cursor.moveToNext()) {
976 try {
977 list.add(0, Message.fromCursor(cursor, conversation));
978 } catch (Exception e) {
979 Log.e(Config.LOGTAG, "unable to restore message");
980 }
981 }
982 cursor.close();
983 return list;
984 }
985
986 public Cursor getMessageSearchCursor(final List<String> term, final String uuid) {
987 final SQLiteDatabase db = this.getReadableDatabase();
988 final StringBuilder SQL = new StringBuilder();
989 final String[] selectionArgs;
990 SQL.append("SELECT " + Message.TABLENAME + ".*," + Conversation.TABLENAME + "." + Conversation.CONTACTJID + "," + Conversation.TABLENAME + "." + Conversation.ACCOUNT + "," + Conversation.TABLENAME + "." + Conversation.MODE + " FROM " + Message.TABLENAME + " JOIN " + Conversation.TABLENAME + " ON " + Message.TABLENAME + "." + Message.CONVERSATION + "=" + Conversation.TABLENAME + "." + Conversation.UUID + " JOIN messages_index ON messages_index.rowid=messages.rowid WHERE " + Message.ENCRYPTION + " NOT IN(" + Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE + "," + Message.ENCRYPTION_PGP + "," + Message.ENCRYPTION_DECRYPTION_FAILED + "," + Message.ENCRYPTION_AXOLOTL_FAILED + ") AND " + Message.TYPE + " IN(" + Message.TYPE_TEXT + "," + Message.TYPE_PRIVATE + ") AND messages_index.body MATCH ?");
991 if (uuid == null) {
992 selectionArgs = new String[]{FtsUtils.toMatchString(term)};
993 } else {
994 selectionArgs = new String[]{FtsUtils.toMatchString(term), uuid};
995 SQL.append(" AND " + Conversation.TABLENAME + '.' + Conversation.UUID + "=?");
996 }
997 SQL.append(" ORDER BY " + Message.TIME_SENT + " DESC limit " + Config.MAX_SEARCH_RESULTS);
998 Log.d(Config.LOGTAG, "search term: " + FtsUtils.toMatchString(term));
999 return db.rawQuery(SQL.toString(), selectionArgs);
1000 }
1001
1002 public List<String> markFileAsDeleted(final File file, final boolean internal) {
1003 SQLiteDatabase db = this.getReadableDatabase();
1004 String selection;
1005 String[] selectionArgs;
1006 if (internal) {
1007 final String name = file.getName();
1008 if (name.endsWith(".pgp")) {
1009 selection = "(" + Message.RELATIVE_FILE_PATH + " IN(?,?) OR (" + Message.RELATIVE_FILE_PATH + "=? and encryption in(1,4))) and type in (1,2,5)";
1010 selectionArgs = new String[]{file.getAbsolutePath(), name, name.substring(0, name.length() - 4)};
1011 } else {
1012 selection = Message.RELATIVE_FILE_PATH + " IN(?,?) and type in (1,2,5)";
1013 selectionArgs = new String[]{file.getAbsolutePath(), name};
1014 }
1015 } else {
1016 selection = Message.RELATIVE_FILE_PATH + "=? and type in (1,2,5)";
1017 selectionArgs = new String[]{file.getAbsolutePath()};
1018 }
1019 final List<String> uuids = new ArrayList<>();
1020 Cursor cursor = db.query(Message.TABLENAME, new String[]{Message.UUID}, selection, selectionArgs, null, null, null);
1021 while (cursor != null && cursor.moveToNext()) {
1022 uuids.add(cursor.getString(0));
1023 }
1024 if (cursor != null) {
1025 cursor.close();
1026 }
1027 markFileAsDeleted(uuids);
1028 return uuids;
1029 }
1030
1031 public void markFileAsDeleted(List<String> uuids) {
1032 SQLiteDatabase db = this.getReadableDatabase();
1033 final ContentValues contentValues = new ContentValues();
1034 final String where = Message.UUID + "=?";
1035 contentValues.put(Message.DELETED, 1);
1036 db.beginTransaction();
1037 for (String uuid : uuids) {
1038 db.update(Message.TABLENAME, contentValues, where, new String[]{uuid});
1039 }
1040 db.setTransactionSuccessful();
1041 db.endTransaction();
1042 }
1043
1044 public void markFilesAsChanged(List<FilePathInfo> files) {
1045 SQLiteDatabase db = this.getReadableDatabase();
1046 final String where = Message.UUID + "=?";
1047 db.beginTransaction();
1048 for (FilePathInfo info : files) {
1049 final ContentValues contentValues = new ContentValues();
1050 contentValues.put(Message.DELETED, info.deleted ? 1 : 0);
1051 db.update(Message.TABLENAME, contentValues, where, new String[]{info.uuid.toString()});
1052 }
1053 db.setTransactionSuccessful();
1054 db.endTransaction();
1055 }
1056
1057 public List<FilePathInfo> getFilePathInfo() {
1058 final SQLiteDatabase db = this.getReadableDatabase();
1059 final Cursor cursor = db.query(Message.TABLENAME, new String[]{Message.UUID, Message.RELATIVE_FILE_PATH, Message.DELETED}, "type in (1,2,5) and " + Message.RELATIVE_FILE_PATH + " is not null", null, null, null, null);
1060 final List<FilePathInfo> list = new ArrayList<>();
1061 while (cursor != null && cursor.moveToNext()) {
1062 list.add(new FilePathInfo(cursor.getString(0), cursor.getString(1), cursor.getInt(2) > 0));
1063 }
1064 if (cursor != null) {
1065 cursor.close();
1066 }
1067 return list;
1068 }
1069
1070 public List<FilePath> getRelativeFilePaths(String account, Jid jid, int limit) {
1071 SQLiteDatabase db = this.getReadableDatabase();
1072 final String SQL = "select uuid,relativeFilePath from messages where type in (1,2,5) and deleted=0 and " + Message.RELATIVE_FILE_PATH + " is not null and conversationUuid=(select uuid from conversations where accountUuid=? and (contactJid=? or contactJid like ?)) order by timeSent desc";
1073 final String[] args = {account, jid.toString(), jid.toString() + "/%"};
1074 Cursor cursor = db.rawQuery(SQL + (limit > 0 ? " limit " + limit : ""), args);
1075 List<FilePath> filesPaths = new ArrayList<>();
1076 while (cursor.moveToNext()) {
1077 filesPaths.add(new FilePath(cursor.getString(0), cursor.getString(1)));
1078 }
1079 cursor.close();
1080 return filesPaths;
1081 }
1082
1083 public static class FilePath {
1084 public final UUID uuid;
1085 public final String path;
1086
1087 private FilePath(String uuid, String path) {
1088 this.uuid = UUID.fromString(uuid);
1089 this.path = path;
1090 }
1091 }
1092
1093 public static class FilePathInfo extends FilePath {
1094 public boolean deleted;
1095
1096 private FilePathInfo(String uuid, String path, boolean deleted) {
1097 super(uuid, path);
1098 this.deleted = deleted;
1099 }
1100
1101 public boolean setDeleted(boolean deleted) {
1102 final boolean changed = deleted != this.deleted;
1103 this.deleted = deleted;
1104 return changed;
1105 }
1106 }
1107
1108 public Conversation findConversation(final Account account, final Jid contactJid) {
1109 SQLiteDatabase db = this.getReadableDatabase();
1110 String[] selectionArgs = {account.getUuid(),
1111 contactJid.asBareJid().toString() + "/%",
1112 contactJid.asBareJid().toString()
1113 };
1114 try(final Cursor cursor = db.query(Conversation.TABLENAME, null,
1115 Conversation.ACCOUNT + "=? AND (" + Conversation.CONTACTJID
1116 + " like ? OR " + Conversation.CONTACTJID + "=?)", selectionArgs, null, null, null)) {
1117 if (cursor.getCount() == 0) {
1118 return null;
1119 }
1120 cursor.moveToFirst();
1121 final Conversation conversation = Conversation.fromCursor(cursor);
1122 if (conversation.getJid() instanceof InvalidJid) {
1123 return null;
1124 }
1125 return conversation;
1126 }
1127 }
1128
1129 public void updateConversation(final Conversation conversation) {
1130 final SQLiteDatabase db = this.getWritableDatabase();
1131 final String[] args = {conversation.getUuid()};
1132 db.update(Conversation.TABLENAME, conversation.getContentValues(),
1133 Conversation.UUID + "=?", args);
1134 }
1135
1136 public List<Account> getAccounts() {
1137 SQLiteDatabase db = this.getReadableDatabase();
1138 return getAccounts(db);
1139 }
1140
1141 public List<Jid> getAccountJids(final boolean enabledOnly) {
1142 final SQLiteDatabase db = this.getReadableDatabase();
1143 final List<Jid> jids = new ArrayList<>();
1144 final String[] columns = new String[]{Account.USERNAME, Account.SERVER};
1145 final String where = enabledOnly ? "not options & (1 <<1)" : null;
1146 try (final Cursor cursor = db.query(Account.TABLENAME, columns, where, null, null, null, null)) {
1147 while (cursor != null && cursor.moveToNext()) {
1148 jids.add(Jid.of(cursor.getString(0), cursor.getString(1), null));
1149 }
1150 } catch (final Exception e) {
1151 return jids;
1152 }
1153 return jids;
1154 }
1155
1156 private List<Account> getAccounts(SQLiteDatabase db) {
1157 final List<Account> list = new ArrayList<>();
1158 try (final Cursor cursor =
1159 db.query(Account.TABLENAME, null, null, null, null, null, null)) {
1160 while (cursor != null && cursor.moveToNext()) {
1161 list.add(Account.fromCursor(cursor));
1162 }
1163 }
1164 return list;
1165 }
1166
1167 public boolean updateAccount(Account account) {
1168 SQLiteDatabase db = this.getWritableDatabase();
1169 String[] args = {account.getUuid()};
1170 final int rows = db.update(Account.TABLENAME, account.getContentValues(), Account.UUID + "=?", args);
1171 return rows == 1;
1172 }
1173
1174 public boolean deleteAccount(Account account) {
1175 SQLiteDatabase db = this.getWritableDatabase();
1176 String[] args = {account.getUuid()};
1177 final int rows = db.delete(Account.TABLENAME, Account.UUID + "=?", args);
1178 return rows == 1;
1179 }
1180
1181 public boolean updateMessage(Message message, boolean includeBody) {
1182 SQLiteDatabase db = this.getWritableDatabase();
1183 String[] args = {message.getUuid()};
1184 ContentValues contentValues = message.getContentValues();
1185 contentValues.remove(Message.UUID);
1186 if (!includeBody) {
1187 contentValues.remove(Message.BODY);
1188 }
1189 return db.update(Message.TABLENAME, message.getContentValues(), Message.UUID + "=?", args) == 1 &&
1190 db.update("cheogram." + Message.TABLENAME, message.getCheogramContentValues(), Message.UUID + "=?", args) == 1;
1191 }
1192
1193 public boolean updateMessage(Message message, String uuid) {
1194 SQLiteDatabase db = this.getWritableDatabase();
1195 String[] args = {uuid};
1196 return db.update(Message.TABLENAME, message.getContentValues(), Message.UUID + "=?", args) == 1 &&
1197 db.update("cheogram." + Message.TABLENAME, message.getCheogramContentValues(), Message.UUID + "=?", args) == 1;
1198 }
1199
1200 public void readRoster(Roster roster) {
1201 final SQLiteDatabase db = this.getReadableDatabase();
1202 final String[] args = {roster.getAccount().getUuid()};
1203 try (final Cursor cursor =
1204 db.query(Contact.TABLENAME, null, Contact.ACCOUNT + "=?", args, null, null, null)) {
1205 while (cursor.moveToNext()) {
1206 roster.initContact(Contact.fromCursor(cursor));
1207 }
1208 }
1209 }
1210
1211 public void writeRoster(final Roster roster) {
1212 long start = SystemClock.elapsedRealtime();
1213 final Account account = roster.getAccount();
1214 final SQLiteDatabase db = this.getWritableDatabase();
1215 db.beginTransaction();
1216 for (Contact contact : roster.getContacts()) {
1217 if (contact.getOption(Contact.Options.IN_ROSTER) || contact.hasAvatarOrPresenceName() || contact.getOption(Contact.Options.SYNCED_VIA_OTHER)) {
1218 db.insert(Contact.TABLENAME, null, contact.getContentValues());
1219 } else {
1220 String where = Contact.ACCOUNT + "=? AND " + Contact.JID + "=?";
1221 String[] whereArgs = {account.getUuid(), contact.getJid().toString()};
1222 db.delete(Contact.TABLENAME, where, whereArgs);
1223 }
1224 }
1225 db.setTransactionSuccessful();
1226 db.endTransaction();
1227 account.setRosterVersion(roster.getVersion());
1228 updateAccount(account);
1229 long duration = SystemClock.elapsedRealtime() - start;
1230 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": persisted roster in " + duration + "ms");
1231 }
1232
1233 public void deleteMessagesInConversation(Conversation conversation) {
1234 long start = SystemClock.elapsedRealtime();
1235 final SQLiteDatabase db = this.getWritableDatabase();
1236 db.beginTransaction();
1237 final String[] args = {conversation.getUuid()};
1238 int num = db.delete(Message.TABLENAME, Message.CONVERSATION + "=?", args);
1239 db.setTransactionSuccessful();
1240 db.endTransaction();
1241 Log.d(Config.LOGTAG, "deleted " + num + " messages for " + conversation.getJid().asBareJid() + " in " + (SystemClock.elapsedRealtime() - start) + "ms");
1242 }
1243
1244 public void expireOldMessages(long timestamp) {
1245 final String[] args = {String.valueOf(timestamp)};
1246 SQLiteDatabase db = this.getReadableDatabase();
1247 db.beginTransaction();
1248 db.delete(Message.TABLENAME, "timeSent<?", args);
1249 db.setTransactionSuccessful();
1250 db.endTransaction();
1251 }
1252
1253 public MamReference getLastMessageReceived(Account account) {
1254 Cursor cursor = null;
1255 try {
1256 SQLiteDatabase db = this.getReadableDatabase();
1257 String sql = "select messages.timeSent,messages.serverMsgId from accounts join conversations on accounts.uuid=conversations.accountUuid join messages on conversations.uuid=messages.conversationUuid where accounts.uuid=? and (messages.status=0 or messages.carbon=1 or messages.serverMsgId not null) and (conversations.mode=0 or (messages.serverMsgId not null and messages.type=4)) order by messages.timesent desc limit 1";
1258 String[] args = {account.getUuid()};
1259 cursor = db.rawQuery(sql, args);
1260 if (cursor.getCount() == 0) {
1261 return null;
1262 } else {
1263 cursor.moveToFirst();
1264 return new MamReference(cursor.getLong(0), cursor.getString(1));
1265 }
1266 } catch (Exception e) {
1267 return null;
1268 } finally {
1269 if (cursor != null) {
1270 cursor.close();
1271 }
1272 }
1273 }
1274
1275 public long getLastTimeFingerprintUsed(Account account, String fingerprint) {
1276 String SQL = "select messages.timeSent from accounts join conversations on accounts.uuid=conversations.accountUuid join messages on conversations.uuid=messages.conversationUuid where accounts.uuid=? and messages.axolotl_fingerprint=? order by messages.timesent desc limit 1";
1277 String[] args = {account.getUuid(), fingerprint};
1278 Cursor cursor = getReadableDatabase().rawQuery(SQL, args);
1279 long time;
1280 if (cursor.moveToFirst()) {
1281 time = cursor.getLong(0);
1282 } else {
1283 time = 0;
1284 }
1285 cursor.close();
1286 return time;
1287 }
1288
1289 public MamReference getLastClearDate(Account account) {
1290 SQLiteDatabase db = this.getReadableDatabase();
1291 String[] columns = {Conversation.ATTRIBUTES};
1292 String selection = Conversation.ACCOUNT + "=?";
1293 String[] args = {account.getUuid()};
1294 Cursor cursor = db.query(Conversation.TABLENAME, columns, selection, args, null, null, null);
1295 MamReference maxClearDate = new MamReference(0);
1296 while (cursor.moveToNext()) {
1297 try {
1298 final JSONObject o = new JSONObject(cursor.getString(0));
1299 maxClearDate = MamReference.max(maxClearDate, MamReference.fromAttribute(o.getString(Conversation.ATTRIBUTE_LAST_CLEAR_HISTORY)));
1300 } catch (Exception e) {
1301 //ignored
1302 }
1303 }
1304 cursor.close();
1305 return maxClearDate;
1306 }
1307
1308 private Cursor getCursorForSession(Account account, SignalProtocolAddress contact) {
1309 final SQLiteDatabase db = this.getReadableDatabase();
1310 String[] selectionArgs = {account.getUuid(),
1311 contact.getName(),
1312 Integer.toString(contact.getDeviceId())};
1313 return db.query(SQLiteAxolotlStore.SESSION_TABLENAME,
1314 null,
1315 SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1316 + SQLiteAxolotlStore.NAME + " = ? AND "
1317 + SQLiteAxolotlStore.DEVICE_ID + " = ? ",
1318 selectionArgs,
1319 null, null, null);
1320 }
1321
1322 public SessionRecord loadSession(Account account, SignalProtocolAddress contact) {
1323 SessionRecord session = null;
1324 Cursor cursor = getCursorForSession(account, contact);
1325 if (cursor.getCount() != 0) {
1326 cursor.moveToFirst();
1327 try {
1328 session = new SessionRecord(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT));
1329 } catch (IOException e) {
1330 cursor.close();
1331 throw new AssertionError(e);
1332 }
1333 }
1334 cursor.close();
1335 return session;
1336 }
1337
1338 public List<Integer> getSubDeviceSessions(Account account, SignalProtocolAddress contact) {
1339 final SQLiteDatabase db = this.getReadableDatabase();
1340 return getSubDeviceSessions(db, account, contact);
1341 }
1342
1343 private List<Integer> getSubDeviceSessions(SQLiteDatabase db, Account account, SignalProtocolAddress contact) {
1344 List<Integer> devices = new ArrayList<>();
1345 String[] columns = {SQLiteAxolotlStore.DEVICE_ID};
1346 String[] selectionArgs = {account.getUuid(),
1347 contact.getName()};
1348 Cursor cursor = db.query(SQLiteAxolotlStore.SESSION_TABLENAME,
1349 columns,
1350 SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1351 + SQLiteAxolotlStore.NAME + " = ?",
1352 selectionArgs,
1353 null, null, null);
1354
1355 while (cursor.moveToNext()) {
1356 devices.add(cursor.getInt(
1357 cursor.getColumnIndex(SQLiteAxolotlStore.DEVICE_ID)));
1358 }
1359
1360 cursor.close();
1361 return devices;
1362 }
1363
1364 public List<String> getKnownSignalAddresses(Account account) {
1365 List<String> addresses = new ArrayList<>();
1366 String[] colums = {"DISTINCT " + SQLiteAxolotlStore.NAME};
1367 String[] selectionArgs = {account.getUuid()};
1368 Cursor cursor = getReadableDatabase().query(SQLiteAxolotlStore.SESSION_TABLENAME,
1369 colums,
1370 SQLiteAxolotlStore.ACCOUNT + " = ?",
1371 selectionArgs,
1372 null, null, null
1373 );
1374 while (cursor.moveToNext()) {
1375 addresses.add(cursor.getString(0));
1376 }
1377 cursor.close();
1378 return addresses;
1379 }
1380
1381 public boolean containsSession(Account account, SignalProtocolAddress contact) {
1382 Cursor cursor = getCursorForSession(account, contact);
1383 int count = cursor.getCount();
1384 cursor.close();
1385 return count != 0;
1386 }
1387
1388 public void storeSession(Account account, SignalProtocolAddress contact, SessionRecord session) {
1389 SQLiteDatabase db = this.getWritableDatabase();
1390 ContentValues values = new ContentValues();
1391 values.put(SQLiteAxolotlStore.NAME, contact.getName());
1392 values.put(SQLiteAxolotlStore.DEVICE_ID, contact.getDeviceId());
1393 values.put(SQLiteAxolotlStore.KEY, Base64.encodeToString(session.serialize(), Base64.DEFAULT));
1394 values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1395 db.insert(SQLiteAxolotlStore.SESSION_TABLENAME, null, values);
1396 }
1397
1398 public void deleteSession(Account account, SignalProtocolAddress contact) {
1399 SQLiteDatabase db = this.getWritableDatabase();
1400 deleteSession(db, account, contact);
1401 }
1402
1403 private void deleteSession(SQLiteDatabase db, Account account, SignalProtocolAddress contact) {
1404 String[] args = {account.getUuid(),
1405 contact.getName(),
1406 Integer.toString(contact.getDeviceId())};
1407 db.delete(SQLiteAxolotlStore.SESSION_TABLENAME,
1408 SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1409 + SQLiteAxolotlStore.NAME + " = ? AND "
1410 + SQLiteAxolotlStore.DEVICE_ID + " = ? ",
1411 args);
1412 }
1413
1414 public void deleteAllSessions(Account account, SignalProtocolAddress contact) {
1415 SQLiteDatabase db = this.getWritableDatabase();
1416 String[] args = {account.getUuid(), contact.getName()};
1417 db.delete(SQLiteAxolotlStore.SESSION_TABLENAME,
1418 SQLiteAxolotlStore.ACCOUNT + "=? AND "
1419 + SQLiteAxolotlStore.NAME + " = ?",
1420 args);
1421 }
1422
1423 private Cursor getCursorForPreKey(Account account, int preKeyId) {
1424 SQLiteDatabase db = this.getReadableDatabase();
1425 String[] columns = {SQLiteAxolotlStore.KEY};
1426 String[] selectionArgs = {account.getUuid(), Integer.toString(preKeyId)};
1427 Cursor cursor = db.query(SQLiteAxolotlStore.PREKEY_TABLENAME,
1428 columns,
1429 SQLiteAxolotlStore.ACCOUNT + "=? AND "
1430 + SQLiteAxolotlStore.ID + "=?",
1431 selectionArgs,
1432 null, null, null);
1433
1434 return cursor;
1435 }
1436
1437 public PreKeyRecord loadPreKey(Account account, int preKeyId) {
1438 PreKeyRecord record = null;
1439 Cursor cursor = getCursorForPreKey(account, preKeyId);
1440 if (cursor.getCount() != 0) {
1441 cursor.moveToFirst();
1442 try {
1443 record = new PreKeyRecord(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT));
1444 } catch (IOException e) {
1445 throw new AssertionError(e);
1446 }
1447 }
1448 cursor.close();
1449 return record;
1450 }
1451
1452 public boolean containsPreKey(Account account, int preKeyId) {
1453 Cursor cursor = getCursorForPreKey(account, preKeyId);
1454 int count = cursor.getCount();
1455 cursor.close();
1456 return count != 0;
1457 }
1458
1459 public void storePreKey(Account account, PreKeyRecord record) {
1460 SQLiteDatabase db = this.getWritableDatabase();
1461 ContentValues values = new ContentValues();
1462 values.put(SQLiteAxolotlStore.ID, record.getId());
1463 values.put(SQLiteAxolotlStore.KEY, Base64.encodeToString(record.serialize(), Base64.DEFAULT));
1464 values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1465 db.insert(SQLiteAxolotlStore.PREKEY_TABLENAME, null, values);
1466 }
1467
1468 public int deletePreKey(Account account, int preKeyId) {
1469 SQLiteDatabase db = this.getWritableDatabase();
1470 String[] args = {account.getUuid(), Integer.toString(preKeyId)};
1471 return db.delete(SQLiteAxolotlStore.PREKEY_TABLENAME,
1472 SQLiteAxolotlStore.ACCOUNT + "=? AND "
1473 + SQLiteAxolotlStore.ID + "=?",
1474 args);
1475 }
1476
1477 private Cursor getCursorForSignedPreKey(Account account, int signedPreKeyId) {
1478 SQLiteDatabase db = this.getReadableDatabase();
1479 String[] columns = {SQLiteAxolotlStore.KEY};
1480 String[] selectionArgs = {account.getUuid(), Integer.toString(signedPreKeyId)};
1481 Cursor cursor = db.query(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1482 columns,
1483 SQLiteAxolotlStore.ACCOUNT + "=? AND " + SQLiteAxolotlStore.ID + "=?",
1484 selectionArgs,
1485 null, null, null);
1486
1487 return cursor;
1488 }
1489
1490 public SignedPreKeyRecord loadSignedPreKey(Account account, int signedPreKeyId) {
1491 SignedPreKeyRecord record = null;
1492 Cursor cursor = getCursorForSignedPreKey(account, signedPreKeyId);
1493 if (cursor.getCount() != 0) {
1494 cursor.moveToFirst();
1495 try {
1496 record = new SignedPreKeyRecord(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT));
1497 } catch (IOException e) {
1498 throw new AssertionError(e);
1499 }
1500 }
1501 cursor.close();
1502 return record;
1503 }
1504
1505 public List<SignedPreKeyRecord> loadSignedPreKeys(Account account) {
1506 List<SignedPreKeyRecord> prekeys = new ArrayList<>();
1507 SQLiteDatabase db = this.getReadableDatabase();
1508 String[] columns = {SQLiteAxolotlStore.KEY};
1509 String[] selectionArgs = {account.getUuid()};
1510 Cursor cursor = db.query(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1511 columns,
1512 SQLiteAxolotlStore.ACCOUNT + "=?",
1513 selectionArgs,
1514 null, null, null);
1515
1516 while (cursor.moveToNext()) {
1517 try {
1518 prekeys.add(new SignedPreKeyRecord(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT)));
1519 } catch (IOException ignored) {
1520 }
1521 }
1522 cursor.close();
1523 return prekeys;
1524 }
1525
1526 public int getSignedPreKeysCount(Account account) {
1527 String[] columns = {"count(" + SQLiteAxolotlStore.KEY + ")"};
1528 String[] selectionArgs = {account.getUuid()};
1529 SQLiteDatabase db = this.getReadableDatabase();
1530 Cursor cursor = db.query(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1531 columns,
1532 SQLiteAxolotlStore.ACCOUNT + "=?",
1533 selectionArgs,
1534 null, null, null);
1535 final int count;
1536 if (cursor.moveToFirst()) {
1537 count = cursor.getInt(0);
1538 } else {
1539 count = 0;
1540 }
1541 cursor.close();
1542 return count;
1543 }
1544
1545 public boolean containsSignedPreKey(Account account, int signedPreKeyId) {
1546 Cursor cursor = getCursorForPreKey(account, signedPreKeyId);
1547 int count = cursor.getCount();
1548 cursor.close();
1549 return count != 0;
1550 }
1551
1552 public void storeSignedPreKey(Account account, SignedPreKeyRecord record) {
1553 SQLiteDatabase db = this.getWritableDatabase();
1554 ContentValues values = new ContentValues();
1555 values.put(SQLiteAxolotlStore.ID, record.getId());
1556 values.put(SQLiteAxolotlStore.KEY, Base64.encodeToString(record.serialize(), Base64.DEFAULT));
1557 values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1558 db.insert(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME, null, values);
1559 }
1560
1561 public void deleteSignedPreKey(Account account, int signedPreKeyId) {
1562 SQLiteDatabase db = this.getWritableDatabase();
1563 String[] args = {account.getUuid(), Integer.toString(signedPreKeyId)};
1564 db.delete(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1565 SQLiteAxolotlStore.ACCOUNT + "=? AND "
1566 + SQLiteAxolotlStore.ID + "=?",
1567 args);
1568 }
1569
1570 private Cursor getIdentityKeyCursor(Account account, String name, boolean own) {
1571 final SQLiteDatabase db = this.getReadableDatabase();
1572 return getIdentityKeyCursor(db, account, name, own);
1573 }
1574
1575 private Cursor getIdentityKeyCursor(SQLiteDatabase db, Account account, String name, boolean own) {
1576 return getIdentityKeyCursor(db, account, name, own, null);
1577 }
1578
1579 private Cursor getIdentityKeyCursor(Account account, String fingerprint) {
1580 final SQLiteDatabase db = this.getReadableDatabase();
1581 return getIdentityKeyCursor(db, account, fingerprint);
1582 }
1583
1584 private Cursor getIdentityKeyCursor(SQLiteDatabase db, Account account, String fingerprint) {
1585 return getIdentityKeyCursor(db, account, null, null, fingerprint);
1586 }
1587
1588 private Cursor getIdentityKeyCursor(SQLiteDatabase db, Account account, String name, Boolean own, String fingerprint) {
1589 String[] columns = {SQLiteAxolotlStore.TRUST,
1590 SQLiteAxolotlStore.ACTIVE,
1591 SQLiteAxolotlStore.LAST_ACTIVATION,
1592 SQLiteAxolotlStore.KEY};
1593 ArrayList<String> selectionArgs = new ArrayList<>(4);
1594 selectionArgs.add(account.getUuid());
1595 String selectionString = SQLiteAxolotlStore.ACCOUNT + " = ?";
1596 if (name != null) {
1597 selectionArgs.add(name);
1598 selectionString += " AND " + SQLiteAxolotlStore.NAME + " = ?";
1599 }
1600 if (fingerprint != null) {
1601 selectionArgs.add(fingerprint);
1602 selectionString += " AND " + SQLiteAxolotlStore.FINGERPRINT + " = ?";
1603 }
1604 if (own != null) {
1605 selectionArgs.add(own ? "1" : "0");
1606 selectionString += " AND " + SQLiteAxolotlStore.OWN + " = ?";
1607 }
1608 Cursor cursor = db.query(SQLiteAxolotlStore.IDENTITIES_TABLENAME,
1609 columns,
1610 selectionString,
1611 selectionArgs.toArray(new String[selectionArgs.size()]),
1612 null, null, null);
1613
1614 return cursor;
1615 }
1616
1617 public IdentityKeyPair loadOwnIdentityKeyPair(Account account) {
1618 SQLiteDatabase db = getReadableDatabase();
1619 return loadOwnIdentityKeyPair(db, account);
1620 }
1621
1622 private IdentityKeyPair loadOwnIdentityKeyPair(SQLiteDatabase db, Account account) {
1623 String name = account.getJid().asBareJid().toString();
1624 IdentityKeyPair identityKeyPair = null;
1625 Cursor cursor = getIdentityKeyCursor(db, account, name, true);
1626 if (cursor.getCount() != 0) {
1627 cursor.moveToFirst();
1628 try {
1629 identityKeyPair = new IdentityKeyPair(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT));
1630 } catch (InvalidKeyException e) {
1631 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Encountered invalid IdentityKey in database for account" + account.getJid().asBareJid() + ", address: " + name);
1632 }
1633 }
1634 cursor.close();
1635
1636 return identityKeyPair;
1637 }
1638
1639 public Set<IdentityKey> loadIdentityKeys(Account account, String name) {
1640 return loadIdentityKeys(account, name, null);
1641 }
1642
1643 public Set<IdentityKey> loadIdentityKeys(Account account, String name, FingerprintStatus status) {
1644 Set<IdentityKey> identityKeys = new HashSet<>();
1645 Cursor cursor = getIdentityKeyCursor(account, name, false);
1646
1647 while (cursor.moveToNext()) {
1648 if (status != null && !FingerprintStatus.fromCursor(cursor).equals(status)) {
1649 continue;
1650 }
1651 try {
1652 String key = cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY));
1653 if (key != null) {
1654 identityKeys.add(new IdentityKey(Base64.decode(key, Base64.DEFAULT), 0));
1655 } else {
1656 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Missing key (possibly preverified) in database for account" + account.getJid().asBareJid() + ", address: " + name);
1657 }
1658 } catch (InvalidKeyException e) {
1659 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Encountered invalid IdentityKey in database for account" + account.getJid().asBareJid() + ", address: " + name);
1660 }
1661 }
1662 cursor.close();
1663
1664 return identityKeys;
1665 }
1666
1667 public long numTrustedKeys(Account account, String name) {
1668 SQLiteDatabase db = getReadableDatabase();
1669 String[] args = {
1670 account.getUuid(),
1671 name,
1672 FingerprintStatus.Trust.TRUSTED.toString(),
1673 FingerprintStatus.Trust.VERIFIED.toString(),
1674 FingerprintStatus.Trust.VERIFIED_X509.toString()
1675 };
1676 return DatabaseUtils.queryNumEntries(db, SQLiteAxolotlStore.IDENTITIES_TABLENAME,
1677 SQLiteAxolotlStore.ACCOUNT + " = ?"
1678 + " AND " + SQLiteAxolotlStore.NAME + " = ?"
1679 + " AND (" + SQLiteAxolotlStore.TRUST + " = ? OR " + SQLiteAxolotlStore.TRUST + " = ? OR " + SQLiteAxolotlStore.TRUST + " = ?)"
1680 + " AND " + SQLiteAxolotlStore.ACTIVE + " > 0",
1681 args
1682 );
1683 }
1684
1685 private void storeIdentityKey(Account account, String name, boolean own, String fingerprint, String base64Serialized, FingerprintStatus status) {
1686 SQLiteDatabase db = this.getWritableDatabase();
1687 ContentValues values = new ContentValues();
1688 values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1689 values.put(SQLiteAxolotlStore.NAME, name);
1690 values.put(SQLiteAxolotlStore.OWN, own ? 1 : 0);
1691 values.put(SQLiteAxolotlStore.FINGERPRINT, fingerprint);
1692 values.put(SQLiteAxolotlStore.KEY, base64Serialized);
1693 values.putAll(status.toContentValues());
1694 String where = SQLiteAxolotlStore.ACCOUNT + "=? AND " + SQLiteAxolotlStore.NAME + "=? AND " + SQLiteAxolotlStore.FINGERPRINT + " =?";
1695 String[] whereArgs = {account.getUuid(), name, fingerprint};
1696 int rows = db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, values, where, whereArgs);
1697 if (rows == 0) {
1698 db.insert(SQLiteAxolotlStore.IDENTITIES_TABLENAME, null, values);
1699 }
1700 }
1701
1702 public void storePreVerification(Account account, String name, String fingerprint, FingerprintStatus status) {
1703 SQLiteDatabase db = this.getWritableDatabase();
1704 ContentValues values = new ContentValues();
1705 values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
1706 values.put(SQLiteAxolotlStore.NAME, name);
1707 values.put(SQLiteAxolotlStore.OWN, 0);
1708 values.put(SQLiteAxolotlStore.FINGERPRINT, fingerprint);
1709 values.putAll(status.toContentValues());
1710 db.insert(SQLiteAxolotlStore.IDENTITIES_TABLENAME, null, values);
1711 }
1712
1713 public FingerprintStatus getFingerprintStatus(Account account, String fingerprint) {
1714 Cursor cursor = getIdentityKeyCursor(account, fingerprint);
1715 final FingerprintStatus status;
1716 if (cursor.getCount() > 0) {
1717 cursor.moveToFirst();
1718 status = FingerprintStatus.fromCursor(cursor);
1719 } else {
1720 status = null;
1721 }
1722 cursor.close();
1723 return status;
1724 }
1725
1726 public boolean setIdentityKeyTrust(Account account, String fingerprint, FingerprintStatus fingerprintStatus) {
1727 SQLiteDatabase db = this.getWritableDatabase();
1728 return setIdentityKeyTrust(db, account, fingerprint, fingerprintStatus);
1729 }
1730
1731 private boolean setIdentityKeyTrust(SQLiteDatabase db, Account account, String fingerprint, FingerprintStatus status) {
1732 String[] selectionArgs = {
1733 account.getUuid(),
1734 fingerprint
1735 };
1736 int rows = db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, status.toContentValues(),
1737 SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1738 + SQLiteAxolotlStore.FINGERPRINT + " = ? ",
1739 selectionArgs);
1740 return rows == 1;
1741 }
1742
1743 public boolean setIdentityKeyCertificate(Account account, String fingerprint, X509Certificate x509Certificate) {
1744 SQLiteDatabase db = this.getWritableDatabase();
1745 String[] selectionArgs = {
1746 account.getUuid(),
1747 fingerprint
1748 };
1749 try {
1750 ContentValues values = new ContentValues();
1751 values.put(SQLiteAxolotlStore.CERTIFICATE, x509Certificate.getEncoded());
1752 return db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, values,
1753 SQLiteAxolotlStore.ACCOUNT + " = ? AND "
1754 + SQLiteAxolotlStore.FINGERPRINT + " = ? ",
1755 selectionArgs) == 1;
1756 } catch (CertificateEncodingException e) {
1757 Log.d(Config.LOGTAG, "could not encode certificate");
1758 return false;
1759 }
1760 }
1761
1762 public X509Certificate getIdentityKeyCertifcate(Account account, String fingerprint) {
1763 SQLiteDatabase db = this.getReadableDatabase();
1764 String[] selectionArgs = {
1765 account.getUuid(),
1766 fingerprint
1767 };
1768 String[] colums = {SQLiteAxolotlStore.CERTIFICATE};
1769 String selection = SQLiteAxolotlStore.ACCOUNT + " = ? AND " + SQLiteAxolotlStore.FINGERPRINT + " = ? ";
1770 Cursor cursor = db.query(SQLiteAxolotlStore.IDENTITIES_TABLENAME, colums, selection, selectionArgs, null, null, null);
1771 if (cursor.getCount() < 1) {
1772 return null;
1773 } else {
1774 cursor.moveToFirst();
1775 byte[] certificate = cursor.getBlob(cursor.getColumnIndex(SQLiteAxolotlStore.CERTIFICATE));
1776 cursor.close();
1777 if (certificate == null || certificate.length == 0) {
1778 return null;
1779 }
1780 try {
1781 CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
1782 return (X509Certificate) certificateFactory.generateCertificate(new ByteArrayInputStream(certificate));
1783 } catch (CertificateException e) {
1784 Log.d(Config.LOGTAG, "certificate exception " + e.getMessage());
1785 return null;
1786 }
1787 }
1788 }
1789
1790 public void storeIdentityKey(Account account, String name, IdentityKey identityKey, FingerprintStatus status) {
1791 storeIdentityKey(account, name, false, CryptoHelper.bytesToHex(identityKey.getPublicKey().serialize()), Base64.encodeToString(identityKey.serialize(), Base64.DEFAULT), status);
1792 }
1793
1794 public void storeOwnIdentityKeyPair(Account account, IdentityKeyPair identityKeyPair) {
1795 storeIdentityKey(account, account.getJid().asBareJid().toString(), true, CryptoHelper.bytesToHex(identityKeyPair.getPublicKey().serialize()), Base64.encodeToString(identityKeyPair.serialize(), Base64.DEFAULT), FingerprintStatus.createActiveVerified(false));
1796 }
1797
1798
1799 private void recreateAxolotlDb(SQLiteDatabase db) {
1800 Log.d(Config.LOGTAG, AxolotlService.LOGPREFIX + " : " + ">>> (RE)CREATING AXOLOTL DATABASE <<<");
1801 db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.SESSION_TABLENAME);
1802 db.execSQL(CREATE_SESSIONS_STATEMENT);
1803 db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.PREKEY_TABLENAME);
1804 db.execSQL(CREATE_PREKEYS_STATEMENT);
1805 db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME);
1806 db.execSQL(CREATE_SIGNED_PREKEYS_STATEMENT);
1807 db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.IDENTITIES_TABLENAME);
1808 db.execSQL(CREATE_IDENTITIES_STATEMENT);
1809 }
1810
1811 public void wipeAxolotlDb(Account account) {
1812 String accountName = account.getUuid();
1813 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ">>> WIPING AXOLOTL DATABASE FOR ACCOUNT " + accountName + " <<<");
1814 SQLiteDatabase db = this.getWritableDatabase();
1815 String[] deleteArgs = {
1816 accountName
1817 };
1818 db.delete(SQLiteAxolotlStore.SESSION_TABLENAME,
1819 SQLiteAxolotlStore.ACCOUNT + " = ?",
1820 deleteArgs);
1821 db.delete(SQLiteAxolotlStore.PREKEY_TABLENAME,
1822 SQLiteAxolotlStore.ACCOUNT + " = ?",
1823 deleteArgs);
1824 db.delete(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
1825 SQLiteAxolotlStore.ACCOUNT + " = ?",
1826 deleteArgs);
1827 db.delete(SQLiteAxolotlStore.IDENTITIES_TABLENAME,
1828 SQLiteAxolotlStore.ACCOUNT + " = ?",
1829 deleteArgs);
1830 }
1831
1832 public List<ShortcutService.FrequentContact> getFrequentContacts(int days) {
1833 SQLiteDatabase db = this.getReadableDatabase();
1834 final String SQL = "select " + Conversation.TABLENAME + "." + Conversation.ACCOUNT + "," + Conversation.TABLENAME + "." + Conversation.CONTACTJID + " from " + Conversation.TABLENAME + " join " + Message.TABLENAME + " on conversations.uuid=messages.conversationUuid where messages.status!=0 and carbon==0 and conversations.mode=0 and messages.timeSent>=? group by conversations.uuid order by count(body) desc limit 4;";
1835 String[] whereArgs = new String[]{String.valueOf(System.currentTimeMillis() - (Config.MILLISECONDS_IN_DAY * days))};
1836 Cursor cursor = db.rawQuery(SQL, whereArgs);
1837 ArrayList<ShortcutService.FrequentContact> contacts = new ArrayList<>();
1838 while (cursor.moveToNext()) {
1839 try {
1840 contacts.add(new ShortcutService.FrequentContact(cursor.getString(0), Jid.of(cursor.getString(1))));
1841 } catch (Exception e) {
1842 Log.d(Config.LOGTAG, e.getMessage());
1843 }
1844 }
1845 cursor.close();
1846 return contacts;
1847 }
1848}