diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt index dc38030779..eddcb80acb 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt @@ -72,6 +72,9 @@ class TelegramApplication : Application(), OsmandHelperListener { if (settings.hasAnyChatToShareLocation() && AndroidUtils.isLocationPermissionAvailable(this)) { shareLocationHelper.startSharingLocation() } + if (settings.monitoringEnabled) { + showLocationHelper.startShowingLocation() + } } fun cleanupResources() { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/LocationMessages.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/LocationMessages.kt index cc1c9549c7..421ada72bf 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/LocationMessages.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/LocationMessages.kt @@ -103,6 +103,10 @@ class LocationMessages(val app: TelegramApplication) { return bufferedMessages.size } + fun getBufferedMessagesCountForChat(chatId: Long): Int { + return bufferedMessages.count { it.chatId == chatId } + } + private fun readBufferedMessages() { this.bufferedMessages = dbHelper.getBufferedMessages() } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt index 412a05f97c..9608affa9e 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt @@ -126,6 +126,7 @@ class OsmandAidlHelper(private val app: TelegramApplication) { this.mUpdatesListener = mUpdatesListener } + fun updatesCallbackRegistered() = osmandCallbackId > 0 /** * Class for interacting with the main interface of the service. */ @@ -1081,7 +1082,11 @@ class OsmandAidlHelper(private val app: TelegramApplication) { fun unregisterFromUpdates(): Boolean { if (mIOsmAndAidlInterface != null) { try { - return mIOsmAndAidlInterface!!.unregisterFromUpdates(osmandCallbackId) + val unregistered = mIOsmAndAidlInterface!!.unregisterFromUpdates(osmandCallbackId) + if (unregistered) { + osmandCallbackId = 0 + } + return unregistered } catch (e: RemoteException) { e.printStackTrace() } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt index 810347e536..d36e899557 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt @@ -11,8 +11,8 @@ import net.osmand.telegram.TelegramApplication import net.osmand.telegram.helpers.TelegramUiHelper.ListItem import net.osmand.telegram.utils.AndroidUtils import net.osmand.telegram.utils.OsmandLocationUtils -import net.osmand.telegram.utils.OsmandLocationUtils.MessageUserLocation import net.osmand.telegram.utils.OsmandLocationUtils.MessageOsmAndBotLocation +import net.osmand.telegram.utils.OsmandLocationUtils.MessageUserLocation import org.drinkless.td.libcore.telegram.TdApi import java.io.File import java.util.concurrent.Executors @@ -171,14 +171,32 @@ class ShowLocationHelper(private val app: TelegramApplication) { forcedStop = force if (showingLocation) { showingLocation = false - if (isUseOsmandCallback()) { + if (isUseOsmandCallback() && app.osmandAidlHelper.updatesCallbackRegistered()) { osmandAidlHelper.unregisterFromUpdates() - } else { + } else if (!app.settings.monitoringEnabled) { app.stopUserLocationService() } } } + fun changeUpdatesType() { + if (!forcedStop) { + if (app.settings.monitoringEnabled) { + if (app.osmandAidlHelper.updatesCallbackRegistered()) { + osmandAidlHelper.unregisterFromUpdates() + } + app.startUserLocationService() + } else { + if (isUseOsmandCallback()) { + app.stopUserLocationService() + osmandAidlHelper.registerForUpdates() + } else { + app.startUserLocationService() + } + } + } + } + fun isUseOsmandCallback(): Boolean { val version = AndroidUtils.getAppVersionCode(app, app.settings.appToConnectPackage) return version >= MIN_OSMAND_CALLBACK_VERSION_CODE diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index 93376ebc2b..a6e23f6f03 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -1309,6 +1309,10 @@ class TelegramHelper private constructor() { } } } + TdApi.UpdateMessageEdited.CONSTRUCTOR -> { + val updateMessageEdited = obj as TdApi.UpdateMessageEdited + lastTelegramUpdateTime = Math.max(lastTelegramUpdateTime, updateMessageEdited.editDate) + } TdApi.UpdateMessageContent.CONSTRUCTOR -> { val updateMessageContent = obj as TdApi.UpdateMessageContent val message = usersLocationMessages[updateMessageContent.messageId] @@ -1319,7 +1323,7 @@ class TelegramHelper private constructor() { } } else { synchronized(message) { - lastTelegramUpdateTime = Math.max(message.date, message.editDate) + lastTelegramUpdateTime = Math.max(lastTelegramUpdateTime, Math.max(message.date, message.editDate)) val newContent = updateMessageContent.newContent val fromBot = isOsmAndBot(message.senderUserId) val viaBot = isOsmAndBot(message.viaBotUserId) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt index 7e2af580a0..312752229f 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt @@ -720,7 +720,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener { } holder.gpsPointsSent?.apply { if (shareInfo != null) { - text = getString(R.string.gps_points_in_buffer,shareInfo.pendingTdLib + app.locationMessages.getBufferedMessagesCount()) + text = getString(R.string.gps_points_in_buffer, shareInfo.pendingTdLib + app.locationMessages.getBufferedMessagesCountForChat(shareInfo.chatId)) } } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/TimelineTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/TimelineTabFragment.kt index 83b7c9b7ea..721b885056 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/TimelineTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/TimelineTabFragment.kt @@ -75,10 +75,10 @@ class TimelineTabFragment : Fragment() { monitoringTv.setText(if (settings.monitoringEnabled) R.string.monitoring_is_enabled else R.string.monitoring_is_disabled) mainView.findViewById(R.id.monitoring_container).setOnClickListener { - val monitoringEnabled = !settings.monitoringEnabled - settings.monitoringEnabled = monitoringEnabled - switcher.isChecked = monitoringEnabled - monitoringTv.setText(if (monitoringEnabled) R.string.monitoring_is_enabled else R.string.monitoring_is_disabled) + settings.monitoringEnabled = !settings.monitoringEnabled + app.showLocationHelper.changeUpdatesType() + switcher.isChecked = settings.monitoringEnabled + monitoringTv.setText(if (settings.monitoringEnabled) R.string.monitoring_is_enabled else R.string.monitoring_is_disabled) } dateBtn = mainView.findViewById(R.id.date_btn).apply { diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java index 949b589fe5..d33222e9ab 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java @@ -684,8 +684,7 @@ public class OsmandAidlService extends Service { @Override public boolean unregisterFromUpdates(long callbackId) throws RemoteException { - callbacks.remove(callbackId); - return true; + return callbacks.remove(callbackId) != null; } @Override