From 3e71d85235fd40f11d84eb61e88d9bc8dcc724aa Mon Sep 17 00:00:00 2001 From: crimean Date: Thu, 14 Jun 2018 15:21:58 +0300 Subject: [PATCH] Telegram - fix service --- OsmAnd-telegram/AndroidManifest.xml | 1 + .../OnTelegramServiceAlarmReceiver.kt | 26 ++++----- .../osmand/telegram/TelegramApplication.kt | 12 +++- .../net/osmand/telegram/TelegramService.kt | 57 +++++++++++-------- .../osmand/telegram/helpers/TelegramHelper.kt | 19 ++++--- 5 files changed, 66 insertions(+), 49 deletions(-) diff --git a/OsmAnd-telegram/AndroidManifest.xml b/OsmAnd-telegram/AndroidManifest.xml index a1863ff954..558d4b38df 100644 --- a/OsmAnd-telegram/AndroidManifest.xml +++ b/OsmAnd-telegram/AndroidManifest.xml @@ -41,6 +41,7 @@ + \ No newline at end of file diff --git a/OsmAnd-telegram/src/net/osmand/telegram/OnTelegramServiceAlarmReceiver.kt b/OsmAnd-telegram/src/net/osmand/telegram/OnTelegramServiceAlarmReceiver.kt index d41cbfa16e..ee2851dcb6 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/OnTelegramServiceAlarmReceiver.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/OnTelegramServiceAlarmReceiver.kt @@ -5,11 +5,9 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.location.LocationManager -import net.osmand.telegram.TelegramApplication -import net.osmand.telegram.TelegramService import net.osmand.telegram.utils.AndroidUtils -class OnNavigationServiceAlarmReceiver : BroadcastReceiver() { +class OnTelegramServiceAlarmReceiver : BroadcastReceiver() { @SuppressLint("MissingPermission") override fun onReceive(context: Context, intent: Intent) { val lock = TelegramService.getLock(context) @@ -19,23 +17,23 @@ class OnNavigationServiceAlarmReceiver : BroadcastReceiver() { if (lock.isHeld || service == null) { return } - lock.acquire(10 * 60 * 1000L /*10 minutes*/) + lock.acquire(15 * 60 * 1000L) // request location updates val locationManager = service.getSystemService(Context.LOCATION_SERVICE) as LocationManager try { if (AndroidUtils.isLocationPermissionAvailable(app)) { locationManager.requestLocationUpdates(service.serviceOffProvider, 0, 0f, service) - val handler = service.handler - if (service.serviceOffInterval > service.serviceError && handler != null) { - handler.postDelayed({ - // if lock is not anymore held - if (lock.isHeld) { - lock.release() - locationManager.removeUpdates(service) - } - }, service.serviceError) - } + } + val handler = service.handler + if (service.serviceOffInterval > service.serviceErrorInterval && handler != null) { + handler.postDelayed({ + // if lock is not anymore held + if (lock.isHeld) { + lock.release() + locationManager.removeUpdates(service) + } + }, service.serviceErrorInterval) } } catch (e: RuntimeException) { e.printStackTrace() diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt index d758f0cfda..8fe6a60d71 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt @@ -92,17 +92,24 @@ class TelegramApplication : Application(), OsmandHelperListener { showLocationHelper.setupMapLayer() } - private fun startTelegramService(intent: Int) { + private fun startTelegramService(intent: Int, serviceOffInterval: Long = 0) { var i = intent + var interval = serviceOffInterval val serviceIntent = Intent(this, TelegramService::class.java) val telegramService = telegramService if (telegramService != null) { i = intent or telegramService.usedBy + interval = if (TelegramService.isOffIntervalDepended(intent)) { + Math.min(telegramService.serviceOffInterval, interval) + } else { + telegramService.serviceOffInterval + } telegramService.stopSelf() } serviceIntent.putExtra(TelegramService.USAGE_INTENT, i) + serviceIntent.putExtra(TelegramService.USAGE_OFF_INTERVAL, interval) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(serviceIntent) } else { @@ -111,7 +118,8 @@ class TelegramApplication : Application(), OsmandHelperListener { } fun startMyLocationService() { - startTelegramService(TelegramService.USED_BY_MY_LOCATION) + val interval = settings.sendMyLocationInterval + startTelegramService(TelegramService.USED_BY_MY_LOCATION, TelegramService.normalizeOffInterval(interval)) } fun stopMyLocationService() { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt index df39894b26..21767c1680 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt @@ -32,7 +32,7 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis private set var serviceOffInterval = 0L private set - var serviceError = 0L + var serviceErrorInterval = 0L private set private var pendingIntent: PendingIntent? = null @@ -51,19 +51,14 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis shouldCleanupResources = false val serviceIntent = Intent(ctx, TelegramService::class.java) ctx.stopService(serviceIntent) - } else if (!needLocation()) { - // Issue #3604 + } else if (isUsedByMyLocation(usedBy)) { val app = app() - if (usedBy == 2 && app.settings.sendMyLocationInterval >= 30000 && serviceOffInterval == 0L) { + if (app.settings.sendMyLocationInterval >= OFF_INTERVAL_THRESHOLD && serviceOffInterval == 0L) { serviceOffInterval = app.settings.sendMyLocationInterval - // From onStartCommand: - serviceError = serviceOffInterval / 5 - serviceError = Math.min(serviceError, 12 * 60 * 1000) - serviceError = Math.max(serviceError, 30 * 1000) - serviceError = Math.min(serviceError, serviceOffInterval) + setupServiceErrorInterval() val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager - pendingIntent = PendingIntent.getBroadcast(this, 0, Intent(this, OnNavigationServiceAlarmReceiver::class.java), PendingIntent.FLAG_UPDATE_CURRENT) + pendingIntent = PendingIntent.getBroadcast(this, 0, Intent(this, OnTelegramServiceAlarmReceiver::class.java), PendingIntent.FLAG_UPDATE_CURRENT) alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 500, serviceOffInterval, pendingIntent) } app.notificationHelper.refreshNotification(NotificationType.LOCATION) @@ -76,20 +71,12 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis usedBy = intent.getIntExtra(USAGE_INTENT, 0) serviceOffInterval = intent.getLongExtra(USAGE_OFF_INTERVAL, 0) - // use only gps provider - serviceOffProvider = LocationManager.GPS_PROVIDER - serviceError = serviceOffInterval / 5 - // 1. not more than 12 mins - serviceError = Math.min(serviceError, 12 * 60 * 1000) - // 2. not less than 30 seconds - serviceError = Math.max(serviceError, 30 * 1000) - // 3. not more than serviceOffInterval - serviceError = Math.min(serviceError, serviceOffInterval) + setupServiceErrorInterval() app.telegramService = this app.telegramHelper.incomingMessagesListener = this - if (needLocation()) { + if (isUsedByMyLocation(usedBy)) { initLocationUpdates() } @@ -100,6 +87,16 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis return Service.START_REDELIVER_INTENT } + private fun setupServiceErrorInterval() { + serviceErrorInterval = serviceOffInterval / 5 + // 1. not more than 12 mins + serviceErrorInterval = Math.min(serviceErrorInterval, 12 * 60 * 1000) + // 2. not less than 30 seconds + serviceErrorInterval = Math.max(serviceErrorInterval, 30 * 1000) + // 3. not more than serviceOffInterval + serviceErrorInterval = Math.min(serviceErrorInterval, serviceOffInterval) + } + override fun onDestroy() { super.onDestroy() val app = app() @@ -141,7 +138,7 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis } } else { val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager - pendingIntent = PendingIntent.getBroadcast(this, 0, Intent(this, OnNavigationServiceAlarmReceiver::class.java), PendingIntent.FLAG_UPDATE_CURRENT) + pendingIntent = PendingIntent.getBroadcast(this, 0, Intent(this, OnTelegramServiceAlarmReceiver::class.java), PendingIntent.FLAG_UPDATE_CURRENT) alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 500, serviceOffInterval, pendingIntent) } } @@ -156,10 +153,6 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis } } - private fun needLocation(): Boolean { - return (usedBy and USED_BY_MY_LOCATION) > 0 - } - private fun isContinuous(): Boolean { return serviceOffInterval == 0L } @@ -228,6 +221,8 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis const val USAGE_INTENT = "SERVICE_USED_BY" const val USAGE_OFF_INTERVAL = "SERVICE_OFF_INTERVAL" + const val OFF_INTERVAL_THRESHOLD: Long = 30000L + private var lockStatic: PowerManager.WakeLock? = null @Synchronized @@ -243,6 +238,18 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis } } + fun isUsedByMyLocation(usedBy: Int): Boolean { + return (usedBy and USED_BY_MY_LOCATION) > 0 + } + + fun isOffIntervalDepended(usedBy: Int): Boolean { + return isUsedByMyLocation(usedBy) + } + + fun normalizeOffInterval(interval: Long): Long { + return if (interval < OFF_INTERVAL_THRESHOLD) 0 else interval + } + fun convertLocation(l: Location?): net.osmand.Location? { if (l == null) { return null diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index 3f5940c566..ee0292447e 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -230,7 +230,7 @@ class TelegramHelper private constructor() { offsetOrder = last.order offsetChatId = last.chatId } - client?.send(TdApi.GetChats(offsetOrder, offsetChatId, CHATS_LIMIT - chatList.size), { obj -> + client?.send(TdApi.GetChats(offsetOrder, offsetChatId, CHATS_LIMIT - chatList.size)) { obj -> when (obj.constructor) { TdApi.Error.CONSTRUCTOR -> { val error = obj as TdApi.Error @@ -250,7 +250,7 @@ class TelegramHelper private constructor() { } else -> listener?.onTelegramError(-1, "Receive wrong response from TDLib: $obj") } - }) + } return } } @@ -264,7 +264,7 @@ class TelegramHelper private constructor() { for (chatTitle in chatTitles) { val chatId = this.chatTitles[chatTitle] if (chatId != null) { - client?.send(TdApi.SearchChatRecentLocationMessages(chatId, CHAT_LIVE_USERS_LIMIT), { obj -> + client?.send(TdApi.SearchChatRecentLocationMessages(chatId, CHAT_LIVE_USERS_LIMIT)) { obj -> when (obj.constructor) { TdApi.Error.CONSTRUCTOR -> { val error = obj as TdApi.Error @@ -284,7 +284,7 @@ class TelegramHelper private constructor() { } else -> listener?.onTelegramError(-1, "Receive wrong response from TDLib: $obj") } - }) + } } } } @@ -313,7 +313,7 @@ class TelegramHelper private constructor() { private fun getActiveLiveLocationMessages(onComplete: (() -> Unit)?) { requestingActiveLiveLocationMessages = true - client?.send(TdApi.GetActiveLiveLocationMessages(), { obj -> + client?.send(TdApi.GetActiveLiveLocationMessages()) { obj -> when (obj.constructor) { TdApi.Error.CONSTRUCTOR -> { val error = obj as TdApi.Error @@ -336,7 +336,7 @@ class TelegramHelper private constructor() { else -> listener?.onSendLiveLicationError(-1, "Receive wrong response from TDLib: $obj") } requestingActiveLiveLocationMessages = false - }) + } } private fun sendLiveLocationImpl(chatTitles: List, livePeriod: Int = 61, latitude: Double, longitude: Double) { @@ -575,7 +575,7 @@ class TelegramHelper private constructor() { val remotePhoto = chat.photo?.small?.remote if (remotePhoto != null && remotePhoto.id.isNotEmpty()) { downloadChatFilesMap[remotePhoto.id] = chat - client!!.send(TdApi.GetRemoteFile(remotePhoto.id, null), { obj -> + client!!.send(TdApi.GetRemoteFile(remotePhoto.id, null)) { obj -> when (obj.constructor) { TdApi.Error.CONSTRUCTOR -> { val error = obj as TdApi.Error @@ -590,7 +590,7 @@ class TelegramHelper private constructor() { } else -> listener?.onTelegramError(-1, "Receive wrong response from TDLib: $obj") } - }) + } } } val order = chat.order @@ -630,6 +630,7 @@ class TelegramHelper private constructor() { chat.lastMessage = updateChat.lastMessage setChatOrder(chat, updateChat.order) } + //listener?.onTelegramChatsChanged() } } TdApi.UpdateChatOrder.CONSTRUCTOR -> { @@ -650,6 +651,7 @@ class TelegramHelper private constructor() { chat.isPinned = updateChat.isPinned setChatOrder(chat, updateChat.order) } + //listener?.onTelegramChatsChanged() } } TdApi.UpdateChatReadInbox.CONSTRUCTOR -> { @@ -751,6 +753,7 @@ class TelegramHelper private constructor() { chat.draftMessage = updateChat.draftMessage setChatOrder(chat, updateChat.order) } + //listener?.onTelegramChatsChanged() } } TdApi.UpdateNotificationSettings.CONSTRUCTOR -> {