﻿Type=Service
Version=6.5
ModulesStructureVersion=1
B4A=true
@EndOfDesignText@
#Region  Service Attributes 
	#StartAtBoot: False
	#ExcludeFromLibrary: True
#End Region

Sub Process_Globals
	Public notify As Notification
	Public job1 As HttpJob
	Public cct As Long
	Public pt As Long
End Sub

Sub Service_Create
	'This is the program entry point.
	'This is a good place to load resources that are not specific to a single activity.
	Log("Iniciado o servico")
End Sub

Sub Service_Start (StartingIntent As Intent)

	StartServiceAt("", DateTime.Now + 21 * DateTime.TicksPerSecond, True)

	Dim pi As PhoneId
	Dim imee As String
	imee = pi.GetDeviceId

	'Send a GET request
	job1.Initialize("Job1", Me)
	job1.Download2("http://192.168.0.11/push_message", _
	Array As String("token", imee))
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
	Return False
End Sub

Sub Service_Destroy
	Log("Destruido")
End Sub

Sub JobDone (job As HttpJob)
	Dim retorno As String
	
	Log("JobName = " & job.JobName & ", Success = " & job.Success)
	If job.Success = True Then
		Select job.JobName
			Case "Job1"

				'print the result to the logs
				Dim retorno As String
				retorno = (job.GetString)
				
				If retorno <> "null" And retorno <> "" Then
					Dim pushmonta As String
					For i = 0 To retorno.Length - 1
						Dim c As Char = retorno.CharAt(i)
						pushmonta = pushmonta & c
						If c = "|" Then
							pushmonta = pushmonta.Replace("|","")
							push_onnotify("budmake","BudMake",pushmonta,"123")
							pushmonta = ""
							Log(pushmonta)
						End If
					Next
	
					 
				End If
				
		End Select
	Else
		ToastMessageShow("Atualizando Budmake", True)
	End If
	job.Release
End Sub

Sub push_onnotify(appid As String,title As String,body As String,uriStr As String)
	
	cct = cct + 1
	pt = pt + 1
	notify.Initialize
	notify.Icon="icon"
	notify.SetInfo(title,body,"main")
	notify.Sound=True
	notify.Light=True
	notify.AutoCancel=False
	notify.notify(pt)
	
End Sub