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.text.TextUtils;
10import android.os.Build;
11import android.os.Environment;
12import android.os.SystemClock;
13import android.util.Base64;
14import android.util.Log;
15
16import com.cheogram.android.WebxdcUpdate;
17
18import com.google.common.base.Stopwatch;
19import com.google.common.collect.Multimap;
20import com.google.common.collect.HashMultimap;
21
22import org.json.JSONException;
23import org.json.JSONObject;
24import org.whispersystems.libsignal.IdentityKey;
25import org.whispersystems.libsignal.IdentityKeyPair;
26import org.whispersystems.libsignal.InvalidKeyException;
27import org.whispersystems.libsignal.SignalProtocolAddress;
28import org.whispersystems.libsignal.state.PreKeyRecord;
29import org.whispersystems.libsignal.state.SessionRecord;
30import org.whispersystems.libsignal.state.SignedPreKeyRecord;
31
32import java.io.ByteArrayInputStream;
33import java.io.File;
34import java.io.IOException;
35import java.security.cert.CertificateEncodingException;
36import java.security.cert.CertificateException;
37import java.security.cert.CertificateFactory;
38import java.security.cert.X509Certificate;
39import java.util.ArrayList;
40import java.util.Collection;
41import java.util.HashMap;
42import java.util.Hashtable;
43import java.util.HashSet;
44import java.util.List;
45import java.util.Map;
46import java.util.Set;
47import java.util.UUID;
48import java.util.concurrent.CopyOnWriteArrayList;
49
50import io.ipfs.cid.Cid;
51
52import eu.siacs.conversations.Config;
53import eu.siacs.conversations.crypto.axolotl.AxolotlService;
54import eu.siacs.conversations.crypto.axolotl.FingerprintStatus;
55import eu.siacs.conversations.crypto.axolotl.SQLiteAxolotlStore;
56import eu.siacs.conversations.entities.Account;
57import eu.siacs.conversations.entities.Contact;
58import eu.siacs.conversations.entities.Conversation;
59import eu.siacs.conversations.entities.DownloadableFile;
60import eu.siacs.conversations.entities.Message;
61import eu.siacs.conversations.entities.MucOptions;
62import eu.siacs.conversations.entities.PresenceTemplate;
63import eu.siacs.conversations.entities.Roster;
64import eu.siacs.conversations.entities.ServiceDiscoveryResult;
65import eu.siacs.conversations.services.QuickConversationsService;
66import eu.siacs.conversations.services.ShortcutService;
67import eu.siacs.conversations.utils.CryptoHelper;
68import eu.siacs.conversations.utils.CursorUtils;
69import eu.siacs.conversations.utils.FtsUtils;
70import eu.siacs.conversations.utils.MimeUtils;
71import eu.siacs.conversations.utils.Resolver;
72import eu.siacs.conversations.xmpp.InvalidJid;
73import eu.siacs.conversations.xmpp.Jid;
74import eu.siacs.conversations.xmpp.mam.MamReference;
75import java.io.ByteArrayInputStream;
76import java.io.File;
77import java.io.IOException;
78import java.security.cert.CertificateEncodingException;
79import java.security.cert.CertificateException;
80import java.security.cert.CertificateFactory;
81import java.security.cert.X509Certificate;
82import java.util.ArrayList;
83import java.util.HashMap;
84import java.util.HashSet;
85import java.util.List;
86import java.util.Map;
87import java.util.Set;
88import java.util.UUID;
89import java.util.concurrent.CopyOnWriteArrayList;
90import org.json.JSONException;
91import org.json.JSONObject;
92import org.whispersystems.libsignal.IdentityKey;
93import org.whispersystems.libsignal.IdentityKeyPair;
94import org.whispersystems.libsignal.InvalidKeyException;
95import org.whispersystems.libsignal.SignalProtocolAddress;
96import org.whispersystems.libsignal.state.PreKeyRecord;
97import org.whispersystems.libsignal.state.SessionRecord;
98import org.whispersystems.libsignal.state.SignedPreKeyRecord;
99
100public class DatabaseBackend extends SQLiteOpenHelper {
101
102 private static final String DATABASE_NAME = "history";
103 private static final int DATABASE_VERSION = 52;
104
105 private static boolean requiresMessageIndexRebuild = false;
106 private static DatabaseBackend instance = null;
107 private static final String CREATE_CONTATCS_STATEMENT =
108 "create table "
109 + Contact.TABLENAME
110 + "("
111 + Contact.ACCOUNT
112 + " TEXT, "
113 + Contact.SERVERNAME
114 + " TEXT, "
115 + Contact.SYSTEMNAME
116 + " TEXT,"
117 + Contact.PRESENCE_NAME
118 + " TEXT,"
119 + Contact.JID
120 + " TEXT,"
121 + Contact.KEYS
122 + " TEXT,"
123 + Contact.PHOTOURI
124 + " TEXT,"
125 + Contact.OPTIONS
126 + " NUMBER,"
127 + Contact.SYSTEMACCOUNT
128 + " NUMBER, "
129 + Contact.AVATAR
130 + " TEXT, "
131 + Contact.LAST_PRESENCE
132 + " TEXT, "
133 + Contact.LAST_TIME
134 + " NUMBER, "
135 + Contact.RTP_CAPABILITY
136 + " TEXT,"
137 + Contact.GROUPS
138 + " TEXT, FOREIGN KEY("
139 + Contact.ACCOUNT
140 + ") REFERENCES "
141 + Account.TABLENAME
142 + "("
143 + Account.UUID
144 + ") ON DELETE CASCADE, UNIQUE("
145 + Contact.ACCOUNT
146 + ", "
147 + Contact.JID
148 + ") ON CONFLICT REPLACE);";
149
150 private static final String CREATE_DISCOVERY_RESULTS_STATEMENT =
151 "create table "
152 + ServiceDiscoveryResult.TABLENAME
153 + "("
154 + ServiceDiscoveryResult.HASH
155 + " TEXT, "
156 + ServiceDiscoveryResult.VER
157 + " TEXT, "
158 + ServiceDiscoveryResult.RESULT
159 + " TEXT, "
160 + "UNIQUE("
161 + ServiceDiscoveryResult.HASH
162 + ", "
163 + ServiceDiscoveryResult.VER
164 + ") ON CONFLICT REPLACE);";
165
166 private static final String CREATE_PRESENCE_TEMPLATES_STATEMENT =
167 "CREATE TABLE "
168 + PresenceTemplate.TABELNAME
169 + "("
170 + PresenceTemplate.UUID
171 + " TEXT, "
172 + PresenceTemplate.LAST_USED
173 + " NUMBER,"
174 + PresenceTemplate.MESSAGE
175 + " TEXT,"
176 + PresenceTemplate.STATUS
177 + " TEXT,"
178 + "UNIQUE("
179 + PresenceTemplate.MESSAGE
180 + ","
181 + PresenceTemplate.STATUS
182 + ") ON CONFLICT REPLACE);";
183
184 private static final String CREATE_PREKEYS_STATEMENT =
185 "CREATE TABLE "
186 + SQLiteAxolotlStore.PREKEY_TABLENAME
187 + "("
188 + SQLiteAxolotlStore.ACCOUNT
189 + " TEXT, "
190 + SQLiteAxolotlStore.ID
191 + " INTEGER, "
192 + SQLiteAxolotlStore.KEY
193 + " TEXT, FOREIGN KEY("
194 + SQLiteAxolotlStore.ACCOUNT
195 + ") REFERENCES "
196 + Account.TABLENAME
197 + "("
198 + Account.UUID
199 + ") ON DELETE CASCADE, "
200 + "UNIQUE( "
201 + SQLiteAxolotlStore.ACCOUNT
202 + ", "
203 + SQLiteAxolotlStore.ID
204 + ") ON CONFLICT REPLACE"
205 + ");";
206
207 private static final String CREATE_SIGNED_PREKEYS_STATEMENT =
208 "CREATE TABLE "
209 + SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME
210 + "("
211 + SQLiteAxolotlStore.ACCOUNT
212 + " TEXT, "
213 + SQLiteAxolotlStore.ID
214 + " INTEGER, "
215 + SQLiteAxolotlStore.KEY
216 + " TEXT, FOREIGN KEY("
217 + SQLiteAxolotlStore.ACCOUNT
218 + ") REFERENCES "
219 + Account.TABLENAME
220 + "("
221 + Account.UUID
222 + ") ON DELETE CASCADE, "
223 + "UNIQUE( "
224 + SQLiteAxolotlStore.ACCOUNT
225 + ", "
226 + SQLiteAxolotlStore.ID
227 + ") ON CONFLICT REPLACE"
228 + ");";
229
230 private static final String CREATE_SESSIONS_STATEMENT =
231 "CREATE TABLE "
232 + SQLiteAxolotlStore.SESSION_TABLENAME
233 + "("
234 + SQLiteAxolotlStore.ACCOUNT
235 + " TEXT, "
236 + SQLiteAxolotlStore.NAME
237 + " TEXT, "
238 + SQLiteAxolotlStore.DEVICE_ID
239 + " INTEGER, "
240 + SQLiteAxolotlStore.KEY
241 + " TEXT, FOREIGN KEY("
242 + SQLiteAxolotlStore.ACCOUNT
243 + ") REFERENCES "
244 + Account.TABLENAME
245 + "("
246 + Account.UUID
247 + ") ON DELETE CASCADE, "
248 + "UNIQUE( "
249 + SQLiteAxolotlStore.ACCOUNT
250 + ", "
251 + SQLiteAxolotlStore.NAME
252 + ", "
253 + SQLiteAxolotlStore.DEVICE_ID
254 + ") ON CONFLICT REPLACE"
255 + ");";
256
257 private static final String CREATE_IDENTITIES_STATEMENT =
258 "CREATE TABLE "
259 + SQLiteAxolotlStore.IDENTITIES_TABLENAME
260 + "("
261 + SQLiteAxolotlStore.ACCOUNT
262 + " TEXT, "
263 + SQLiteAxolotlStore.NAME
264 + " TEXT, "
265 + SQLiteAxolotlStore.OWN
266 + " INTEGER, "
267 + SQLiteAxolotlStore.FINGERPRINT
268 + " TEXT, "
269 + SQLiteAxolotlStore.CERTIFICATE
270 + " BLOB, "
271 + SQLiteAxolotlStore.TRUST
272 + " TEXT, "
273 + SQLiteAxolotlStore.ACTIVE
274 + " NUMBER, "
275 + SQLiteAxolotlStore.LAST_ACTIVATION
276 + " NUMBER,"
277 + SQLiteAxolotlStore.KEY
278 + " TEXT, FOREIGN KEY("
279 + SQLiteAxolotlStore.ACCOUNT
280 + ") REFERENCES "
281 + Account.TABLENAME
282 + "("
283 + Account.UUID
284 + ") ON DELETE CASCADE, "
285 + "UNIQUE( "
286 + SQLiteAxolotlStore.ACCOUNT
287 + ", "
288 + SQLiteAxolotlStore.NAME
289 + ", "
290 + SQLiteAxolotlStore.FINGERPRINT
291 + ") ON CONFLICT IGNORE"
292 + ");";
293
294 private static final String RESOLVER_RESULTS_TABLENAME = "resolver_results";
295
296 private static final String CREATE_RESOLVER_RESULTS_TABLE =
297 "create table "
298 + RESOLVER_RESULTS_TABLENAME
299 + "("
300 + Resolver.Result.DOMAIN
301 + " TEXT,"
302 + Resolver.Result.HOSTNAME
303 + " TEXT,"
304 + Resolver.Result.IP
305 + " BLOB,"
306 + Resolver.Result.PRIORITY
307 + " NUMBER,"
308 + Resolver.Result.DIRECT_TLS
309 + " NUMBER,"
310 + Resolver.Result.AUTHENTICATED
311 + " NUMBER,"
312 + Resolver.Result.PORT
313 + " NUMBER,"
314 + "UNIQUE("
315 + Resolver.Result.DOMAIN
316 + ") ON CONFLICT REPLACE"
317 + ");";
318
319 private static final String CREATE_MESSAGE_TIME_INDEX =
320 "CREATE INDEX message_time_index ON "
321 + Message.TABLENAME
322 + "("
323 + Message.TIME_SENT
324 + ")";
325 private static final String CREATE_MESSAGE_CONVERSATION_INDEX =
326 "CREATE INDEX message_conversation_index ON "
327 + Message.TABLENAME
328 + "("
329 + Message.CONVERSATION
330 + ")";
331 private static final String CREATE_MESSAGE_DELETED_INDEX =
332 "CREATE INDEX message_deleted_index ON "
333 + Message.TABLENAME
334 + "("
335 + Message.DELETED
336 + ")";
337 private static final String CREATE_MESSAGE_RELATIVE_FILE_PATH_INDEX =
338 "CREATE INDEX message_file_path_index ON "
339 + Message.TABLENAME
340 + "("
341 + Message.RELATIVE_FILE_PATH
342 + ")";
343 private static final String CREATE_MESSAGE_TYPE_INDEX =
344 "CREATE INDEX message_type_index ON " + Message.TABLENAME + "(" + Message.TYPE + ")";
345
346 private static final String CREATE_MESSAGE_INDEX_TABLE =
347 "CREATE VIRTUAL TABLE messages_index USING fts4"
348 + " (uuid,body,notindexed=\"uuid\",content=\""
349 + Message.TABLENAME
350 + "\",tokenize='unicode61')";
351 private static final String CREATE_MESSAGE_INSERT_TRIGGER =
352 "CREATE TRIGGER after_message_insert AFTER INSERT ON "
353 + Message.TABLENAME
354 + " BEGIN INSERT INTO messages_index(rowid,uuid,body)"
355 + " VALUES(NEW.rowid,NEW.uuid,NEW.body); END;";
356 private static final String CREATE_MESSAGE_UPDATE_TRIGGER =
357 "CREATE TRIGGER after_message_update UPDATE OF uuid,body ON "
358 + Message.TABLENAME
359 + " BEGIN UPDATE messages_index SET body=NEW.body,uuid=NEW.uuid WHERE"
360 + " rowid=OLD.rowid; END;";
361 private static final String CREATE_MESSAGE_DELETE_TRIGGER =
362 "CREATE TRIGGER after_message_delete AFTER DELETE ON "
363 + Message.TABLENAME
364 + " BEGIN DELETE FROM messages_index WHERE rowid=OLD.rowid; END;";
365 private static final String COPY_PREEXISTING_ENTRIES =
366 "INSERT INTO messages_index(messages_index) VALUES('rebuild');";
367
368 protected Context context;
369
370 private DatabaseBackend(Context context) {
371 super(context, DATABASE_NAME, null, DATABASE_VERSION);
372 this.context = context;
373 setWriteAheadLoggingEnabled(true);
374 }
375
376 private static ContentValues createFingerprintStatusContentValues(
377 FingerprintStatus.Trust trust, boolean active) {
378 ContentValues values = new ContentValues();
379 values.put(SQLiteAxolotlStore.TRUST, trust.toString());
380 values.put(SQLiteAxolotlStore.ACTIVE, active ? 1 : 0);
381 return values;
382 }
383
384 public static boolean requiresMessageIndexRebuild() {
385 return requiresMessageIndexRebuild;
386 }
387
388 public void rebuildMessagesIndex() {
389 final SQLiteDatabase db = getWritableDatabase();
390 final Stopwatch stopwatch = Stopwatch.createStarted();
391 db.execSQL(COPY_PREEXISTING_ENTRIES);
392 Log.d(Config.LOGTAG, "rebuilt message index in " + stopwatch.stop().toString());
393 }
394
395 public static synchronized DatabaseBackend getInstance(Context context) {
396 if (instance == null) {
397 instance = new DatabaseBackend(context);
398 }
399 return instance;
400 }
401
402 protected void cheogramMigrate(SQLiteDatabase db) {
403 db.beginTransaction();
404
405 try {
406 Cursor cursor = db.rawQuery("PRAGMA cheogram.user_version", null);
407 cursor.moveToNext();
408 int cheogramVersion = cursor.getInt(0);
409 cursor.close();
410
411 if(cheogramVersion < 1) {
412 // No cross-DB foreign keys unfortunately
413 db.execSQL(
414 "CREATE TABLE cheogram." + Message.TABLENAME + "(" +
415 Message.UUID + " TEXT PRIMARY KEY, " +
416 "subject TEXT" +
417 ")"
418 );
419 db.execSQL("PRAGMA cheogram.user_version = 1");
420 }
421
422 if(cheogramVersion < 2) {
423 db.execSQL(
424 "ALTER TABLE cheogram." + Message.TABLENAME + " " +
425 "ADD COLUMN oobUri TEXT"
426 );
427 db.execSQL(
428 "ALTER TABLE cheogram." + Message.TABLENAME + " " +
429 "ADD COLUMN fileParams TEXT"
430 );
431 db.execSQL("PRAGMA cheogram.user_version = 2");
432 }
433
434 if(cheogramVersion < 3) {
435 db.execSQL(
436 "ALTER TABLE cheogram." + Message.TABLENAME + " " +
437 "ADD COLUMN payloads TEXT"
438 );
439 db.execSQL("PRAGMA cheogram.user_version = 3");
440 }
441
442 if(cheogramVersion < 4) {
443 db.execSQL(
444 "CREATE TABLE cheogram.cids (" +
445 "cid TEXT NOT NULL PRIMARY KEY," +
446 "path TEXT NOT NULL" +
447 ")"
448 );
449 db.execSQL("PRAGMA cheogram.user_version = 4");
450 }
451
452 if(cheogramVersion < 5) {
453 db.execSQL(
454 "ALTER TABLE cheogram." + Message.TABLENAME + " " +
455 "ADD COLUMN timeReceived NUMBER"
456 );
457 db.execSQL("CREATE INDEX cheogram.message_time_received_index ON " + Message.TABLENAME + " (timeReceived)");
458 db.execSQL("PRAGMA cheogram.user_version = 5");
459 }
460
461 if(cheogramVersion < 6) {
462 db.execSQL(
463 "CREATE TABLE cheogram.blocked_media (" +
464 "cid TEXT NOT NULL PRIMARY KEY" +
465 ")"
466 );
467 db.execSQL("PRAGMA cheogram.user_version = 6");
468 }
469
470 if(cheogramVersion < 7) {
471 db.execSQL(
472 "ALTER TABLE cheogram.cids " +
473 "ADD COLUMN url TEXT"
474 );
475 db.execSQL("PRAGMA cheogram.user_version = 7");
476 }
477
478 if(cheogramVersion < 8) {
479 db.execSQL(
480 "CREATE TABLE cheogram.webxdc_updates (" +
481 "serial INTEGER PRIMARY KEY AUTOINCREMENT, " +
482 Message.CONVERSATION + " TEXT NOT NULL, " +
483 "sender TEXT NOT NULL, " +
484 "thread TEXT NOT NULL, " +
485 "threadParent TEXT, " +
486 "info TEXT, " +
487 "document TEXT, " +
488 "summary TEXT, " +
489 "payload TEXT" +
490 ")"
491 );
492 db.execSQL("CREATE INDEX cheogram.webxdc_index ON webxdc_updates (" + Message.CONVERSATION + ", thread)");
493 db.execSQL("PRAGMA cheogram.user_version = 8");
494 }
495
496 if(cheogramVersion < 9) {
497 db.execSQL(
498 "ALTER TABLE cheogram.webxdc_updates " +
499 "ADD COLUMN message_id TEXT"
500 );
501 db.execSQL("CREATE UNIQUE INDEX cheogram.webxdc_message_id_index ON webxdc_updates (" + Message.CONVERSATION + ", message_id)");
502 db.execSQL("PRAGMA cheogram.user_version = 9");
503 }
504
505 if(cheogramVersion < 10) {
506 db.execSQL(
507 "CREATE TABLE cheogram.muted_participants (" +
508 "muc_jid TEXT NOT NULL, " +
509 "occupant_id TEXT NOT NULL, " +
510 "nick TEXT NOT NULL," +
511 "PRIMARY KEY (muc_jid, occupant_id)" +
512 ")"
513 );
514 db.execSQL(
515 "ALTER TABLE cheogram." + Message.TABLENAME + " " +
516 "ADD COLUMN occupant_id TEXT"
517 );
518 db.execSQL("PRAGMA cheogram.user_version = 10");
519 }
520
521 if(cheogramVersion < 11) {
522 if (Build.VERSION.SDK_INT >= 34) {
523 db.execSQL(
524 "ALTER TABLE cheogram.muted_participants " +
525 "DROP COLUMN nick"
526 );
527 } else {
528 db.execSQL("DROP TABLE cheogram.muted_participants");
529 db.execSQL(
530 "CREATE TABLE cheogram.muted_participants (" +
531 "muc_jid TEXT NOT NULL, " +
532 "occupant_id TEXT NOT NULL, " +
533 "PRIMARY KEY (muc_jid, occupant_id)" +
534 ")"
535 );
536 }
537 db.execSQL("PRAGMA cheogram.user_version = 11");
538 }
539
540 if(cheogramVersion < 12) {
541 db.execSQL(
542 "ALTER TABLE cheogram." + Message.TABLENAME + " " +
543 "ADD COLUMN notificationDismissed NUMBER DEFAULT 0"
544 );
545 db.execSQL("PRAGMA cheogram.user_version = 12");
546 }
547
548 db.setTransactionSuccessful();
549 } finally {
550 db.endTransaction();
551 }
552 }
553
554 @Override
555 public void onConfigure(SQLiteDatabase db) {
556 db.execSQL("PRAGMA foreign_keys=ON");
557 db.rawQuery("PRAGMA secure_delete=ON", null).close();
558 db.execSQL("ATTACH DATABASE ? AS cheogram", new Object[]{context.getDatabasePath("cheogram").getPath()});
559 cheogramMigrate(db);
560 }
561
562 @Override
563 public void onCreate(SQLiteDatabase db) {
564 db.execSQL(
565 "create table "
566 + Account.TABLENAME
567 + "("
568 + Account.UUID
569 + " TEXT PRIMARY KEY,"
570 + Account.USERNAME
571 + " TEXT,"
572 + Account.SERVER
573 + " TEXT,"
574 + Account.PASSWORD
575 + " TEXT,"
576 + Account.DISPLAY_NAME
577 + " TEXT, "
578 + Account.STATUS
579 + " TEXT,"
580 + Account.STATUS_MESSAGE
581 + " TEXT,"
582 + Account.ROSTERVERSION
583 + " TEXT,"
584 + Account.OPTIONS
585 + " NUMBER, "
586 + Account.AVATAR
587 + " TEXT, "
588 + Account.KEYS
589 + " TEXT, "
590 + Account.HOSTNAME
591 + " TEXT, "
592 + Account.RESOURCE
593 + " TEXT,"
594 + Account.PINNED_MECHANISM
595 + " TEXT,"
596 + Account.PINNED_CHANNEL_BINDING
597 + " TEXT,"
598 + Account.FAST_MECHANISM
599 + " TEXT,"
600 + Account.FAST_TOKEN
601 + " TEXT,"
602 + Account.PORT
603 + " NUMBER DEFAULT 5222)");
604 db.execSQL(
605 "create table "
606 + Conversation.TABLENAME
607 + " ("
608 + Conversation.UUID
609 + " TEXT PRIMARY KEY, "
610 + Conversation.NAME
611 + " TEXT, "
612 + Conversation.CONTACT
613 + " TEXT, "
614 + Conversation.ACCOUNT
615 + " TEXT, "
616 + Conversation.CONTACTJID
617 + " TEXT, "
618 + Conversation.CREATED
619 + " NUMBER, "
620 + Conversation.STATUS
621 + " NUMBER, "
622 + Conversation.MODE
623 + " NUMBER, "
624 + Conversation.ATTRIBUTES
625 + " TEXT, FOREIGN KEY("
626 + Conversation.ACCOUNT
627 + ") REFERENCES "
628 + Account.TABLENAME
629 + "("
630 + Account.UUID
631 + ") ON DELETE CASCADE);");
632 db.execSQL(
633 "create table "
634 + Message.TABLENAME
635 + "( "
636 + Message.UUID
637 + " TEXT PRIMARY KEY, "
638 + Message.CONVERSATION
639 + " TEXT, "
640 + Message.TIME_SENT
641 + " NUMBER, "
642 + Message.COUNTERPART
643 + " TEXT, "
644 + Message.TRUE_COUNTERPART
645 + " TEXT,"
646 + Message.BODY
647 + " TEXT, "
648 + Message.ENCRYPTION
649 + " NUMBER, "
650 + Message.STATUS
651 + " NUMBER,"
652 + Message.TYPE
653 + " NUMBER, "
654 + Message.RELATIVE_FILE_PATH
655 + " TEXT, "
656 + Message.SERVER_MSG_ID
657 + " TEXT, "
658 + Message.FINGERPRINT
659 + " TEXT, "
660 + Message.CARBON
661 + " INTEGER, "
662 + Message.EDITED
663 + " TEXT, "
664 + Message.READ
665 + " NUMBER DEFAULT 1, "
666 + Message.OOB
667 + " INTEGER, "
668 + Message.ERROR_MESSAGE
669 + " TEXT,"
670 + Message.READ_BY_MARKERS
671 + " TEXT,"
672 + Message.MARKABLE
673 + " NUMBER DEFAULT 0,"
674 + Message.DELETED
675 + " NUMBER DEFAULT 0,"
676 + Message.BODY_LANGUAGE
677 + " TEXT,"
678 + Message.OCCUPANT_ID
679 + " TEXT,"
680 + Message.REACTIONS
681 + " TEXT,"
682 + Message.REMOTE_MSG_ID
683 + " TEXT, FOREIGN KEY("
684 + Message.CONVERSATION
685 + ") REFERENCES "
686 + Conversation.TABLENAME
687 + "("
688 + Conversation.UUID
689 + ") ON DELETE CASCADE);");
690 db.execSQL(CREATE_MESSAGE_TIME_INDEX);
691 db.execSQL(CREATE_MESSAGE_CONVERSATION_INDEX);
692 db.execSQL(CREATE_MESSAGE_DELETED_INDEX);
693 db.execSQL(CREATE_MESSAGE_RELATIVE_FILE_PATH_INDEX);
694 db.execSQL(CREATE_MESSAGE_TYPE_INDEX);
695 db.execSQL(CREATE_CONTATCS_STATEMENT);
696 db.execSQL(CREATE_DISCOVERY_RESULTS_STATEMENT);
697 db.execSQL(CREATE_SESSIONS_STATEMENT);
698 db.execSQL(CREATE_PREKEYS_STATEMENT);
699 db.execSQL(CREATE_SIGNED_PREKEYS_STATEMENT);
700 db.execSQL(CREATE_IDENTITIES_STATEMENT);
701 db.execSQL(CREATE_PRESENCE_TEMPLATES_STATEMENT);
702 db.execSQL(CREATE_RESOLVER_RESULTS_TABLE);
703 db.execSQL(CREATE_MESSAGE_INDEX_TABLE);
704 db.execSQL(CREATE_MESSAGE_INSERT_TRIGGER);
705 db.execSQL(CREATE_MESSAGE_UPDATE_TRIGGER);
706 db.execSQL(CREATE_MESSAGE_DELETE_TRIGGER);
707 }
708
709 @Override
710 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
711 if (oldVersion < 2 && newVersion >= 2) {
712 db.execSQL(
713 "update "
714 + Account.TABLENAME
715 + " set "
716 + Account.OPTIONS
717 + " = "
718 + Account.OPTIONS
719 + " | 8");
720 }
721 if (oldVersion < 3 && newVersion >= 3) {
722 db.execSQL(
723 "ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.TYPE + " NUMBER");
724 }
725 if (oldVersion < 5 && newVersion >= 5) {
726 db.execSQL("DROP TABLE " + Contact.TABLENAME);
727 db.execSQL(CREATE_CONTATCS_STATEMENT);
728 db.execSQL("UPDATE " + Account.TABLENAME + " SET " + Account.ROSTERVERSION + " = NULL");
729 }
730 if (oldVersion < 6 && newVersion >= 6) {
731 db.execSQL(
732 "ALTER TABLE "
733 + Message.TABLENAME
734 + " ADD COLUMN "
735 + Message.TRUE_COUNTERPART
736 + " TEXT");
737 }
738 if (oldVersion < 7 && newVersion >= 7) {
739 db.execSQL(
740 "ALTER TABLE "
741 + Message.TABLENAME
742 + " ADD COLUMN "
743 + Message.REMOTE_MSG_ID
744 + " TEXT");
745 db.execSQL(
746 "ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN " + Contact.AVATAR + " TEXT");
747 db.execSQL(
748 "ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.AVATAR + " TEXT");
749 }
750 if (oldVersion < 8 && newVersion >= 8) {
751 db.execSQL(
752 "ALTER TABLE "
753 + Conversation.TABLENAME
754 + " ADD COLUMN "
755 + Conversation.ATTRIBUTES
756 + " TEXT");
757 }
758 if (oldVersion < 9 && newVersion >= 9) {
759 db.execSQL(
760 "ALTER TABLE "
761 + Contact.TABLENAME
762 + " ADD COLUMN "
763 + Contact.LAST_TIME
764 + " NUMBER");
765 db.execSQL(
766 "ALTER TABLE "
767 + Contact.TABLENAME
768 + " ADD COLUMN "
769 + Contact.LAST_PRESENCE
770 + " TEXT");
771 }
772 if (oldVersion < 10 && newVersion >= 10) {
773 db.execSQL(
774 "ALTER TABLE "
775 + Message.TABLENAME
776 + " ADD COLUMN "
777 + Message.RELATIVE_FILE_PATH
778 + " TEXT");
779 }
780 if (oldVersion < 11 && newVersion >= 11) {
781 db.execSQL(
782 "ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN " + Contact.GROUPS + " TEXT");
783 db.execSQL("delete from " + Contact.TABLENAME);
784 db.execSQL("update " + Account.TABLENAME + " set " + Account.ROSTERVERSION + " = NULL");
785 }
786 if (oldVersion < 12 && newVersion >= 12) {
787 db.execSQL(
788 "ALTER TABLE "
789 + Message.TABLENAME
790 + " ADD COLUMN "
791 + Message.SERVER_MSG_ID
792 + " TEXT");
793 }
794 if (oldVersion < 13 && newVersion >= 13) {
795 db.execSQL("delete from " + Contact.TABLENAME);
796 db.execSQL("update " + Account.TABLENAME + " set " + Account.ROSTERVERSION + " = NULL");
797 }
798 if (oldVersion < 14 && newVersion >= 14) {
799 canonicalizeJids(db);
800 }
801 if (oldVersion < 15 && newVersion >= 15) {
802 recreateAxolotlDb(db);
803 db.execSQL(
804 "ALTER TABLE "
805 + Message.TABLENAME
806 + " ADD COLUMN "
807 + Message.FINGERPRINT
808 + " TEXT");
809 }
810 if (oldVersion < 16 && newVersion >= 16) {
811 db.execSQL(
812 "ALTER TABLE "
813 + Message.TABLENAME
814 + " ADD COLUMN "
815 + Message.CARBON
816 + " INTEGER");
817 }
818 if (oldVersion < 19 && newVersion >= 19) {
819 db.execSQL(
820 "ALTER TABLE "
821 + Account.TABLENAME
822 + " ADD COLUMN "
823 + Account.DISPLAY_NAME
824 + " TEXT");
825 }
826 if (oldVersion < 20 && newVersion >= 20) {
827 db.execSQL(
828 "ALTER TABLE "
829 + Account.TABLENAME
830 + " ADD COLUMN "
831 + Account.HOSTNAME
832 + " TEXT");
833 db.execSQL(
834 "ALTER TABLE "
835 + Account.TABLENAME
836 + " ADD COLUMN "
837 + Account.PORT
838 + " NUMBER DEFAULT 5222");
839 }
840 if (oldVersion < 26 && newVersion >= 26) {
841 db.execSQL(
842 "ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.STATUS + " TEXT");
843 db.execSQL(
844 "ALTER TABLE "
845 + Account.TABLENAME
846 + " ADD COLUMN "
847 + Account.STATUS_MESSAGE
848 + " TEXT");
849 }
850 if (oldVersion < 40 && newVersion >= 40) {
851 db.execSQL(
852 "ALTER TABLE "
853 + Account.TABLENAME
854 + " ADD COLUMN "
855 + Account.RESOURCE
856 + " TEXT");
857 }
858 /* Any migrations that alter the Account table need to happen BEFORE this migration, as it
859 * depends on account de-serialization.
860 */
861 if (oldVersion < 17 && newVersion >= 17 && newVersion < 31) {
862 List<Account> accounts = getAccounts(db);
863 for (Account account : accounts) {
864 String ownDeviceIdString =
865 account.getKey(SQLiteAxolotlStore.JSONKEY_REGISTRATION_ID);
866 if (ownDeviceIdString == null) {
867 continue;
868 }
869 int ownDeviceId = Integer.valueOf(ownDeviceIdString);
870 SignalProtocolAddress ownAddress =
871 new SignalProtocolAddress(
872 account.getJid().asBareJid().toString(), ownDeviceId);
873 deleteSession(db, account, ownAddress);
874 IdentityKeyPair identityKeyPair = loadOwnIdentityKeyPair(db, account);
875 if (identityKeyPair != null) {
876 String[] selectionArgs = {
877 account.getUuid(),
878 CryptoHelper.bytesToHex(identityKeyPair.getPublicKey().serialize())
879 };
880 ContentValues values = new ContentValues();
881 values.put(SQLiteAxolotlStore.TRUSTED, 2);
882 db.update(
883 SQLiteAxolotlStore.IDENTITIES_TABLENAME,
884 values,
885 SQLiteAxolotlStore.ACCOUNT
886 + " = ? AND "
887 + SQLiteAxolotlStore.FINGERPRINT
888 + " = ? ",
889 selectionArgs);
890 } else {
891 Log.d(
892 Config.LOGTAG,
893 account.getJid().asBareJid()
894 + ": could not load own identity key pair");
895 }
896 }
897 }
898 if (oldVersion < 18 && newVersion >= 18) {
899 db.execSQL(
900 "ALTER TABLE "
901 + Message.TABLENAME
902 + " ADD COLUMN "
903 + Message.READ
904 + " NUMBER DEFAULT 1");
905 }
906
907 if (oldVersion < 21 && newVersion >= 21) {
908 List<Account> accounts = getAccounts(db);
909 for (Account account : accounts) {
910 account.unsetPgpSignature();
911 db.update(
912 Account.TABLENAME,
913 account.getContentValues(),
914 Account.UUID + "=?",
915 new String[] {account.getUuid()});
916 }
917 }
918
919 if (oldVersion >= 15 && oldVersion < 22 && newVersion >= 22) {
920 db.execSQL(
921 "ALTER TABLE "
922 + SQLiteAxolotlStore.IDENTITIES_TABLENAME
923 + " ADD COLUMN "
924 + SQLiteAxolotlStore.CERTIFICATE);
925 }
926
927 if (oldVersion < 23 && newVersion >= 23) {
928 db.execSQL(CREATE_DISCOVERY_RESULTS_STATEMENT);
929 }
930
931 if (oldVersion < 24 && newVersion >= 24) {
932 db.execSQL(
933 "ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.EDITED + " TEXT");
934 }
935
936 if (oldVersion < 25 && newVersion >= 25) {
937 db.execSQL(
938 "ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.OOB + " INTEGER");
939 }
940
941 if (oldVersion < 26 && newVersion >= 26) {
942 db.execSQL(CREATE_PRESENCE_TEMPLATES_STATEMENT);
943 }
944
945 if (oldVersion < 27 && newVersion >= 27) {
946 db.execSQL("DELETE FROM " + ServiceDiscoveryResult.TABLENAME);
947 }
948
949 if (oldVersion < 28 && newVersion >= 28) {
950 canonicalizeJids(db);
951 }
952
953 if (oldVersion < 29 && newVersion >= 29) {
954 db.execSQL(
955 "ALTER TABLE "
956 + Message.TABLENAME
957 + " ADD COLUMN "
958 + Message.ERROR_MESSAGE
959 + " TEXT");
960 }
961 if (oldVersion >= 15 && oldVersion < 31 && newVersion >= 31) {
962 db.execSQL(
963 "ALTER TABLE "
964 + SQLiteAxolotlStore.IDENTITIES_TABLENAME
965 + " ADD COLUMN "
966 + SQLiteAxolotlStore.TRUST
967 + " TEXT");
968 db.execSQL(
969 "ALTER TABLE "
970 + SQLiteAxolotlStore.IDENTITIES_TABLENAME
971 + " ADD COLUMN "
972 + SQLiteAxolotlStore.ACTIVE
973 + " NUMBER");
974 HashMap<Integer, ContentValues> migration = new HashMap<>();
975 migration.put(
976 0, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, true));
977 migration.put(
978 1, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, true));
979 migration.put(
980 2,
981 createFingerprintStatusContentValues(FingerprintStatus.Trust.UNTRUSTED, true));
982 migration.put(
983 3,
984 createFingerprintStatusContentValues(
985 FingerprintStatus.Trust.COMPROMISED, false));
986 migration.put(
987 4,
988 createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, false));
989 migration.put(
990 5,
991 createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, false));
992 migration.put(
993 6,
994 createFingerprintStatusContentValues(FingerprintStatus.Trust.UNTRUSTED, false));
995 migration.put(
996 7,
997 createFingerprintStatusContentValues(
998 FingerprintStatus.Trust.VERIFIED_X509, true));
999 migration.put(
1000 8,
1001 createFingerprintStatusContentValues(
1002 FingerprintStatus.Trust.VERIFIED_X509, false));
1003 for (Map.Entry<Integer, ContentValues> entry : migration.entrySet()) {
1004 String whereClause = SQLiteAxolotlStore.TRUSTED + "=?";
1005 String[] where = {String.valueOf(entry.getKey())};
1006 db.update(
1007 SQLiteAxolotlStore.IDENTITIES_TABLENAME,
1008 entry.getValue(),
1009 whereClause,
1010 where);
1011 }
1012 }
1013 if (oldVersion >= 15 && oldVersion < 32 && newVersion >= 32) {
1014 db.execSQL(
1015 "ALTER TABLE "
1016 + SQLiteAxolotlStore.IDENTITIES_TABLENAME
1017 + " ADD COLUMN "
1018 + SQLiteAxolotlStore.LAST_ACTIVATION
1019 + " NUMBER");
1020 ContentValues defaults = new ContentValues();
1021 defaults.put(SQLiteAxolotlStore.LAST_ACTIVATION, System.currentTimeMillis());
1022 db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, defaults, null, null);
1023 }
1024 if (oldVersion >= 15 && oldVersion < 33 && newVersion >= 33) {
1025 String whereClause = SQLiteAxolotlStore.OWN + "=1";
1026 db.update(
1027 SQLiteAxolotlStore.IDENTITIES_TABLENAME,
1028 createFingerprintStatusContentValues(FingerprintStatus.Trust.VERIFIED, true),
1029 whereClause,
1030 null);
1031 }
1032
1033 if (oldVersion < 34 && newVersion >= 34) {
1034 db.execSQL(CREATE_MESSAGE_TIME_INDEX);
1035
1036 final File oldPicturesDirectory =
1037 new File(
1038 Environment.getExternalStoragePublicDirectory(
1039 Environment.DIRECTORY_PICTURES)
1040 + "/Conversations/");
1041 final File oldFilesDirectory =
1042 new File(Environment.getExternalStorageDirectory() + "/Conversations/");
1043 final File newFilesDirectory =
1044 new File(
1045 Environment.getExternalStorageDirectory()
1046 + "/Conversations/Media/Conversations Files/");
1047 final File newVideosDirectory =
1048 new File(
1049 Environment.getExternalStorageDirectory()
1050 + "/Conversations/Media/Conversations Videos/");
1051 if (oldPicturesDirectory.exists() && oldPicturesDirectory.isDirectory()) {
1052 final File newPicturesDirectory =
1053 new File(
1054 Environment.getExternalStorageDirectory()
1055 + "/Conversations/Media/Conversations Images/");
1056 newPicturesDirectory.getParentFile().mkdirs();
1057 if (oldPicturesDirectory.renameTo(newPicturesDirectory)) {
1058 Log.d(
1059 Config.LOGTAG,
1060 "moved "
1061 + oldPicturesDirectory.getAbsolutePath()
1062 + " to "
1063 + newPicturesDirectory.getAbsolutePath());
1064 }
1065 }
1066 if (oldFilesDirectory.exists() && oldFilesDirectory.isDirectory()) {
1067 newFilesDirectory.mkdirs();
1068 newVideosDirectory.mkdirs();
1069 final File[] files = oldFilesDirectory.listFiles();
1070 if (files == null) {
1071 return;
1072 }
1073 for (File file : files) {
1074 if (file.getName().equals(".nomedia")) {
1075 if (file.delete()) {
1076 Log.d(
1077 Config.LOGTAG,
1078 "deleted nomedia file in "
1079 + oldFilesDirectory.getAbsolutePath());
1080 }
1081 } else if (file.isFile()) {
1082 final String name = file.getName();
1083 boolean isVideo = false;
1084 int start = name.lastIndexOf('.') + 1;
1085 if (start < name.length()) {
1086 String mime =
1087 MimeUtils.guessMimeTypeFromExtension(name.substring(start));
1088 isVideo = mime != null && mime.startsWith("video/");
1089 }
1090 File dst =
1091 new File(
1092 (isVideo ? newVideosDirectory : newFilesDirectory)
1093 .getAbsolutePath()
1094 + "/"
1095 + file.getName());
1096 if (file.renameTo(dst)) {
1097 Log.d(Config.LOGTAG, "moved " + file + " to " + dst);
1098 }
1099 }
1100 }
1101 }
1102 }
1103 if (oldVersion < 35 && newVersion >= 35) {
1104 db.execSQL(CREATE_MESSAGE_CONVERSATION_INDEX);
1105 }
1106 if (oldVersion < 36 && newVersion >= 36) {
1107 List<Account> accounts = getAccounts(db);
1108 for (Account account : accounts) {
1109 account.setOption(Account.OPTION_REQUIRES_ACCESS_MODE_CHANGE, true);
1110 account.setOption(Account.OPTION_LOGGED_IN_SUCCESSFULLY, false);
1111 db.update(
1112 Account.TABLENAME,
1113 account.getContentValues(),
1114 Account.UUID + "=?",
1115 new String[] {account.getUuid()});
1116 }
1117 }
1118
1119 if (oldVersion < 37 && newVersion >= 37) {
1120 db.execSQL(
1121 "ALTER TABLE "
1122 + Message.TABLENAME
1123 + " ADD COLUMN "
1124 + Message.READ_BY_MARKERS
1125 + " TEXT");
1126 }
1127
1128 if (oldVersion < 38 && newVersion >= 38) {
1129 db.execSQL(
1130 "ALTER TABLE "
1131 + Message.TABLENAME
1132 + " ADD COLUMN "
1133 + Message.MARKABLE
1134 + " NUMBER DEFAULT 0");
1135 }
1136
1137 if (oldVersion < 39 && newVersion >= 39) {
1138 db.execSQL(CREATE_RESOLVER_RESULTS_TABLE);
1139 }
1140
1141 if (QuickConversationsService.isQuicksy() && oldVersion < 43 && newVersion >= 43) {
1142 List<Account> accounts = getAccounts(db);
1143 for (Account account : accounts) {
1144 account.setOption(Account.OPTION_MAGIC_CREATE, true);
1145 db.update(
1146 Account.TABLENAME,
1147 account.getContentValues(),
1148 Account.UUID + "=?",
1149 new String[] {account.getUuid()});
1150 }
1151 }
1152
1153 if (oldVersion < 44 && newVersion >= 44) {
1154 db.execSQL(
1155 "ALTER TABLE "
1156 + Message.TABLENAME
1157 + " ADD COLUMN "
1158 + Message.DELETED
1159 + " NUMBER DEFAULT 0");
1160 db.execSQL(CREATE_MESSAGE_DELETED_INDEX);
1161 db.execSQL(CREATE_MESSAGE_RELATIVE_FILE_PATH_INDEX);
1162 db.execSQL(CREATE_MESSAGE_TYPE_INDEX);
1163 }
1164
1165 if (oldVersion < 45 && newVersion >= 45) {
1166 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.BODY_LANGUAGE);
1167 }
1168
1169 if (oldVersion < 46 && newVersion >= 46) {
1170 final long start = SystemClock.elapsedRealtime();
1171 db.rawQuery("PRAGMA secure_delete = FALSE", null).close();
1172 db.execSQL("update " + Message.TABLENAME + " set " + Message.EDITED + "=NULL");
1173 db.rawQuery("PRAGMA secure_delete=ON", null).close();
1174 final long diff = SystemClock.elapsedRealtime() - start;
1175 Log.d(Config.LOGTAG, "deleted old edit information in " + diff + "ms");
1176 }
1177 if (oldVersion < 47 && newVersion >= 47) {
1178 db.execSQL(
1179 "ALTER TABLE "
1180 + Contact.TABLENAME
1181 + " ADD COLUMN "
1182 + Contact.PRESENCE_NAME
1183 + " TEXT");
1184 }
1185 if (oldVersion < 48 && newVersion >= 48) {
1186 db.execSQL(
1187 "ALTER TABLE "
1188 + Contact.TABLENAME
1189 + " ADD COLUMN "
1190 + Contact.RTP_CAPABILITY
1191 + " TEXT");
1192 }
1193 if (oldVersion < 49 && newVersion >= 49) {
1194 db.beginTransaction();
1195 db.execSQL("DROP TRIGGER IF EXISTS after_message_insert;");
1196 db.execSQL("DROP TRIGGER IF EXISTS after_message_update;");
1197 db.execSQL("DROP TRIGGER IF EXISTS after_message_delete;");
1198 db.execSQL("DROP TABLE IF EXISTS messages_index;");
1199 // a hack that should not be necessary, but
1200 // there was at least one occurence when SQLite failed at this
1201 db.execSQL("DROP TABLE IF EXISTS messages_index_docsize;");
1202 db.execSQL("DROP TABLE IF EXISTS messages_index_segdir;");
1203 db.execSQL("DROP TABLE IF EXISTS messages_index_segments;");
1204 db.execSQL("DROP TABLE IF EXISTS messages_index_stat;");
1205 db.execSQL(CREATE_MESSAGE_INDEX_TABLE);
1206 db.execSQL(CREATE_MESSAGE_INSERT_TRIGGER);
1207 db.execSQL(CREATE_MESSAGE_UPDATE_TRIGGER);
1208 db.execSQL(CREATE_MESSAGE_DELETE_TRIGGER);
1209 db.setTransactionSuccessful();
1210 db.endTransaction();
1211 requiresMessageIndexRebuild = true;
1212 }
1213 if (oldVersion < 50 && newVersion >= 50) {
1214 db.execSQL(
1215 "ALTER TABLE "
1216 + Account.TABLENAME
1217 + " ADD COLUMN "
1218 + Account.PINNED_MECHANISM
1219 + " TEXT");
1220 db.execSQL(
1221 "ALTER TABLE "
1222 + Account.TABLENAME
1223 + " ADD COLUMN "
1224 + Account.PINNED_CHANNEL_BINDING
1225 + " TEXT");
1226 }
1227 if (oldVersion < 51 && newVersion >= 51) {
1228 db.execSQL(
1229 "ALTER TABLE "
1230 + Account.TABLENAME
1231 + " ADD COLUMN "
1232 + Account.FAST_MECHANISM
1233 + " TEXT");
1234 db.execSQL(
1235 "ALTER TABLE "
1236 + Account.TABLENAME
1237 + " ADD COLUMN "
1238 + Account.FAST_TOKEN
1239 + " TEXT");
1240 }
1241 if (oldVersion < 52 && newVersion >= 52) {
1242 db.execSQL(
1243 "ALTER TABLE "
1244 + Message.TABLENAME
1245 + " ADD COLUMN "
1246 + Message.OCCUPANT_ID
1247 + " TEXT");
1248 db.execSQL(
1249 "ALTER TABLE "
1250 + Message.TABLENAME
1251 + " ADD COLUMN "
1252 + Message.REACTIONS
1253 + " TEXT");
1254 }
1255 }
1256
1257 private void canonicalizeJids(SQLiteDatabase db) {
1258 // migrate db to new, canonicalized JID domainpart representation
1259
1260 // Conversation table
1261 Cursor cursor = db.rawQuery("select * from " + Conversation.TABLENAME, new String[0]);
1262 while (cursor.moveToNext()) {
1263 String newJid;
1264 try {
1265 newJid =
1266 Jid.of(cursor.getString(cursor.getColumnIndex(Conversation.CONTACTJID)))
1267 .toString();
1268 } catch (IllegalArgumentException ignored) {
1269 Log.e(
1270 Config.LOGTAG,
1271 "Failed to migrate Conversation CONTACTJID "
1272 + cursor.getString(cursor.getColumnIndex(Conversation.CONTACTJID))
1273 + ": "
1274 + ignored
1275 + ". Skipping...");
1276 continue;
1277 }
1278
1279 final String[] updateArgs = {
1280 newJid, cursor.getString(cursor.getColumnIndex(Conversation.UUID)),
1281 };
1282 db.execSQL(
1283 "update "
1284 + Conversation.TABLENAME
1285 + " set "
1286 + Conversation.CONTACTJID
1287 + " = ? "
1288 + " where "
1289 + Conversation.UUID
1290 + " = ?",
1291 updateArgs);
1292 }
1293 cursor.close();
1294
1295 // Contact table
1296 cursor = db.rawQuery("select * from " + Contact.TABLENAME, new String[0]);
1297 while (cursor.moveToNext()) {
1298 String newJid;
1299 try {
1300 newJid = Jid.of(cursor.getString(cursor.getColumnIndex(Contact.JID))).toString();
1301 } catch (final IllegalArgumentException e) {
1302 Log.e(
1303 Config.LOGTAG,
1304 "Failed to migrate Contact JID "
1305 + cursor.getString(cursor.getColumnIndex(Contact.JID))
1306 + ": Skipping...",
1307 e);
1308 continue;
1309 }
1310
1311 final String[] updateArgs = {
1312 newJid,
1313 cursor.getString(cursor.getColumnIndex(Contact.ACCOUNT)),
1314 cursor.getString(cursor.getColumnIndex(Contact.JID)),
1315 };
1316 db.execSQL(
1317 "update "
1318 + Contact.TABLENAME
1319 + " set "
1320 + Contact.JID
1321 + " = ? "
1322 + " where "
1323 + Contact.ACCOUNT
1324 + " = ? "
1325 + " AND "
1326 + Contact.JID
1327 + " = ?",
1328 updateArgs);
1329 }
1330 cursor.close();
1331
1332 // Account table
1333 cursor = db.rawQuery("select * from " + Account.TABLENAME, new String[0]);
1334 while (cursor.moveToNext()) {
1335 String newServer;
1336 try {
1337 newServer =
1338 Jid.of(
1339 cursor.getString(cursor.getColumnIndex(Account.USERNAME)),
1340 cursor.getString(cursor.getColumnIndex(Account.SERVER)),
1341 null)
1342 .getDomain()
1343 .toEscapedString();
1344 } catch (IllegalArgumentException ignored) {
1345 Log.e(
1346 Config.LOGTAG,
1347 "Failed to migrate Account SERVER "
1348 + cursor.getString(cursor.getColumnIndex(Account.SERVER))
1349 + ": "
1350 + ignored
1351 + ". Skipping...");
1352 continue;
1353 }
1354
1355 String[] updateArgs = {
1356 newServer, cursor.getString(cursor.getColumnIndex(Account.UUID)),
1357 };
1358 db.execSQL(
1359 "update "
1360 + Account.TABLENAME
1361 + " set "
1362 + Account.SERVER
1363 + " = ? "
1364 + " where "
1365 + Account.UUID
1366 + " = ?",
1367 updateArgs);
1368 }
1369 cursor.close();
1370 }
1371
1372 public DownloadableFile getFileForCid(Cid cid) {
1373 if (cid == null) return null;
1374
1375 SQLiteDatabase db = this.getReadableDatabase();
1376 Cursor cursor = db.query("cheogram.cids", new String[]{"path"}, "cid=?", new String[]{cid.toString()}, null, null, null);
1377 DownloadableFile f = null;
1378 if (cursor.moveToNext()) {
1379 f = new DownloadableFile(cursor.getString(0));
1380 }
1381 cursor.close();
1382 return f;
1383 }
1384
1385 public String getUrlForCid(Cid cid) {
1386 SQLiteDatabase db = this.getReadableDatabase();
1387 Cursor cursor = db.query("cheogram.cids", new String[]{"url"}, "cid=?", new String[]{cid.toString()}, null, null, null);
1388 String url = null;
1389 if (cursor.moveToNext()) {
1390 url = cursor.getString(0);
1391 }
1392 cursor.close();
1393 return url;
1394 }
1395
1396 public void saveCid(Cid cid, File file) {
1397 saveCid(cid, file, null);
1398 }
1399
1400 public void saveCid(Cid cid, File file, String url) {
1401 SQLiteDatabase db = this.getWritableDatabase();
1402 ContentValues cv = new ContentValues();
1403 cv.put("cid", cid.toString());
1404 if (file != null) cv.put("path", file.getAbsolutePath());
1405 if (url != null) cv.put("url", url);
1406 if (db.update("cheogram.cids", cv, "cid=?", new String[]{cid.toString()}) < 1) {
1407 db.insertWithOnConflict("cheogram.cids", null, cv, SQLiteDatabase.CONFLICT_REPLACE);
1408 }
1409 }
1410
1411 public void blockMedia(Cid cid) {
1412 SQLiteDatabase db = this.getWritableDatabase();
1413 ContentValues cv = new ContentValues();
1414 cv.put("cid", cid.toString());
1415 db.insertWithOnConflict("cheogram.blocked_media", null, cv, SQLiteDatabase.CONFLICT_REPLACE);
1416 }
1417
1418 public boolean isBlockedMedia(Cid cid) {
1419 SQLiteDatabase db = this.getReadableDatabase();
1420 Cursor cursor = db.query("cheogram.blocked_media", new String[]{"count(*)"}, "cid=?", new String[]{cid.toString()}, null, null, null);
1421 boolean is = false;
1422 if (cursor.moveToNext()) {
1423 is = cursor.getInt(0) > 0;
1424 }
1425 cursor.close();
1426 return is;
1427 }
1428
1429 public void clearBlockedMedia() {
1430 SQLiteDatabase db = this.getWritableDatabase();
1431 db.execSQL("DELETE FROM cheogram.blocked_media");
1432 }
1433
1434 public Multimap<String, String> loadMutedMucUsers() {
1435 Multimap<String, String> result = HashMultimap.create();
1436 SQLiteDatabase db = this.getReadableDatabase();
1437 Cursor cursor = db.query("cheogram.muted_participants", new String[]{"muc_jid", "occupant_id"}, null, null, null, null, null);
1438 while (cursor.moveToNext()) {
1439 result.put(cursor.getString(0), cursor.getString(1));
1440 }
1441 cursor.close();
1442 return result;
1443 }
1444
1445 public boolean muteMucUser(MucOptions.User user) {
1446 if (user.getMuc() == null || user.getOccupantId() == null) return false;
1447
1448 SQLiteDatabase db = this.getWritableDatabase();
1449 ContentValues cv = new ContentValues();
1450 cv.put("muc_jid", user.getMuc().toString());
1451 cv.put("occupant_id", user.getOccupantId());
1452 db.insertWithOnConflict("cheogram.muted_participants", null, cv, SQLiteDatabase.CONFLICT_REPLACE);
1453
1454 return true;
1455 }
1456
1457 public boolean unmuteMucUser(MucOptions.User user) {
1458 if (user.getMuc() == null || user.getOccupantId() == null) return false;
1459
1460 SQLiteDatabase db = this.getWritableDatabase();
1461 String where = "muc_jid=? AND occupant_id=?";
1462 String[] whereArgs = {user.getMuc().toString(), user.getOccupantId()};
1463 db.delete("cheogram.muted_participants", where, whereArgs);
1464
1465 return true;
1466 }
1467
1468 public void insertWebxdcUpdate(final WebxdcUpdate update) {
1469 SQLiteDatabase db = this.getWritableDatabase();
1470 db.insertWithOnConflict("cheogram.webxdc_updates", null, update.getContentValues(), SQLiteDatabase.CONFLICT_IGNORE);
1471 }
1472
1473 public WebxdcUpdate findLastWebxdcUpdate(Message message) {
1474 if (message.getThread() == null) {
1475 Log.w(Config.LOGTAG, "WebXDC message with no thread!");
1476 return null;
1477 }
1478
1479 SQLiteDatabase db = this.getReadableDatabase();
1480 String[] selectionArgs = {message.getConversation().getUuid(), message.getThread().getContent()};
1481 Cursor cursor = db.query("cheogram.webxdc_updates", null,
1482 Message.CONVERSATION + "=? AND thread=?",
1483 selectionArgs, null, null, "serial ASC");
1484 WebxdcUpdate update = null;
1485 if (cursor.moveToLast()) {
1486 update = new WebxdcUpdate(cursor, cursor.getLong(cursor.getColumnIndex("serial")));
1487 }
1488 cursor.close();
1489 return update;
1490 }
1491
1492 public List<WebxdcUpdate> findWebxdcUpdates(Message message, long serial) {
1493 SQLiteDatabase db = this.getReadableDatabase();
1494 String[] selectionArgs = {message.getConversation().getUuid(), message.getThread().getContent(), String.valueOf(serial)};
1495 Cursor cursor = db.query("cheogram.webxdc_updates", null,
1496 Message.CONVERSATION + "=? AND thread=? AND serial>?",
1497 selectionArgs, null, null, "serial ASC");
1498 long maxSerial = 0;
1499 if (cursor.moveToLast()) {
1500 maxSerial = cursor.getLong(cursor.getColumnIndex("serial"));
1501 }
1502 cursor.moveToFirst();
1503 cursor.moveToPrevious();
1504
1505 List<WebxdcUpdate> updates = new ArrayList<>();
1506 while (cursor.moveToNext()) {
1507 updates.add(new WebxdcUpdate(cursor, maxSerial));
1508 }
1509 cursor.close();
1510 return updates;
1511 }
1512
1513 public void createConversation(Conversation conversation) {
1514 SQLiteDatabase db = this.getWritableDatabase();
1515 db.insert(Conversation.TABLENAME, null, conversation.getContentValues());
1516 }
1517
1518 public void createMessage(Message message) {
1519 SQLiteDatabase db = this.getWritableDatabase();
1520 db.insert(Message.TABLENAME, null, message.getContentValues());
1521 db.insert("cheogram." + Message.TABLENAME, null, message.getCheogramContentValues());
1522 }
1523
1524 public void createAccount(Account account) {
1525 SQLiteDatabase db = this.getWritableDatabase();
1526 db.insert(Account.TABLENAME, null, account.getContentValues());
1527 }
1528
1529 public void insertDiscoveryResult(ServiceDiscoveryResult result) {
1530 SQLiteDatabase db = this.getWritableDatabase();
1531 db.insert(ServiceDiscoveryResult.TABLENAME, null, result.getContentValues());
1532 }
1533
1534 public ServiceDiscoveryResult findDiscoveryResult(final String hash, final String ver) {
1535 SQLiteDatabase db = this.getReadableDatabase();
1536 String[] selectionArgs = {hash, ver};
1537 Cursor cursor =
1538 db.query(
1539 ServiceDiscoveryResult.TABLENAME,
1540 null,
1541 ServiceDiscoveryResult.HASH + "=? AND " + ServiceDiscoveryResult.VER + "=?",
1542 selectionArgs,
1543 null,
1544 null,
1545 null);
1546 if (cursor.getCount() == 0) {
1547 cursor.close();
1548 return null;
1549 }
1550 cursor.moveToFirst();
1551
1552 ServiceDiscoveryResult result = null;
1553 try {
1554 result = new ServiceDiscoveryResult(cursor);
1555 } catch (JSONException e) {
1556 /* result is still null */
1557 }
1558
1559 cursor.close();
1560 return result;
1561 }
1562
1563 public void saveResolverResult(String domain, Resolver.Result result) {
1564 SQLiteDatabase db = this.getWritableDatabase();
1565 ContentValues contentValues = result.toContentValues();
1566 contentValues.put(Resolver.Result.DOMAIN, domain);
1567 db.insert(RESOLVER_RESULTS_TABLENAME, null, contentValues);
1568 }
1569
1570 public synchronized Resolver.Result findResolverResult(String domain) {
1571 SQLiteDatabase db = this.getReadableDatabase();
1572 String where = Resolver.Result.DOMAIN + "=?";
1573 String[] whereArgs = {domain};
1574 final Cursor cursor =
1575 db.query(RESOLVER_RESULTS_TABLENAME, null, where, whereArgs, null, null, null);
1576 Resolver.Result result = null;
1577 if (cursor != null) {
1578 try {
1579 if (cursor.moveToFirst()) {
1580 result = Resolver.Result.fromCursor(cursor);
1581 }
1582 } catch (Exception e) {
1583 Log.d(
1584 Config.LOGTAG,
1585 "unable to find cached resolver result in database " + e.getMessage());
1586 return null;
1587 } finally {
1588 cursor.close();
1589 }
1590 }
1591 return result;
1592 }
1593
1594 public void insertPresenceTemplate(PresenceTemplate template) {
1595 SQLiteDatabase db = this.getWritableDatabase();
1596 String whereToDelete = PresenceTemplate.MESSAGE + "=?";
1597 String[] whereToDeleteArgs = {template.getStatusMessage()};
1598 db.delete(PresenceTemplate.TABELNAME, whereToDelete, whereToDeleteArgs);
1599 db.delete(
1600 PresenceTemplate.TABELNAME,
1601 PresenceTemplate.UUID
1602 + " not in (select "
1603 + PresenceTemplate.UUID
1604 + " from "
1605 + PresenceTemplate.TABELNAME
1606 + " order by "
1607 + PresenceTemplate.LAST_USED
1608 + " desc limit 9)",
1609 null);
1610 db.insert(PresenceTemplate.TABELNAME, null, template.getContentValues());
1611 }
1612
1613 public List<PresenceTemplate> getPresenceTemplates() {
1614 ArrayList<PresenceTemplate> templates = new ArrayList<>();
1615 SQLiteDatabase db = this.getReadableDatabase();
1616 Cursor cursor =
1617 db.query(
1618 PresenceTemplate.TABELNAME,
1619 null,
1620 null,
1621 null,
1622 null,
1623 null,
1624 PresenceTemplate.LAST_USED + " desc");
1625 while (cursor.moveToNext()) {
1626 templates.add(PresenceTemplate.fromCursor(cursor));
1627 }
1628 cursor.close();
1629 return templates;
1630 }
1631
1632 public CopyOnWriteArrayList<Conversation> getConversations(int status) {
1633 CopyOnWriteArrayList<Conversation> list = new CopyOnWriteArrayList<>();
1634 SQLiteDatabase db = this.getReadableDatabase();
1635 String[] selectionArgs = {Integer.toString(status)};
1636 Cursor cursor =
1637 db.rawQuery(
1638 "select * from "
1639 + Conversation.TABLENAME
1640 + " where "
1641 + Conversation.STATUS
1642 + " = ? and "
1643 + Conversation.CONTACTJID
1644 + " is not null order by "
1645 + Conversation.CREATED
1646 + " desc",
1647 selectionArgs);
1648 while (cursor.moveToNext()) {
1649 final Conversation conversation = Conversation.fromCursor(cursor);
1650 if (conversation.getJid() instanceof InvalidJid) {
1651 continue;
1652 }
1653 list.add(conversation);
1654 }
1655 cursor.close();
1656 return list;
1657 }
1658
1659 public Message getMessage(Conversation conversation, String uuid) {
1660 ArrayList<Message> list = new ArrayList<>();
1661 SQLiteDatabase db = this.getReadableDatabase();
1662 Cursor cursor;
1663 cursor = db.rawQuery(
1664 "SELECT * FROM " + Message.TABLENAME + " " +
1665 "LEFT JOIN cheogram." + Message.TABLENAME +
1666 " USING (" + Message.UUID + ")" +
1667 "WHERE " + Message.UUID + "=?",
1668 new String[]{uuid}
1669 );
1670 while (cursor.moveToNext()) {
1671 try {
1672 return Message.fromCursor(cursor, conversation);
1673 } catch (Exception e) {
1674 Log.e(Config.LOGTAG, "unable to restore message");
1675 }
1676 }
1677 cursor.close();
1678 return null;
1679 }
1680
1681 public ArrayList<Message> getMessages(Conversation conversations, int limit) {
1682 return getMessages(conversations, limit, -1);
1683 }
1684
1685 public Map<String, Message> getMessageFuzzyIds(Conversation conversation, Collection<String> ids) {
1686 final var result = new Hashtable<String, Message>();
1687 if (ids.size() < 1) return result;
1688 final ArrayList<String> params = new ArrayList<>();
1689 final ArrayList<String> template = new ArrayList<>();
1690 for (final var id : ids) {
1691 template.add("?");
1692 }
1693 params.addAll(ids);
1694 params.addAll(ids);
1695 params.addAll(ids);
1696 ArrayList<Message> list = new ArrayList<>();
1697 SQLiteDatabase db = this.getReadableDatabase();
1698 Cursor cursor;
1699 cursor = db.rawQuery(
1700 "SELECT * FROM " + Message.TABLENAME + " " +
1701 "LEFT JOIN cheogram." + Message.TABLENAME +
1702 " USING (" + Message.UUID + ")" +
1703 "WHERE " + Message.UUID + " IN (" + TextUtils.join(",", template) + ") OR " + Message.SERVER_MSG_ID + " IN (" + TextUtils.join(",", template) + ") OR " + Message.REMOTE_MSG_ID + " IN (" + TextUtils.join(",", template) + ")",
1704 params.toArray(new String[0])
1705 );
1706
1707 while (cursor.moveToNext()) {
1708 try {
1709 final var m = Message.fromCursor(cursor, conversation);
1710 if (ids.contains(m.getUuid())) result.put(m.getUuid(), m);
1711 if (ids.contains(m.getServerMsgId())) result.put(m.getServerMsgId(), m);
1712 if (ids.contains(m.getRemoteMsgId())) result.put(m.getRemoteMsgId(), m);
1713 } catch (Exception e) {
1714 Log.e(Config.LOGTAG, "unable to restore message");
1715 }
1716 }
1717 cursor.close();
1718 return result;
1719 }
1720
1721 public ArrayList<Message> getMessages(Conversation conversation, int limit, long timestamp) {
1722 ArrayList<Message> list = new ArrayList<>();
1723 SQLiteDatabase db = this.getReadableDatabase();
1724 Cursor cursor;
1725 if (timestamp == -1) {
1726 String[] selectionArgs = {conversation.getUuid()};
1727 cursor = db.rawQuery(
1728 "SELECT * FROM " + Message.TABLENAME + " " +
1729 "LEFT JOIN cheogram." + Message.TABLENAME +
1730 " USING (" + Message.UUID + ")" +
1731 " WHERE " + Message.UUID + " IN (" +
1732 "SELECT " + Message.UUID + " FROM " + Message.TABLENAME +
1733 " WHERE " + Message.CONVERSATION + "=? " +
1734 "ORDER BY " + Message.TIME_SENT + " DESC " +
1735 "LIMIT " + String.valueOf(limit) + ") " +
1736 "ORDER BY " + Message.TIME_SENT + " DESC ",
1737 selectionArgs
1738 );
1739 } else {
1740 String[] selectionArgs = {conversation.getUuid(),
1741 Long.toString(timestamp)};
1742 cursor = db.rawQuery(
1743 "SELECT * FROM " + Message.TABLENAME + " " +
1744 "LEFT JOIN cheogram." + Message.TABLENAME +
1745 " USING (" + Message.UUID + ")" +
1746 " WHERE " + Message.UUID + " IN (" +
1747 "SELECT " + Message.UUID + " FROM " + Message.TABLENAME +
1748 " WHERE " + Message.CONVERSATION + "=? AND " +
1749 Message.TIME_SENT + "<? " +
1750 "ORDER BY " + Message.TIME_SENT + " DESC " +
1751 "LIMIT " + String.valueOf(limit) + ") " +
1752 "ORDER BY " + Message.TIME_SENT + " DESC ",
1753 selectionArgs
1754 );
1755 }
1756 CursorUtils.upgradeCursorWindowSize(cursor);
1757 final Multimap<String, Message> waitingForReplies = HashMultimap.create();
1758 final var replyIds = new HashSet<String>();
1759 while (cursor.moveToNext()) {
1760 try {
1761 final var m = Message.fromCursor(cursor, conversation);
1762 final var reply = m.getReply();
1763 if (reply != null && reply.getAttribute("id") != null) { // Guard against busted replies
1764 replyIds.add(reply.getAttribute("id"));
1765 waitingForReplies.put(reply.getAttribute("id"), m);
1766 }
1767 list.add(0, m);
1768 } catch (Exception e) {
1769 Log.e(Config.LOGTAG, "unable to restore message", e);
1770 }
1771 }
1772 for (final var parent : getMessageFuzzyIds(conversation, replyIds).entrySet()) {
1773 for (final var m : waitingForReplies.get(parent.getKey())) {
1774 m.setInReplyTo(parent.getValue());
1775 }
1776 }
1777 cursor.close();
1778 return list;
1779 }
1780
1781 public Cursor getMessageSearchCursor(final List<String> term, final String uuid) {
1782 final SQLiteDatabase db = this.getReadableDatabase();
1783 final StringBuilder SQL = new StringBuilder();
1784 final String[] selectionArgs;
1785 SQL.append(
1786 "SELECT "
1787 + Message.TABLENAME
1788 + ".*,"
1789 + Conversation.TABLENAME
1790 + "."
1791 + Conversation.CONTACTJID
1792 + ","
1793 + Conversation.TABLENAME
1794 + "."
1795 + Conversation.ACCOUNT
1796 + ","
1797 + Conversation.TABLENAME
1798 + "."
1799 + Conversation.MODE
1800 + " FROM "
1801 + Message.TABLENAME
1802 + " JOIN "
1803 + Conversation.TABLENAME
1804 + " ON "
1805 + Message.TABLENAME
1806 + "."
1807 + Message.CONVERSATION
1808 + "="
1809 + Conversation.TABLENAME
1810 + "."
1811 + Conversation.UUID
1812 + " JOIN messages_index ON messages_index.rowid=messages.rowid WHERE "
1813 + Message.ENCRYPTION
1814 + " NOT IN("
1815 + Message.ENCRYPTION_AXOLOTL_NOT_FOR_THIS_DEVICE
1816 + ","
1817 + Message.ENCRYPTION_PGP
1818 + ","
1819 + Message.ENCRYPTION_DECRYPTION_FAILED
1820 + ","
1821 + Message.ENCRYPTION_AXOLOTL_FAILED
1822 + ") AND "
1823 + Message.TYPE
1824 + " IN("
1825 + Message.TYPE_TEXT
1826 + ","
1827 + Message.TYPE_PRIVATE
1828 + ") AND messages_index.body MATCH ?");
1829 if (uuid == null) {
1830 selectionArgs = new String[] {FtsUtils.toMatchString(term)};
1831 } else {
1832 selectionArgs = new String[] {FtsUtils.toMatchString(term), uuid};
1833 SQL.append(" AND " + Conversation.TABLENAME + '.' + Conversation.UUID + "=?");
1834 }
1835 SQL.append(" ORDER BY " + Message.TIME_SENT + " DESC limit " + Config.MAX_SEARCH_RESULTS);
1836 Log.d(Config.LOGTAG, "search term: " + FtsUtils.toMatchString(term));
1837 return db.rawQuery(SQL.toString(), selectionArgs);
1838 }
1839
1840 public List<String> markFileAsDeleted(final File file, final boolean internal) {
1841 SQLiteDatabase db = this.getReadableDatabase();
1842 String selection;
1843 String[] selectionArgs;
1844 if (internal) {
1845 final String name = file.getName();
1846 if (name.endsWith(".pgp")) {
1847 selection =
1848 "("
1849 + Message.RELATIVE_FILE_PATH
1850 + " IN(?,?) OR ("
1851 + Message.RELATIVE_FILE_PATH
1852 + "=? and encryption in(1,4))) and type in (1,2,5)";
1853 selectionArgs =
1854 new String[] {
1855 file.getAbsolutePath(), name, name.substring(0, name.length() - 4)
1856 };
1857 } else {
1858 selection = Message.RELATIVE_FILE_PATH + " IN(?,?) and type in (1,2,5)";
1859 selectionArgs = new String[] {file.getAbsolutePath(), name};
1860 }
1861 } else {
1862 selection = Message.RELATIVE_FILE_PATH + "=? and type in (1,2,5)";
1863 selectionArgs = new String[] {file.getAbsolutePath()};
1864 }
1865 final List<String> uuids = new ArrayList<>();
1866 Cursor cursor =
1867 db.query(
1868 Message.TABLENAME,
1869 new String[] {Message.UUID},
1870 selection,
1871 selectionArgs,
1872 null,
1873 null,
1874 null);
1875 while (cursor != null && cursor.moveToNext()) {
1876 uuids.add(cursor.getString(0));
1877 }
1878 if (cursor != null) {
1879 cursor.close();
1880 }
1881 markFileAsDeleted(uuids);
1882 return uuids;
1883 }
1884
1885 public void markFileAsDeleted(List<String> uuids) {
1886 SQLiteDatabase db = this.getReadableDatabase();
1887 final ContentValues contentValues = new ContentValues();
1888 final String where = Message.UUID + "=?";
1889 contentValues.put(Message.DELETED, 1);
1890 db.beginTransaction();
1891 for (String uuid : uuids) {
1892 db.update(Message.TABLENAME, contentValues, where, new String[] {uuid});
1893 }
1894 db.setTransactionSuccessful();
1895 db.endTransaction();
1896 }
1897
1898 public void markFilesAsChanged(List<FilePathInfo> files) {
1899 SQLiteDatabase db = this.getReadableDatabase();
1900 final String where = Message.UUID + "=?";
1901 db.beginTransaction();
1902 for (FilePathInfo info : files) {
1903 final ContentValues contentValues = new ContentValues();
1904 contentValues.put(Message.DELETED, info.deleted ? 1 : 0);
1905 db.update(Message.TABLENAME, contentValues, where, new String[] {info.uuid.toString()});
1906 }
1907 db.setTransactionSuccessful();
1908 db.endTransaction();
1909 }
1910
1911 public List<FilePathInfo> getFilePathInfo() {
1912 final SQLiteDatabase db = this.getReadableDatabase();
1913 final Cursor cursor =
1914 db.query(
1915 Message.TABLENAME,
1916 new String[] {Message.UUID, Message.RELATIVE_FILE_PATH, Message.DELETED},
1917 "type in (1,2,5) and " + Message.RELATIVE_FILE_PATH + " is not null",
1918 null,
1919 null,
1920 null,
1921 null);
1922 final List<FilePathInfo> list = new ArrayList<>();
1923 while (cursor != null && cursor.moveToNext()) {
1924 list.add(
1925 new FilePathInfo(
1926 cursor.getString(0), cursor.getString(1), cursor.getInt(2) > 0));
1927 }
1928 if (cursor != null) {
1929 cursor.close();
1930 }
1931 return list;
1932 }
1933
1934 public List<FilePath> getRelativeFilePaths(String account, Jid jid, int limit) {
1935 SQLiteDatabase db = this.getReadableDatabase();
1936 final String SQL =
1937 "select uuid,relativeFilePath from messages where type in (1,2,5) and deleted=0 and"
1938 + " "
1939 + Message.RELATIVE_FILE_PATH
1940 + " is not null and conversationUuid=(select uuid from conversations where"
1941 + " accountUuid=? and (contactJid=? or contactJid like ?)) order by"
1942 + " timeSent desc";
1943 final String[] args = {account, jid.toString(), jid.toString() + "/%"};
1944 Cursor cursor = db.rawQuery(SQL + (limit > 0 ? " limit " + limit : ""), args);
1945 List<FilePath> filesPaths = new ArrayList<>();
1946 while (cursor.moveToNext()) {
1947 filesPaths.add(new FilePath(cursor.getString(0), cursor.getString(1)));
1948 }
1949 cursor.close();
1950 return filesPaths;
1951 }
1952
1953 public Message getMessageWithServerMsgId(
1954 final Conversation conversation, final String messageId) {
1955 final var db = this.getReadableDatabase();
1956 final String sql =
1957 "select * from messages LEFT JOIN cheogram.messages USING (uuid) where conversationUuid=? and serverMsgId=? LIMIT 1";
1958 final String[] args = {conversation.getUuid(), messageId};
1959 final Cursor cursor = db.rawQuery(sql, args);
1960 if (cursor == null) {
1961 return null;
1962 }
1963 Message message = null;
1964 try {
1965 if (cursor.moveToFirst()) {
1966 message = Message.fromCursor(cursor, conversation);
1967 }
1968 } catch (final IOException e) { }
1969 cursor.close();
1970 return message;
1971 }
1972
1973 public Message getMessageWithUuidOrRemoteId(
1974 final Conversation conversation, final String messageId) {
1975 final var db = this.getReadableDatabase();
1976 final String sql =
1977 "select * from messages LEFT JOIN cheogram.messages USING (uuid) where conversationUuid=? and (uuid=? OR remoteMsgId=?) LIMIT 1";
1978 final String[] args = {conversation.getUuid(), messageId, messageId};
1979 final Cursor cursor = db.rawQuery(sql, args);
1980 if (cursor == null) {
1981 return null;
1982 }
1983 Message message = null;
1984 try {
1985 if (cursor.moveToFirst()) {
1986 message = Message.fromCursor(cursor, conversation);
1987 }
1988 } catch (final IOException e) { }
1989 cursor.close();
1990 return message;
1991 }
1992
1993 public static class FilePath {
1994 public final UUID uuid;
1995 public final String path;
1996
1997 private FilePath(String uuid, String path) {
1998 this.uuid = UUID.fromString(uuid);
1999 this.path = path;
2000 }
2001 }
2002
2003 public static class FilePathInfo extends FilePath {
2004 public boolean deleted;
2005
2006 private FilePathInfo(String uuid, String path, boolean deleted) {
2007 super(uuid, path);
2008 this.deleted = deleted;
2009 }
2010
2011 public boolean setDeleted(boolean deleted) {
2012 final boolean changed = deleted != this.deleted;
2013 this.deleted = deleted;
2014 return changed;
2015 }
2016 }
2017
2018 public Conversation findConversation(final String uuid) {
2019 final var db = this.getReadableDatabase();
2020 final String[] selectionArgs = {uuid};
2021 try (final Cursor cursor =
2022 db.query(
2023 Conversation.TABLENAME,
2024 null,
2025 Conversation.UUID + "=?",
2026 selectionArgs,
2027 null,
2028 null,
2029 null)) {
2030 if (cursor.getCount() == 0) {
2031 return null;
2032 }
2033 cursor.moveToFirst();
2034 final Conversation conversation = Conversation.fromCursor(cursor);
2035 if (conversation.getJid() instanceof InvalidJid) {
2036 return null;
2037 }
2038 return conversation;
2039 }
2040 }
2041
2042 public Conversation findConversation(final Account account, final Jid contactJid) {
2043 final SQLiteDatabase db = this.getReadableDatabase();
2044 final String[] selectionArgs = {
2045 account.getUuid(),
2046 contactJid.asBareJid().toString() + "/%",
2047 contactJid.asBareJid().toString()
2048 };
2049 try (final Cursor cursor =
2050 db.query(
2051 Conversation.TABLENAME,
2052 null,
2053 Conversation.ACCOUNT
2054 + "=? AND ("
2055 + Conversation.CONTACTJID
2056 + " like ? OR "
2057 + Conversation.CONTACTJID
2058 + "=?)",
2059 selectionArgs,
2060 null,
2061 null,
2062 null)) {
2063 if (cursor.getCount() == 0) {
2064 return null;
2065 }
2066 cursor.moveToFirst();
2067 final Conversation conversation = Conversation.fromCursor(cursor);
2068 if (conversation.getJid() instanceof InvalidJid) {
2069 return null;
2070 }
2071 conversation.setAccount(account);
2072 return conversation;
2073 }
2074 }
2075
2076 public void updateConversation(final Conversation conversation) {
2077 final SQLiteDatabase db = this.getWritableDatabase();
2078 final String[] args = {conversation.getUuid()};
2079 db.update(
2080 Conversation.TABLENAME,
2081 conversation.getContentValues(),
2082 Conversation.UUID + "=?",
2083 args);
2084 }
2085
2086 public List<Account> getAccounts() {
2087 SQLiteDatabase db = this.getReadableDatabase();
2088 return getAccounts(db);
2089 }
2090
2091 public List<Jid> getAccountJids(final boolean enabledOnly) {
2092 final SQLiteDatabase db = this.getReadableDatabase();
2093 final List<Jid> jids = new ArrayList<>();
2094 final String[] columns = new String[] {Account.USERNAME, Account.SERVER};
2095 final String where = enabledOnly ? "not options & (1 <<1)" : null;
2096 try (final Cursor cursor =
2097 db.query(Account.TABLENAME, columns, where, null, null, null, null)) {
2098 while (cursor != null && cursor.moveToNext()) {
2099 jids.add(Jid.of(cursor.getString(0), cursor.getString(1), null));
2100 }
2101 } catch (final Exception e) {
2102 return jids;
2103 }
2104 return jids;
2105 }
2106
2107 private List<Account> getAccounts(SQLiteDatabase db) {
2108 final List<Account> list = new ArrayList<>();
2109 try (final Cursor cursor =
2110 db.query(Account.TABLENAME, null, null, null, null, null, null)) {
2111 while (cursor != null && cursor.moveToNext()) {
2112 list.add(Account.fromCursor(cursor));
2113 }
2114 }
2115 return list;
2116 }
2117
2118 public boolean updateAccount(Account account) {
2119 final var db = this.getWritableDatabase();
2120 final String[] args = {account.getUuid()};
2121 final int rows =
2122 db.update(Account.TABLENAME, account.getContentValues(), Account.UUID + "=?", args);
2123 return rows == 1;
2124 }
2125
2126 public boolean deleteAccount(final Account account) {
2127 final var db = this.getWritableDatabase();
2128 final String[] args = {account.getUuid()};
2129 final int rows = db.delete(Account.TABLENAME, Account.UUID + "=?", args);
2130 return rows == 1;
2131 }
2132
2133 public boolean updateMessage(final Message message, final boolean includeBody) {
2134 final var db = this.getWritableDatabase();
2135 final String[] args = {message.getUuid()};
2136 final var contentValues = message.getContentValues();
2137 contentValues.remove(Message.UUID);
2138 if (!includeBody) {
2139 contentValues.remove(Message.BODY);
2140 }
2141 return db.update(Message.TABLENAME, contentValues, Message.UUID + "=?", args) == 1 &&
2142 db.update("cheogram." + Message.TABLENAME, message.getCheogramContentValues(), Message.UUID + "=?", args) == 1;
2143 }
2144
2145 public boolean updateMessage(Message message, String uuid) {
2146 SQLiteDatabase db = this.getWritableDatabase();
2147 String[] args = {uuid};
2148 return db.update(Message.TABLENAME, message.getContentValues(), Message.UUID + "=?", args) == 1 &&
2149 db.update("cheogram." + Message.TABLENAME, message.getCheogramContentValues(), Message.UUID + "=?", args) == 1;
2150 }
2151
2152
2153 public boolean deleteMessage(String uuid) {
2154 SQLiteDatabase db = this.getWritableDatabase();
2155 String[] args = {uuid};
2156 return db.delete(Message.TABLENAME, Message.UUID + "=?", args) == 1 &&
2157 db.delete("cheogram." + Message.TABLENAME, Message.UUID + "=?", args) == 1;
2158 }
2159
2160 public void readRoster(Roster roster) {
2161 final SQLiteDatabase db = this.getReadableDatabase();
2162 final String[] args = {roster.getAccount().getUuid()};
2163 try (final Cursor cursor =
2164 db.query(Contact.TABLENAME, null, Contact.ACCOUNT + "=?", args, null, null, null)) {
2165 while (cursor.moveToNext()) {
2166 roster.initContact(Contact.fromCursor(cursor));
2167 }
2168 }
2169 }
2170
2171 public void writeRoster(final Roster roster) {
2172 long start = SystemClock.elapsedRealtime();
2173 final Account account = roster.getAccount();
2174 final SQLiteDatabase db = this.getWritableDatabase();
2175 db.beginTransaction();
2176 for (Contact contact : roster.getContacts()) {
2177 if (contact.getOption(Contact.Options.IN_ROSTER)
2178 || contact.hasAvatarOrPresenceName()
2179 || contact.getOption(Contact.Options.SYNCED_VIA_OTHER)) {
2180 db.insert(Contact.TABLENAME, null, contact.getContentValues());
2181 } else {
2182 String where = Contact.ACCOUNT + "=? AND " + Contact.JID + "=?";
2183 String[] whereArgs = {account.getUuid(), contact.getJid().toString()};
2184 db.delete(Contact.TABLENAME, where, whereArgs);
2185 }
2186 }
2187 db.setTransactionSuccessful();
2188 db.endTransaction();
2189 account.setRosterVersion(roster.getVersion());
2190 updateAccount(account);
2191 long duration = SystemClock.elapsedRealtime() - start;
2192 Log.d(
2193 Config.LOGTAG,
2194 account.getJid().asBareJid() + ": persisted roster in " + duration + "ms");
2195 }
2196
2197 public void deleteMessagesInConversation(Conversation conversation) {
2198 long start = SystemClock.elapsedRealtime();
2199 final SQLiteDatabase db = this.getWritableDatabase();
2200 db.beginTransaction();
2201 final String[] args = {conversation.getUuid()};
2202 int num = db.delete(Message.TABLENAME, Message.CONVERSATION + "=?", args);
2203 db.delete("cheogram.webxdc_updates", Message.CONVERSATION + "=?", args);
2204 db.setTransactionSuccessful();
2205 db.endTransaction();
2206 Log.d(
2207 Config.LOGTAG,
2208 "deleted "
2209 + num
2210 + " messages for "
2211 + conversation.getJid().asBareJid()
2212 + " in "
2213 + (SystemClock.elapsedRealtime() - start)
2214 + "ms");
2215 }
2216
2217 public void expireOldMessages(long timestamp) {
2218 final String[] args = {String.valueOf(timestamp)};
2219 SQLiteDatabase db = this.getReadableDatabase();
2220 db.beginTransaction();
2221 db.delete(Message.TABLENAME, "timeSent<?", args);
2222 db.delete("cheogram.messages", "timeReceived<?", args);
2223 db.setTransactionSuccessful();
2224 db.endTransaction();
2225 }
2226
2227 public MamReference getLastMessageReceived(Account account) {
2228 Cursor cursor = null;
2229 try {
2230 SQLiteDatabase db = this.getReadableDatabase();
2231 String sql =
2232 "select messages.timeSent,messages.serverMsgId from accounts join conversations"
2233 + " on accounts.uuid=conversations.accountUuid join messages on"
2234 + " conversations.uuid=messages.conversationUuid where accounts.uuid=? and"
2235 + " (messages.status=0 or messages.carbon=1 or messages.serverMsgId not"
2236 + " null) and (conversations.mode=0 or (messages.serverMsgId not null and"
2237 + " messages.type=4)) order by messages.timesent desc limit 1";
2238 String[] args = {account.getUuid()};
2239 cursor = db.rawQuery(sql, args);
2240 if (cursor.getCount() == 0) {
2241 return null;
2242 } else {
2243 cursor.moveToFirst();
2244 return new MamReference(cursor.getLong(0), cursor.getString(1));
2245 }
2246 } catch (Exception e) {
2247 return null;
2248 } finally {
2249 if (cursor != null) {
2250 cursor.close();
2251 }
2252 }
2253 }
2254
2255 public long getLastTimeFingerprintUsed(Account account, String fingerprint) {
2256 String SQL =
2257 "select messages.timeSent from accounts join conversations on"
2258 + " accounts.uuid=conversations.accountUuid join messages on"
2259 + " conversations.uuid=messages.conversationUuid where accounts.uuid=? and"
2260 + " messages.axolotl_fingerprint=? order by messages.timesent desc limit 1";
2261 String[] args = {account.getUuid(), fingerprint};
2262 Cursor cursor = getReadableDatabase().rawQuery(SQL, args);
2263 long time;
2264 if (cursor.moveToFirst()) {
2265 time = cursor.getLong(0);
2266 } else {
2267 time = 0;
2268 }
2269 cursor.close();
2270 return time;
2271 }
2272
2273 public MamReference getLastClearDate(Account account) {
2274 SQLiteDatabase db = this.getReadableDatabase();
2275 String[] columns = {Conversation.ATTRIBUTES};
2276 String selection = Conversation.ACCOUNT + "=?";
2277 String[] args = {account.getUuid()};
2278 Cursor cursor =
2279 db.query(Conversation.TABLENAME, columns, selection, args, null, null, null);
2280 MamReference maxClearDate = new MamReference(0);
2281 while (cursor.moveToNext()) {
2282 try {
2283 final JSONObject o = new JSONObject(cursor.getString(0));
2284 maxClearDate =
2285 MamReference.max(
2286 maxClearDate,
2287 MamReference.fromAttribute(
2288 o.getString(Conversation.ATTRIBUTE_LAST_CLEAR_HISTORY)));
2289 } catch (Exception e) {
2290 // ignored
2291 }
2292 }
2293 cursor.close();
2294 return maxClearDate;
2295 }
2296
2297 private Cursor getCursorForSession(Account account, SignalProtocolAddress contact) {
2298 final SQLiteDatabase db = this.getReadableDatabase();
2299 String[] selectionArgs = {
2300 account.getUuid(), contact.getName(), Integer.toString(contact.getDeviceId())
2301 };
2302 return db.query(
2303 SQLiteAxolotlStore.SESSION_TABLENAME,
2304 null,
2305 SQLiteAxolotlStore.ACCOUNT
2306 + " = ? AND "
2307 + SQLiteAxolotlStore.NAME
2308 + " = ? AND "
2309 + SQLiteAxolotlStore.DEVICE_ID
2310 + " = ? ",
2311 selectionArgs,
2312 null,
2313 null,
2314 null);
2315 }
2316
2317 public SessionRecord loadSession(Account account, SignalProtocolAddress contact) {
2318 SessionRecord session = null;
2319 Cursor cursor = getCursorForSession(account, contact);
2320 if (cursor.getCount() != 0) {
2321 cursor.moveToFirst();
2322 try {
2323 session =
2324 new SessionRecord(
2325 Base64.decode(
2326 cursor.getString(
2327 cursor.getColumnIndex(SQLiteAxolotlStore.KEY)),
2328 Base64.DEFAULT));
2329 } catch (IOException e) {
2330 cursor.close();
2331 throw new AssertionError(e);
2332 }
2333 }
2334 cursor.close();
2335 return session;
2336 }
2337
2338 public List<Integer> getSubDeviceSessions(Account account, SignalProtocolAddress contact) {
2339 final SQLiteDatabase db = this.getReadableDatabase();
2340 return getSubDeviceSessions(db, account, contact);
2341 }
2342
2343 private List<Integer> getSubDeviceSessions(
2344 SQLiteDatabase db, Account account, SignalProtocolAddress contact) {
2345 List<Integer> devices = new ArrayList<>();
2346 String[] columns = {SQLiteAxolotlStore.DEVICE_ID};
2347 String[] selectionArgs = {account.getUuid(), contact.getName()};
2348 Cursor cursor =
2349 db.query(
2350 SQLiteAxolotlStore.SESSION_TABLENAME,
2351 columns,
2352 SQLiteAxolotlStore.ACCOUNT + " = ? AND " + SQLiteAxolotlStore.NAME + " = ?",
2353 selectionArgs,
2354 null,
2355 null,
2356 null);
2357
2358 while (cursor.moveToNext()) {
2359 devices.add(cursor.getInt(cursor.getColumnIndex(SQLiteAxolotlStore.DEVICE_ID)));
2360 }
2361
2362 cursor.close();
2363 return devices;
2364 }
2365
2366 public List<String> getKnownSignalAddresses(Account account) {
2367 List<String> addresses = new ArrayList<>();
2368 String[] colums = {"DISTINCT " + SQLiteAxolotlStore.NAME};
2369 String[] selectionArgs = {account.getUuid()};
2370 Cursor cursor =
2371 getReadableDatabase()
2372 .query(
2373 SQLiteAxolotlStore.SESSION_TABLENAME,
2374 colums,
2375 SQLiteAxolotlStore.ACCOUNT + " = ?",
2376 selectionArgs,
2377 null,
2378 null,
2379 null);
2380 while (cursor.moveToNext()) {
2381 addresses.add(cursor.getString(0));
2382 }
2383 cursor.close();
2384 return addresses;
2385 }
2386
2387 public boolean containsSession(Account account, SignalProtocolAddress contact) {
2388 Cursor cursor = getCursorForSession(account, contact);
2389 int count = cursor.getCount();
2390 cursor.close();
2391 return count != 0;
2392 }
2393
2394 public void storeSession(
2395 Account account, SignalProtocolAddress contact, SessionRecord session) {
2396 SQLiteDatabase db = this.getWritableDatabase();
2397 ContentValues values = new ContentValues();
2398 values.put(SQLiteAxolotlStore.NAME, contact.getName());
2399 values.put(SQLiteAxolotlStore.DEVICE_ID, contact.getDeviceId());
2400 values.put(
2401 SQLiteAxolotlStore.KEY, Base64.encodeToString(session.serialize(), Base64.DEFAULT));
2402 values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
2403 db.insert(SQLiteAxolotlStore.SESSION_TABLENAME, null, values);
2404 }
2405
2406 public void deleteSession(Account account, SignalProtocolAddress contact) {
2407 SQLiteDatabase db = this.getWritableDatabase();
2408 deleteSession(db, account, contact);
2409 }
2410
2411 private void deleteSession(SQLiteDatabase db, Account account, SignalProtocolAddress contact) {
2412 String[] args = {
2413 account.getUuid(), contact.getName(), Integer.toString(contact.getDeviceId())
2414 };
2415 db.delete(
2416 SQLiteAxolotlStore.SESSION_TABLENAME,
2417 SQLiteAxolotlStore.ACCOUNT
2418 + " = ? AND "
2419 + SQLiteAxolotlStore.NAME
2420 + " = ? AND "
2421 + SQLiteAxolotlStore.DEVICE_ID
2422 + " = ? ",
2423 args);
2424 }
2425
2426 public void deleteAllSessions(Account account, SignalProtocolAddress contact) {
2427 SQLiteDatabase db = this.getWritableDatabase();
2428 String[] args = {account.getUuid(), contact.getName()};
2429 db.delete(
2430 SQLiteAxolotlStore.SESSION_TABLENAME,
2431 SQLiteAxolotlStore.ACCOUNT + "=? AND " + SQLiteAxolotlStore.NAME + " = ?",
2432 args);
2433 }
2434
2435 private Cursor getCursorForPreKey(Account account, int preKeyId) {
2436 SQLiteDatabase db = this.getReadableDatabase();
2437 String[] columns = {SQLiteAxolotlStore.KEY};
2438 String[] selectionArgs = {account.getUuid(), Integer.toString(preKeyId)};
2439 Cursor cursor =
2440 db.query(
2441 SQLiteAxolotlStore.PREKEY_TABLENAME,
2442 columns,
2443 SQLiteAxolotlStore.ACCOUNT + "=? AND " + SQLiteAxolotlStore.ID + "=?",
2444 selectionArgs,
2445 null,
2446 null,
2447 null);
2448
2449 return cursor;
2450 }
2451
2452 public PreKeyRecord loadPreKey(Account account, int preKeyId) {
2453 PreKeyRecord record = null;
2454 Cursor cursor = getCursorForPreKey(account, preKeyId);
2455 if (cursor.getCount() != 0) {
2456 cursor.moveToFirst();
2457 try {
2458 record =
2459 new PreKeyRecord(
2460 Base64.decode(
2461 cursor.getString(
2462 cursor.getColumnIndex(SQLiteAxolotlStore.KEY)),
2463 Base64.DEFAULT));
2464 } catch (IOException e) {
2465 throw new AssertionError(e);
2466 }
2467 }
2468 cursor.close();
2469 return record;
2470 }
2471
2472 public boolean containsPreKey(Account account, int preKeyId) {
2473 Cursor cursor = getCursorForPreKey(account, preKeyId);
2474 int count = cursor.getCount();
2475 cursor.close();
2476 return count != 0;
2477 }
2478
2479 public void storePreKey(Account account, PreKeyRecord record) {
2480 SQLiteDatabase db = this.getWritableDatabase();
2481 ContentValues values = new ContentValues();
2482 values.put(SQLiteAxolotlStore.ID, record.getId());
2483 values.put(
2484 SQLiteAxolotlStore.KEY, Base64.encodeToString(record.serialize(), Base64.DEFAULT));
2485 values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
2486 db.insert(SQLiteAxolotlStore.PREKEY_TABLENAME, null, values);
2487 }
2488
2489 public int deletePreKey(Account account, int preKeyId) {
2490 SQLiteDatabase db = this.getWritableDatabase();
2491 String[] args = {account.getUuid(), Integer.toString(preKeyId)};
2492 return db.delete(
2493 SQLiteAxolotlStore.PREKEY_TABLENAME,
2494 SQLiteAxolotlStore.ACCOUNT + "=? AND " + SQLiteAxolotlStore.ID + "=?",
2495 args);
2496 }
2497
2498 private Cursor getCursorForSignedPreKey(Account account, int signedPreKeyId) {
2499 SQLiteDatabase db = this.getReadableDatabase();
2500 String[] columns = {SQLiteAxolotlStore.KEY};
2501 String[] selectionArgs = {account.getUuid(), Integer.toString(signedPreKeyId)};
2502 Cursor cursor =
2503 db.query(
2504 SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
2505 columns,
2506 SQLiteAxolotlStore.ACCOUNT + "=? AND " + SQLiteAxolotlStore.ID + "=?",
2507 selectionArgs,
2508 null,
2509 null,
2510 null);
2511
2512 return cursor;
2513 }
2514
2515 public SignedPreKeyRecord loadSignedPreKey(Account account, int signedPreKeyId) {
2516 SignedPreKeyRecord record = null;
2517 Cursor cursor = getCursorForSignedPreKey(account, signedPreKeyId);
2518 if (cursor.getCount() != 0) {
2519 cursor.moveToFirst();
2520 try {
2521 record =
2522 new SignedPreKeyRecord(
2523 Base64.decode(
2524 cursor.getString(
2525 cursor.getColumnIndex(SQLiteAxolotlStore.KEY)),
2526 Base64.DEFAULT));
2527 } catch (IOException e) {
2528 throw new AssertionError(e);
2529 }
2530 }
2531 cursor.close();
2532 return record;
2533 }
2534
2535 public List<SignedPreKeyRecord> loadSignedPreKeys(Account account) {
2536 List<SignedPreKeyRecord> prekeys = new ArrayList<>();
2537 SQLiteDatabase db = this.getReadableDatabase();
2538 String[] columns = {SQLiteAxolotlStore.KEY};
2539 String[] selectionArgs = {account.getUuid()};
2540 Cursor cursor =
2541 db.query(
2542 SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
2543 columns,
2544 SQLiteAxolotlStore.ACCOUNT + "=?",
2545 selectionArgs,
2546 null,
2547 null,
2548 null);
2549
2550 while (cursor.moveToNext()) {
2551 try {
2552 prekeys.add(
2553 new SignedPreKeyRecord(
2554 Base64.decode(
2555 cursor.getString(
2556 cursor.getColumnIndex(SQLiteAxolotlStore.KEY)),
2557 Base64.DEFAULT)));
2558 } catch (IOException ignored) {
2559 }
2560 }
2561 cursor.close();
2562 return prekeys;
2563 }
2564
2565 public int getSignedPreKeysCount(Account account) {
2566 String[] columns = {"count(" + SQLiteAxolotlStore.KEY + ")"};
2567 String[] selectionArgs = {account.getUuid()};
2568 SQLiteDatabase db = this.getReadableDatabase();
2569 Cursor cursor =
2570 db.query(
2571 SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
2572 columns,
2573 SQLiteAxolotlStore.ACCOUNT + "=?",
2574 selectionArgs,
2575 null,
2576 null,
2577 null);
2578 final int count;
2579 if (cursor.moveToFirst()) {
2580 count = cursor.getInt(0);
2581 } else {
2582 count = 0;
2583 }
2584 cursor.close();
2585 return count;
2586 }
2587
2588 public boolean containsSignedPreKey(Account account, int signedPreKeyId) {
2589 Cursor cursor = getCursorForPreKey(account, signedPreKeyId);
2590 int count = cursor.getCount();
2591 cursor.close();
2592 return count != 0;
2593 }
2594
2595 public void storeSignedPreKey(Account account, SignedPreKeyRecord record) {
2596 SQLiteDatabase db = this.getWritableDatabase();
2597 ContentValues values = new ContentValues();
2598 values.put(SQLiteAxolotlStore.ID, record.getId());
2599 values.put(
2600 SQLiteAxolotlStore.KEY, Base64.encodeToString(record.serialize(), Base64.DEFAULT));
2601 values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
2602 db.insert(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME, null, values);
2603 }
2604
2605 public void deleteSignedPreKey(Account account, int signedPreKeyId) {
2606 SQLiteDatabase db = this.getWritableDatabase();
2607 String[] args = {account.getUuid(), Integer.toString(signedPreKeyId)};
2608 db.delete(
2609 SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
2610 SQLiteAxolotlStore.ACCOUNT + "=? AND " + SQLiteAxolotlStore.ID + "=?",
2611 args);
2612 }
2613
2614 private Cursor getIdentityKeyCursor(Account account, String name, boolean own) {
2615 final SQLiteDatabase db = this.getReadableDatabase();
2616 return getIdentityKeyCursor(db, account, name, own);
2617 }
2618
2619 private Cursor getIdentityKeyCursor(
2620 SQLiteDatabase db, Account account, String name, boolean own) {
2621 return getIdentityKeyCursor(db, account, name, own, null);
2622 }
2623
2624 private Cursor getIdentityKeyCursor(Account account, String fingerprint) {
2625 final SQLiteDatabase db = this.getReadableDatabase();
2626 return getIdentityKeyCursor(db, account, fingerprint);
2627 }
2628
2629 private Cursor getIdentityKeyCursor(SQLiteDatabase db, Account account, String fingerprint) {
2630 return getIdentityKeyCursor(db, account, null, null, fingerprint);
2631 }
2632
2633 private Cursor getIdentityKeyCursor(
2634 SQLiteDatabase db, Account account, String name, Boolean own, String fingerprint) {
2635 String[] columns = {
2636 SQLiteAxolotlStore.TRUST,
2637 SQLiteAxolotlStore.ACTIVE,
2638 SQLiteAxolotlStore.LAST_ACTIVATION,
2639 SQLiteAxolotlStore.KEY
2640 };
2641 ArrayList<String> selectionArgs = new ArrayList<>(4);
2642 selectionArgs.add(account.getUuid());
2643 String selectionString = SQLiteAxolotlStore.ACCOUNT + " = ?";
2644 if (name != null) {
2645 selectionArgs.add(name);
2646 selectionString += " AND " + SQLiteAxolotlStore.NAME + " = ?";
2647 }
2648 if (fingerprint != null) {
2649 selectionArgs.add(fingerprint);
2650 selectionString += " AND " + SQLiteAxolotlStore.FINGERPRINT + " = ?";
2651 }
2652 if (own != null) {
2653 selectionArgs.add(own ? "1" : "0");
2654 selectionString += " AND " + SQLiteAxolotlStore.OWN + " = ?";
2655 }
2656 Cursor cursor =
2657 db.query(
2658 SQLiteAxolotlStore.IDENTITIES_TABLENAME,
2659 columns,
2660 selectionString,
2661 selectionArgs.toArray(new String[selectionArgs.size()]),
2662 null,
2663 null,
2664 null);
2665
2666 return cursor;
2667 }
2668
2669 public IdentityKeyPair loadOwnIdentityKeyPair(Account account) {
2670 SQLiteDatabase db = getReadableDatabase();
2671 return loadOwnIdentityKeyPair(db, account);
2672 }
2673
2674 private IdentityKeyPair loadOwnIdentityKeyPair(SQLiteDatabase db, Account account) {
2675 String name = account.getJid().asBareJid().toString();
2676 IdentityKeyPair identityKeyPair = null;
2677 Cursor cursor = getIdentityKeyCursor(db, account, name, true);
2678 if (cursor.getCount() != 0) {
2679 cursor.moveToFirst();
2680 try {
2681 identityKeyPair =
2682 new IdentityKeyPair(
2683 Base64.decode(
2684 cursor.getString(
2685 cursor.getColumnIndex(SQLiteAxolotlStore.KEY)),
2686 Base64.DEFAULT));
2687 } catch (InvalidKeyException e) {
2688 Log.d(
2689 Config.LOGTAG,
2690 AxolotlService.getLogprefix(account)
2691 + "Encountered invalid IdentityKey in database for account"
2692 + account.getJid().asBareJid()
2693 + ", address: "
2694 + name);
2695 }
2696 }
2697 cursor.close();
2698
2699 return identityKeyPair;
2700 }
2701
2702 public Set<IdentityKey> loadIdentityKeys(Account account, String name) {
2703 return loadIdentityKeys(account, name, null);
2704 }
2705
2706 public Set<IdentityKey> loadIdentityKeys(
2707 Account account, String name, FingerprintStatus status) {
2708 Set<IdentityKey> identityKeys = new HashSet<>();
2709 Cursor cursor = getIdentityKeyCursor(account, name, false);
2710
2711 while (cursor.moveToNext()) {
2712 if (status != null && !FingerprintStatus.fromCursor(cursor).equals(status)) {
2713 continue;
2714 }
2715 try {
2716 String key = cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY));
2717 if (key != null) {
2718 identityKeys.add(new IdentityKey(Base64.decode(key, Base64.DEFAULT), 0));
2719 } else {
2720 Log.d(
2721 Config.LOGTAG,
2722 AxolotlService.getLogprefix(account)
2723 + "Missing key (possibly preverified) in database for account"
2724 + account.getJid().asBareJid()
2725 + ", address: "
2726 + name);
2727 }
2728 } catch (InvalidKeyException e) {
2729 Log.d(
2730 Config.LOGTAG,
2731 AxolotlService.getLogprefix(account)
2732 + "Encountered invalid IdentityKey in database for account"
2733 + account.getJid().asBareJid()
2734 + ", address: "
2735 + name);
2736 }
2737 }
2738 cursor.close();
2739
2740 return identityKeys;
2741 }
2742
2743 public long numTrustedKeys(Account account, String name) {
2744 SQLiteDatabase db = getReadableDatabase();
2745 String[] args = {
2746 account.getUuid(),
2747 name,
2748 FingerprintStatus.Trust.TRUSTED.toString(),
2749 FingerprintStatus.Trust.VERIFIED.toString(),
2750 FingerprintStatus.Trust.VERIFIED_X509.toString()
2751 };
2752 return DatabaseUtils.queryNumEntries(
2753 db,
2754 SQLiteAxolotlStore.IDENTITIES_TABLENAME,
2755 SQLiteAxolotlStore.ACCOUNT
2756 + " = ?"
2757 + " AND "
2758 + SQLiteAxolotlStore.NAME
2759 + " = ?"
2760 + " AND ("
2761 + SQLiteAxolotlStore.TRUST
2762 + " = ? OR "
2763 + SQLiteAxolotlStore.TRUST
2764 + " = ? OR "
2765 + SQLiteAxolotlStore.TRUST
2766 + " = ?)"
2767 + " AND "
2768 + SQLiteAxolotlStore.ACTIVE
2769 + " > 0",
2770 args);
2771 }
2772
2773 private void storeIdentityKey(
2774 Account account,
2775 String name,
2776 boolean own,
2777 String fingerprint,
2778 String base64Serialized,
2779 FingerprintStatus status) {
2780 SQLiteDatabase db = this.getWritableDatabase();
2781 ContentValues values = new ContentValues();
2782 values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
2783 values.put(SQLiteAxolotlStore.NAME, name);
2784 values.put(SQLiteAxolotlStore.OWN, own ? 1 : 0);
2785 values.put(SQLiteAxolotlStore.FINGERPRINT, fingerprint);
2786 values.put(SQLiteAxolotlStore.KEY, base64Serialized);
2787 values.putAll(status.toContentValues());
2788 String where =
2789 SQLiteAxolotlStore.ACCOUNT
2790 + "=? AND "
2791 + SQLiteAxolotlStore.NAME
2792 + "=? AND "
2793 + SQLiteAxolotlStore.FINGERPRINT
2794 + " =?";
2795 String[] whereArgs = {account.getUuid(), name, fingerprint};
2796 int rows = db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, values, where, whereArgs);
2797 if (rows == 0) {
2798 db.insert(SQLiteAxolotlStore.IDENTITIES_TABLENAME, null, values);
2799 }
2800 }
2801
2802 public void storePreVerification(
2803 Account account, String name, String fingerprint, FingerprintStatus status) {
2804 SQLiteDatabase db = this.getWritableDatabase();
2805 ContentValues values = new ContentValues();
2806 values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
2807 values.put(SQLiteAxolotlStore.NAME, name);
2808 values.put(SQLiteAxolotlStore.OWN, 0);
2809 values.put(SQLiteAxolotlStore.FINGERPRINT, fingerprint);
2810 values.putAll(status.toContentValues());
2811 db.insert(SQLiteAxolotlStore.IDENTITIES_TABLENAME, null, values);
2812 }
2813
2814 public FingerprintStatus getFingerprintStatus(Account account, String fingerprint) {
2815 Cursor cursor = getIdentityKeyCursor(account, fingerprint);
2816 final FingerprintStatus status;
2817 if (cursor.getCount() > 0) {
2818 cursor.moveToFirst();
2819 status = FingerprintStatus.fromCursor(cursor);
2820 } else {
2821 status = null;
2822 }
2823 cursor.close();
2824 return status;
2825 }
2826
2827 public boolean setIdentityKeyTrust(
2828 Account account, String fingerprint, FingerprintStatus fingerprintStatus) {
2829 SQLiteDatabase db = this.getWritableDatabase();
2830 return setIdentityKeyTrust(db, account, fingerprint, fingerprintStatus);
2831 }
2832
2833 private boolean setIdentityKeyTrust(
2834 SQLiteDatabase db, Account account, String fingerprint, FingerprintStatus status) {
2835 String[] selectionArgs = {account.getUuid(), fingerprint};
2836 int rows =
2837 db.update(
2838 SQLiteAxolotlStore.IDENTITIES_TABLENAME,
2839 status.toContentValues(),
2840 SQLiteAxolotlStore.ACCOUNT
2841 + " = ? AND "
2842 + SQLiteAxolotlStore.FINGERPRINT
2843 + " = ? ",
2844 selectionArgs);
2845 return rows == 1;
2846 }
2847
2848 public boolean setIdentityKeyCertificate(
2849 Account account, String fingerprint, X509Certificate x509Certificate) {
2850 SQLiteDatabase db = this.getWritableDatabase();
2851 String[] selectionArgs = {account.getUuid(), fingerprint};
2852 try {
2853 ContentValues values = new ContentValues();
2854 values.put(SQLiteAxolotlStore.CERTIFICATE, x509Certificate.getEncoded());
2855 return db.update(
2856 SQLiteAxolotlStore.IDENTITIES_TABLENAME,
2857 values,
2858 SQLiteAxolotlStore.ACCOUNT
2859 + " = ? AND "
2860 + SQLiteAxolotlStore.FINGERPRINT
2861 + " = ? ",
2862 selectionArgs)
2863 == 1;
2864 } catch (CertificateEncodingException e) {
2865 Log.d(Config.LOGTAG, "could not encode certificate");
2866 return false;
2867 }
2868 }
2869
2870 public X509Certificate getIdentityKeyCertifcate(Account account, String fingerprint) {
2871 SQLiteDatabase db = this.getReadableDatabase();
2872 String[] selectionArgs = {account.getUuid(), fingerprint};
2873 String[] colums = {SQLiteAxolotlStore.CERTIFICATE};
2874 String selection =
2875 SQLiteAxolotlStore.ACCOUNT + " = ? AND " + SQLiteAxolotlStore.FINGERPRINT + " = ? ";
2876 Cursor cursor =
2877 db.query(
2878 SQLiteAxolotlStore.IDENTITIES_TABLENAME,
2879 colums,
2880 selection,
2881 selectionArgs,
2882 null,
2883 null,
2884 null);
2885 if (cursor.getCount() < 1) {
2886 return null;
2887 } else {
2888 cursor.moveToFirst();
2889 byte[] certificate =
2890 cursor.getBlob(cursor.getColumnIndex(SQLiteAxolotlStore.CERTIFICATE));
2891 cursor.close();
2892 if (certificate == null || certificate.length == 0) {
2893 return null;
2894 }
2895 try {
2896 CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
2897 return (X509Certificate)
2898 certificateFactory.generateCertificate(
2899 new ByteArrayInputStream(certificate));
2900 } catch (CertificateException e) {
2901 Log.d(Config.LOGTAG, "certificate exception " + e.getMessage());
2902 return null;
2903 }
2904 }
2905 }
2906
2907 public void storeIdentityKey(
2908 Account account, String name, IdentityKey identityKey, FingerprintStatus status) {
2909 storeIdentityKey(
2910 account,
2911 name,
2912 false,
2913 CryptoHelper.bytesToHex(identityKey.getPublicKey().serialize()),
2914 Base64.encodeToString(identityKey.serialize(), Base64.DEFAULT),
2915 status);
2916 }
2917
2918 public void storeOwnIdentityKeyPair(Account account, IdentityKeyPair identityKeyPair) {
2919 storeIdentityKey(
2920 account,
2921 account.getJid().asBareJid().toString(),
2922 true,
2923 CryptoHelper.bytesToHex(identityKeyPair.getPublicKey().serialize()),
2924 Base64.encodeToString(identityKeyPair.serialize(), Base64.DEFAULT),
2925 FingerprintStatus.createActiveVerified(false));
2926 }
2927
2928 private void recreateAxolotlDb(SQLiteDatabase db) {
2929 Log.d(
2930 Config.LOGTAG,
2931 AxolotlService.LOGPREFIX + " : " + ">>> (RE)CREATING AXOLOTL DATABASE <<<");
2932 db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.SESSION_TABLENAME);
2933 db.execSQL(CREATE_SESSIONS_STATEMENT);
2934 db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.PREKEY_TABLENAME);
2935 db.execSQL(CREATE_PREKEYS_STATEMENT);
2936 db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME);
2937 db.execSQL(CREATE_SIGNED_PREKEYS_STATEMENT);
2938 db.execSQL("DROP TABLE IF EXISTS " + SQLiteAxolotlStore.IDENTITIES_TABLENAME);
2939 db.execSQL(CREATE_IDENTITIES_STATEMENT);
2940 }
2941
2942 public void wipeAxolotlDb(Account account) {
2943 String accountName = account.getUuid();
2944 Log.d(
2945 Config.LOGTAG,
2946 AxolotlService.getLogprefix(account)
2947 + ">>> WIPING AXOLOTL DATABASE FOR ACCOUNT "
2948 + accountName
2949 + " <<<");
2950 SQLiteDatabase db = this.getWritableDatabase();
2951 String[] deleteArgs = {accountName};
2952 db.delete(
2953 SQLiteAxolotlStore.SESSION_TABLENAME,
2954 SQLiteAxolotlStore.ACCOUNT + " = ?",
2955 deleteArgs);
2956 db.delete(
2957 SQLiteAxolotlStore.PREKEY_TABLENAME,
2958 SQLiteAxolotlStore.ACCOUNT + " = ?",
2959 deleteArgs);
2960 db.delete(
2961 SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
2962 SQLiteAxolotlStore.ACCOUNT + " = ?",
2963 deleteArgs);
2964 db.delete(
2965 SQLiteAxolotlStore.IDENTITIES_TABLENAME,
2966 SQLiteAxolotlStore.ACCOUNT + " = ?",
2967 deleteArgs);
2968 }
2969
2970 public List<ShortcutService.FrequentContact> getFrequentContacts(final int days) {
2971 final var db = this.getReadableDatabase();
2972 final String SQL =
2973 "select "
2974 + Conversation.TABLENAME
2975 + "."
2976 + Conversation.UUID
2977 + ","
2978 + Conversation.TABLENAME
2979 + "."
2980 + Conversation.ACCOUNT
2981 + ","
2982 + Conversation.TABLENAME
2983 + "."
2984 + Conversation.CONTACTJID
2985 + " from "
2986 + Conversation.TABLENAME
2987 + " join "
2988 + Message.TABLENAME
2989 + " on conversations.uuid=messages.conversationUuid where"
2990 + " messages.status!=0 and carbon==0 and conversations.mode=0 and"
2991 + " messages.timeSent>=? group by conversations.uuid order by count(body)"
2992 + " desc limit 4;";
2993 String[] whereArgs =
2994 new String[] {
2995 String.valueOf(System.currentTimeMillis() - (Config.MILLISECONDS_IN_DAY * days))
2996 };
2997 Cursor cursor = db.rawQuery(SQL, whereArgs);
2998 ArrayList<ShortcutService.FrequentContact> contacts = new ArrayList<>();
2999 while (cursor.moveToNext()) {
3000 try {
3001 contacts.add(
3002 new ShortcutService.FrequentContact(
3003 cursor.getString(0),
3004 cursor.getString(1),
3005 Jid.of(cursor.getString(2))));
3006 } catch (final Exception e) {
3007 Log.e(Config.LOGTAG, "could not create frequent contact", e);
3008 }
3009 }
3010 cursor.close();
3011 return contacts;
3012 }
3013}