package b4a.example;


import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.objects.ServiceHelper;
import anywheresoftware.b4a.debug.*;

public class starter extends  android.app.Service{
	public static class starter_BR extends android.content.BroadcastReceiver {

		@Override
		public void onReceive(android.content.Context context, android.content.Intent intent) {
            BA.LogInfo("** Receiver (starter) OnReceive **");
			android.content.Intent in = new android.content.Intent(context, starter.class);
			if (intent != null)
				in.putExtra("b4a_internal_intent", intent);
            ServiceHelper.StarterHelper.startServiceFromReceiver (context, in, true, anywheresoftware.b4a.ShellBA.class);
		}

	}
    static starter mostCurrent;
	public static BA processBA;
    private ServiceHelper _service;
    public static Class<?> getObject() {
		return starter.class;
	}
	@Override
	public void onCreate() {
        super.onCreate();
        mostCurrent = this;
        if (processBA == null) {
		    processBA = new anywheresoftware.b4a.ShellBA(this, null, null, "b4a.example", "b4a.example.starter");
            if (BA.isShellModeRuntimeCheck(processBA)) {
                processBA.raiseEvent2(null, true, "SHELL", false);
		    }
            try {
                Class.forName(BA.applicationContext.getPackageName() + ".main").getMethod("initializeProcessGlobals").invoke(null, null);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            processBA.loadHtSubs(this.getClass());
            ServiceHelper.init();
        }
        _service = new ServiceHelper(this);
        processBA.service = this;
        
        if (BA.isShellModeRuntimeCheck(processBA)) {
			processBA.raiseEvent2(null, true, "CREATE", true, "b4a.example.starter", processBA, _service, anywheresoftware.b4a.keywords.Common.Density);
		}
        if (!true && ServiceHelper.StarterHelper.startFromServiceCreate(processBA, false) == false) {
				
		}
		else {
            processBA.setActivityPaused(false);
            BA.LogInfo("*** Service (starter) Create ***");
            processBA.raiseEvent(null, "service_create");
        }
        processBA.runHook("oncreate", this, null);
        if (true) {
			ServiceHelper.StarterHelper.runWaitForLayouts();
		}
    }
		@Override
	public void onStart(android.content.Intent intent, int startId) {
		onStartCommand(intent, 0, 0);
    }
    @Override
    public int onStartCommand(final android.content.Intent intent, int flags, int startId) {
    	if (ServiceHelper.StarterHelper.onStartCommand(processBA, new Runnable() {
            public void run() {
                handleStart(intent);
            }}))
			;
		else {
			ServiceHelper.StarterHelper.addWaitForLayout (new Runnable() {
				public void run() {
                    processBA.setActivityPaused(false);
                    BA.LogInfo("** Service (starter) Create **");
                    processBA.raiseEvent(null, "service_create");
					handleStart(intent);
                    ServiceHelper.StarterHelper.removeWaitForLayout();
				}
			});
		}
        processBA.runHook("onstartcommand", this, new Object[] {intent, flags, startId});
		return android.app.Service.START_NOT_STICKY;
    }
    public void onTaskRemoved(android.content.Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        if (true)
            processBA.raiseEvent(null, "service_taskremoved");
            
    }
    private void handleStart(android.content.Intent intent) {
    	BA.LogInfo("** Service (starter) Start **");
    	java.lang.reflect.Method startEvent = processBA.htSubs.get("service_start");
    	if (startEvent != null) {
    		if (startEvent.getParameterTypes().length > 0) {
    			anywheresoftware.b4a.objects.IntentWrapper iw = ServiceHelper.StarterHelper.handleStartIntent(intent, _service, processBA);
    			processBA.raiseEvent(null, "service_start", iw);
    		}
    		else {
    			processBA.raiseEvent(null, "service_start");
    		}
    	}
    }
	
	@Override
	public void onDestroy() {
        super.onDestroy();
        if (true) {
            BA.LogInfo("** Service (starter) Destroy (ignored)**");
        }
        else {
            BA.LogInfo("** Service (starter) Destroy **");
		    processBA.raiseEvent(null, "service_destroy");
            processBA.service = null;
		    mostCurrent = null;
		    processBA.setActivityPaused(true);
            processBA.runHook("ondestroy", this, null);
        }
	}

@Override
	public android.os.IBinder onBind(android.content.Intent intent) {
		return null;
	}
public anywheresoftware.b4a.keywords.Common __c = null;
public static boolean _connected = false;
public static anywheresoftware.b4a.objects.SocketWrapper _client = null;
public static anywheresoftware.b4a.objects.SocketWrapper.ServerSocketWrapper _server = null;
public static anywheresoftware.b4a.randomaccessfile.AsyncStreams _astream = null;
public static int _port = 0;
public static boolean _working = false;
public static boolean _return_data_tplink = false;
public b4a.example.main _main = null;
public b4a.example.get_data _get_data = null;
public static void  _connecttoserver(String _host) throws Exception{
RDebugUtils.currentModule="starter";
if (Debug.shouldDelegate(processBA, "connecttoserver", false))
	 {Debug.delegate(processBA, "connecttoserver", new Object[] {_host}); return;}
ResumableSub_ConnectToServer rsub = new ResumableSub_ConnectToServer(null,_host);
rsub.resume(processBA, null);
}
public static class ResumableSub_ConnectToServer extends BA.ResumableSub {
public ResumableSub_ConnectToServer(b4a.example.starter parent,String _host) {
this.parent = parent;
this._host = _host;
}
b4a.example.starter parent;
String _host;
boolean _successful = false;

@Override
public void resume(BA ba, Object[] result) throws Exception{
RDebugUtils.currentModule="starter";

    while (true) {
        switch (state) {
            case -1:
return;

case 0:
//C
this.state = 1;
RDebugUtils.currentLine=917505;
 //BA.debugLineNum = 917505;BA.debugLine="Log(\"Trying to connect to: \" & Host)";
anywheresoftware.b4a.keywords.Common.LogImpl("1917505","Trying to connect to: "+_host,0);
RDebugUtils.currentLine=917506;
 //BA.debugLineNum = 917506;BA.debugLine="CloseExistingConnection";
_closeexistingconnection();
RDebugUtils.currentLine=917507;
 //BA.debugLineNum = 917507;BA.debugLine="Dim client As Socket";
parent._client = new anywheresoftware.b4a.objects.SocketWrapper();
RDebugUtils.currentLine=917508;
 //BA.debugLineNum = 917508;BA.debugLine="client.Initialize(\"client\")";
parent._client.Initialize("client");
RDebugUtils.currentLine=917509;
 //BA.debugLineNum = 917509;BA.debugLine="client.Connect(Host, PORT, 10000)";
parent._client.Connect(processBA,_host,parent._port,(int) (10000));
RDebugUtils.currentLine=917510;
 //BA.debugLineNum = 917510;BA.debugLine="Wait For Client_Connected (Successful As Boolean)";
anywheresoftware.b4a.keywords.Common.WaitFor("client_connected", processBA, new anywheresoftware.b4a.shell.DebugResumableSub.DelegatableResumableSub(this, "starter", "connecttoserver"), null);
this.state = 7;
return;
case 7:
//C
this.state = 1;
_successful = (Boolean) result[0];
;
RDebugUtils.currentLine=917512;
 //BA.debugLineNum = 917512;BA.debugLine="If Successful Then";
if (true) break;

case 1:
//if
this.state = 6;
if (_successful) { 
this.state = 3;
}else {
this.state = 5;
}if (true) break;

case 3:
//C
this.state = 6;
RDebugUtils.currentLine=917514;
 //BA.debugLineNum = 917514;BA.debugLine="astream.Initialize(client.InputStream, client.Ou";
parent._astream.Initialize(processBA,parent._client.getInputStream(),parent._client.getOutputStream(),"astream");
RDebugUtils.currentLine=917516;
 //BA.debugLineNum = 917516;BA.debugLine="UpdateState (True)";
_updatestate(anywheresoftware.b4a.keywords.Common.True);
 if (true) break;

case 5:
//C
this.state = 6;
RDebugUtils.currentLine=917519;
 //BA.debugLineNum = 917519;BA.debugLine="Log(\"Failed to connect: \" & LastException)";
anywheresoftware.b4a.keywords.Common.LogImpl("1917519","Failed to connect: "+BA.ObjectToString(anywheresoftware.b4a.keywords.Common.LastException(processBA)),0);
 if (true) break;

case 6:
//C
this.state = -1;
;
RDebugUtils.currentLine=917521;
 //BA.debugLineNum = 917521;BA.debugLine="End Sub";
if (true) break;

            }
        }
    }
}
public static String  _disconnect() throws Exception{
RDebugUtils.currentModule="starter";
if (Debug.shouldDelegate(processBA, "disconnect", false))
	 {return ((String) Debug.delegate(processBA, "disconnect", null));}
RDebugUtils.currentLine=983040;
 //BA.debugLineNum = 983040;BA.debugLine="Public Sub Disconnect";
RDebugUtils.currentLine=983042;
 //BA.debugLineNum = 983042;BA.debugLine="CloseExistingConnection";
_closeexistingconnection();
RDebugUtils.currentLine=983043;
 //BA.debugLineNum = 983043;BA.debugLine="End Sub";
return "";
}
public static String  _senddata(byte[] _data) throws Exception{
RDebugUtils.currentModule="starter";
if (Debug.shouldDelegate(processBA, "senddata", false))
	 {return ((String) Debug.delegate(processBA, "senddata", new Object[] {_data}));}
RDebugUtils.currentLine=1376256;
 //BA.debugLineNum = 1376256;BA.debugLine="Public Sub SendData (data() As Byte)";
RDebugUtils.currentLine=1376258;
 //BA.debugLineNum = 1376258;BA.debugLine="If connected Then astream.Write(data)";
if (_connected) { 
_astream.Write(_data);};
RDebugUtils.currentLine=1376259;
 //BA.debugLineNum = 1376259;BA.debugLine="End Sub";
return "";
}
public static boolean  _application_error(anywheresoftware.b4a.objects.B4AException _error,String _stacktrace) throws Exception{
RDebugUtils.currentModule="starter";
if (Debug.shouldDelegate(processBA, "application_error", false))
	 {return ((Boolean) Debug.delegate(processBA, "application_error", new Object[] {_error,_stacktrace}));}
RDebugUtils.currentLine=1441792;
 //BA.debugLineNum = 1441792;BA.debugLine="Sub Application_Error (Error As Exception, StackTr";
RDebugUtils.currentLine=1441793;
 //BA.debugLineNum = 1441793;BA.debugLine="Return True";
if (true) return anywheresoftware.b4a.keywords.Common.True;
RDebugUtils.currentLine=1441794;
 //BA.debugLineNum = 1441794;BA.debugLine="End Sub";
return false;
}
public static String  _astream_error() throws Exception{
RDebugUtils.currentModule="starter";
if (Debug.shouldDelegate(processBA, "astream_error", false))
	 {return ((String) Debug.delegate(processBA, "astream_error", null));}
RDebugUtils.currentLine=1179648;
 //BA.debugLineNum = 1179648;BA.debugLine="Sub AStream_Error";
RDebugUtils.currentLine=1179649;
 //BA.debugLineNum = 1179649;BA.debugLine="UpdateState(False)";
_updatestate(anywheresoftware.b4a.keywords.Common.False);
RDebugUtils.currentLine=1179650;
 //BA.debugLineNum = 1179650;BA.debugLine="End Sub";
return "";
}
public static String  _updatestate(boolean _newstate) throws Exception{
RDebugUtils.currentModule="starter";
if (Debug.shouldDelegate(processBA, "updatestate", false))
	 {return ((String) Debug.delegate(processBA, "updatestate", new Object[] {_newstate}));}
RDebugUtils.currentLine=1114112;
 //BA.debugLineNum = 1114112;BA.debugLine="Sub UpdateState (NewState As Boolean)";
RDebugUtils.currentLine=1114113;
 //BA.debugLineNum = 1114113;BA.debugLine="connected = NewState";
_connected = _newstate;
RDebugUtils.currentLine=1114114;
 //BA.debugLineNum = 1114114;BA.debugLine="CallSub(Main, \"SetState\")";
anywheresoftware.b4a.keywords.Common.CallSubDebug(processBA,(Object)(mostCurrent._main.getObject()),"SetState");
RDebugUtils.currentLine=1114115;
 //BA.debugLineNum = 1114115;BA.debugLine="End Sub";
return "";
}
public static String  _astream_newdata(byte[] _buffer) throws Exception{
RDebugUtils.currentModule="starter";
if (Debug.shouldDelegate(processBA, "astream_newdata", false))
	 {return ((String) Debug.delegate(processBA, "astream_newdata", new Object[] {_buffer}));}
RDebugUtils.currentLine=1310720;
 //BA.debugLineNum = 1310720;BA.debugLine="Sub AStream_NewData (Buffer() As Byte)";
RDebugUtils.currentLine=1310722;
 //BA.debugLineNum = 1310722;BA.debugLine="return_data_TPlink=True";
_return_data_tplink = anywheresoftware.b4a.keywords.Common.True;
RDebugUtils.currentLine=1310723;
 //BA.debugLineNum = 1310723;BA.debugLine="If IsPaused(Get_data)=False Then";
if (anywheresoftware.b4a.keywords.Common.IsPaused(processBA,(Object)(mostCurrent._get_data.getObject()))==anywheresoftware.b4a.keywords.Common.False) { 
RDebugUtils.currentLine=1310724;
 //BA.debugLineNum = 1310724;BA.debugLine="CallSub2(Get_data, \"NewData\", Buffer)";
anywheresoftware.b4a.keywords.Common.CallSubDebug2(processBA,(Object)(mostCurrent._get_data.getObject()),"NewData",(Object)(_buffer));
 };
RDebugUtils.currentLine=1310726;
 //BA.debugLineNum = 1310726;BA.debugLine="StopService(Get_data)";
anywheresoftware.b4a.keywords.Common.StopService(processBA,(Object)(mostCurrent._get_data.getObject()));
RDebugUtils.currentLine=1310728;
 //BA.debugLineNum = 1310728;BA.debugLine="End Sub";
return "";
}
public static String  _astream_terminated() throws Exception{
RDebugUtils.currentModule="starter";
if (Debug.shouldDelegate(processBA, "astream_terminated", false))
	 {return ((String) Debug.delegate(processBA, "astream_terminated", null));}
RDebugUtils.currentLine=1245184;
 //BA.debugLineNum = 1245184;BA.debugLine="Sub AStream_Terminated";
RDebugUtils.currentLine=1245185;
 //BA.debugLineNum = 1245185;BA.debugLine="UpdateState(False)";
_updatestate(anywheresoftware.b4a.keywords.Common.False);
RDebugUtils.currentLine=1245186;
 //BA.debugLineNum = 1245186;BA.debugLine="End Sub";
return "";
}
public static String  _closeexistingconnection() throws Exception{
RDebugUtils.currentModule="starter";
if (Debug.shouldDelegate(processBA, "closeexistingconnection", false))
	 {return ((String) Debug.delegate(processBA, "closeexistingconnection", null));}
RDebugUtils.currentLine=1048576;
 //BA.debugLineNum = 1048576;BA.debugLine="Sub CloseExistingConnection";
RDebugUtils.currentLine=1048577;
 //BA.debugLineNum = 1048577;BA.debugLine="If astream.IsInitialized Then astream.Close";
if (_astream.IsInitialized()) { 
_astream.Close();};
RDebugUtils.currentLine=1048579;
 //BA.debugLineNum = 1048579;BA.debugLine="If client.IsInitialized Then client.Close";
if (_client.IsInitialized()) { 
_client.Close();};
RDebugUtils.currentLine=1048580;
 //BA.debugLineNum = 1048580;BA.debugLine="UpdateState (False)";
_updatestate(anywheresoftware.b4a.keywords.Common.False);
RDebugUtils.currentLine=1048581;
 //BA.debugLineNum = 1048581;BA.debugLine="End Sub";
return "";
}
public static void  _listenforconnections() throws Exception{
RDebugUtils.currentModule="starter";
if (Debug.shouldDelegate(processBA, "listenforconnections", false))
	 {Debug.delegate(processBA, "listenforconnections", null); return;}
ResumableSub_ListenForConnections rsub = new ResumableSub_ListenForConnections(null);
rsub.resume(processBA, null);
}
public static class ResumableSub_ListenForConnections extends BA.ResumableSub {
public ResumableSub_ListenForConnections(b4a.example.starter parent) {
this.parent = parent;
}
b4a.example.starter parent;
boolean _successful = false;
anywheresoftware.b4a.objects.SocketWrapper _newsocket = null;

@Override
public void resume(BA ba, Object[] result) throws Exception{
RDebugUtils.currentModule="starter";

    while (true) {
        switch (state) {
            case -1:
return;

case 0:
//C
this.state = 1;
RDebugUtils.currentLine=786433;
 //BA.debugLineNum = 786433;BA.debugLine="Do While working";
if (true) break;

case 1:
//do while
this.state = 8;
while (parent._working) {
this.state = 3;
if (true) break;
}
if (true) break;

case 3:
//C
this.state = 4;
RDebugUtils.currentLine=786434;
 //BA.debugLineNum = 786434;BA.debugLine="server.Listen";
parent._server.Listen();
RDebugUtils.currentLine=786435;
 //BA.debugLineNum = 786435;BA.debugLine="Wait For Server_NewConnection (Successful As Boo";
anywheresoftware.b4a.keywords.Common.WaitFor("server_newconnection", processBA, new anywheresoftware.b4a.shell.DebugResumableSub.DelegatableResumableSub(this, "starter", "listenforconnections"), null);
this.state = 9;
return;
case 9:
//C
this.state = 4;
_successful = (Boolean) result[0];
_newsocket = (anywheresoftware.b4a.objects.SocketWrapper) result[1];
;
RDebugUtils.currentLine=786437;
 //BA.debugLineNum = 786437;BA.debugLine="If Successful Then";
if (true) break;

case 4:
//if
this.state = 7;
if (_successful) { 
this.state = 6;
}if (true) break;

case 6:
//C
this.state = 7;
RDebugUtils.currentLine=786438;
 //BA.debugLineNum = 786438;BA.debugLine="CloseExistingConnection";
_closeexistingconnection();
RDebugUtils.currentLine=786439;
 //BA.debugLineNum = 786439;BA.debugLine="client = NewSocket";
parent._client = _newsocket;
RDebugUtils.currentLine=786441;
 //BA.debugLineNum = 786441;BA.debugLine="astream.Initialize(client.InputStream ,client.O";
parent._astream.Initialize(processBA,parent._client.getInputStream(),parent._client.getOutputStream(),"astream");
RDebugUtils.currentLine=786442;
 //BA.debugLineNum = 786442;BA.debugLine="UpdateState(True)";
_updatestate(anywheresoftware.b4a.keywords.Common.True);
 if (true) break;

case 7:
//C
this.state = 1;
;
 if (true) break;

case 8:
//C
this.state = -1;
;
RDebugUtils.currentLine=786445;
 //BA.debugLineNum = 786445;BA.debugLine="End Sub";
if (true) break;

            }
        }
    }
}
public static String  _service_create() throws Exception{
RDebugUtils.currentModule="starter";
if (Debug.shouldDelegate(processBA, "service_create", false))
	 {return ((String) Debug.delegate(processBA, "service_create", null));}
RDebugUtils.currentLine=720896;
 //BA.debugLineNum = 720896;BA.debugLine="Sub Service_Create";
RDebugUtils.currentLine=720897;
 //BA.debugLineNum = 720897;BA.debugLine="server.Initialize(PORT, \"server\")";
_server.Initialize(processBA,_port,"server");
RDebugUtils.currentLine=720898;
 //BA.debugLineNum = 720898;BA.debugLine="ListenForConnections";
_listenforconnections();
RDebugUtils.currentLine=720900;
 //BA.debugLineNum = 720900;BA.debugLine="End Sub";
return "";
}
public static String  _service_destroy() throws Exception{
RDebugUtils.currentModule="starter";
if (Debug.shouldDelegate(processBA, "service_destroy", false))
	 {return ((String) Debug.delegate(processBA, "service_destroy", null));}
RDebugUtils.currentLine=1507328;
 //BA.debugLineNum = 1507328;BA.debugLine="Sub Service_Destroy";
RDebugUtils.currentLine=1507330;
 //BA.debugLineNum = 1507330;BA.debugLine="End Sub";
return "";
}
public static String  _service_start(anywheresoftware.b4a.objects.IntentWrapper _startingintent) throws Exception{
RDebugUtils.currentModule="starter";
if (Debug.shouldDelegate(processBA, "service_start", false))
	 {return ((String) Debug.delegate(processBA, "service_start", new Object[] {_startingintent}));}
RDebugUtils.currentLine=851968;
 //BA.debugLineNum = 851968;BA.debugLine="Sub Service_Start (StartingIntent As Intent)";
RDebugUtils.currentLine=851970;
 //BA.debugLineNum = 851970;BA.debugLine="End Sub";
return "";
}
}