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)) { if (settings.hasAnyChatToShareLocation() && AndroidUtils.isLocationPermissionAvailable(this)) {
shareLocationHelper.startSharingLocation() shareLocationHelper.startSharingLocation()
} }
if (settings.monitoringEnabled) {
showLocationHelper.startShowingLocation()
}
} }
fun cleanupResources() { fun cleanupResources() {

View file

@ -103,6 +103,10 @@ class LocationMessages(val app: TelegramApplication) {
return bufferedMessages.size return bufferedMessages.size
} }
fun getBufferedMessagesCountForChat(chatId: Long): Int {
return bufferedMessages.count { it.chatId == chatId }
}
private fun readBufferedMessages() { private fun readBufferedMessages() {
this.bufferedMessages = dbHelper.getBufferedMessages() this.bufferedMessages = dbHelper.getBufferedMessages()
} }

View file

@ -126,6 +126,7 @@ class OsmandAidlHelper(private val app: TelegramApplication) {
this.mUpdatesListener = mUpdatesListener this.mUpdatesListener = mUpdatesListener
} }
fun updatesCallbackRegistered() = osmandCallbackId > 0
/** /**
* Class for interacting with the main interface of the service. * Class for interacting with the main interface of the service.
*/ */
@ -1081,7 +1082,11 @@ class OsmandAidlHelper(private val app: TelegramApplication) {
fun unregisterFromUpdates(): Boolean { fun unregisterFromUpdates(): Boolean {
if (mIOsmAndAidlInterface != null) { if (mIOsmAndAidlInterface != null) {
try { try {
return mIOsmAndAidlInterface!!.unregisterFromUpdates(osmandCallbackId) val unregistered = mIOsmAndAidlInterface!!.unregisterFromUpdates(osmandCallbackId)
if (unregistered) {
osmandCallbackId = 0
}
return unregistered
} catch (e: RemoteException) { } catch (e: RemoteException) {
e.printStackTrace() e.printStackTrace()
} }

View file

@ -11,8 +11,8 @@ import net.osmand.telegram.TelegramApplication
import net.osmand.telegram.helpers.TelegramUiHelper.ListItem import net.osmand.telegram.helpers.TelegramUiHelper.ListItem
import net.osmand.telegram.utils.AndroidUtils import net.osmand.telegram.utils.AndroidUtils
import net.osmand.telegram.utils.OsmandLocationUtils 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.MessageOsmAndBotLocation
import net.osmand.telegram.utils.OsmandLocationUtils.MessageUserLocation
import org.drinkless.td.libcore.telegram.TdApi import org.drinkless.td.libcore.telegram.TdApi
import java.io.File import java.io.File
import java.util.concurrent.Executors import java.util.concurrent.Executors
@ -171,14 +171,32 @@ class ShowLocationHelper(private val app: TelegramApplication) {
forcedStop = force forcedStop = force
if (showingLocation) { if (showingLocation) {
showingLocation = false showingLocation = false
if (isUseOsmandCallback()) { if (isUseOsmandCallback() && app.osmandAidlHelper.updatesCallbackRegistered()) {
osmandAidlHelper.unregisterFromUpdates() osmandAidlHelper.unregisterFromUpdates()
} else { } else if (!app.settings.monitoringEnabled) {
app.stopUserLocationService() 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 { fun isUseOsmandCallback(): Boolean {
val version = AndroidUtils.getAppVersionCode(app, app.settings.appToConnectPackage) val version = AndroidUtils.getAppVersionCode(app, app.settings.appToConnectPackage)
return version >= MIN_OSMAND_CALLBACK_VERSION_CODE 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 -> { TdApi.UpdateMessageContent.CONSTRUCTOR -> {
val updateMessageContent = obj as TdApi.UpdateMessageContent val updateMessageContent = obj as TdApi.UpdateMessageContent
val message = usersLocationMessages[updateMessageContent.messageId] val message = usersLocationMessages[updateMessageContent.messageId]
@ -1319,7 +1323,7 @@ class TelegramHelper private constructor() {
} }
} else { } else {
synchronized(message) { synchronized(message) {
lastTelegramUpdateTime = Math.max(message.date, message.editDate) lastTelegramUpdateTime = Math.max(lastTelegramUpdateTime, Math.max(message.date, message.editDate))
val newContent = updateMessageContent.newContent val newContent = updateMessageContent.newContent
val fromBot = isOsmAndBot(message.senderUserId) val fromBot = isOsmAndBot(message.senderUserId)
val viaBot = isOsmAndBot(message.viaBotUserId) val viaBot = isOsmAndBot(message.viaBotUserId)

View file

@ -720,7 +720,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener {
} }
holder.gpsPointsSent?.apply { holder.gpsPointsSent?.apply {
if (shareInfo != null) { 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) monitoringTv.setText(if (settings.monitoringEnabled) R.string.monitoring_is_enabled else R.string.monitoring_is_disabled)
mainView.findViewById<View>(R.id.monitoring_container).setOnClickListener { mainView.findViewById<View>(R.id.monitoring_container).setOnClickListener {
val monitoringEnabled = !settings.monitoringEnabled settings.monitoringEnabled = !settings.monitoringEnabled
settings.monitoringEnabled = monitoringEnabled app.showLocationHelper.changeUpdatesType()
switcher.isChecked = monitoringEnabled switcher.isChecked = settings.monitoringEnabled
monitoringTv.setText(if (monitoringEnabled) R.string.monitoring_is_enabled else R.string.monitoring_is_disabled) 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 { dateBtn = mainView.findViewById<TextView>(R.id.date_btn).apply {

View file

@ -684,8 +684,7 @@ public class OsmandAidlService extends Service {
@Override @Override
public boolean unregisterFromUpdates(long callbackId) throws RemoteException { public boolean unregisterFromUpdates(long callbackId) throws RemoteException {
callbacks.remove(callbackId); return callbacks.remove(callbackId) != null;
return true;
} }
@Override @Override