Files
FLP_2.0/NotificationService.bas
Jose Alberto Guerra Ugalde 59b0831740 - Se paso la logica y restricciones de Tracker.FLP_LocationChanged a Subs.mandaLoc2.
- Se puso que con cualquier notificacion revise si puede mandar la ubicacion.
- Se puso que cuando se solicite la ubicacion con FirebaseMessaging, se mande tambien a PUSH_INFO.
2024-05-11 04:27:54 -06:00

215 lines
8.7 KiB
QBasic

B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Service
Version=11
@EndOfDesignText@
#Region Service Attributes
#StartAtBoot: False
#End Region
'##################################################################################################
'Requiere las liberias: Reflection y NotificationListener}
'##################################################################################################
'Agregar estas lienas al manifiesto:
'
'AddApplicationText(
'<Service android:name="b4a.jsaplication.com.br.ReplyAuto"
' android:label="Mariana" android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
' <intent-filter><action android:name="android.service.notification.NotificationListenerService" /></intent-filter>
' </Service>)
'
'Lo siguiente es para revisar que tenga autorización para interceptar notificaciones, hay que ponerlo en Main o Starter:
' If Not(CheckNotificationAccess) Then
' Msgbox2Async($"Se necesita acceso a las notificaciones, haga clic en "Aceptar" y en la siguiente pantalla permita el acceso a la aplicación "${Application.LabelName}"."$, "Permisos necesarios", "Aceptar", "Cancelar", "", Null, True)
' Wait For Msgbox_Result (resultado As Int)
' If resultado = DialogResponse.POSITIVE Then
' Dim In As Intent
' In.Initialize("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS", "")
' StartActivity(In)
' End If
' End If
'
''Revisa si la aplicación tiene permiso para acceder a las notificaciones.
'Sub CheckNotificationAccess As Boolean
' Dim ph As Phone
' Dim nstr, pstr As String
' Dim r As Reflector
' pstr = r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
' nstr = ph.GetSettings("enabled_notification_listeners")
' Return nstr.Contains(pstr)
'End Sub
'##################################################################################################
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim rp As ReplyAuto
' Dim activo As Boolean = True
' Dim ultimaNoti As String
Dim logger As Boolean = False 'ignore
End Sub
Sub Service_Create
rp.Initialize("NotiMon")
#if not(DEBUG)
logger = False
#end if
Subs.revisaBD
' If logger Then Log("**************** Iniciamos Monitor Keymon ***********************")
End Sub
Sub Service_Start (StartingIntent As Intent)
' Log("NotificationService Start")
If rp.HandleIntent(StartingIntent) Then Return
' DateTime.DateFormat = "mm"
' ultimaNoti = DateTime.Date(DateTime.now)
' If logger Then LogColor($"Ultima notificación en el minuto ${ultimaNoti}"$, Colors.green)
End Sub
Sub Service_Destroy
End Sub
Sub NotiMon_NotificationPosted (SBN As StatusBarNotification)
Subs.revisaYmandaUUC
If Starter.logger Then LogColor(SBN.PackageName & " - " & SBN.Message, Colors.blue)
' Log("-= Notificacion recibida =-")
' If SBN.PackageName = "com.whatsapp" Then
' If logger Then LogColor(SBN.PackageName, Colors.Red)
' If logger Then LogColor("isGroupWA2: "&isGroupWA(SBN),Colors.Magenta)
' If logger Then LogColor("isPersonWA: "&isPersonWA(SBN),Colors.Magenta)
' If logger Then Log($"getGroupName: |${getGroupName(SBN.Title)}|"$)
' Si recibimos un mensaje con la palabra "donde", "Donde" o "bien", entonces mandamos la última ubicación conocida.
' If SBN.Message.Contains("donde") Or SBN.Message.Contains("Donde") Or SBN.Message.Contains("bien") Then 'Si el mensaje contiene "#NS" y tiene un segundo parametro ...
' If esMensajeWAValido(SBN) Then
' Subs.bitacora($"Recibimos notificación, pedimos actualización y mandamos UUC."$)
If Starter.logger Then LogColor($"Recibimos notificación, pedimos actualización y mandamos UUC."$, Colors.Green)
If Tracker.flp.IsInitialized Then Tracker.flp.RequestLocationUpdates(Tracker.locReqSmall)
Sleep(2000)
Private ultimaLoc As Location = Subs.traeUltimaUbicacionGuardada
Subs.mandaLoc2(ultimaLoc, Starter.devModel)
' End If
' End If
' End If
End Sub
'Regresa verdadero si el mensaje de whatsapp es un mensaje valido.
Sub esMensajeWAValido(SBN As StatusBarNotification) As Boolean 'ignore
Private valido As Boolean = False
Private ww() As String = Regex.Split("\|", SBN.Key)
If ww(3) <> Null And ww(3) <> "null" Then valido = True
Return valido
End Sub
'Returns TRUE if the sender is a GROUP.
'Searches the provided sbn.title for the string ": " to know if the sender is a group.
Sub isGroupWA2(sbnTitle As String) As Boolean 'ignore
Private x As Boolean = Regex.ismatch(".*(: ).*", sbnTitle)
Return x
End Sub
'Returns TRUE if the sender is a GROUP.
'Searches the provided notification object for the string "@g.us" and if found returns TRUE.
Sub isGroupWA(sbn As StatusBarNotification) As Boolean 'ignore
Private a As Boolean = False
If sbn.As(String).IndexOf("@g.us") > -1 Then a = True 'ignore
Return a
End Sub
'Returns TRUE if the sender is a PERSON.
'Searches the provided notification object for the string "@s.whatsapp.net" and if found returns TRUE.
Sub isPersonWA(sbn As StatusBarNotification) As Boolean 'ignore
Private a As Boolean = False
If sbn.As(String).IndexOf("@s.whatsapp.net") > -1 Then a = True 'ignore
Return a
End Sub
'Returns TRUE if the sender is a PERSON.
'Searches the provided notification object for the string "channel=individual" and if found returns TRUE.
Sub isPersonWA2(sbn As StatusBarNotification) As Boolean 'ignore
Private a As Boolean = False
If sbn.As(String).IndexOf("channel=individual") > -1 Then a = True 'ignore
Return a
End Sub
'Returns the sender's number.
'Searches the provided notification object and gets the numbers between "shortcut=" and "@s.whatsapp.net".
Sub getNumberWA(sbn As StatusBarNotification) As String
Private a As Int = sbn.As(String).IndexOf("@s.whatsapp.net") 'ignore
If a > -1 Then
Private x As String = sbn.As(String) 'ignore
Private y As Int = x.IndexOf("shortcut=")
If (y+9) > 0 And a > (y+9) Then x = x.SubString2(y+9, a) Else x = "Not a person"
Else 'It is probably is a group.
x = "Not a person"
End If
Return x
End Sub
'Returns the name of the group from the given text.
'If it is not a group, then returns the notification's title.
Sub getGroupName(sbnTitle As String) As String 'ignore
Private a As Int = sbnTitle.IndexOf(": ")
Private x As String = sbnTitle
If a > -1 Then
Private b As String = sbnTitle.SubString2(0, a)
x = Regex.Replace(" \(.+\)", b, "")
End If
Return x
End Sub
'Returns the name of the group from the given notification object.
'Searches the provided notification for the string "hiddenConversationTitle" and if found, gets the name.
'If it is not a group,then it returns the notification's title.
Sub getGroupName2(sbn As StatusBarNotification) As String 'ignore
Private inicio As Int = sbn.Extras.As(String).IndexOf("hiddenConversationTitle=") 'ignore
If inicio > -1 And sbn.Extras.As(String).IndexOf("hiddenConversationTitle=null") = -1 Then 'ignore
Private x As String = sbn.Extras.As(String) 'ignore
Private fin As Int = x.IndexOf(", android.reduced.images=")
x = x.SubString2(inicio+24, fin)
x = Regex.Replace(" \(.+\)", x, "") 'Replace anything between () with "", this en the case that we have something like "MyGroupName (5 messages)"
Else 'Is not from a group.
Private x As String = sbn.Title
End If
Return x
End Sub
'Returns the person's name (or the number) when the message comes from a group.
'Searches the provided sbn.title for the string ": " in the title and returns the string after that,
'if it does not find ": " then returns the complete string.
Sub getPersonFromGroup(sbnTitle As String) As String 'ignore
Private a As Int = sbnTitle.IndexOf(": ")
If a = -1 Then a = -2 'Is not from a group.
Private b As String = sbnTitle.SubString(a+2)
Return b
End Sub
'Returns the NUMBER of the sender and if NOT a person, then returns the name of the group.
Sub getNumberOrGroupWA(sbn As StatusBarNotification) As String 'ignore
Private numRemitente As String = getNumberWA(sbn)
If numRemitente = "Not a person" Then numRemitente = getGroupName(sbn.Title)
Return numRemitente
End Sub
'Regresa el "shortcut" del remitente.
'Si es de un grupo, es algo como "120363023512345678@g.us"
'Si es de una persona, entonces "5215512345678@s.whatsapp.net"
Sub getShortcut(sbn As StatusBarNotification) As String 'ignore
Private ap As Int = sbn.As(String).IndexOf("@s.whatsapp.net") 'ignore
Private ag As Int = sbn.As(String).IndexOf("@g.us") 'ignore
Private x As String = sbn.As(String) 'ignore
Private y As Int = x.IndexOf("shortcut=")
If ap > -1 Then
Private x As String = sbn.As(String) 'ignore
Private y As Int = x.IndexOf("shortcut=")
x = x.SubString2(y+9, ap+15)
Else if ag > -1 Then 'It is probably is a group.
Private x As String = sbn.As(String) 'ignore
Private y As Int = x.IndexOf("shortcut=")
x = x.SubString2(y+9, ag+5)
End If
Return x
End Sub