From eedcf67d35d57769aad466b8893835aa0aef3744 Mon Sep 17 00:00:00 2001 From: Chumva Date: Fri, 25 Jan 2019 19:02:43 +0200 Subject: [PATCH] add new status for incoming messages --- .../net/osmand/telegram/TelegramService.kt | 3 +- .../telegram/helpers/LocationMessages.kt | 53 +++++++------------ .../telegram/helpers/ShareLocationHelper.kt | 2 +- .../net/osmand/telegram/ui/MainActivity.kt | 3 +- .../telegram/utils/OsmandLocationUtils.kt | 2 +- 5 files changed, 22 insertions(+), 41 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt index e6c2ebc7e4..187ad7ba81 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt @@ -122,7 +122,6 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis override fun onDestroy() { super.onDestroy() val app = app() - app.locationMessages.saveMessages() app.telegramHelper.stopLiveMessagesUpdates() app.telegramHelper.removeIncomingMessagesListener(this) app.telegramHelper.removeOutgoingMessagesListener(this) @@ -278,7 +277,7 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis override fun onReceiveChatLocationMessages(chatId: Long, vararg messages: TdApi.Message) { app().showLocationHelper.startShowMessagesTask(chatId, *messages) messages.forEach { - val locationMessage = OsmandLocationUtils.parseMessage(it, app().telegramHelper, LocationMessages.LocationMessage.STATUS_SENT) + val locationMessage = OsmandLocationUtils.parseMessage(it, app().telegramHelper, LocationMessages.LocationMessage.STATUS_INCOMING) if (locationMessage != null) { app().locationMessages.addLocationMessage(locationMessage) } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/LocationMessages.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/LocationMessages.kt index bf49ff9725..0e4fa5507a 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/LocationMessages.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/LocationMessages.kt @@ -4,15 +4,11 @@ import android.content.Context import android.database.Cursor import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper -import net.osmand.PlatformUtil import net.osmand.telegram.TelegramApplication -import org.apache.commons.logging.Log -import java.util.* +import kotlin.collections.ArrayList class LocationMessages(val app: TelegramApplication) { - private val log: Log = PlatformUtil.getLog(LocationMessages::class.java) - private val locationMessages = ArrayList() private val sqliteHelper: SQLiteHelper @@ -28,7 +24,7 @@ class LocationMessages(val app: TelegramApplication) { fun getPreparedToShareMessages(): List { val currentUserId = app.telegramHelper.getCurrentUserId() - return locationMessages.filter { it.userId == currentUserId && it.status == LocationMessage.STATUS_PREPARING }.sortedBy { it.date } + return locationMessages.filter { it.userId == currentUserId && it.status == LocationMessage.STATUS_PREPARED }.sortedBy { it.date } } fun getOutgoingMessagesToChat(chatId: Long): List { @@ -47,28 +43,12 @@ class LocationMessages(val app: TelegramApplication) { } fun getIncomingMessages(): List { - val currentUserId = app.telegramHelper.getCurrentUserId() - return locationMessages.filter { it.userId != currentUserId }.sortedBy { it.date } + return locationMessages.filter { it.status != LocationMessage.STATUS_INCOMING }.sortedBy { it.date } } fun addLocationMessage(locationMessage: LocationMessage) { - log.debug("addLocationMessage $locationMessage") - synchronized(locationMessages) { - locationMessages.add(locationMessage) - } - } - - fun removeMessage(locationMessage: LocationMessage) { - synchronized(locationMessages) { - locationMessages.remove(locationMessage) - } - } - - fun saveMessages() { - clearMessages() - synchronized(locationMessages) { - sqliteHelper.addLocationMessages(locationMessages) - } + locationMessages.add(locationMessage) + sqliteHelper.addLocationMessage(locationMessage) } fun clearMessages() { @@ -92,9 +72,7 @@ class LocationMessages(val app: TelegramApplication) { private fun readMessages() { val messages = sqliteHelper.getLocationMessages() - synchronized(locationMessages) { - locationMessages.addAll(messages) - } + locationMessages.addAll(messages) } private class SQLiteHelper(context: Context) : @@ -117,8 +95,14 @@ class LocationMessages(val app: TelegramApplication) { } } - internal fun getLocationMessages(): Set { - val res = HashSet() + internal fun addLocationMessage(locationMessage: LocationMessage) { + writableDatabase?.execSQL(TRACKS_TABLE_INSERT, + arrayOf(locationMessage.userId, locationMessage.chatId, locationMessage.lat, locationMessage.lon, locationMessage.altitude, locationMessage.speed, + locationMessage.hdop, locationMessage.bearing, locationMessage.date, locationMessage.type, locationMessage.status, locationMessage.messageId)) + } + + internal fun getLocationMessages(): List { + val res = ArrayList() readableDatabase?.rawQuery(TRACKS_TABLE_SELECT, null)?.apply { if (moveToFirst()) { do { @@ -166,10 +150,8 @@ class LocationMessages(val app: TelegramApplication) { private const val TRACK_COL_SPEED = "speed" private const val TRACK_COL_HDOP = "hdop" private const val TRACK_COL_BEARING = "bearing" - private const val TRACK_COL_TYPE = - "type" // 0 = user map message, 1 = user text message, 2 = bot map message, 3 = bot text message - private const val TRACK_COL_MESSAGE_STATUS = - "status" // 0 = preparing , 1 = pending, 2 = sent, 3 = error + private const val TRACK_COL_TYPE = "type" // 0 = user map message, 1 = user text message, 2 = bot map message, 3 = bot text message + private const val TRACK_COL_MESSAGE_STATUS = "status" // 0 = preparing , 1 = pending, 2 = sent, 3 = error private const val TRACK_COL_MESSAGE_ID = "message_id" private const val TRACK_DATE_INDEX = "date_index" @@ -206,10 +188,11 @@ class LocationMessages(val app: TelegramApplication) { companion object { - const val STATUS_PREPARING = 0 + const val STATUS_PREPARED = 0 const val STATUS_PENDING = 1 const val STATUS_SENT = 2 const val STATUS_ERROR = 3 + const val STATUS_INCOMING = 4 const val TYPE_USER_MAP = 0 const val TYPE_USER_TEXT = 1 diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt index f7691710f6..f39f7aeae2 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt @@ -154,7 +154,7 @@ class ShareLocationHelper(private val app: TelegramApplication) { chatsShareInfo.values.forEach { shareInfo -> types.forEach { val message = LocationMessage(userId, shareInfo.chatId, latitude, longitude, location.altitude, location.speed.toDouble(), - location.accuracy.toDouble(), location.bearing.toDouble(), location.time, it, LocationMessage.STATUS_PREPARING, shareInfo.currentMapMessageId) + location.accuracy.toDouble(), location.bearing.toDouble(), location.time, it, LocationMessage.STATUS_PREPARED, shareInfo.currentMapMessageId) app.locationMessages.addLocationMessage(message) } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt index 6e62c2576b..674dca3eaf 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt @@ -184,7 +184,6 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene override fun onStop() { super.onStop() settings.save() - app.locationMessages.saveMessages() } override fun onDestroy() { @@ -294,7 +293,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene } if (app.telegramService == null) { messages.forEach { - val locationMessage = OsmandLocationUtils.parseMessage(it, telegramHelper, LocationMessages.LocationMessage.STATUS_SENT) + val locationMessage = OsmandLocationUtils.parseMessage(it, telegramHelper, LocationMessages.LocationMessage.STATUS_INCOMING) if (locationMessage != null) { app.locationMessages.addLocationMessage(locationMessage) } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandLocationUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandLocationUtils.kt index ef1cf80f00..9a7fd57edd 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandLocationUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandLocationUtils.kt @@ -106,7 +106,7 @@ object OsmandLocationUtils { } if (parsedMessageContent != null) { - locationMessage = LocationMessages.LocationMessage(message.senderUserId, message.chatId, parsedMessageContent.lat, + locationMessage = LocationMessages.LocationMessage(helper.getSenderMessageId(message), message.chatId, parsedMessageContent.lat, parsedMessageContent.lon, parsedMessageContent.altitude, parsedMessageContent.speed, parsedMessageContent.hdop, parsedMessageContent.bearing, parsedMessageContent.lastUpdated * 1000L, messageType, status, message.id) }