OsmAnd/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt

452 lines
14 KiB
Kotlin
Raw Normal View History

2018-06-11 18:57:33 +02:00
package net.osmand.telegram
import android.annotation.SuppressLint
2018-06-13 16:18:54 +02:00
import android.app.AlarmManager
import android.app.PendingIntent
2018-06-11 18:57:33 +02:00
import android.app.Service
import android.content.Context
import android.content.Intent
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.os.*
import android.util.Log
import android.widget.Toast
import net.osmand.PlatformUtil
2020-01-26 13:01:23 +01:00
import net.osmand.telegram.TelegramSettings.ShareChatInfo
import net.osmand.telegram.helpers.TelegramHelper
import net.osmand.telegram.helpers.TelegramHelper.*
2018-06-13 16:18:54 +02:00
import net.osmand.telegram.notifications.TelegramNotification.NotificationType
import net.osmand.telegram.utils.AndroidUtils
2020-01-26 13:01:23 +01:00
import net.osmand.telegram.utils.OsmandLocationUtils
2018-06-11 18:57:33 +02:00
import org.drinkless.td.libcore.telegram.TdApi
import java.util.*
2018-06-11 18:57:33 +02:00
2019-12-24 16:06:46 +01:00
private const val UPDATE_WIDGET_INTERVAL_MS = 1000L // 1 sec
private const val UPDATE_LIVE_MESSAGES_INTERVAL_MS = 10000L // 10 sec
2019-07-17 16:23:27 +02:00
private const val UPDATE_LIVE_TRACKS_INTERVAL_MS = 30000L // 30 sec
2018-10-08 17:33:59 +02:00
class TelegramService : Service(), LocationListener, TelegramIncomingMessagesListener,
TelegramOutgoingMessagesListener {
2018-06-11 18:57:33 +02:00
private val log = PlatformUtil.getLog(TelegramService::class.java)
2018-06-11 18:57:33 +02:00
private fun app() = application as TelegramApplication
private val binder = LocationServiceBinder()
private var shouldCleanupResources: Boolean = false
2018-06-11 18:57:33 +02:00
private var updateShareInfoHandler: Handler? = null
private var mHandlerThread = HandlerThread("SharingServiceThread")
2019-07-17 16:23:27 +02:00
private var updateTracksHandler: Handler? = null
private var tracksHandlerThread = HandlerThread("TracksUpdateServiceThread")
2019-12-24 16:06:46 +01:00
private var updateWidgetHandler: Handler? = null
private var updateWidgetThread = HandlerThread("WidgetUpdateServiceThread")
2018-06-11 18:57:33 +02:00
var handler: Handler? = null
2018-06-13 16:18:54 +02:00
private set
2018-06-11 18:57:33 +02:00
var usedBy = 0
2018-06-13 16:18:54 +02:00
private set
var serviceOffProvider: String = LocationManager.GPS_PROVIDER
private set
var serviceOffInterval = 0L
private set
2018-06-14 14:21:58 +02:00
var serviceErrorInterval = 0L
2018-06-13 16:18:54 +02:00
private set
var sendLocationInterval = 0L
private set
2018-06-13 16:18:54 +02:00
private var lastLocationSentTime = 0L
2018-06-13 16:18:54 +02:00
private var pendingIntent: PendingIntent? = null
2018-06-11 18:57:33 +02:00
class LocationServiceBinder : Binder()
override fun onCreate() {
super.onCreate()
mHandlerThread.start()
2019-07-17 16:23:27 +02:00
tracksHandlerThread.start()
2019-12-24 16:06:46 +01:00
updateWidgetThread.start()
updateShareInfoHandler = Handler(mHandlerThread.looper)
2019-07-17 16:23:27 +02:00
updateTracksHandler = Handler(tracksHandlerThread.looper)
2019-12-24 16:06:46 +01:00
updateWidgetHandler = Handler(updateWidgetThread.looper)
}
2018-06-11 18:57:33 +02:00
override fun onBind(intent: Intent): IBinder? {
return binder
}
fun stopIfNeeded(ctx: Context, usageIntent: Int) {
if (usedBy and usageIntent > 0) {
usedBy -= usageIntent
}
2019-05-30 15:08:40 +02:00
when {
usedBy == 0 -> {
shouldCleanupResources = false
val serviceIntent = Intent(ctx, TelegramService::class.java)
ctx.stopService(serviceIntent)
2018-06-13 16:18:54 +02:00
}
2019-05-30 15:08:40 +02:00
isUsedByMyLocation(usedBy) -> {
val app = app()
if (app.settings.sendMyLocInterval >= OFF_INTERVAL_THRESHOLD && serviceOffInterval == 0L) {
serviceOffInterval = app.settings.sendMyLocInterval
setupServiceErrorInterval()
setupAlarm()
}
app.notificationHelper.refreshNotification(NotificationType.LOCATION)
}
isUsedByUsersLocations(usedBy) -> removeLocationUpdates()
2018-06-11 18:57:33 +02:00
}
}
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
val app = app()
handler = Handler()
2019-05-30 15:08:40 +02:00
val usageIntent = intent.getIntExtra(USAGE_INTENT, 0)
usedBy = usageIntent or usedBy
2018-06-11 18:57:33 +02:00
2018-06-13 16:18:54 +02:00
serviceOffInterval = intent.getLongExtra(USAGE_OFF_INTERVAL, 0)
sendLocationInterval = intent.getLongExtra(SEND_LOCATION_INTERVAL, 0)
2018-06-14 14:21:58 +02:00
setupServiceErrorInterval()
2018-06-13 16:18:54 +02:00
app.telegramHelper.addIncomingMessagesListener(this)
2018-10-08 17:33:59 +02:00
app.telegramHelper.addOutgoingMessagesListener(this)
2018-06-11 18:57:33 +02:00
app.telegramService = this
2018-06-14 14:21:58 +02:00
if (isUsedByMyLocation(usedBy)) {
initLocationUpdates()
startShareInfoUpdates()
2019-12-24 16:06:46 +01:00
startWidgetUpdates()
2018-06-11 18:57:33 +02:00
}
if (isUsedByUsersLocations(usedBy)) {
app.telegramHelper.startLiveMessagesUpdates(app.settings.sendMyLocInterval)
2019-07-17 16:23:27 +02:00
startTracksUpdates()
}
app.shareLocationHelper.checkAndSendBufferMessages()
2018-06-11 18:57:33 +02:00
val locationNotification = app.notificationHelper.locationNotification
val notification = app.notificationHelper.buildNotification(locationNotification)
startForeground(locationNotification.telegramNotificationId, notification)
app.notificationHelper.refreshNotification(locationNotification.type)
return Service.START_REDELIVER_INTENT
}
2018-06-14 14:21:58 +02:00
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)
}
2018-06-11 18:57:33 +02:00
override fun onDestroy() {
super.onDestroy()
val app = app()
app.telegramHelper.stopLiveMessagesUpdates()
app.telegramHelper.removeIncomingMessagesListener(this)
2018-10-08 17:33:59 +02:00
app.telegramHelper.removeOutgoingMessagesListener(this)
2019-01-31 11:26:09 +01:00
app.settings.save()
2018-06-11 18:57:33 +02:00
app.telegramService = null
2019-07-17 16:23:27 +02:00
tracksHandlerThread.quit()
mHandlerThread.quit()
2019-12-24 16:06:46 +01:00
updateWidgetThread.quit()
app().showLocationHelper.addOrUpdateStatusWidget(-1, false)
2018-06-11 18:57:33 +02:00
usedBy = 0
removeLocationUpdates()
2018-06-13 20:01:16 +02:00
if (!isContinuous()) {
val lock = getLock(this)
if (lock.isHeld) {
lock.release()
}
}
if (shouldCleanupResources) {
app.cleanupResources()
}
// remove notification
stopForeground(java.lang.Boolean.TRUE)
}
fun updateSendLocationInterval(newInterval: Long) {
sendLocationInterval = newInterval
}
fun forceLocationUpdate() {
val location = getFirstTimeRunDefaultLocation()
app().shareLocationHelper.updateLocation(location)
}
private fun initLocationUpdates() {
val firstLocation = getFirstTimeRunDefaultLocation()
app().shareLocationHelper.updateLocation(firstLocation)
2018-06-13 20:01:16 +02:00
// requesting
if (isContinuous()) {
// request location updates
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
try {
locationManager.requestLocationUpdates(serviceOffProvider, 0, 0f, this@TelegramService)
} catch (e: SecurityException) {
Toast.makeText(this, R.string.no_location_permission, Toast.LENGTH_LONG).show()
Log.d(PlatformUtil.TAG, "Location service permission not granted") //$NON-NLS-1$
} catch (e: IllegalArgumentException) {
Toast.makeText(this, R.string.gps_not_available, Toast.LENGTH_LONG).show()
Log.d(PlatformUtil.TAG, "GPS location provider not available") //$NON-NLS-1$
}
} else {
setupAlarm()
}
}
private fun startShareInfoUpdates() {
updateShareInfoHandler?.postDelayed({
if (isUsedByMyLocation(usedBy)) {
app().shareLocationHelper.updateSendLiveMessages()
startShareInfoUpdates()
}
}, UPDATE_LIVE_MESSAGES_INTERVAL_MS)
}
2019-07-17 16:23:27 +02:00
private fun startTracksUpdates() {
updateTracksHandler?.postDelayed({
if (isUsedByUsersLocations(usedBy)) {
if (app().settings.hasAnyLiveTracksToShowOnMap()) {
app().showLocationHelper.startUpdateTracksTask()
}
startTracksUpdates()
}
}, UPDATE_LIVE_TRACKS_INTERVAL_MS)
}
2019-12-24 16:06:46 +01:00
private fun startWidgetUpdates() {
updateWidgetHandler?.postDelayed({
if (isUsedByMyLocation(usedBy)) {
val sharingStatus = app().settings.sharingStatusChanges.last()
var isSending = sharingStatus.statusType == TelegramSettings.SharingStatusType.SENDING
2020-01-06 13:08:21 +01:00
val sharingChats = app().settings.getShareLocationChats()
var oldestTime = 0L
if (sharingChats.isNotEmpty() && app().shareLocationHelper.sharingLocation) {
2020-01-06 13:08:21 +01:00
sharingChats.forEach { id ->
val bufferMessages = app().locationMessages.getBufferedMessagesForChat(id)
if (bufferMessages.isNotEmpty()) {
val newTime = bufferMessages[0].time
if (oldestTime == 0L || newTime < oldestTime) {
oldestTime = newTime
}
} else {
oldestTime = 0L
}
}
} else {
isSending = false
oldestTime = -1
2020-01-06 13:08:21 +01:00
}
app().showLocationHelper.addOrUpdateStatusWidget(oldestTime, isSending)
2020-01-13 15:46:23 +01:00
} else {
app().showLocationHelper.addOrUpdateStatusWidget(-1, false)
}
2019-12-24 16:06:46 +01:00
startWidgetUpdates()
}, UPDATE_WIDGET_INTERVAL_MS)
}
@SuppressLint("MissingPermission")
private fun getFirstTimeRunDefaultLocation(): net.osmand.Location? {
val app = app()
if (!AndroidUtils.isLocationPermissionAvailable(app)) {
return null
}
val service = app.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val ps = service.getProviders(true) ?: return null
val providers = ArrayList(ps)
// note, passive provider is from API_LEVEL 8 but it is a constant, we can check for it.
// constant should not be changed in future
val passiveFirst = providers.indexOf("passive") // LocationManager.PASSIVE_PROVIDER
// put passive provider to first place
if (passiveFirst > -1) {
providers.add(0, providers.removeAt(passiveFirst))
}
// find location
2019-02-02 12:14:57 +01:00
var location: net.osmand.Location? = null
for (provider in providers) {
2019-02-02 12:14:57 +01:00
val loc = convertLocation(service.getLastKnownLocation(provider))
if (loc != null && (location == null || loc.hasAccuracy() && loc.accuracy < location.accuracy)) {
location = loc
}
}
2019-02-02 12:14:57 +01:00
return location
}
private fun setupAlarm() {
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
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)
}
private fun removeLocationUpdates() {
2018-06-11 18:57:33 +02:00
// remove updates
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
try {
locationManager.removeUpdates(this)
} catch (e: SecurityException) {
Log.d(PlatformUtil.TAG, "Location service permission not granted")
}
}
2018-06-13 16:18:54 +02:00
private fun isContinuous(): Boolean {
return serviceOffInterval == 0L
}
2018-06-11 18:57:33 +02:00
override fun onLocationChanged(l: Location?) {
2018-10-16 17:42:43 +02:00
val location = convertLocation(l)
if (!isContinuous()) {
// unregister listener and wait next time
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
try {
locationManager.removeUpdates(this)
} catch (e: Throwable) {
Log.d(PlatformUtil.TAG, "Location service permission not granted") //$NON-NLS-1$
}
val lock = getLock(this)
if (lock.isHeld) {
lock.release()
2018-06-13 20:01:16 +02:00
}
2018-10-16 17:42:43 +02:00
app().shareLocationHelper.updateLocation(location)
} else if (System.currentTimeMillis() - lastLocationSentTime > sendLocationInterval * 1000) {
lastLocationSentTime = System.currentTimeMillis()
app().shareLocationHelper.updateLocation(location)
2018-06-11 18:57:33 +02:00
}
}
override fun onProviderDisabled(provider: String) {
Toast.makeText(this, getString(R.string.location_service_no_gps_available), Toast.LENGTH_LONG).show()
}
override fun onProviderEnabled(provider: String) {}
override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {}
override fun onTaskRemoved(rootIntent: Intent) {
val app = app()
if (app.telegramService != null) {
shouldCleanupResources = true
2018-06-11 18:57:33 +02:00
// Do not stop service after UI task was dismissed
//this@TelegramService.stopSelf()
}
}
2018-07-13 13:02:59 +02:00
override fun onReceiveChatLocationMessages(chatId: Long, vararg messages: TdApi.Message) {
2018-08-29 14:27:50 +02:00
app().showLocationHelper.startShowMessagesTask(chatId, *messages)
2019-01-25 17:04:26 +01:00
messages.forEach {
2019-02-01 12:34:22 +01:00
if (!it.isOutgoing) {
app().locationMessages.addNewLocationMessage(it)
}
2019-01-25 17:04:26 +01:00
}
2018-06-11 18:57:33 +02:00
}
2018-08-02 16:30:59 +02:00
override fun onDeleteChatLocationMessages(chatId: Long, messages: List<TdApi.Message>) {
2018-08-29 15:01:53 +02:00
app().showLocationHelper.startDeleteMessagesTask(chatId, messages)
2018-08-02 16:30:59 +02:00
}
override fun updateLocationMessages() {
2018-08-29 15:01:53 +02:00
app().showLocationHelper.startUpdateMessagesTask()
}
2018-10-08 17:33:59 +02:00
override fun onUpdateMessages(messages: List<TdApi.Message>) {
messages.forEach {
app().settings.updateShareInfo(it)
2019-01-29 18:03:44 +01:00
app().shareLocationHelper.checkAndSendBufferMessagesToChat(it.chatId)
2019-02-08 18:12:17 +01:00
if (it.sendingState == null && !it.isOutgoing && (it.content is TdApi.MessageLocation || it.content is TdApi.MessageText)) {
app().locationMessages.addNewLocationMessage(it)
2019-01-29 18:03:44 +01:00
}
2018-10-08 17:33:59 +02:00
}
}
override fun onDeleteMessages(chatId: Long, messages: List<Long>) {
app().settings.onDeleteLiveMessages(chatId, messages)
}
2020-01-26 13:01:23 +01:00
override fun onSendLiveLocationError(code: Int, message: String, shareInfo: ShareChatInfo, messageType: Int) {
2018-10-12 18:17:38 +02:00
Log.d(PlatformUtil.TAG, "Send live location error: $code - $message")
2020-01-26 13:01:23 +01:00
when (messageType) {
TelegramHelper.MESSAGE_TYPE_TEXT -> shareInfo.pendingTdLibText--
TelegramHelper.MESSAGE_TYPE_MAP -> shareInfo.pendingTdLibMap--
}
2018-10-12 18:17:38 +02:00
}
2018-06-11 18:57:33 +02:00
companion object {
const val USED_BY_MY_LOCATION: Int = 1
const val USED_BY_USERS_LOCATIONS: Int = 2
const val USAGE_INTENT = "SERVICE_USED_BY"
2018-06-13 16:18:54 +02:00
const val USAGE_OFF_INTERVAL = "SERVICE_OFF_INTERVAL"
const val SEND_LOCATION_INTERVAL = "SEND_LOCATION_INTERVAL"
2018-06-13 16:18:54 +02:00
2018-06-14 14:21:58 +02:00
const val OFF_INTERVAL_THRESHOLD: Long = 30000L
2018-06-13 16:18:54 +02:00
private var lockStatic: PowerManager.WakeLock? = null
@Synchronized
fun getLock(context: Context): PowerManager.WakeLock {
var lockStatic = lockStatic
2018-06-13 20:01:16 +02:00
return if (lockStatic == null) {
2018-06-13 16:18:54 +02:00
val mgr = context.getSystemService(Context.POWER_SERVICE) as PowerManager
lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "OsmandServiceLock")
this.lockStatic = lockStatic
2018-06-13 20:01:16 +02:00
lockStatic
2018-06-13 16:18:54 +02:00
} else {
2018-06-13 20:01:16 +02:00
lockStatic
2018-06-13 16:18:54 +02:00
}
}
2018-06-11 18:57:33 +02:00
2018-06-14 14:21:58 +02:00
fun isUsedByMyLocation(usedBy: Int): Boolean {
return (usedBy and USED_BY_MY_LOCATION) > 0
}
fun isUsedByUsersLocations(usedBy: Int): Boolean {
return (usedBy and USED_BY_USERS_LOCATIONS) > 0
}
2018-06-14 14:21:58 +02:00
fun isOffIntervalDepended(usedBy: Int): Boolean {
return isUsedByMyLocation(usedBy)
}
fun normalizeOffInterval(interval: Long): Long {
return if (interval < OFF_INTERVAL_THRESHOLD) 0 else interval
}
2018-06-11 18:57:33 +02:00
fun convertLocation(l: Location?): net.osmand.Location? {
if (l == null) {
return null
}
val r = net.osmand.Location(l.provider)
r.latitude = l.latitude
r.longitude = l.longitude
r.time = l.time
if (l.hasAccuracy()) {
r.accuracy = l.accuracy
}
if (l.hasSpeed()) {
r.speed = l.speed
}
if (l.hasAltitude()) {
r.altitude = l.altitude
}
if (l.hasBearing()) {
r.bearing = l.bearing
}
if (l.hasAltitude()) {
r.altitude = l.altitude
}
return r
}
}
}