From 7f04fd37d95a3555096802c347d569b58218056e Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 9 Oct 2018 16:12:52 +0300 Subject: [PATCH] Remove ScheduledExecutorService from ShareLocationHelper --- .../net/osmand/telegram/TelegramService.kt | 22 +++++++++++++++++ .../telegram/helpers/ShareLocationHelper.kt | 24 ++++--------------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt index d1cf9b5a36..fdb8c97897 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt @@ -20,6 +20,8 @@ import net.osmand.telegram.utils.AndroidUtils import org.drinkless.td.libcore.telegram.TdApi import java.util.* +private const val UPDATE_LIVE_MESSAGES_INTERVAL_MS = 10000L // 10 sec + class TelegramService : Service(), LocationListener, TelegramIncomingMessagesListener, TelegramOutgoingMessagesListener { @@ -27,6 +29,9 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis private val binder = LocationServiceBinder() private var shouldCleanupResources: Boolean = false + private var updateShareInfoHandler: Handler? = null + private var mHandlerThread = HandlerThread("SharingServiceThread") + var handler: Handler? = null private set var usedBy = 0 @@ -45,6 +50,12 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis class LocationServiceBinder : Binder() + override fun onCreate() { + super.onCreate() + mHandlerThread.start() + updateShareInfoHandler = Handler(mHandlerThread.looper) + } + override fun onBind(intent: Intent): IBinder? { return binder } @@ -83,6 +94,7 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis if (isUsedByMyLocation(usedBy)) { initLocationUpdates() + startShareInfoUpdates() } if (isUsedByUsersLocations(usedBy)) { app.telegramHelper.startLiveMessagesUpdates(app.settings.sendMyLocInterval) @@ -112,6 +124,7 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis app.telegramHelper.removeIncomingMessagesListener(this) app.telegramHelper.removeOutgoingMessagesListener(this) app.telegramService = null + mHandlerThread.quit() usedBy = 0 @@ -163,6 +176,15 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis } } + private fun startShareInfoUpdates() { + updateShareInfoHandler?.postDelayed({ + if (isUsedByMyLocation(usedBy)) { + app().shareLocationHelper.updateSendLiveMessages() + startShareInfoUpdates() + } + }, UPDATE_LIVE_MESSAGES_INTERVAL_MS) + } + @SuppressLint("MissingPermission") private fun getFirstTimeRunDefaultLocation(): net.osmand.Location? { val app = app() diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt index 36861bfbf9..f0e043e8ed 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShareLocationHelper.kt @@ -1,19 +1,16 @@ package net.osmand.telegram.helpers import net.osmand.Location +import net.osmand.PlatformUtil import net.osmand.telegram.TelegramApplication import net.osmand.telegram.notifications.TelegramNotification.NotificationType import net.osmand.telegram.utils.AndroidNetworkUtils -import java.util.concurrent.Executors -import java.util.concurrent.ScheduledExecutorService -import java.util.concurrent.TimeUnit -private const val UPDATE_LIVE_MESSAGES_INTERVAL_SEC = 10L // 10 sec private const val USER_SET_LIVE_PERIOD_DELAY_MS = 5000 // 5 sec class ShareLocationHelper(private val app: TelegramApplication) { - private var updateLiveMessagesExecutor: ScheduledExecutorService? = null + private val log = PlatformUtil.getLog(ShareLocationHelper::class.java) var sharingLocation: Boolean = false private set @@ -64,6 +61,7 @@ class ShareLocationHelper(private val app: TelegramApplication) { } fun updateSendLiveMessages() { + log.info("updateSendLiveMessages") app.settings.getChatsShareInfo().forEach { chatId, shareInfo -> val currentTime = System.currentTimeMillis() / 1000 when { @@ -102,29 +100,17 @@ class ShareLocationHelper(private val app: TelegramApplication) { sharingLocation = true app.startMyLocationService() - startLiveMessagesUpdates(UPDATE_LIVE_MESSAGES_INTERVAL_SEC) + refreshNotification() } else { app.forceUpdateMyLocation() } } - fun startLiveMessagesUpdates(interval: Long) { - stopLiveMessagesUpdates() - - updateLiveMessagesExecutor = Executors.newSingleThreadScheduledExecutor() - updateLiveMessagesExecutor?.scheduleWithFixedDelay({ updateSendLiveMessages() }, interval, interval, TimeUnit.SECONDS) - } - - fun stopLiveMessagesUpdates() { - updateLiveMessagesExecutor?.shutdown() - updateLiveMessagesExecutor?.awaitTermination(1, TimeUnit.MINUTES) - } - fun stopSharingLocation() { if (sharingLocation) { sharingLocation = false - stopLiveMessagesUpdates() + app.stopMyLocationService() lastLocation = null lastTimeInMillis = 0L