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

1496 lines
49 KiB
Kotlin
Raw Normal View History

package net.osmand.telegram
import android.content.Context
2018-10-23 11:21:15 +02:00
import android.location.LocationManager
2018-10-12 18:17:38 +02:00
import android.support.annotation.ColorRes
import android.support.annotation.DrawableRes
import android.support.annotation.StringRes
2018-10-12 18:17:38 +02:00
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
2019-12-27 18:20:30 +01:00
import com.google.gson.JsonObject
2019-01-29 18:03:44 +01:00
import net.osmand.PlatformUtil
import net.osmand.telegram.helpers.OsmandAidlHelper
import net.osmand.telegram.helpers.ShowLocationHelper
2018-07-13 14:53:58 +02:00
import net.osmand.telegram.helpers.TelegramHelper
2019-12-06 13:41:42 +01:00
import net.osmand.telegram.utils.*
import net.osmand.telegram.utils.OsmandFormatter.MetricsConstants
import net.osmand.telegram.utils.OsmandFormatter.SpeedConstants
2018-10-08 17:33:59 +02:00
import org.drinkless.td.libcore.telegram.TdApi
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
2019-12-27 18:20:30 +01:00
import java.util.*
2018-10-12 18:17:38 +02:00
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentLinkedQueue
2019-12-27 18:20:30 +01:00
import kotlin.collections.ArrayList
val ADDITIONAL_ACTIVE_TIME_VALUES_SEC = listOf(15 * 60L, 30 * 60L, 60 * 60L, 180 * 60L)
const val SHARE_DEVICES_KEY = "devices"
2019-07-15 14:09:29 +02:00
const val LIVE_TRACKS_KEY = "live_tracks"
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 =
2019-02-12 10:31:29 +01:00
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,
2019-02-12 10:31:29 +01:00
24 * 60 * 60L
)
private val MIN_LOCATION_DISTANCE = listOf(0f, 2.0f, 5.0f, 10.0f, 20.0f, 30.0f, 50.0f)
private val MIN_LOCATION_ACCURACY = listOf(0f, 1.0f, 2.0f, 5.0f, 10.0f, 15.0f, 20.0f, 50.0f, 100.0f)
private val MIN_LOCATION_SPEED = listOf(0f, 0.000001f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f)
const val SHARE_TYPE_MAP = "Map"
const val SHARE_TYPE_TEXT = "Text"
2018-12-10 16:37:27 +01:00
const val SHARE_TYPE_MAP_AND_TEXT = "Map_and_text"
private val SHARE_TYPE_VALUES = listOf(SHARE_TYPE_MAP, SHARE_TYPE_TEXT, SHARE_TYPE_MAP_AND_TEXT)
private const val SEND_MY_LOC_DEFAULT_INDEX = 6
private const val STALE_LOC_DEFAULT_INDEX = 0
private const val LOC_HISTORY_DEFAULT_INDEX = 7
private const val SHARE_TYPE_DEFAULT_INDEX = 2
private const val MIN_LOCATION_DISTANCE_INDEX = 0
private const val MIN_LOCATION_ACCURACY_INDEX = 0
private const val MIN_LOCATION_SPEED_INDEX = 0
private const val SETTINGS_NAME = "osmand_telegram_settings"
private const val SHARE_LOCATION_CHATS_KEY = "share_location_chats"
2018-09-05 11:58:32 +02:00
private const val HIDDEN_ON_MAP_CHATS_KEY = "hidden_on_map_chats"
2018-06-11 18:57:33 +02:00
private const val SHARING_MODE_KEY = "current_sharing_mode"
private const val METRICS_CONSTANTS_KEY = "metrics_constants"
private const val SPEED_CONSTANTS_KEY = "speed_constants"
2019-12-06 13:41:42 +01:00
private const val UTC_OFFSET_CONSTANTS_KEY = "utc_offset_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"
private const val SHARE_TYPE_KEY = "share_type"
2018-07-13 15:28:37 +02:00
private const val MIN_LOCATION_DISTANCE_KEY = "min_location_distance"
private const val MIN_LOCATION_ACCURACY_KEY = "min_location_accuracy"
private const val MIN_LOCATION_SPEED_KEY = "min_location_speed"
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"
2018-09-04 16:43:42 +02:00
private const val LIVE_NOW_SORT_TYPE_KEY = "live_now_sort_type"
2018-09-04 15:43:27 +02:00
2018-10-08 17:33:59 +02:00
private const val SHARE_CHATS_INFO_KEY = "share_chats_info"
private const val BATTERY_OPTIMISATION_ASKED = "battery_optimisation_asked"
private const val MONITORING_ENABLED = "monitoring_enabled"
private const val SHOW_GPS_POINTS = "show_gps_points"
2019-04-29 11:11:35 +02:00
private const val PROXY_ENABLED = "proxy_enabled"
private const val PROXY_PREFERENCES_KEY = "proxy_preferences"
2018-10-23 11:21:15 +02:00
private const val SHARING_INITIALIZATION_TIME = 60 * 2L // 2 minutes
2019-02-08 18:12:17 +01:00
private const val WAITING_TDLIB_TIME = 30 // 2 seconds
2018-10-23 11:21:15 +02:00
2018-10-29 14:57:57 +01:00
private const val GPS_UPDATE_EXPIRED_TIME = 60 * 3L // 3 minutes
2019-12-23 12:06:01 +01:00
private const val LAST_CHATS_INFO_KEY = "last_chats_info"
class TelegramSettings(private val app: TelegramApplication) {
2018-06-11 18:57:33 +02:00
2019-01-29 18:03:44 +01:00
private val log = PlatformUtil.getLog(TelegramSettings::class.java)
2018-10-12 18:17:38 +02:00
private var shareChatsInfo = ConcurrentHashMap<Long, ShareChatInfo>()
2018-09-05 11:58:32 +02:00
private var hiddenOnMapChats: Set<Long> = emptySet()
private var shareDevices: Set<DeviceBot> = emptySet()
private var liveTracksInfo = emptyList<LiveTrackInfo>()
2019-12-26 18:17:06 +01:00
var lastChatsInfo = ConcurrentHashMap<Long, LastChatInfo>()
2018-06-11 18:57:33 +02:00
2018-10-12 18:17:38 +02:00
var sharingStatusChanges = ConcurrentLinkedQueue<SharingStatus>()
2018-10-03 11:25:42 +02:00
var currentSharingMode = ""
private set
2018-10-02 12:01:26 +02:00
2019-04-29 11:11:35 +02:00
var currentProxyPref: ProxyPref = ProxySOCKS5Pref(-1, "", -1, "", "")
private set
2018-06-11 18:57:33 +02:00
var metricsConstants = MetricsConstants.KILOMETERS_AND_METERS
var speedConstants = SpeedConstants.KILOMETERS_PER_HOUR
2019-12-06 13:41:42 +01:00
var utcOffset = DataConstants.UTC_FORMAT
2018-06-11 18:57:33 +02:00
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]
var shareTypeValue = SHARE_TYPE_VALUES[SHARE_TYPE_DEFAULT_INDEX]
2018-06-11 18:57:33 +02:00
var minLocationDistance = MIN_LOCATION_DISTANCE[MIN_LOCATION_DISTANCE_INDEX]
var minLocationAccuracy = MIN_LOCATION_ACCURACY[MIN_LOCATION_ACCURACY_INDEX]
var minLocationSpeed = MIN_LOCATION_SPEED[MIN_LOCATION_SPEED_INDEX]
var appToConnectPackage = ""
private set
2019-01-10 14:47:56 +01:00
var liveNowSortType = LiveNowSortType.SORT_BY_DISTANCE
2018-09-04 15:43:27 +02:00
val gpsAndLocPrefs = listOf(SendMyLocPref(), StaleLocPref(), LocHistoryPref(), ShareTypePref())
val gpxLoggingPrefs = listOf(MinLocationDistance(), MinLocationAccuracy(), MinLocationSpeed())
2019-12-06 13:41:42 +01:00
val unitsAndFormatsPrefs = listOf(UnitsOfSpeed(), UnitsOfLength(), UtcOffset())
var batteryOptimisationAsked = false
var monitoringEnabled = false
var showGpsPoints = false
2019-04-29 11:11:35 +02:00
var proxyEnabled = false
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()
2019-04-29 11:11:35 +02:00
applyProxyPref()
2018-06-11 18:57:33 +02:00
}
2018-10-08 17:33:59 +02:00
fun hasAnyChatToShareLocation() = shareChatsInfo.isNotEmpty()
2018-06-11 18:57:33 +02:00
2018-10-12 18:17:38 +02:00
fun isSharingLocationToChat(chatId: Long) = shareChatsInfo.containsKey(chatId)
2018-06-11 18:57:33 +02:00
fun isSharingLocationToUser(userId: Int) = shareChatsInfo.values.any { it.userId == userId }
2018-09-05 11:58:32 +02:00
fun hasAnyChatToShowOnMap() = !hiddenOnMapChats.containsAll(getLiveNowChats())
2018-06-11 18:57:33 +02:00
2019-07-17 16:23:27 +02:00
fun hasAnyLiveTracksToShowOnMap(): Boolean {
val time = System.currentTimeMillis() - locHistoryTime * 1000
val locations = app.locationMessages.getLastLocationMessagesSinceTime(time)
locations.forEach { loc ->
if (liveTracksInfo.any { it.userId == loc.userId && it.chatId == loc.chatId && it.deviceName == loc.deviceName }) {
return true
}
}
return false
}
fun getLiveTracksInfo() = liveTracksInfo
fun getLiveTrackInfo(userId: Int, chatId: Long, deviceName: String) =
liveTracksInfo.firstOrNull { it.userId == userId && it.chatId == chatId && it.deviceName == deviceName }
2018-09-05 11:58:32 +02:00
fun isShowingChatOnMap(chatId: Long) = !hiddenOnMapChats.contains(chatId)
2018-06-11 18:57:33 +02:00
2019-07-15 14:09:29 +02:00
fun isLiveTrackEnabled(userId: Int, chatId: Long, deviceName: String) =
liveTracksInfo.any { (it.chatId == chatId && it.userId == userId && it.deviceName == deviceName) }
fun updateLiveTrack(userId: Int, chatId: Long, deviceName: String, enable: Boolean) {
val tracksInfo = liveTracksInfo.toMutableList()
if (enable) {
val colorIndex = if (tracksInfo.size > 0) (tracksInfo.last().colorIndex + 1) % ShowLocationHelper.GPX_COLORS_COUNT else 0
tracksInfo.add(LiveTrackInfo(userId, chatId, deviceName, colorIndex))
2019-07-15 14:09:29 +02:00
} else {
tracksInfo.removeAll { it.userId == userId && it.chatId == chatId && it.deviceName == deviceName }
2019-07-15 14:09:29 +02:00
}
liveTracksInfo = tracksInfo.toList()
2019-07-15 14:09:29 +02:00
}
2018-07-13 13:02:59 +02:00
fun removeNonexistingChats(presentChatIds: List<Long>) {
2018-09-05 11:58:32 +02:00
val hiddenChats = hiddenOnMapChats.toMutableList()
hiddenChats.intersect(presentChatIds)
hiddenOnMapChats = hiddenChats.toHashSet()
2018-07-13 14:53:58 +02:00
2018-10-17 11:55:26 +02:00
shareChatsInfo = ConcurrentHashMap(shareChatsInfo.filter { (key, _) -> presentChatIds.contains(key) })
2018-06-11 18:57:33 +02:00
}
fun shareLocationToChat(
chatId: Long,
share: Boolean,
livePeriod: Long = DEFAULT_VISIBLE_TIME_SECONDS,
2018-10-12 11:08:27 +02:00
addActiveTime: Long = ADDITIONAL_ACTIVE_TIME_VALUES_SEC[0]
) {
2018-06-11 18:57:33 +02:00
if (share) {
2018-10-08 17:33:59 +02:00
var shareChatInfo = shareChatsInfo[chatId]
if (shareChatInfo == null) {
shareChatInfo = ShareChatInfo()
}
val chat = app.telegramHelper.getChat(chatId)
if (chat != null && (chat.type is TdApi.ChatTypePrivate || chat.type is TdApi.ChatTypeSecret)) {
shareChatInfo.userId = app.telegramHelper.getUserIdFromChatType(chat.type)
}
2018-10-12 18:17:38 +02:00
shareChatInfo.chatId = chatId
updateChatShareInfo(shareChatInfo, livePeriod, addActiveTime)
2018-10-08 17:33:59 +02:00
shareChatsInfo[chatId] = shareChatInfo
2018-06-11 18:57:33 +02:00
} else {
2018-10-08 17:33:59 +02:00
shareChatsInfo.remove(chatId)
2018-06-11 18:57:33 +02:00
}
2018-07-13 14:53:58 +02:00
}
fun shareLocationToUser(
userId: Int,
livePeriod: Long = DEFAULT_VISIBLE_TIME_SECONDS,
addActiveTime: Long = ADDITIONAL_ACTIVE_TIME_VALUES_SEC[0]
) {
val shareChatInfo = ShareChatInfo()
shareChatInfo.userId = userId
updateChatShareInfo(shareChatInfo, livePeriod, addActiveTime)
app.telegramHelper.createPrivateChatWithUser(userId, shareChatInfo, shareChatsInfo)
}
fun updateShareDevices(list: List<DeviceBot>) {
shareDevices = list.toHashSet()
2018-10-02 12:01:26 +02:00
}
2018-10-08 17:33:59 +02:00
fun addShareDevice(device: DeviceBot) {
val devices = shareDevices.toMutableList()
devices.add(device)
shareDevices = devices.toHashSet()
}
fun updateCurrentSharingMode(sharingMode: String) {
if (currentSharingMode != sharingMode) {
shareChatsInfo.forEach { (_, shareInfo) ->
2019-02-08 18:12:17 +01:00
shareInfo.shouldSendViaBotTextMessage = true
shareInfo.shouldSendViaBotMapMessage = true
}
2019-02-08 18:12:17 +01:00
prepareForSharingNewMessages()
}
currentSharingMode = sharingMode
}
2019-04-29 11:11:35 +02:00
fun updateCurrentProxyPref(proxyPref: ProxyPref, proxyEnabled: Boolean) {
this.proxyEnabled = proxyEnabled
currentProxyPref = proxyPref
applyProxyPref()
}
fun updateProxySetting(enable: Boolean) {
this.proxyEnabled = enable
if (enable) {
app.telegramHelper.enableProxy(currentProxyPref.id)
} else {
app.telegramHelper.disableProxy()
}
}
fun applyProxyPref() {
val proxyId = currentProxyPref.id
if (proxyId != -1) {
app.telegramHelper.editProxyPref(currentProxyPref, proxyEnabled)
} else {
app.telegramHelper.addProxyPref(currentProxyPref, proxyEnabled)
}
}
2019-02-08 18:12:17 +01:00
fun prepareForSharingNewMessages() {
shareChatsInfo.forEach { (_, shareInfo) ->
prepareForSharingNewMessages(shareInfo)
2019-02-08 18:12:17 +01:00
}
}
fun prepareForSharingNewMessages(chatsIds: List<Long>) {
chatsIds.forEach {
shareChatsInfo[it]?.also { shareInfo ->
prepareForSharingNewMessages(shareInfo)
}
}
}
fun prepareForSharingNewMessages(shareInfo: ShareChatInfo) {
shareInfo.pendingTdLibText = 0
shareInfo.pendingTdLibMap = 0
shareInfo.currentTextMessageId = -1L
shareInfo.currentMapMessageId = -1L
shareInfo.pendingTextMessage = false
shareInfo.pendingMapMessage = false
}
2018-10-08 17:33:59 +02:00
fun getChatLivePeriod(chatId: Long) = shareChatsInfo[chatId]?.livePeriod
fun getChatsShareInfo() = shareChatsInfo
2018-06-11 18:57:33 +02:00
fun getShareDevices() = shareDevices
fun containsShareDeviceWithName(name: String): Boolean {
shareDevices.forEach {
if (it.deviceName == name) {
return true
}
}
return false
}
fun getCurrentSharingDevice() = shareDevices.singleOrNull { it.externalId == currentSharingMode }
fun getLastSuccessfulSendTime(): Long {
val lastSuccessTextSend = shareChatsInfo.values.maxBy { it.lastTextSuccessfulSendTime }?.lastTextSuccessfulSendTime ?: -1
val lastSuccessMapSend = shareChatsInfo.values.maxBy { it.lastMapSuccessfulSendTime }?.lastMapSuccessfulSendTime ?: -1
return Math.max(lastSuccessTextSend, lastSuccessMapSend)
}
2018-10-12 18:17:38 +02:00
fun stopSharingLocationToChats() {
2018-10-08 17:33:59 +02:00
shareChatsInfo.clear()
}
2018-07-13 13:32:11 +02:00
fun showChatOnMap(chatId: Long, show: Boolean) {
2018-09-05 11:58:32 +02:00
val hiddenChats = hiddenOnMapChats.toMutableList()
2018-06-11 18:57:33 +02:00
if (show) {
2018-09-05 11:58:32 +02:00
hiddenChats.remove(chatId)
2018-06-11 18:57:33 +02:00
} else {
2018-09-05 11:58:32 +02:00
hiddenChats.add(chatId)
2018-06-11 18:57:33 +02:00
}
2018-09-05 11:58:32 +02:00
hiddenOnMapChats = hiddenChats.toHashSet()
2018-06-11 18:57:33 +02:00
}
2018-10-08 17:33:59 +02:00
fun getShareLocationChats() = shareChatsInfo.keys
2018-07-04 15:00:51 +02:00
2018-09-05 11:58:32 +02:00
fun getShowOnMapChats() = getLiveNowChats().minus(hiddenOnMapChats)
2018-06-11 18:57:33 +02:00
2018-09-05 11:58:32 +02:00
fun getShowOnMapChatsCount() = getShowOnMapChats().size
2018-06-11 18:57:33 +02:00
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) {
app.showLocationHelper.stopShowingLocation()
this.appToConnectPackage = appToConnectPackage
app.osmandAidlHelper.reconnectOsmand()
}
2018-10-08 17:33:59 +02:00
fun updateShareInfo(message: TdApi.Message) {
val shareInfo = shareChatsInfo[message.chatId]
2018-10-08 17:33:59 +02:00
val content = message.content
val isOsmAndBot = app.telegramHelper.isOsmAndBot(OsmandLocationUtils.getSenderMessageId(message)) || app.telegramHelper.isOsmAndBot(message.viaBotUserId)
if (shareInfo != null) {
2018-12-10 14:10:11 +01:00
when (content) {
is TdApi.MessageLocation -> {
2019-01-29 18:03:44 +01:00
val state = message.sendingState
if (state != null) {
if (state.constructor == TdApi.MessageSendingStatePending.CONSTRUCTOR) {
shareInfo.pendingMapMessage = true
2019-01-29 18:03:44 +01:00
log.debug("updateShareInfo MAP ${message.id} MessageSendingStatePending")
shareInfo.oldMapMessageId = message.id
2019-02-08 18:12:17 +01:00
if (isOsmAndBot) {
shareInfo.shouldSendViaBotMapMessage = false
}
2019-01-29 18:03:44 +01:00
} else if (state.constructor == TdApi.MessageSendingStateFailed.CONSTRUCTOR) {
shareInfo.hasSharingError = true
shareInfo.pendingMapMessage = false
2019-01-29 18:03:44 +01:00
log.debug("updateShareInfo MAP ${message.id} MessageSendingStateFailed")
}
} else {
shareInfo.currentMapMessageId = message.id
shareInfo.pendingMapMessage = false
shareInfo.lastMapSuccessfulSendTime = System.currentTimeMillis() / 1000
2019-02-08 18:12:17 +01:00
if (!isOsmAndBot) {
shareInfo.pendingTdLibMap--
if (shareTypeValue == SHARE_TYPE_MAP) {
shareInfo.sentMessages++
}
} else {
shareInfo.shouldSendViaBotMapMessage = false
2019-01-29 18:03:44 +01:00
}
2019-02-08 18:12:17 +01:00
log.debug("updateShareInfo MAP ${message.id} SUCCESS pendingTdLibMap: ${shareInfo.pendingTdLibMap}")
2019-01-29 18:03:44 +01:00
}
}
is TdApi.MessageText -> {
2019-01-29 18:03:44 +01:00
val state = message.sendingState
if (state != null) {
if (state.constructor == TdApi.MessageSendingStatePending.CONSTRUCTOR) {
log.debug("updateShareInfo TEXT ${message.id} MessageSendingStatePending")
shareInfo.pendingTextMessage = true
shareInfo.oldTextMessageId = message.id
2019-02-08 18:12:17 +01:00
if (isOsmAndBot) {
shareInfo.shouldSendViaBotTextMessage = false
}
2019-01-29 18:03:44 +01:00
} else if (state.constructor == TdApi.MessageSendingStateFailed.CONSTRUCTOR) {
log.debug("updateShareInfo TEXT ${message.id} MessageSendingStateFailed")
shareInfo.hasSharingError = true
shareInfo.pendingTextMessage = false
2019-01-29 18:03:44 +01:00
}
} else {
shareInfo.currentTextMessageId = message.id
shareInfo.updateTextMessageId++
shareInfo.pendingTextMessage = false
shareInfo.lastTextSuccessfulSendTime = System.currentTimeMillis() / 1000
2019-02-08 18:12:17 +01:00
if (!isOsmAndBot) {
shareInfo.pendingTdLibText--
shareInfo.sentMessages++
} else {
shareInfo.shouldSendViaBotTextMessage = false
}
log.debug("updateShareInfo TEXT ${message.id} SUCCESS pendingTdLibMap: ${shareInfo.pendingTdLibText}")
2019-01-29 18:03:44 +01:00
}
}
}
2018-10-12 18:17:38 +02:00
}
}
fun updateSharingStatusHistory() {
2018-10-23 11:21:15 +02:00
val newSharingStatus = getNewSharingStatusHistoryItem()
2018-10-12 18:17:38 +02:00
if (sharingStatusChanges.isNotEmpty()) {
val lastSharingStatus = sharingStatusChanges.last()
if (lastSharingStatus.statusType != newSharingStatus.statusType) {
sharingStatusChanges.add(newSharingStatus)
} else {
lastSharingStatus.apply {
statusChangeTime = newSharingStatus.statusChangeTime
locationTime = newSharingStatus.locationTime
chatsIds = newSharingStatus.chatsIds
title = newSharingStatus.title
2018-10-23 11:21:15 +02:00
if (statusType == SharingStatusType.INITIALIZING
&& newSharingStatus.statusType == SharingStatusType.INITIALIZING) {
if (!description.contains(newSharingStatus.description)) {
description = "$description, ${newSharingStatus.description}"
}
} else {
description = newSharingStatus.description
2018-10-23 11:21:15 +02:00
}
2018-10-12 18:17:38 +02:00
}
}
} else {
sharingStatusChanges.add(newSharingStatus)
2018-10-08 17:33:59 +02:00
}
}
private fun updateChatShareInfo(
shareChatInfo: ShareChatInfo,
livePeriod: Long = DEFAULT_VISIBLE_TIME_SECONDS,
addActiveTime: Long = ADDITIONAL_ACTIVE_TIME_VALUES_SEC[0]
) {
val lp: Long = when {
livePeriod < TelegramHelper.MIN_LOCATION_MESSAGE_LIVE_PERIOD_SEC -> TelegramHelper.MIN_LOCATION_MESSAGE_LIVE_PERIOD_SEC.toLong()
else -> livePeriod
}
val currentTime = System.currentTimeMillis() / 1000
val user = app.telegramHelper.getCurrentUser()
if (user != null && currentSharingMode != user.id.toString() && shareChatInfo.start == -1L) {
2019-02-08 18:12:17 +01:00
shareChatInfo.shouldSendViaBotTextMessage = true
shareChatInfo.shouldSendViaBotMapMessage = true
}
shareChatInfo.start = currentTime
if (shareChatInfo.livePeriod == -1L) {
shareChatInfo.livePeriod = lp
}
shareChatInfo.userSetLivePeriod = lp
shareChatInfo.userSetLivePeriodStart = currentTime
shareChatInfo.currentMessageLimit = currentTime + Math.min(lp, TelegramHelper.MAX_LOCATION_MESSAGE_LIVE_PERIOD_SEC.toLong())
shareChatInfo.additionalActiveTime = addActiveTime
}
2018-10-23 11:21:15 +02:00
private fun getNewSharingStatusHistoryItem(): SharingStatus {
return SharingStatus().apply {
statusChangeTime = System.currentTimeMillis()
val lm = app.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val gpsEnabled = try {
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
val loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER)
2018-10-29 15:50:24 +01:00
val gpsActive = loc != null && ((statusChangeTime - loc.time) / 1000) < GPS_UPDATE_EXPIRED_TIME
2019-02-01 12:34:22 +01:00
val lastSentLocationExpired = ((statusChangeTime - app.shareLocationHelper.lastLocationUpdateTime) / 1000) > GPS_UPDATE_EXPIRED_TIME
2018-10-29 15:50:24 +01:00
(gpsActive || !lastSentLocationExpired)
} else {
false
}
2018-10-23 11:21:15 +02:00
} catch (ex: Exception) {
false
}
var initializing = false
var sendChatsErrors = false
shareChatsInfo.forEach { (_, shareInfo) ->
if (shareInfo.lastTextSuccessfulSendTime == -1L && shareInfo.lastMapSuccessfulSendTime == -1L
&& ((statusChangeTime / 1000 - shareInfo.start) < SHARING_INITIALIZATION_TIME)) {
2018-10-23 11:21:15 +02:00
initializing = true
} else {
val textSharingError = shareInfo.lastSendTextMessageTime - shareInfo.lastTextSuccessfulSendTime > WAITING_TDLIB_TIME
val mapSharingError = shareInfo.lastSendMapMessageTime - shareInfo.lastMapSuccessfulSendTime > WAITING_TDLIB_TIME
if (shareInfo.hasSharingError
|| (shareTypeValue == SHARE_TYPE_MAP_AND_TEXT && (textSharingError || mapSharingError))
|| textSharingError && (shareTypeValue == SHARE_TYPE_TEXT)
|| mapSharingError && (shareTypeValue == SHARE_TYPE_MAP)
) {
sendChatsErrors = true
locationTime = Math.max(shareInfo.lastTextSuccessfulSendTime, shareInfo.lastMapSuccessfulSendTime)
chatsIds.add(shareInfo.chatId)
}
2018-10-23 11:21:15 +02:00
}
}
if (sendChatsErrors) {
title = app.getString(R.string.not_possible_to_send_to_telegram_chats)
description = app.getString(R.string.last_updated_location)
statusType = SharingStatusType.NOT_POSSIBLE_TO_SENT_TO_CHATS
} else if (!initializing) {
when {
!gpsEnabled -> {
locationTime = app.shareLocationHelper.lastLocationUpdateTime / 1000
if (locationTime <= 0) {
locationTime = getLastSuccessfulSendTime()
}
2018-10-23 11:21:15 +02:00
title = app.getString(R.string.no_gps_connection)
description = app.getString(R.string.last_updated_location)
statusType = SharingStatusType.NO_GPS
}
!app.isInternetConnectionAvailable -> {
locationTime = getLastSuccessfulSendTime()
title = app.getString(R.string.no_internet_connection)
description = app.getString(R.string.last_updated_location)
statusType = SharingStatusType.NO_INTERNET
}
else -> {
locationTime = getLastSuccessfulSendTime()
2018-10-25 16:37:01 +02:00
statusType = SharingStatusType.SENDING
2018-10-25 16:34:57 +02:00
if (locationTime == -1L) {
title = app.getString(R.string.sending_location_messages)
description = app.getString(R.string.waiting_for_response_from_telegram)
} else {
title = app.getString(R.string.successfully_sent_and_updated)
description = app.getString(R.string.last_updated_location)
}
2018-10-23 11:21:15 +02:00
}
}
} else {
2018-10-23 11:36:28 +02:00
if (gpsEnabled && app.isInternetConnectionAvailable) {
title = app.getString(R.string.sending_location_messages)
description = app.getString(R.string.waiting_for_response_from_telegram)
statusType = SharingStatusType.SENDING
} else {
title = app.getString(R.string.initializing)
statusType = SharingStatusType.INITIALIZING
if (!gpsEnabled) {
2018-10-23 11:21:15 +02:00
description = app.getString(R.string.searching_for_gps)
2018-10-23 11:36:28 +02:00
} else if (!app.isInternetConnectionAvailable) {
2018-10-23 11:21:15 +02:00
description = app.getString(R.string.connecting_to_the_internet)
}
}
}
}
}
2018-10-08 17:33:59 +02:00
fun onDeleteLiveMessages(chatId: Long, messages: List<Long>) {
val currentMapMessageId = shareChatsInfo[chatId]?.currentMapMessageId
if (messages.contains(currentMapMessageId)) {
shareChatsInfo[chatId]?.currentMapMessageId = -1
2019-02-08 18:12:17 +01:00
shareChatsInfo[chatId]?.shouldSendViaBotMapMessage = true
shareChatsInfo[chatId]?.shouldSendViaBotTextMessage = true
}
val currentTextMessageId = shareChatsInfo[chatId]?.currentTextMessageId
if (messages.contains(currentTextMessageId)) {
shareChatsInfo[chatId]?.currentTextMessageId = -1
shareChatsInfo[chatId]?.updateTextMessageId = 1
2019-02-08 18:12:17 +01:00
shareChatsInfo[chatId]?.shouldSendViaBotMapMessage = true
shareChatsInfo[chatId]?.shouldSendViaBotTextMessage = true
2018-10-08 17:33:59 +02:00
}
}
2018-06-11 18:57:33 +02:00
fun save() {
val prefs = app.getSharedPreferences(SETTINGS_NAME, Context.MODE_PRIVATE)
val edit = prefs.edit()
2018-09-05 11:58:32 +02:00
val hiddenChatsSet = mutableSetOf<String>()
val hiddenChats = ArrayList(hiddenOnMapChats)
for (chatId in hiddenChats) {
hiddenChatsSet.add(chatId.toString())
2018-06-11 18:57:33 +02:00
}
2018-09-05 11:58:32 +02:00
edit.putStringSet(HIDDEN_ON_MAP_CHATS_KEY, hiddenChatsSet)
2018-06-11 18:57:33 +02:00
edit.putString(SHARING_MODE_KEY, currentSharingMode)
2018-06-11 18:57:33 +02:00
edit.putString(METRICS_CONSTANTS_KEY, metricsConstants.name)
edit.putString(SPEED_CONSTANTS_KEY, speedConstants.name)
2019-12-06 13:41:42 +01:00
edit.putString(UTC_OFFSET_CONSTANTS_KEY, utcOffset)
2018-06-11 18:57:33 +02:00
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.putFloat(MIN_LOCATION_DISTANCE_KEY, minLocationDistance)
edit.putFloat(MIN_LOCATION_ACCURACY_KEY, minLocationAccuracy)
edit.putFloat(MIN_LOCATION_SPEED_KEY, minLocationSpeed)
edit.putString(SHARE_TYPE_KEY, shareTypeValue)
edit.putString(APP_TO_CONNECT_PACKAGE_KEY, appToConnectPackage)
2018-09-04 16:43:42 +02:00
edit.putString(LIVE_NOW_SORT_TYPE_KEY, liveNowSortType.name)
edit.putBoolean(BATTERY_OPTIMISATION_ASKED, batteryOptimisationAsked)
edit.putBoolean(MONITORING_ENABLED, monitoringEnabled)
edit.putBoolean(SHOW_GPS_POINTS, showGpsPoints)
2019-04-29 11:11:35 +02:00
edit.putBoolean(PROXY_ENABLED, proxyEnabled)
val jArray = convertShareChatsInfoToJson()
if (jArray != null) {
2018-10-08 17:33:59 +02:00
edit.putString(SHARE_CHATS_INFO_KEY, jArray.toString())
}
val jsonObject = convertShareDevicesToJson()
if (jsonObject != null) {
edit.putString(SHARE_DEVICES_KEY, jsonObject.toString())
}
2019-04-29 11:11:35 +02:00
val jsonObjectProxy = convertProxyPrefToJson()
if (jsonObjectProxy != null) {
edit.putString(PROXY_PREFERENCES_KEY, jsonObjectProxy.toString())
}
2019-07-15 14:09:29 +02:00
val jsonArrayLiveTracks = convertLiveTracksInfoToJson()
if (jsonArrayLiveTracks != null) {
edit.putString(LIVE_TRACKS_KEY, jsonArrayLiveTracks.toString())
}
2019-12-23 12:06:01 +01:00
val jArrayLastInfo = convertLastChatsInfoToJson()
if (jArrayLastInfo != null) {
edit.putString(LAST_CHATS_INFO_KEY, jArrayLastInfo.toString())
}
2018-06-11 18:57:33 +02:00
edit.apply()
}
fun read() {
val prefs = app.getSharedPreferences(SETTINGS_NAME, Context.MODE_PRIVATE)
2018-09-05 11:58:32 +02:00
val hiddenChats = mutableSetOf<Long>()
val hiddenChatsSet = prefs.getStringSet(HIDDEN_ON_MAP_CHATS_KEY, mutableSetOf())
for (chatId in hiddenChatsSet) {
hiddenChats.add(chatId.toLong())
2018-06-11 18:57:33 +02:00
}
2018-09-05 11:58:32 +02:00
hiddenOnMapChats = hiddenChats
2018-06-11 18:57:33 +02:00
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
)
2019-12-06 13:41:42 +01:00
utcOffset = prefs.getString(UTC_OFFSET_CONSTANTS_KEY, DataConstants.UTC_FORMAT)
2018-07-04 15:00:51 +02:00
2018-10-08 17:33:59 +02:00
try {
parseShareChatsInfo(JSONArray(prefs.getString(SHARE_CHATS_INFO_KEY, "")))
} catch (e: JSONException) {
e.printStackTrace()
}
2019-12-23 12:06:01 +01:00
try {
parseLastChatsInfo(JSONArray(prefs.getString(LAST_CHATS_INFO_KEY, "")))
} catch (e: JSONException) {
log.error(e)
}
parseShareDevices(prefs.getString(SHARE_DEVICES_KEY, ""))
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)
val shareTypeDef = SHARE_TYPE_VALUES[SHARE_TYPE_DEFAULT_INDEX]
shareTypeValue = prefs.getString(SHARE_TYPE_KEY, shareTypeDef)
val minLocationDistanceDef = MIN_LOCATION_DISTANCE[MIN_LOCATION_DISTANCE_INDEX]
minLocationDistance = prefs.getFloat(MIN_LOCATION_DISTANCE_KEY, minLocationDistanceDef)
val minLocationPrecisionDef = MIN_LOCATION_ACCURACY[MIN_LOCATION_ACCURACY_INDEX]
minLocationAccuracy = prefs.getFloat(MIN_LOCATION_ACCURACY_KEY, minLocationPrecisionDef)
val minLocationSpeedDef = MIN_LOCATION_SPEED[MIN_LOCATION_SPEED_INDEX]
minLocationSpeed = prefs.getFloat(MIN_LOCATION_SPEED_KEY, minLocationSpeedDef)
val currentUserId = app.telegramHelper.getCurrentUserId()
currentSharingMode = prefs.getString(SHARING_MODE_KEY, if (currentUserId != -1) currentUserId.toString() else "")
val defPackage = if (AppConnect.getInstalledApps(app).size == 1) AppConnect.getInstalledApps(app).first().appPackage else ""
appToConnectPackage = prefs.getString(APP_TO_CONNECT_PACKAGE_KEY, defPackage)
liveNowSortType = LiveNowSortType.valueOf(
2019-01-10 14:47:56 +01:00
prefs.getString(LIVE_NOW_SORT_TYPE_KEY, LiveNowSortType.SORT_BY_DISTANCE.name)
)
batteryOptimisationAsked = prefs.getBoolean(BATTERY_OPTIMISATION_ASKED,false)
monitoringEnabled = prefs.getBoolean(MONITORING_ENABLED,false)
2019-04-29 11:11:35 +02:00
showGpsPoints = prefs.getBoolean(SHOW_GPS_POINTS, false)
proxyEnabled = prefs.getBoolean(PROXY_ENABLED, false)
try {
parseProxyPreferences(JSONObject(prefs.getString(PROXY_PREFERENCES_KEY, "")))
} catch (e: JSONException) {
2019-07-15 14:09:29 +02:00
log.error(e)
}
try {
parseLiveTracks(JSONArray(prefs.getString(LIVE_TRACKS_KEY, "")))
} catch (e: JSONException) {
log.error(e)
2019-04-29 11:11:35 +02:00
}
2018-06-11 18:57:33 +02:00
}
2018-07-13 13:32:11 +02:00
private fun convertShareDevicesToJson():JSONObject?{
return try {
val jsonObject = JSONObject()
val jArray = JSONArray()
shareDevices.forEach { device ->
val obj = JSONObject()
obj.put(DeviceBot.DEVICE_ID, device.id)
obj.put(DeviceBot.USER_ID, device.userId)
obj.put(DeviceBot.CHAT_ID, device.chatId)
obj.put(DeviceBot.DEVICE_NAME, device.deviceName)
obj.put(DeviceBot.EXTERNAL_ID, device.externalId)
obj.put(DeviceBot.DATA, JSONObject(device.data))
jArray.put(obj)
}
jsonObject.put(SHARE_DEVICES_KEY, jArray)
} catch (e: JSONException) {
e.printStackTrace()
null
}
}
2019-07-15 14:09:29 +02:00
private fun convertLiveTracksInfoToJson(): JSONArray? {
return try {
JSONArray().apply {
liveTracksInfo.forEach { liveTrackInfo ->
val obj = JSONObject()
obj.put(LiveTrackInfo.USER_ID, liveTrackInfo.userId)
obj.put(LiveTrackInfo.CHAT_ID, liveTrackInfo.chatId)
obj.put(LiveTrackInfo.DEVICE_NAME, liveTrackInfo.deviceName)
obj.put(LiveTrackInfo.COLOR_INDEX, liveTrackInfo.colorIndex)
2019-07-15 14:09:29 +02:00
put(obj)
}
}
} catch (e: JSONException) {
log.error(e)
null
}
}
2019-04-29 11:11:35 +02:00
private fun convertProxyPrefToJson(): JSONObject? {
return try {
val proxyPref = currentProxyPref
JSONObject().apply {
put(ProxyPref.PROXY_ID, proxyPref.id)
put(ProxyPref.TYPE_ID, proxyPref.type)
put(ProxyPref.SERVER_ID, proxyPref.server)
put(ProxyPref.PORT_ID, proxyPref.port)
if (proxyPref is ProxyMTProtoPref) {
put(ProxyMTProtoPref.KEY_ID, proxyPref.key)
} else if (proxyPref is ProxySOCKS5Pref) {
put(ProxySOCKS5Pref.LOGIN_ID, proxyPref.login)
put(ProxySOCKS5Pref.PASSWORD_ID, proxyPref.password)
}
}
} catch (e: JSONException) {
e.printStackTrace()
null
}
}
private fun convertShareChatsInfoToJson(): JSONArray? {
return try {
val jArray = JSONArray()
shareChatsInfo.forEach { (chatId, chatInfo) ->
val obj = JSONObject()
obj.put(ShareChatInfo.CHAT_ID_KEY, chatId)
obj.put(ShareChatInfo.USER_ID_KEY, chatInfo.userId)
obj.put(ShareChatInfo.START_KEY, chatInfo.start)
obj.put(ShareChatInfo.LIVE_PERIOD_KEY, chatInfo.livePeriod)
obj.put(ShareChatInfo.LIMIT_KEY, chatInfo.currentMessageLimit)
obj.put(ShareChatInfo.UPDATE_TEXT_MESSAGE_ID_KEY, chatInfo.updateTextMessageId)
obj.put(ShareChatInfo.CURRENT_MAP_MESSAGE_ID_KEY, chatInfo.currentMapMessageId)
obj.put(ShareChatInfo.CURRENT_TEXT_MESSAGE_ID_KEY, chatInfo.currentTextMessageId)
obj.put(ShareChatInfo.USER_SET_LIVE_PERIOD_KEY, chatInfo.userSetLivePeriod)
obj.put(ShareChatInfo.USER_SET_LIVE_PERIOD_START_KEY, chatInfo.userSetLivePeriodStart)
obj.put(ShareChatInfo.LAST_TEXT_SUCCESSFUL_SEND_TIME_KEY, chatInfo.lastTextSuccessfulSendTime)
obj.put(ShareChatInfo.LAST_MAP_SUCCESSFUL_SEND_TIME_KEY, chatInfo.lastMapSuccessfulSendTime)
obj.put(ShareChatInfo.LAST_SEND_MAP_TIME_KEY, chatInfo.lastSendMapMessageTime)
obj.put(ShareChatInfo.LAST_SEND_TEXT_TIME_KEY, chatInfo.lastSendTextMessageTime)
2019-01-29 18:03:44 +01:00
obj.put(ShareChatInfo.PENDING_TEXT_MESSAGE_KEY, chatInfo.pendingTextMessage)
obj.put(ShareChatInfo.PENDING_MAP_MESSAGE_KEY, chatInfo.pendingMapMessage)
obj.put(ShareChatInfo.SENT_MESSAGES_KEY, chatInfo.sentMessages)
jArray.put(obj)
}
jArray
} catch (e: JSONException) {
2019-07-15 14:09:29 +02:00
log.error(e)
null
}
}
2019-12-23 12:06:01 +01:00
private fun convertLastChatsInfoToJson(): JSONArray? {
return try {
val jArray = JSONArray()
lastChatsInfo.forEach { (chatId, lastInfo) ->
val obj = JSONObject()
obj.put(LastChatInfo.CHAT_ID_KEY, chatId)
obj.put(LastChatInfo.LIVE_PERIOD_KEY, lastInfo.livePeriod)
2019-12-27 18:20:30 +01:00
obj.put(LastChatInfo.PERIODS_KEY, convertPeriodsToJson(lastInfo.periods))
log.info("Periods to put: ${lastInfo.periods}")
jArray.put(obj)
}
jArray
} catch (e: JSONException) {
log.error(e)
null
}
}
private fun convertPeriodsToJson(periods: LinkedList<Long>): JSONArray? {
return try {
val jArray = JSONArray()
for (i in 0 until periods.count()) {
val obj = JSONObject()
obj.put(i.toString(), periods[i])
2019-12-23 12:06:01 +01:00
jArray.put(obj)
}
2019-12-27 18:20:30 +01:00
log.info("Json array periods: $jArray")
2019-12-23 12:06:01 +01:00
jArray
} catch (e: JSONException) {
log.error(e)
null
}
}
2018-10-08 17:33:59 +02:00
private fun parseShareChatsInfo(json: JSONArray) {
for (i in 0 until json.length()) {
val obj = json.getJSONObject(i)
val shareInfo = ShareChatInfo().apply {
chatId = obj.optLong(ShareChatInfo.CHAT_ID_KEY)
userId = obj.optInt(ShareChatInfo.USER_ID_KEY)
2018-10-08 17:33:59 +02:00
start = obj.optLong(ShareChatInfo.START_KEY)
livePeriod = obj.optLong(ShareChatInfo.LIVE_PERIOD_KEY)
currentMessageLimit = obj.optLong(ShareChatInfo.LIMIT_KEY)
updateTextMessageId = obj.optInt(ShareChatInfo.UPDATE_TEXT_MESSAGE_ID_KEY)
currentMapMessageId = obj.optLong(ShareChatInfo.CURRENT_MAP_MESSAGE_ID_KEY)
currentTextMessageId = obj.optLong(ShareChatInfo.CURRENT_TEXT_MESSAGE_ID_KEY)
2018-10-08 17:33:59 +02:00
userSetLivePeriod = obj.optLong(ShareChatInfo.USER_SET_LIVE_PERIOD_KEY)
userSetLivePeriodStart = obj.optLong(ShareChatInfo.USER_SET_LIVE_PERIOD_START_KEY)
lastTextSuccessfulSendTime = obj.optLong(ShareChatInfo.LAST_TEXT_SUCCESSFUL_SEND_TIME_KEY)
lastMapSuccessfulSendTime = obj.optLong(ShareChatInfo.LAST_MAP_SUCCESSFUL_SEND_TIME_KEY)
lastSendMapMessageTime = obj.optInt(ShareChatInfo.LAST_SEND_MAP_TIME_KEY)
lastSendTextMessageTime = obj.optInt(ShareChatInfo.LAST_SEND_TEXT_TIME_KEY)
2019-01-29 18:03:44 +01:00
pendingTextMessage = obj.optBoolean(ShareChatInfo.PENDING_TEXT_MESSAGE_KEY)
pendingMapMessage = obj.optBoolean(ShareChatInfo.PENDING_MAP_MESSAGE_KEY)
sentMessages = obj.optInt(ShareChatInfo.SENT_MESSAGES_KEY)
2018-10-08 17:33:59 +02:00
}
shareChatsInfo[shareInfo.chatId] = shareInfo
}
}
2019-04-29 11:11:35 +02:00
private fun parseProxyPreferences(jsonObject: JSONObject) {
val proxyId = jsonObject.optInt(ProxyPref.PROXY_ID)
val typeString = jsonObject.optString(ProxyPref.TYPE_ID)
val server = jsonObject.optString(ProxyPref.SERVER_ID)
val port = jsonObject.optInt(ProxyPref.PORT_ID)
val proxyPref = when {
ProxyType.valueOf(typeString) == ProxyType.MTPROTO -> {
val key = jsonObject.optString(ProxyMTProtoPref.KEY_ID)
ProxyMTProtoPref(proxyId, server, port, key)
}
ProxyType.valueOf(typeString) == ProxyType.SOCKS5 -> {
val login = jsonObject.optString(ProxySOCKS5Pref.LOGIN_ID)
val password = jsonObject.optString(ProxySOCKS5Pref.PASSWORD_ID)
ProxySOCKS5Pref(proxyId, server, port, login, password)
}
else -> null
}
if (proxyPref != null) {
currentProxyPref = proxyPref
}
}
2019-07-15 14:09:29 +02:00
private fun parseLiveTracks(json: JSONArray) {
val list = mutableListOf<LiveTrackInfo>()
for (i in 0 until json.length()) {
val obj = json.getJSONObject(i)
val userId = obj.optInt(LiveTrackInfo.USER_ID)
val chatId = obj.optLong(LiveTrackInfo.CHAT_ID)
val deviceName = obj.optString(LiveTrackInfo.DEVICE_NAME)
val colorIndex = obj.optInt(LiveTrackInfo.COLOR_INDEX)
2019-07-15 14:09:29 +02:00
list.add(LiveTrackInfo(userId, chatId, deviceName, colorIndex))
2019-07-15 14:09:29 +02:00
}
liveTracksInfo = list.toList()
2019-07-15 14:09:29 +02:00
}
private fun parseShareDevices(json: String) {
shareDevices = OsmandApiUtils.parseJsonContents(json).toHashSet()
}
2019-12-23 12:06:01 +01:00
private fun parseLastChatsInfo(json: JSONArray) {
for (i in 0 until json.length()) {
val obj = json.getJSONObject(i)
val lastInfo = LastChatInfo().apply {
chatId = obj.optLong(LastChatInfo.CHAT_ID_KEY)
2019-12-27 18:20:30 +01:00
// livePeriod = obj.optLong(LastChatInfo.LIVE_PERIOD_KEY)
periods = LinkedList<Long>()
val jsonArray = obj.getJSONArray(LastChatInfo.PERIODS_KEY)
log.info("getJson: $jsonArray")
for (j in 0 until jsonArray.length()) {
val obj=jsonArray.get(j) as JSONObject
periods.add(obj.optLong(j.toString()))
}
log.info("Periods: $periods")
livePeriod = calcLivePeriod(periods)
2019-12-23 12:06:01 +01:00
}
lastChatsInfo[lastInfo.chatId] = lastInfo
}
}
2019-12-27 18:20:30 +01:00
fun addTimePeriodToLastItem(id: Long, time: Long) {
if (lastChatsInfo.containsKey(id)) {
lastChatsInfo[id]?.periods = addTimeToPeriods(lastChatsInfo[id]?.periods, time)
} else {
lastChatsInfo[id] = LastChatInfo().apply {
chatId = id
livePeriod = time
periods = LinkedList<Long>().apply {
addFirst(time)
}
}
}
}
private fun addTimeToPeriods(periods: LinkedList<Long>?, time: Long): LinkedList<Long> {
if (periods?.isNotEmpty() != null) {
return if (periods.count() < 5) {
periods.addFirst(time)
periods
} else {
periods.removeLast()
periods.addFirst(time)
periods
}
}
return LinkedList<Long>().apply { addFirst(time) }
}
private fun calcLivePeriod(periods: LinkedList<Long>): Long {
periods.sort()
return if (periods.count() % 2 == 0) {
(periods[periods.count() / 2] + periods[periods.count() / 2 - 1]) / 2
} else {
periods[periods.count() / 2]
}
}
2018-09-05 11:58:32 +02:00
private fun getLiveNowChats() = app.telegramHelper.getMessagesByChatIds(locHistoryTime).keys
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.putBoolean(TITLES_REPLACED_WITH_IDS, true)
edit.apply()
}
}
inner class SendMyLocPref : ListPreference(
R.drawable.ic_action_share_location,
R.string.send_my_location,
R.string.send_my_location_desc
) {
override fun getCurrentValue() =
OsmandFormatter.getFormattedDuration(app, sendMyLocInterval)
override fun setCurrentValue(index: Int) {
sendMyLocInterval = SEND_MY_LOC_VALUES_SEC[index]
app.updateSendLocationInterval()
}
override fun getMenuItems() =
SEND_MY_LOC_VALUES_SEC.map { OsmandFormatter.getFormattedDuration(app, it) }
}
inner class StaleLocPref : ListPreference(
R.drawable.ic_action_time_span,
R.string.stale_location,
R.string.stale_location_desc
) {
override fun getCurrentValue() =
OsmandFormatter.getFormattedDuration(app, staleLocTime)
override fun setCurrentValue(index: Int) {
staleLocTime = STALE_LOC_VALUES_SEC[index]
}
override fun getMenuItems() =
STALE_LOC_VALUES_SEC.map { OsmandFormatter.getFormattedDuration(app, it) }
}
inner class LocHistoryPref : ListPreference(
R.drawable.ic_action_location_history,
R.string.location_history,
R.string.location_history_desc
) {
override fun getCurrentValue() =
OsmandFormatter.getFormattedDuration(app, locHistoryTime)
override fun setCurrentValue(index: Int) {
val value = LOC_HISTORY_VALUES_SEC[index]
locHistoryTime = value
app.telegramHelper.messageActiveTimeSec = value
}
override fun getMenuItems() =
LOC_HISTORY_VALUES_SEC.map { OsmandFormatter.getFormattedDuration(app, it) }
}
inner class ShareTypePref : ListPreference(
R.drawable.ic_action_location_history,
2018-12-10 16:37:27 +01:00
R.string.send_location_as,
R.string.send_location_as_descr
) {
override fun getCurrentValue(): String {
2018-12-10 16:37:27 +01:00
return getTextValue(shareTypeValue)
}
override fun setCurrentValue(index: Int) {
val newSharingType = SHARE_TYPE_VALUES[index]
if (shareTypeValue != newSharingType && app.telegramHelper.getCurrentUser()?.id.toString() != currentSharingMode) {
shareChatsInfo.forEach { (_, shareInfo) ->
2019-02-08 18:12:17 +01:00
shareInfo.shouldSendViaBotTextMessage = true
shareInfo.shouldSendViaBotMapMessage = true
}
}
shareTypeValue = newSharingType
}
2018-12-10 16:37:27 +01:00
override fun getMenuItems(): List<String> {
return SHARE_TYPE_VALUES.map { getTextValue(it) }
}
2018-12-10 16:37:27 +01:00
private fun getTextValue(shareType: String): String {
return when (shareType) {
SHARE_TYPE_MAP -> app.getString(R.string.shared_string_map)
SHARE_TYPE_TEXT -> app.getString(R.string.shared_string_text)
SHARE_TYPE_MAP_AND_TEXT -> app.getString(R.string.map_and_text)
else -> ""
}
}
}
inner class MinLocationDistance :
ListPreference(0, R.string.min_logging_distance, R.string.min_logging_distance_descr) {
override fun getCurrentValue() = getFormattedValue(minLocationDistance)
override fun setCurrentValue(index: Int) {
2019-12-11 08:29:36 +01:00
minLocationDistance = MIN_LOCATION_DISTANCE[index]
}
override fun getMenuItems() = MIN_LOCATION_DISTANCE.map { getFormattedValue(it) }
private fun getFormattedValue(value: Float): String {
return if (value == 0f) app.getString(R.string.shared_string_select)
else OsmandFormatter.getFormattedDistance(value, app)
}
}
inner class MinLocationAccuracy :
ListPreference(0, R.string.min_logging_accuracy, R.string.min_logging_accuracy_descr) {
override fun getCurrentValue() = getFormattedValue(minLocationAccuracy)
override fun setCurrentValue(index: Int) {
2019-12-11 08:29:36 +01:00
minLocationAccuracy = MIN_LOCATION_ACCURACY[index]
}
override fun getMenuItems() = MIN_LOCATION_ACCURACY.map { getFormattedValue(it) }
private fun getFormattedValue(value: Float): String {
return if (value == 0f) app.getString(R.string.shared_string_select)
else OsmandFormatter.getFormattedDistance(value, app)
}
}
inner class MinLocationSpeed :
ListPreference(0, R.string.min_logging_speed, R.string.min_logging_speed_descr) {
override fun getCurrentValue() = getFormattedValue(minLocationSpeed)
override fun setCurrentValue(index: Int) {
2019-12-11 08:29:36 +01:00
minLocationSpeed = MIN_LOCATION_SPEED[index]
}
override fun getMenuItems() = MIN_LOCATION_SPEED.map { getFormattedValue(it) }
private fun getFormattedValue(value: Float): String {
return when (value) {
MIN_LOCATION_SPEED[0] -> app.getString(R.string.shared_string_select)
MIN_LOCATION_SPEED[1] -> "> 0"
else -> OsmandFormatter.getFormattedSpeed(value, app)
}
}
}
inner class UnitsOfSpeed : ListPreference(
R.drawable.ic_action_speed,
R.string.unit_of_speed_system,
R.string.unit_of_speed_system_descr
) {
override fun getCurrentValue() = speedConstants.toShortString(app)
override fun setCurrentValue(index: Int) {
speedConstants = SpeedConstants.values()[index]
}
override fun getMenuItems() = SpeedConstants.values().map { it.toShortString(app) }
}
inner class UnitsOfLength : ListPreference(
R.drawable.ic_action_ruler_unit, R.string.unit_of_length,
R.string.unit_of_length_descr
) {
override fun getCurrentValue() = metricsConstants.toHumanString(app)
override fun setCurrentValue(index: Int) {
metricsConstants = MetricsConstants.values()[index]
}
override fun getMenuItems() = MetricsConstants.values().map { it.toHumanString(app) }
}
2019-12-06 13:41:42 +01:00
inner class UtcOffset : ListPreference(
R.drawable.ic_world_globe_dark, R.string.time_zone,
R.string.time_zone_descr
) {
2019-12-09 16:33:20 +01:00
private val formattedUtcOffsets = DataConstants.utcOffsets.keys.toList()
2019-12-06 13:41:42 +01:00
override fun getCurrentValue() = utcOffset
override fun setCurrentValue(index: Int) {
2019-12-09 16:33:20 +01:00
utcOffset = formattedUtcOffsets[index]
2019-12-06 13:41:42 +01:00
}
2019-12-09 16:33:20 +01:00
override fun getMenuItems() = formattedUtcOffsets
2019-12-06 13:41:42 +01:00
}
abstract inner class ListPreference(
@DrawableRes val iconId: Int,
@StringRes val titleId: Int,
@StringRes val descriptionId: Int
) {
abstract fun getCurrentValue(): String
abstract fun setCurrentValue(index: Int)
abstract fun getMenuItems(): List<String>
}
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(
2018-09-03 14:58:51 +02:00
R.drawable.ic_logo_osmand_nightly,
2018-09-03 13:30:40 +02:00
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
}
@DrawableRes
fun getIconId(appPackage: String): Int {
for (item in values()) {
if (item.appPackage == appPackage) {
return item.iconId
}
}
return 0
}
fun getInstalledApps(context: Context) =
values().filter { AndroidUtils.isAppInstalled(context, it.appPackage) }
}
}
2018-09-04 16:43:42 +02:00
enum class LiveNowSortType(
@DrawableRes val iconId: Int,
@StringRes val titleId: Int,
@StringRes val shortTitleId: Int
) {
2018-09-04 16:43:42 +02:00
SORT_BY_GROUP(
R.drawable.ic_action_sort_by_group,
R.string.shared_string_group,
R.string.by_group
),
SORT_BY_NAME(
R.drawable.ic_action_sort_by_name,
R.string.shared_string_name,
R.string.by_name
),
SORT_BY_DISTANCE(
R.drawable.ic_action_sort_by_distance,
R.string.shared_string_distance,
R.string.by_distance
);
fun isSortByGroup() = this == SORT_BY_GROUP
}
2018-10-02 12:01:26 +02:00
2018-10-12 18:17:38 +02:00
enum class SharingStatusType(
@DrawableRes val iconId: Int,
@ColorRes val iconColorRes: Int,
val canResendLocation: Boolean
) {
NO_INTERNET(
R.drawable.ic_action_wifi_off,
R.color.sharing_status_icon_error,
true
),
2018-10-23 11:21:15 +02:00
SENDING(
R.drawable.ic_action_share_location,
R.color.sharing_status_icon_success,
2018-10-12 18:17:38 +02:00
false
),
NOT_POSSIBLE_TO_SENT_TO_CHATS(
R.drawable.ic_action_message_send_error,
R.color.sharing_status_icon_error,
true
),
NO_GPS(
R.drawable.ic_action_location_off,
R.color.sharing_status_icon_error,
2018-10-23 11:21:15 +02:00
false
),
INITIALIZING(
R.drawable.ic_action_connect,
R.color.sharing_status_icon_error,
2018-10-12 18:17:38 +02:00
false
);
}
2018-10-02 12:01:26 +02:00
class DeviceBot {
var id: Long = -1
var userId: Long = -1
var chatId: Long = -1
var deviceName: String = ""
var externalId: String = ""
var data: String = ""
companion object {
internal const val DEVICE_ID = "id"
internal const val USER_ID = "userId"
internal const val CHAT_ID = "chatId"
internal const val DEVICE_NAME = "deviceName"
internal const val EXTERNAL_ID = "externalId"
internal const val DATA = "data"
}
2018-10-02 12:01:26 +02:00
}
2018-10-08 17:33:59 +02:00
data class LiveTrackInfo(val userId: Int, val chatId: Long, val deviceName: String, val colorIndex: Int) {
2019-07-15 14:09:29 +02:00
companion object {
internal const val USER_ID = "userId"
internal const val CHAT_ID = "chatId"
internal const val DEVICE_NAME = "deviceName"
internal const val COLOR_INDEX = "colorIndex"
2019-07-15 14:09:29 +02:00
}
}
2019-04-29 11:11:35 +02:00
enum class ProxyType {
MTPROTO, SOCKS5
}
abstract class ProxyPref(
var id: Int,
var type: ProxyType,
open var server: String,
open var port: Int
) {
companion object {
internal const val PROXY_ID = "proxyId"
internal const val TYPE_ID = "type"
internal const val SERVER_ID = "serverId"
internal const val PORT_ID = "portId"
}
}
class ProxyMTProtoPref(id: Int, server: String, port: Int, var key: String) :
ProxyPref(id, ProxyType.MTPROTO, server, port) {
companion object {
internal const val KEY_ID = "key"
}
}
class ProxySOCKS5Pref(
id: Int,
server: String,
port: Int,
var login: String,
var password: String
) :
ProxyPref(id, ProxyType.SOCKS5, server, port) {
companion object {
internal const val LOGIN_ID = "login"
internal const val PASSWORD_ID = "password"
}
}
2018-10-12 18:17:38 +02:00
class SharingStatus {
2018-10-23 11:21:15 +02:00
var title: String = ""
var description: String = ""
2018-10-12 18:17:38 +02:00
var locationTime: Long = -1
var statusChangeTime: Long = -1
var chatsIds: MutableList<Long> = mutableListOf()
2018-10-23 11:21:15 +02:00
2018-10-12 18:17:38 +02:00
lateinit var statusType: SharingStatusType
2018-10-23 11:21:15 +02:00
fun getTitle(app: TelegramApplication): CharSequence {
return if (statusType != SharingStatusType.NOT_POSSIBLE_TO_SENT_TO_CHATS || chatsIds.isEmpty()) {
2018-10-23 11:21:15 +02:00
title
2018-10-12 18:17:38 +02:00
} else {
2018-10-23 11:21:15 +02:00
val spannableString = SpannableStringBuilder(title)
val iterator = chatsIds.iterator()
2018-10-12 18:17:38 +02:00
while (iterator.hasNext()) {
val chatId = iterator.next()
val chatTitle = app.telegramHelper.getChat(chatId)?.title
if (chatTitle != null) {
val start = spannableString.length
val newSpannable = if (iterator.hasNext()) " @$chatTitle," else " @$chatTitle."
spannableString.append(newSpannable)
spannableString.setSpan(ForegroundColorSpan(app.uiUtils.getActiveColor()), start, spannableString.length - 1, 0)
}
2018-10-12 18:17:38 +02:00
}
spannableString
}
}
}
2018-10-08 17:33:59 +02:00
class ShareChatInfo {
var chatId = -1L
var userId = -1
2018-10-08 17:33:59 +02:00
var start = -1L
var livePeriod = -1L
var updateTextMessageId = 1
2018-10-08 17:33:59 +02:00
var currentMessageLimit = -1L
var currentMapMessageId = -1L
2019-01-28 17:09:35 +01:00
var oldMapMessageId = -1L
var currentTextMessageId = -1L
2019-01-28 17:09:35 +01:00
var oldTextMessageId = -1L
2018-10-08 17:33:59 +02:00
var userSetLivePeriod = -1L
var userSetLivePeriodStart = -1L
var lastTextSuccessfulSendTime = -1L
var lastMapSuccessfulSendTime = -1L
var lastSendTextMessageTime = -1
var lastSendMapMessageTime = -1
2019-01-29 18:03:44 +01:00
var sentMessages = 0
2019-02-08 18:12:17 +01:00
var pendingTdLibText = 0
var pendingTdLibMap = 0
var pendingTextMessage = false
var pendingMapMessage = false
2019-02-08 18:12:17 +01:00
var shouldSendViaBotTextMessage = false
var shouldSendViaBotMapMessage = false
2018-10-16 17:42:43 +02:00
var hasSharingError = false
var additionalActiveTime = ADDITIONAL_ACTIVE_TIME_VALUES_SEC[0]
2018-10-16 17:42:43 +02:00
fun getNextAdditionalActiveTime(): Long {
var index = ADDITIONAL_ACTIVE_TIME_VALUES_SEC.indexOf(additionalActiveTime)
return if (ADDITIONAL_ACTIVE_TIME_VALUES_SEC.lastIndex > index) {
ADDITIONAL_ACTIVE_TIME_VALUES_SEC[++index]
} else {
ADDITIONAL_ACTIVE_TIME_VALUES_SEC[index]
}
}
fun getChatLiveMessageExpireTime(): Long {
return userSetLivePeriod - ((System.currentTimeMillis() / 1000) - start)
}
2018-10-08 17:33:59 +02:00
companion object {
internal const val CHAT_ID_KEY = "chatId"
internal const val USER_ID_KEY = "userId"
2018-10-08 17:33:59 +02:00
internal const val START_KEY = "start"
internal const val LIVE_PERIOD_KEY = "livePeriod"
internal const val LIMIT_KEY = "limit"
internal const val UPDATE_TEXT_MESSAGE_ID_KEY = "updateTextMessageId"
internal const val CURRENT_MAP_MESSAGE_ID_KEY = "currentMapMessageId"
internal const val CURRENT_TEXT_MESSAGE_ID_KEY = "currentTextMessageId"
2018-10-08 17:33:59 +02:00
internal const val USER_SET_LIVE_PERIOD_KEY = "userSetLivePeriod"
internal const val USER_SET_LIVE_PERIOD_START_KEY = "userSetLivePeriodStart"
internal const val LAST_MAP_SUCCESSFUL_SEND_TIME_KEY = "lastMapSuccessfulSendTime"
internal const val LAST_TEXT_SUCCESSFUL_SEND_TIME_KEY = "lastTextSuccessfulSendTime"
internal const val LAST_SEND_MAP_TIME_KEY = "lastSendMapMessageTime"
internal const val LAST_SEND_TEXT_TIME_KEY = "lastSendTextMessageTime"
2019-01-29 18:03:44 +01:00
internal const val PENDING_TEXT_MESSAGE_KEY = "pendingTextMessage"
internal const val PENDING_MAP_MESSAGE_KEY = "pendingMapMessage"
internal const val SENT_MESSAGES_KEY = "sentMessages"
2018-10-08 17:33:59 +02:00
}
}
2019-12-23 12:06:01 +01:00
class LastChatInfo {
var chatId = -1L
var livePeriod = -1L
2019-12-27 18:20:30 +01:00
var periods = LinkedList<Long>()
2019-12-23 12:06:01 +01:00
companion object {
internal const val CHAT_ID_KEY = "chatId"
internal const val LIVE_PERIOD_KEY = "livePeriod"
2019-12-27 18:20:30 +01:00
internal const val PERIODS_KEY = "periods"
2019-12-23 12:06:01 +01:00
}
}
2018-10-08 17:33:59 +02:00
}