diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index c487f33877..3745c3fe16 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -75,6 +75,8 @@ private const val BATTERY_OPTIMISATION_ASKED = "battery_optimisation_asked" private const val SHARING_INITIALIZATION_TIME = 60 * 2L // 2 minutes +private const val GPS_UPDATE_EXPIRED_TIME = 60 * 3L // 3 minutes + class TelegramSettings(private val app: TelegramApplication) { private var shareChatsInfo = ConcurrentHashMap() @@ -159,10 +161,16 @@ class TelegramSettings(private val app: TelegramApplication) { } } - fun updateShareDevicesIds(list: List) { + fun updateShareDevices(list: List) { shareDevices = list.toHashSet() } + fun addShareDevice(device: DeviceBot) { + val devices = shareDevices.toMutableList() + devices.add(device) + shareDevices = devices.toHashSet() + } + fun updateCurrentSharingMode(sharingMode: String) { if (currentSharingMode != sharingMode) { shareChatsInfo.forEach { (_, shareInfo) -> @@ -268,7 +276,12 @@ class TelegramSettings(private val app: TelegramApplication) { statusChangeTime = System.currentTimeMillis() val lm = app.getSystemService(Context.LOCATION_SERVICE) as LocationManager val gpsEnabled = try { - lm.isProviderEnabled(LocationManager.GPS_PROVIDER) + if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) { + val loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER) + loc != null && ((statusChangeTime - loc.time) / 1000) < GPS_UPDATE_EXPIRED_TIME + } else { + false + } } catch (ex: Exception) { false } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt index 71b220f91a..3860139bf5 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt @@ -64,13 +64,13 @@ class ShareLocationHelper(private val app: TelegramApplication) { object : AndroidNetworkUtils.OnRequestResultListener { override fun onResult(result: String?) { updateShareInfoSuccessfulSendTime(result, chatsShareInfo) + + val osmandBot = app.telegramHelper.getOsmandBot() + if (osmandBot != null) { + checkAndSendViaBotMessages(chatsShareInfo, TdApi.Location(latitude, longitude), osmandBot) + } } }) - - val osmandBot = app.telegramHelper.getOsmandBot() - if (osmandBot != null) { - checkAndSendViaBotMessages(chatsShareInfo, TdApi.Location(latitude, longitude), osmandBot) - } } } lastLocationMessageSentTime = System.currentTimeMillis() diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index dd30557979..a1a14880da 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -982,7 +982,9 @@ class TelegramHelper private constructor() { val updatedStr = s.removePrefix(UPDATED_PREFIX) val endIndex = updatedStr.indexOf("(") val updatedS = updatedStr.substring(0, if (endIndex != -1) endIndex else updatedStr.length) - res.lastUpdated = (parseTime(updatedS.trim()) / 1000).toInt() + val parsedTime = (parseTime(updatedS.trim()) / 1000).toInt() + val currentTime = (System.currentTimeMillis() / 1000) - 1 + res.lastUpdated = if (parsedTime < currentTime) parsedTime else currentTime.toInt() } } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/AddNewDeviceBottomSheet.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/AddNewDeviceBottomSheet.kt index 3f92948100..b562ba3024 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/AddNewDeviceBottomSheet.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/AddNewDeviceBottomSheet.kt @@ -105,10 +105,10 @@ class AddNewDeviceBottomSheet : BaseDialogFragment() { object : AndroidNetworkUtils.OnRequestResultListener { override fun onResult(result: String?) { updatePrimaryBtnAndProgress(false) - val deviceBot = getDeviceFromJson(result) - if (deviceBot != null) { + val deviceJson = getDeviceJson(result) + if (deviceJson != null) { targetFragment?.also { target -> - val intent = Intent().putExtra(DEVICE_NAME, deviceBot.deviceName).putExtra(DEVICE_EXTERNAL_ID, deviceBot.externalId) + val intent = Intent().putExtra(DEVICE_JSON, deviceJson.toString()) target.onActivityResult(targetRequestCode, NEW_DEVICE_REQUEST_CODE, intent) } dismiss() @@ -151,28 +151,23 @@ class AddNewDeviceBottomSheet : BaseDialogFragment() { } } - private fun getDeviceFromJson(json: String?): TelegramSettings.DeviceBot? { - var device: TelegramSettings.DeviceBot? = null + private fun getDeviceJson(json: String?): JSONObject? { if (json != null) { - device = try { + try { val jsonResult = JSONObject(json) val status = jsonResult.getString("status") if (status == "OK") { - OsmandApiUtils.parseDeviceBot(jsonResult.getJSONObject("device")) - } else { - null + return jsonResult.getJSONObject("device") } } catch (e: JSONException) { - null } } - return device + return null } companion object { const val NEW_DEVICE_REQUEST_CODE = 5 - const val DEVICE_NAME = "DEVICE_NAME" - const val DEVICE_EXTERNAL_ID = "DEVICE_EXTERNAL_ID" + const val DEVICE_JSON = "device_json" const val MAX_DEVICE_NAME_LENGTH = 200 private const val TAG = "AddNewDeviceBottomSheet" diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt index c7e5dabcf8..2c169ce44d 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt @@ -20,7 +20,9 @@ import net.osmand.telegram.TelegramSettings.DurationPref import net.osmand.telegram.helpers.TelegramHelper.Companion.OSMAND_BOT_USERNAME import net.osmand.telegram.helpers.TelegramUiHelper import net.osmand.telegram.utils.AndroidUtils +import net.osmand.telegram.utils.OsmandApiUtils import org.drinkless.td.libcore.telegram.TdApi +import org.json.JSONObject class SettingsDialogFragment : BaseDialogFragment() { @@ -182,14 +184,16 @@ class SettingsDialogFragment : BaseDialogFragment() { } AddNewDeviceBottomSheet.NEW_DEVICE_REQUEST_CODE -> { val user = app.telegramHelper.getCurrentUser() - if (user != null && data != null) { - val deviceName = data.getStringExtra(AddNewDeviceBottomSheet.DEVICE_NAME) - val deviceExternalId = data.getStringExtra(AddNewDeviceBottomSheet.DEVICE_EXTERNAL_ID) - - val inflater = activity?.layoutInflater - if (inflater != null && deviceName != null && deviceExternalId != null) { - addItemToContainer(inflater, shareAsContainer, deviceExternalId, deviceName) - Toast.makeText(app, getString(R.string.device_added_successfully, deviceName), Toast.LENGTH_SHORT).show() + if (user != null && data != null && data.hasExtra(AddNewDeviceBottomSheet.DEVICE_JSON)) { + val deviceJson = data.getStringExtra(AddNewDeviceBottomSheet.DEVICE_JSON) + val device = OsmandApiUtils.parseDeviceBot(JSONObject(deviceJson)) + if (device != null) { + app.settings.addShareDevice(device) + val inflater = activity?.layoutInflater + if (inflater != null) { + addItemToContainer(inflater, shareAsContainer, device.externalId, device.deviceName) + Toast.makeText(app, getString(R.string.device_added_successfully, device.deviceName), Toast.LENGTH_SHORT).show() + } } } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandApiUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandApiUtils.kt index 019d7603f0..d836158570 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandApiUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandApiUtils.kt @@ -20,7 +20,7 @@ object OsmandApiUtils { override fun onResult(result: String?) { if (result != null) { val list = parseJsonContents(result) - app.settings.updateShareDevicesIds(list) + app.settings.updateShareDevices(list) } } }