merge code

This commit is contained in:
AriaLyy
2017-07-06 11:18:55 +08:00
95 changed files with 3614 additions and 1535 deletions

View File

@ -40,12 +40,8 @@ class Configuration {
/**
* 旧任务数
*/
public static int oldMaxTaskNum = 2;
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;
}
@ -176,7 +162,8 @@ class Configuration {
try {
for (Field field : fields) {
int m = field.getModifiers();
if (Modifier.isFinal(m) || Modifier.isStatic(m)) {
if (field.getName().equals("oldMaxTaskNum") || Modifier.isFinal(m) || Modifier.isStatic(
m)) {
continue;
}
field.setAccessible(true);

View File

@ -16,12 +16,10 @@
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.inf.AbsTask;
import com.arialyy.aria.core.inf.AbsNormalTask;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.scheduler.ISchedulers;
import com.arialyy.aria.util.CommonUtil;
@ -31,16 +29,15 @@ import java.lang.ref.WeakReference;
* Created by lyy on 2017/2/23.
* 上传任务
*/
public class UploadTask extends AbsTask<UploadTaskEntity, UploadEntity> {
public class UploadTask extends AbsNormalTask<UploadEntity> {
private static final String TAG = "UploadTask";
private Handler mOutHandler;
private UploadUtil mUtil;
private UListener mListener;
private UploadTask(UploadTaskEntity taskEntity, Handler outHandler) {
mOutHandler = outHandler;
mEntity = taskEntity.uploadEntity;
mEntity = taskEntity.getEntity();
mListener = new UListener(mOutHandler, this);
mUtil = new UploadUtil(taskEntity, mListener);
}
@ -77,118 +74,99 @@ public class UploadTask extends AbsTask<UploadTaskEntity, UploadEntity> {
if (mOutHandler != null) {
mOutHandler.obtainMessage(ISchedulers.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(IEntity.STATE_RUNNING);
sendInState2Target(ISchedulers.RESUME);
sendIntent(Aria.ACTION_RESUME, resumeLocation);
saveData(IEntity.STATE_RUNNING, resumeLocation);
}
@Override public void onStop(long stopLocation) {
uploadEntity.setState(IEntity.STATE_STOP);
handleSpeed(0);
sendInState2Target(ISchedulers.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(ISchedulers.RUNNING);
AriaManager.APP.sendBroadcast(sendIntent);
}
}
@Override public void onCancel() {
uploadEntity.setState(IEntity.STATE_CANCEL);
handleSpeed(0);
sendInState2Target(ISchedulers.CANCEL);
sendIntent(Aria.ACTION_CANCEL, -1);
uploadEntity.deleteData();
saveData(IEntity.STATE_CANCEL, -1);
entity.deleteData();
}
@Override public void onComplete() {
uploadEntity.setState(IEntity.STATE_COMPLETE);
uploadEntity.setComplete(true);
entity.setComplete(true);
handleSpeed(0);
sendInState2Target(ISchedulers.COMPLETE);
sendIntent(Aria.ACTION_COMPLETE, uploadEntity.getFileSize());
saveData(IEntity.STATE_COMPLETE, entity.getFileSize());
}
@Override public void onFail() {
uploadEntity.setFailNum(uploadEntity.getFailNum() + 1);
uploadEntity.setState(IEntity.STATE_FAIL);
entity.setFailNum(entity.getFailNum() + 1);
handleSpeed(0);
sendInState2Target(ISchedulers.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);
}
}
@ -203,17 +181,11 @@ public class UploadTask extends AbsTask<UploadTaskEntity, 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();
}
}

View File

@ -15,7 +15,9 @@
*/
package com.arialyy.aria.core.upload;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.orm.OneToOne;
import java.util.HashMap;
import java.util.Map;
@ -23,24 +25,23 @@ import java.util.Map;
* Created by lyy on 2017/2/9.
* 上传任务实体
*/
public class UploadTaskEntity extends AbsTaskEntity {
public UploadEntity uploadEntity;
public class UploadTaskEntity extends AbsTaskEntity<UploadEntity> {
public String uploadUrl; //上传路径
public String attachment; //文件上传需要的key
public String contentType = "multipart/form-data"; //上传的文件类型
public String userAgent = "User-Agent";
public String charset = "utf-8";
@OneToOne(table = UploadEntity.class, key = "filePath") public UploadEntity entity;
/**
* 文件上传表单
*/
public Map<String, String> formFields = new HashMap<>();
public UploadTaskEntity(UploadEntity downloadEntity) {
this.uploadEntity = downloadEntity;
public UploadTaskEntity() {
}
@Override public UploadEntity getEntity() {
return uploadEntity;
return entity;
}
}