Telegram - single service. Right way of cleanup resources.
This commit is contained in:
parent
0be04a0474
commit
092e0b26b6
6 changed files with 79 additions and 30 deletions
|
@ -81,25 +81,34 @@ class MainActivity : AppCompatActivity(), TelegramListener {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
telegramHelper.listener = this
|
telegramHelper.listener = this
|
||||||
|
if (!telegramHelper.isInit()) {
|
||||||
telegramHelper.init()
|
telegramHelper.init()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (osmandHelper.isOsmandBound() && !osmandHelper.isOsmandConnected()) {
|
||||||
|
osmandHelper.connectOsmand()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
paused = false
|
paused = false
|
||||||
|
|
||||||
invalidateOptionsMenu()
|
invalidateOptionsMenu()
|
||||||
updateTitle()
|
updateTitle()
|
||||||
|
updateChatsList()
|
||||||
|
|
||||||
if (settings.hasAnyChatToShareLocation() && !AndroidUtils.isLocationPermissionAvailable(this)) {
|
if (settings.hasAnyChatToShareLocation() && !AndroidUtils.isLocationPermissionAvailable(this)) {
|
||||||
requestLocationPermission()
|
requestLocationPermission()
|
||||||
} else if (settings.hasAnyChatToShowOnMap() && osmandHelper.initialized && !osmandHelper.isOsmandBound()) {
|
} else if (settings.hasAnyChatToShowOnMap() && osmandHelper.isOsmandNotInstalled()) {
|
||||||
showOsmandMissingDialog()
|
showOsmandMissingDialog()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onPause() {
|
override fun onPause() {
|
||||||
super.onPause()
|
super.onPause()
|
||||||
|
telegramHelper.listener = null
|
||||||
|
|
||||||
paused = true
|
paused = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,8 +120,10 @@ class MainActivity : AppCompatActivity(), TelegramListener {
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
|
|
||||||
|
if (app.telegramService == null) {
|
||||||
app.cleanupResources()
|
app.cleanupResources()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onTelegramStatusChanged(prevTelegramAuthorizationState: TelegramAuthorizationState,
|
override fun onTelegramStatusChanged(prevTelegramAuthorizationState: TelegramAuthorizationState,
|
||||||
newTelegramAuthorizationState: TelegramAuthorizationState) {
|
newTelegramAuthorizationState: TelegramAuthorizationState) {
|
||||||
|
@ -299,7 +310,7 @@ class MainActivity : AppCompatActivity(), TelegramListener {
|
||||||
app.shareLocationHelper.startSharingLocation()
|
app.shareLocationHelper.startSharingLocation()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (settings.hasAnyChatToShowOnMap() && osmandHelper.initialized && !osmandHelper.isOsmandBound()) {
|
if (settings.hasAnyChatToShowOnMap() && osmandHelper.isOsmandNotInstalled()) {
|
||||||
showOsmandMissingDialog()
|
showOsmandMissingDialog()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -372,7 +383,7 @@ class MainActivity : AppCompatActivity(), TelegramListener {
|
||||||
holder.showOnMapSwitch?.setOnCheckedChangeListener { _, isChecked ->
|
holder.showOnMapSwitch?.setOnCheckedChangeListener { _, isChecked ->
|
||||||
settings.showChatOnMap(chatTitle, isChecked)
|
settings.showChatOnMap(chatTitle, isChecked)
|
||||||
if (settings.hasAnyChatToShowOnMap()) {
|
if (settings.hasAnyChatToShowOnMap()) {
|
||||||
if (osmandHelper.initialized && !osmandHelper.isOsmandBound()) {
|
if (osmandHelper.isOsmandNotInstalled()) {
|
||||||
if (isChecked) {
|
if (isChecked) {
|
||||||
showOsmandMissingDialog()
|
showOsmandMissingDialog()
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,13 +8,14 @@ import android.net.NetworkInfo
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import net.osmand.telegram.helpers.OsmandAidlHelper
|
import net.osmand.telegram.helpers.OsmandAidlHelper
|
||||||
|
import net.osmand.telegram.helpers.OsmandAidlHelper.OsmandHelperListener
|
||||||
import net.osmand.telegram.helpers.ShareLocationHelper
|
import net.osmand.telegram.helpers.ShareLocationHelper
|
||||||
import net.osmand.telegram.helpers.ShowLocationHelper
|
import net.osmand.telegram.helpers.ShowLocationHelper
|
||||||
import net.osmand.telegram.helpers.TelegramHelper
|
import net.osmand.telegram.helpers.TelegramHelper
|
||||||
import net.osmand.telegram.notifications.NotificationHelper
|
import net.osmand.telegram.notifications.NotificationHelper
|
||||||
import net.osmand.telegram.utils.AndroidUtils
|
import net.osmand.telegram.utils.AndroidUtils
|
||||||
|
|
||||||
class TelegramApplication : Application() {
|
class TelegramApplication : Application(), OsmandHelperListener {
|
||||||
|
|
||||||
val telegramHelper = TelegramHelper.instance
|
val telegramHelper = TelegramHelper.instance
|
||||||
lateinit var settings: TelegramSettings private set
|
lateinit var settings: TelegramSettings private set
|
||||||
|
@ -84,6 +85,10 @@ class TelegramApplication : Application() {
|
||||||
return internetConnectionAvailable
|
return internetConnectionAvailable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onOsmandConnectionStateChanged(connected: Boolean) {
|
||||||
|
showLocationHelper.setupMapLayer()
|
||||||
|
}
|
||||||
|
|
||||||
private fun startTelegramService(intent: Int) {
|
private fun startTelegramService(intent: Int) {
|
||||||
var i = intent
|
var i = intent
|
||||||
val serviceIntent = Intent(this, TelegramService::class.java)
|
val serviceIntent = Intent(this, TelegramService::class.java)
|
||||||
|
|
|
@ -19,6 +19,7 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
|
||||||
private fun app() = application as TelegramApplication
|
private fun app() = application as TelegramApplication
|
||||||
private val binder = LocationServiceBinder()
|
private val binder = LocationServiceBinder()
|
||||||
private val executor = Executors.newSingleThreadExecutor()
|
private val executor = Executors.newSingleThreadExecutor()
|
||||||
|
private var shouldCleanupResources: Boolean = false
|
||||||
|
|
||||||
var handler: Handler? = null
|
var handler: Handler? = null
|
||||||
var usedBy = 0
|
var usedBy = 0
|
||||||
|
@ -34,8 +35,11 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
|
||||||
usedBy -= usageIntent
|
usedBy -= usageIntent
|
||||||
}
|
}
|
||||||
if (usedBy == 0) {
|
if (usedBy == 0) {
|
||||||
|
shouldCleanupResources = false
|
||||||
val serviceIntent = Intent(ctx, TelegramService::class.java)
|
val serviceIntent = Intent(ctx, TelegramService::class.java)
|
||||||
ctx.stopService(serviceIntent)
|
ctx.stopService(serviceIntent)
|
||||||
|
} else if (!needLocation()) {
|
||||||
|
removeLocationUpdates()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,17 +52,7 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
|
||||||
app.telegramHelper.incomingMessagesListener = this
|
app.telegramHelper.incomingMessagesListener = this
|
||||||
|
|
||||||
if (needLocation()) {
|
if (needLocation()) {
|
||||||
// request location updates
|
initLocationUpdates()
|
||||||
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
|
||||||
try {
|
|
||||||
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 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")
|
|
||||||
} catch (e: IllegalArgumentException) {
|
|
||||||
Toast.makeText(this, R.string.gps_not_available, Toast.LENGTH_LONG).show()
|
|
||||||
Log.d(PlatformUtil.TAG, "GPS location provider not available")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val locationNotification = app.notificationHelper.locationNotification
|
val locationNotification = app.notificationHelper.locationNotification
|
||||||
|
@ -76,6 +70,31 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
|
||||||
|
|
||||||
usedBy = 0
|
usedBy = 0
|
||||||
|
|
||||||
|
removeLocationUpdates()
|
||||||
|
|
||||||
|
if (shouldCleanupResources) {
|
||||||
|
app.cleanupResources()
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove notification
|
||||||
|
stopForeground(java.lang.Boolean.TRUE)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun initLocationUpdates() {
|
||||||
|
// request location updates
|
||||||
|
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
||||||
|
try {
|
||||||
|
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 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")
|
||||||
|
} catch (e: IllegalArgumentException) {
|
||||||
|
Toast.makeText(this, R.string.gps_not_available, Toast.LENGTH_LONG).show()
|
||||||
|
Log.d(PlatformUtil.TAG, "GPS location provider not available")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun removeLocationUpdates() {
|
||||||
// remove updates
|
// remove updates
|
||||||
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
|
||||||
try {
|
try {
|
||||||
|
@ -83,9 +102,6 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
|
||||||
} catch (e: SecurityException) {
|
} catch (e: SecurityException) {
|
||||||
Log.d(PlatformUtil.TAG, "Location service permission not granted")
|
Log.d(PlatformUtil.TAG, "Location service permission not granted")
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove notification
|
|
||||||
stopForeground(java.lang.Boolean.TRUE)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun needLocation(): Boolean {
|
private fun needLocation(): Boolean {
|
||||||
|
@ -112,6 +128,7 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
|
||||||
override fun onTaskRemoved(rootIntent: Intent) {
|
override fun onTaskRemoved(rootIntent: Intent) {
|
||||||
val app = app()
|
val app = app()
|
||||||
if (app.telegramService != null) {
|
if (app.telegramService != null) {
|
||||||
|
shouldCleanupResources = true
|
||||||
// Do not stop service after UI task was dismissed
|
// Do not stop service after UI task was dismissed
|
||||||
//this@TelegramService.stopSelf()
|
//this@TelegramService.stopSelf()
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,11 +56,8 @@ class OsmandAidlHelper(private val app: Application) {
|
||||||
|
|
||||||
private var mIOsmAndAidlInterface: IOsmAndAidlInterface? = null
|
private var mIOsmAndAidlInterface: IOsmAndAidlInterface? = null
|
||||||
|
|
||||||
var initialized: Boolean = false
|
private var initialized: Boolean = false
|
||||||
private set
|
private var bound: Boolean = false
|
||||||
|
|
||||||
var bound: Boolean = false
|
|
||||||
private set
|
|
||||||
|
|
||||||
var listener: OsmandHelperListener? = null
|
var listener: OsmandHelperListener? = null
|
||||||
|
|
||||||
|
@ -95,7 +92,11 @@ class OsmandAidlHelper(private val app: Application) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isOsmandBound(): Boolean {
|
fun isOsmandBound(): Boolean {
|
||||||
return bound
|
return initialized && bound
|
||||||
|
}
|
||||||
|
|
||||||
|
fun isOsmandNotInstalled(): Boolean {
|
||||||
|
return initialized && !bound
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isOsmandConnected(): Boolean {
|
fun isOsmandConnected(): Boolean {
|
||||||
|
@ -124,6 +125,10 @@ class OsmandAidlHelper(private val app: Application) {
|
||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
connectOsmand()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun connectOsmand() {
|
||||||
when {
|
when {
|
||||||
bindService(OSMAND_PLUS_PACKAGE_NAME) -> {
|
bindService(OSMAND_PLUS_PACKAGE_NAME) -> {
|
||||||
OSMAND_PACKAGE_NAME = OSMAND_PLUS_PACKAGE_NAME
|
OSMAND_PACKAGE_NAME = OSMAND_PLUS_PACKAGE_NAME
|
||||||
|
@ -151,9 +156,14 @@ class OsmandAidlHelper(private val app: Application) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun cleanupResources() {
|
fun cleanupResources() {
|
||||||
|
try {
|
||||||
if (mIOsmAndAidlInterface != null) {
|
if (mIOsmAndAidlInterface != null) {
|
||||||
|
mIOsmAndAidlInterface = null
|
||||||
app.unbindService(mConnection)
|
app.unbindService(mConnection)
|
||||||
}
|
}
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun refreshMap(): Boolean {
|
fun refreshMap(): Boolean {
|
||||||
|
|
|
@ -17,7 +17,7 @@ class ShowLocationHelper(private val app: TelegramApplication) {
|
||||||
var showingLocation: Boolean = false
|
var showingLocation: Boolean = false
|
||||||
private set
|
private set
|
||||||
|
|
||||||
private fun setMapLayer() {
|
fun setupMapLayer() {
|
||||||
osmandHelper.addMapLayer(MAP_LAYER_ID, "Telegram", 5.5f, null)
|
osmandHelper.addMapLayer(MAP_LAYER_ID, "Telegram", 5.5f, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,10 +39,12 @@ class ShowLocationHelper(private val app: TelegramApplication) {
|
||||||
if (userName.isEmpty()) {
|
if (userName.isEmpty()) {
|
||||||
userName = message.senderUserId.toString()
|
userName = message.senderUserId.toString()
|
||||||
}
|
}
|
||||||
setMapLayer()
|
setupMapLayer()
|
||||||
osmandHelper.addMapPoint(MAP_LAYER_ID, "${chatTitle}_${message.senderUserId}", userName, userName,
|
osmandHelper.addMapPoint(MAP_LAYER_ID, "${chatTitle}_${message.senderUserId}", userName, userName,
|
||||||
chatTitle, Color.RED, ALatLon(content.location.latitude, content.location.longitude), null)
|
chatTitle, Color.RED, ALatLon(content.location.latitude, content.location.longitude), null)
|
||||||
}
|
}
|
||||||
|
} else if (osmandHelper.isOsmandBound()) {
|
||||||
|
osmandHelper.connectOsmand()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -197,6 +197,10 @@ class TelegramHelper private constructor() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun isInit(): Boolean {
|
||||||
|
return client != null && haveAuthorization
|
||||||
|
}
|
||||||
|
|
||||||
private fun requestChats(reload: Boolean = false) {
|
private fun requestChats(reload: Boolean = false) {
|
||||||
synchronized(chatList) {
|
synchronized(chatList) {
|
||||||
if (reload) {
|
if (reload) {
|
||||||
|
|
Loading…
Reference in a new issue