﻿B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Service
Version=9.01
@EndOfDesignText@
#Region  Service Attributes 
	#StartAtBoot: true
	
	
#End Region

Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.
	Public NextStartTime, OtherStartTime As Double
	Public X1, X2, sNotes1, sNotes2 As String
	Public i As Int
	
	
End Sub

Sub Service_Create
	
End Sub

Sub Service_Start (StartingIntent As Intent)
	' Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
	StartSchedule
		
	
'							
End Sub

public Sub StartSchedule
	' sub to schedule the next notification   pretty well finished on 6/3/19 cwm
	Dim ITime As Int
	
	' get notifications for next alert/notif time
	CallSub (Starter,"getnotifications")
		
	' only notif if SNotifTime is non zero
	If Starter.SNotifTime.trim.CompareTo("")=0 Then Starter.SNotifTime=0
	If Starter.SNotifTime>0 Then
	

		' get the current time and compare to the notif time 
		' this was very tricky-had to add seconds to itime and compare to our time with '00' added
		'   in order not to loop and do multiple notifications. 
		DateTime.DateFormat="HHmmss"
		ITime=DateTime.date (DateTime.Now+DateTime.TicksPerMinute)
'		Log ("Comparing Notification times " & ITime & " " & Starter.SNotifTime & "00")
		
		If ITime< Starter.SNotifTime & "00" Then
			' not yet the time for the notify-keep it to today's date
			NextStartTime=DateTime.Now
		 Else
			' bump the notify to tomorrow
			NextStartTime=DateTime.Add(DateTime.now,0,0,1)
		End If
		
		' now change the hhmm to use the SNotifTime
		DateTime.DateFormat="yyyyMMddHHmm"
		X1=DateTime.Date (NextStartTime)
		' get string for Snotiftime
		X2=NumberFormat(Starter.SNotifTime,4,0)
		' note numberformat puts commas in a 4 digit number
		X2=X2.SubString2(0,1) & X2.SubString2(2,5)
		' now put together the date and hhmm
		X1=X1.SubString2(0,8) & X2
		NextStartTime=DateTime.DateParse(X1)
		
		' test every 15 seconds
		'  NextStartTime=DateTime.Now+ DateTime.TicksPerMinute/4 
		
		
		' now compare the nextstarttime to the alertstarttime   6/13/19 cwm
		If Starter.NextAlerttime.CompareTo("300012312400") <>0 Then
			' we have a valid next alert time
			If Starter.NextAlertTime.CompareTo(X1) <0 Then
				NextStartTime=DateTime.DateParse(Starter.nextalerttime)
				End If	
		End If
	
		' get final x1 formated
		DateTime.DateFormat="yyyy/MM/dd HH:mm"
		X1=DateTime.Date (NextStartTime)
		
		' set up the notification
		Dim n As Notification
		n.Initialize
		n.Sound=False
		n.Vibrate=False
		n.Icon="icon"
		
		' mod to vary the display depending if alerts are available  6/24/19 cwm
		If Starter.SNotif.Size>0 Then
			sNotes1="FS has alerts for you. Touch here."
			If Starter.SnotifSound=1 Then n.Sound = True
			If Starter.SnotifSound=3 Then n.Sound = True
			If Starter.SnotifSound=2 Then n.Vibrate = True
			If Starter.SnotifSound=3 Then n.vibrate = True
			' check if the notification situation has changed-  10/8/20 cwm
			'  if so mod the starter.nextstarttime to force a notif
			If Starter.Notifications=False Then 
				Starter.NextStartTime=0
				Starter.Notifications=True
				End If
			Else
			sNotes1="FS has no alerts for today."
			' check if the notification situation has changed- 10/8/20 cwn
			' if so mod the starter.nextstarttime to force a notif
			If Starter.Notifications=True Then 
				Starter.NextStartTime=0
				Starter.Notifications=False
				End If
			End If
		
		sNotes2= "Next Notification at: " & X1
		
		' fire the notification
		' only if the starter.nextStartTime has changed or the notification status changed   10/8/20 cwm
'		Log ("Before:" & Starter.NextStartTime & " " & NextStartTime & " " & Starter.Notifications)
	
		If Starter.NextstartTime<>NextStartTime Then
			n.SetInfo(sNotes1,sNotes2,  "Main")
			Log (sNotes1 & CRLF & sNotes2)
			' note the significance of the '1'/it indicates to replace any other notification from FS with a '1' in it
			' if you wanted multiple notifications you could use '2', '3', etc.
			n.Notify(1)
			
			' start the next service- 
			StartServiceAt (FSService, NextStartTime,False)
			' save the nextstarttime so we don't repeat unless it does change 10/8/20 cwm
			Starter.NextStartTime=NextStartTime
'			Log ("After:" & Starter.NextStartTime & " " & NextStartTime & " " & Starter.Notifications)
		End If	
			
		
	End If
	
End Sub

Sub Service_Destroy 

End Sub
