Files
Izca_Multiventa/B4A/NotificationService.bas
2025-09-15 17:40:09 -06:00

218 lines
8.1 KiB
QBasic

B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Service
Version=11
@EndOfDesignText@
#Region Service Attributes
#StartAtBoot: False
#End Region
'******************************************************************************
'Se necesita la libreria ReplyAuto.
'No olvidar agregar esta linea al editor de 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>)
'En B4XPage_Appear de MainPage agregar estas lineas:
'
'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
'
'Y al final de Mainpage agregar esta funcion:
'
''Revisa si la aplicación tiene permiso para acceder a las notificaciones.
'Sub CheckNotificationAccess As Boolean 'ignore
' 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
End Sub
Sub Service_Create
rp.Initialize("NotiMon")
#if not(DEBUG)
logger = False
#end if
' 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)
Private logger As Boolean = True
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)}|"$)
Private cmd() As String = Regex.Split(" ", SBN.Message)
If SBN.Message.StartsWith("#NS") And cmd.Length = 2 Then 'Si el mensaje contiene "#NS" y tiene un segundo parametro ...
If esMensajeWAValido(SBN) Then
' Log(cmd(1))
rp.ClearNotification(SBN)
Starter.reinicializaReqManager(cmd(1))
rp.reply(SBN.Notification, SBN.PackageName, $"Servidor cambiado a ${cmd(1)} "$)
Sleep(1000)
' rp.ClearNotification(SBN)
rp.ClearAll
End If
End If
End If
End Sub
'Regresa verdadero si el mensaje de whatsapp es un mensaje valido.
Sub esMensajeWAValido(SBN As StatusBarNotification)
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
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