Build1=Default,b4a.rocklogster
File1=mapfrag.bal
File2=marker1.png
File3=marker2.png
File4=overlay (1).png
File5=overlay.png
File6=overlaywhite.png
File7=Rockloggervisual.bal
FileGroup1=Default Group
FileGroup2=Default Group
FileGroup3=Default Group
FileGroup4=Default Group
FileGroup5=Default Group
FileGroup6=Default Group
FileGroup7=Default Group
Group=Default Group
Library1=core
Library2=gps
Library3=runtimepermissions
Library4=randomaccessfile
Library5=googlemaps
Library6=googlemapsextras
ManifestCode='This code will be applied to the manifest file during compilation.~\n~'You do not need to modify it in most cases.~\n~'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136~\n~AddManifestText(~\n~<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="28"/>~\n~<supports-screens android:largeScreens="true" ~\n~    android:normalScreens="true" ~\n~    android:smallScreens="true" ~\n~    android:anyDensity="true"/>)~\n~SetApplicationAttribute(android:label, "$LABEL$")~\n~CreateResourceFromFile(Macro, Themes.DarkTheme)~\n~~\n~AddApplicationText(~\n~<meta-data~\n~  android:name="com.google.android.geo.API_KEY"~\n~  android:value="AIzaSyBPqtpSYMEAfgvf-K7wF_L2TFOTUxeDwsA"/>~\n~)~\n~~\n~AddApplicationText(~\n~<uses-library~\n~      android:name="org.apache.http.legacy"~\n~      android:required="false" />~\n~	  <meta-data android:name="com.google.android.gms.version"~\n~      android:value="@integer/google_play_services_version" />~\n~)~\n~~\n~~\n~'End of default text.~\n~'broken icon manifest~\n~SetApplicationAttribute(android:icon, "@mipmap/ic_launcher")~\n~CreateResource(mipmap-anydpi-v26, ic_launcher.xml,~\n~<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">~\n~    <background android:drawable="@mipmap/background"/>~\n~    <foreground android:drawable="@mipmap/foreground"/>~\n~</adaptive-icon>~\n~)~\n~'CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)~\n~~\n~
Module1=Mapmod
Module2=Starter
NumberOfFiles=7
NumberOfLibraries=6
NumberOfModules=2
Version=9.9
@EndOfDesignText@
#Region Module Attributes
	#FullScreen: False
	#IncludeTitle: True
	#ApplicationLabel: RockLogster
	#VersionCode: 1
	#VersionName: 
	#SupportedOrientations: unspecified
#End Region
#BridgeLogger: true
Sub Process_Globals
	Public updateclick As Boolean
	Public RockList As List
	Public WeedList As List
	Public PointList As List
	Dim headerlist As List
	Public locationstringlist As List
	Public headerposition As Map

End Sub

'Correct the log syntax
'add menu item to clear log
'add comment to last log
'highlight last log
'more buttons

Sub Globals

	
	Dim locationstrings() As String
	Dim headerstringget As Matcher

	Type LoggedEntry (Date As String, Lat As Double, Lot As Double, obstacle As String, comment As String)
	Private lblLon As LoggedEntry
	Public lblpnlrock As Label
	Dim lblSpeed As Label
	Dim lblSatellites As Label
	Private btnRock As Button
	Private btnWeed As Button
	Private editxt As EditText
	Private lblSatIndicator As Label
	Private STime As Double
	Private SLong As String
	Private SLat As String
	Private BtnDownTime As Double
	Private BtnUpTime As Double
	Public FLong As String
	Public FLat As String
	Public FTime As Double

	Public FileStringBuilder As StringBuilder
	Public EstimatorStringBuilder As StringBuilder
	Public FileString As String
	Private FileList As List
	Public DelayedSat As Boolean
	Public lbl2secwarn As Label
	Public EstimatorString As String
	Private pnlrock As Panel
	Private lblpnlpoint As Label
	Private lblpnlweed As Label
	Private rp As RuntimePermissions
	Dim cust2 As LoggedEntry
	Dim raf2 As B4XSerializator
	Dim cust2list As List
	Private printbut As Button
	Private savebut As Button
	
	Private ButDownTime As Long
	Private ButUpTime As Long
	Private LocDown As Location
	Private LocUp As Location
	Public ELong As Double
	Public ELat As Double
	

	
	Public paneldowncomplete As Boolean
	
	Private butviewmap As Button
	Private lblpnlcust As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
	Activity.LoadLayout("Rockloggervisual")
	Activity.AddMenuItem("Clear Log", "Clearlog")
	FileStringBuilder.Initialize
	EstimatorStringBuilder.Initialize
	FileList.Initialize
	RockList.Initialize
	WeedList.Initialize
	PointList.Initialize

	'cust2.Initialize
	'cust2list.Initialize
	rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
	rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE)
	If File.Exists(rp.GetSafeDirdefaultExternal(""), "Editxt.txt") Then
		editxt.text = File.Readstring(rp.GetSafeDirDefaultExternal(""), "editxt.txt")
'		End If
'	If File.Exists(rp.GetSafeDirdefaultExternal(""), "Weedlist.txt") Then
'		WeedList = File.ReadList(rp.GetSafeDirDefaultExternal(""), "Weedlist.txt")
'		End If
'	If File.Exists(rp.GetSafeDirdefaultExternal(""), "Pointlist.txt") Then
'		PointList = File.ReadList(rp.GetSafeDirDefaultExternal(""), "Pointlist.txt")
	End If
	convert_texttolists
End Sub

Sub Print_rocks

	Private sb As StringBuilder
	sb.Initialize
	sb.append(DateTime.Date(DateTime.Now))'.Append(CRLF)

	If headerlist.Size >0 Then
'		If updateclick = False Then
			For i = 0 To headerlist.Size - 1
			sb.Append(CRLF).Append(headerlist.get(i))'.Append(CRLF)
			sb.Append(CRLF).Append(locationstringlist.get(i+1))'.Append(CRLF)
			'Log(sb.ToString)
			Next
'		Else
'			For i = 0 To headerlist.Size - 1
'				sb.Append(headerlist.get(i))
'				sb.Append(locationstringlist.get(i+1))'.Append(CRLF)
'				'Log(sb.ToString)
'				Log("trueb")
'			Next
'		End If
	End If
	updateclick = False 'flag
	editxt.Text = (sb.ToString)
End Sub

Sub convert_texttolists

	headerposition.Initialize
	headerlist.Initialize
	headerstringget = Regex.matcher2("^\D+$",Regex.MULTILINE, editxt.text)'use this to break loaded textedit file into strings or when textedit text is changed
	Do While headerstringget.Find = True
		headerlist.Add(headerstringget.Match)
		headerposition.Put(headerstringget.Match,headerlist.Size)

		Log($"headerstringsize${headerlist.size}"$)
	Loop
	For i = 0 To headerlist.Size-1
	Log(headerlist.Get(i))
	Next
	locationstringlist.Initialize
	
	locationstrings = Regex.Split2("\n^\D+$\n",Regex.MULTILINE, editxt.text)'/n to split around the newline, and not include it in the string
	For i=0 To locationstrings.Length - 1
		locationstringlist.Add(locationstrings(i))
	Next

	Log($"Splitted Size: ${locationstrings.Length} "$)
'	For i= 0 To teststring.Length-1
'		Log($"Val #${i}=${teststring(i)}"$)
'	Next

'	For Each k As String In headerposition.Keys
'			Log($"k${k}"$)
'			Log($"Positionk${headerposition.Get(k)}"$)
'		Next

'	Private sb As StringBuilder
'	sb.Initialize
'	sb.append(DateTime.Date(DateTime.Now)).Append(CRLF)
'	For i = 0 To headerlist.Size - 1
'		sb.Append(headerlist.get(i)).Append(CRLF)
'		sb.Append(locationstrings(i+1)).Append(CRLF)
'	Next
'Log(sb.ToString)
	Print_rocks

End Sub

Sub Clearlog_Click
	'cust2list.Clear
	locationstringlist.Clear
	headerlist.Clear
	headerposition.clear
	
	FileList.Clear
	FileString = ""
	editxt.Text = ""
	EstimatorString = ""
	FileStringBuilder.Initialize
	RockList.Clear
	WeedList.Clear
	PointList.Clear
	Log ("Clear")
convert_texttolists
End Sub

Sub Activity_Resume
	If Starter.GPS1.GPSEnabled = False Then
		ToastMessageShow("Please enable the GPS device.", True)
		StartActivity(Starter.GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
	Else
		Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
		Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
		If Result Then CallSubDelayed(Starter, "StartGPS")
	End If
End Sub
	

Sub Activity_Pause (UserClosed As Boolean)
	File.WriteList(rp.GetSafeDirDefaultExternal(""), "Rocklist.txt", RockList)
	File.WriteList(rp.GetSafeDirDefaultExternal(""), "Weedlist.txt", WeedList)
	File.WriteList(rp.GetSafeDirDefaultExternal(""), "Pointlist.txt", PointList)
	File.WriteString(rp.GetSafeDirDefaultExternal(""), "editext.txt", editxt.text)
	Log("writelists")
	CallSubDelayed(Starter, "StopGPS")
	'File.WriteList(File.DirRootExternal, "LogRocks.txt", FileList)
	'File.WriteBytes(File.DirInternal, "List", raf2.ConvertObjectToBytes(cust2list))
	'Log("listsave")
End Sub

Public Sub GpsStatus (Satellites As List)
	'Dim sb As StringBuilder
	'sb.Initialize
	'sb.Append("Satellites:").Append(CRLF)
	'For i = 0 To Satellites.Size - 1
	'	Dim Satellite As GPSSatellite = Satellites.Get(i)
	'	sb.Append(CRLF).Append(Satellite.Prn).Append($" $1.2{Satellite.Snr}"$).Append(" ").Append(Satellite.UsedInFix)
	'	sb.Append(" ").Append($" $1.2{Satellite.Azimuth}"$).Append($" $1.2{Satellite.Elevation}"$)
	'Next
	'lblSatellites.Text = sb.ToString
	'lblSatIndicator.Color = 0xff7fff00
End Sub
Public Sub LocationChanged(Location1 As Location)
	'lblLat.Text = "Lat = " & Location1.ConvertToMinutes(Location1.Latitude)
	'lblLon.Text = "Lon = " & Location1.ConvertToMinutes(Location1.Longitude)
	'lblSpeed.Text = $"Speed = $1.2{Location1.Speed} m/s "$
	lblSatIndicator.Color = 0xff7fff00
End Sub
Sub pnlrock_touch(ACTION As Int,X As Float,Y As Float)
	Select ACTION
		Case Activity.ACTION_DOWN
			lblpnlrock.Color = 0xFFDEB887
			Panel_down
			

		Case Activity.ACTION_UP
			lblpnlrock.Color = 0xFFFFEBCD
			Panel_up("Rocks")

		Case Activity.ACTION_MOVE
        
	End Select 
End Sub
Sub Panel_down
	ButDownTime = DateTime.Now
	paneldowncomplete = False
	Do While Starter.ValidGPS = False
		Sleep (100)
	Loop
	'STime = DateTime.Now
	LocDown = Starter.Location2
	paneldowncomplete = True
	'SLong = Starter.Location2.Longitude
	'checks for valid gps
	'logs lat and long and time
End Sub
Sub Panel_up(obstacle As String)
	ButUpTime = DateTime.Now
	Log("panelup")
	Do While Starter.ValidGPS = False Or paneldowncomplete = False
		Sleep (200)
	Loop
	If (ButUpTime - LocDown.Time < 2000) And (ButUpTime - ButDownTime >500) Then
		lbl2secwarn.Visible = True
		Sleep (2000)
		lbl2secwarn.Visible = False
		
	End If
	LocUp = Starter.Location2
	Estimator(obstacle)
End Sub

Sub Estimator(obstacle As String)
	DateTime.DateFormat = ("dd/MM/yyyy")
If (LocDown.Time = LocUp.Time) Then 
	ELat = LocUp.Latitude
	ELong = LocUp.Longitude
Else
	ELat =LocDown.Latitude - (LocDown.Latitude - LocUp.Latitude)*(LocDown.Time - 2*ButDownTime + ButUpTime)/(LocDown.Time - LocUp.Time)
	ELong = (LocDown.Longitude - LocUp.Longitude)*(2*ButDownTime-LocUp.Time-ButUpTime)/(LocDown.Time - LocUp.Time)+LocUp.longitude
End If
Log(ELong)
Log(obstacle)
	Log(headerposition.Get(obstacle))
	Log(headerposition.ContainsKey(obstacle))
	'origional calculationELat = FLat + (BtnDownTime-FTime)*(SLat - FLat)/(STime - FTime)
	'ELong = FLong + (BtnDownTime-FTime)*(SLong - FLong)/(STime - FTime)
Dim sb2 As StringBuilder
	sb2.Initialize
If headerposition.ContainsKey(obstacle) = False Then
	Log("false")
		headerlist.Add(obstacle)
		headerposition.Put(obstacle,headerlist.Size)
		sb2.Append($",$2.6{ELat} $2.6{ELong},"$)
		locationstringlist.Add(sb2.tostring)
		'locationstringlist.get(1)="e"
Else
	Log("true")
		sb2.Append(locationstringlist.get(headerposition.Get(obstacle))).Append($",$2.6{ELat} $2.6{ELong},"$)
		locationstringlist.InsertAt(headerposition.Get(obstacle),sb2.tostring)
		locationstringlist.RemoveAt(headerposition.Get(obstacle)+1)
End If

'If obstacle = "Rock" Then
'		If RockList.Size = 0 Then
'			RockList.Add("Rocks")
'		End If
'	RockList.Add($",$2.6{ELat} $2.6{ELong},"$)
'Else If obstacle = "Weed" Then
'		If WeedList.Size = 0 Then
'			WeedList.Add("Weeds")
'		End If
'	WeedList.Add($",$2.6{ELat} $2.6{ELong},"$)
'Else If obstacle = "Point" Then
'		If PointList.Size = 0 Then
'			PointList.Add("Points")
'		End If
'	PointList.Add($",$2.6{ELat} $2.6{ELong},"$)
'End If
Print_rocks
End Sub


Sub pnlweed_Touch (Action As Int, X As Float, Y As Float)
	Select Action
		Case Activity.ACTION_DOWN
			lblpnlweed.Color = 0xFF61c800
			Panel_down

		Case Activity.ACTION_UP
			lblpnlweed.Color = 0xFF7cfc00
			'_up
			Panel_up("Weed")

		Case Activity.ACTION_MOVE
        
	End Select
End Sub

Sub pnlpoint_Touch (Action As Int, X As Float, Y As Float)
	Select Action
		Case Activity.ACTION_DOWN
			lblpnlpoint.Color = 0xFF68320d
			Panel_down

		Case Activity.ACTION_UP
			lblpnlpoint.Color = 0xff8b4513
			'_up
			Panel_up("Point")

		Case Activity.ACTION_MOVE
        
	End Select
End Sub

Sub savebut_Click
	If File.Exists(rp.GetSafeDirdefaultExternal(""), "Editxt.txt") Then
	editxt.text = File.Readstring(rp.GetSafeDirDefaultExternal(""), "editxt.txt")
	End If
	convert_texttolists
End Sub

Sub printbut_Click
editxt.RequestFocus
editxt.SelectAll

End Sub

Sub butviewmap_click
	StartActivity(Mapmod)
End Sub

'Sub Editxt_TextChanged(Old As String,New As String)

'End Sub

Sub Updatebtn_Click
	'updateclick=True 'used to prevent double new lines in string
	convert_texttolists

	'Log(headerlist.Size)
	'Log(locationstringlist)
End Sub

Sub pnlcust_Touch (Action As Int, X As Float, Y As Float)
	Select Action
		Case Activity.ACTION_DOWN
			'lblpnlpoint.Color = 0xFF68320d
			Panel_down
			For i = 0 To headerlist.Size - 1
				If headerlist.Get(i) <> "Rocks" Then
					If headerlist.Get(i) <> "Weed" Then
						If headerlist.Get(i) <> "Point" Then
						lblpnlcust.Text = headerlist.Get(i)
						Exit
						End If
						End If
				End If
			Next
		Case Activity.ACTION_UP
			'lblpnlpoint.Color = 0xff8b4513
			'_up
			Panel_up(lblpnlcust.text)

		Case Activity.ACTION_MOVE
        
	End Select
End Sub