Spanish (B4A) ¿Cómo evitar que el usuario falsee la fecha, hora y DST en el terminal?

Seneca

Active Member
Licensed User
Hola.

Estoy desarollando una app que muestra noticias alojadas en una BD. Todas las noticias tienen una fecha de inicio y otra de fin, las cuáles delimitan durante qué días pueden ser vistas.

Quiero evitar que el usuario pueda acceder a noticias ya expiradas o a noticias que, aún estando ya alojadas en la BD, todavía es pronto para ser visualizadas. Para hacer esto bastaría con que el usuario cambie la fecha del terminal, de forma que la consulta a la BD extraería las noticias que no corresponden a la fecha real.

Estoy buscando la forma de que la app conozca la fecha, hora y DST (horario verano/invierno) real, al margen de la que tenga configurada el dispositivo, para evitar asi el problema anterior.

He combinado dos ejemplos que he encontrado indagando en distintos hilos, y he conseguido que la app defina de manera obligatoria la Zona Horaria a la de España y luego me indique la fecha y hora reales, las cuales son consultadas a un servidor NTP. (Todo esto sin cambiar fecha ni hora en el terminal)

El ejemplo se basa en las librerías SPSntp y Threading. Tengo duda si puedo prescindir de esta última.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim Thread1 As Thread 'ignore => ignore warning of this line
    Dim Thread2 As Thread 'ignore => ignore warning of this line
    Dim ticks As Long
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim BtnExit As Button
    Dim BtnGetTime As Button
    Dim sntp As SntpTime
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'DateTime.SetTimeZone(0)
    SetTimeZone("Europe/Madrid")
    Activity.LoadLayout("standardLayout")
    If FirstTime = True Then
        Thread1.Initialise("Thread1")
        Thread2.Initialise("Thread2")
    End If
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    Thread1.Interrupt
End Sub

Sub BtnGetTime_Click
    Dim args(0) As Object
    ' since android 3 all network access must be done within an background thread
    Thread1.Start(Me, "Get_Current_Time", args)
End Sub

Sub Thread1_Ended(fail As Boolean, error As String)
    Dim args(0) As Object
    ' to perfom giu-actions directly after receiving the time
    ' we have to do this with the help of a separate gui-thread
    Thread2.RunOnGuiThread("View_Results", args)
End Sub

' must be startet in a non-gui background thread
' the undescores prevents the sub name from obfuscation, so Thread1.Start will find this sub even with the compile option "Relaese (obfuscated)"
Sub Get_Current_Time
    sntp.NtpServerName = "0.de.pool.ntp.org"  ' only needed if you don't want to use the default server "0.us.pool.ntp.org"
    sntp.TimeOut = 10000                      ' only needed if you don't want to use the default timeout value of 30000 milliseconds (30 seconds)
    ticks = sntp.getGmtNtpTime
End Sub

' the undescores prevents the sub name from obfuscation, so Thread1.Start will find this sub even with the compile option "Relaese (obfuscated)"
Sub View_Results
    Msgbox(ticks, "GMT Result in Ticks")
    Msgbox(DateTime.Date(ticks), "GMT Date")
    Msgbox(DateTime.Time(ticks), "GMT Time")
    Msgbox(DateTime.Time(sntp.NtpTimeReference), "NtpTimeReference")
    Msgbox(sntp.RoundTripTime, "RoundTripTime")
End Sub

Sub BtnExit_Click
    Activity.Finish
End Sub


Sub SetTimeZone(id As String)
    Dim r As Reflector
    Dim idTz As Object = r.RunStaticMethod("java.util.TimeZone", "getTimeZone", Array As String(id), _
  Array As String("java.lang.String"))
    r.RunStaticMethod("anywheresoftware.b4a.keywords.DateTime", "setTimeZoneInternal", _
  Array As Object(idTz), Array As String("java.util.TimeZone"))
End Sub

¿Puede alguien asesorarme si puedo sustituir las llamadas a la librería Tread por un Servicio, que una vez obtenga la fecha/hora del servidor sea finalizado?

¿Conoce alguien un método alternativo para lo que pretendo mejor que este?

Gracias.
 
Top