Added more loki databases.
parent
4f8af1b4f2
commit
bccde8baba
@ -0,0 +1,57 @@
|
|||||||
|
package org.thoughtcrime.securesms.loki
|
||||||
|
|
||||||
|
import android.content.ContentValues
|
||||||
|
import android.content.Context
|
||||||
|
import net.sqlcipher.database.SQLiteDatabase
|
||||||
|
import org.thoughtcrime.securesms.crypto.PreKeyUtil
|
||||||
|
import org.thoughtcrime.securesms.database.Database
|
||||||
|
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
|
||||||
|
import org.whispersystems.libsignal.state.PreKeyRecord
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A database for associating a `PreKeyRecord` id to a contact public key.
|
||||||
|
*/
|
||||||
|
class LokiContactPreKeyDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) {
|
||||||
|
companion object {
|
||||||
|
private val tableName = "loki_contact_pre_key_database"
|
||||||
|
private val pubKey = "pub_key"
|
||||||
|
private val preKeyId = "pre_key_id"
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
val createTableCommand = "CREATE TABLE $tableName ($preKeyId INTEGER PRIMARY KEY, $pubKey TEXT);"
|
||||||
|
}
|
||||||
|
|
||||||
|
fun hasPreKey(pubKey: String): Boolean {
|
||||||
|
val database = databaseHelper.readableDatabase
|
||||||
|
return database.get(tableName, "${Companion.pubKey} = ?", arrayOf(pubKey)) { cursor ->
|
||||||
|
cursor.count > 0
|
||||||
|
} ?: false
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getPreKey(pubKey: String): PreKeyRecord? {
|
||||||
|
val database = databaseHelper.readableDatabase
|
||||||
|
return database.get(tableName, "${Companion.pubKey} = ?", arrayOf(pubKey)) { cursor ->
|
||||||
|
val preKeyId = cursor.getInt(cursor.getColumnIndexOrThrow(preKeyId))
|
||||||
|
PreKeyUtil.loadPreKey(context, preKeyId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getOrCreatePreKey(pubKey: String): PreKeyRecord {
|
||||||
|
return getPreKey(pubKey) ?: generateAndStorePreKey(pubKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun generateAndStorePreKey(pubKey: String): PreKeyRecord {
|
||||||
|
val records = PreKeyUtil.generatePreKeys(context, 1)
|
||||||
|
PreKeyUtil.storePreKeyRecords(context, records)
|
||||||
|
|
||||||
|
val record = records.first()
|
||||||
|
val database = databaseHelper.writableDatabase
|
||||||
|
|
||||||
|
val values = ContentValues()
|
||||||
|
values.put(Companion.pubKey, pubKey)
|
||||||
|
values.put(preKeyId, record.id)
|
||||||
|
|
||||||
|
database.insertWithOnConflict(tableName, null, values, SQLiteDatabase.CONFLICT_REPLACE)
|
||||||
|
return record
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package org.thoughtcrime.securesms.loki
|
||||||
|
|
||||||
|
import android.database.Cursor
|
||||||
|
import net.sqlcipher.database.SQLiteDatabase
|
||||||
|
|
||||||
|
fun <T> SQLiteDatabase.get(table: String, query: String, arguments: Array<String>, get: (Cursor) -> T): T? {
|
||||||
|
var cursor: Cursor? = null
|
||||||
|
try {
|
||||||
|
cursor = this.query(table, null, query, arguments, null, null, null)
|
||||||
|
if (cursor != null && cursor.moveToFirst()) { return get(cursor) }
|
||||||
|
} catch (e: Exception) {
|
||||||
|
// Do nothing
|
||||||
|
} finally {
|
||||||
|
cursor?.close()
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package org.thoughtcrime.securesms.loki
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import org.thoughtcrime.securesms.database.Database
|
||||||
|
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
|
||||||
|
|
||||||
|
class LokiPreKeyBundleDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) {
|
||||||
|
}
|
Loading…
Reference in New Issue