|
|
@ -36,19 +36,17 @@ import java.lang.ref.WeakReference; |
|
|
|
public class UploadTask implements ITask { |
|
|
|
public class UploadTask implements ITask { |
|
|
|
private static final String TAG = "UploadTask"; |
|
|
|
private static final String TAG = "UploadTask"; |
|
|
|
private Handler mOutHandler; |
|
|
|
private Handler mOutHandler; |
|
|
|
private UploadTaskEntity mTaskEntity; |
|
|
|
|
|
|
|
private UploadEntity mUploadEntity; |
|
|
|
private UploadEntity mUploadEntity; |
|
|
|
private String mTargetName; |
|
|
|
private String mTargetName; |
|
|
|
|
|
|
|
|
|
|
|
private UploadUtil mUtil; |
|
|
|
private UploadUtil mUtil; |
|
|
|
private UListener mListener; |
|
|
|
private UListener mListener; |
|
|
|
|
|
|
|
|
|
|
|
UploadTask(UploadTaskEntity taskEntity, Handler outHandler) { |
|
|
|
private UploadTask(UploadTaskEntity taskEntity, Handler outHandler) { |
|
|
|
mTaskEntity = taskEntity; |
|
|
|
|
|
|
|
mOutHandler = outHandler; |
|
|
|
mOutHandler = outHandler; |
|
|
|
mUploadEntity = mTaskEntity.uploadEntity; |
|
|
|
mUploadEntity = taskEntity.uploadEntity; |
|
|
|
mListener = new UListener(mOutHandler, this); |
|
|
|
mListener = new UListener(mOutHandler, this); |
|
|
|
mUtil = new UploadUtil(mTaskEntity, mListener); |
|
|
|
mUtil = new UploadUtil(taskEntity, mListener); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void setTargetName(String targetName) { |
|
|
|
@Override public void setTargetName(String targetName) { |
|
|
@ -110,10 +108,41 @@ public class UploadTask implements ITask { |
|
|
|
return mTargetName; |
|
|
|
return mTargetName; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @return 返回原始byte速度,需要你在配置文件中配置 |
|
|
|
|
|
|
|
* <pre> |
|
|
|
|
|
|
|
* <upload> |
|
|
|
|
|
|
|
* ... |
|
|
|
|
|
|
|
* <convertSpeed value="false"/> |
|
|
|
|
|
|
|
* </upload> |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* 或在代码中设置 |
|
|
|
|
|
|
|
* Aria.get(this).getUploadConfig().setConvertSpeed(false); |
|
|
|
|
|
|
|
* </pre> |
|
|
|
|
|
|
|
* 才能生效 |
|
|
|
|
|
|
|
*/ |
|
|
|
@Override public long getSpeed() { |
|
|
|
@Override public long getSpeed() { |
|
|
|
return mUploadEntity.getSpeed(); |
|
|
|
return mUploadEntity.getSpeed(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @return 返回转换单位后的速度,需要你在配置文件中配置,转换完成后为:1b/s、1k/s、1m/s、1g/s、1t/s |
|
|
|
|
|
|
|
* <pre> |
|
|
|
|
|
|
|
* <upload> |
|
|
|
|
|
|
|
* ... |
|
|
|
|
|
|
|
* <convertSpeed value="true"/> |
|
|
|
|
|
|
|
* </upload> |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* 或在代码中设置 |
|
|
|
|
|
|
|
* Aria.get(this).getUploadConfig().setConvertSpeed(true); |
|
|
|
|
|
|
|
* </pre> |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* 才能生效 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@Override public String getConvertSpeed() { |
|
|
|
|
|
|
|
return mUploadEntity.getConvertSpeed(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public long getFileSize() { |
|
|
|
@Override public long getFileSize() { |
|
|
|
return mUploadEntity.getFileSize(); |
|
|
|
return mUploadEntity.getFileSize(); |
|
|
|
} |
|
|
|
} |
|
|
@ -129,43 +158,46 @@ public class UploadTask implements ITask { |
|
|
|
long lastTime = 0; |
|
|
|
long lastTime = 0; |
|
|
|
long INTERVAL_TIME = 1000; //1m更新周期
|
|
|
|
long INTERVAL_TIME = 1000; //1m更新周期
|
|
|
|
boolean isFirst = true; |
|
|
|
boolean isFirst = true; |
|
|
|
UploadEntity entity; |
|
|
|
UploadEntity uploadEntity; |
|
|
|
Intent sendIntent; |
|
|
|
Intent sendIntent; |
|
|
|
boolean isOpenBroadCast = false; |
|
|
|
boolean isOpenBroadCast = false; |
|
|
|
|
|
|
|
boolean isConvertSpeed = false; |
|
|
|
Context context; |
|
|
|
Context context; |
|
|
|
|
|
|
|
|
|
|
|
UListener(Handler outHandle, UploadTask task) { |
|
|
|
UListener(Handler outHandle, UploadTask task) { |
|
|
|
this.outHandler = new WeakReference<>(outHandle); |
|
|
|
this.outHandler = new WeakReference<>(outHandle); |
|
|
|
this.task = new WeakReference<>(task); |
|
|
|
this.task = new WeakReference<>(task); |
|
|
|
entity = this.task.get().getUploadEntity(); |
|
|
|
uploadEntity = this.task.get().getUploadEntity(); |
|
|
|
sendIntent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), Aria.ACTION_RUNNING); |
|
|
|
sendIntent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), Aria.ACTION_RUNNING); |
|
|
|
sendIntent.putExtra(Aria.ENTITY, entity); |
|
|
|
sendIntent.putExtra(Aria.ENTITY, uploadEntity); |
|
|
|
context = AriaManager.APP; |
|
|
|
context = AriaManager.APP; |
|
|
|
isOpenBroadCast = AriaManager.getInstance(context).getUploadConfig().isOpenBreadCast(); |
|
|
|
final AriaManager manager = AriaManager.getInstance(context); |
|
|
|
|
|
|
|
isOpenBroadCast = manager.getUploadConfig().isOpenBreadCast(); |
|
|
|
|
|
|
|
isConvertSpeed = manager.getUploadConfig().isConvertSpeed(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void onPre() { |
|
|
|
@Override public void onPre() { |
|
|
|
entity.setState(IEntity.STATE_PRE); |
|
|
|
uploadEntity.setState(IEntity.STATE_PRE); |
|
|
|
sendIntent(Aria.ACTION_PRE, -1); |
|
|
|
sendIntent(Aria.ACTION_PRE, -1); |
|
|
|
sendInState2Target(ISchedulers.PRE); |
|
|
|
sendInState2Target(ISchedulers.PRE); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void onStart(long fileSize) { |
|
|
|
@Override public void onStart(long fileSize) { |
|
|
|
entity.setFileSize(fileSize); |
|
|
|
uploadEntity.setFileSize(fileSize); |
|
|
|
entity.setState(IEntity.STATE_RUNNING); |
|
|
|
uploadEntity.setState(IEntity.STATE_RUNNING); |
|
|
|
sendIntent(Aria.ACTION_PRE, -1); |
|
|
|
sendIntent(Aria.ACTION_PRE, -1); |
|
|
|
sendInState2Target(ISchedulers.START); |
|
|
|
sendInState2Target(ISchedulers.START); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void onResume(long resumeLocation) { |
|
|
|
@Override public void onResume(long resumeLocation) { |
|
|
|
entity.setState(DownloadEntity.STATE_RUNNING); |
|
|
|
uploadEntity.setState(DownloadEntity.STATE_RUNNING); |
|
|
|
sendInState2Target(DownloadSchedulers.RESUME); |
|
|
|
sendInState2Target(DownloadSchedulers.RESUME); |
|
|
|
sendIntent(Aria.ACTION_RESUME, resumeLocation); |
|
|
|
sendIntent(Aria.ACTION_RESUME, resumeLocation); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void onStop(long stopLocation) { |
|
|
|
@Override public void onStop(long stopLocation) { |
|
|
|
entity.setState(DownloadEntity.STATE_STOP); |
|
|
|
uploadEntity.setState(DownloadEntity.STATE_STOP); |
|
|
|
entity.setSpeed(0); |
|
|
|
handleSpeed(0); |
|
|
|
sendInState2Target(DownloadSchedulers.STOP); |
|
|
|
sendInState2Target(DownloadSchedulers.STOP); |
|
|
|
sendIntent(Aria.ACTION_STOP, stopLocation); |
|
|
|
sendIntent(Aria.ACTION_STOP, stopLocation); |
|
|
|
} |
|
|
|
} |
|
|
@ -177,12 +209,11 @@ public class UploadTask implements ITask { |
|
|
|
sendIntent.putExtra(Aria.CURRENT_SPEED, speed); |
|
|
|
sendIntent.putExtra(Aria.CURRENT_SPEED, speed); |
|
|
|
lastTime = System.currentTimeMillis(); |
|
|
|
lastTime = System.currentTimeMillis(); |
|
|
|
if (isFirst) { |
|
|
|
if (isFirst) { |
|
|
|
entity.setSpeed(0); |
|
|
|
speed = 0; |
|
|
|
isFirst = false; |
|
|
|
isFirst = false; |
|
|
|
} else { |
|
|
|
|
|
|
|
entity.setSpeed(speed); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
entity.setCurrentProgress(currentLocation); |
|
|
|
handleSpeed(speed); |
|
|
|
|
|
|
|
uploadEntity.setCurrentProgress(currentLocation); |
|
|
|
lastLen = currentLocation; |
|
|
|
lastLen = currentLocation; |
|
|
|
sendInState2Target(DownloadSchedulers.RUNNING); |
|
|
|
sendInState2Target(DownloadSchedulers.RUNNING); |
|
|
|
AriaManager.APP.sendBroadcast(sendIntent); |
|
|
|
AriaManager.APP.sendBroadcast(sendIntent); |
|
|
@ -190,28 +221,37 @@ public class UploadTask implements ITask { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void onCancel() { |
|
|
|
@Override public void onCancel() { |
|
|
|
entity.setState(DownloadEntity.STATE_CANCEL); |
|
|
|
uploadEntity.setState(DownloadEntity.STATE_CANCEL); |
|
|
|
|
|
|
|
handleSpeed(0); |
|
|
|
sendInState2Target(DownloadSchedulers.CANCEL); |
|
|
|
sendInState2Target(DownloadSchedulers.CANCEL); |
|
|
|
sendIntent(Aria.ACTION_CANCEL, -1); |
|
|
|
sendIntent(Aria.ACTION_CANCEL, -1); |
|
|
|
entity.deleteData(); |
|
|
|
uploadEntity.deleteData(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void onComplete() { |
|
|
|
@Override public void onComplete() { |
|
|
|
entity.setState(DownloadEntity.STATE_COMPLETE); |
|
|
|
uploadEntity.setState(DownloadEntity.STATE_COMPLETE); |
|
|
|
entity.setComplete(true); |
|
|
|
uploadEntity.setComplete(true); |
|
|
|
entity.setSpeed(0); |
|
|
|
handleSpeed(0); |
|
|
|
sendInState2Target(DownloadSchedulers.COMPLETE); |
|
|
|
sendInState2Target(DownloadSchedulers.COMPLETE); |
|
|
|
sendIntent(Aria.ACTION_COMPLETE, entity.getFileSize()); |
|
|
|
sendIntent(Aria.ACTION_COMPLETE, uploadEntity.getFileSize()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override public void onFail() { |
|
|
|
@Override public void onFail() { |
|
|
|
entity.setFailNum(entity.getFailNum() + 1); |
|
|
|
uploadEntity.setFailNum(uploadEntity.getFailNum() + 1); |
|
|
|
entity.setState(DownloadEntity.STATE_FAIL); |
|
|
|
uploadEntity.setState(DownloadEntity.STATE_FAIL); |
|
|
|
entity.setSpeed(0); |
|
|
|
handleSpeed(0); |
|
|
|
sendInState2Target(DownloadSchedulers.FAIL); |
|
|
|
sendInState2Target(DownloadSchedulers.FAIL); |
|
|
|
sendIntent(Aria.ACTION_FAIL, -1); |
|
|
|
sendIntent(Aria.ACTION_FAIL, -1); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void handleSpeed(long speed) { |
|
|
|
|
|
|
|
if (isConvertSpeed) { |
|
|
|
|
|
|
|
uploadEntity.setConvertSpeed(CommonUtil.formatFileSize(speed) + "/s"); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
uploadEntity.setSpeed(speed); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 将任务状态发送给下载器 |
|
|
|
* 将任务状态发送给下载器 |
|
|
|
* |
|
|
|
* |
|
|
@ -224,12 +264,12 @@ public class UploadTask implements ITask { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void sendIntent(String action, long location) { |
|
|
|
private void sendIntent(String action, long location) { |
|
|
|
entity.setComplete(action.equals(Aria.ACTION_COMPLETE)); |
|
|
|
uploadEntity.setComplete(action.equals(Aria.ACTION_COMPLETE)); |
|
|
|
entity.setCurrentProgress(location); |
|
|
|
uploadEntity.setCurrentProgress(location); |
|
|
|
entity.update(); |
|
|
|
uploadEntity.update(); |
|
|
|
if (!isOpenBroadCast) return; |
|
|
|
if (!isOpenBroadCast) return; |
|
|
|
Intent intent = CommonUtil.createIntent(context.getPackageName(), action); |
|
|
|
Intent intent = CommonUtil.createIntent(context.getPackageName(), action); |
|
|
|
intent.putExtra(Aria.ENTITY, entity); |
|
|
|
intent.putExtra(Aria.ENTITY, uploadEntity); |
|
|
|
if (location != -1) { |
|
|
|
if (location != -1) { |
|
|
|
intent.putExtra(Aria.CURRENT_LOCATION, location); |
|
|
|
intent.putExtra(Aria.CURRENT_LOCATION, location); |
|
|
|
} |
|
|
|
} |
|
|
|