Remove ScheduledExecutorService from ShareLocationHelper

This commit is contained in:
Chumva 2018-10-09 16:12:52 +03:00
parent 9bcd87ce27
commit 7f04fd37d9
2 changed files with 27 additions and 19 deletions

View file

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

View file

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