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