Merge pull request #5846 from osmandapp/messages_db
Save live messages to DB
This commit is contained in:
commit
136840af68
4 changed files with 140 additions and 9 deletions
|
@ -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()
|
||||
|
|
|
@ -0,0 +1,118 @@
|
|||
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 saveMessages() {
|
||||
sqliteHelper.clearMessages()
|
||||
synchronized(messages) {
|
||||
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)
|
||||
}
|
|
@ -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) {
|
||||
|
|
|
@ -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.saveMessages()
|
||||
}
|
||||
|
||||
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) {
|
||||
|
|
Loading…
Reference in a new issue