package Box.Control;


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, BA.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 BA(this, null, null, "Box.Control", "Box.Control.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, "Box.Control.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 anywheresoftware.b4a.audio.AudioStreamer _streamer = null;
public static anywheresoftware.b4a.objects.RuntimePermissions _rp = null;
public static String _sharedfolder = "";
public Box.Control.main _main = null;
public Box.Control.agent _agent = null;
public Box.Control.param _param = null;
public Box.Control.s1 _s1 = null;
public static boolean  _application_error(anywheresoftware.b4a.objects.B4AException _error,String _stacktrace) throws Exception{
 //BA.debugLineNum = 40;BA.debugLine="Sub Application_Error (Error As Exception, StackTr";
 //BA.debugLineNum = 41;BA.debugLine="Return True";
if (true) return anywheresoftware.b4a.keywords.Common.True;
 //BA.debugLineNum = 42;BA.debugLine="End Sub";
return false;
}
public static String  _beep(double _durationms,int _frequency) throws Exception{
int _samplerate = 0;
int _numsamples = 0;
byte[] _gsnd = null;
int _i = 0;
double _d = 0;
short _val = (short)0;
 //BA.debugLineNum = 27;BA.debugLine="Public Sub Beep (DurationMs As Double, Frequency A";
 //BA.debugLineNum = 28;BA.debugLine="Dim sampleRate As Int = 8000";
_samplerate = (int) (8000);
 //BA.debugLineNum = 29;BA.debugLine="Dim numSamples As Int = sampleRate * DurationMs /";
_numsamples = (int) (_samplerate*_durationms/(double)1000);
 //BA.debugLineNum = 30;BA.debugLine="Dim gsnd(2 * numSamples) As Byte";
_gsnd = new byte[(int) (2*_numsamples)];
;
 //BA.debugLineNum = 31;BA.debugLine="For i = 0 To numSamples - 1";
{
final int step4 = 1;
final int limit4 = (int) (_numsamples-1);
_i = (int) (0) ;
for (;_i <= limit4 ;_i = _i + step4 ) {
 //BA.debugLineNum = 32;BA.debugLine="Dim d As Double = Sin(2 * cPI * i / (sampleRate";
_d = anywheresoftware.b4a.keywords.Common.Sin(2*anywheresoftware.b4a.keywords.Common.cPI*_i/(double)(_samplerate/(double)_frequency));
 //BA.debugLineNum = 33;BA.debugLine="Dim val As Short = d * 32767";
_val = (short) (_d*32767);
 //BA.debugLineNum = 34;BA.debugLine="gsnd(2 * i) = Bit.And(val, 0x00ff)";
_gsnd[(int) (2*_i)] = (byte) (anywheresoftware.b4a.keywords.Common.Bit.And((int) (_val),(int) (0x00ff)));
 //BA.debugLineNum = 35;BA.debugLine="gsnd(2 * i + 1) = Bit.UnsignedShiftRight(Bit.And";
_gsnd[(int) (2*_i+1)] = (byte) (anywheresoftware.b4a.keywords.Common.Bit.UnsignedShiftRight(anywheresoftware.b4a.keywords.Common.Bit.And((int) (_val),(int) (0xff00)),(int) (8)));
 }
};
 //BA.debugLineNum = 37;BA.debugLine="streamer.Write(gsnd)";
_streamer.Write(_gsnd);
 //BA.debugLineNum = 38;BA.debugLine="End Sub";
return "";
}
public static String  _process_globals() throws Exception{
 //BA.debugLineNum = 6;BA.debugLine="Sub Process_Globals";
 //BA.debugLineNum = 7;BA.debugLine="Private streamer As AudioStreamer";
_streamer = new anywheresoftware.b4a.audio.AudioStreamer();
 //BA.debugLineNum = 8;BA.debugLine="Public rp As RuntimePermissions";
_rp = new anywheresoftware.b4a.objects.RuntimePermissions();
 //BA.debugLineNum = 9;BA.debugLine="Public SharedFolder As String";
_sharedfolder = "";
 //BA.debugLineNum = 10;BA.debugLine="End Sub";
return "";
}
public static String  _service_create() throws Exception{
 //BA.debugLineNum = 12;BA.debugLine="Sub Service_Create";
 //BA.debugLineNum = 13;BA.debugLine="SharedFolder = rp.GetSafeDirDefaultExternal(\"shar";
_sharedfolder = _rp.GetSafeDirDefaultExternal("shared");
 //BA.debugLineNum = 15;BA.debugLine="streamer.Initialize(\"streamer\", 8000, True, 16, s";
_streamer.Initialize(processBA,"streamer",(int) (8000),anywheresoftware.b4a.keywords.Common.True,(int) (16),_streamer.VOLUME_MUSIC);
 //BA.debugLineNum = 16;BA.debugLine="streamer.StartPlaying";
_streamer.StartPlaying();
 //BA.debugLineNum = 18;BA.debugLine="End Sub";
return "";
}
public static String  _service_destroy() throws Exception{
 //BA.debugLineNum = 44;BA.debugLine="Sub Service_Destroy";
 //BA.debugLineNum = 46;BA.debugLine="End Sub";
return "";
}
public static String  _service_start(anywheresoftware.b4a.objects.IntentWrapper _startingintent) throws Exception{
 //BA.debugLineNum = 20;BA.debugLine="Sub Service_Start (StartingIntent As Intent)";
 //BA.debugLineNum = 23;BA.debugLine="Service.StopAutomaticForeground 'Call this when t";
mostCurrent._service.StopAutomaticForeground();
 //BA.debugLineNum = 25;BA.debugLine="End Sub";
return "";
}
}
