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.List;
34import java.util.Map;
35import java.util.Set;
36import java.util.UUID;
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.QuickConversationsService;
53import eu.siacs.conversations.services.ShortcutService;
54import eu.siacs.conversations.utils.CryptoHelper;
55import eu.siacs.conversations.utils.FtsUtils;
56import eu.siacs.conversations.utils.MimeUtils;
57import eu.siacs.conversations.utils.Resolver;
58import eu.siacs.conversations.xmpp.InvalidJid;
59import eu.siacs.conversations.xmpp.mam.MamReference;
60import rocks.xmpp.addr.Jid;
61
62public class DatabaseBackend extends SQLiteOpenHelper {
63
64 private static final String DATABASE_NAME = "history";
65 private static final int DATABASE_VERSION = 44;
66 private static DatabaseBackend instance = null;
67 private static String CREATE_CONTATCS_STATEMENT = "create table "
68 + Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, "
69 + Contact.SERVERNAME + " TEXT, " + Contact.SYSTEMNAME + " TEXT,"
70 + Contact.JID + " TEXT," + Contact.KEYS + " TEXT,"
71 + Contact.PHOTOURI + " TEXT," + Contact.OPTIONS + " NUMBER,"
72 + Contact.SYSTEMACCOUNT + " NUMBER, " + Contact.AVATAR + " TEXT, "
73 + Contact.LAST_PRESENCE + " TEXT, " + Contact.LAST_TIME + " NUMBER, "
74 + Contact.GROUPS + " TEXT, FOREIGN KEY(" + Contact.ACCOUNT + ") REFERENCES "
75 + Account.TABLENAME + "(" + Account.UUID
76 + ") ON DELETE CASCADE, UNIQUE(" + Contact.ACCOUNT + ", "
77 + Contact.JID + ") ON CONFLICT REPLACE);";
78
79 private static String CREATE_DISCOVERY_RESULTS_STATEMENT = "create table "
80 + ServiceDiscoveryResult.TABLENAME + "("
81 + ServiceDiscoveryResult.HASH + " TEXT, "
82 + ServiceDiscoveryResult.VER + " TEXT, "
83 + ServiceDiscoveryResult.RESULT + " TEXT, "
84 + "UNIQUE(" + ServiceDiscoveryResult.HASH + ", "
85 + ServiceDiscoveryResult.VER + ") ON CONFLICT REPLACE);";
86
87 private static String CREATE_PRESENCE_TEMPLATES_STATEMENT = "CREATE TABLE "
88 + PresenceTemplate.TABELNAME + "("
89 + PresenceTemplate.UUID + " TEXT, "
90 + PresenceTemplate.LAST_USED + " NUMBER,"
91 + PresenceTemplate.MESSAGE + " TEXT,"
92 + PresenceTemplate.STATUS + " TEXT,"
93 + "UNIQUE(" + PresenceTemplate.MESSAGE + "," + PresenceTemplate.STATUS + ") ON CONFLICT REPLACE);";
94
95 private static String CREATE_PREKEYS_STATEMENT = "CREATE TABLE "
96 + SQLiteAxolotlStore.PREKEY_TABLENAME + "("
97 + SQLiteAxolotlStore.ACCOUNT + " TEXT, "
98 + SQLiteAxolotlStore.ID + " INTEGER, "
99 + SQLiteAxolotlStore.KEY + " TEXT, FOREIGN KEY("
100 + SQLiteAxolotlStore.ACCOUNT
101 + ") REFERENCES " + Account.TABLENAME + "(" + Account.UUID + ") ON DELETE CASCADE, "
102 + "UNIQUE( " + SQLiteAxolotlStore.ACCOUNT + ", "
103 + SQLiteAxolotlStore.ID
104 + ") ON CONFLICT REPLACE"
105 + ");";
106
107 private static String CREATE_SIGNED_PREKEYS_STATEMENT = "CREATE TABLE "
108 + SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME + "("
109 + SQLiteAxolotlStore.ACCOUNT + " TEXT, "
110 + SQLiteAxolotlStore.ID + " INTEGER, "
111 + SQLiteAxolotlStore.KEY + " TEXT, FOREIGN KEY("
112 + SQLiteAxolotlStore.ACCOUNT
113 + ") REFERENCES " + Account.TABLENAME + "(" + Account.UUID + ") ON DELETE CASCADE, "
114 + "UNIQUE( " + SQLiteAxolotlStore.ACCOUNT + ", "
115 + SQLiteAxolotlStore.ID
116 + ") ON CONFLICT REPLACE" +
117 ");";
118
119 private static String CREATE_SESSIONS_STATEMENT = "CREATE TABLE "
120 + SQLiteAxolotlStore.SESSION_TABLENAME + "("
121 + SQLiteAxolotlStore.ACCOUNT + " TEXT, "
122 + SQLiteAxolotlStore.NAME + " TEXT, "
123 + SQLiteAxolotlStore.DEVICE_ID + " INTEGER, "
124 + SQLiteAxolotlStore.KEY + " TEXT, FOREIGN KEY("
125 + SQLiteAxolotlStore.ACCOUNT
126 + ") REFERENCES " + Account.TABLENAME + "(" + Account.UUID + ") ON DELETE CASCADE, "
127 + "UNIQUE( " + SQLiteAxolotlStore.ACCOUNT + ", "
128 + SQLiteAxolotlStore.NAME + ", "
129 + SQLiteAxolotlStore.DEVICE_ID
130 + ") ON CONFLICT REPLACE"
131 + ");";
132
133 private static String CREATE_IDENTITIES_STATEMENT = "CREATE TABLE "
134 + SQLiteAxolotlStore.IDENTITIES_TABLENAME + "("
135 + SQLiteAxolotlStore.ACCOUNT + " TEXT, "
136 + SQLiteAxolotlStore.NAME + " TEXT, "
137 + SQLiteAxolotlStore.OWN + " INTEGER, "
138 + SQLiteAxolotlStore.FINGERPRINT + " TEXT, "
139 + SQLiteAxolotlStore.CERTIFICATE + " BLOB, "
140 + SQLiteAxolotlStore.TRUST + " TEXT, "
141 + SQLiteAxolotlStore.ACTIVE + " NUMBER, "
142 + SQLiteAxolotlStore.LAST_ACTIVATION + " NUMBER,"
143 + SQLiteAxolotlStore.KEY + " TEXT, FOREIGN KEY("
144 + SQLiteAxolotlStore.ACCOUNT
145 + ") REFERENCES " + Account.TABLENAME + "(" + Account.UUID + ") ON DELETE CASCADE, "
146 + "UNIQUE( " + SQLiteAxolotlStore.ACCOUNT + ", "
147 + SQLiteAxolotlStore.NAME + ", "
148 + SQLiteAxolotlStore.FINGERPRINT
149 + ") ON CONFLICT IGNORE"
150 + ");";
151
152 private static String RESOLVER_RESULTS_TABLENAME = "resolver_results";
153
154 private static String CREATE_RESOLVER_RESULTS_TABLE = "create table " + RESOLVER_RESULTS_TABLENAME + "("
155 + Resolver.Result.DOMAIN + " TEXT,"
156 + Resolver.Result.HOSTNAME + " TEXT,"
157 + Resolver.Result.IP + " BLOB,"
158 + Resolver.Result.PRIORITY + " NUMBER,"
159 + Resolver.Result.DIRECT_TLS + " NUMBER,"
160 + Resolver.Result.AUTHENTICATED + " NUMBER,"
161 + Resolver.Result.PORT + " NUMBER,"
162 + "UNIQUE(" + Resolver.Result.DOMAIN + ") ON CONFLICT REPLACE"
163 + ");";
164
165 private static String CREATE_MESSAGE_TIME_INDEX = "create INDEX message_time_index ON " + Message.TABLENAME + "(" + Message.TIME_SENT + ")";
166 private static String CREATE_MESSAGE_CONVERSATION_INDEX = "create INDEX message_conversation_index ON " + Message.TABLENAME + "(" + Message.CONVERSATION + ")";
167 private static String CREATE_MESSAGE_DELETED_INDEX = "create index message_deleted_index ON " + Message.TABLENAME + "(" + Message.DELETED + ")";
168 private static String CREATE_MESSAGE_RELATIVE_FILE_PATH_INDEX = "create INDEX message_file_path_index ON " + Message.TABLENAME + "(" + Message.RELATIVE_FILE_PATH + ")";
169 private static String CREATE_MESSAGE_TYPE_INDEX = "create INDEX message_type_index ON " + Message.TABLENAME + "(" + Message.TYPE + ")";
170
171 private static String CREATE_MESSAGE_INDEX_TABLE = "CREATE VIRTUAL TABLE messages_index USING FTS4(uuid TEXT PRIMARY KEY, body TEXT)";
172 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;";
173 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;";
174 private static String COPY_PREEXISTING_ENTRIES = "INSERT into messages_index(uuid,body) select uuid,body FROM " + Message.TABLENAME + ";";
175
176 private DatabaseBackend(Context context) {
177 super(context, DATABASE_NAME, null, DATABASE_VERSION);
178 }
179
180 private static ContentValues createFingerprintStatusContentValues(FingerprintStatus.Trust trust, boolean active) {
181 ContentValues values = new ContentValues();
182 values.put(SQLiteAxolotlStore.TRUST, trust.toString());
183 values.put(SQLiteAxolotlStore.ACTIVE, active ? 1 : 0);
184 return values;
185 }
186
187 public static synchronized DatabaseBackend getInstance(Context context) {
188 if (instance == null) {
189 instance = new DatabaseBackend(context);
190 }
191 return instance;
192 }
193
194 @Override
195 public void onConfigure(SQLiteDatabase db) {
196 db.execSQL("PRAGMA foreign_keys=ON");
197 db.rawQuery("PRAGMA secure_delete=ON", null);
198 }
199
200 @Override
201 public void onCreate(SQLiteDatabase db) {
202 db.execSQL("create table " + Account.TABLENAME + "(" + Account.UUID + " TEXT PRIMARY KEY,"
203 + Account.USERNAME + " TEXT,"
204 + Account.SERVER + " TEXT,"
205 + Account.PASSWORD + " TEXT,"
206 + Account.DISPLAY_NAME + " TEXT, "
207 + Account.STATUS + " TEXT,"
208 + Account.STATUS_MESSAGE + " TEXT,"
209 + Account.ROSTERVERSION + " TEXT,"
210 + Account.OPTIONS + " NUMBER, "
211 + Account.AVATAR + " TEXT, "
212 + Account.KEYS + " TEXT, "
213 + Account.HOSTNAME + " TEXT, "
214 + Account.RESOURCE + " TEXT,"
215 + Account.PORT + " NUMBER DEFAULT 5222)");
216 db.execSQL("create table " + Conversation.TABLENAME + " ("
217 + Conversation.UUID + " TEXT PRIMARY KEY, " + Conversation.NAME
218 + " TEXT, " + Conversation.CONTACT + " TEXT, "
219 + Conversation.ACCOUNT + " TEXT, " + Conversation.CONTACTJID
220 + " TEXT, " + Conversation.CREATED + " NUMBER, "
221 + Conversation.STATUS + " NUMBER, " + Conversation.MODE
222 + " NUMBER, " + Conversation.ATTRIBUTES + " TEXT, FOREIGN KEY("
223 + Conversation.ACCOUNT + ") REFERENCES " + Account.TABLENAME
224 + "(" + Account.UUID + ") ON DELETE CASCADE);");
225 db.execSQL("create table " + Message.TABLENAME + "( " + Message.UUID
226 + " TEXT PRIMARY KEY, " + Message.CONVERSATION + " TEXT, "
227 + Message.TIME_SENT + " NUMBER, " + Message.COUNTERPART
228 + " TEXT, " + Message.TRUE_COUNTERPART + " TEXT,"
229 + Message.BODY + " TEXT, " + Message.ENCRYPTION + " NUMBER, "
230 + Message.STATUS + " NUMBER," + Message.TYPE + " NUMBER, "
231 + Message.RELATIVE_FILE_PATH + " TEXT, "
232 + Message.SERVER_MSG_ID + " TEXT, "
233 + Message.FINGERPRINT + " TEXT, "
234 + Message.CARBON + " INTEGER, "
235 + Message.EDITED + " TEXT, "
236 + Message.READ + " NUMBER DEFAULT 1, "
237 + Message.OOB + " INTEGER, "
238 + Message.ERROR_MESSAGE + " TEXT,"
239 + Message.READ_BY_MARKERS + " TEXT,"
240 + Message.MARKABLE + " NUMBER DEFAULT 0,"
241 + Message.DELETED + " NUMBER DEFAULT 0,"
242 + Message.REMOTE_MSG_ID + " TEXT, FOREIGN KEY("
243 + Message.CONVERSATION + ") REFERENCES "
244 + Conversation.TABLENAME + "(" + Conversation.UUID
245 + ") ON DELETE CASCADE);");
246 db.execSQL(CREATE_MESSAGE_TIME_INDEX);
247 db.execSQL(CREATE_MESSAGE_CONVERSATION_INDEX);
248 db.execSQL(CREATE_MESSAGE_DELETED_INDEX);
249 db.execSQL(CREATE_MESSAGE_RELATIVE_FILE_PATH_INDEX);
250 db.execSQL(CREATE_MESSAGE_TYPE_INDEX);
251 db.execSQL(CREATE_CONTATCS_STATEMENT);
252 db.execSQL(CREATE_DISCOVERY_RESULTS_STATEMENT);
253 db.execSQL(CREATE_SESSIONS_STATEMENT);
254 db.execSQL(CREATE_PREKEYS_STATEMENT);
255 db.execSQL(CREATE_SIGNED_PREKEYS_STATEMENT);
256 db.execSQL(CREATE_IDENTITIES_STATEMENT);
257 db.execSQL(CREATE_PRESENCE_TEMPLATES_STATEMENT);
258 db.execSQL(CREATE_RESOLVER_RESULTS_TABLE);
259 db.execSQL(CREATE_MESSAGE_INDEX_TABLE);
260 db.execSQL(CREATE_MESSAGE_INSERT_TRIGGER);
261 db.execSQL(CREATE_MESSAGE_UPDATE_TRIGGER);
262 }
263
264 @Override
265 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
266 if (oldVersion < 2 && newVersion >= 2) {
267 db.execSQL("update " + Account.TABLENAME + " set "
268 + Account.OPTIONS + " = " + Account.OPTIONS + " | 8");
269 }
270 if (oldVersion < 3 && newVersion >= 3) {
271 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
272 + Message.TYPE + " NUMBER");
273 }
274 if (oldVersion < 5 && newVersion >= 5) {
275 db.execSQL("DROP TABLE " + Contact.TABLENAME);
276 db.execSQL(CREATE_CONTATCS_STATEMENT);
277 db.execSQL("UPDATE " + Account.TABLENAME + " SET "
278 + Account.ROSTERVERSION + " = NULL");
279 }
280 if (oldVersion < 6 && newVersion >= 6) {
281 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
282 + Message.TRUE_COUNTERPART + " TEXT");
283 }
284 if (oldVersion < 7 && newVersion >= 7) {
285 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
286 + Message.REMOTE_MSG_ID + " TEXT");
287 db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN "
288 + Contact.AVATAR + " TEXT");
289 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN "
290 + Account.AVATAR + " TEXT");
291 }
292 if (oldVersion < 8 && newVersion >= 8) {
293 db.execSQL("ALTER TABLE " + Conversation.TABLENAME + " ADD COLUMN "
294 + Conversation.ATTRIBUTES + " TEXT");
295 }
296 if (oldVersion < 9 && newVersion >= 9) {
297 db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN "
298 + Contact.LAST_TIME + " NUMBER");
299 db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN "
300 + Contact.LAST_PRESENCE + " TEXT");
301 }
302 if (oldVersion < 10 && newVersion >= 10) {
303 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
304 + Message.RELATIVE_FILE_PATH + " TEXT");
305 }
306 if (oldVersion < 11 && newVersion >= 11) {
307 db.execSQL("ALTER TABLE " + Contact.TABLENAME + " ADD COLUMN "
308 + Contact.GROUPS + " TEXT");
309 db.execSQL("delete from " + Contact.TABLENAME);
310 db.execSQL("update " + Account.TABLENAME + " set " + Account.ROSTERVERSION + " = NULL");
311 }
312 if (oldVersion < 12 && newVersion >= 12) {
313 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
314 + Message.SERVER_MSG_ID + " TEXT");
315 }
316 if (oldVersion < 13 && newVersion >= 13) {
317 db.execSQL("delete from " + Contact.TABLENAME);
318 db.execSQL("update " + Account.TABLENAME + " set " + Account.ROSTERVERSION + " = NULL");
319 }
320 if (oldVersion < 14 && newVersion >= 14) {
321 canonicalizeJids(db);
322 }
323 if (oldVersion < 15 && newVersion >= 15) {
324 recreateAxolotlDb(db);
325 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
326 + Message.FINGERPRINT + " TEXT");
327 }
328 if (oldVersion < 16 && newVersion >= 16) {
329 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
330 + Message.CARBON + " INTEGER");
331 }
332 if (oldVersion < 19 && newVersion >= 19) {
333 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.DISPLAY_NAME + " TEXT");
334 }
335 if (oldVersion < 20 && newVersion >= 20) {
336 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.HOSTNAME + " TEXT");
337 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.PORT + " NUMBER DEFAULT 5222");
338 }
339 if (oldVersion < 26 && newVersion >= 26) {
340 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.STATUS + " TEXT");
341 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.STATUS_MESSAGE + " TEXT");
342 }
343 if (oldVersion < 40 && newVersion >= 40) {
344 db.execSQL("ALTER TABLE " + Account.TABLENAME + " ADD COLUMN " + Account.RESOURCE + " TEXT");
345 }
346 /* Any migrations that alter the Account table need to happen BEFORE this migration, as it
347 * depends on account de-serialization.
348 */
349 if (oldVersion < 17 && newVersion >= 17 && newVersion < 31) {
350 List<Account> accounts = getAccounts(db);
351 for (Account account : accounts) {
352 String ownDeviceIdString = account.getKey(SQLiteAxolotlStore.JSONKEY_REGISTRATION_ID);
353 if (ownDeviceIdString == null) {
354 continue;
355 }
356 int ownDeviceId = Integer.valueOf(ownDeviceIdString);
357 SignalProtocolAddress ownAddress = new SignalProtocolAddress(account.getJid().asBareJid().toString(), ownDeviceId);
358 deleteSession(db, account, ownAddress);
359 IdentityKeyPair identityKeyPair = loadOwnIdentityKeyPair(db, account);
360 if (identityKeyPair != null) {
361 String[] selectionArgs = {
362 account.getUuid(),
363 CryptoHelper.bytesToHex(identityKeyPair.getPublicKey().serialize())
364 };
365 ContentValues values = new ContentValues();
366 values.put(SQLiteAxolotlStore.TRUSTED, 2);
367 db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, values,
368 SQLiteAxolotlStore.ACCOUNT + " = ? AND "
369 + SQLiteAxolotlStore.FINGERPRINT + " = ? ",
370 selectionArgs);
371 } else {
372 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": could not load own identity key pair");
373 }
374 }
375 }
376 if (oldVersion < 18 && newVersion >= 18) {
377 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.READ + " NUMBER DEFAULT 1");
378 }
379
380 if (oldVersion < 21 && newVersion >= 21) {
381 List<Account> accounts = getAccounts(db);
382 for (Account account : accounts) {
383 account.unsetPgpSignature();
384 db.update(Account.TABLENAME, account.getContentValues(), Account.UUID
385 + "=?", new String[]{account.getUuid()});
386 }
387 }
388
389 if (oldVersion >= 15 && oldVersion < 22 && newVersion >= 22) {
390 db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.CERTIFICATE);
391 }
392
393 if (oldVersion < 23 && newVersion >= 23) {
394 db.execSQL(CREATE_DISCOVERY_RESULTS_STATEMENT);
395 }
396
397 if (oldVersion < 24 && newVersion >= 24) {
398 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.EDITED + " TEXT");
399 }
400
401 if (oldVersion < 25 && newVersion >= 25) {
402 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.OOB + " INTEGER");
403 }
404
405 if (oldVersion < 26 && newVersion >= 26) {
406 db.execSQL(CREATE_PRESENCE_TEMPLATES_STATEMENT);
407 }
408
409 if (oldVersion < 27 && newVersion >= 27) {
410 db.execSQL("DELETE FROM " + ServiceDiscoveryResult.TABLENAME);
411 }
412
413 if (oldVersion < 28 && newVersion >= 28) {
414 canonicalizeJids(db);
415 }
416
417 if (oldVersion < 29 && newVersion >= 29) {
418 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.ERROR_MESSAGE + " TEXT");
419 }
420 if (oldVersion >= 15 && oldVersion < 31 && newVersion >= 31) {
421 db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.TRUST + " TEXT");
422 db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.ACTIVE + " NUMBER");
423 HashMap<Integer, ContentValues> migration = new HashMap<>();
424 migration.put(0, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, true));
425 migration.put(1, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, true));
426 migration.put(2, createFingerprintStatusContentValues(FingerprintStatus.Trust.UNTRUSTED, true));
427 migration.put(3, createFingerprintStatusContentValues(FingerprintStatus.Trust.COMPROMISED, false));
428 migration.put(4, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, false));
429 migration.put(5, createFingerprintStatusContentValues(FingerprintStatus.Trust.TRUSTED, false));
430 migration.put(6, createFingerprintStatusContentValues(FingerprintStatus.Trust.UNTRUSTED, false));
431 migration.put(7, createFingerprintStatusContentValues(FingerprintStatus.Trust.VERIFIED_X509, true));
432 migration.put(8, createFingerprintStatusContentValues(FingerprintStatus.Trust.VERIFIED_X509, false));
433 for (Map.Entry<Integer, ContentValues> entry : migration.entrySet()) {
434 String whereClause = SQLiteAxolotlStore.TRUSTED + "=?";
435 String[] where = {String.valueOf(entry.getKey())};
436 db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, entry.getValue(), whereClause, where);
437 }
438
439 }
440 if (oldVersion >= 15 && oldVersion < 32 && newVersion >= 32) {
441 db.execSQL("ALTER TABLE " + SQLiteAxolotlStore.IDENTITIES_TABLENAME + " ADD COLUMN " + SQLiteAxolotlStore.LAST_ACTIVATION + " NUMBER");
442 ContentValues defaults = new ContentValues();
443 defaults.put(SQLiteAxolotlStore.LAST_ACTIVATION, System.currentTimeMillis());
444 db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, defaults, null, null);
445 }
446 if (oldVersion >= 15 && oldVersion < 33 && newVersion >= 33) {
447 String whereClause = SQLiteAxolotlStore.OWN + "=1";
448 db.update(SQLiteAxolotlStore.IDENTITIES_TABLENAME, createFingerprintStatusContentValues(FingerprintStatus.Trust.VERIFIED, true), whereClause, null);
449 }
450
451 if (oldVersion < 34 && newVersion >= 34) {
452 db.execSQL(CREATE_MESSAGE_TIME_INDEX);
453
454 final File oldPicturesDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/Conversations/");
455 final File oldFilesDirectory = new File(Environment.getExternalStorageDirectory() + "/Conversations/");
456 final File newFilesDirectory = new File(Environment.getExternalStorageDirectory() + "/Conversations/Media/Conversations Files/");
457 final File newVideosDirectory = new File(Environment.getExternalStorageDirectory() + "/Conversations/Media/Conversations Videos/");
458 if (oldPicturesDirectory.exists() && oldPicturesDirectory.isDirectory()) {
459 final File newPicturesDirectory = new File(Environment.getExternalStorageDirectory() + "/Conversations/Media/Conversations Images/");
460 newPicturesDirectory.getParentFile().mkdirs();
461 if (oldPicturesDirectory.renameTo(newPicturesDirectory)) {
462 Log.d(Config.LOGTAG, "moved " + oldPicturesDirectory.getAbsolutePath() + " to " + newPicturesDirectory.getAbsolutePath());
463 }
464 }
465 if (oldFilesDirectory.exists() && oldFilesDirectory.isDirectory()) {
466 newFilesDirectory.mkdirs();
467 newVideosDirectory.mkdirs();
468 final File[] files = oldFilesDirectory.listFiles();
469 if (files == null) {
470 return;
471 }
472 for (File file : files) {
473 if (file.getName().equals(".nomedia")) {
474 if (file.delete()) {
475 Log.d(Config.LOGTAG, "deleted nomedia file in " + oldFilesDirectory.getAbsolutePath());
476 }
477 } else if (file.isFile()) {
478 final String name = file.getName();
479 boolean isVideo = false;
480 int start = name.lastIndexOf('.') + 1;
481 if (start < name.length()) {
482 String mime = MimeUtils.guessMimeTypeFromExtension(name.substring(start));
483 isVideo = mime != null && mime.startsWith("video/");
484 }
485 File dst = new File((isVideo ? newVideosDirectory : newFilesDirectory).getAbsolutePath() + "/" + file.getName());
486 if (file.renameTo(dst)) {
487 Log.d(Config.LOGTAG, "moved " + file + " to " + dst);
488 }
489 }
490 }
491 }
492 }
493 if (oldVersion < 35 && newVersion >= 35) {
494 db.execSQL(CREATE_MESSAGE_CONVERSATION_INDEX);
495 }
496 if (oldVersion < 36 && newVersion >= 36) {
497 List<Account> accounts = getAccounts(db);
498 for (Account account : accounts) {
499 account.setOption(Account.OPTION_REQUIRES_ACCESS_MODE_CHANGE, true);
500 account.setOption(Account.OPTION_LOGGED_IN_SUCCESSFULLY, false);
501 db.update(Account.TABLENAME, account.getContentValues(), Account.UUID
502 + "=?", new String[]{account.getUuid()});
503 }
504 }
505
506 if (oldVersion < 37 && newVersion >= 37) {
507 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.READ_BY_MARKERS + " TEXT");
508 }
509
510 if (oldVersion < 38 && newVersion >= 38) {
511 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.MARKABLE + " NUMBER DEFAULT 0");
512 }
513
514 if (oldVersion < 39 && newVersion >= 39) {
515 db.execSQL(CREATE_RESOLVER_RESULTS_TABLE);
516 }
517
518 if (oldVersion < 41 && newVersion >= 41) {
519 db.execSQL(CREATE_MESSAGE_INDEX_TABLE);
520 db.execSQL(CREATE_MESSAGE_INSERT_TRIGGER);
521 db.execSQL(CREATE_MESSAGE_UPDATE_TRIGGER);
522 db.execSQL(COPY_PREEXISTING_ENTRIES);
523 }
524
525 if (oldVersion < 42 && newVersion >= 42) {
526 db.execSQL("DROP TRIGGER IF EXISTS after_message_delete");
527 }
528 if (QuickConversationsService.isQuicksy() && oldVersion < 43 && newVersion >= 43) {
529 List<Account> accounts = getAccounts(db);
530 for (Account account : accounts) {
531 account.setOption(Account.OPTION_MAGIC_CREATE, true);
532 db.update(Account.TABLENAME, account.getContentValues(), Account.UUID
533 + "=?", new String[]{account.getUuid()});
534 }
535 }
536
537 if (oldVersion < 44 && newVersion >= 44) {
538 db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.DELETED + " NUMBER DEFAULT 0");
539 db.execSQL(CREATE_MESSAGE_DELETED_INDEX);
540 db.execSQL(CREATE_MESSAGE_RELATIVE_FILE_PATH_INDEX);
541 db.execSQL(CREATE_MESSAGE_TYPE_INDEX);
542 }
543 }
544
545 private void canonicalizeJids(SQLiteDatabase db) {
546 // migrate db to new, canonicalized JID domainpart representation
547
548 // Conversation table
549 Cursor cursor = db.rawQuery("select * from " + Conversation.TABLENAME, new String[0]);
550 while (cursor.moveToNext()) {
551 String newJid;
552 try {
553 newJid = Jid.of(cursor.getString(cursor.getColumnIndex(Conversation.CONTACTJID))).toString();
554 } catch (IllegalArgumentException ignored) {
555 Log.e(Config.LOGTAG, "Failed to migrate Conversation CONTACTJID "
556 + cursor.getString(cursor.getColumnIndex(Conversation.CONTACTJID))
557 + ": " + ignored + ". Skipping...");
558 continue;
559 }
560
561 String updateArgs[] = {
562 newJid,
563 cursor.getString(cursor.getColumnIndex(Conversation.UUID)),
564 };
565 db.execSQL("update " + Conversation.TABLENAME
566 + " set " + Conversation.CONTACTJID + " = ? "
567 + " where " + Conversation.UUID + " = ?", updateArgs);
568 }
569 cursor.close();
570
571 // Contact table
572 cursor = db.rawQuery("select * from " + Contact.TABLENAME, new String[0]);
573 while (cursor.moveToNext()) {
574 String newJid;
575 try {
576 newJid = Jid.of(cursor.getString(cursor.getColumnIndex(Contact.JID))).toString();
577 } catch (IllegalArgumentException ignored) {
578 Log.e(Config.LOGTAG, "Failed to migrate Contact JID "
579 + cursor.getString(cursor.getColumnIndex(Contact.JID))
580 + ": " + ignored + ". Skipping...");
581 continue;
582 }
583
584 String updateArgs[] = {
585 newJid,
586 cursor.getString(cursor.getColumnIndex(Contact.ACCOUNT)),
587 cursor.getString(cursor.getColumnIndex(Contact.JID)),
588 };
589 db.execSQL("update " + Contact.TABLENAME
590 + " set " + Contact.JID + " = ? "
591 + " where " + Contact.ACCOUNT + " = ? "
592 + " AND " + Contact.JID + " = ?", updateArgs);
593 }
594 cursor.close();
595
596 // Account table
597 cursor = db.rawQuery("select * from " + Account.TABLENAME, new String[0]);
598 while (cursor.moveToNext()) {
599 String newServer;
600 try {
601 newServer = Jid.of(
602 cursor.getString(cursor.getColumnIndex(Account.USERNAME)),
603 cursor.getString(cursor.getColumnIndex(Account.SERVER)),
604 null
605 ).getDomain();
606 } catch (IllegalArgumentException ignored) {
607 Log.e(Config.LOGTAG, "Failed to migrate Account SERVER "
608 + cursor.getString(cursor.getColumnIndex(Account.SERVER))
609 + ": " + ignored + ". Skipping...");
610 continue;
611 }
612
613 String updateArgs[] = {
614 newServer,
615 cursor.getString(cursor.getColumnIndex(Account.UUID)),
616 };
617 db.execSQL("update " + Account.TABLENAME
618 + " set " + Account.SERVER + " = ? "
619 + " where " + Account.UUID + " = ?", updateArgs);
620 }
621 cursor.close();
622 }
623
624 public void createConversation(Conversation conversation) {
625 SQLiteDatabase db = this.getWritableDatabase();
626 db.insert(Conversation.TABLENAME, null, conversation.getContentValues());
627 }
628
629 public void createMessage(Message message) {
630 SQLiteDatabase db = this.getWritableDatabase();
631 db.insert(Message.TABLENAME, null, message.getContentValues());
632 }
633
634 public void createAccount(Account account) {
635 SQLiteDatabase db = this.getWritableDatabase();
636 db.insert(Account.TABLENAME, null, account.getContentValues());
637 }
638
639 public void insertDiscoveryResult(ServiceDiscoveryResult result) {
640 SQLiteDatabase db = this.getWritableDatabase();
641 db.insert(ServiceDiscoveryResult.TABLENAME, null, result.getContentValues());
642 }
643
644 public ServiceDiscoveryResult findDiscoveryResult(final String hash, final String ver) {
645 SQLiteDatabase db = this.getReadableDatabase();
646 String[] selectionArgs = {hash, ver};
647 Cursor cursor = db.query(ServiceDiscoveryResult.TABLENAME, null,
648 ServiceDiscoveryResult.HASH + "=? AND " + ServiceDiscoveryResult.VER + "=?",
649 selectionArgs, null, null, null);
650 if (cursor.getCount() == 0) {
651 cursor.close();
652 return null;
653 }
654 cursor.moveToFirst();
655
656 ServiceDiscoveryResult result = null;
657 try {
658 result = new ServiceDiscoveryResult(cursor);
659 } catch (JSONException e) { /* result is still null */ }
660
661 cursor.close();
662 return result;
663 }
664
665 public void saveResolverResult(String domain, Resolver.Result result) {
666 SQLiteDatabase db = this.getWritableDatabase();
667 ContentValues contentValues = result.toContentValues();
668 contentValues.put(Resolver.Result.DOMAIN, domain);
669 db.insert(RESOLVER_RESULTS_TABLENAME, null, contentValues);
670 }
671
672 public synchronized Resolver.Result findResolverResult(String domain) {
673 SQLiteDatabase db = this.getReadableDatabase();
674 String where = Resolver.Result.DOMAIN + "=?";
675 String[] whereArgs = {domain};
676 final Cursor cursor = db.query(RESOLVER_RESULTS_TABLENAME, null, where, whereArgs, null, null, null);
677 Resolver.Result result = null;
678 if (cursor != null) {
679 try {
680 if (cursor.moveToFirst()) {
681 result = Resolver.Result.fromCursor(cursor);
682 }
683 } catch (Exception e) {
684 Log.d(Config.LOGTAG, "unable to find cached resolver result in database " + e.getMessage());
685 return null;
686 } finally {
687 cursor.close();
688 }
689 }
690 return result;
691 }
692
693 public void insertPresenceTemplate(PresenceTemplate template) {
694 SQLiteDatabase db = this.getWritableDatabase();
695 String whereToDelete = PresenceTemplate.MESSAGE + "=?";
696 String[] whereToDeleteArgs = {template.getStatusMessage()};
697 db.delete(PresenceTemplate.TABELNAME, whereToDelete, whereToDeleteArgs);
698 db.delete(PresenceTemplate.TABELNAME, PresenceTemplate.UUID + " not in (select " + PresenceTemplate.UUID + " from " + PresenceTemplate.TABELNAME + " order by " + PresenceTemplate.LAST_USED + " desc limit 9)", null);
699 db.insert(PresenceTemplate.TABELNAME, null, template.getContentValues());
700 }
701
702 public List<PresenceTemplate> getPresenceTemplates() {
703 ArrayList<PresenceTemplate> templates = new ArrayList<>();
704 SQLiteDatabase db = this.getReadableDatabase();
705 Cursor cursor = db.query(PresenceTemplate.TABELNAME, null, null, null, null, null, PresenceTemplate.LAST_USED + " desc");
706 while (cursor.moveToNext()) {
707 templates.add(PresenceTemplate.fromCursor(cursor));
708 }
709 cursor.close();
710 return templates;
711 }
712
713 public CopyOnWriteArrayList<Conversation> getConversations(int status) {
714 CopyOnWriteArrayList<Conversation> list = new CopyOnWriteArrayList<>();
715 SQLiteDatabase db = this.getReadableDatabase();
716 String[] selectionArgs = {Integer.toString(status)};
717 Cursor cursor = db.rawQuery("select * from " + Conversation.TABLENAME
718 + " where " + Conversation.STATUS + " = ? and " + Conversation.CONTACTJID + " is not null order by "
719 + Conversation.CREATED + " desc", selectionArgs);
720 while (cursor.moveToNext()) {
721 final Conversation conversation = Conversation.fromCursor(cursor);
722 if (conversation.getJid() instanceof InvalidJid) {
723 continue;
724 }
725 list.add(conversation);
726 }
727 cursor.close();
728 return list;
729 }
730
731 public ArrayList<Message> getMessages(Conversation conversations, int limit) {
732 return getMessages(conversations, limit, -1);
733 }
734
735 public ArrayList<Message> getMessages(Conversation conversation, int limit, long timestamp) {
736 ArrayList<Message> list = new ArrayList<>();
737 SQLiteDatabase db = this.getReadableDatabase();
738 Cursor cursor;
739 if (timestamp == -1) {
740 String[] selectionArgs = {conversation.getUuid()};
741 cursor = db.query(Message.TABLENAME, null, Message.CONVERSATION
742 + "=?", selectionArgs, null, null, Message.TIME_SENT
743 + " DESC", String.valueOf(limit));
744 } else {
745 String[] selectionArgs = {conversation.getUuid(),
746 Long.toString(timestamp)};
747 cursor = db.query(Message.TABLENAME, null, Message.CONVERSATION
748 + "=? and " + Message.TIME_SENT + "<?", selectionArgs,
749 null, null, Message.TIME_SENT + " DESC",
750 String.valueOf(limit));
751 }
752 while (cursor.moveToNext()) {
753 try {
754 final Message message = Message.fromCursor(cursor, conversation);
755 if (message != null) {
756 list.add(0, message);
757 }
758 } catch (Exception e) {
759 Log.e(Config.LOGTAG,"unable to restore message");
760 }
761 }
762 cursor.close();
763 return list;
764 }
765
766 public Cursor getMessageSearchCursor(List<String> term) {
767 SQLiteDatabase db = this.getReadableDatabase();
768 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;
769 Log.d(Config.LOGTAG, "search term: " + FtsUtils.toMatchString(term));
770 return db.rawQuery(SQL, new String[]{FtsUtils.toMatchString(term)});
771 }
772
773 public List<String> markFileAsDeleted(final File file, final boolean internal) {
774 SQLiteDatabase db = this.getReadableDatabase();
775 String selection;
776 String[] selectionArgs;
777 if (internal) {
778 final String name = file.getName();
779 if (name.endsWith(".pgp")) {
780 selection = "(" + Message.RELATIVE_FILE_PATH + " IN(?,?) OR (" + Message.RELATIVE_FILE_PATH + "=? and encryption in(1,4))) and type in (1,2,5)";
781 selectionArgs = new String[]{file.getAbsolutePath(), name, name.substring(0, name.length() - 4)};
782 } else {
783 selection = Message.RELATIVE_FILE_PATH + " IN(?,?) and type in (1,2,5)";
784 selectionArgs = new String[]{file.getAbsolutePath(), name};
785 }
786 } else {
787 selection = Message.RELATIVE_FILE_PATH + "=? and type in (1,2,5)";
788 selectionArgs = new String[]{file.getAbsolutePath()};
789 }
790 final List<String> uuids = new ArrayList<>();
791 Cursor cursor = db.query(Message.TABLENAME, new String[]{Message.UUID}, selection, selectionArgs, null, null, null);
792 while (cursor != null && cursor.moveToNext()) {
793 uuids.add(cursor.getString(0));
794 }
795 if (cursor != null) {
796 cursor.close();
797 }
798 markFileAsDeleted(uuids);
799 return uuids;
800 }
801
802 public void markFileAsDeleted(List<String> uuids) {
803 SQLiteDatabase db = this.getReadableDatabase();
804 final ContentValues contentValues = new ContentValues();
805 final String where = Message.UUID + "=?";
806 contentValues.put(Message.DELETED, 1);
807 db.beginTransaction();
808 for (String uuid : uuids) {
809 db.update(Message.TABLENAME, contentValues, where, new String[]{uuid});
810 }
811 db.setTransactionSuccessful();
812 db.endTransaction();
813 }
814
815 public void markFilesAsChanged(List<FilePathInfo> files) {
816 SQLiteDatabase db = this.getReadableDatabase();
817 final String where = Message.UUID + "=?";
818 db.beginTransaction();
819 for (FilePathInfo info : files) {
820 final ContentValues contentValues = new ContentValues();
821 contentValues.put(Message.DELETED, info.deleted ? 1 : 0);
822 db.update(Message.TABLENAME, contentValues, where, new String[]{info.uuid.toString()});
823 }
824 db.setTransactionSuccessful();
825 db.endTransaction();
826 }
827
828 public List<FilePathInfo> getFilePathInfo() {
829 final SQLiteDatabase db = this.getReadableDatabase();
830 final Cursor cursor = db.query(Message.TABLENAME, new String[]{Message.UUID, Message.RELATIVE_FILE_PATH, Message.DELETED}, "type in (1,2,5) and "+Message.RELATIVE_FILE_PATH+" is not null", null, null, null, null);
831 final List<FilePathInfo> list = new ArrayList<>();
832 while (cursor != null && cursor.moveToNext()) {
833 list.add(new FilePathInfo(cursor.getString(0), cursor.getString(1), cursor.getInt(2) > 0));
834 }
835 if (cursor != null) {
836 cursor.close();
837 }
838 return list;
839 }
840
841 public List<FilePath> getRelativeFilePaths(String account, Jid jid, int limit) {
842 SQLiteDatabase db = this.getReadableDatabase();
843 final String SQL = "select uuid,relativeFilePath from messages where type in (1,2,5) and deleted=0 and "+Message.RELATIVE_FILE_PATH+" is not null and conversationUuid=(select uuid from conversations where accountUuid=? and (contactJid=? or contactJid like ?)) order by timeSent desc";
844 final String[] args = {account, jid.toEscapedString(), jid.toEscapedString() + "/%"};
845 Cursor cursor = db.rawQuery(SQL + (limit > 0 ? " limit " + String.valueOf(limit) : ""), args);
846 List<FilePath> filesPaths = new ArrayList<>();
847 while (cursor.moveToNext()) {
848 filesPaths.add(new FilePath(cursor.getString(0), cursor.getString(1)));
849 }
850 cursor.close();
851 return filesPaths;
852 }
853
854 public static class FilePath {
855 public final UUID uuid;
856 public final String path;
857
858 private FilePath(String uuid, String path) {
859 this.uuid = UUID.fromString(uuid);
860 this.path = path;
861 }
862 }
863
864 public static class FilePathInfo extends FilePath {
865 public boolean deleted;
866
867 private FilePathInfo(String uuid, String path, boolean deleted) {
868 super(uuid,path);
869 this.deleted = deleted;
870 }
871
872 public boolean setDeleted(boolean deleted) {
873 final boolean changed = deleted != this.deleted;
874 this.deleted = deleted;
875 return changed;
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(final boolean enabledOnly) {
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 = enabledOnly ? "not options & (1 <<1)" : null;
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}