From 1d5360f58f0ca1c413f1a04abbf8aa2eac0b9c8f Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Mon, 13 Aug 2018 17:35:49 +0300 Subject: [PATCH] Save live messages to DB --- .../osmand/telegram/TelegramApplication.kt | 8 +- .../telegram/helpers/MessagesDbHelper.kt | 116 ++++++++++++++++++ .../osmand/telegram/helpers/TelegramHelper.kt | 13 +- .../net/osmand/telegram/ui/MainActivity.kt | 10 +- 4 files changed, 138 insertions(+), 9 deletions(-) create mode 100644 OsmAnd-telegram/src/net/osmand/telegram/helpers/MessagesDbHelper.kt diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt index 67b0bb52dc..bc8bcdcade 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt @@ -7,11 +7,8 @@ import android.net.ConnectivityManager import android.net.NetworkInfo import android.os.Build import android.os.Handler -import net.osmand.telegram.helpers.OsmandAidlHelper +import net.osmand.telegram.helpers.* import net.osmand.telegram.helpers.OsmandAidlHelper.OsmandHelperListener -import net.osmand.telegram.helpers.ShareLocationHelper -import net.osmand.telegram.helpers.ShowLocationHelper -import net.osmand.telegram.helpers.TelegramHelper import net.osmand.telegram.notifications.NotificationHelper import net.osmand.telegram.utils.AndroidUtils import net.osmand.telegram.utils.UiUtils @@ -26,6 +23,7 @@ class TelegramApplication : Application(), OsmandHelperListener { lateinit var notificationHelper: NotificationHelper private set lateinit var osmandAidlHelper: OsmandAidlHelper private set lateinit var locationProvider: TelegramLocationProvider private set + lateinit var messagesDbHelper: MessagesDbHelper private set var telegramService: TelegramService? = null @@ -37,6 +35,7 @@ class TelegramApplication : Application(), OsmandHelperListener { override fun onCreate() { super.onCreate() telegramHelper.appDir = filesDir.absolutePath + telegramHelper.init() settings = TelegramSettings(this) telegramHelper.messageActiveTimeSec = settings.locHistoryTime @@ -61,6 +60,7 @@ class TelegramApplication : Application(), OsmandHelperListener { showLocationHelper = ShowLocationHelper(this) notificationHelper = NotificationHelper(this) locationProvider = TelegramLocationProvider(this) + messagesDbHelper = MessagesDbHelper(this) if (settings.hasAnyChatToShareLocation() && AndroidUtils.isLocationPermissionAvailable(this)) { shareLocationHelper.startSharingLocation() diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/MessagesDbHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/MessagesDbHelper.kt new file mode 100644 index 0000000000..ffbbf999d6 --- /dev/null +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/MessagesDbHelper.kt @@ -0,0 +1,116 @@ +package net.osmand.telegram.helpers + +import android.content.Context +import android.database.sqlite.SQLiteDatabase +import android.database.sqlite.SQLiteOpenHelper +import net.osmand.telegram.TelegramApplication +import org.drinkless.td.libcore.telegram.TdApi + +class MessagesDbHelper(val app: TelegramApplication) { + + private val messages = HashSet() + + private val sqliteHelper: SQLiteHelper + + init { + sqliteHelper = SQLiteHelper(app) + sqliteHelper.getMessages().forEach { + app.telegramHelper.loadMessage(it.chatId, it.messageId) + } + app.telegramHelper.addIncomingMessagesListener(object : + TelegramHelper.TelegramIncomingMessagesListener { + + override fun onReceiveChatLocationMessages( + chatId: Long, vararg messages: TdApi.Message + ) { + messages.forEach { addMessage(chatId, it.id) } + } + + override fun onDeleteChatLocationMessages(chatId: Long, messages: List) { + messages.forEach { removeMessage(chatId, it.id) } + } + + override fun updateLocationMessages() {} + }) + } + + fun save() { + sqliteHelper.clearMessages() + sqliteHelper.addMessages(messages) + } + + private fun addMessage(chatId: Long, messageId: Long) { + synchronized(messages) { + messages.add(Message(chatId, messageId)) + } + } + + private fun removeMessage(chatId: Long, messageId: Long) { + synchronized(messages) { + messages.remove(Message(chatId, messageId)) + } + } + + private class SQLiteHelper(context: Context) : + SQLiteOpenHelper(context, DB_NAME, null, DB_VERSION) { + + override fun onCreate(db: SQLiteDatabase) { + db.execSQL(MESSAGES_TABLE_CREATE) + } + + override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { + db.execSQL(MESSAGES_TABLE_DELETE) + onCreate(db) + } + + internal fun addMessages(messages: Set) { + messages.forEach { + writableDatabase?.execSQL(MESSAGES_TABLE_INSERT, arrayOf(it.chatId, it.messageId)) + } + } + + internal fun getMessages(): Set { + val res = HashSet() + readableDatabase?.rawQuery(MESSAGES_TABLE_SELECT, null)?.apply { + if (moveToFirst()) { + do { + res.add(Message(getLong(0), getLong(1))) + } while (moveToNext()) + } + close() + } + return res + } + + internal fun clearMessages() { + writableDatabase?.execSQL(MESSAGES_TABLE_CLEAR) + } + + companion object { + + private const val DB_NAME = "messages.db" + private const val DB_VERSION = 1 + + private const val MESSAGES_TABLE_NAME = "messages" + private const val MESSAGES_COL_CHAT_ID = "chat_id" + private const val MESSAGES_COL_MESSAGE_ID = "message_id" + + private const val MESSAGES_TABLE_CREATE = + "CREATE TABLE IF NOT EXISTS $MESSAGES_TABLE_NAME (" + + "$MESSAGES_COL_CHAT_ID LONG, " + + "$MESSAGES_COL_MESSAGE_ID LONG)" + + private const val MESSAGES_TABLE_DELETE = "DROP TABLE IF EXISTS $MESSAGES_TABLE_NAME" + + private const val MESSAGES_TABLE_SELECT = + "SELECT $MESSAGES_COL_CHAT_ID, $MESSAGES_COL_MESSAGE_ID FROM $MESSAGES_TABLE_NAME" + + private const val MESSAGES_TABLE_CLEAR = "DELETE FROM $MESSAGES_TABLE_NAME" + + private const val MESSAGES_TABLE_INSERT = "INSERT INTO $MESSAGES_TABLE_NAME (" + + "$MESSAGES_COL_CHAT_ID, $MESSAGES_COL_MESSAGE_ID) VALUES (?, ?)" + } + } + + private data class Message(val chatId: Long, val messageId: Long) +} diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index 6ffbb6852b..c8f0857954 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -292,13 +292,20 @@ class TelegramHelper private constructor() { return if (libraryLoaded) { // create client client = Client.create(UpdatesHandler(), null, null) - client!!.send(TdApi.GetAuthorizationState(), defaultHandler) true } else { false } } + fun requestAuthorizationState() { + client?.send(TdApi.GetAuthorizationState()) { obj -> + if (obj is TdApi.AuthorizationState) { + onAuthorizationStateUpdated(obj) + } + } + } + fun isInit() = client != null && haveAuthorization fun getUserPhotoPath(user: TdApi.User): String? { @@ -473,6 +480,10 @@ class TelegramHelper private constructor() { } } + fun loadMessage(chatId: Long, messageId: Long) { + requestMessage(chatId, messageId, this@TelegramHelper::addNewMessage) + } + private fun requestMessage(chatId: Long, messageId: Long, onComplete: (TdApi.Message) -> Unit) { client?.send(TdApi.GetMessage(chatId, messageId)) { obj -> when (obj.constructor) { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt index 9487d8ab08..0b0d570d39 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt @@ -117,6 +117,8 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene } } }) + telegramHelper.listener = this + telegramHelper.requestAuthorizationState() if (osmandAidlHelper.isOsmandBound() && !osmandAidlHelper.isOsmandConnected()) { osmandAidlHelper.connectOsmand() @@ -138,9 +140,8 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene super.onResume() paused = false - telegramHelper.listener = this - if (!telegramHelper.isInit()) { - telegramHelper.init() + if (telegramHelper.listener != this) { + telegramHelper.listener = this } app.locationProvider.checkIfLastKnownLocationIsValid() @@ -167,6 +168,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene override fun onStop() { super.onStop() settings.save() + app.messagesDbHelper.save() } override fun onDestroy() { @@ -253,7 +255,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene if (telegramHelper.getTelegramAuthorizationState() != TelegramAuthorizationState.CLOSED) { telegramHelper.logout() } - telegramHelper.init() + // FIXME: update UI } private fun logoutTelegram(silent: Boolean = false) {