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

42 lines
1.3 KiB
Kotlin
Raw Normal View History

2018-06-13 20:01:16 +02:00
package net.osmand.telegram
2018-06-13 16:18:54 +02:00
import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.location.LocationManager
import net.osmand.telegram.utils.AndroidUtils
2018-06-14 14:21:58 +02:00
class OnTelegramServiceAlarmReceiver : BroadcastReceiver() {
2018-06-13 16:18:54 +02:00
@SuppressLint("MissingPermission")
override fun onReceive(context: Context, intent: Intent) {
val lock = TelegramService.getLock(context)
val app = context.applicationContext as TelegramApplication
val service = app.telegramService
// do not do nothing
if (lock.isHeld || service == null) {
return
}
2018-06-14 14:21:58 +02:00
lock.acquire(15 * 60 * 1000L)
2018-06-13 16:18:54 +02:00
// request location updates
val locationManager = service.getSystemService(Context.LOCATION_SERVICE) as LocationManager
try {
if (AndroidUtils.isLocationPermissionAvailable(app)) {
locationManager.requestLocationUpdates(service.serviceOffProvider, 0, 0f, service)
2018-06-14 14:21:58 +02:00
}
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)
2018-06-13 16:18:54 +02:00
}
} catch (e: RuntimeException) {
e.printStackTrace()
}
}
}