Merge pull request #5986 from osmandapp/show_by_default

Show all chats on map by default
This commit is contained in:
vshcherb 2018-09-05 13:34:28 +02:00 committed by GitHub
commit 18171c6a3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 40 deletions

View file

@ -74,9 +74,6 @@ class TelegramApplication : Application(), OsmandHelperListener {
if (settings.hasAnyChatToShareLocation() && AndroidUtils.isLocationPermissionAvailable(this)) { if (settings.hasAnyChatToShareLocation() && AndroidUtils.isLocationPermissionAvailable(this)) {
shareLocationHelper.startSharingLocation() shareLocationHelper.startSharingLocation()
} }
if (settings.hasAnyChatToShowOnMap()) {
showLocationHelper.startShowingLocation()
}
} }
fun cleanupResources() { fun cleanupResources() {

View file

@ -34,7 +34,7 @@ private const val LOC_HISTORY_DEFAULT_INDEX = 2
private const val SETTINGS_NAME = "osmand_telegram_settings" private const val SETTINGS_NAME = "osmand_telegram_settings"
private const val SHARE_LOCATION_CHATS_KEY = "share_location_chats" 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 METRICS_CONSTANTS_KEY = "metrics_constants"
private const val SPEED_CONSTANTS_KEY = "speed_constants" private const val SPEED_CONSTANTS_KEY = "speed_constants"
@ -57,7 +57,7 @@ class TelegramSettings(private val app: TelegramApplication) {
private var chatShareLocStartSec = mutableMapOf<Long, Long>() private var chatShareLocStartSec = mutableMapOf<Long, Long>()
private var shareLocationChats: Set<Long> = emptySet() private var shareLocationChats: Set<Long> = emptySet()
private var showOnMapChats: Set<Long> = emptySet() private var hiddenOnMapChats: Set<Long> = emptySet()
var metricsConstants = MetricsConstants.KILOMETERS_AND_METERS var metricsConstants = MetricsConstants.KILOMETERS_AND_METERS
var speedConstants = SpeedConstants.KILOMETERS_PER_HOUR var speedConstants = SpeedConstants.KILOMETERS_PER_HOUR
@ -82,18 +82,18 @@ class TelegramSettings(private val app: TelegramApplication) {
fun isSharingLocationToChat(chatId: Long) = shareLocationChats.contains(chatId) 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<Long>) { fun removeNonexistingChats(presentChatIds: List<Long>) {
val shareLocationChats = shareLocationChats.toMutableList() val shareLocationChats = shareLocationChats.toMutableList()
shareLocationChats.intersect(presentChatIds) shareLocationChats.intersect(presentChatIds)
this.shareLocationChats = shareLocationChats.toHashSet() this.shareLocationChats = shareLocationChats.toHashSet()
val showOnMapChats = showOnMapChats.toMutableList() val hiddenChats = hiddenOnMapChats.toMutableList()
showOnMapChats.intersect(presentChatIds) hiddenChats.intersect(presentChatIds)
this.showOnMapChats = showOnMapChats.toHashSet() hiddenOnMapChats = hiddenChats.toHashSet()
chatLivePeriods = chatLivePeriods.filter { (key, _) -> chatLivePeriods = chatLivePeriods.filter { (key, _) ->
presentChatIds.contains(key) presentChatIds.contains(key)
@ -157,25 +157,21 @@ class TelegramSettings(private val app: TelegramApplication) {
this.chatShareLocStartSec.clear() this.chatShareLocStartSec.clear()
} }
fun clearShowOnMapChats() {
showOnMapChats = emptySet()
}
fun showChatOnMap(chatId: Long, show: Boolean) { fun showChatOnMap(chatId: Long, show: Boolean) {
val showOnMapChats = showOnMapChats.toMutableList() val hiddenChats = hiddenOnMapChats.toMutableList()
if (show) { if (show) {
showOnMapChats.add(chatId) hiddenChats.remove(chatId)
} else { } else {
showOnMapChats.remove(chatId) hiddenChats.add(chatId)
} }
this.showOnMapChats = showOnMapChats.toHashSet() hiddenOnMapChats = hiddenChats.toHashSet()
} }
fun getShareLocationChats() = ArrayList(shareLocationChats) fun getShareLocationChats() = ArrayList(shareLocationChats)
fun getShowOnMapChats() = ArrayList(showOnMapChats) fun getShowOnMapChats() = getLiveNowChats().minus(hiddenOnMapChats)
fun getShowOnMapChatsCount() = showOnMapChats.size fun getShowOnMapChatsCount() = getShowOnMapChats().size
fun clear() { fun clear() {
stopSharingLocationToChats() stopSharingLocationToChats()
@ -199,12 +195,12 @@ class TelegramSettings(private val app: TelegramApplication) {
} }
edit.putStringSet(SHARE_LOCATION_CHATS_KEY, shareLocationChatsSet) edit.putStringSet(SHARE_LOCATION_CHATS_KEY, shareLocationChatsSet)
val showOnMapChatsSet = mutableSetOf<String>() val hiddenChatsSet = mutableSetOf<String>()
val showOnMapChats = ArrayList(showOnMapChats) val hiddenChats = ArrayList(hiddenOnMapChats)
for (chatId in showOnMapChats) { for (chatId in hiddenChats) {
showOnMapChatsSet.add(chatId.toString()) 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(METRICS_CONSTANTS_KEY, metricsConstants.name)
edit.putString(SPEED_CONSTANTS_KEY, speedConstants.name) edit.putString(SPEED_CONSTANTS_KEY, speedConstants.name)
@ -230,12 +226,12 @@ class TelegramSettings(private val app: TelegramApplication) {
} }
this.shareLocationChats = shareLocationChats this.shareLocationChats = shareLocationChats
val showOnMapChats = mutableSetOf<Long>() val hiddenChats = mutableSetOf<Long>()
val showOnMapChatsSet = prefs.getStringSet(SHOW_ON_MAP_CHATS_KEY, mutableSetOf()) val hiddenChatsSet = prefs.getStringSet(HIDDEN_ON_MAP_CHATS_KEY, mutableSetOf())
for (chatId in showOnMapChatsSet) { for (chatId in hiddenChatsSet) {
showOnMapChats.add(chatId.toLong()) hiddenChats.add(chatId.toLong())
} }
this.showOnMapChats = showOnMapChats hiddenOnMapChats = hiddenChats
metricsConstants = MetricsConstants.valueOf( metricsConstants = MetricsConstants.valueOf(
prefs.getString(METRICS_CONSTANTS_KEY, MetricsConstants.KILOMETERS_AND_METERS.name) 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() { private fun updatePrefs() {
val prefs = app.getSharedPreferences(SETTINGS_NAME, Context.MODE_PRIVATE) val prefs = app.getSharedPreferences(SETTINGS_NAME, Context.MODE_PRIVATE)
val idsInUse = prefs.getBoolean(TITLES_REPLACED_WITH_IDS, false) val idsInUse = prefs.getBoolean(TITLES_REPLACED_WITH_IDS, false)
@ -265,7 +263,6 @@ class TelegramSettings(private val app: TelegramApplication) {
val edit = prefs.edit() val edit = prefs.edit()
edit.putStringSet(SHARE_LOCATION_CHATS_KEY, emptySet()) edit.putStringSet(SHARE_LOCATION_CHATS_KEY, emptySet())
edit.putStringSet(SHOW_ON_MAP_CHATS_KEY, emptySet())
edit.putBoolean(TITLES_REPLACED_WITH_IDS, true) edit.putBoolean(TITLES_REPLACED_WITH_IDS, true)
edit.apply() edit.apply()

View file

@ -2,18 +2,18 @@ package net.osmand.telegram.helpers
import android.content.Intent import android.content.Intent
import android.graphics.Color import android.graphics.Color
import android.net.Uri
import android.os.AsyncTask
import android.text.TextUtils import android.text.TextUtils
import net.osmand.aidl.map.ALatLon import net.osmand.aidl.map.ALatLon
import net.osmand.aidl.maplayer.point.AMapPoint import net.osmand.aidl.maplayer.point.AMapPoint
import net.osmand.telegram.R
import net.osmand.telegram.TelegramApplication import net.osmand.telegram.TelegramApplication
import net.osmand.telegram.helpers.TelegramHelper.MessageOsmAndBotLocation import net.osmand.telegram.helpers.TelegramHelper.MessageOsmAndBotLocation
import net.osmand.telegram.helpers.TelegramUiHelper.ListItem import net.osmand.telegram.helpers.TelegramUiHelper.ListItem
import net.osmand.telegram.utils.AndroidUtils import net.osmand.telegram.utils.AndroidUtils
import org.drinkless.td.libcore.telegram.TdApi import org.drinkless.td.libcore.telegram.TdApi
import java.io.File import java.io.File
import android.net.Uri
import android.os.AsyncTask
import net.osmand.telegram.R
import java.util.concurrent.Executors import java.util.concurrent.Executors
class ShowLocationHelper(private val app: TelegramApplication) { class ShowLocationHelper(private val app: TelegramApplication) {
@ -31,6 +31,8 @@ class ShowLocationHelper(private val app: TelegramApplication) {
var showingLocation: Boolean = false var showingLocation: Boolean = false
private set private set
private var forcedStop: Boolean = false
fun setupMapLayer() { fun setupMapLayer() {
execOsmandApi { execOsmandApi {
osmandAidlHelper.addMapLayer(MAP_LAYER_ID, "Telegram", 5.5f, null) osmandAidlHelper.addMapLayer(MAP_LAYER_ID, "Telegram", 5.5f, null)
@ -147,7 +149,7 @@ class ShowLocationHelper(private val app: TelegramApplication) {
} }
fun startShowingLocation() { fun startShowingLocation() {
if (!showingLocation) { if (!showingLocation && !forcedStop) {
showingLocation = if (isUseOsmandCallback()) { showingLocation = if (isUseOsmandCallback()) {
osmandAidlHelper.registerForUpdates() osmandAidlHelper.registerForUpdates()
} else { } else {
@ -157,7 +159,8 @@ class ShowLocationHelper(private val app: TelegramApplication) {
} }
} }
fun stopShowingLocation() { fun stopShowingLocation(force: Boolean = false) {
forcedStop = force
if (showingLocation) { if (showingLocation) {
showingLocation = false showingLocation = false
if (isUseOsmandCallback()) { if (isUseOsmandCallback()) {

View file

@ -269,6 +269,9 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
override fun onReceiveChatLocationMessages(chatId: Long, vararg messages: TdApi.Message) { override fun onReceiveChatLocationMessages(chatId: Long, vararg messages: TdApi.Message) {
addGrayPhoto(chatId) addGrayPhoto(chatId)
if (!app.showLocationHelper.showingLocation && settings.hasAnyChatToShowOnMap()) {
app.showLocationHelper.startShowingLocation()
}
} }
override fun onDeleteChatLocationMessages(chatId: Long, messages: List<TdApi.Message>) {} override fun onDeleteChatLocationMessages(chatId: Long, messages: List<TdApi.Message>) {}
@ -294,15 +297,14 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
telegramHelper.stopSendingLiveLocationMessages() telegramHelper.stopSendingLiveLocationMessages()
} }
fun stopShowingChatsOnMap() { fun stopShowingChatsOnMap(forceStop: Boolean) {
settings.getShowOnMapChats().forEach { app.showLocationHelper.hideChatMessages(it) } settings.getShowOnMapChats().forEach { app.showLocationHelper.hideChatMessages(it) }
settings.clearShowOnMapChats() app.showLocationHelper.stopShowingLocation(forceStop)
app.showLocationHelper.stopShowingLocation()
} }
private fun closeApp() { private fun closeApp() {
stopSharingLocation() stopSharingLocation()
stopShowingChatsOnMap() stopShowingChatsOnMap(true)
finish() finish()
android.os.Process.killProcess(android.os.Process.myPid()) android.os.Process.killProcess(android.os.Process.myPid())
} }