From 092e0b26b659d5f2c84f5a8c9ebda02568578517 Mon Sep 17 00:00:00 2001 From: crimean Date: Mon, 11 Jun 2018 21:22:07 +0300 Subject: [PATCH] Telegram - single service. Right way of cleanup resources. --- .../src/net/osmand/telegram/MainActivity.kt | 21 ++++++--- .../osmand/telegram/TelegramApplication.kt | 7 ++- .../net/osmand/telegram/TelegramService.kt | 45 +++++++++++++------ .../telegram/helpers/OsmandAidlHelper.kt | 26 +++++++---- .../telegram/helpers/ShowLocationHelper.kt | 6 ++- .../osmand/telegram/helpers/TelegramHelper.kt | 4 ++ 6 files changed, 79 insertions(+), 30 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/MainActivity.kt b/OsmAnd-telegram/src/net/osmand/telegram/MainActivity.kt index 4828358de2..99510cbd10 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/MainActivity.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/MainActivity.kt @@ -81,7 +81,13 @@ class MainActivity : AppCompatActivity(), TelegramListener { } }) telegramHelper.listener = this - telegramHelper.init() + if (!telegramHelper.isInit()) { + telegramHelper.init() + } + + if (osmandHelper.isOsmandBound() && !osmandHelper.isOsmandConnected()) { + osmandHelper.connectOsmand() + } } override fun onResume() { @@ -90,16 +96,19 @@ class MainActivity : AppCompatActivity(), TelegramListener { 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,7 +120,9 @@ class MainActivity : AppCompatActivity(), TelegramListener { override fun onDestroy() { super.onDestroy() - app.cleanupResources() + if (app.telegramService == null) { + app.cleanupResources() + } } override fun onTelegramStatusChanged(prevTelegramAuthorizationState: 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() } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt index 81e849a079..811524a9ab 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramApplication.kt @@ -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) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt index 0f0b0937c1..1e0ceb8d1f 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt @@ -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() } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt index 6c0f64cb40..d1b706384f 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt @@ -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,8 +156,13 @@ class OsmandAidlHelper(private val app: Application) { } fun cleanupResources() { - if (mIOsmAndAidlInterface != null) { - app.unbindService(mConnection) + try { + if (mIOsmAndAidlInterface != null) { + mIOsmAndAidlInterface = null + app.unbindService(mConnection) + } + } catch (e: Throwable) { + e.printStackTrace() } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt index 091e31ff4e..4f88d1906f 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt @@ -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() } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index 3c68ba23c5..5769867c1d 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -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) {