Add ability to change updates type from callback to service and back and fix last update time

This commit is contained in:
Chumva 2019-02-06 13:50:02 +02:00
parent 56e6c31bc1
commit 355b7f46ac
8 changed files with 45 additions and 12 deletions

View file

@ -72,6 +72,9 @@ class TelegramApplication : Application(), OsmandHelperListener {
if (settings.hasAnyChatToShareLocation() && AndroidUtils.isLocationPermissionAvailable(this)) {
shareLocationHelper.startSharingLocation()
}
if (settings.monitoringEnabled) {
showLocationHelper.startShowingLocation()
}
}
fun cleanupResources() {

View file

@ -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()
}

View file

@ -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()
}

View file

@ -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

View file

@ -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)

View file

@ -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))
}
}
}

View file

@ -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<View>(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<TextView>(R.id.date_btn).apply {

View file

@ -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