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