target 编写
This commit is contained in:
@ -42,10 +42,6 @@ class Configuration {
|
||||
*/
|
||||
public int oldMaxTaskNum = 2;
|
||||
|
||||
/**
|
||||
* 是否发送任务广播,true,发送
|
||||
*/
|
||||
boolean isOpenBreadCast = false;
|
||||
/**
|
||||
* 任务队列最大任务数, 默认为2
|
||||
*/
|
||||
@ -85,16 +81,6 @@ class Configuration {
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isOpenBreadCast() {
|
||||
return isOpenBreadCast;
|
||||
}
|
||||
|
||||
public BaseConfig setOpenBreadCast(boolean openBreadCast) {
|
||||
isOpenBreadCast = openBreadCast;
|
||||
saveKey("isOpenBreadCast", openBreadCast + "");
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getMaxTaskNum() {
|
||||
return maxTaskNum;
|
||||
}
|
||||
|
@ -16,12 +16,9 @@
|
||||
package com.arialyy.aria.core.upload;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.core.Aria;
|
||||
import com.arialyy.aria.core.AriaManager;
|
||||
import com.arialyy.aria.core.download.DownloadEntity;
|
||||
import com.arialyy.aria.core.inf.AbsNormalTask;
|
||||
import com.arialyy.aria.core.inf.IEntity;
|
||||
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
|
||||
@ -41,7 +38,7 @@ public class UploadTask extends AbsNormalTask<UploadEntity> {
|
||||
|
||||
private UploadTask(UploadTaskEntity taskEntity, Handler outHandler) {
|
||||
mOutHandler = outHandler;
|
||||
mEntity = taskEntity.uploadEntity;
|
||||
mEntity = taskEntity.getEntity();
|
||||
mListener = new UListener(mOutHandler, this);
|
||||
mUtil = new UploadUtil(taskEntity, mListener);
|
||||
}
|
||||
@ -78,118 +75,99 @@ public class UploadTask extends AbsNormalTask<UploadEntity> {
|
||||
if (mOutHandler != null) {
|
||||
mOutHandler.obtainMessage(DownloadSchedulers.CANCEL, this).sendToTarget();
|
||||
}
|
||||
//发送取消下载的广播
|
||||
Intent intent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), Aria.ACTION_CANCEL);
|
||||
intent.putExtra(Aria.UPLOAD_ENTITY, mEntity);
|
||||
AriaManager.APP.sendBroadcast(intent);
|
||||
}
|
||||
}
|
||||
|
||||
private static class UListener extends UploadListener {
|
||||
private static class
|
||||
UListener extends UploadListener {
|
||||
WeakReference<Handler> outHandler;
|
||||
WeakReference<UploadTask> task;
|
||||
long lastLen = 0; //上一次发送长度
|
||||
long lastTime = 0;
|
||||
long INTERVAL_TIME = 1000; //1m更新周期
|
||||
boolean isFirst = true;
|
||||
UploadEntity uploadEntity;
|
||||
Intent sendIntent;
|
||||
boolean isOpenBroadCast = false;
|
||||
UploadEntity entity;
|
||||
boolean isConvertSpeed = false;
|
||||
Context context;
|
||||
|
||||
UListener(Handler outHandle, UploadTask task) {
|
||||
this.outHandler = new WeakReference<>(outHandle);
|
||||
this.task = new WeakReference<>(task);
|
||||
uploadEntity = this.task.get().getEntity();
|
||||
sendIntent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), Aria.ACTION_RUNNING);
|
||||
sendIntent.putExtra(Aria.UPLOAD_ENTITY, uploadEntity);
|
||||
entity = this.task.get().getEntity();
|
||||
context = AriaManager.APP;
|
||||
final AriaManager manager = AriaManager.getInstance(context);
|
||||
isOpenBroadCast = manager.getUploadConfig().isOpenBreadCast();
|
||||
isConvertSpeed = manager.getUploadConfig().isConvertSpeed();
|
||||
}
|
||||
|
||||
@Override public void onPre() {
|
||||
uploadEntity.setState(IEntity.STATE_PRE);
|
||||
sendIntent(Aria.ACTION_PRE, -1);
|
||||
sendInState2Target(ISchedulers.PRE);
|
||||
saveData(IEntity.STATE_PRE, -1);
|
||||
}
|
||||
|
||||
@Override public void onPostPre(long fileSize) {
|
||||
super.onPostPre(fileSize);
|
||||
uploadEntity.setFileSize(fileSize);
|
||||
uploadEntity.setState(IEntity.STATE_POST_PRE);
|
||||
sendIntent(Aria.ACTION_POST_PRE, 0);
|
||||
entity.setFileSize(fileSize);
|
||||
sendInState2Target(ISchedulers.POST_PRE);
|
||||
saveData(IEntity.STATE_POST_PRE, 0);
|
||||
}
|
||||
|
||||
@Override public void onStart() {
|
||||
uploadEntity.setState(IEntity.STATE_RUNNING);
|
||||
sendIntent(Aria.ACTION_START, 0);
|
||||
sendInState2Target(ISchedulers.START);
|
||||
saveData(IEntity.STATE_RUNNING, 0);
|
||||
}
|
||||
|
||||
@Override public void onResume(long resumeLocation) {
|
||||
uploadEntity.setState(DownloadEntity.STATE_RUNNING);
|
||||
sendInState2Target(DownloadSchedulers.RESUME);
|
||||
sendIntent(Aria.ACTION_RESUME, resumeLocation);
|
||||
saveData(IEntity.STATE_RUNNING, resumeLocation);
|
||||
}
|
||||
|
||||
@Override public void onStop(long stopLocation) {
|
||||
uploadEntity.setState(DownloadEntity.STATE_STOP);
|
||||
handleSpeed(0);
|
||||
sendInState2Target(DownloadSchedulers.STOP);
|
||||
sendIntent(Aria.ACTION_STOP, stopLocation);
|
||||
saveData(IEntity.STATE_STOP, stopLocation);
|
||||
}
|
||||
|
||||
@Override public void onProgress(long currentLocation) {
|
||||
if (System.currentTimeMillis() - lastTime > INTERVAL_TIME) {
|
||||
long speed = currentLocation - lastLen;
|
||||
sendIntent.putExtra(Aria.CURRENT_LOCATION, currentLocation);
|
||||
sendIntent.putExtra(Aria.CURRENT_SPEED, speed);
|
||||
lastTime = System.currentTimeMillis();
|
||||
if (isFirst) {
|
||||
speed = 0;
|
||||
isFirst = false;
|
||||
}
|
||||
handleSpeed(speed);
|
||||
uploadEntity.setCurrentProgress(currentLocation);
|
||||
entity.setCurrentProgress(currentLocation);
|
||||
lastLen = currentLocation;
|
||||
sendInState2Target(DownloadSchedulers.RUNNING);
|
||||
AriaManager.APP.sendBroadcast(sendIntent);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void onCancel() {
|
||||
uploadEntity.setState(DownloadEntity.STATE_CANCEL);
|
||||
handleSpeed(0);
|
||||
sendInState2Target(DownloadSchedulers.CANCEL);
|
||||
sendIntent(Aria.ACTION_CANCEL, -1);
|
||||
uploadEntity.deleteData();
|
||||
saveData(IEntity.STATE_CANCEL, -1);
|
||||
entity.deleteData();
|
||||
}
|
||||
|
||||
@Override public void onComplete() {
|
||||
uploadEntity.setState(DownloadEntity.STATE_COMPLETE);
|
||||
uploadEntity.setComplete(true);
|
||||
entity.setComplete(true);
|
||||
handleSpeed(0);
|
||||
sendInState2Target(DownloadSchedulers.COMPLETE);
|
||||
sendIntent(Aria.ACTION_COMPLETE, uploadEntity.getFileSize());
|
||||
saveData(IEntity.STATE_COMPLETE, entity.getFileSize());
|
||||
}
|
||||
|
||||
@Override public void onFail() {
|
||||
uploadEntity.setFailNum(uploadEntity.getFailNum() + 1);
|
||||
uploadEntity.setState(DownloadEntity.STATE_FAIL);
|
||||
entity.setFailNum(entity.getFailNum() + 1);
|
||||
handleSpeed(0);
|
||||
sendInState2Target(DownloadSchedulers.FAIL);
|
||||
sendIntent(Aria.ACTION_FAIL, -1);
|
||||
saveData(IEntity.STATE_FAIL, -1);
|
||||
}
|
||||
|
||||
private void handleSpeed(long speed) {
|
||||
if (isConvertSpeed) {
|
||||
uploadEntity.setConvertSpeed(CommonUtil.formatFileSize(speed) + "/s");
|
||||
entity.setConvertSpeed(CommonUtil.formatFileSize(speed) + "/s");
|
||||
} else {
|
||||
uploadEntity.setSpeed(speed);
|
||||
entity.setSpeed(speed);
|
||||
}
|
||||
}
|
||||
|
||||
@ -204,17 +182,11 @@ public class UploadTask extends AbsNormalTask<UploadEntity> {
|
||||
}
|
||||
}
|
||||
|
||||
private void sendIntent(String action, long location) {
|
||||
uploadEntity.setComplete(action.equals(Aria.ACTION_COMPLETE));
|
||||
uploadEntity.setCurrentProgress(location);
|
||||
uploadEntity.update();
|
||||
if (!isOpenBroadCast) return;
|
||||
Intent intent = CommonUtil.createIntent(context.getPackageName(), action);
|
||||
intent.putExtra(Aria.UPLOAD_ENTITY, uploadEntity);
|
||||
if (location != -1) {
|
||||
intent.putExtra(Aria.CURRENT_LOCATION, location);
|
||||
}
|
||||
context.sendBroadcast(intent);
|
||||
private void saveData(int state, long location) {
|
||||
entity.setState(state);
|
||||
entity.setComplete(state == IEntity.STATE_COMPLETE);
|
||||
entity.setCurrentProgress(location);
|
||||
entity.update();
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user