From d6cd0df2ea43ba3f5c686da3221f8bda5757ce5c Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Wed, 5 Sep 2018 12:58:32 +0300 Subject: [PATCH] Show all chats on map by default --- .../osmand/telegram/TelegramApplication.kt | 3 -- .../net/osmand/telegram/TelegramSettings.kt | 53 +++++++++---------- .../telegram/helpers/ShowLocationHelper.kt | 13 +++-- .../net/osmand/telegram/ui/MainActivity.kt | 10 ++-- 4 files changed, 39 insertions(+), 40 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt index 492b906feb..36cf9da896 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt @@ -74,9 +74,6 @@ class TelegramApplication : Application(), OsmandHelperListener { if (settings.hasAnyChatToShareLocation() && AndroidUtils.isLocationPermissionAvailable(this)) { shareLocationHelper.startSharingLocation() } - if (settings.hasAnyChatToShowOnMap()) { - showLocationHelper.startShowingLocation() - } } fun cleanupResources() { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index d0236e55ae..36c72dfc82 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -34,7 +34,7 @@ private const val LOC_HISTORY_DEFAULT_INDEX = 2 private const val SETTINGS_NAME = "osmand_telegram_settings" private const val SHARE_LOCATION_CHATS_KEY = "share_location_chats" -private const val SHOW_ON_MAP_CHATS_KEY = "show_on_map_chats" +private const val HIDDEN_ON_MAP_CHATS_KEY = "hidden_on_map_chats" private const val METRICS_CONSTANTS_KEY = "metrics_constants" private const val SPEED_CONSTANTS_KEY = "speed_constants" @@ -57,7 +57,7 @@ class TelegramSettings(private val app: TelegramApplication) { private var chatShareLocStartSec = mutableMapOf() private var shareLocationChats: Set = emptySet() - private var showOnMapChats: Set = emptySet() + private var hiddenOnMapChats: Set = emptySet() var metricsConstants = MetricsConstants.KILOMETERS_AND_METERS var speedConstants = SpeedConstants.KILOMETERS_PER_HOUR @@ -82,18 +82,18 @@ class TelegramSettings(private val app: TelegramApplication) { fun isSharingLocationToChat(chatId: Long) = shareLocationChats.contains(chatId) - fun hasAnyChatToShowOnMap() = showOnMapChats.isNotEmpty() + fun hasAnyChatToShowOnMap() = !hiddenOnMapChats.containsAll(getLiveNowChats()) - fun isShowingChatOnMap(chatId: Long) = showOnMapChats.contains(chatId) + fun isShowingChatOnMap(chatId: Long) = !hiddenOnMapChats.contains(chatId) fun removeNonexistingChats(presentChatIds: List) { val shareLocationChats = shareLocationChats.toMutableList() shareLocationChats.intersect(presentChatIds) this.shareLocationChats = shareLocationChats.toHashSet() - val showOnMapChats = showOnMapChats.toMutableList() - showOnMapChats.intersect(presentChatIds) - this.showOnMapChats = showOnMapChats.toHashSet() + val hiddenChats = hiddenOnMapChats.toMutableList() + hiddenChats.intersect(presentChatIds) + hiddenOnMapChats = hiddenChats.toHashSet() chatLivePeriods = chatLivePeriods.filter { (key, _) -> presentChatIds.contains(key) @@ -157,25 +157,21 @@ class TelegramSettings(private val app: TelegramApplication) { this.chatShareLocStartSec.clear() } - fun clearShowOnMapChats() { - showOnMapChats = emptySet() - } - fun showChatOnMap(chatId: Long, show: Boolean) { - val showOnMapChats = showOnMapChats.toMutableList() + val hiddenChats = hiddenOnMapChats.toMutableList() if (show) { - showOnMapChats.add(chatId) + hiddenChats.remove(chatId) } else { - showOnMapChats.remove(chatId) + hiddenChats.add(chatId) } - this.showOnMapChats = showOnMapChats.toHashSet() + hiddenOnMapChats = hiddenChats.toHashSet() } fun getShareLocationChats() = ArrayList(shareLocationChats) - fun getShowOnMapChats() = ArrayList(showOnMapChats) + fun getShowOnMapChats() = getLiveNowChats().minus(hiddenOnMapChats) - fun getShowOnMapChatsCount() = showOnMapChats.size + fun getShowOnMapChatsCount() = getShowOnMapChats().size fun clear() { stopSharingLocationToChats() @@ -199,12 +195,12 @@ class TelegramSettings(private val app: TelegramApplication) { } edit.putStringSet(SHARE_LOCATION_CHATS_KEY, shareLocationChatsSet) - val showOnMapChatsSet = mutableSetOf() - val showOnMapChats = ArrayList(showOnMapChats) - for (chatId in showOnMapChats) { - showOnMapChatsSet.add(chatId.toString()) + val hiddenChatsSet = mutableSetOf() + val hiddenChats = ArrayList(hiddenOnMapChats) + for (chatId in hiddenChats) { + hiddenChatsSet.add(chatId.toString()) } - edit.putStringSet(SHOW_ON_MAP_CHATS_KEY, showOnMapChatsSet) + edit.putStringSet(HIDDEN_ON_MAP_CHATS_KEY, hiddenChatsSet) edit.putString(METRICS_CONSTANTS_KEY, metricsConstants.name) edit.putString(SPEED_CONSTANTS_KEY, speedConstants.name) @@ -230,12 +226,12 @@ class TelegramSettings(private val app: TelegramApplication) { } this.shareLocationChats = shareLocationChats - val showOnMapChats = mutableSetOf() - val showOnMapChatsSet = prefs.getStringSet(SHOW_ON_MAP_CHATS_KEY, mutableSetOf()) - for (chatId in showOnMapChatsSet) { - showOnMapChats.add(chatId.toLong()) + val hiddenChats = mutableSetOf() + val hiddenChatsSet = prefs.getStringSet(HIDDEN_ON_MAP_CHATS_KEY, mutableSetOf()) + for (chatId in hiddenChatsSet) { + hiddenChats.add(chatId.toLong()) } - this.showOnMapChats = showOnMapChats + hiddenOnMapChats = hiddenChats metricsConstants = MetricsConstants.valueOf( prefs.getString(METRICS_CONSTANTS_KEY, MetricsConstants.KILOMETERS_AND_METERS.name) @@ -258,6 +254,8 @@ class TelegramSettings(private val app: TelegramApplication) { ) } + private fun getLiveNowChats() = app.telegramHelper.getMessagesByChatIds(locHistoryTime).keys + private fun updatePrefs() { val prefs = app.getSharedPreferences(SETTINGS_NAME, Context.MODE_PRIVATE) val idsInUse = prefs.getBoolean(TITLES_REPLACED_WITH_IDS, false) @@ -265,7 +263,6 @@ class TelegramSettings(private val app: TelegramApplication) { val edit = prefs.edit() edit.putStringSet(SHARE_LOCATION_CHATS_KEY, emptySet()) - edit.putStringSet(SHOW_ON_MAP_CHATS_KEY, emptySet()) edit.putBoolean(TITLES_REPLACED_WITH_IDS, true) edit.apply() diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt index 1e5ebe5ab6..e08b9f33d0 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt @@ -2,18 +2,18 @@ package net.osmand.telegram.helpers import android.content.Intent import android.graphics.Color +import android.net.Uri +import android.os.AsyncTask import android.text.TextUtils import net.osmand.aidl.map.ALatLon import net.osmand.aidl.maplayer.point.AMapPoint +import net.osmand.telegram.R import net.osmand.telegram.TelegramApplication import net.osmand.telegram.helpers.TelegramHelper.MessageOsmAndBotLocation import net.osmand.telegram.helpers.TelegramUiHelper.ListItem import net.osmand.telegram.utils.AndroidUtils import org.drinkless.td.libcore.telegram.TdApi import java.io.File -import android.net.Uri -import android.os.AsyncTask -import net.osmand.telegram.R import java.util.concurrent.Executors class ShowLocationHelper(private val app: TelegramApplication) { @@ -31,6 +31,8 @@ class ShowLocationHelper(private val app: TelegramApplication) { var showingLocation: Boolean = false private set + private var forcedStop: Boolean = false + fun setupMapLayer() { execOsmandApi { osmandAidlHelper.addMapLayer(MAP_LAYER_ID, "Telegram", 5.5f, null) @@ -147,7 +149,7 @@ class ShowLocationHelper(private val app: TelegramApplication) { } fun startShowingLocation() { - if (!showingLocation) { + if (!showingLocation && !forcedStop) { showingLocation = if (isUseOsmandCallback()) { osmandAidlHelper.registerForUpdates() } else { @@ -157,7 +159,8 @@ class ShowLocationHelper(private val app: TelegramApplication) { } } - fun stopShowingLocation() { + fun stopShowingLocation(force: Boolean = false) { + forcedStop = force if (showingLocation) { showingLocation = false if (isUseOsmandCallback()) { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt index 721f428776..14b12b5253 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt @@ -269,6 +269,9 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene override fun onReceiveChatLocationMessages(chatId: Long, vararg messages: TdApi.Message) { addGrayPhoto(chatId) + if (!app.showLocationHelper.showingLocation && settings.hasAnyChatToShowOnMap()) { + app.showLocationHelper.startShowingLocation() + } } override fun onDeleteChatLocationMessages(chatId: Long, messages: List) {} @@ -294,15 +297,14 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene telegramHelper.stopSendingLiveLocationMessages() } - fun stopShowingChatsOnMap() { + fun stopShowingChatsOnMap(forceStop: Boolean) { settings.getShowOnMapChats().forEach { app.showLocationHelper.hideChatMessages(it) } - settings.clearShowOnMapChats() - app.showLocationHelper.stopShowingLocation() + app.showLocationHelper.stopShowingLocation(forceStop) } private fun closeApp() { stopSharingLocation() - stopShowingChatsOnMap() + stopShowingChatsOnMap(true) finish() android.os.Process.killProcess(android.os.Process.myPid()) }