safedispatch-mobile/libSafeMobile/src/main/java/com/safemobile/services/TCPService.java

177 lines
4.6 KiB
Java

package com.safemobile.services;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.safemobile.interfaces.ITCPListener;
import com.safemobile.lib.AppParams;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
public class TCPService extends Service {
private int[] startModes = {START_STICKY, START_NOT_STICKY, START_REDELIVER_INTENT};
private int mStartMode = startModes[0]; // indicates how to behave if the service is killed
private IBinder mBinder = new TCPBinder(); // interface for clients that bind
private boolean mAllowRebind = true; // indicates whether onRebind should be used
private TCPhandler tcp = null;
private TCPmsgParser tcpParser = null;
@Override
public void onCreate() {
// create a TCP connection and a TCP msg Parser
int port = 13589;
try {
port = Integer.parseInt(AppParams.PORT);
} catch(Exception ex) { }
tcpParser = new TCPmsgParser();
if(tcp == null && !AppParams.IP.equalsIgnoreCase("n/a")) {
tcp = new TCPhandler(getApplicationContext(), AppParams.IP, port);
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// The service is starting, due to a call to startService()
return mStartMode;
}
@Override
public IBinder onBind(Intent intent) {
// A client is binding to the service with bindService()
return mBinder;
}
@Override
public boolean onUnbind(Intent intent) {
// All clients have unbound with unbindService()
return mAllowRebind;
}
@Override
public void onRebind(Intent intent) {
// A client is binding to the service with bindService(),
// after onUnbind() has already been called
}
@Override
public void onDestroy() {
// The service is no longer used and is being destroyed
Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
}
/** get TCP Connection in order to send messages through TCP */
public TCPhandler getTCPConnection()
{
return tcp;
}
/** Add TCP Listener to TCPmsgParser */
public boolean addITCPListener(ITCPListener listener)
{
if(tcpParser != null)
tcpParser.addTCPListener(listener);
else
return false;
return true;
}
/** remove all TCP Listeners */
public boolean clearITCPListeners()
{
if(tcpParser != null)
tcpParser.clearITCPListeners();
else
return false;
return true;
}
/** get TCP msg Parser in order to add a new TCP Listener */
public TCPmsgParser getTCPmsgParser()
{
if(tcpParser == null)
tcpParser = new TCPmsgParser();
return tcpParser;
}
/** Stop TCP Connection */
public void stopTCPConnection() {
if(tcp != null) {
tcp.Stop();
tcp = null;
}
}
/** restart the TCP Connection after the connection parameters had been changed */
public void recreateTCPConnection()
{
/*
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000*2);
} catch (InterruptedException e) {
e.printStackTrace();
}
*/
// create a tcp connection
int port = 13589;
try {
port = Integer.parseInt(AppParams.PORT);
}
catch(Exception ex) { }
tcp = new TCPhandler(getApplicationContext(), AppParams.IP, port);
/* }
});
t.start();
*/
}
public void recreateTCPConnection(String _ip, String _port)
{
// create a tcp connection
int port = 13589;
try {
port = Integer.parseInt(_port);
}
catch(Exception ex) { }
tcp = new TCPhandler(getApplicationContext(), _ip, port);
}
public void updateTCPparameters(String ip, String _port)
{
if(tcp!=null)
tcp.updateTCPparameters(ip, _port);
else
recreateTCPConnection();
}
/** recreate a TCPmsgParser */
public void recreateTCPmsgParser()
{
tcpParser = new TCPmsgParser();
}
@SuppressLint("SimpleDateFormat")
public String getCurrentTime() {
SimpleDateFormat dateformat =
new SimpleDateFormat("HH:mm:ss MM/dd/yyyy");
return (dateformat.format(new Date()));
}
/** get TCPService Binder */
public class TCPBinder extends Binder {
public TCPService getService() {
return TCPService.this;
}
}
}