B4A=true Group=Default Group ModulesStructureVersion=1 Type=Class Version=12.2 @EndOfDesignText@ Sub Class_Globals 'These global variables will be declared once when the application starts. 'These variables can be accessed from all modules. ' Public GZip As GZipStrings 'Usa la libreria CompressStrings ' Private su As StringUtils 'Usa la libreria StringUtils Dim phn As Phone Dim devModel As String Dim kmt, errorLog As SQL 'Requiere la libreria "SQL" 'ignore ' Dim wifi As MLwifi Dim ssid As String 'ignore ' Dim rutaMaxPoints As Int = 3000 ' Dim rutaHrsAtras As Int = 48 ' Dim rutaInicioHoy As String = "" Private subsLogs As Boolean = False End Sub 'You can add more parameters here. Public Sub Initialize As Object Return Me End Sub 'Pone el valor de phn.Model en la variable global "devModel" Sub getPhnId As String 'ignore 'Requiere la libreria "Phone" devModel = phn.Model If devModel.Length <= 3 Then 'Si phn.Model esta en blanco ... Dim t As String = phn.GetSettings("android_id") 'Intentamos con "android_id" devModel = t End If If devModel.Length >= 3 Then 'Si tenemos valor para phn.Model File.WriteString(File.DirInternal, "phnId.txt", devModel) 'Sobreescribimos archivo phnId.txt with deviceId ' Log("Tenemos phnId: "&devModel&" "&File.DirInternal&"/phn.txt sobreescrito") Else If devModel.Length < 3 Then ' Si no tenemos valor, lo leemos de phnId.txt Dim s As String = File.ReadString(File.DirInternal, "phnId.txt") devModel = s ' Log("Leemos id de "&File.DirInternal&"/phnId.txt") ' Log(devModel) End If Return devModel End Sub 'Convierte una fecha al formato yyMMddHHmmss Sub fechaKMT(fecha As String) As String 'ignore ' Log(fecha) Dim OrigFormat As String = DateTime.DateFormat 'save orig date format DateTime.DateFormat="yyMMddHHmmss" Dim nuevaFecha As String=DateTime.Date(fecha) DateTime.DateFormat=OrigFormat 'return to orig date format ' Log(nuevaFecha) Return nuevaFecha End Sub 'Genera una notificacion con importancia alta 'Sub notiHigh(title As String, body As String, activity As Object) 'ignore ' Private notif As Notification ' notif.Initialize2(notif.IMPORTANCE_HIGH) ' notif.Icon = "icon" ' notif.Vibrate = False ' notif.Sound = False ' notif.AutoCancel = True ' Log("notiHigh: "&title) ' notif.SetInfo(title, body, activity) '' Log("notiHigh SetInfo") ' notif.Notify(777) 'End Sub 'Regresa el objeto de una notificacion con importancia baja 'Sub notiLowReturn(title As String, Body As String, id As Int) As Notification 'ignore ' Private notification As Notification ' notification.Initialize2(notification.IMPORTANCE_LOW) ' Log("notiLowReturn: "&title) ' notification.Icon = "icon" ' notification.Sound = False ' notification.Vibrate = False ' notification.SetInfo(title, Body, Main) ' notification.Notify(id) '' Log("notiLowReturn SetInfo") ' Return notification 'End Sub 'Escribimos las coordenadas y fecha a un archivo de texto Sub guardaInfoEnArchivo(coords As String) 'ignore ' Cambiamos el formato de la hora Dim OrigFormat As String=DateTime.DateFormat 'save orig date format DateTime.DateFormat="MMM-dd HH:mm:ss" Dim lastUpdate As String=DateTime.Date(DateTime.Now) DateTime.DateFormat=OrigFormat 'return to orig date format Dim ubic As String = coords&","&lastUpdate Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "gps.txt", True) Dim s As String = ubic & CRLF Dim t() As Byte = s.GetBytes("UTF-8") out.WriteBytes(t, 0, t.Length) out.Close End Sub 'Escribimos las coordenadas (latitud, longitud, fecha) y fecha a una BD Sub guardaInfoEnBD(coords As String) 'ignore Log("Guardamos ubicacion en BD - "&coords) Try Dim latlon() As String = Regex.Split("\|", coords) If latlon.Length < 2 Then latlon = Regex.Split(",", coords) 'Si son menos de 2, entonces estan separadas por comas y no por "|" If subsLogs Then Log("LatLon="&latlon) kmt.ExecNonQuery2("INSERT INTO RUTA_GPS(FECHA, LAT, LON) VALUES (?,?,?)", Array As Object (latlon(2),latlon(0),latlon(1))) Catch LogColor(LastException, Colors.red) End Try End Sub 'Limpiamos la tabla RUTA_GPS de la BD Sub deleteGPS_DB 'ignore kmt.ExecNonQuery("delete from RUTA_GPS") kmt.ExecNonQuery("vacuum;") ToastMessageShow("Borramos BD Coords GPS", False) End Sub 'Limpiamos la tabla errorLog de la BD Sub deleteErrorLog_DB 'ignore errorLog.ExecNonQuery("delete from errores") errorLog.ExecNonQuery("vacuum;") ToastMessageShow("BD Errores Borrada", False) End Sub 'Mandamos "coords" en un mensaje a "Sprvsr" 'Sub mandamosLoc(coords As String) 'ignore '' Log("Iniciamos mandamosLoc "&coords) '' Log("locRequest="&Tracker.locRequest) ' guardaInfoEnBD(coords)'Escribimos coordenadas y fecha a una bd ' Dim t As String ' If Tracker.locRequest="Activa" Then ' If PushService.au = 1 Then ' t = "au" ' es una actualizacion ' Else ' t = "u" ' es una peticion ' End If ' Dim params As Map = CreateMap("topic":"Sprvsr", "coords":coords, "t":t, "b":PushService.battery, "mt":Main.montoActual) ' CallSub2(PushService, "mandaMensaje",params) ' Tracker.locRequest="Enviada" ' CallSubDelayed(Tracker,"CreateLocationRequest") ' End If 'End Sub 'Regresa la fecha y hora de hoy a las 00:00 en el formato "yyMMddHHMMSS" Sub fechaInicioHoy As String 'ignore Dim OrigFormat As String = DateTime.DateFormat 'save orig date format DateTime.DateFormat="yyMMdd" Private h As String = DateTime.Date(DateTime.Now)&"000000" DateTime.DateFormat=OrigFormat 'return to orig date format Log("Hoy="&h) Return h End Sub 'Guardamos "texto" a la bitacora Sub log2DB(texto As String) 'ignore LogColor(fechaKMT(DateTime.Now)&" - log2BD: '"&texto&"'", Colors.LightGray) kmt.ExecNonQuery2("INSERT INTO bitacora(fecha, texto) VALUES (?,?)", Array As Object (fechaKMT(DateTime.now), texto)) End Sub 'Regresa verdadero si ya pasaron XX minutos de la fecha dada Sub masDeXXMins(hora As Int, mins As Int) As Boolean 'ignore If (hora + mins * DateTime.TicksPerMinute) < DateTime.Now Then Return True Else Return False End If End Sub 'Regresa verdadero si ya pasaron XX minutos de la fechaKMT dada Sub masDeXXMinsKMT(hora As String, mins As Int) As Boolean 'ignore Try ' LogColor($"Hora=${fechaKMT(fechaKMT2Ticks(hora) + mins * DateTime.TicksPerMinute)}, Mins=${mins}, Actual=${fechaKMT(DateTime.Now)}"$,Colors.red) If fechaKMT2Ticks(hora) + mins * DateTime.TicksPerMinute < DateTime.Now Then ' Log("+++ +++ "&fechaKMT(fechaKMT2Ticks(hora) + mins * DateTime.TicksPerMinute) & " < " & fechaKMT(DateTime.Now)) Return True Else ' Log("+++ +++ "&fechaKMT(fechaKMT2Ticks(hora) + mins * DateTime.TicksPerMinute) & " > " & fechaKMT(DateTime.Now)) Return False End If Catch Log(LastException) End Try End Sub 'Limpiamos la tabla "bitacora" de la BD Sub borraLogDB 'ignore LogColor("Borramos BD de log", Colors.Magenta) kmt.ExecNonQuery("delete from bitacora") kmt.ExecNonQuery("vacuum;") End Sub 'Monitoreamos los servicios para ver si estan activos (No pausados), y si no, los reniciamos 'Sub Monitor 'ignore ' Private monitorStatus As Boolean = True ' LogColor("Corriendo Subs.Monitor", Colors.RGB(161,150,0)) ' If IsPaused(Tracker) Then ' log2DB("Reiniciando 'Tracker Pausado' desde Subs.Monitor") ' StartService(Tracker) ' monitorStatus = False ' Else ' CallSubDelayed(Tracker, "revisaFLP") ' End If ' If IsPaused(PushService) Then ' log2DB("Reiniciando 'PushService Pausado' desde Subs.Monitor") ' StartService(PushService) ' monitorStatus = False ' Else ' revisaPushService ' End If ' If monitorStatus Then LogColor(" +++ +++ Servicios Activos", Colors.Green) 'End Sub 'Convierte una fecha en formato YYMMDDHHMMSS a Ticks Sub fechaKMT2Ticks(fKMT As String) As Long 'ignore Try If fKMT.Length = 12 Then Private parteFecha As String = fKMT.SubString2(0,6) Private parteHora As String = fKMT.SubString(6) Private OrigFormat As String = DateTime.DateFormat 'save original date format DateTime.DateFormat="yymmdd" DateTime.TimeFormat="HHmmss" Private ticks As Long = DateTime.DateTimeParse(parteFecha,parteHora) DateTime.DateFormat=OrigFormat 'return to original date format Return ticks Else Log("Formato de fecha incorrecto, debe de ser 'YYMMDDHHMMSS', no '"&fKMT&"' largo="&fKMT.Length) Return 0 End If Catch Log(LastException) LogColor($"Fecha dada: ${fKMT}, Parte Fecha: ${parteFecha}, Parte Hora: ${parteHora}"$, Colors.Red) Return 0 End Try End Sub Sub InstallAPK(dir As String, apk As String) 'ignore If File.Exists(dir, apk) Then Dim i As Intent i.Initialize(i.ACTION_VIEW, "file://" & File.Combine(dir, apk)) i.SetType("application/vnd.android.package-archive") StartActivity(i) End If End Sub 'Copia la base de datos del almacenamiento interno al externo en el directorio kmts Sub copiaDB(result As Boolean) 'ignore ToastMessageShow("copiaDB", False) If result Then Dim p As String If File.ExternalWritable Then p = File.DirRootExternal ' Log("Externo") Else p = File.DirInternal ' Log("Interno") End If Dim theDir As String Try File.MakeDir(File.DirRootExternal,"kmts") theDir = "/kmts" Catch theDir = "" End Try Try File.Copy(File.DirInternal,"kmt.db",File.DirRootExternal&theDir,"cedex_kmt.db") File.Copy(File.DirInternal,"errorLog.db",File.DirRootExternal&theDir,"cedex_errorLog.db") ToastMessageShow("BD copiada!", False) Catch ToastMessageShow("No se pudo hacer la copia: "&LastException, True) End Try Log("rootExternal="&p) Log("File.DirInternal="&File.DirInternal) Log("File.DirRootExternal="&File.DirRootExternal) Else ToastMessageShow("Sin permisos", False) End If End Sub 'Hace visible y trae al frente el panel con los parametros "Top" y "Left" dados Sub panelVisible(panel As Panel, top As Int, left As Int) 'ignore panel.BringToFront panel.Visible = True panel.Top = top panel.Left = left End Sub 'Centra una etiqueta dentro de un elemento superior Sub centraEtiqueta(elemento As Label, anchoElementoSuperior As Int) 'ignore elemento.Left = Round(anchoElementoSuperior/2)-(elemento.Width/2) End Sub 'Centra un panel horizontalmente dentro de un elemento superior Sub centraPanel(elemento As Panel, anchoElementoSuperior As Int) 'ignore elemento.Left = Round(anchoElementoSuperior/2)-(elemento.Width/2) End Sub 'Centra un panel verticalmente dentro de un elemento superior Sub centraPanelV(elemento As Panel, altoElementoSuperior As Int) 'ignore elemento.Top = Round(altoElementoSuperior/2)-(elemento.Height/2) End Sub 'Centra una barra de progreso dentro de un elemento superior Sub centraProgressBar(elemento As ProgressBar, anchoElementoSuperior As Int) 'ignore elemento.Left = Round(anchoElementoSuperior/2)-(elemento.Width/2) End Sub 'Regresa el usuario de la tabla USUARIOA si es que existe, si no existe, regresa "SinUsuario". Sub buscaDBUsuario As String 'ignore Private c As Cursor Private usuario As String = "SinUsuario" c=kmt.ExecQuery("select USUARIO from usuarioa") c.Position=0 If c.RowCount > 0 Then usuario = c.GetString("USUARIO") Return usuario End Sub Sub traeFecha As String 'ignore DateTime.DateFormat = "MM/dd/yyyy" Private sDate As String =DateTime.Date(DateTime.Now) Private sTime As String =DateTime.Time(DateTime.Now) Return sDate & sTime End Sub 'Guarda el nombre y version de la app en CAT_VARIABLES. Sub guardaAppInfo(skmt As SQL) 'ignore skmt.ExecNonQuery("delete from CAT_VARIABLES where CAT_VA_DESCRIPCION = 'EMPRESA' or CAT_VA_DESCRIPCION = 'APP_NAME' or CAT_VA_DESCRIPCION = 'APP_VERSION'") skmt.ExecNonQuery($"insert into CAT_VARIABLES (CAT_VA_DESCRIPCION, CAT_VA_VALOR) values ('APP_NAME', '${Application.LabelName}')"$) skmt.ExecNonQuery($"insert into CAT_VARIABLES (CAT_VA_DESCRIPCION, CAT_VA_VALOR) values ('APP_VERSION', '${Application.VersionName}')"$) End Sub 'Muestra en el Log los campos y valores que regresan en el JobDone. Sub logJobDoneResultados(resultado As DBResult) 'ignore For Each records() As Object In resultado.Rows LogColor($"====== ${resultado.Tag} - REGISTROS = ${resultado.Rows.Size}"$, Colors.RGB(215,37,0)) For Each k As String In resultado.Columns.Keys LogColor(k & " = " & records(resultado.Columns.Get(k)), Colors.RGB(215,37,0)) Next Next End Sub 'Regresa la base de datos espscificada ya inicializada. Sub inicializaBD(ruta As String, BDName As String) As SQL Dim skmt As SQL If File.Exists(ruta, BDName) = False Then File.Copy(File.DirAssets, BDName, ruta, BDName) LogColor($"Copiamos ${BDName} de ${File.DirAssets} a ${ruta}"$,Colors.Green) End If skmt.Initialize(ruta, BDName, True) Return skmt End Sub 'Agrega una columna a la tabla especificada. 'Hay que indicar el "tipo" de la columna (TEXT, INTEGER, ETC) 'Ej. agregaColumna("TABLA", "COLUMNA", "TIPO") Sub agregaColumna(tabla As String, columna As String, tipo As String) 'ignore Try 'Intentamos usar "pragma_table_info" para revisar si existe la columna en la tabla Private c As Cursor = Starter.skmt.ExecQuery($"SELECT COUNT(*) AS fCol FROM pragma_table_info('${tabla}') WHERE name='${columna}'"$) c.Position = 0 If c.GetString("fCol") = 0 Then 'Si no esta la columna la agregamos Starter.skmt.ExecNonQuery($"ALTER TABLE ${tabla} ADD COLUMN ${columna} ${tipo}"$) Log($"Columna "${columna} ${tipo}", agregada a "${tabla}"."$) End If Catch 'Si no funciona "pragma_table_info" lo hacemos con try/catch Try Starter.skmt.ExecNonQuery($"ALTER TABLE ${tabla} ADD COLUMN ${columna} ${tipo}"$) Log($"Columna "${columna} ${tipo}", agregada a "${tabla}".."$) Catch Log(LastException) End Try End Try End Sub 'Regresa el el DBReqServer desde la base de datos o "N/A" si no existe. Sub traeDBReqServerDeBD As String 'ignore Dim srvr As String = "N/A" Dim rs As ResultSet = Starter.skmt.ExecQuery("select valor from cat_variables where nombre = 'servidor'") If rs.RowCount > 0 Then rs.NextRow srvr = rs.GetString("valor") End If Return srvr End Sub 'Regresa el el intervalo desde la base de datos o "30" si no existe. Sub traeIntervaloDeBD As String 'ignore Dim intrvl As String = "30" Dim rs As ResultSet = Starter.skmt.ExecQuery("select valor from cat_variables where nombre = 'intervalo'") If rs.RowCount > 0 Then rs.NextRow intrvl = rs.GetString("valor") End If Return intrvl End Sub 'Regresa el timeout desde la base de datos o "11000" si no existe. Sub traeTimeoutDeBD As String 'ignore Dim tmout As String = "11000" Dim rs As ResultSet = Starter.skmt.ExecQuery("select valor from cat_variables where nombre = 'timeout'") If rs.RowCount > 0 Then rs.NextRow tmout = rs.GetString("valor") End If Return tmout End Sub Sub CreateNotification (Body As String) As Notification Dim notification As Notification notification.Initialize2(notification.IMPORTANCE_LOW) notification.Icon = "icon" notification.SetInfo("Activo", Body, Main) Return notification End Sub 'Genera una notificacion con importancia alta Sub notiHigh(title As String, body As String, id As String, activity As Object) 'ignore activity = Main Private notif As Notification notif.Initialize2(notif.IMPORTANCE_HIGH) notif.Icon = "icon" notif.Vibrate = False notif.Sound = False notif.AutoCancel = True ' If logger Then Log("notiHigh: "&title) notif.SetInfo(title, body, activity) ' Log("notiHigh SetInfo") notif.Notify(id) End Sub 'Regresa el objeto de una notificacion con importancia baja Sub notiLowReturn(title As String, Body As String, id As Int) As Notification 'ignore Private notification As Notification notification.Initialize2(notification.IMPORTANCE_LOW) ' Log("notiLowReturn: "&title) notification.Icon = "icon" notification.Sound = False notification.Vibrate = False notification.SetInfo(title, Body, Main) notification.Notify(id) ' Log("notiLowReturn SetInfo") Return notification End Sub