2018-06-09 11:20:21 +02:00
|
|
|
package net.osmand.telegram
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import net.osmand.telegram.utils.OsmandFormatter.MetricsConstants
|
|
|
|
import net.osmand.telegram.utils.OsmandFormatter.SpeedConstants
|
|
|
|
|
2018-06-27 10:06:24 +02:00
|
|
|
private const val SETTINGS_NAME = "osmand_telegram_settings"
|
2018-06-09 11:20:21 +02:00
|
|
|
|
2018-06-27 10:06:24 +02:00
|
|
|
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
|
|
|
|
2018-06-27 10:06:24 +02:00
|
|
|
private const val METRICS_CONSTANTS_KEY = "metrics_constants"
|
|
|
|
private const val SPEED_CONSTANTS_KEY = "speed_constants"
|
2018-06-09 11:20:21 +02:00
|
|
|
|
2018-06-27 10:06:24 +02:00
|
|
|
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
|
|
|
|
2018-06-27 10:06:24 +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-06-16 13:55:14 +02:00
|
|
|
|
2018-06-27 10:06:24 +02:00
|
|
|
class TelegramSettings(private val app: TelegramApplication) {
|
2018-06-11 18:57:33 +02:00
|
|
|
|
|
|
|
private var shareLocationChats: Set<String> = emptySet()
|
|
|
|
private var showOnMapChats: Set<String> = emptySet()
|
|
|
|
|
|
|
|
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
|
2018-06-16 13:55:14 +02:00
|
|
|
var userLocationExpireTime = USER_LOCATION_EXPIRE_TIME_DEFAULT
|
2018-06-11 18:57:33 +02:00
|
|
|
|
|
|
|
init {
|
|
|
|
read()
|
|
|
|
}
|
|
|
|
|
2018-07-04 15:00:51 +02:00
|
|
|
fun hasAnyChatToShareLocation() = shareLocationChats.isNotEmpty()
|
2018-06-11 18:57:33 +02:00
|
|
|
|
2018-07-04 15:00:51 +02:00
|
|
|
fun isSharingLocationToChat(chatTitle: String) = shareLocationChats.contains(chatTitle)
|
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-04 15:00:51 +02:00
|
|
|
fun isShowingChatOnMap(chatTitle: String) = showOnMapChats.contains(chatTitle)
|
2018-06-11 18:57:33 +02:00
|
|
|
|
|
|
|
fun removeNonexistingChats(presentChatTitles: List<String>) {
|
|
|
|
val shareLocationChats = shareLocationChats.toMutableList()
|
|
|
|
shareLocationChats.intersect(presentChatTitles)
|
|
|
|
this.shareLocationChats = shareLocationChats.toHashSet()
|
|
|
|
|
|
|
|
val showOnMapChats = showOnMapChats.toMutableList()
|
|
|
|
showOnMapChats.intersect(presentChatTitles)
|
|
|
|
this.showOnMapChats = showOnMapChats.toHashSet()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun shareLocationToChat(chatTitle: String, share: Boolean) {
|
|
|
|
val shareLocationChats = shareLocationChats.toMutableList()
|
|
|
|
if (share) {
|
|
|
|
shareLocationChats.add(chatTitle)
|
|
|
|
} else {
|
|
|
|
shareLocationChats.remove(chatTitle)
|
|
|
|
}
|
|
|
|
this.shareLocationChats = shareLocationChats.toHashSet()
|
|
|
|
}
|
|
|
|
|
2018-06-19 15:00:13 +02:00
|
|
|
fun stopSharingLocationToChats() {
|
|
|
|
this.shareLocationChats = emptySet()
|
|
|
|
}
|
|
|
|
|
2018-06-11 18:57:33 +02:00
|
|
|
fun showChatOnMap(chatTitle: String, show: Boolean) {
|
|
|
|
val showOnMapChats = showOnMapChats.toMutableList()
|
|
|
|
if (show) {
|
|
|
|
showOnMapChats.add(chatTitle)
|
|
|
|
} else {
|
|
|
|
showOnMapChats.remove(chatTitle)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
for (chatTitle in shareLocationChats) {
|
|
|
|
shareLocationChatsSet.add(chatTitle)
|
|
|
|
}
|
|
|
|
edit.putStringSet(SHARE_LOCATION_CHATS_KEY, shareLocationChatsSet)
|
|
|
|
|
|
|
|
val showOnMapChatsSet = mutableSetOf<String>()
|
|
|
|
val showOnMapChats = ArrayList(showOnMapChats)
|
|
|
|
for (chatTitle in showOnMapChats) {
|
|
|
|
showOnMapChatsSet.add(chatTitle)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
|
|
|
|
val shareLocationChats = mutableSetOf<String>()
|
|
|
|
val shareLocationChatsSet = prefs.getStringSet(SHARE_LOCATION_CHATS_KEY, mutableSetOf())
|
|
|
|
for (chatTitle in shareLocationChatsSet) {
|
|
|
|
shareLocationChats.add(chatTitle)
|
|
|
|
}
|
|
|
|
this.shareLocationChats = shareLocationChats
|
|
|
|
|
|
|
|
val showOnMapChats = mutableSetOf<String>()
|
|
|
|
val showOnMapChatsSet = prefs.getStringSet(SHOW_ON_MAP_CHATS_KEY, mutableSetOf())
|
|
|
|
for (chatTitle in showOnMapChatsSet) {
|
|
|
|
showOnMapChats.add(chatTitle)
|
|
|
|
}
|
|
|
|
this.showOnMapChats = showOnMapChats
|
|
|
|
|
2018-07-04 15:00:51 +02:00
|
|
|
metricsConstants = MetricsConstants.valueOf(
|
|
|
|
prefs.getString(METRICS_CONSTANTS_KEY, MetricsConstants.KILOMETERS_AND_METERS.name)
|
|
|
|
)
|
|
|
|
speedConstants = SpeedConstants.valueOf(
|
|
|
|
prefs.getString(SPEED_CONSTANTS_KEY, SpeedConstants.KILOMETERS_PER_HOUR.name)
|
|
|
|
)
|
|
|
|
|
|
|
|
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-06-09 11:20:21 +02:00
|
|
|
}
|