Save live messages to DB

This commit is contained in:
Alex Sytnyk 2018-08-13 17:35:49 +03:00
parent cc256f6a35
commit 1d5360f58f
4 changed files with 138 additions and 9 deletions

View file

@ -7,11 +7,8 @@ import android.net.ConnectivityManager
import android.net.NetworkInfo import android.net.NetworkInfo
import android.os.Build import android.os.Build
import android.os.Handler 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.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.notifications.NotificationHelper
import net.osmand.telegram.utils.AndroidUtils import net.osmand.telegram.utils.AndroidUtils
import net.osmand.telegram.utils.UiUtils import net.osmand.telegram.utils.UiUtils
@ -26,6 +23,7 @@ class TelegramApplication : Application(), OsmandHelperListener {
lateinit var notificationHelper: NotificationHelper private set lateinit var notificationHelper: NotificationHelper private set
lateinit var osmandAidlHelper: OsmandAidlHelper private set lateinit var osmandAidlHelper: OsmandAidlHelper private set
lateinit var locationProvider: TelegramLocationProvider private set lateinit var locationProvider: TelegramLocationProvider private set
lateinit var messagesDbHelper: MessagesDbHelper private set
var telegramService: TelegramService? = null var telegramService: TelegramService? = null
@ -37,6 +35,7 @@ class TelegramApplication : Application(), OsmandHelperListener {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
telegramHelper.appDir = filesDir.absolutePath telegramHelper.appDir = filesDir.absolutePath
telegramHelper.init()
settings = TelegramSettings(this) settings = TelegramSettings(this)
telegramHelper.messageActiveTimeSec = settings.locHistoryTime telegramHelper.messageActiveTimeSec = settings.locHistoryTime
@ -61,6 +60,7 @@ class TelegramApplication : Application(), OsmandHelperListener {
showLocationHelper = ShowLocationHelper(this) showLocationHelper = ShowLocationHelper(this)
notificationHelper = NotificationHelper(this) notificationHelper = NotificationHelper(this)
locationProvider = TelegramLocationProvider(this) locationProvider = TelegramLocationProvider(this)
messagesDbHelper = MessagesDbHelper(this)
if (settings.hasAnyChatToShareLocation() && AndroidUtils.isLocationPermissionAvailable(this)) { if (settings.hasAnyChatToShareLocation() && AndroidUtils.isLocationPermissionAvailable(this)) {
shareLocationHelper.startSharingLocation() shareLocationHelper.startSharingLocation()

View file

@ -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<Message>()
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<TdApi.Message>) {
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<Message>) {
messages.forEach {
writableDatabase?.execSQL(MESSAGES_TABLE_INSERT, arrayOf(it.chatId, it.messageId))
}
}
internal fun getMessages(): Set<Message> {
val res = HashSet<Message>()
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)
}

View file

@ -292,13 +292,20 @@ class TelegramHelper private constructor() {
return if (libraryLoaded) { return if (libraryLoaded) {
// create client // create client
client = Client.create(UpdatesHandler(), null, null) client = Client.create(UpdatesHandler(), null, null)
client!!.send(TdApi.GetAuthorizationState(), defaultHandler)
true true
} else { } else {
false false
} }
} }
fun requestAuthorizationState() {
client?.send(TdApi.GetAuthorizationState()) { obj ->
if (obj is TdApi.AuthorizationState) {
onAuthorizationStateUpdated(obj)
}
}
}
fun isInit() = client != null && haveAuthorization fun isInit() = client != null && haveAuthorization
fun getUserPhotoPath(user: TdApi.User): String? { 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) { private fun requestMessage(chatId: Long, messageId: Long, onComplete: (TdApi.Message) -> Unit) {
client?.send(TdApi.GetMessage(chatId, messageId)) { obj -> client?.send(TdApi.GetMessage(chatId, messageId)) { obj ->
when (obj.constructor) { when (obj.constructor) {

View file

@ -117,6 +117,8 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
} }
} }
}) })
telegramHelper.listener = this
telegramHelper.requestAuthorizationState()
if (osmandAidlHelper.isOsmandBound() && !osmandAidlHelper.isOsmandConnected()) { if (osmandAidlHelper.isOsmandBound() && !osmandAidlHelper.isOsmandConnected()) {
osmandAidlHelper.connectOsmand() osmandAidlHelper.connectOsmand()
@ -138,9 +140,8 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
super.onResume() super.onResume()
paused = false paused = false
telegramHelper.listener = this if (telegramHelper.listener != this) {
if (!telegramHelper.isInit()) { telegramHelper.listener = this
telegramHelper.init()
} }
app.locationProvider.checkIfLastKnownLocationIsValid() app.locationProvider.checkIfLastKnownLocationIsValid()
@ -167,6 +168,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
override fun onStop() { override fun onStop() {
super.onStop() super.onStop()
settings.save() settings.save()
app.messagesDbHelper.save()
} }
override fun onDestroy() { override fun onDestroy() {
@ -253,7 +255,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
if (telegramHelper.getTelegramAuthorizationState() != TelegramAuthorizationState.CLOSED) { if (telegramHelper.getTelegramAuthorizationState() != TelegramAuthorizationState.CLOSED) {
telegramHelper.logout() telegramHelper.logout()
} }
telegramHelper.init() // FIXME: update UI
} }
private fun logoutTelegram(silent: Boolean = false) { private fun logoutTelegram(silent: Boolean = false) {