2018-06-06 13:06:56 +02:00
|
|
|
package net.osmand.telegram
|
|
|
|
|
|
|
|
import android.app.Application
|
|
|
|
import android.content.Context
|
2018-06-09 11:20:21 +02:00
|
|
|
import android.content.Intent
|
2020-10-05 12:03:18 +02:00
|
|
|
import android.content.pm.PackageManager
|
2018-06-06 13:06:56 +02:00
|
|
|
import android.net.ConnectivityManager
|
|
|
|
import android.net.NetworkInfo
|
2018-06-09 11:20:21 +02:00
|
|
|
import android.os.Build
|
|
|
|
import android.os.Handler
|
2021-01-11 18:04:51 +01:00
|
|
|
import androidx.core.content.ContextCompat
|
2020-10-05 12:03:18 +02:00
|
|
|
import net.osmand.PlatformUtil
|
2018-08-13 16:35:49 +02:00
|
|
|
import net.osmand.telegram.helpers.*
|
2018-06-11 20:22:07 +02:00
|
|
|
import net.osmand.telegram.helpers.OsmandAidlHelper.OsmandHelperListener
|
2018-08-29 15:01:53 +02:00
|
|
|
import net.osmand.telegram.helpers.OsmandAidlHelper.UpdatesListener
|
2018-06-09 11:20:21 +02:00
|
|
|
import net.osmand.telegram.notifications.NotificationHelper
|
2021-01-11 18:04:51 +01:00
|
|
|
import net.osmand.telegram.ui.TrackerLogcatActivity
|
2018-06-09 11:20:21 +02:00
|
|
|
import net.osmand.telegram.utils.AndroidUtils
|
2018-06-13 19:14:08 +02:00
|
|
|
import net.osmand.telegram.utils.UiUtils
|
2020-10-05 12:03:18 +02:00
|
|
|
import java.io.File
|
2018-06-06 13:06:56 +02:00
|
|
|
|
2020-01-16 17:04:33 +01:00
|
|
|
class TelegramApplication : Application() {
|
2018-06-06 13:06:56 +02:00
|
|
|
|
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
|
2018-07-09 12:59:30 +02:00
|
|
|
lateinit var osmandAidlHelper: OsmandAidlHelper private set
|
2018-07-05 17:16:55 +02:00
|
|
|
lateinit var locationProvider: TelegramLocationProvider private set
|
2019-01-25 17:04:26 +01:00
|
|
|
lateinit var locationMessages: LocationMessages 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)
|
2018-07-09 12:59:30 +02:00
|
|
|
osmandAidlHelper = OsmandAidlHelper(this)
|
2020-01-29 11:07:34 +01:00
|
|
|
osmandAidlHelper.addConnectionListener(object : OsmandHelperListener {
|
2020-01-16 17:04:33 +01:00
|
|
|
override fun onOsmandConnectionStateChanged(connected: Boolean) {
|
|
|
|
if (connected) {
|
|
|
|
osmandAidlHelper.clearNavDrawerItems("net.osmand.telegram")
|
|
|
|
osmandAidlHelper.clearNavDrawerItems("net.osmand.telegram.debug")
|
|
|
|
osmandAidlHelper.setNavDrawerItems(
|
|
|
|
applicationContext.packageName,
|
|
|
|
listOf(getString(R.string.app_name_short)),
|
|
|
|
listOf("osmand_telegram://main_activity"),
|
|
|
|
listOf("ic_action_location_sharing_app"),
|
|
|
|
listOf(-1)
|
|
|
|
)
|
|
|
|
showLocationHelper.setupMapLayer()
|
|
|
|
showLocationHelper.addDirectionContextMenuButton()
|
|
|
|
showLocationHelper.startShowingLocation()
|
|
|
|
showLocationHelper.addOrUpdateStatusWidget(-1, false)
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 11:07:34 +01:00
|
|
|
})
|
2018-08-29 15:01:53 +02:00
|
|
|
osmandAidlHelper.setUpdatesListener(object : UpdatesListener {
|
2018-08-29 14:27:50 +02:00
|
|
|
override fun update() {
|
2019-05-29 14:20:52 +02:00
|
|
|
if (settings.hasAnyChatToShowOnMap()) {
|
|
|
|
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)
|
2019-01-25 17:04:26 +01:00
|
|
|
locationMessages = LocationMessages(this)
|
2018-06-11 18:57:33 +02:00
|
|
|
|
|
|
|
if (settings.hasAnyChatToShareLocation() && AndroidUtils.isLocationPermissionAvailable(this)) {
|
|
|
|
shareLocationHelper.startSharingLocation()
|
|
|
|
}
|
2019-02-06 12:50:02 +01:00
|
|
|
if (settings.monitoringEnabled) {
|
|
|
|
showLocationHelper.startShowingLocation()
|
|
|
|
}
|
2018-06-11 18:57:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun cleanupResources() {
|
2018-07-09 12:59:30 +02:00
|
|
|
osmandAidlHelper.cleanupResources()
|
2018-06-11 18:57:33 +02:00
|
|
|
telegramHelper.close()
|
|
|
|
}
|
|
|
|
|
2018-09-07 14:34:03 +02:00
|
|
|
fun stopSharingLocation() {
|
|
|
|
settings.stopSharingLocationToChats()
|
|
|
|
shareLocationHelper.stopSharingLocation()
|
2018-10-08 18:10:55 +02:00
|
|
|
telegramHelper.stopSendingLiveLocationMessages(settings.getChatsShareInfo())
|
2018-09-07 14:34:03 +02:00
|
|
|
}
|
|
|
|
|
2019-02-15 18:32:17 +01:00
|
|
|
fun stopMonitoring() {
|
|
|
|
settings.monitoringEnabled = false
|
|
|
|
stopUserLocationService()
|
|
|
|
}
|
|
|
|
|
2019-02-05 17:15:51 +01:00
|
|
|
fun isAnyOsmAndInstalled() = TelegramSettings.AppConnect.getInstalledApps(this).isNotEmpty()
|
2019-02-05 16:26:42 +01:00
|
|
|
|
2019-02-05 17:15:51 +01:00
|
|
|
fun isOsmAndChosen() = settings.appToConnectPackage.isNotEmpty()
|
2019-02-05 16:26:42 +01:00
|
|
|
|
2019-02-05 17:15:51 +01:00
|
|
|
fun isOsmAndInstalled() = AndroidUtils.isAppInstalled(this, settings.appToConnectPackage)
|
2018-09-07 12:32:21 +02:00
|
|
|
|
2018-06-11 18:57:33 +02:00
|
|
|
val isWifiConnected: Boolean
|
|
|
|
get() {
|
|
|
|
val mgr = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
|
|
|
val ni = mgr.activeNetworkInfo
|
|
|
|
return ni != null && ni.type == ConnectivityManager.TYPE_WIFI
|
|
|
|
}
|
|
|
|
|
2019-01-25 17:04:26 +01:00
|
|
|
val isMobileConnected: Boolean
|
|
|
|
get() {
|
|
|
|
val mgr = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
|
|
|
val ni = mgr.activeNetworkInfo
|
|
|
|
return ni != null && ni.type == ConnectivityManager.TYPE_MOBILE
|
|
|
|
}
|
|
|
|
|
2018-06-11 18:57:33 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-11 18:04:51 +01:00
|
|
|
private fun startTelegramService(intent: Int) {
|
2018-06-11 18:57:33 +02:00
|
|
|
var i = intent
|
|
|
|
val serviceIntent = Intent(this, TelegramService::class.java)
|
|
|
|
val telegramService = telegramService
|
|
|
|
if (telegramService != null) {
|
|
|
|
i = intent or telegramService.usedBy
|
|
|
|
telegramService.stopSelf()
|
|
|
|
}
|
|
|
|
serviceIntent.putExtra(TelegramService.USAGE_INTENT, i)
|
2018-08-10 12:16:46 +02:00
|
|
|
serviceIntent.putExtra(TelegramService.SEND_LOCATION_INTERVAL, settings.sendMyLocInterval)
|
2021-01-11 18:04:51 +01:00
|
|
|
ContextCompat.startForegroundService(this, serviceIntent)
|
2018-06-11 18:57:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun startMyLocationService() {
|
2021-01-11 18:04:51 +01:00
|
|
|
startTelegramService(TelegramService.USED_BY_MY_LOCATION)
|
2018-06-11 18:57:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun stopMyLocationService() {
|
|
|
|
telegramService?.stopIfNeeded(this, TelegramService.USED_BY_MY_LOCATION)
|
|
|
|
}
|
|
|
|
|
2018-06-19 15:33:15 +02:00
|
|
|
fun forceUpdateMyLocation() {
|
|
|
|
telegramService?.forceLocationUpdate()
|
|
|
|
}
|
|
|
|
|
2018-08-15 12:58:59 +02:00
|
|
|
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)
|
|
|
|
}
|
2020-10-05 12:03:18 +02:00
|
|
|
|
|
|
|
fun sendCrashLog(file: File) {
|
|
|
|
val intent = Intent(Intent.ACTION_SEND)
|
|
|
|
intent.putExtra(Intent.EXTRA_EMAIL, arrayOf("crash@osmand.net"))
|
|
|
|
intent.putExtra(Intent.EXTRA_STREAM, AndroidUtils.getUriForFile(this, file))
|
|
|
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
|
|
|
intent.type = "vnd.android.cursor.dir/email"
|
|
|
|
intent.putExtra(Intent.EXTRA_SUBJECT, "OsmAnd bug")
|
|
|
|
val text = StringBuilder()
|
|
|
|
text.append("\nDevice : ").append(Build.DEVICE)
|
|
|
|
text.append("\nBrand : ").append(Build.BRAND)
|
|
|
|
text.append("\nModel : ").append(Build.MODEL)
|
|
|
|
text.append("\nProduct : ").append(Build.PRODUCT)
|
|
|
|
text.append("\nBuild : ").append(Build.DISPLAY)
|
|
|
|
text.append("\nVersion : ").append(Build.VERSION.RELEASE)
|
|
|
|
text.append("\nApp : ").append(getString(R.string.app_name_short))
|
|
|
|
try {
|
|
|
|
val info = packageManager.getPackageInfo(packageName, 0)
|
|
|
|
if (info != null) {
|
|
|
|
text.append("\nApk Version : ").append(info.versionName).append(" ").append(info.versionCode)
|
|
|
|
}
|
|
|
|
} catch (e: PackageManager.NameNotFoundException) {
|
|
|
|
PlatformUtil.getLog(TrackerLogcatActivity::class.java).error("", e)
|
|
|
|
}
|
|
|
|
intent.putExtra(Intent.EXTRA_TEXT, text.toString())
|
|
|
|
val chooserIntent = Intent.createChooser(intent, getString(R.string.send_report))
|
|
|
|
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
|
|
startActivity(chooserIntent)
|
|
|
|
}
|
2018-06-06 13:06:56 +02:00
|
|
|
}
|