Telegram - delayed location done

This commit is contained in:
crimean 2018-06-13 21:01:16 +03:00
parent cfa3861d4d
commit b347c8a45c
3 changed files with 45 additions and 18 deletions

View file

@ -1,4 +1,4 @@
package net.osmand package net.osmand.telegram
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.BroadcastReceiver import android.content.BroadcastReceiver

View file

@ -11,7 +11,6 @@ import android.location.LocationManager
import android.os.* import android.os.*
import android.util.Log import android.util.Log
import android.widget.Toast import android.widget.Toast
import net.osmand.OnNavigationServiceAlarmReceiver
import net.osmand.PlatformUtil import net.osmand.PlatformUtil
import net.osmand.telegram.helpers.TelegramHelper.TelegramIncomingMessagesListener import net.osmand.telegram.helpers.TelegramHelper.TelegramIncomingMessagesListener
import net.osmand.telegram.notifications.TelegramNotification.NotificationType import net.osmand.telegram.notifications.TelegramNotification.NotificationType
@ -65,7 +64,7 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
pendingIntent = PendingIntent.getBroadcast(this, 0, Intent(this, OnNavigationServiceAlarmReceiver::class.java), PendingIntent.FLAG_UPDATE_CURRENT) 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) app.notificationHelper.refreshNotification(NotificationType.LOCATION)
} }
@ -111,6 +110,13 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
removeLocationUpdates() removeLocationUpdates()
if (!isContinuous()) {
val lock = getLock(this)
if (lock.isHeld) {
lock.release()
}
}
if (shouldCleanupResources) { if (shouldCleanupResources) {
app.cleanupResources() app.cleanupResources()
} }
@ -120,16 +126,23 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
} }
private fun initLocationUpdates() { private fun initLocationUpdates() {
// requesting
if (isContinuous()) {
// request location updates // request location updates
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
try { try {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0f, this@TelegramService) locationManager.requestLocationUpdates(serviceOffProvider, 0, 0f, this@TelegramService)
} catch (e: SecurityException) { } catch (e: SecurityException) {
Toast.makeText(this, R.string.no_location_permission, Toast.LENGTH_LONG).show() Toast.makeText(this, R.string.no_location_permission, Toast.LENGTH_LONG).show()
Log.d(PlatformUtil.TAG, "Location service permission not granted") Log.d(PlatformUtil.TAG, "Location service permission not granted") //$NON-NLS-1$
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
Toast.makeText(this, R.string.gps_not_available, Toast.LENGTH_LONG).show() Toast.makeText(this, R.string.gps_not_available, Toast.LENGTH_LONG).show()
Log.d(PlatformUtil.TAG, "GPS location provider not available") 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?) { override fun onLocationChanged(l: Location?) {
if (l != null) { if (l != null) {
val location = convertLocation(l) 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) app().shareLocationHelper.updateLocation(location)
} }
} }
@ -206,13 +233,13 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
@Synchronized @Synchronized
fun getLock(context: Context): PowerManager.WakeLock { fun getLock(context: Context): PowerManager.WakeLock {
var lockStatic = lockStatic var lockStatic = lockStatic
if (lockStatic == null) { return if (lockStatic == null) {
val mgr = context.getSystemService(Context.POWER_SERVICE) as PowerManager val mgr = context.getSystemService(Context.POWER_SERVICE) as PowerManager
lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "OsmandServiceLock") lockStatic = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "OsmandServiceLock")
this.lockStatic = lockStatic this.lockStatic = lockStatic
return lockStatic lockStatic
} else { } else {
return lockStatic lockStatic
} }
} }

View file

@ -78,7 +78,7 @@ class OsmandAidlHelper(private val app: Application) {
// representation of that from the raw service object. // representation of that from the raw service object.
mIOsmAndAidlInterface = IOsmAndAidlInterface.Stub.asInterface(service) mIOsmAndAidlInterface = IOsmAndAidlInterface.Stub.asInterface(service)
initialized = true initialized = true
Toast.makeText(app, "OsmAnd connected", Toast.LENGTH_SHORT).show() //Toast.makeText(app, "OsmAnd connected", Toast.LENGTH_SHORT).show()
listener?.onOsmandConnectionStateChanged(true) listener?.onOsmandConnectionStateChanged(true)
} }
@ -86,7 +86,7 @@ class OsmandAidlHelper(private val app: Application) {
// This is called when the connection with the service has been // This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed. // unexpectedly disconnected -- that is, its process crashed.
mIOsmAndAidlInterface = null mIOsmAndAidlInterface = null
Toast.makeText(app, "OsmAnd disconnected", Toast.LENGTH_SHORT).show() //Toast.makeText(app, "OsmAnd disconnected", Toast.LENGTH_SHORT).show()
listener?.onOsmandConnectionStateChanged(false) listener?.onOsmandConnectionStateChanged(false)
} }
} }