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

215 lines
7 KiB
Kotlin
Raw Normal View History

package net.osmand.telegram
import android.content.Context
import net.osmand.telegram.helpers.OsmandAidlHelper
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
val SEND_MY_LOC_VALUES_SEC =
listOf(1L, 2L, 3L, 5L, 10L, 15L, 30L, 60L, 90L, 2 * 60L, 3 * 60L, 5 * 60L)
val STALE_LOC_VALUES_SEC =
listOf(1 * 60L, 2 * 60L, 5 * 60L, 10 * 60L, 15 * 60L, 30 * 60L, 60 * 60L)
val LOC_HISTORY_VALUES_SEC = listOf(
5 * 60L,
15 * 60L,
30 * 60L,
1 * 60 * 60L,
2 * 60 * 60L,
3 * 60 * 60L,
5 * 60 * 60L,
8 * 60 * 60L,
12 * 60 * 60L,
24 * 60 * 60L
)
private const val SEND_MY_LOC_DEFAULT_INDEX = 6
private const val STALE_LOC_DEFAULT_INDEX = 4
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"
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_LOC_INTERVAL_KEY = "send_my_loc_interval"
private const val STALE_LOC_TIME_KEY = "stale_loc_time"
private const val LOC_HISTORY_TIME_KEY = "loc_history_time"
2018-07-13 15:28:37 +02:00
private const val APP_TO_CONNECT_PACKAGE_KEY = "app_to_connect_package"
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
var sendMyLocInterval = SEND_MY_LOC_VALUES_SEC[SEND_MY_LOC_DEFAULT_INDEX]
var staleLocTime = STALE_LOC_VALUES_SEC[STALE_LOC_DEFAULT_INDEX]
var locHistoryTime = LOC_HISTORY_VALUES_SEC[LOC_HISTORY_DEFAULT_INDEX]
2018-06-11 18:57:33 +02:00
var appToConnectPackage = OsmandAidlHelper.OSMAND_PLUS_PACKAGE_NAME
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
}
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)
edit.putLong(SEND_MY_LOC_INTERVAL_KEY, sendMyLocInterval)
edit.putLong(STALE_LOC_TIME_KEY, staleLocTime)
edit.putLong(LOC_HISTORY_TIME_KEY, locHistoryTime)
2018-06-11 18:57:33 +02:00
edit.putString(APP_TO_CONNECT_PACKAGE_KEY, appToConnectPackage)
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
)
val sendMyLocDef = SEND_MY_LOC_VALUES_SEC[SEND_MY_LOC_DEFAULT_INDEX]
sendMyLocInterval = prefs.getLong(SEND_MY_LOC_INTERVAL_KEY, sendMyLocDef)
val staleLocDef = STALE_LOC_VALUES_SEC[STALE_LOC_DEFAULT_INDEX]
staleLocTime = prefs.getLong(STALE_LOC_TIME_KEY, staleLocDef)
val locHistoryDef = LOC_HISTORY_VALUES_SEC[LOC_HISTORY_DEFAULT_INDEX]
locHistoryTime = prefs.getLong(LOC_HISTORY_TIME_KEY, locHistoryDef)
appToConnectPackage = prefs.getString(
APP_TO_CONNECT_PACKAGE_KEY, OsmandAidlHelper.OSMAND_PLUS_PACKAGE_NAME
)
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()
}
}
}