package com.smartandroid.sa.aysnc;
/**
* <p>
* ��ȫ�첽�����Բ��������쳣�����������������ߡ�
* <p>
* ��ִ��ǰ��ִ���У�ִ�к���������ʱ���쳣������
* <p>
* ��{@link #doInBackgroundSafely(Object...)}���쳣ʱ��Exception���ᱻ���ݵ�
* {@link #onPostExecuteSafely(Object, Exception)}��
* <p/>
* <p>
* ����û�ȡ����������ô�Ὣ�ص�{@link #onCancelled()}��
*
* @author MaTianyu 2014-2-23����9:22:34
*/
public abstract class ASafeTask<Params, Progress, Result> extends
AsyncTask<Params, Progress, Result> {
private Exception cause;
private boolean printStackTrace = true;
@Override
protected final void onPreExecute() {
try {
onPreExecuteSafely();
} catch (Exception e) {
if (printStackTrace)
e.printStackTrace();
}
}
@Override
protected final Result doInBackground(Params... params) {
try {
return doInBackgroundSafely(params);
} catch (Exception e) {
if (printStackTrace)
e.printStackTrace();
cause = e;
}
return null;
}
@Override
protected final void onProgressUpdate(Progress... values) {
try {
onProgressUpdateSafely(values);
} catch (Exception e) {
if (printStackTrace)
e.printStackTrace();
}
}
@Override
protected final void onPostExecute(Result result) {
try {
onPostExecuteSafely(result, cause);
} catch (Exception e) {
if (printStackTrace)
e.printStackTrace();
}
}
;
@Override
protected final void onCancelled(Result result) {
onCancelled();
}
;
/**
* <p>
* ȡ����{@link AsyncTask#onPreExecute()}, ��������������쳣���ܱ��������ǰ�ȫ�ġ�
* <p>
* ע�⣺���������ڿ���������������߳�ִ�С�
*/
protected void onPreExecuteSafely() throws Exception {
}
;
/**
* <p>
* Child Thread
* <p>
* ȡ����{@link AsyncTask#doInBackground(Object...)}, ��������������쳣���ܱ��������ǰ�ȫ�ġ�
* <p>
* ������������쳣��Exception����ͨ��{@link #onPostExecuteSafely(Object, Exception)} ���ݡ�
*
* @param params
* ���
* @return
*/
protected abstract Result doInBackgroundSafely(Params... params)
throws Exception;
/**
* <p>
* Main UI Thread
* <p>
* ����ȡ��{@link AsyncTask#onPostExecute(Object)}��
* <p>
* ע�⣺������һ��ִ�������߳�, ��������������쳣���ܱ��������ǰ�ȫ�ġ�
*
* @param result
*/
protected void onPostExecuteSafely(Result result, Exception e)
throws Exception {
}
;
/**
* <p>
* Main UI Thread
* <p>
* ����ȡ��{@link AsyncTask#onProgressUpdate(Object...)},
* <p>
* ��������������쳣���ܱ��������ǰ�ȫ�ġ�
*
* @param values
* ���´��ݵ�ֵ
*/
protected void onProgressUpdateSafely(Progress... values) throws Exception {
}
}