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

375 lines
11 KiB
Kotlin
Raw Normal View History

package net.osmand.telegram
import android.content.Context
import android.support.annotation.DrawableRes
import android.support.annotation.StringRes
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.AndroidUtils
import net.osmand.telegram.utils.OsmandFormatter
import net.osmand.telegram.utils.OsmandFormatter.MetricsConstants
import net.osmand.telegram.utils.OsmandFormatter.SpeedConstants
private val SEND_MY_LOC_VALUES_SEC =
listOf(1L, 2L, 3L, 5L, 10L, 15L, 30L, 60L, 90L, 2 * 60L, 3 * 60L, 5 * 60L)
private val STALE_LOC_VALUES_SEC =
listOf(1 * 60L, 2 * 60L, 5 * 60L, 10 * 60L, 15 * 60L, 30 * 60L, 60 * 60L)
private 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
private var chatLivePeriods = mutableMapOf<Long, Long>()
private var chatShareLocStartSec = 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 = ""
private set
val gpsAndLocPrefs = listOf(SendMyLocPref(), StaleLocPref(), LocHistoryPref())
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()
chatShareLocStartSec = chatShareLocStartSec.filter { (key, _) ->
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
chatShareLocStartSec[chatId] = (System.currentTimeMillis() / 1000)
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)
chatShareLocStartSec.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 getChatLivePeriods(): Map<Long, Long> {
return chatLivePeriods.filter {
getChatLiveMessageExpireTime(it.key) > 0
}
}
fun getChatShareLocStartSec(chatId: Long) = chatShareLocStartSec[chatId]
fun getChatLiveMessageExpireTime(chatId: Long): Long {
val startTime = getChatShareLocStartSec(chatId)
val livePeriod = getChatLivePeriod(chatId)
return if (startTime != null && livePeriod != null) {
livePeriod - ((System.currentTimeMillis() / 1000) - startTime)
} else {
0
}
}
fun updateChatShareLocStartSec(chatId: Long, startTime: Long) {
chatShareLocStartSec[chatId] = startTime
}
fun stopSharingLocationToChats() {
this.shareLocationChats = emptySet()
2018-07-13 16:02:49 +02:00
this.chatLivePeriods.clear()
this.chatShareLocStartSec.clear()
}
fun clearShowOnMapChats() {
showOnMapChats = emptySet()
}
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 clear() {
stopSharingLocationToChats()
2018-08-28 11:50:49 +02:00
app.getSharedPreferences(SETTINGS_NAME, Context.MODE_PRIVATE).edit().clear().apply()
}
fun updateAppToConnect(appToConnectPackage: String) {
this.appToConnectPackage = appToConnectPackage
app.showLocationHelper.stopShowingLocation()
app.osmandAidlHelper.reconnectOsmand()
}
2018-06-11 18:57:33 +02:00
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()
}
}
inner class SendMyLocPref : DurationPref(
R.drawable.ic_action_share_location,
R.string.send_my_location,
R.string.send_my_location_desc,
SEND_MY_LOC_VALUES_SEC
) {
override fun getCurrentValue() =
OsmandFormatter.getFormattedDuration(app, sendMyLocInterval)
override fun setCurrentValue(index: Int) {
sendMyLocInterval = values[index]
app.updateSendLocationInterval()
}
}
inner class StaleLocPref : DurationPref(
R.drawable.ic_action_time_span,
R.string.stale_location,
R.string.stale_location_desc,
STALE_LOC_VALUES_SEC
) {
override fun getCurrentValue() =
OsmandFormatter.getFormattedDuration(app, staleLocTime)
override fun setCurrentValue(index: Int) {
staleLocTime = values[index]
}
}
inner class LocHistoryPref : DurationPref(
R.drawable.ic_action_location_history,
R.string.location_history,
R.string.location_history_desc,
LOC_HISTORY_VALUES_SEC
) {
override fun getCurrentValue() =
OsmandFormatter.getFormattedDuration(app, locHistoryTime)
override fun setCurrentValue(index: Int) {
val value = values[index]
locHistoryTime = value
app.telegramHelper.messageActiveTimeSec = value
}
}
abstract inner class DurationPref(
@DrawableRes val iconId: Int,
@StringRes val titleId: Int,
@StringRes val descriptionId: Int,
val values: List<Long>
) {
abstract fun getCurrentValue(): String
abstract fun setCurrentValue(index: Int)
fun getMenuItems() = values.map { OsmandFormatter.getFormattedDuration(app, it) }
}
enum class AppConnect(
@DrawableRes val iconId: Int,
@DrawableRes val whiteIconId: Int,
val title: String,
2018-09-03 13:30:40 +02:00
val appPackage: String,
val showOnlyInstalled: Boolean
) {
OSMAND_PLUS(
R.drawable.ic_logo_osmand_plus,
R.drawable.ic_action_osmand_plus,
"OsmAnd+",
2018-09-03 13:30:40 +02:00
OsmandAidlHelper.OSMAND_PLUS_PACKAGE_NAME,
false
),
OSMAND_FREE(
R.drawable.ic_logo_osmand_free,
R.drawable.ic_action_osmand_free,
"OsmAnd",
2018-09-03 13:30:40 +02:00
OsmandAidlHelper.OSMAND_FREE_PACKAGE_NAME,
false
),
OSMAND_NIGHTLY(
R.drawable.ic_logo_osmand_free,
R.drawable.ic_action_osmand_free,
"OsmAnd Nightly",
OsmandAidlHelper.OSMAND_NIGHTLY_PACKAGE_NAME,
true
);
companion object {
@DrawableRes
fun getWhiteIconId(appPackage: String): Int {
for (item in values()) {
if (item.appPackage == appPackage) {
return item.whiteIconId
}
}
return 0
}
fun getInstalledApps(context: Context) =
values().filter { AndroidUtils.isAppInstalled(context, it.appPackage) }
}
}
}