Fix points update via callback

This commit is contained in:
Vitaliy 2020-01-29 12:07:34 +02:00
parent c590a42e8a
commit d8012120c0
4 changed files with 43 additions and 17 deletions

View file

@ -7,6 +7,11 @@ import android.content.Intent
class InitAppBroadcastReceiver : BroadcastReceiver() { class InitAppBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
// do nothing, TelegramApplication already initialized // check if aidl connection was lost
val app = context.applicationContext as TelegramApplication
val aidlHelper = app.osmandAidlHelper
if (aidlHelper.isOsmandBound() && !aidlHelper.isOsmandConnected()) {
aidlHelper.connectOsmand()
}
} }
} }

View file

@ -42,7 +42,7 @@ class TelegramApplication : Application() {
telegramHelper.messageActiveTimeSec = settings.locHistoryTime telegramHelper.messageActiveTimeSec = settings.locHistoryTime
uiUtils = UiUtils(this) uiUtils = UiUtils(this)
osmandAidlHelper = OsmandAidlHelper(this) osmandAidlHelper = OsmandAidlHelper(this)
osmandAidlHelper.listener = object : OsmandHelperListener { osmandAidlHelper.addConnectionListener(object : OsmandHelperListener {
override fun onOsmandConnectionStateChanged(connected: Boolean) { override fun onOsmandConnectionStateChanged(connected: Boolean) {
if (connected) { if (connected) {
osmandAidlHelper.clearNavDrawerItems("net.osmand.telegram") osmandAidlHelper.clearNavDrawerItems("net.osmand.telegram")
@ -60,7 +60,7 @@ class TelegramApplication : Application() {
showLocationHelper.addOrUpdateStatusWidget(-1, false) showLocationHelper.addOrUpdateStatusWidget(-1, false)
} }
} }
} })
osmandAidlHelper.setUpdatesListener(object : UpdatesListener { osmandAidlHelper.setUpdatesListener(object : UpdatesListener {
override fun update() { override fun update() {
if (settings.hasAnyChatToShowOnMap()) { if (settings.hasAnyChatToShowOnMap()) {

View file

@ -58,6 +58,8 @@ class OsmandAidlHelper(private val app: TelegramApplication) {
const val OSMAND_NIGHTLY_PACKAGE_NAME = "net.osmand.dev" const val OSMAND_NIGHTLY_PACKAGE_NAME = "net.osmand.dev"
const val UPDATE_TIME_MS = 5000L const val UPDATE_TIME_MS = 5000L
private const val CALLBACK_INVALID_ID = -1L
} }
private var mIOsmAndAidlInterface: IOsmAndAidlInterface? = null private var mIOsmAndAidlInterface: IOsmAndAidlInterface? = null
@ -68,7 +70,7 @@ class OsmandAidlHelper(private val app: TelegramApplication) {
private var osmandUpdatesCallbackId: Long = -1 private var osmandUpdatesCallbackId: Long = -1
private var osmandContextMenuCallbackId: Long = -1 private var osmandContextMenuCallbackId: Long = -1
var listener: OsmandHelperListener? = null private val connectionListeners = HashSet<OsmandHelperListener>()
interface OsmandHelperListener { interface OsmandHelperListener {
fun onOsmandConnectionStateChanged(connected: Boolean) fun onOsmandConnectionStateChanged(connected: Boolean)
@ -133,6 +135,14 @@ class OsmandAidlHelper(private val app: TelegramApplication) {
} }
} }
fun addConnectionListener(listener: OsmandHelperListener) {
connectionListeners.add(listener)
}
fun removeConnectionListener(listener: OsmandHelperListener) {
connectionListeners.remove(listener)
}
fun setSearchCompleteListener(mSearchCompleteListener: SearchCompleteListener) { fun setSearchCompleteListener(mSearchCompleteListener: SearchCompleteListener) {
this.mSearchCompleteListener = mSearchCompleteListener this.mSearchCompleteListener = mSearchCompleteListener
} }
@ -155,7 +165,7 @@ class OsmandAidlHelper(private val app: TelegramApplication) {
this.mUpdatesListener = mUpdatesListener this.mUpdatesListener = mUpdatesListener
} }
fun updatesCallbackRegistered() = osmandUpdatesCallbackId > 0 fun updatesCallbackRegistered() = osmandUpdatesCallbackId > CALLBACK_INVALID_ID
/** /**
* Class for interacting with the main interface of the service. * Class for interacting with the main interface of the service.
*/ */
@ -169,16 +179,18 @@ class OsmandAidlHelper(private val app: TelegramApplication) {
// representation of that from the raw service object. // representation of that from the raw service object.
mIOsmAndAidlInterface = IOsmAndAidlInterface.Stub.asInterface(service) mIOsmAndAidlInterface = IOsmAndAidlInterface.Stub.asInterface(service)
initialized = true initialized = true
//Toast.makeText(app, "OsmAnd connected", Toast.LENGTH_SHORT).show() connectionListeners.forEach {
listener?.onOsmandConnectionStateChanged(true) it.onOsmandConnectionStateChanged(true)
}
} }
override fun onServiceDisconnected(className: ComponentName) { override fun onServiceDisconnected(className: ComponentName) {
// This is called when the connection with the service has been // This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed. // unexpectedly disconnected -- that is, its process crashed.
mIOsmAndAidlInterface = null mIOsmAndAidlInterface = null
//Toast.makeText(app, "OsmAnd disconnected", Toast.LENGTH_SHORT).show() connectionListeners.forEach {
listener?.onOsmandConnectionStateChanged(false) it.onOsmandConnectionStateChanged(false)
}
} }
} }
@ -193,7 +205,7 @@ class OsmandAidlHelper(private val app: TelegramApplication) {
fun isOsmandConnected(): Boolean { fun isOsmandConnected(): Boolean {
return mIOsmAndAidlInterface != null return mIOsmAndAidlInterface != null
} }
/** /**
* Get list of active GPX files. * Get list of active GPX files.
* *
@ -1162,20 +1174,20 @@ class OsmandAidlHelper(private val app: TelegramApplication) {
if (mIOsmAndAidlInterface != null) { if (mIOsmAndAidlInterface != null) {
try { try {
osmandUpdatesCallbackId = mIOsmAndAidlInterface!!.registerForUpdates(UPDATE_TIME_MS, mIOsmAndAidlCallback) osmandUpdatesCallbackId = mIOsmAndAidlInterface!!.registerForUpdates(UPDATE_TIME_MS, mIOsmAndAidlCallback)
return osmandUpdatesCallbackId > 0 return osmandUpdatesCallbackId > CALLBACK_INVALID_ID
} catch (e: RemoteException) { } catch (e: RemoteException) {
e.printStackTrace() e.printStackTrace()
} }
} }
return false return false
} }
fun unregisterFromUpdates(): Boolean { fun unregisterFromUpdates(): Boolean {
if (mIOsmAndAidlInterface != null) { if (mIOsmAndAidlInterface != null) {
try { try {
val unregistered = mIOsmAndAidlInterface!!.unregisterFromUpdates(osmandUpdatesCallbackId) val unregistered = mIOsmAndAidlInterface!!.unregisterFromUpdates(osmandUpdatesCallbackId)
if (unregistered) { if (unregistered) {
osmandUpdatesCallbackId = 0 osmandUpdatesCallbackId = CALLBACK_INVALID_ID
} }
return unregistered return unregistered
} catch (e: RemoteException) { } catch (e: RemoteException) {
@ -1222,7 +1234,7 @@ class OsmandAidlHelper(private val app: TelegramApplication) {
val params = RemoveContextMenuButtonsParams(paramsId, osmandContextMenuCallbackId) val params = RemoveContextMenuButtonsParams(paramsId, osmandContextMenuCallbackId)
val removed = mIOsmAndAidlInterface!!.removeContextMenuButtons(params) val removed = mIOsmAndAidlInterface!!.removeContextMenuButtons(params)
if (removed) { if (removed) {
osmandContextMenuCallbackId = -1 osmandContextMenuCallbackId = CALLBACK_INVALID_ID
} }
return removed return removed
} catch (e: RemoteException) { } catch (e: RemoteException) {

View file

@ -77,6 +77,15 @@ class ShowLocationHelper(private val app: TelegramApplication) {
private var forcedStop: Boolean = false private var forcedStop: Boolean = false
init { init {
app.osmandAidlHelper.addConnectionListener(object : OsmandAidlHelper.OsmandHelperListener {
override fun onOsmandConnectionStateChanged(connected: Boolean) {
if (!connected && showingLocation && !app.settings.monitoringEnabled) {
if (isUseOsmandCallback() && app.osmandAidlHelper.updatesCallbackRegistered()) {
showingLocation = false
}
}
}
})
app.osmandAidlHelper.setContextMenuButtonsListener(object : ContextMenuButtonsListener { app.osmandAidlHelper.setContextMenuButtonsListener(object : ContextMenuButtonsListener {
override fun onContextMenuButtonClicked(buttonId: Int, pointId: String, layerId: String) { override fun onContextMenuButtonClicked(buttonId: Int, pointId: String, layerId: String) {
@ -137,7 +146,7 @@ class ShowLocationHelper(private val app: TelegramApplication) {
osmandAidlHelper.showMapPoint(MAP_LAYER_ID, pointId, name, name, item.chatTitle, Color.WHITE, aLatLon, details, params) osmandAidlHelper.showMapPoint(MAP_LAYER_ID, pointId, name, name, item.chatTitle, Color.WHITE, aLatLon, details, params)
} }
} }
fun updateLocationsOnMap() { fun updateLocationsOnMap() {
osmandAidlHelper.execOsmandApi { osmandAidlHelper.execOsmandApi {
val messages = telegramHelper.getMessages() val messages = telegramHelper.getMessages()
@ -427,7 +436,7 @@ class ShowLocationHelper(private val app: TelegramApplication) {
forcedStop = force forcedStop = force
if (showingLocation) { if (showingLocation) {
showingLocation = false showingLocation = false
if (isUseOsmandCallback() && app.osmandAidlHelper.updatesCallbackRegistered()) { if (isUseOsmandCallback() && osmandAidlHelper.updatesCallbackRegistered()) {
osmandAidlHelper.unregisterFromUpdates() osmandAidlHelper.unregisterFromUpdates()
} else if (!app.settings.monitoringEnabled) { } else if (!app.settings.monitoringEnabled) {
app.stopUserLocationService() app.stopUserLocationService()