#Region Service Attributes
#StartAtBoot: True
#End Region
Sub Process_Globals
'Private nid As Int = 1
Private GPS As GPS
Private Tracking As Boolean
Private LastUpdateTime As Long
Private lock As PhoneWakeState
Private Distance As Double
Private UserWasNotified As Boolean = False
Public Close As Boolean
End Sub
Sub Service_Create
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
GPS.Initialize("gps")
lock.PartialLock
End Sub
Sub Service_Start (StartingIntent As Intent)
'Service.StartForeground(0, Null)
Service.StartForeground(1, CreateNotification("","",0))
Track
End Sub
Public Sub Track
'Sleep(0)
If Tracking Then Return
If Starter.rp.Check(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION) = False Then
Log("No permission")
Return
End If
GPS.Start(0, 0)
Tracking = True
End Sub
Sub GPS_LocationChanged (Location1 As Location)
If DateTime.Now > LastUpdateTime + 10 * DateTime.TicksPerSecond Then
LastUpdateTime = DateTime.Now
CheckDistance (Location1)
End If
End Sub
Sub Service_Destroy
If Tracking Then
GPS.Stop
End If
Tracking = False
lock.ReleasePartialLock
End Sub
Sub CheckDistance(Location1 As Location)
Dim Location2 As Location
Dim TrueCount As Byte = 0
For Each TempContext As Context In Main.Contexts
'Calculate distances to each context (location) that has a lat/lon.
If TempContext.Latitude <> 0 Then 'Some contexts have no lat or lon. Can't use them.
'Find the distance from each context to the phone.
Location2.Initialize2(TempContext.Latitude,TempContext.Longitude)
Distance = Location1.DistanceTo(Location2) * .00062137 'miles.
Distance = Round2(Distance, 2)
'ToastMessageShow (Main.Contexts(X,0)&": " & Distance & " miles.", True)
'Check to see if the distance to the context is within the specifed distance to be considered "close".
'Make a map of those that are close.
If Distance < TempContext.Distance Then 'Context is Close.
TempContext.Close = True
'Log("Close Context = "&Main.Contexts(x,0))
TrueCount = TrueCount + 1
'ToastMessageShow("Close to " & Main.Contexts(x,0),True)
Else 'Context is not close.
TempContext.Close = False 'Clear close contexts from Context array.
TempContext.Show = False 'Clear desired contexts from Context array.
End If
End If
Main.Contexts.Set(Main.Contexts.IndexOf(TempContext),TempContext)
Next
If TrueCount > 0 Then 'Some of the contexts are close.
Log("**Close = True")
Close = True
Log("MenuBuilt = "&Main.MenuBuilt)
If Main.MenuBuilt = False Then
CallSubDelayed(Main,"BuildMenuMap")
Main.MenuBuilt = True
End If
If UserWasNotified = False Then
Dim n As Notification = CreateNotification("Contexts Nearby","Tap to Proceed.",3)
n.Notify(1)
UserWasNotified = True
End If
Else
Log("**Close = False")
Close = False
UserWasNotified = False
Dim n As Notification = CreateNotification("No Contexts Near","",0)
n.Notify(1)
End If
'Log("TrueCount = "&TrueCount)
'Log("Close = "&Main.Close)
End Sub
Sub CreateNotification (Title As String, Body As String, Importance As Int) As Notification
Log("CreateNotification sub started")
Public notification As Notification
notification.Initialize2(Importance)
notification.Icon = "icon"
notification.AutoCancel = True 'This makes the notification go away when it is clicked on.
notification.Sound = True
notification.SetInfo(Title, Body, ShowMenuFlag)
Log("CreateNotification sub done")
Return notification
End Sub