#Region Module Attributes
#StartAtBoot: False
#End Region
'Service module
'Version 0.9
Sub Process_Globals
End Sub
Sub Service_Create
Log("SERVICE PUSH")
End Sub
Sub Service_Start (StartingIntent As Intent)
Try
Select StartingIntent.Action
Case "com.google.android.c2dm.intent.REGISTRATION"
HandleRegistrationResult(StartingIntent)
Case "com.google.android.c2dm.intent.RECEIVE"
MessageArrived(StartingIntent)
End Select
Catch
End Try
End Sub
Sub MessageArrived (Intent As Intent)
'Log(Intent.ExtrasToString)
If Intent.GetExtra("code")="status" Then
Dim sqlString As String="select isOnline from accesos where hashAcceso='" & Intent.GetExtra("hashAcceso") & "'"
Dim C1 As Cursor
C1=x.SQLDB.ExecQuery(sqlString)
If C1.RowCount>0 Then
C1.Position=0
Dim WhereFields As Map
WhereFields.Initialize
WhereFields.Put("hashAcceso", Intent.GetExtra("hashAcceso"))
DBUtils.UpdateRecord(x.SQLDB, "accesos", "isOnline", Intent.GetExtra("statusCode"), WhereFields)
End If
C1.Close
End If
If x.mac="adetail" Then
CallSubDelayed2(aDetail, "msgArrival", Intent)
CallSubDelayed(aDetail, "updateOnlineStatus")
Else If x.mac="amain" AND Intent.GetExtra("code")="status" Then
CallSubDelayed2(aMain, "msgArrival", Intent)
End If
End Sub
Sub RegisterDevice (Unregister As Boolean)
Dim i As Intent
If Unregister Then
i.Initialize("com.google.android.c2dm.intent.UNREGISTER", "")
Else
i.Initialize("com.google.android.c2dm.intent.REGISTER", "")
i.PutExtra("sender", Main.GoogleProjectNumber)
End If
Dim r As Reflector
Dim i2 As Intent
i2 = r.CreateObject("android.content.Intent")
Dim pi As Object
pi = r.RunStaticMethod("android.app.PendingIntent", "getBroadcast", _
Array As Object(r.GetContext, 0, i2, 0), _
Array As String("android.content.Context", "java.lang.int", "android.content.Intent", "java.lang.int"))
i.PutExtra("app", pi)
StartService(i)
End Sub
Sub HandleRegistrationResult(Intent As Intent)
Dim url As String="http://www.llavemovil.com/api/ws/push.id.reg.aspx"
Dim data As String = ""
If Intent.HasExtra("error") Then
'Log("Error: " & Intent.GetExtra("error"))
'ToastMessageShow("Error: " & Intent.GetExtra("error"), True)
Else If Intent.HasExtra("unregistered") Then
Dim j As HttpJob
x.WriteMap("pushId", "")
j.Initialize("RegisterTask", Me)
data="token=" & x.ReadMap("token") & "&movil=" & x.ReadMap("movil") & "&pushId=" & x.ReadMap("pushId")
j.PostString(url, data)
'ToastMessageShow("http://www.llavemovil.com/api/ws/push.id.reg.aspx?token=" & x.ReadMap("token") & "&movil=" & x.ReadMap("movil") & "&pushId=" & x.ReadMap("pushId"), True)
Else If Intent.HasExtra("registration_id") Then
Dim rid As String
rid = Intent.GetExtra("registration_id")
Dim j As HttpJob
'Log(rid)
data="token=" & x.ReadMap("token") & "&pin=" & x.ReadMap("pin") & "&movil=" & x.ReadMap("movil") & "&pushId=" & x.ReadMap("pushId")
x.WriteMap("pushId", rid)
'Log(url & "?" & data)
j.Initialize("RegisterTask", Me)
j.PostString(url, data)
End If
End Sub
Sub JobDone(Job As HttpJob)
If Job.Success Then
Select Job.JobName
Case "RegisterTask"
'ToastMessageShow("Registration completed successfully.", False)
Case "UnregisterTask"
'ToastMessageShow("Unregistration completed successfully.", False)
End Select
Else
'ToastMessageShow("Error sending request.", True)
Log(Job.ErrorMessage)
End If
Job.Release
End Sub
Sub Service_Destroy
End Sub