OsmAnd/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt

176 lines
5.8 KiB
Kotlin
Raw Normal View History

package net.osmand.telegram
import android.content.Context
2018-07-13 14:53:58 +02:00
import net.osmand.telegram.helpers.TelegramHelper
import net.osmand.telegram.utils.OsmandFormatter.MetricsConstants
import net.osmand.telegram.utils.OsmandFormatter.SpeedConstants
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"
2018-06-11 18:57:33 +02:00
private const val METRICS_CONSTANTS_KEY = "metrics_constants"
private const val SPEED_CONSTANTS_KEY = "speed_constants"
private const val SEND_MY_LOCATION_INTERVAL_KEY = "send_my_location_interval"
private const val SEND_MY_LOCATION_INTERVAL_DEFAULT = 5L * 1000 // 5 seconds
2018-06-11 18:57:33 +02:00
private const val USER_LOCATION_EXPIRE_TIME_KEY = "user_location_expire_time"
private const val USER_LOCATION_EXPIRE_TIME_DEFAULT = 15L * 60 * 1000 // 15 minutes
2018-07-13 15:28:37 +02:00
2018-07-13 14:53:58 +02:00
private const val DEFAULT_VISIBLE_TIME_SECONDS = 60 * 60L // 1 hour
2018-07-13 13:32:11 +02:00
private const val TITLES_REPLACED_WITH_IDS = "changed_to_chat_id"
class TelegramSettings(private val app: TelegramApplication) {
2018-06-11 18:57:33 +02:00
2018-07-13 16:02:49 +02:00
var chatLivePeriods = mutableMapOf<Long, Long>()
2018-07-13 16:01:28 +02:00
2018-07-13 13:02:59 +02:00
private var shareLocationChats: Set<Long> = emptySet()
private var showOnMapChats: Set<Long> = emptySet()
2018-06-11 18:57:33 +02:00
var metricsConstants = MetricsConstants.KILOMETERS_AND_METERS
var speedConstants = SpeedConstants.KILOMETERS_PER_HOUR
2018-06-13 16:18:54 +02:00
var sendMyLocationInterval = SEND_MY_LOCATION_INTERVAL_DEFAULT
var userLocationExpireTime = USER_LOCATION_EXPIRE_TIME_DEFAULT
2018-06-11 18:57:33 +02:00
init {
2018-07-13 13:32:11 +02:00
updatePrefs()
2018-06-11 18:57:33 +02:00
read()
}
2018-07-04 15:00:51 +02:00
fun hasAnyChatToShareLocation() = shareLocationChats.isNotEmpty()
2018-06-11 18:57:33 +02:00
2018-07-13 13:32:11 +02:00
fun isSharingLocationToChat(chatId: Long) = shareLocationChats.contains(chatId)
2018-06-11 18:57:33 +02:00
2018-07-04 15:00:51 +02:00
fun hasAnyChatToShowOnMap() = showOnMapChats.isNotEmpty()
2018-06-11 18:57:33 +02:00
2018-07-13 13:32:11 +02:00
fun isShowingChatOnMap(chatId: Long) = showOnMapChats.contains(chatId)
2018-06-11 18:57:33 +02:00
2018-07-13 13:02:59 +02:00
fun removeNonexistingChats(presentChatIds: List<Long>) {
2018-06-11 18:57:33 +02:00
val shareLocationChats = shareLocationChats.toMutableList()
2018-07-13 13:02:59 +02:00
shareLocationChats.intersect(presentChatIds)
2018-06-11 18:57:33 +02:00
this.shareLocationChats = shareLocationChats.toHashSet()
val showOnMapChats = showOnMapChats.toMutableList()
2018-07-13 13:02:59 +02:00
showOnMapChats.intersect(presentChatIds)
2018-06-11 18:57:33 +02:00
this.showOnMapChats = showOnMapChats.toHashSet()
2018-07-13 14:53:58 +02:00
2018-07-13 16:02:49 +02:00
chatLivePeriods = chatLivePeriods.filter { (key, _) ->
2018-07-13 15:28:37 +02:00
presentChatIds.contains(key)
}.toMutableMap()
2018-06-11 18:57:33 +02:00
}
2018-07-13 16:08:47 +02:00
fun shareLocationToChat(chatId: Long, share: Boolean, livePeriod: Long = DEFAULT_VISIBLE_TIME_SECONDS) {
2018-06-11 18:57:33 +02:00
val shareLocationChats = shareLocationChats.toMutableList()
if (share) {
2018-07-13 15:28:37 +02:00
val lp: Long = when {
2018-07-13 16:08:47 +02:00
livePeriod < TelegramHelper.MIN_LOCATION_MESSAGE_LIVE_PERIOD_SEC -> TelegramHelper.MIN_LOCATION_MESSAGE_LIVE_PERIOD_SEC.toLong()
livePeriod > TelegramHelper.MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC -> TelegramHelper.MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC.toLong()
else -> livePeriod
2018-07-13 15:28:37 +02:00
}
2018-07-13 16:02:49 +02:00
chatLivePeriods[chatId] = lp
2018-07-13 13:32:11 +02:00
shareLocationChats.add(chatId)
2018-06-11 18:57:33 +02:00
} else {
2018-07-13 13:32:11 +02:00
shareLocationChats.remove(chatId)
2018-07-13 16:02:49 +02:00
chatLivePeriods.remove(chatId)
2018-06-11 18:57:33 +02:00
}
this.shareLocationChats = shareLocationChats.toHashSet()
2018-07-13 14:53:58 +02:00
}
2018-07-13 16:06:05 +02:00
fun getChatLivePeriod(chatId: Long) = chatLivePeriods[chatId]
2018-06-11 18:57:33 +02:00
fun stopSharingLocationToChats() {
this.shareLocationChats = emptySet()
2018-07-13 16:02:49 +02:00
this.chatLivePeriods.clear()
}
2018-07-13 13:32:11 +02:00
fun showChatOnMap(chatId: Long, show: Boolean) {
2018-06-11 18:57:33 +02:00
val showOnMapChats = showOnMapChats.toMutableList()
if (show) {
2018-07-13 13:32:11 +02:00
showOnMapChats.add(chatId)
2018-06-11 18:57:33 +02:00
} else {
2018-07-13 13:32:11 +02:00
showOnMapChats.remove(chatId)
2018-06-11 18:57:33 +02:00
}
this.showOnMapChats = showOnMapChats.toHashSet()
}
fun getShareLocationChats() = ArrayList(shareLocationChats)
2018-07-04 15:00:51 +02:00
2018-06-11 18:57:33 +02:00
fun getShowOnMapChats() = ArrayList(showOnMapChats)
fun getShowOnMapChatsCount() = showOnMapChats.size
fun save() {
val prefs = app.getSharedPreferences(SETTINGS_NAME, Context.MODE_PRIVATE)
val edit = prefs.edit()
val shareLocationChatsSet = mutableSetOf<String>()
val shareLocationChats = ArrayList(shareLocationChats)
2018-07-13 13:02:59 +02:00
for (chatId in shareLocationChats) {
shareLocationChatsSet.add(chatId.toString())
2018-06-11 18:57:33 +02:00
}
edit.putStringSet(SHARE_LOCATION_CHATS_KEY, shareLocationChatsSet)
val showOnMapChatsSet = mutableSetOf<String>()
val showOnMapChats = ArrayList(showOnMapChats)
2018-07-13 13:02:59 +02:00
for (chatId in showOnMapChats) {
showOnMapChatsSet.add(chatId.toString())
2018-06-11 18:57:33 +02:00
}
edit.putStringSet(SHOW_ON_MAP_CHATS_KEY, showOnMapChatsSet)
edit.putString(METRICS_CONSTANTS_KEY, metricsConstants.name)
edit.putString(SPEED_CONSTANTS_KEY, speedConstants.name)
2018-06-13 16:18:54 +02:00
edit.putLong(SEND_MY_LOCATION_INTERVAL_KEY, sendMyLocationInterval)
2018-06-11 18:57:33 +02:00
edit.apply()
}
fun read() {
val prefs = app.getSharedPreferences(SETTINGS_NAME, Context.MODE_PRIVATE)
2018-07-13 13:02:59 +02:00
val shareLocationChats = mutableSetOf<Long>()
2018-06-11 18:57:33 +02:00
val shareLocationChatsSet = prefs.getStringSet(SHARE_LOCATION_CHATS_KEY, mutableSetOf())
2018-07-13 13:27:01 +02:00
for (chatId in shareLocationChatsSet) {
shareLocationChats.add(chatId.toLong())
}
this.shareLocationChats = shareLocationChats
2018-07-13 13:32:11 +02:00
val showOnMapChats = mutableSetOf<Long>()
val showOnMapChatsSet = prefs.getStringSet(SHOW_ON_MAP_CHATS_KEY, mutableSetOf())
2018-07-13 13:27:01 +02:00
for (chatId in showOnMapChatsSet) {
2018-07-13 13:02:59 +02:00
showOnMapChats.add(chatId.toLong())
2018-06-11 18:57:33 +02:00
}
this.showOnMapChats = showOnMapChats
2018-07-04 15:00:51 +02:00
metricsConstants = MetricsConstants.valueOf(
2018-07-13 13:32:11 +02:00
prefs.getString(METRICS_CONSTANTS_KEY, MetricsConstants.KILOMETERS_AND_METERS.name)
2018-07-04 15:00:51 +02:00
)
speedConstants = SpeedConstants.valueOf(
2018-07-13 13:32:11 +02:00
prefs.getString(SPEED_CONSTANTS_KEY, SpeedConstants.KILOMETERS_PER_HOUR.name)
2018-07-04 15:00:51 +02:00
)
sendMyLocationInterval =
prefs.getLong(SEND_MY_LOCATION_INTERVAL_KEY, SEND_MY_LOCATION_INTERVAL_DEFAULT)
userLocationExpireTime =
prefs.getLong(USER_LOCATION_EXPIRE_TIME_KEY, USER_LOCATION_EXPIRE_TIME_DEFAULT)
2018-06-11 18:57:33 +02:00
}
2018-07-13 13:32:11 +02:00
private fun updatePrefs() {
val prefs = app.getSharedPreferences(SETTINGS_NAME, Context.MODE_PRIVATE)
val idsInUse = prefs.getBoolean(TITLES_REPLACED_WITH_IDS, false)
if (!idsInUse) {
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()
}
}
}