|
|
@ -24,7 +24,6 @@ import com.arialyy.aria.core.upload.UploadEntity; |
|
|
|
import com.arialyy.aria.util.ALog; |
|
|
|
import com.arialyy.aria.util.ALog; |
|
|
|
import com.arialyy.aria.util.ErrorHelp; |
|
|
|
import com.arialyy.aria.util.ErrorHelp; |
|
|
|
import com.arialyy.aria.util.NetUtils; |
|
|
|
import com.arialyy.aria.util.NetUtils; |
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
import java.math.BigDecimal; |
|
|
|
import java.math.BigDecimal; |
|
|
|
import java.util.Timer; |
|
|
|
import java.util.Timer; |
|
|
|
import java.util.TimerTask; |
|
|
|
import java.util.TimerTask; |
|
|
@ -61,15 +60,12 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY |
|
|
|
protected int mConnectTimeOut; //连接超时时间
|
|
|
|
protected int mConnectTimeOut; //连接超时时间
|
|
|
|
protected int mReadTimeOut; //流读取的超时时间
|
|
|
|
protected int mReadTimeOut; //流读取的超时时间
|
|
|
|
protected boolean isNotNetRetry = false; //断网情况是否重试
|
|
|
|
protected boolean isNotNetRetry = false; //断网情况是否重试
|
|
|
|
|
|
|
|
protected boolean taskBreak = false; //任务中断
|
|
|
|
|
|
|
|
|
|
|
|
private Thread mConfigThread = new Thread(new Runnable() { |
|
|
|
private Thread mConfigThread = new Thread(new Runnable() { |
|
|
|
@Override public void run() { |
|
|
|
@Override public void run() { |
|
|
|
final long currentTemp = mChildCurrentLocation; |
|
|
|
final long currentTemp = mChildCurrentLocation; |
|
|
|
try { |
|
|
|
writeConfig(false, currentTemp); |
|
|
|
writeConfig(false, currentTemp); |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
@ -108,35 +104,43 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 任务是否中断,中断条件: |
|
|
|
|
|
|
|
* 1、任务取消 |
|
|
|
|
|
|
|
* 2、任务停止 |
|
|
|
|
|
|
|
* 3、手动中断 {@link #taskBreak} |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return {@code true} 中断,{@code false} 不是中断 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected boolean isBreak() { |
|
|
|
|
|
|
|
return STATE.isCancel || STATE.isStop || taskBreak; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 停止任务 |
|
|
|
* 停止任务 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void stop() { |
|
|
|
public void stop() { |
|
|
|
synchronized (AriaManager.LOCK) { |
|
|
|
synchronized (AriaManager.LOCK) { |
|
|
|
try { |
|
|
|
if (mConfig.SUPPORT_BP) { |
|
|
|
if (mConfig.SUPPORT_BP) { |
|
|
|
final long currentTemp = mChildCurrentLocation; |
|
|
|
final long currentTemp = mChildCurrentLocation; |
|
|
|
STATE.STOP_NUM++; |
|
|
|
STATE.STOP_NUM++; |
|
|
|
ALog.d(TAG, "任务【" |
|
|
|
ALog.d(TAG, "任务【" |
|
|
|
+ mConfig.TEMP_FILE.getName() |
|
|
|
+ mConfig.TEMP_FILE.getName() |
|
|
|
+ "】thread__" |
|
|
|
+ "】thread__" |
|
|
|
+ mConfig.THREAD_ID |
|
|
|
+ mConfig.THREAD_ID |
|
|
|
+ "__停止【停止位置: " |
|
|
|
+ "__停止【停止位置: " |
|
|
|
+ currentTemp |
|
|
|
+ currentTemp |
|
|
|
+ "】"); |
|
|
|
+ "】"); |
|
|
|
writeConfig(false, currentTemp); |
|
|
|
writeConfig(false, currentTemp); |
|
|
|
if (STATE.isStop()) { |
|
|
|
if (STATE.isStop()) { |
|
|
|
|
|
|
|
ALog.i(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】已停止"); |
|
|
|
|
|
|
|
STATE.isRunning = false; |
|
|
|
|
|
|
|
mListener.onStop(STATE.CURRENT_LOCATION); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
ALog.i(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】已停止"); |
|
|
|
ALog.i(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】已停止"); |
|
|
|
STATE.isRunning = false; |
|
|
|
STATE.isRunning = false; |
|
|
|
mListener.onStop(STATE.CURRENT_LOCATION); |
|
|
|
mListener.onStop(STATE.CURRENT_LOCATION); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
} else { |
|
|
|
e.printStackTrace(); |
|
|
|
ALog.i(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】已停止"); |
|
|
|
|
|
|
|
STATE.isRunning = false; |
|
|
|
|
|
|
|
mListener.onStop(STATE.CURRENT_LOCATION); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -146,6 +150,11 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected void progress(long len) { |
|
|
|
protected void progress(long len) { |
|
|
|
synchronized (AriaManager.LOCK) { |
|
|
|
synchronized (AriaManager.LOCK) { |
|
|
|
|
|
|
|
if (STATE.CURRENT_LOCATION > mEntity.getFileSize()) { |
|
|
|
|
|
|
|
taskBreak = true; |
|
|
|
|
|
|
|
fail(mChildCurrentLocation, "下载失败,下载长度超出文件大小", null, false); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
mChildCurrentLocation += len; |
|
|
|
mChildCurrentLocation += len; |
|
|
|
STATE.CURRENT_LOCATION += len; |
|
|
|
STATE.CURRENT_LOCATION += len; |
|
|
|
if (System.currentTimeMillis() - mLastSaveTime > 5000 |
|
|
|
if (System.currentTimeMillis() - mLastSaveTime > 5000 |
|
|
@ -182,27 +191,36 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 线程任务失败 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param subCurrentLocation 当前线程下载进度 |
|
|
|
|
|
|
|
* @param msg 自定义信息 |
|
|
|
|
|
|
|
* @param ex 异常信息 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected void fail(final long subCurrentLocation, String msg, Exception ex) { |
|
|
|
|
|
|
|
fail(subCurrentLocation, msg, ex, true); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 任务失败 |
|
|
|
* 任务失败 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param subCurrentLocation 当前子线程进度 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected void fail(final long currentLocation, String msg, Exception ex) { |
|
|
|
protected void fail(final long subCurrentLocation, String msg, Exception ex, boolean needRetry) { |
|
|
|
synchronized (AriaManager.LOCK) { |
|
|
|
synchronized (AriaManager.LOCK) { |
|
|
|
try { |
|
|
|
if (ex != null) { |
|
|
|
if (ex != null) { |
|
|
|
ALog.e(TAG, msg + "\n" + ALog.getExceptionString(ex)); |
|
|
|
ALog.e(TAG, msg + "\n" + ALog.getExceptionString(ex)); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
ALog.e(TAG, msg); |
|
|
|
ALog.e(TAG, msg); |
|
|
|
} |
|
|
|
} |
|
|
|
if (mConfig.SUPPORT_BP) { |
|
|
|
if (mConfig.SUPPORT_BP) { |
|
|
|
writeConfig(false, subCurrentLocation); |
|
|
|
writeConfig(false, currentLocation); |
|
|
|
retryThis(STATE.START_THREAD_NUM != 1); |
|
|
|
retryThis(STATE.START_THREAD_NUM != 1); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
ALog.e(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】执行失败"); |
|
|
|
ALog.e(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】执行失败"); |
|
|
|
mListener.onFail(true); |
|
|
|
mListener.onFail(true); |
|
|
|
ErrorHelp.saveError(TAG, "", ALog.getExceptionString(ex)); |
|
|
|
ErrorHelp.saveError(TAG, "", ALog.getExceptionString(ex)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -233,9 +251,8 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY |
|
|
|
mFailNum++; |
|
|
|
mFailNum++; |
|
|
|
ALog.w(TAG, |
|
|
|
ALog.w(TAG, |
|
|
|
"任务【" + mConfig.TEMP_FILE.getName() + "】thread__" + mConfig.THREAD_ID + "__正在重试"); |
|
|
|
"任务【" + mConfig.TEMP_FILE.getName() + "】thread__" + mConfig.THREAD_ID + "__正在重试"); |
|
|
|
final long retryLocation = |
|
|
|
mConfig.START_LOCATION = |
|
|
|
mChildCurrentLocation == 0 ? mConfig.START_LOCATION : mChildCurrentLocation; |
|
|
|
mChildCurrentLocation == 0 ? mConfig.START_LOCATION : mChildCurrentLocation; |
|
|
|
mConfig.START_LOCATION = retryLocation; |
|
|
|
|
|
|
|
AbsThreadTask.this.run(); |
|
|
|
AbsThreadTask.this.run(); |
|
|
|
} |
|
|
|
} |
|
|
|
}, RETRY_INTERVAL); |
|
|
|
}, RETRY_INTERVAL); |
|
|
@ -253,7 +270,7 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_ENTITY |
|
|
|
/** |
|
|
|
/** |
|
|
|
* 将记录写入到配置文件 |
|
|
|
* 将记录写入到配置文件 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected void writeConfig(boolean isComplete, final long record) throws IOException { |
|
|
|
protected void writeConfig(boolean isComplete, final long record) { |
|
|
|
if (mConfig.THREAD_RECORD != null) { |
|
|
|
if (mConfig.THREAD_RECORD != null) { |
|
|
|
mConfig.THREAD_RECORD.isComplete = isComplete; |
|
|
|
mConfig.THREAD_RECORD.isComplete = isComplete; |
|
|
|
if (0 < record && record < mConfig.END_LOCATION) { |
|
|
|
if (0 < record && record < mConfig.END_LOCATION) { |
|
|
|