package com.aincc.lib.network.common;
import android.os.AsyncTask;
import com.aincc.lib.network.NetHttpProcessor;
/**
*
* <h3><b>BaseTask</b></h3></br>
*
* abstract base task class
*
* @author aincc@barusoft.com
* @version 1.0.0
* @since 1.0.0
* @param <T>
*/
public abstract class BaseTask<T> extends AsyncTask<Void, Void, T>
{
/**
* reference NetHttpProcessor
*
* @since 1.0.0
*/
protected NetHttpProcessor processor = null;
/**
* task timeout
*
* @since 1.0.0
*/
private long timeout = NetworkDefine.NETWORK_TIMEOUT;
/**
* task sequence
*
* @since 1.0.0
*/
protected int taskSeq = 0;
/**
* network listener
*
* @since 1.0.0
*/
protected INetworkListener listener = null;
/**
* transaction sent time
*
* @since 1.0.0
*/
private long sentTime;
/**
* polling flag
*
* @since 1.0.0
*/
private boolean poll = false;
/**
* synchronous mode flag
*
* @since 1.0.0
*/
private boolean sync = false;
/**
* running flag
*
* @since 1.0.0
*/
private boolean running = false;
/**
* done flag
*
* @since 1.0.0
*/
private boolean done = false;
/**
* transaction object
*
* @since 1.0.0
*/
protected T tr;
/**
* @return the processor
*/
public NetHttpProcessor getProcessor()
{
return processor;
}
/**
* @param processor
* the processor to set
*/
public void setProcessor(NetHttpProcessor processor)
{
this.processor = processor;
}
/**
* @return the timeout
*/
public long getTimeout()
{
return timeout;
}
/**
* @param timeout
* the timeout to set
*/
public void setTimeout(long timeout)
{
this.timeout = timeout;
}
/**
* @return the sentTime
*/
public long getSentTime()
{
return sentTime;
}
/**
* @param sentTime
* the sentTime to set
*/
public void setSentTime(long sentTime)
{
this.sentTime = sentTime;
}
/**
* @return the tr
*/
public T getTr()
{
return tr;
}
/**
* @param tr
* the tr to set
*/
public void setTr(T tr)
{
this.tr = tr;
}
/**
* @param poll
* the poll to set
*/
public void setPoll(boolean poll)
{
this.poll = poll;
}
/**
* @param sync
* the sync to set
*/
public void setSync(boolean sync)
{
this.sync = sync;
}
/**
* @param running
* the running to set
*/
public void setRunning(boolean running)
{
this.running = running;
}
/**
* @param done
* the done to set
*/
public void setDone(boolean done)
{
this.done = done;
}
/**
* @since 1.0.0
* @return return true if current time is over than sentTime + timeout, or false
*/
public boolean isTimeout()
{
return this.sentTime + this.timeout < System.currentTimeMillis();
}
/**
* @since 1.0.0
* @return return polling flag
*/
public boolean isPoll()
{
return this.poll;
}
/**
* @since 1.0.0
* @return return synchronous mode flag
*/
public boolean isSync()
{
return this.sync;
}
/**
* @since 1.0.0
* @return return true if task is not running and status is done, or false
*/
public boolean isDone()
{
return this.done && !this.running ? true : false;
}
/**
* @since 1.0.0
* @return return true if task is running and status is not done, or false
*/
public boolean isRunning()
{
return !this.done && this.running ? true : false;
}
/**
* set sentTime to current system time
*
* @since 1.0.0
*/
public void markSentTime()
{
setSentTime(System.currentTimeMillis());
}
/*
* (non-Javadoc)
*
* @see android.os.AsyncTask#doInBackground(Params[])
*/
@Override
protected T doInBackground(Void... params)
{
return null;
}
/*
* (non-Javadoc)
*
* @see android.os.AsyncTask#onCancelled()
*/
@Override
protected void onCancelled()
{
setDone(true);
setRunning(false);
super.onCancelled();
}
/*
* (non-Javadoc)
*
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
@Override
protected void onPostExecute(T result)
{
setDone(true);
setRunning(false);
super.onPostExecute(result);
}
/*
* (non-Javadoc)
*
* @see android.os.AsyncTask#onPreExecute()
*/
@Override
protected void onPreExecute()
{
setDone(false);
setRunning(true);
super.onPreExecute();
}
/*
* (non-Javadoc)
*
* @see android.os.AsyncTask#onProgressUpdate(Progress[])
*/
@Override
protected void onProgressUpdate(Void... values)
{
super.onProgressUpdate(values);
}
/**
* @return the taskSeq
*/
public int getTaskSeq()
{
return taskSeq;
}
/**
* @param taskSeq
* the taskSeq to set
*/
public void setTaskSeq(int taskSeq)
{
this.taskSeq = taskSeq;
}
/**
* @return the listener
*/
public INetworkListener getListener()
{
return listener;
}
/**
* @param listener
* the listener to set
*/
public void setListener(INetworkListener listener)
{
this.listener = listener;
}
}