From b347c8a45c30b611935204b5dfc58b59f2c262a7 Mon Sep 17 00:00:00 2001 From: crimean Date: Wed, 13 Jun 2018 21:01:16 +0300 Subject: [PATCH] Telegram - delayed location done --- .../OnNavigationServiceAlarmReceiver.kt | 2 +- .../net/osmand/telegram/TelegramService.kt | 57 ++++++++++++++----- .../telegram/helpers/OsmandAidlHelper.kt | 4 +- 3 files changed, 45 insertions(+), 18 deletions(-) rename OsmAnd-telegram/src/net/osmand/{ => telegram}/OnNavigationServiceAlarmReceiver.kt (98%) diff --git a/OsmAnd-telegram/src/net/osmand/OnNavigationServiceAlarmReceiver.kt b/OsmAnd-telegram/src/net/osmand/telegram/OnNavigationServiceAlarmReceiver.kt similarity index 98% rename from OsmAnd-telegram/src/net/osmand/OnNavigationServiceAlarmReceiver.kt rename to OsmAnd-telegram/src/net/osmand/telegram/OnNavigationServiceAlarmReceiver.kt index 1b0be01661..d41cbfa16e 100644 --- a/OsmAnd-telegram/src/net/osmand/OnNavigationServiceAlarmReceiver.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/OnNavigationServiceAlarmReceiver.kt @@ -1,4 +1,4 @@ -package net.osmand +package net.osmand.telegram import android.annotation.SuppressLint import android.content.BroadcastReceiver diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt index ec9b01254f..df39894b26 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt @@ -11,7 +11,6 @@ import android.location.LocationManager import android.os.* import android.util.Log import android.widget.Toast -import net.osmand.OnNavigationServiceAlarmReceiver import net.osmand.PlatformUtil import net.osmand.telegram.helpers.TelegramHelper.TelegramIncomingMessagesListener import net.osmand.telegram.notifications.TelegramNotification.NotificationType @@ -65,7 +64,7 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager pendingIntent = PendingIntent.getBroadcast(this, 0, Intent(this, OnNavigationServiceAlarmReceiver::class.java), PendingIntent.FLAG_UPDATE_CURRENT) - alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 500, serviceOffInterval.toLong(), pendingIntent) + alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 500, serviceOffInterval, pendingIntent) } app.notificationHelper.refreshNotification(NotificationType.LOCATION) } @@ -111,6 +110,13 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis removeLocationUpdates() + if (!isContinuous()) { + val lock = getLock(this) + if (lock.isHeld) { + lock.release() + } + } + if (shouldCleanupResources) { app.cleanupResources() } @@ -120,16 +126,23 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis } 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") + // requesting + if (isContinuous()) { + // request location updates + val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager + try { + locationManager.requestLocationUpdates(serviceOffProvider, 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") //$NON-NLS-1$ + } catch (e: IllegalArgumentException) { + Toast.makeText(this, R.string.gps_not_available, Toast.LENGTH_LONG).show() + Log.d(PlatformUtil.TAG, "GPS location provider not available") //$NON-NLS-1$ + } + } else { + val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager + pendingIntent = PendingIntent.getBroadcast(this, 0, Intent(this, OnNavigationServiceAlarmReceiver::class.java), PendingIntent.FLAG_UPDATE_CURRENT) + alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 500, serviceOffInterval, pendingIntent) } } @@ -154,6 +167,20 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis override fun onLocationChanged(l: Location?) { if (l != null) { val location = convertLocation(l) + if (!isContinuous()) { + // unregister listener and wait next time + val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager + try { + locationManager.removeUpdates(this) + } catch (e: SecurityException) { + Log.d(PlatformUtil.TAG, "Location service permission not granted") //$NON-NLS-1$ + } + + val lock = getLock(this) + if (lock.isHeld) { + lock.release() + } + } app().shareLocationHelper.updateLocation(location) } } @@ -206,13 +233,13 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis @Synchronized fun getLock(context: Context): PowerManager.WakeLock { var lockStatic = lockStatic - if (lockStatic == null) { + return if (lockStatic == null) { val mgr = context.getSystemService(Context.POWER_SERVICE) as PowerManager lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "OsmandServiceLock") this.lockStatic = lockStatic - return lockStatic + lockStatic } else { - return lockStatic + lockStatic } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt index d1b706384f..1e2dddc467 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/OsmandAidlHelper.kt @@ -78,7 +78,7 @@ class OsmandAidlHelper(private val app: Application) { // representation of that from the raw service object. mIOsmAndAidlInterface = IOsmAndAidlInterface.Stub.asInterface(service) initialized = true - Toast.makeText(app, "OsmAnd connected", Toast.LENGTH_SHORT).show() + //Toast.makeText(app, "OsmAnd connected", Toast.LENGTH_SHORT).show() listener?.onOsmandConnectionStateChanged(true) } @@ -86,7 +86,7 @@ class OsmandAidlHelper(private val app: Application) { // This is called when the connection with the service has been // unexpectedly disconnected -- that is, its process crashed. mIOsmAndAidlInterface = null - Toast.makeText(app, "OsmAnd disconnected", Toast.LENGTH_SHORT).show() + //Toast.makeText(app, "OsmAnd disconnected", Toast.LENGTH_SHORT).show() listener?.onOsmandConnectionStateChanged(false) } }