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

178 lines
5.7 KiB
Kotlin
Raw Normal View History

package net.osmand.telegram
import android.app.Application
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.os.Build
import android.os.Handler
2018-08-13 16:35:49 +02:00
import net.osmand.telegram.helpers.*
import net.osmand.telegram.helpers.OsmandAidlHelper.OsmandHelperListener
2018-08-29 15:01:53 +02:00
import net.osmand.telegram.helpers.OsmandAidlHelper.UpdatesListener
import net.osmand.telegram.notifications.NotificationHelper
import net.osmand.telegram.utils.AndroidUtils
2018-06-13 19:14:08 +02:00
import net.osmand.telegram.utils.UiUtils
class TelegramApplication : Application(), OsmandHelperListener {
2018-08-10 12:35:30 +02:00
val telegramHelper = TelegramHelper.instance
2018-06-11 18:57:33 +02:00
lateinit var settings: TelegramSettings private set
2018-06-13 19:14:08 +02:00
lateinit var uiUtils: UiUtils private set
2018-06-11 18:57:33 +02:00
lateinit var shareLocationHelper: ShareLocationHelper private set
lateinit var showLocationHelper: ShowLocationHelper private set
lateinit var notificationHelper: NotificationHelper private set
lateinit var osmandAidlHelper: OsmandAidlHelper private set
2018-07-05 17:16:55 +02:00
lateinit var locationProvider: TelegramLocationProvider private set
2018-08-13 16:35:49 +02:00
lateinit var messagesDbHelper: MessagesDbHelper private set
2018-06-11 18:57:33 +02:00
var telegramService: TelegramService? = null
private val uiHandler = Handler()
private val lastTimeInternetConnectionChecked: Long = 0
private var internetConnectionAvailable = true
override fun onCreate() {
super.onCreate()
2018-08-10 12:35:30 +02:00
telegramHelper.appDir = filesDir.absolutePath
2018-08-13 16:35:49 +02:00
telegramHelper.init()
2018-06-11 18:57:33 +02:00
settings = TelegramSettings(this)
2018-08-10 12:35:30 +02:00
telegramHelper.messageActiveTimeSec = settings.locHistoryTime
2018-06-13 19:14:08 +02:00
uiUtils = UiUtils(this)
osmandAidlHelper = OsmandAidlHelper(this)
osmandAidlHelper.listener = object : OsmandAidlHelper.OsmandHelperListener {
override fun onOsmandConnectionStateChanged(connected: Boolean) {
if (connected) {
val basePackage = "net.osmand.telegram"
val appPackage = if (BuildConfig.DEBUG) "$basePackage.debug" else basePackage
2018-07-31 20:36:31 +02:00
osmandAidlHelper.setNavDrawerItems(
appPackage,
listOf(getString(R.string.app_name)),
listOf("osmand_telegram://main_activity"),
listOf("ic_action_location_sharing_app"),
listOf(-1)
)
2018-08-29 16:26:09 +02:00
if (settings.hasAnyChatToShowOnMap()) {
showLocationHelper.startShowingLocation()
}
}
}
}
2018-08-29 15:01:53 +02:00
osmandAidlHelper.setUpdatesListener(object : UpdatesListener {
2018-08-29 14:27:50 +02:00
override fun update() {
2018-08-29 15:01:53 +02:00
showLocationHelper.startUpdateMessagesTask()
2018-08-29 14:27:50 +02:00
}
})
2018-06-11 18:57:33 +02:00
shareLocationHelper = ShareLocationHelper(this)
showLocationHelper = ShowLocationHelper(this)
notificationHelper = NotificationHelper(this)
2018-07-05 17:16:55 +02:00
locationProvider = TelegramLocationProvider(this)
2018-08-13 16:35:49 +02:00
messagesDbHelper = MessagesDbHelper(this)
2018-06-11 18:57:33 +02:00
if (settings.hasAnyChatToShareLocation() && AndroidUtils.isLocationPermissionAvailable(this)) {
shareLocationHelper.startSharingLocation()
}
}
fun cleanupResources() {
osmandAidlHelper.cleanupResources()
2018-06-11 18:57:33 +02:00
telegramHelper.close()
}
val isWifiConnected: Boolean
get() {
val mgr = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val ni = mgr.activeNetworkInfo
return ni != null && ni.type == ConnectivityManager.TYPE_WIFI
}
private val isInternetConnected: Boolean
get() {
val mgr = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val active = mgr.activeNetworkInfo
if (active == null) {
return false
} else {
val state = active.state
return state != NetworkInfo.State.DISCONNECTED && state != NetworkInfo.State.DISCONNECTING
}
}
// Check internet connection available every 15 seconds
val isInternetConnectionAvailable: Boolean
get() = isInternetConnectionAvailable(false)
fun isInternetConnectionAvailable(update: Boolean): Boolean {
val delta = System.currentTimeMillis() - lastTimeInternetConnectionChecked
if (delta < 0 || delta > 15000 || update) {
internetConnectionAvailable = isInternetConnected
}
return internetConnectionAvailable
}
override fun onOsmandConnectionStateChanged(connected: Boolean) {
showLocationHelper.setupMapLayer()
}
2018-06-14 14:21:58 +02:00
private fun startTelegramService(intent: Int, serviceOffInterval: Long = 0) {
2018-06-11 18:57:33 +02:00
var i = intent
2018-06-14 14:21:58 +02:00
var interval = serviceOffInterval
2018-06-11 18:57:33 +02:00
val serviceIntent = Intent(this, TelegramService::class.java)
val telegramService = telegramService
if (telegramService != null) {
i = intent or telegramService.usedBy
2018-06-14 14:21:58 +02:00
interval = if (TelegramService.isOffIntervalDepended(intent)) {
Math.min(telegramService.serviceOffInterval, interval)
} else {
telegramService.serviceOffInterval
}
2018-06-11 18:57:33 +02:00
telegramService.stopSelf()
}
serviceIntent.putExtra(TelegramService.USAGE_INTENT, i)
2018-06-14 14:21:58 +02:00
serviceIntent.putExtra(TelegramService.USAGE_OFF_INTERVAL, interval)
serviceIntent.putExtra(TelegramService.SEND_LOCATION_INTERVAL, settings.sendMyLocInterval)
2018-06-11 18:57:33 +02:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(serviceIntent)
} else {
startService(serviceIntent)
}
}
fun startMyLocationService() {
val interval = settings.sendMyLocInterval
2018-06-14 14:21:58 +02:00
startTelegramService(TelegramService.USED_BY_MY_LOCATION, TelegramService.normalizeOffInterval(interval))
2018-06-11 18:57:33 +02:00
}
fun stopMyLocationService() {
telegramService?.stopIfNeeded(this, TelegramService.USED_BY_MY_LOCATION)
}
fun forceUpdateMyLocation() {
telegramService?.forceLocationUpdate()
}
fun updateSendLocationInterval() {
telegramService?.updateSendLocationInterval(settings.sendMyLocInterval)
}
2018-06-11 18:57:33 +02:00
fun startUserLocationService() {
startTelegramService(TelegramService.USED_BY_USERS_LOCATIONS)
}
fun stopUserLocationService() {
telegramService?.stopIfNeeded(this, TelegramService.USED_BY_USERS_LOCATIONS)
}
fun runInUIThread(action: (() -> Unit)) {
uiHandler.post(action)
}
fun runInUIThread(action: (() -> Unit), delay: Long) {
uiHandler.postDelayed(action, delay)
}
}