mirror of
https://github.com/cheveguerra/FLP_3.0.git
synced 2026-04-18 03:39:20 +00:00
. Commit inicial.
This commit is contained in:
221
B4A/Tracker.bas
Normal file
221
B4A/Tracker.bas
Normal file
@@ -0,0 +1,221 @@
|
||||
B4A=true
|
||||
Group=Default Group
|
||||
ModulesStructureVersion=1
|
||||
Type=Service
|
||||
Version=10.5
|
||||
@EndOfDesignText@
|
||||
#Region Service Attributes
|
||||
#StartAtBoot: True
|
||||
#End Region
|
||||
'******************************************************************************
|
||||
'No olvidar agregar esta linea al editor de manifiesto:
|
||||
' SetServiceAttribute(Tracker, android:foregroundServiceType, "location")
|
||||
'
|
||||
'En Starter agregar estas lineas en Process_Globals
|
||||
' Public rp As RuntimePermissions
|
||||
' Public FLP As FusedLocationProvider
|
||||
' Private flpStarted As Boolean
|
||||
'
|
||||
'En Main agregar estas lineas a Activity_Resume
|
||||
' Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
|
||||
' Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
|
||||
' If Result Then
|
||||
' StartService(Tracker)
|
||||
' Log("Start Tracker")
|
||||
' Else
|
||||
' ToastMessageShow("No permission", True)
|
||||
' End If
|
||||
'
|
||||
'Se necesitan las librerias FusedLocationProvider, GPS, Phone y RunTimePermissions
|
||||
'
|
||||
'Y en Main agregar estas dos lineas:
|
||||
'#AdditionalJar: com.android.support:support-v4
|
||||
'#AdditionalJar: com.google.android.gms:play-services-location
|
||||
|
||||
Sub Process_Globals
|
||||
Private nid As Int = 1
|
||||
Private Tracking As Boolean
|
||||
Private lock As PhoneWakeState
|
||||
Public flp As FusedLocationProvider
|
||||
Private flpStarted As Boolean
|
||||
Dim minAccuracy As Int = 50
|
||||
Dim killerCalled As Int = 0
|
||||
Dim locReqSmall, locReqNormal As LocationRequest
|
||||
End Sub
|
||||
|
||||
Sub Service_Create
|
||||
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
|
||||
Subs.revisaBD
|
||||
flp.Initialize("flp")
|
||||
flp.Connect
|
||||
lock.PartialLock
|
||||
StartFLP
|
||||
' If Starter.logger Then Log("FLP initialized")
|
||||
Subs.bitacora($"Iniciamos Tracker"$)
|
||||
End Sub
|
||||
|
||||
Sub Service_Start (StartingIntent As Intent)
|
||||
locReqSmall = CreateLocationRequest2(1000, 0) 'ignore
|
||||
locReqNormal = CreateLocationRequest2(30000, 30) 'ignore
|
||||
Service.StopAutomaticForeground
|
||||
Service.StartForeground(nid, Subs.CreateNotification("..."))
|
||||
Track
|
||||
StartServiceAt(Me, DateTime.Now + 60 * DateTime.TicksPerMinute, True)
|
||||
Subs.bitacora($"Tracker - Service_Start"$)
|
||||
End Sub
|
||||
|
||||
'Apagamos y prendemos sel servicio "Tracker" o reinicamos la aplicacion.
|
||||
'killerCalled = 0 -> Apagamos y prendemos.
|
||||
'killerCalled = 1 -> ExitApplication (se reinican todos los servicios).
|
||||
Sub flpReConnect
|
||||
Try
|
||||
If killerCalled = 0 Then
|
||||
killerCalled = 1
|
||||
CallSubDelayed(Starter, "restartTracker")
|
||||
Else
|
||||
Subs.bitacora("Llamamos EXIT-APP")
|
||||
ExitApplication
|
||||
End If
|
||||
Catch
|
||||
Log("FLP-Tracker Error -> " & LastException)
|
||||
End Try
|
||||
End Sub
|
||||
|
||||
Sub flp_ConnectionSuccess
|
||||
' If Starter.logger Then Log("FLP - Connected to location provider")
|
||||
Subs.bitacora("FLP - Connection Success")
|
||||
End Sub
|
||||
|
||||
Sub flp_ConnectionFailed(ConnectionResult1 As Int)
|
||||
Log("Failed to connect to location provider")
|
||||
Subs.bitacora("FLP - Connection Failed")
|
||||
End Sub
|
||||
|
||||
Public Sub Track
|
||||
If Starter.logger Then Log("Track")
|
||||
If Tracking Then Return 'Si ya estamos "rastreando" no hacemos nada (return)
|
||||
If Starter.rp.Check(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION) = False Then
|
||||
Log("No permission")
|
||||
Return
|
||||
End If
|
||||
StartFLP 'Iniciamos FusedLocationProvider
|
||||
Tracking = True
|
||||
End Sub
|
||||
|
||||
Public Sub StartFLP
|
||||
' Log("StartFLP - flpStarted="&flpStarted)
|
||||
Private cont As Int = 0
|
||||
Do While flp.IsConnected = False
|
||||
Sleep(500)
|
||||
' If Starter.logger Then Log($"FLP Sleeping - ${cont}"$)
|
||||
Subs.bitacora($"Sleeping - ${cont}"$)
|
||||
cont = cont + 1
|
||||
If cont > 70 Then
|
||||
cont = 0
|
||||
LogColor("Reiniciamos FLP", Colors.red)
|
||||
killerCalled = 1 : flpReConnect
|
||||
End If
|
||||
Loop
|
||||
flp.RequestLocationUpdates(CreateLocationRequest) 'Buscamos ubicacion
|
||||
flpStarted = True
|
||||
' End If
|
||||
End Sub
|
||||
|
||||
Sub CreateLocationRequest As LocationRequest
|
||||
' Log("CreateLocationRequest")
|
||||
Dim lr As LocationRequest
|
||||
lr.Initialize
|
||||
lr.SetInterval(30000) 'Intervalo deseado para actualizaciones de ubicacion en milisegundos
|
||||
lr.SetFastestInterval(lr.GetInterval / 2) 'Intervalo minimo para actualizaciones de ubicacion
|
||||
lr.SetSmallestDisplacement(30) 'Solo registra cambio de ubicacion si es mayor a XX mts
|
||||
lr.SetPriority(lr.Priority.PRIORITY_HIGH_ACCURACY)
|
||||
Return lr
|
||||
End Sub
|
||||
|
||||
Sub CreateLocationRequest2(interval0 As Int, displacement0 As Int) As LocationRequest 'ignore
|
||||
' Log("CreateLocationRequestSmall")
|
||||
Dim lr As LocationRequest
|
||||
lr.Initialize
|
||||
lr.SetInterval(interval0) 'Intervalo deseado para actualizaciones de ubicacion en milisegundos
|
||||
lr.SetFastestInterval(lr.GetInterval / 2) 'Intervalo minimo para actualizaciones de ubicacion
|
||||
lr.SetSmallestDisplacement(displacement0) 'Solo registra cambio de ubicacion si es mayor a XX mts
|
||||
lr.SetPriority(lr.Priority.PRIORITY_HIGH_ACCURACY)
|
||||
Return lr
|
||||
End Sub
|
||||
|
||||
Public Sub StopFLP
|
||||
If Starter.logger Then Log("StopFLP")
|
||||
If flpStarted Then
|
||||
If flp.IsConnected Then flp.RemoveLocationUpdates 'Eliminamos todas las solicitudes de ubicacion
|
||||
flpStarted = False
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Sub flp_LocationChanged (Location1 As Location)
|
||||
Starter.UUC = Location1
|
||||
' If Starter.logger Then LogColor("FLP_LocationChanged", Colors.Red)
|
||||
If Starter.logger Then LogColor($"FLP_LocationChanged - Acc: ${Location1.Accuracy}"$, Colors.Red)
|
||||
Private vel As String = (Location1.Speed * 60 * 60) / 1000 ' Kms x hora de ultima ubicacion.
|
||||
' If Starter.logger Then Log((Location1.time & " - " & Starter.locAntTime))
|
||||
If Starter.logger Then Log(((Location1.time - Subs.traeVar("locAntTime", 0)) / 1000) & " segs")
|
||||
|
||||
If Location1.Latitude <> 0 Then Subs.guardaInfoEnBD(Location1)
|
||||
|
||||
' Private minsTranscurridosLoc As String = Subs.ticksAMins(Location1.time - Starter.locAntTime) 'Minutos transcurridos desde la ultima ubicacion ACTUALIZADA.
|
||||
' Private minsTranscurridos As String = Subs.ticksAMins(DateTime.Now - Subs.traeUltimaUbicacionGuardada.Time) 'Minutos transcurridos desde la ultima ubicacion guardada.
|
||||
' If Starter.logger Then LogColor(">>>>>>>> Guardada: " & minsTranscurridos & " mins., Actualizada: " & minsTranscurridosLoc & " mins.", Colors.red)
|
||||
' If Not(IsPaused(Main)) Then
|
||||
' Main.laUbicacion = Location1 ' Actualizamos la etiqueta de ubicacion en la pantalla principal.
|
||||
' If Starter.logger Then Log("Main.laubicacion actializada")
|
||||
' End If
|
||||
' Dim el_texto As String = ""
|
||||
' If Starter.logger Then Log(Location1.Accuracy&" - "&Starter.UUC.AccuracyValid)
|
||||
' If minsTranscurridosLoc > 1 Or minsTranscurridos >= 15 Then ' Para que no mande mensajes constantes, minimo 1 minuto entre mensajes.
|
||||
' flp.RequestLocationUpdates(CreateLocationRequest)
|
||||
' 'Solo mandamos la ubicacion si la precision es dentro de XX mts
|
||||
' el_texto = $"LocChange - Coords NO enviadas (Acc:${Location1.Accuracy})."$
|
||||
' If Location1.Accuracy < minAccuracy Then
|
||||
' If Starter.logger Then LogColor("Guardamos y enviamos ubicacion.", Colors.green)
|
||||
Subs.mandaLoc2(Location1, Starter.devModel)
|
||||
' el_texto = $"LocChange - Coords enviadas (Acc:${Location1.Accuracy})."$
|
||||
' End If
|
||||
' ' If Starter.logger Then Log("Loc changed : "&Location1.Latitude&","&Location1.Longitude&"|"&Starter.devModel&"|")
|
||||
' Subs.bitacora($"${el_texto}"$)
|
||||
'' ToastMessageShow("LocChanged MORE than a min.", False)
|
||||
' Else
|
||||
'' ToastMessageShow("Locatin changed but less than a min!.", False)
|
||||
' End If
|
||||
' Subs.actualizaVar("locAntTime", Location1.time)
|
||||
End Sub
|
||||
|
||||
Sub flp_ConnectionSuspended(SuspendedCause1 As Int)
|
||||
Dim Cause As String
|
||||
Log("ConnectionSuspended")
|
||||
Subs.bitacora("ConnectionSuspended")
|
||||
If(SuspendedCause1=flp.SuspendedCause.CAUSE_NETWORK_LOST) Then
|
||||
Cause="Suspended by NetworkLost"
|
||||
Else If(SuspendedCause1=flp.SuspendedCause.CAUSE_SERVICE_DISCONNECTED) Then
|
||||
Cause="Suspended by Disconnected"
|
||||
Else
|
||||
Cause="Suspended: Unknow cause"
|
||||
End If
|
||||
Cause = Cause&": "& Subs.fechaNormal(DateTime.Now)
|
||||
LogColor(Cause, Colors.magenta)
|
||||
Subs.bitacora(Cause)
|
||||
flp.Disconnect
|
||||
Sleep(5000)
|
||||
flp.Connect
|
||||
End Sub
|
||||
|
||||
Sub Service_Destroy
|
||||
If Tracking Then
|
||||
Try
|
||||
StopFLP
|
||||
Catch
|
||||
Log(LastException)
|
||||
End Try
|
||||
End If
|
||||
Tracking = False
|
||||
lock.ReleasePartialLock
|
||||
Subs.bitacora($"Tracker - Service_Destroy"$)
|
||||
End Sub
|
||||
Reference in New Issue
Block a user