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