diff --git a/app/schemas/com.greenart7c3.nostrsigner.database.AppDatabase/15.json b/app/schemas/com.greenart7c3.nostrsigner.database.AppDatabase/15.json new file mode 100644 index 00000000..bc1f9a9e --- /dev/null +++ b/app/schemas/com.greenart7c3.nostrsigner.database.AppDatabase/15.json @@ -0,0 +1,308 @@ +{ + "formatVersion": 1, + "database": { + "version": 15, + "identityHash": "3690c8720be33c75a319014268cf1afe", + "entities": [ + { + "tableName": "application", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`key` TEXT NOT NULL, `name` TEXT NOT NULL, `relays` TEXT NOT NULL, `url` TEXT NOT NULL, `icon` TEXT NOT NULL, `description` TEXT NOT NULL, `pubKey` TEXT NOT NULL, `isConnected` INTEGER NOT NULL, `secret` TEXT NOT NULL, `useSecret` INTEGER NOT NULL, `signPolicy` INTEGER NOT NULL, `closeApplication` INTEGER NOT NULL, `deleteAfter` INTEGER NOT NULL, `lastUsed` INTEGER NOT NULL, PRIMARY KEY(`key`))", + "fields": [ + { + "fieldPath": "key", + "columnName": "key", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "relays", + "columnName": "relays", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "url", + "columnName": "url", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "icon", + "columnName": "icon", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "description", + "columnName": "description", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "pubKey", + "columnName": "pubKey", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "isConnected", + "columnName": "isConnected", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "secret", + "columnName": "secret", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "useSecret", + "columnName": "useSecret", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "signPolicy", + "columnName": "signPolicy", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "closeApplication", + "columnName": "closeApplication", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "deleteAfter", + "columnName": "deleteAfter", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "lastUsed", + "columnName": "lastUsed", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "key" + ] + }, + "indices": [ + { + "name": "index_key", + "unique": true, + "columnNames": [ + "key" + ], + "orders": [], + "createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_key` ON `${TABLE_NAME}` (`key`)" + }, + { + "name": "index_name", + "unique": false, + "columnNames": [ + "name" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_name` ON `${TABLE_NAME}` (`name`)" + } + ] + }, + { + "tableName": "applicationPermission", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER, `pkKey` TEXT NOT NULL, `type` TEXT NOT NULL, `kind` INTEGER, `acceptable` INTEGER NOT NULL, `rememberType` INTEGER NOT NULL, `acceptUntil` INTEGER NOT NULL, `rejectUntil` INTEGER NOT NULL, PRIMARY KEY(`id`), FOREIGN KEY(`pkKey`) REFERENCES `application`(`key`) ON UPDATE NO ACTION ON DELETE CASCADE )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER" + }, + { + "fieldPath": "pkKey", + "columnName": "pkKey", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "type", + "columnName": "type", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "kind", + "columnName": "kind", + "affinity": "INTEGER" + }, + { + "fieldPath": "acceptable", + "columnName": "acceptable", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "rememberType", + "columnName": "rememberType", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "acceptUntil", + "columnName": "acceptUntil", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "rejectUntil", + "columnName": "rejectUntil", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "permissions_by_pk_key", + "unique": false, + "columnNames": [ + "pkKey" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `permissions_by_pk_key` ON `${TABLE_NAME}` (`pkKey`)" + }, + { + "name": "permissions_unique", + "unique": true, + "columnNames": [ + "pkKey", + "type", + "kind" + ], + "orders": [], + "createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `permissions_unique` ON `${TABLE_NAME}` (`pkKey`, `type`, `kind`)" + } + ], + "foreignKeys": [ + { + "table": "application", + "onDelete": "CASCADE", + "onUpdate": "NO ACTION", + "columns": [ + "pkKey" + ], + "referencedColumns": [ + "key" + ] + } + ] + }, + { + "tableName": "history2", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `pkKey` TEXT NOT NULL, `type` TEXT NOT NULL, `kind` INTEGER, `time` INTEGER NOT NULL, `accepted` INTEGER NOT NULL)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "pkKey", + "columnName": "pkKey", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "type", + "columnName": "type", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "kind", + "columnName": "kind", + "affinity": "INTEGER" + }, + { + "fieldPath": "time", + "columnName": "time", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "accepted", + "columnName": "accepted", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "history_by_pk_key2", + "unique": false, + "columnNames": [ + "pkKey" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `history_by_pk_key2` ON `${TABLE_NAME}` (`pkKey`)" + }, + { + "name": "history_by_id2", + "unique": false, + "columnNames": [ + "id" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `history_by_id2` ON `${TABLE_NAME}` (`id`)" + }, + { + "name": "history_by_time", + "unique": false, + "columnNames": [ + "time" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `history_by_time` ON `${TABLE_NAME}` (`time`)" + }, + { + "name": "history_by_key_and_time", + "unique": false, + "columnNames": [ + "pkKey", + "time" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `history_by_key_and_time` ON `${TABLE_NAME}` (`pkKey`, `time`)" + } + ] + } + ], + "setupQueries": [ + "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '3690c8720be33c75a319014268cf1afe')" + ] + } +} \ No newline at end of file diff --git a/app/schemas/com.greenart7c3.nostrsigner.database.LogDatabase/1.json b/app/schemas/com.greenart7c3.nostrsigner.database.LogDatabase/1.json new file mode 100644 index 00000000..b53587e8 --- /dev/null +++ b/app/schemas/com.greenart7c3.nostrsigner.database.LogDatabase/1.json @@ -0,0 +1,55 @@ +{ + "formatVersion": 1, + "database": { + "version": 1, + "identityHash": "3d70a45c6ee30a3f8c9d79b6d3a55cf0", + "entities": [ + { + "tableName": "amber_log", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `url` TEXT NOT NULL, `type` TEXT NOT NULL, `message` TEXT NOT NULL, `time` INTEGER NOT NULL)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "url", + "columnName": "url", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "type", + "columnName": "type", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "message", + "columnName": "message", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "time", + "columnName": "time", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + } + } + ], + "setupQueries": [ + "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '3d70a45c6ee30a3f8c9d79b6d3a55cf0')" + ] + } +} \ No newline at end of file diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/Amber.kt b/app/src/main/java/com/greenart7c3/nostrsigner/Amber.kt index 60ee6d1c..fb91e429 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/Amber.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/Amber.kt @@ -16,6 +16,7 @@ import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.PeriodicWorkRequestBuilder import androidx.work.WorkManager import com.greenart7c3.nostrsigner.database.AppDatabase +import com.greenart7c3.nostrsigner.database.LogDatabase import com.greenart7c3.nostrsigner.models.Account import com.greenart7c3.nostrsigner.models.AmberSettings import com.greenart7c3.nostrsigner.models.FeedbackType @@ -115,6 +116,7 @@ class Amber : Application(), LifecycleObserver { val profileSubscription = ProfileSubscription(client, this, applicationIOScope) private var databases = ConcurrentHashMap() + private var logDatabases = ConcurrentHashMap() val isOnMobileDataState = mutableStateOf(false) val isOnWifiDataState = mutableStateOf(false) @@ -373,6 +375,13 @@ class Amber : Application(), LifecycleObserver { return databases[npub]!! } + fun getLogDatabase(npub: String): LogDatabase { + if (!logDatabases.containsKey(npub)) { + logDatabases[npub] = LogDatabase.getDatabase(this, npub) + } + return logDatabases[npub]!! + } + fun getSavedRelays(): Set { val savedRelays = mutableSetOf() LocalPreferences.allSavedAccounts(this).forEach { accountInfo -> diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/SignerProvider.kt b/app/src/main/java/com/greenart7c3/nostrsigner/SignerProvider.kt index ae7ceb7b..3a69d04f 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/SignerProvider.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/SignerProvider.kt @@ -277,6 +277,7 @@ class SignerProvider : ContentProvider() { val pubkey = projection[1] val account = LocalPreferences.loadFromEncryptedStorageSync(context!!, npub) ?: return null val database = Amber.instance.getDatabase(account.npub) + val logDatabase = Amber.instance.getLogDatabase(account.npub) var permission = database .applicationDao() @@ -351,7 +352,7 @@ class SignerProvider : ContentProvider() { ) ?: "Could not decrypt the message" } catch (e: Exception) { scope.launch { - database.applicationDao().insertLog( + logDatabase.logDao().insertLog( LogEntity( 0, packageName, @@ -460,8 +461,8 @@ class SignerProvider : ContentProvider() { } catch (e: Exception) { scope.launch { LocalPreferences.allSavedAccounts(context!!).forEach { accInfo -> - val database = Amber.instance.getDatabase(accInfo.npub) - database.applicationDao().insertLog( + val database = Amber.instance.getLogDatabase(accInfo.npub) + database.logDao().insertLog( LogEntity( 0, packageName, diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/database/AppDatabase.kt b/app/src/main/java/com/greenart7c3/nostrsigner/database/AppDatabase.kt index 40effd1f..cb573d5c 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/database/AppDatabase.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/database/AppDatabase.kt @@ -128,14 +128,23 @@ val MIGRATION_13_14 = object : Migration(13, 14) { } } +val MIGRATION_14_15 = object : Migration(14, 15) { + override fun migrate(db: SupportSQLiteDatabase) { + try { + db.execSQL("DROP TABLE amber_log") + } catch (e: Exception) { + Log.e(Amber.TAG, "No amber_log table", e) + } + } +} + @Database( entities = [ ApplicationEntity::class, ApplicationPermissionsEntity::class, - LogEntity::class, HistoryEntity2::class, ], - version = 14, + version = 15, ) @TypeConverters(Converters::class) abstract class AppDatabase : RoomDatabase() { @@ -171,6 +180,7 @@ abstract class AppDatabase : RoomDatabase() { .addMigrations(MIGRATION_11_12) .addMigrations(MIGRATION_12_13) .addMigrations(MIGRATION_13_14) + .addMigrations(MIGRATION_14_15) .build() instance.openHelper.writableDatabase.execSQL("VACUUM") diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/database/ApplicationDao.kt b/app/src/main/java/com/greenart7c3/nostrsigner/database/ApplicationDao.kt index ab6abfe4..abaa680d 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/database/ApplicationDao.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/database/ApplicationDao.kt @@ -158,20 +158,6 @@ interface ApplicationDao { @Query("DELETE FROM history2 where pkKey = :pk") suspend fun deleteHistory(pk: String) - @Insert - @Transaction - suspend fun insertLog(logEntity: LogEntity) - - @Query("SELECT * FROM amber_log ORDER BY time DESC") - fun getLogs(): Flow> - - @Query("SELECT * FROM amber_log where url = :url ORDER BY time DESC") - fun getLogsByUrl(url: String): Flow> - - @Query("DELETE FROM amber_log") - @Transaction - suspend fun clearLogs() - @Delete @Transaction suspend fun deletePermission(permission: ApplicationPermissionsEntity) @@ -188,18 +174,6 @@ interface ApplicationDao { @Transaction suspend fun deleteHistory(historyEntity: HistoryEntity2) - @Query("SELECT COUNT(*) FROM amber_log WHERE time < :time") - @Transaction - suspend fun countOldLog(time: Long): Long - - @Query("SELECT * FROM amber_log WHERE time < :time LIMIT 100") - @Transaction - suspend fun getOldLog(time: Long): List - - @Delete - @Transaction - suspend fun deleteLog(logEntity: LogEntity) - @Query("DELETE FROM application WHERE deleteAfter < :time AND deleteAfter > 0") @Transaction suspend fun deleteOldApplications(time: Long): Int diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/database/ApplicationEntity.kt b/app/src/main/java/com/greenart7c3/nostrsigner/database/ApplicationEntity.kt index eb44cb44..fabee693 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/database/ApplicationEntity.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/database/ApplicationEntity.kt @@ -93,8 +93,3 @@ class Converters { } } } - -data class ApplicationWithLatestHistory( - @Embedded val application: ApplicationEntity, - val latestTime: Long?, -) diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/database/LogDao.kt b/app/src/main/java/com/greenart7c3/nostrsigner/database/LogDao.kt new file mode 100644 index 00000000..064c6db5 --- /dev/null +++ b/app/src/main/java/com/greenart7c3/nostrsigner/database/LogDao.kt @@ -0,0 +1,37 @@ +package com.greenart7c3.nostrsigner.database + +import androidx.room.Dao +import androidx.room.Delete +import androidx.room.Insert +import androidx.room.Query +import androidx.room.Transaction +import kotlinx.coroutines.flow.Flow + +@Dao +interface LogDao { + @Insert + @Transaction + suspend fun insertLog(logEntity: LogEntity) + + @Query("SELECT * FROM amber_log ORDER BY time DESC") + fun getLogs(): Flow> + + @Query("SELECT * FROM amber_log where url = :url ORDER BY time DESC") + fun getLogsByUrl(url: String): Flow> + + @Query("DELETE FROM amber_log") + @Transaction + suspend fun clearLogs() + + @Query("SELECT COUNT(*) FROM amber_log WHERE time < :time") + @Transaction + suspend fun countOldLog(time: Long): Long + + @Query("SELECT * FROM amber_log WHERE time < :time LIMIT 100") + @Transaction + suspend fun getOldLog(time: Long): List + + @Delete + @Transaction + suspend fun deleteLog(logEntity: LogEntity) +} diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/database/LogDatabase.kt b/app/src/main/java/com/greenart7c3/nostrsigner/database/LogDatabase.kt new file mode 100644 index 00000000..99c6be27 --- /dev/null +++ b/app/src/main/java/com/greenart7c3/nostrsigner/database/LogDatabase.kt @@ -0,0 +1,44 @@ +package com.greenart7c3.nostrsigner.database + +import android.content.Context +import androidx.room.Database +import androidx.room.Room +import androidx.room.RoomDatabase +import androidx.room.TypeConverters +import java.util.concurrent.Executors + +@Database( + entities = [ + LogEntity::class, + ], + version = 1, +) +@TypeConverters(Converters::class) +abstract class LogDatabase : RoomDatabase() { + abstract fun logDao(): LogDao + + companion object { + fun getDatabase( + context: Context, + npub: String, + ): LogDatabase { + return synchronized(this) { + val executor = Executors.newCachedThreadPool() + val transactionExecutor = Executors.newCachedThreadPool() + + val instance = + Room.databaseBuilder( + context, + LogDatabase::class.java, + "log_db_$npub", + ) + .setQueryExecutor(executor) + .setTransactionExecutor(transactionExecutor) + .build() + instance.openHelper.writableDatabase.execSQL("VACUUM") + + instance + } + } + } +} diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/relays/NostrClientLoggerListener.kt b/app/src/main/java/com/greenart7c3/nostrsigner/relays/NostrClientLoggerListener.kt index 10390cef..17a7a925 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/relays/NostrClientLoggerListener.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/relays/NostrClientLoggerListener.kt @@ -50,7 +50,7 @@ class NostrClientLoggerListener( override fun onAuth(relay: IRelayClient, challenge: String) { scope.launch { LocalPreferences.currentAccount(context)?.let { account -> - Amber.instance.getDatabase(account).applicationDao().insertLog( + Amber.instance.getLogDatabase(account).logDao().insertLog( LogEntity( id = 0, url = relay.url.url, @@ -66,7 +66,7 @@ class NostrClientLoggerListener( override fun onBeforeSend(relay: IRelayClient, event: Event) { scope.launch { LocalPreferences.currentAccount(context)?.let { account -> - Amber.instance.getDatabase(account).applicationDao().insertLog( + Amber.instance.getLogDatabase(account).logDao().insertLog( LogEntity( id = 0, url = relay.url.url, @@ -82,7 +82,7 @@ class NostrClientLoggerListener( override fun onSend(relay: IRelayClient, msg: String, success: Boolean) { scope.launch { LocalPreferences.currentAccount(context)?.let { account -> - Amber.instance.getDatabase(account).applicationDao().insertLog( + Amber.instance.getLogDatabase(account).logDao().insertLog( LogEntity( id = 0, url = relay.url.url, @@ -105,7 +105,7 @@ class NostrClientLoggerListener( override fun onSendResponse(relay: IRelayClient, eventId: String, success: Boolean, message: String) { scope.launch { LocalPreferences.currentAccount(context)?.let { account -> - Amber.instance.getDatabase(account).applicationDao().insertLog( + Amber.instance.getLogDatabase(account).logDao().insertLog( LogEntity( id = 0, url = relay.url.url, @@ -132,7 +132,7 @@ class NostrClientLoggerListener( if (error.message?.trim()?.equals("Relay sent notice:") == true) return scope.launch { LocalPreferences.currentAccount(context)?.let { account -> - Amber.instance.getDatabase(account).applicationDao().insertLog( + Amber.instance.getLogDatabase(account).logDao().insertLog( LogEntity( id = 0, url = relay.url.url, @@ -157,7 +157,7 @@ class NostrClientLoggerListener( override fun onEvent(relay: IRelayClient, subId: String, event: Event, arrivalTime: Long, afterEOSE: Boolean) { scope.launch { LocalPreferences.currentAccount(context)?.let { account -> - Amber.instance.getDatabase(account).applicationDao().insertLog( + Amber.instance.getLogDatabase(account).logDao().insertLog( LogEntity( id = 0, url = relay.url.url, @@ -175,7 +175,7 @@ class NostrClientLoggerListener( override fun onNotify(relay: IRelayClient, description: String) { scope.launch { LocalPreferences.currentAccount(context)?.let { account -> - Amber.instance.getDatabase(account).applicationDao().insertLog( + Amber.instance.getLogDatabase(account).logDao().insertLog( LogEntity( id = 0, url = relay.url.url, @@ -191,7 +191,7 @@ class NostrClientLoggerListener( override fun onRelayStateChange(relay: IRelayClient, type: RelayState) { scope.launch { LocalPreferences.currentAccount(context)?.let { account -> - Amber.instance.getDatabase(account).applicationDao().insertLog( + Amber.instance.getLogDatabase(account).logDao().insertLog( LogEntity( id = 0, url = relay.url.url, diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/service/BunkerRequestUtils.kt b/app/src/main/java/com/greenart7c3/nostrsigner/service/BunkerRequestUtils.kt index 4d048e9f..1f4958db 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/service/BunkerRequestUtils.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/service/BunkerRequestUtils.kt @@ -65,7 +65,7 @@ object BunkerRequestUtils { if (relays.isEmpty()) { onDone(true) onLoading(false) - Amber.instance.getDatabase(account.npub).applicationDao().insertLog( + Amber.instance.getLogDatabase(account.npub).logDao().insertLog( LogEntity( id = 0, url = bunkerRequest.localKey, @@ -89,7 +89,7 @@ object BunkerRequestUtils { Amber.instance.applicationIOScope.launch { relays.forEach { relay -> - Amber.instance.getDatabase(account.npub).applicationDao().insertLog( + Amber.instance.getLogDatabase(account.npub).logDao().insertLog( LogEntity( id = 0, url = relay.url, @@ -124,7 +124,7 @@ object BunkerRequestUtils { onDone(false) onLoading(false) Amber.instance.applicationIOScope.launch { - Amber.instance.getDatabase(account.npub).applicationDao().insertLog( + Amber.instance.getLogDatabase(account.npub).logDao().insertLog( LogEntity( id = 0, url = bunkerRequest.localKey, diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/service/ClearLogsWorker.kt b/app/src/main/java/com/greenart7c3/nostrsigner/service/ClearLogsWorker.kt index a63d84ae..1703cadb 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/service/ClearLogsWorker.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/service/ClearLogsWorker.kt @@ -33,18 +33,19 @@ class ClearLogsWorker(appContext: Context, workerParams: WorkerParameters) : } } - val countLog = database.applicationDao().countOldLog(oneWeek) + val logDatabase = Amber.instance.getLogDatabase(it.npub) + val countLog = logDatabase.logDao().countOldLog(oneWeek) Log.d(Amber.TAG, "Deleting $countLog old log entries from ${com.greenart7c3.nostrsigner.models.TimeUtils.formatLongToCustomDateTimeWithSeconds(oneWeek)}") if (countLog > 0) { - var logs = database.applicationDao().getOldLog(oneWeek) + var logs = logDatabase.logDao().getOldLog(oneWeek) var count = 0 while (logs.isNotEmpty()) { count++ logs.forEach { history -> Log.d(Amber.TAG, "Deleting log entry ${com.greenart7c3.nostrsigner.models.TimeUtils.formatLongToCustomDateTimeWithSeconds(history.time)}") - database.applicationDao().deleteLog(history) + logDatabase.logDao().deleteLog(history) } - logs = database.applicationDao().getOldLog(oneWeek) + logs = logDatabase.logDao().getOldLog(oneWeek) } } } catch (e: Exception) { diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/service/ConnectivityService.kt b/app/src/main/java/com/greenart7c3/nostrsigner/service/ConnectivityService.kt index bdea1a3e..660ff1c6 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/service/ConnectivityService.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/service/ConnectivityService.kt @@ -129,15 +129,17 @@ class ConnectivityService : Service() { LocalPreferences.allSavedAccounts(Amber.instance).forEach { accountInfo -> val now = System.currentTimeMillis() / 1000 val deleted = Amber.instance.getDatabase(accountInfo.npub).applicationDao().deleteOldApplications(now) - Amber.instance.getDatabase(accountInfo.npub).applicationDao().insertLog( - LogEntity( - id = 0, - url = "", - type = "deleteApplications", - message = "Deleted $deleted expired applications", - time = System.currentTimeMillis(), - ), - ) + if (deleted > 0) { + Amber.instance.getLogDatabase(accountInfo.npub).logDao().insertLog( + LogEntity( + id = 0, + url = "", + type = "deleteApplications", + message = "Deleted $deleted expired applications", + time = System.currentTimeMillis(), + ), + ) + } } } diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/service/EventNotificationConsumer.kt b/app/src/main/java/com/greenart7c3/nostrsigner/service/EventNotificationConsumer.kt index d94393d3..790a77cc 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/service/EventNotificationConsumer.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/service/EventNotificationConsumer.kt @@ -67,7 +67,7 @@ class EventNotificationConsumer(private val applicationContext: Context) { val accounts = LocalPreferences.allSavedAccounts(applicationContext) accounts.forEach { LocalPreferences.loadFromEncryptedStorage(applicationContext, it.npub)?.let { acc -> - val dao = Amber.instance.getDatabase(acc.npub).applicationDao() + val dao = Amber.instance.getLogDatabase(acc.npub).logDao() dao.insertLog( LogEntity( 0, @@ -114,7 +114,7 @@ class EventNotificationConsumer(private val applicationContext: Context) { ) { if (event.content.isEmpty()) return - val dao = Amber.instance.getDatabase(acc.npub).applicationDao() + val dao = Amber.instance.getLogDatabase(acc.npub).logDao() Amber.instance.applicationIOScope.launch { dao.insertLog( LogEntity( @@ -149,12 +149,13 @@ class EventNotificationConsumer(private val applicationContext: Context) { val responseRelay = listOf(relay) val database = Amber.instance.getDatabase(acc.npub) val dao = database.applicationDao() + val logDao = Amber.instance.getLogDatabase(acc.npub).logDao() val notification = Amber.instance.notificationCache[event.id] if (notification != null) return Amber.instance.notificationCache.put(event.id, event.createdAt) - dao.insertLog( + logDao.insertLog( LogEntity( 0, "nostrsigner", @@ -194,7 +195,7 @@ class EventNotificationConsumer(private val applicationContext: Context) { val type = BunkerRequestUtils.getTypeFromBunker(bunkerRequest) if (type == SignerType.INVALID) { Log.d(Amber.TAG, "Invalid request method ${bunkerRequest.method}") - dao.insertLog( + logDao.insertLog( LogEntity( 0, "nostrsigner", diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/service/IntentUtils.kt b/app/src/main/java/com/greenart7c3/nostrsigner/service/IntentUtils.kt index 01ced358..26b254e9 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/service/IntentUtils.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/service/IntentUtils.kt @@ -176,8 +176,8 @@ object IntentUtils { ) ?: "Could not decrypt the message" } catch (e: Exception) { Amber.instance.applicationIOScope.launch { - val database = Amber.instance.getDatabase(account.npub) - database.applicationDao().insertLog( + val database = Amber.instance.getLogDatabase(account.npub) + database.logDao().insertLog( LogEntity( 0, packageName ?: "", @@ -359,8 +359,8 @@ object IntentUtils { ) ?: "Could not decrypt the message" } catch (e: Exception) { Amber.instance.applicationIOScope.launch { - val database = Amber.instance.getDatabase(account.npub) - database.applicationDao().insertLog( + val database = Amber.instance.getLogDatabase(account.npub) + database.logDao().insertLog( LogEntity( 0, packageName ?: "", @@ -508,7 +508,7 @@ object IntentUtils { } catch (e: Exception) { Amber.instance.applicationIOScope.launch { LocalPreferences.allSavedAccounts(Amber.instance).forEach { - Amber.instance.getDatabase(it.npub).applicationDao().insertLog( + Amber.instance.getLogDatabase(it.npub).logDao().insertLog( LogEntity( id = 0, url = "IntentUtils", diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/service/NostrConnectUtils.kt b/app/src/main/java/com/greenart7c3/nostrsigner/service/NostrConnectUtils.kt index e30270fb..688934ac 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/service/NostrConnectUtils.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/service/NostrConnectUtils.kt @@ -131,7 +131,7 @@ object NostrConnectUtils { } catch (e: Exception) { Log.e(Amber.TAG, e.message, e) Amber.instance.applicationIOScope.launch { - Amber.instance.getDatabase(account.npub).applicationDao().insertLog( + Amber.instance.getLogDatabase(account.npub).logDao().insertLog( LogEntity( 0, "nostrconnect", diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/ui/LogsScreen.kt b/app/src/main/java/com/greenart7c3/nostrsigner/ui/LogsScreen.kt index ccee904f..15823c2b 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/ui/LogsScreen.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/ui/LogsScreen.kt @@ -36,7 +36,7 @@ fun LogsScreen( account: Account, ) { val scope = rememberCoroutineScope() - val logsFlow = Amber.instance.getDatabase(account.npub).applicationDao().getLogs() + val logsFlow = Amber.instance.getLogDatabase(account.npub).logDao().getLogs() val logs = logsFlow.collectAsStateWithLifecycle(initialValue = emptyList()) val state = rememberLazyListState() @@ -51,7 +51,7 @@ fun LogsScreen( modifier = Modifier.padding(bottom = 8.dp), onClick = { scope.launch(Dispatchers.IO) { - Amber.instance.getDatabase(account.npub).applicationDao().clearLogs() + Amber.instance.getLogDatabase(account.npub).logDao().clearLogs() } }, text = stringResource(R.string.clear_logs), diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/ui/SettingsScreen.kt b/app/src/main/java/com/greenart7c3/nostrsigner/ui/SettingsScreen.kt index b0493d64..90ba20da 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/ui/SettingsScreen.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/ui/SettingsScreen.kt @@ -290,18 +290,19 @@ fun SettingsScreen( } } - val countLog = database.applicationDao().countOldLog(oneWeek) + val logDatabase = Amber.instance.getLogDatabase(it.npub) + val countLog = logDatabase.logDao().countOldLog(oneWeek) if (countLog > 0) { status = context.getString(R.string.deleting_old_log_entries, countLog) - var logs = database.applicationDao().getOldLog(oneWeek) + var logs = logDatabase.logDao().getOldLog(oneWeek) var count = 0 while (logs.isNotEmpty()) { count++ status = context.getString(R.string.deleting_old_log_entries_2, 100 * count, countLog) logs.forEach { history -> - database.applicationDao().deleteLog(history) + logDatabase.logDao().deleteLog(history) } - logs = database.applicationDao().getOldLog(oneWeek) + logs = logDatabase.logDao().getOldLog(oneWeek) } } val dbFile = context.getDatabasePath("amber_db_${account.npub}") diff --git a/app/src/main/java/com/greenart7c3/nostrsigner/ui/actions/EditRelaysDialog.kt b/app/src/main/java/com/greenart7c3/nostrsigner/ui/actions/EditRelaysDialog.kt index 2988cc3f..5a2659ae 100644 --- a/app/src/main/java/com/greenart7c3/nostrsigner/ui/actions/EditRelaysDialog.kt +++ b/app/src/main/java/com/greenart7c3/nostrsigner/ui/actions/EditRelaysDialog.kt @@ -451,7 +451,7 @@ fun RelayLogScreen( val context = LocalContext.current val flows = LocalPreferences.allSavedAccounts(context).map { - Amber.instance.getDatabase(it.npub).applicationDao().getLogsByUrl(url) + Amber.instance.getLogDatabase(it.npub).logDao().getLogsByUrl(url) }.merge() val logs = flows.collectAsStateWithLifecycle(initialValue = emptyList())