Improve tracker location update

This commit is contained in:
max-klaus 2021-02-19 16:23:50 +03:00
parent cb556beb2b
commit 1867b3d07d
2 changed files with 15 additions and 7 deletions

View file

@ -4,7 +4,7 @@ import android.annotation.SuppressLint
import android.content.Context
import android.hardware.*
import android.location.Location
import android.os.Looper
import android.os.HandlerThread
import android.util.Log
import com.google.android.gms.location.*
import net.osmand.PlatformUtil
@ -43,11 +43,12 @@ class TelegramLocationProvider(private val app: TelegramApplication) : SensorEve
var lastKnownLocation: net.osmand.Location? = null
private set
private val locationUpdateHandlerThread = HandlerThread("LocationProviderUpdateHandlerThread")
private var fusedLocationProviderClient: FusedLocationProviderClient? = null
private val locationRequest = LocationRequest().apply {
interval = 1000
fastestInterval = 500
maxWaitTime = 2000
//fastestInterval = 500
maxWaitTime = 0
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
private val locationCallback = object : LocationCallback() {
@ -81,6 +82,10 @@ class TelegramLocationProvider(private val app: TelegramApplication) : SensorEve
fun updateCompassValue(value: Float)
}
init {
locationUpdateHandlerThread.start()
}
@SuppressLint("MissingPermission")
fun resumeAllUpdates() {
if (AndroidUtils.isLocationPermissionAvailable(app) && fusedLocationProviderClient == null) {
@ -89,7 +94,7 @@ class TelegramLocationProvider(private val app: TelegramApplication) : SensorEve
try {
fusedLocationProviderClient?.requestLocationUpdates(
locationRequest, locationCallback, Looper.myLooper())
locationRequest, locationCallback, locationUpdateHandlerThread.looper)
} catch (unlikely: SecurityException) {
Log.d(PlatformUtil.TAG, "Lost location permissions. Couldn't request updates. $unlikely")
}

View file

@ -38,6 +38,7 @@ class TelegramService : Service(), TelegramIncomingMessagesListener,
private var updateWidgetHandler: Handler? = null
private var updateWidgetThread = HandlerThread("WidgetUpdateServiceThread")
private var locationUpdateHandlerThread = HandlerThread("LocationUpdateServiceThread")
// FusedLocationProviderClient - Main class for receiving location updates.
private lateinit var fusedLocationProviderClient: FusedLocationProviderClient
@ -62,6 +63,7 @@ class TelegramService : Service(), TelegramIncomingMessagesListener,
mHandlerThread.start()
tracksHandlerThread.start()
updateWidgetThread.start()
locationUpdateHandlerThread.start()
updateShareInfoHandler = Handler(mHandlerThread.looper)
updateTracksHandler = Handler(tracksHandlerThread.looper)
updateWidgetHandler = Handler(updateWidgetThread.looper)
@ -82,11 +84,11 @@ class TelegramService : Service(), TelegramIncomingMessagesListener,
// Sets the fastest rate for active location updates. This interval is exact, and your
// application will never receive updates more frequently than this value.
fastestInterval = 500
//fastestInterval = 500
// Sets the maximum time when batched location updates are delivered. Updates may be
// delivered sooner than this interval.
maxWaitTime = 2000
maxWaitTime = 0
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
@ -166,6 +168,7 @@ class TelegramService : Service(), TelegramIncomingMessagesListener,
tracksHandlerThread.quit()
mHandlerThread.quit()
updateWidgetThread.quit()
locationUpdateHandlerThread.quit()
app().showLocationHelper.addOrUpdateStatusWidget(-1, false)
usedBy = 0
@ -198,7 +201,7 @@ class TelegramService : Service(), TelegramIncomingMessagesListener,
// request location updates
try {
fusedLocationProviderClient.requestLocationUpdates(
locationRequest, locationCallback, Looper.myLooper())
locationRequest, locationCallback, locationUpdateHandlerThread.looper)
} catch (unlikely: SecurityException) {
Toast.makeText(this, R.string.no_location_permission, Toast.LENGTH_LONG).show()
Log.d(PlatformUtil.TAG, "Lost location permissions. Couldn't request updates. $unlikely")