优化任务代码部分的结构,优化实体结构
This commit is contained in:
@ -17,7 +17,7 @@
|
||||
package com.arialyy.aria.core.command;
|
||||
|
||||
import com.arialyy.aria.core.download.DownloadTaskEntity;
|
||||
import com.arialyy.aria.core.inf.ITaskEntity;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
import com.arialyy.aria.core.queue.DownloadTaskQueue;
|
||||
import com.arialyy.aria.core.queue.ITaskQueue;
|
||||
import com.arialyy.aria.core.inf.ICmd;
|
||||
@ -30,7 +30,7 @@ import com.arialyy.aria.util.CommonUtil;
|
||||
* Created by lyy on 2016/8/22.
|
||||
* 下载命令
|
||||
*/
|
||||
public abstract class AbsCmd<T extends ITaskEntity> implements ICmd {
|
||||
public abstract class AbsCmd<T extends AbsTaskEntity> implements ICmd {
|
||||
ITaskQueue mQueue;
|
||||
T mEntity;
|
||||
String TAG;
|
||||
|
@ -18,13 +18,13 @@ package com.arialyy.aria.core.command;
|
||||
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.core.inf.ITask;
|
||||
import com.arialyy.aria.core.inf.ITaskEntity;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2016/8/22.
|
||||
* 添加任务的命令
|
||||
*/
|
||||
class AddCmd<T extends ITaskEntity> extends AbsCmd<T> {
|
||||
class AddCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
|
||||
|
||||
AddCmd(String targetName, T entity) {
|
||||
super(targetName, entity);
|
||||
|
@ -18,13 +18,13 @@ package com.arialyy.aria.core.command;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import com.arialyy.aria.core.inf.ITask;
|
||||
import com.arialyy.aria.core.inf.ITaskEntity;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2016/9/20.
|
||||
* 取消命令
|
||||
*/
|
||||
class CancelCmd<T extends ITaskEntity> extends AbsCmd<T> {
|
||||
class CancelCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
|
||||
CancelCmd(String targetName, T entity) {
|
||||
super(targetName, entity);
|
||||
}
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
package com.arialyy.aria.core.command;
|
||||
|
||||
import com.arialyy.aria.core.inf.ITaskEntity;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
|
||||
/**
|
||||
* Created by Lyy on 2016/9/23.
|
||||
@ -72,7 +72,7 @@ public class CmdFactory {
|
||||
* @param type 命令类型{@link #TASK_CREATE}、{@link #TASK_START}、{@link #TASK_CANCEL}、{@link
|
||||
* #TASK_STOP}、{@link #TASK_HIGHEST_PRIORITY}
|
||||
*/
|
||||
public <T extends ITaskEntity> AbsCmd createCmd(String target, T entity, int type) {
|
||||
public <T extends AbsTaskEntity> AbsCmd createCmd(String target, T entity, int type) {
|
||||
switch (type) {
|
||||
case TASK_CREATE:
|
||||
return new AddCmd(target, entity);
|
||||
|
@ -17,7 +17,7 @@ package com.arialyy.aria.core.command;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import com.arialyy.aria.core.inf.ITask;
|
||||
import com.arialyy.aria.core.inf.ITaskEntity;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2017/6/2.
|
||||
@ -29,7 +29,7 @@ import com.arialyy.aria.core.inf.ITaskEntity;
|
||||
* 5、如果下载队列中已经满了,则会停止队尾的任务
|
||||
* 6、把任务设置为最高优先级任务后,将自动执行任务,不需要重新调用start()启动任务
|
||||
*/
|
||||
final class HighestPriorityCmd<T extends ITaskEntity> extends AbsCmd<T> {
|
||||
final class HighestPriorityCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
|
||||
/**
|
||||
* @param targetName 产生任务的对象名
|
||||
*/
|
||||
|
@ -18,13 +18,13 @@ package com.arialyy.aria.core.command;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import com.arialyy.aria.core.inf.ITask;
|
||||
import com.arialyy.aria.core.inf.ITaskEntity;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2016/8/22.
|
||||
* 开始命令
|
||||
*/
|
||||
class StartCmd<T extends ITaskEntity> extends AbsCmd<T> {
|
||||
class StartCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
|
||||
|
||||
StartCmd(String targetName, T entity) {
|
||||
super(targetName, entity);
|
||||
|
@ -20,13 +20,13 @@ import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.core.inf.IEntity;
|
||||
import com.arialyy.aria.core.inf.ITask;
|
||||
import com.arialyy.aria.core.inf.ITaskEntity;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2016/9/20.
|
||||
* 停止命令
|
||||
*/
|
||||
class StopCmd<T extends ITaskEntity> extends AbsCmd<T> {
|
||||
class StopCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
|
||||
|
||||
StopCmd(String targetName, T entity) {
|
||||
super(targetName, entity);
|
||||
|
@ -18,10 +18,8 @@ package com.arialyy.aria.core.download;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import com.arialyy.aria.core.inf.IEntity;
|
||||
import com.arialyy.aria.core.inf.ITaskEntity;
|
||||
import com.arialyy.aria.core.inf.AbsEntity;
|
||||
import com.arialyy.aria.orm.Ignore;
|
||||
import com.arialyy.aria.orm.DbEntity;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2015/12/25.
|
||||
@ -29,50 +27,16 @@ import com.arialyy.aria.orm.DbEntity;
|
||||
* !!! 注意:CREATOR要进行@Ignore注解
|
||||
* !!!并且需要Parcelable时需要手动填写rowID;
|
||||
*/
|
||||
public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
|
||||
@Ignore private long speed = 0; //下载速度
|
||||
@Ignore private String convertSpeed = "0b/s";
|
||||
@Ignore private int failNum = 0;
|
||||
public class DownloadEntity extends AbsEntity implements Parcelable {
|
||||
private String downloadUrl = ""; //下载路径
|
||||
private String downloadPath = ""; //保存路径
|
||||
private String fileName = ""; //文件名
|
||||
private String str = ""; //其它字段
|
||||
private long fileSize = 1;
|
||||
private int state = STATE_WAIT;
|
||||
private boolean isDownloadComplete = false; //是否下载完成
|
||||
private long currentProgress = 0; //当前下载进度
|
||||
private long completeTime; //完成时间
|
||||
private boolean isRedirect = false;
|
||||
private boolean isRedirect = false; //是否重定向
|
||||
private String redirectUrl = ""; //重定向链接
|
||||
|
||||
public DownloadEntity() {
|
||||
}
|
||||
|
||||
public String getStr() {
|
||||
return str;
|
||||
}
|
||||
|
||||
public void setStr(String str) {
|
||||
this.str = str;
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public DownloadEntity setFileName(String fileName) {
|
||||
this.fileName = fileName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getFailNum() {
|
||||
return failNum;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFailNum(int failNum) {
|
||||
this.failNum = failNum;
|
||||
}
|
||||
|
||||
public String getDownloadUrl() {
|
||||
return downloadUrl;
|
||||
@ -83,14 +47,6 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
|
||||
return this;
|
||||
}
|
||||
|
||||
public long getCompleteTime() {
|
||||
return completeTime;
|
||||
}
|
||||
|
||||
public void setCompleteTime(long completeTime) {
|
||||
this.completeTime = completeTime;
|
||||
}
|
||||
|
||||
public String getDownloadPath() {
|
||||
return downloadPath;
|
||||
}
|
||||
@ -100,22 +56,6 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
|
||||
return this;
|
||||
}
|
||||
|
||||
public long getFileSize() {
|
||||
return fileSize;
|
||||
}
|
||||
|
||||
public void setFileSize(long fileSize) {
|
||||
this.fileSize = fileSize;
|
||||
}
|
||||
|
||||
@Override public int getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(int state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public boolean isDownloadComplete() {
|
||||
return isDownloadComplete;
|
||||
}
|
||||
@ -124,30 +64,6 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
|
||||
isDownloadComplete = downloadComplete;
|
||||
}
|
||||
|
||||
public String getConvertSpeed() {
|
||||
return convertSpeed;
|
||||
}
|
||||
|
||||
public void setConvertSpeed(String convertSpeed) {
|
||||
this.convertSpeed = convertSpeed;
|
||||
}
|
||||
|
||||
public long getSpeed() {
|
||||
return speed;
|
||||
}
|
||||
|
||||
public void setSpeed(long speed) {
|
||||
this.speed = speed;
|
||||
}
|
||||
|
||||
public long getCurrentProgress() {
|
||||
return currentProgress;
|
||||
}
|
||||
|
||||
public void setCurrentProgress(long currentProgress) {
|
||||
this.currentProgress = currentProgress;
|
||||
}
|
||||
|
||||
@Override public DownloadEntity clone() throws CloneNotSupportedException {
|
||||
return (DownloadEntity) super.clone();
|
||||
}
|
||||
@ -168,79 +84,24 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
|
||||
this.redirectUrl = redirectUrl;
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
return "DownloadEntity{"
|
||||
+ "speed="
|
||||
+ speed
|
||||
+ ", convertSpeed='"
|
||||
+ convertSpeed
|
||||
+ '\''
|
||||
+ ", failNum="
|
||||
+ failNum
|
||||
+ ", downloadUrl='"
|
||||
+ downloadUrl
|
||||
+ '\''
|
||||
+ ", downloadPath='"
|
||||
+ downloadPath
|
||||
+ '\''
|
||||
+ ", fileName='"
|
||||
+ fileName
|
||||
+ '\''
|
||||
+ ", str='"
|
||||
+ str
|
||||
+ '\''
|
||||
+ ", fileSize="
|
||||
+ fileSize
|
||||
+ ", state="
|
||||
+ state
|
||||
+ ", isDownloadComplete="
|
||||
+ isDownloadComplete
|
||||
+ ", currentProgress="
|
||||
+ currentProgress
|
||||
+ ", completeTime="
|
||||
+ completeTime
|
||||
+ ", isRedirect="
|
||||
+ isRedirect
|
||||
+ ", redirectUrl='"
|
||||
+ redirectUrl
|
||||
+ '\''
|
||||
+ '}';
|
||||
}
|
||||
|
||||
@Override public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override public void writeToParcel(Parcel dest, int flags) {
|
||||
dest.writeLong(this.speed);
|
||||
dest.writeString(this.convertSpeed);
|
||||
dest.writeInt(this.failNum);
|
||||
super.writeToParcel(dest, flags);
|
||||
dest.writeString(this.downloadUrl);
|
||||
dest.writeString(this.downloadPath);
|
||||
dest.writeString(this.fileName);
|
||||
dest.writeString(this.str);
|
||||
dest.writeLong(this.fileSize);
|
||||
dest.writeInt(this.state);
|
||||
dest.writeByte(this.isDownloadComplete ? (byte) 1 : (byte) 0);
|
||||
dest.writeLong(this.currentProgress);
|
||||
dest.writeLong(this.completeTime);
|
||||
dest.writeByte(this.isRedirect ? (byte) 1 : (byte) 0);
|
||||
dest.writeString(this.redirectUrl);
|
||||
}
|
||||
|
||||
protected DownloadEntity(Parcel in) {
|
||||
this.speed = in.readLong();
|
||||
this.convertSpeed = in.readString();
|
||||
this.failNum = in.readInt();
|
||||
super(in);
|
||||
this.downloadUrl = in.readString();
|
||||
this.downloadPath = in.readString();
|
||||
this.fileName = in.readString();
|
||||
this.str = in.readString();
|
||||
this.fileSize = in.readLong();
|
||||
this.state = in.readInt();
|
||||
this.isDownloadComplete = in.readByte() != 0;
|
||||
this.currentProgress = in.readLong();
|
||||
this.completeTime = in.readLong();
|
||||
this.isRedirect = in.readByte() != 0;
|
||||
this.redirectUrl = in.readString();
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ 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.ITask;
|
||||
import com.arialyy.aria.core.inf.AbsTask;
|
||||
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
|
||||
import com.arialyy.aria.core.scheduler.ISchedulers;
|
||||
import com.arialyy.aria.util.CheckUtil;
|
||||
@ -33,18 +33,12 @@ import java.lang.ref.WeakReference;
|
||||
* Created by lyy on 2016/8/11.
|
||||
* 下载任务类
|
||||
*/
|
||||
public class DownloadTask implements ITask {
|
||||
public class DownloadTask extends AbsTask<DownloadTaskEntity, DownloadEntity> {
|
||||
public static final String TAG = "DownloadTask";
|
||||
/**
|
||||
* 产生该任务对象的hash码
|
||||
*/
|
||||
private String mTargetName;
|
||||
private DownloadEntity mEntity;
|
||||
|
||||
private IDownloadListener mListener;
|
||||
private Handler mOutHandler;
|
||||
private IDownloadUtil mUtil;
|
||||
private Context mContext;
|
||||
private boolean isHeighestTask = false;
|
||||
|
||||
private DownloadTask(DownloadTaskEntity taskEntity, Handler outHandler) {
|
||||
mEntity = taskEntity.downloadEntity;
|
||||
@ -54,98 +48,6 @@ public class DownloadTask implements ITask {
|
||||
mUtil = new DownloadUtil(mContext, taskEntity, mListener);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 返回原始byte速度,需要你在配置文件中配置
|
||||
* <pre>
|
||||
* {@code
|
||||
* <xml>
|
||||
* <download>
|
||||
* ...
|
||||
* <convertSpeed value="false"/>
|
||||
* </download>
|
||||
*
|
||||
* 或在代码中设置
|
||||
* Aria.get(this).getDownloadConfig().setConvertSpeed(false);
|
||||
* </xml>
|
||||
* }
|
||||
* </pre>
|
||||
* 才能生效
|
||||
*/
|
||||
@Override public long getSpeed() {
|
||||
return mEntity.getSpeed();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 返回转换单位后的速度,需要你在配置文件中配置,转换完成后为:1b/s、1kb/s、1mb/s、1gb/s、1tb/s
|
||||
* <pre>
|
||||
* {@code
|
||||
* <xml>
|
||||
* <download>
|
||||
* ...
|
||||
* <convertSpeed value="true"/>
|
||||
* </download>
|
||||
*
|
||||
* 或在代码中设置
|
||||
* Aria.get(this).getDownloadConfig().setConvertSpeed(true);
|
||||
* </xml>
|
||||
* }
|
||||
* </pre>
|
||||
* 才能生效
|
||||
*/
|
||||
@Override public String getConvertSpeed() {
|
||||
return mEntity.getConvertSpeed();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取百分比进度
|
||||
*
|
||||
* @return 返回百分比进度,如果文件长度为0,返回0
|
||||
*/
|
||||
@Override public int getPercent() {
|
||||
if (mEntity.getFileSize() == 0) {
|
||||
return 0;
|
||||
}
|
||||
return (int) (mEntity.getCurrentProgress() * 100 / mEntity.getFileSize());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件大小
|
||||
*/
|
||||
@Override public long getFileSize() {
|
||||
return mEntity.getFileSize();
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换单位后的文件长度
|
||||
*
|
||||
* @return 如果文件长度为0,则返回0m,否则返回转换后的长度1b、1kb、1mb、1gb、1tb
|
||||
*/
|
||||
@Override public String getConvertFileSize() {
|
||||
if (mEntity.getFileSize() == 0) {
|
||||
return "0mb";
|
||||
}
|
||||
return CommonUtil.formatFileSize(mEntity.getFileSize());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前下载进度
|
||||
*/
|
||||
@Override public long getCurrentProgress() {
|
||||
return mEntity.getCurrentProgress();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取单位转换后的进度
|
||||
*
|
||||
* @return 如:已经下载3mb的大小,则返回{@code 3mb}
|
||||
*/
|
||||
@Override public String getConvertCurrentProgress() {
|
||||
if (mEntity.getCurrentProgress() == 0) {
|
||||
return "0b";
|
||||
}
|
||||
return CommonUtil.formatFileSize(mEntity.getCurrentProgress());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前下载任务的下载地址
|
||||
*
|
||||
@ -155,14 +57,6 @@ public class DownloadTask implements ITask {
|
||||
return mEntity.getDownloadUrl();
|
||||
}
|
||||
|
||||
@Override public void setHighestPriority(boolean isHighestPriority) {
|
||||
isHeighestTask = isHighestPriority;
|
||||
}
|
||||
|
||||
@Override public boolean isHighestPriorityTask() {
|
||||
return isHeighestTask;
|
||||
}
|
||||
|
||||
@Override public String getKey() {
|
||||
return getDownloadUrl();
|
||||
}
|
||||
@ -180,10 +74,11 @@ public class DownloadTask implements ITask {
|
||||
return isDownloading();
|
||||
}
|
||||
|
||||
@Override public DownloadEntity getEntity() {
|
||||
public DownloadEntity getDownloadEntity() {
|
||||
return mEntity;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 开始下载
|
||||
*/
|
||||
@ -198,22 +93,6 @@ public class DownloadTask implements ITask {
|
||||
}
|
||||
}
|
||||
|
||||
public DownloadEntity getDownloadEntity() {
|
||||
return mEntity;
|
||||
}
|
||||
|
||||
public String getTargetName() {
|
||||
return mTargetName;
|
||||
}
|
||||
|
||||
@Override public void setTargetName(String targetName) {
|
||||
this.mTargetName = targetName;
|
||||
}
|
||||
|
||||
@Override public void removeRecord() {
|
||||
mEntity.deleteData();
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止下载
|
||||
*/
|
||||
|
@ -15,14 +15,13 @@
|
||||
*/
|
||||
package com.arialyy.aria.core.download;
|
||||
|
||||
import com.arialyy.aria.core.inf.IEntity;
|
||||
import com.arialyy.aria.core.inf.ITaskEntity;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2017/1/23.
|
||||
* 下载任务实体
|
||||
*/
|
||||
public class DownloadTaskEntity extends ITaskEntity {
|
||||
public class DownloadTaskEntity extends AbsTaskEntity {
|
||||
|
||||
public DownloadEntity downloadEntity;
|
||||
|
||||
@ -30,7 +29,7 @@ public class DownloadTaskEntity extends ITaskEntity {
|
||||
this.downloadEntity = downloadEntity;
|
||||
}
|
||||
|
||||
@Override public IEntity getEntity() {
|
||||
@Override public DownloadEntity getEntity() {
|
||||
return downloadEntity;
|
||||
}
|
||||
}
|
||||
|
@ -15,11 +15,149 @@
|
||||
*/
|
||||
package com.arialyy.aria.core.inf;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import com.arialyy.aria.orm.DbEntity;
|
||||
import com.arialyy.aria.orm.Ignore;
|
||||
|
||||
/**
|
||||
* Created by AriaL on 2017/6/3.
|
||||
*/
|
||||
public abstract class AbsEntity extends DbEntity implements IEntity {
|
||||
public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable {
|
||||
/**
|
||||
* 速度
|
||||
*/
|
||||
@Ignore private long speed = 0;
|
||||
/**
|
||||
* 单位转换后的速度
|
||||
*/
|
||||
@Ignore private String convertSpeed = "0b/s";
|
||||
/**
|
||||
* 下载失败计数,每次开始都重置为0
|
||||
*/
|
||||
@Ignore private int failNum = 0;
|
||||
/**
|
||||
* 扩展字段
|
||||
*/
|
||||
private String str = "";
|
||||
/**
|
||||
* 文件大小
|
||||
*/
|
||||
private long fileSize = 1;
|
||||
private int state = STATE_WAIT;
|
||||
/**
|
||||
* 当前下载进度
|
||||
*/
|
||||
private long currentProgress = 0;
|
||||
/**
|
||||
* 完成时间
|
||||
*/
|
||||
private long completeTime;
|
||||
/**
|
||||
* 文件名
|
||||
*/
|
||||
private String fileName = "";
|
||||
|
||||
public long getSpeed() {
|
||||
return speed;
|
||||
}
|
||||
|
||||
public void setSpeed(long speed) {
|
||||
this.speed = speed;
|
||||
}
|
||||
|
||||
public String getConvertSpeed() {
|
||||
return convertSpeed;
|
||||
}
|
||||
|
||||
public void setConvertSpeed(String convertSpeed) {
|
||||
this.convertSpeed = convertSpeed;
|
||||
}
|
||||
|
||||
public int getFailNum() {
|
||||
return failNum;
|
||||
}
|
||||
|
||||
public void setFailNum(int failNum) {
|
||||
this.failNum = failNum;
|
||||
}
|
||||
|
||||
public String getStr() {
|
||||
return str;
|
||||
}
|
||||
|
||||
public void setStr(String str) {
|
||||
this.str = str;
|
||||
}
|
||||
|
||||
public long getFileSize() {
|
||||
return fileSize;
|
||||
}
|
||||
|
||||
public void setFileSize(long fileSize) {
|
||||
this.fileSize = fileSize;
|
||||
}
|
||||
|
||||
public int getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(int state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public long getCurrentProgress() {
|
||||
return currentProgress;
|
||||
}
|
||||
|
||||
public void setCurrentProgress(long currentProgress) {
|
||||
this.currentProgress = currentProgress;
|
||||
}
|
||||
|
||||
public long getCompleteTime() {
|
||||
return completeTime;
|
||||
}
|
||||
|
||||
public void setCompleteTime(long completeTime) {
|
||||
this.completeTime = completeTime;
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public void setFileName(String fileName) {
|
||||
this.fileName = fileName;
|
||||
}
|
||||
|
||||
public AbsEntity() {
|
||||
}
|
||||
|
||||
@Override public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override public void writeToParcel(Parcel dest, int flags) {
|
||||
dest.writeLong(this.speed);
|
||||
dest.writeString(this.convertSpeed);
|
||||
dest.writeInt(this.failNum);
|
||||
dest.writeString(this.str);
|
||||
dest.writeLong(this.fileSize);
|
||||
dest.writeInt(this.state);
|
||||
dest.writeLong(this.currentProgress);
|
||||
dest.writeLong(this.completeTime);
|
||||
dest.writeString(this.fileName);
|
||||
}
|
||||
|
||||
protected AbsEntity(Parcel in) {
|
||||
this.speed = in.readLong();
|
||||
this.convertSpeed = in.readString();
|
||||
this.failNum = in.readInt();
|
||||
this.str = in.readString();
|
||||
this.fileSize = in.readLong();
|
||||
this.state = in.readInt();
|
||||
this.currentProgress = in.readLong();
|
||||
this.completeTime = in.readLong();
|
||||
this.fileName = in.readString();
|
||||
}
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ import java.util.Set;
|
||||
/**
|
||||
* Created by lyy on 2017/2/28.
|
||||
*/
|
||||
public abstract class AbsTarget<ENTITY extends IEntity, TASK_ENTITY extends ITaskEntity> {
|
||||
public abstract class AbsTarget<ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity> {
|
||||
protected ENTITY entity;
|
||||
protected TASK_ENTITY taskEntity;
|
||||
protected String targetName;
|
||||
|
@ -1,9 +1,163 @@
|
||||
/*
|
||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.arialyy.aria.core.inf;
|
||||
|
||||
import android.content.Context;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2017/6/3.
|
||||
*/
|
||||
public abstract class AbsTask implements ITask{
|
||||
public abstract class AbsTask<TASK_ENTITY extends AbsTaskEntity, ENTITY extends AbsEntity>
|
||||
implements ITask<ENTITY> {
|
||||
|
||||
protected ENTITY mEntity;
|
||||
|
||||
/**
|
||||
* 用于生成该任务对象的hash码
|
||||
*/
|
||||
private String mTargetName;
|
||||
protected Context mContext;
|
||||
private boolean isHeighestTask = false;
|
||||
|
||||
/**
|
||||
* @return 返回原始byte速度,需要你在配置文件中配置
|
||||
* <pre>
|
||||
* {@code
|
||||
* <xml>
|
||||
* <download>
|
||||
* ...
|
||||
* <convertSpeed value="false"/>
|
||||
* </download>
|
||||
*
|
||||
* 或在代码中设置
|
||||
* Aria.get(this).getDownloadConfig().setConvertSpeed(false);
|
||||
* </xml>
|
||||
* }
|
||||
* </pre>
|
||||
* 才能生效
|
||||
*/
|
||||
@Override public long getSpeed() {
|
||||
return mEntity.getSpeed();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 返回转换单位后的速度,需要你在配置文件中配置,转换完成后为:1b/s、1kb/s、1mb/s、1gb/s、1tb/s
|
||||
* <pre>
|
||||
* {@code
|
||||
* <xml>
|
||||
* <download>
|
||||
* ...
|
||||
* <convertSpeed value="true"/>
|
||||
* </download>
|
||||
*
|
||||
* 或在代码中设置
|
||||
* Aria.get(this).getDownloadConfig().setConvertSpeed(true);
|
||||
* </xml>
|
||||
* }
|
||||
* </pre>
|
||||
* 才能生效
|
||||
*/
|
||||
@Override public String getConvertSpeed() {
|
||||
return mEntity.getConvertSpeed();
|
||||
}
|
||||
|
||||
/**
|
||||
* 最高优先级命令,最高优先级命令有以下属性
|
||||
* 1、在下载队列中,有且只有一个最高优先级任务
|
||||
* 2、最高优先级任务会一直存在,直到用户手动暂停或任务完成
|
||||
* 3、任务调度器不会暂停最高优先级任务
|
||||
* 4、用户手动暂停或任务完成后,第二次重新执行该任务,该命令将失效
|
||||
* 5、如果下载队列中已经满了,则会停止队尾的任务
|
||||
* 6、把任务设置为最高优先级任务后,将自动执行任务,不需要重新调用start()启动任务
|
||||
*/
|
||||
@Override public void setHighestPriority(boolean isHighestPriority) {
|
||||
isHeighestTask = isHighestPriority;
|
||||
}
|
||||
|
||||
@Override public boolean isHighestPriorityTask() {
|
||||
return isHeighestTask;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取百分比进度
|
||||
*
|
||||
* @return 返回百分比进度,如果文件长度为0,返回0
|
||||
*/
|
||||
@Override public int getPercent() {
|
||||
if (mEntity.getFileSize() == 0) {
|
||||
return 0;
|
||||
}
|
||||
return (int) (mEntity.getCurrentProgress() * 100 / mEntity.getFileSize());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件大小
|
||||
*/
|
||||
@Override public long getFileSize() {
|
||||
return mEntity.getFileSize();
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换单位后的文件长度
|
||||
*
|
||||
* @return 如果文件长度为0,则返回0m,否则返回转换后的长度1b、1kb、1mb、1gb、1tb
|
||||
*/
|
||||
@Override public String getConvertFileSize() {
|
||||
if (mEntity.getFileSize() == 0) {
|
||||
return "0mb";
|
||||
}
|
||||
return CommonUtil.formatFileSize(mEntity.getFileSize());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前下载进度
|
||||
*/
|
||||
@Override public long getCurrentProgress() {
|
||||
return mEntity.getCurrentProgress();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取单位转换后的进度
|
||||
*
|
||||
* @return 如:已经下载3mb的大小,则返回{@code 3mb}
|
||||
*/
|
||||
@Override public String getConvertCurrentProgress() {
|
||||
if (mEntity.getCurrentProgress() == 0) {
|
||||
return "0b";
|
||||
}
|
||||
return CommonUtil.formatFileSize(mEntity.getCurrentProgress());
|
||||
}
|
||||
|
||||
@Override public ENTITY getEntity() {
|
||||
return mEntity;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除任务记录,删除后,再次启动该任务的下载时,将重新下载
|
||||
*/
|
||||
@Override public void removeRecord() {
|
||||
mEntity.deleteData();
|
||||
}
|
||||
|
||||
public String getTargetName() {
|
||||
return mTargetName;
|
||||
}
|
||||
|
||||
@Override public void setTargetName(String targetName) {
|
||||
this.mTargetName = targetName;
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ import java.util.Map;
|
||||
* Created by lyy on 2017/2/23.
|
||||
*/
|
||||
|
||||
public abstract class ITaskEntity {
|
||||
public abstract class AbsTaskEntity {
|
||||
/**
|
||||
* http 请求头
|
||||
*/
|
||||
@ -39,5 +39,5 @@ public abstract class ITaskEntity {
|
||||
*/
|
||||
public String redirectUrlKey = "location";
|
||||
|
||||
public abstract IEntity getEntity();
|
||||
public abstract AbsEntity getEntity();
|
||||
}
|
@ -58,8 +58,4 @@ public interface IEntity {
|
||||
* 取消下载
|
||||
*/
|
||||
@Ignore public static final int STATE_CANCEL = 7;
|
||||
|
||||
public int getState();
|
||||
|
||||
public void setFailNum(int failNum);
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ package com.arialyy.aria.core.inf;
|
||||
* Created by lyy on 2017/2/13.
|
||||
*/
|
||||
|
||||
public interface ITask {
|
||||
public interface ITask<ENTITY extends AbsEntity> {
|
||||
|
||||
/**
|
||||
* 设置任务为最高优先级任务,在下载队列中,有且只有一个最高优先级任务
|
||||
@ -48,7 +48,7 @@ public interface ITask {
|
||||
/**
|
||||
* 获取工具实体
|
||||
*/
|
||||
public IEntity getEntity();
|
||||
public ENTITY getEntity();
|
||||
|
||||
public void start();
|
||||
|
||||
|
@ -17,9 +17,9 @@
|
||||
package com.arialyy.aria.core.queue;
|
||||
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.core.inf.IEntity;
|
||||
import com.arialyy.aria.core.inf.AbsEntity;
|
||||
import com.arialyy.aria.core.inf.ITask;
|
||||
import com.arialyy.aria.core.inf.ITaskEntity;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
import com.arialyy.aria.core.queue.pool.CachePool;
|
||||
import com.arialyy.aria.core.queue.pool.ExecutePool;
|
||||
|
||||
@ -27,7 +27,7 @@ import com.arialyy.aria.core.queue.pool.ExecutePool;
|
||||
* Created by lyy on 2017/2/23.
|
||||
* 任务队列
|
||||
*/
|
||||
abstract class AbsTaskQueue<TASK extends ITask, TASK_ENTITY extends ITaskEntity, ENTITY extends IEntity>
|
||||
abstract class AbsTaskQueue<TASK extends ITask, TASK_ENTITY extends AbsTaskEntity, ENTITY extends AbsEntity>
|
||||
implements ITaskQueue<TASK, TASK_ENTITY, ENTITY> {
|
||||
private final String TAG = "AbsTaskQueue";
|
||||
CachePool<TASK> mCachePool = new CachePool<>();
|
||||
|
@ -88,14 +88,6 @@ public class DownloadTaskQueue
|
||||
for (DownloadTask temp : tempTasks){
|
||||
mExecutePool.putTask(temp);
|
||||
}
|
||||
|
||||
//int i = 0, len = tempTasks.size() - 1;
|
||||
//for (DownloadTask oldTask : tempTasks) {
|
||||
// if (i < len) {
|
||||
// startTask(oldTask);
|
||||
// }
|
||||
// i++;
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ import com.arialyy.aria.core.download.DownloadTaskEntity;
|
||||
import com.arialyy.aria.core.download.DownloadTask;
|
||||
import com.arialyy.aria.core.inf.IEntity;
|
||||
import com.arialyy.aria.core.inf.ITask;
|
||||
import com.arialyy.aria.core.inf.ITaskEntity;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
import com.arialyy.aria.core.upload.UploadEntity;
|
||||
import com.arialyy.aria.core.upload.UploadTask;
|
||||
import com.arialyy.aria.core.upload.UploadTaskEntity;
|
||||
@ -30,7 +30,7 @@ import com.arialyy.aria.core.upload.UploadTaskEntity;
|
||||
* Created by lyy on 2016/8/16.
|
||||
* 任务功能接口
|
||||
*/
|
||||
public interface ITaskQueue<TASK extends ITask, TASK_ENTITY extends ITaskEntity, ENTITY extends IEntity> {
|
||||
public interface ITaskQueue<TASK extends ITask, TASK_ENTITY extends AbsTaskEntity, ENTITY extends IEntity> {
|
||||
|
||||
/**
|
||||
* 设置任务为最高优先级任务
|
||||
|
@ -19,7 +19,7 @@ package com.arialyy.aria.core.queue;
|
||||
import com.arialyy.aria.core.download.DownloadTask;
|
||||
import com.arialyy.aria.core.download.DownloadTaskEntity;
|
||||
import com.arialyy.aria.core.inf.ITask;
|
||||
import com.arialyy.aria.core.inf.ITaskEntity;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
|
||||
import com.arialyy.aria.core.scheduler.ISchedulers;
|
||||
import com.arialyy.aria.core.upload.UploadTask;
|
||||
@ -57,7 +57,7 @@ public class TaskFactory {
|
||||
* @param <SCHEDULER> {@link DownloadSchedulers}
|
||||
* @return {@link DownloadTask}、{@link UploadTask}
|
||||
*/
|
||||
<ENTITY extends ITaskEntity, SCHEDULER extends ISchedulers> ITask createTask(String targetName,
|
||||
<ENTITY extends AbsTaskEntity, SCHEDULER extends ISchedulers> ITask createTask(String targetName,
|
||||
ENTITY entity, SCHEDULER schedulers) {
|
||||
if (entity instanceof DownloadTaskEntity) {
|
||||
return createDownloadTask(targetName, (DownloadTaskEntity) entity, schedulers);
|
||||
|
@ -78,7 +78,7 @@ public class UploadSchedulers implements ISchedulers<UploadTask> {
|
||||
}
|
||||
|
||||
@Override public void onFinish() {
|
||||
UploadEntity entity = task.getUploadEntity();
|
||||
UploadEntity entity = task.getEntity();
|
||||
if (entity.getFailNum() <= reTryNum) {
|
||||
UploadTask task = mQueue.getTask(entity);
|
||||
mQueue.reTryStart(task);
|
||||
@ -102,7 +102,7 @@ public class UploadSchedulers implements ISchedulers<UploadTask> {
|
||||
Log.w(TAG, "没有下一任务");
|
||||
return;
|
||||
}
|
||||
if (newTask.getUploadEntity().getState() == IEntity.STATE_WAIT) {
|
||||
if (newTask.getEntity().getState() == IEntity.STATE_WAIT) {
|
||||
mQueue.startTask(newTask);
|
||||
}
|
||||
}
|
||||
@ -169,7 +169,7 @@ public class UploadSchedulers implements ISchedulers<UploadTask> {
|
||||
return true;
|
||||
}
|
||||
callback(msg.what, task);
|
||||
UploadEntity entity = task.getUploadEntity();
|
||||
UploadEntity entity = task.getEntity();
|
||||
switch (msg.what) {
|
||||
case STOP:
|
||||
case CANCEL:
|
||||
|
@ -17,33 +17,17 @@ package com.arialyy.aria.core.upload;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import com.arialyy.aria.core.inf.IEntity;
|
||||
import com.arialyy.aria.orm.DbEntity;
|
||||
import com.arialyy.aria.core.inf.AbsEntity;
|
||||
import com.arialyy.aria.orm.Ignore;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2017/2/9.
|
||||
* 上传文件实体
|
||||
*/
|
||||
public class UploadEntity extends DbEntity implements IEntity, Parcelable {
|
||||
public class UploadEntity extends AbsEntity implements Parcelable {
|
||||
|
||||
private String filePath; //文件路径
|
||||
private String fileName; //文件名
|
||||
private long fileSize; //文件大小
|
||||
private int state = STATE_WAIT;
|
||||
private long currentProgress = 0;
|
||||
private boolean isComplete = false;
|
||||
@Ignore private long speed = 0; //下载速度
|
||||
@Ignore private String convertSpeed = "0/s";
|
||||
@Ignore private int failNum = 0;
|
||||
|
||||
public String getConvertSpeed() {
|
||||
return convertSpeed;
|
||||
}
|
||||
|
||||
public void setConvertSpeed(String convertSpeed) {
|
||||
this.convertSpeed = convertSpeed;
|
||||
}
|
||||
|
||||
public boolean isComplete() {
|
||||
return isComplete;
|
||||
@ -53,46 +37,6 @@ public class UploadEntity extends DbEntity implements IEntity, Parcelable {
|
||||
isComplete = complete;
|
||||
}
|
||||
|
||||
public long getCurrentProgress() {
|
||||
return currentProgress;
|
||||
}
|
||||
|
||||
public void setCurrentProgress(long currentProgress) {
|
||||
this.currentProgress = currentProgress;
|
||||
}
|
||||
|
||||
public long getFileSize() {
|
||||
return fileSize;
|
||||
}
|
||||
|
||||
public void setFileSize(long fileSize) {
|
||||
this.fileSize = fileSize;
|
||||
}
|
||||
|
||||
public long getSpeed() {
|
||||
return speed;
|
||||
}
|
||||
|
||||
public void setSpeed(long speed) {
|
||||
this.speed = speed;
|
||||
}
|
||||
|
||||
public int getFailNum() {
|
||||
return failNum;
|
||||
}
|
||||
|
||||
@Override public void setFailNum(int failNum) {
|
||||
this.failNum = failNum;
|
||||
}
|
||||
|
||||
@Override public int getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(int state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public String getFilePath() {
|
||||
return filePath;
|
||||
}
|
||||
@ -101,14 +45,6 @@ public class UploadEntity extends DbEntity implements IEntity, Parcelable {
|
||||
this.filePath = filePath;
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public void setFileName(String fileName) {
|
||||
this.fileName = fileName;
|
||||
}
|
||||
|
||||
public UploadEntity() {
|
||||
}
|
||||
|
||||
@ -117,27 +53,15 @@ public class UploadEntity extends DbEntity implements IEntity, Parcelable {
|
||||
}
|
||||
|
||||
@Override public void writeToParcel(Parcel dest, int flags) {
|
||||
super.writeToParcel(dest, flags);
|
||||
dest.writeString(this.filePath);
|
||||
dest.writeString(this.fileName);
|
||||
dest.writeLong(this.fileSize);
|
||||
dest.writeInt(this.state);
|
||||
dest.writeLong(this.currentProgress);
|
||||
dest.writeByte(this.isComplete ? (byte) 1 : (byte) 0);
|
||||
dest.writeLong(this.speed);
|
||||
dest.writeString(this.convertSpeed);
|
||||
dest.writeInt(this.failNum);
|
||||
}
|
||||
|
||||
protected UploadEntity(Parcel in) {
|
||||
super(in);
|
||||
this.filePath = in.readString();
|
||||
this.fileName = in.readString();
|
||||
this.fileSize = in.readLong();
|
||||
this.state = in.readInt();
|
||||
this.currentProgress = in.readLong();
|
||||
this.isComplete = in.readByte() != 0;
|
||||
this.speed = in.readLong();
|
||||
this.convertSpeed = in.readString();
|
||||
this.failNum = in.readInt();
|
||||
}
|
||||
|
||||
@Ignore public static final Creator<UploadEntity> CREATOR = new Creator<UploadEntity>() {
|
||||
|
@ -22,8 +22,8 @@ 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.AbsTask;
|
||||
import com.arialyy.aria.core.inf.IEntity;
|
||||
import com.arialyy.aria.core.inf.ITask;
|
||||
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
|
||||
import com.arialyy.aria.core.scheduler.ISchedulers;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
@ -33,55 +33,28 @@ import java.lang.ref.WeakReference;
|
||||
* Created by lyy on 2017/2/23.
|
||||
* 上传任务
|
||||
*/
|
||||
public class UploadTask implements ITask {
|
||||
public class UploadTask extends AbsTask<UploadTaskEntity, UploadEntity> {
|
||||
private static final String TAG = "UploadTask";
|
||||
private Handler mOutHandler;
|
||||
private UploadEntity mUploadEntity;
|
||||
private String mTargetName;
|
||||
|
||||
private UploadUtil mUtil;
|
||||
private UListener mListener;
|
||||
private boolean isHeighestTask = false;
|
||||
|
||||
private UploadTask(UploadTaskEntity taskEntity, Handler outHandler) {
|
||||
mOutHandler = outHandler;
|
||||
mUploadEntity = taskEntity.uploadEntity;
|
||||
mEntity = taskEntity.uploadEntity;
|
||||
mListener = new UListener(mOutHandler, this);
|
||||
mUtil = new UploadUtil(taskEntity, mListener);
|
||||
}
|
||||
|
||||
@Override public void setTargetName(String targetName) {
|
||||
mTargetName = targetName;
|
||||
}
|
||||
|
||||
@Override public void removeRecord() {
|
||||
mUploadEntity.deleteData();
|
||||
}
|
||||
|
||||
@Override public void setHighestPriority(boolean isHighestPriority) {
|
||||
isHeighestTask = isHighestPriority;
|
||||
}
|
||||
|
||||
@Override public boolean isHighestPriorityTask() {
|
||||
return isHeighestTask;
|
||||
}
|
||||
|
||||
@Override public String getKey() {
|
||||
return mUploadEntity.getFilePath();
|
||||
return mEntity.getFilePath();
|
||||
}
|
||||
|
||||
@Override public boolean isRunning() {
|
||||
return mUtil.isRunning();
|
||||
}
|
||||
|
||||
public UploadEntity getUploadEntity() {
|
||||
return mUploadEntity;
|
||||
}
|
||||
|
||||
@Override public IEntity getEntity() {
|
||||
return mUploadEntity;
|
||||
}
|
||||
|
||||
@Override public void start() {
|
||||
if (mUtil.isRunning()) {
|
||||
Log.d(TAG, "任务正在下载");
|
||||
@ -99,111 +72,20 @@ public class UploadTask implements ITask {
|
||||
|
||||
@Override public void cancel() {
|
||||
|
||||
if (!mUploadEntity.isComplete()) {
|
||||
if (!mEntity.isComplete()) {
|
||||
// 如果任务不是下载状态
|
||||
mUtil.cancel();
|
||||
mUploadEntity.deleteData();
|
||||
mEntity.deleteData();
|
||||
if (mOutHandler != null) {
|
||||
mOutHandler.obtainMessage(DownloadSchedulers.CANCEL, this).sendToTarget();
|
||||
}
|
||||
//发送取消下载的广播
|
||||
Intent intent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), Aria.ACTION_CANCEL);
|
||||
intent.putExtra(Aria.UPLOAD_ENTITY, mUploadEntity);
|
||||
intent.putExtra(Aria.UPLOAD_ENTITY, mEntity);
|
||||
AriaManager.APP.sendBroadcast(intent);
|
||||
}
|
||||
}
|
||||
|
||||
public String getTargetName() {
|
||||
return mTargetName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 返回原始byte速度,需要你在配置文件中配置
|
||||
* <pre>
|
||||
* {@code
|
||||
* <xml>
|
||||
* <upload>
|
||||
* ...
|
||||
* <convertSpeed value="false"/>
|
||||
* </upload>
|
||||
*
|
||||
* 或在代码中设置
|
||||
* Aria.get(this).getUploadConfig().setConvertSpeed(false);
|
||||
* </xml>
|
||||
* }
|
||||
* </pre>
|
||||
* 才能生效
|
||||
*/
|
||||
@Override public long getSpeed() {
|
||||
return mUploadEntity.getSpeed();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 返回转换单位后的速度,需要你在配置文件中配置,转换完成后为:1b/s、1kb/s、1mb/s、1gb/s、1tb/s
|
||||
* <pre>
|
||||
* {@code
|
||||
* <xml>
|
||||
* <upload>
|
||||
* ...
|
||||
* <convertSpeed value="true"/>
|
||||
* </upload>
|
||||
*
|
||||
* 或在代码中设置
|
||||
* Aria.get(this).getUploadConfig().setConvertSpeed(true);
|
||||
* </xml>
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* 才能生效
|
||||
*/
|
||||
@Override public String getConvertSpeed() {
|
||||
return mUploadEntity.getConvertSpeed();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取百分比进度
|
||||
*
|
||||
* @return 返回百分比进度,如果文件长度为0,返回0
|
||||
*/
|
||||
@Override public int getPercent() {
|
||||
if (mUploadEntity.getFileSize() == 0) {
|
||||
return 0;
|
||||
}
|
||||
return (int) (mUploadEntity.getCurrentProgress() * 100 / mUploadEntity.getFileSize());
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换单位后的文件长度
|
||||
*
|
||||
* @return 如果文件长度为0,则返回0m,否则返回转换后的长度1b、1kb、1mb、1gb、1tb
|
||||
*/
|
||||
@Override public String getConvertFileSize() {
|
||||
if (mUploadEntity.getFileSize() == 0) {
|
||||
return "0m";
|
||||
}
|
||||
return CommonUtil.formatFileSize(mUploadEntity.getFileSize());
|
||||
}
|
||||
|
||||
@Override public long getFileSize() {
|
||||
return mUploadEntity.getFileSize();
|
||||
}
|
||||
|
||||
@Override public long getCurrentProgress() {
|
||||
return mUploadEntity.getCurrentProgress();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取单位转换后的进度
|
||||
*
|
||||
* @return 如:已经下载3mb的大小,则返回{@code 3mb}
|
||||
*/
|
||||
@Override public String getConvertCurrentProgress() {
|
||||
if (mUploadEntity.getCurrentProgress() == 0) {
|
||||
return "0b";
|
||||
}
|
||||
return CommonUtil.formatFileSize(mUploadEntity.getCurrentProgress());
|
||||
}
|
||||
|
||||
private static class UListener extends UploadListener {
|
||||
WeakReference<Handler> outHandler;
|
||||
WeakReference<UploadTask> task;
|
||||
@ -220,7 +102,7 @@ public class UploadTask implements ITask {
|
||||
UListener(Handler outHandle, UploadTask task) {
|
||||
this.outHandler = new WeakReference<>(outHandle);
|
||||
this.task = new WeakReference<>(task);
|
||||
uploadEntity = this.task.get().getUploadEntity();
|
||||
uploadEntity = this.task.get().getEntity();
|
||||
sendIntent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), Aria.ACTION_RUNNING);
|
||||
sendIntent.putExtra(Aria.UPLOAD_ENTITY, uploadEntity);
|
||||
context = AriaManager.APP;
|
||||
|
@ -15,8 +15,7 @@
|
||||
*/
|
||||
package com.arialyy.aria.core.upload;
|
||||
|
||||
import com.arialyy.aria.core.inf.IEntity;
|
||||
import com.arialyy.aria.core.inf.ITaskEntity;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@ -24,7 +23,7 @@ import java.util.Map;
|
||||
* Created by lyy on 2017/2/9.
|
||||
* 上传任务实体
|
||||
*/
|
||||
public class UploadTaskEntity extends ITaskEntity {
|
||||
public class UploadTaskEntity extends AbsTaskEntity {
|
||||
public UploadEntity uploadEntity;
|
||||
public String uploadUrl; //上传路径
|
||||
public String attachment; //文件上传需要的key
|
||||
@ -41,7 +40,7 @@ public class UploadTaskEntity extends ITaskEntity {
|
||||
this.uploadEntity = downloadEntity;
|
||||
}
|
||||
|
||||
@Override public IEntity getEntity() {
|
||||
@Override public UploadEntity getEntity() {
|
||||
return uploadEntity;
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ import java.util.Map;
|
||||
public class DBConfig {
|
||||
static Map<String, Class> mapping = new HashMap<>();
|
||||
static String DB_NAME;
|
||||
static int VERSION = 2;
|
||||
static int VERSION = 3;
|
||||
|
||||
static {
|
||||
if (TextUtils.isEmpty(DB_NAME)) {
|
||||
|
@ -140,8 +140,8 @@ final class SqlHelper extends SQLiteOpenHelper {
|
||||
*/
|
||||
private int getEntityAttr(Class clazz) {
|
||||
int count = 1;
|
||||
Field[] fields = CommonUtil.getFields(clazz);
|
||||
if (fields != null && fields.length > 0) {
|
||||
List<Field> fields = CommonUtil.getAllFields(clazz);
|
||||
if (fields != null && fields.size() > 0) {
|
||||
for (Field field : fields) {
|
||||
field.setAccessible(true);
|
||||
if (ignoreField(field)) {
|
||||
@ -258,8 +258,8 @@ final class SqlHelper extends SQLiteOpenHelper {
|
||||
static synchronized void modifyData(SQLiteDatabase db, DbEntity dbEntity) {
|
||||
db = checkDb(db);
|
||||
Class<?> clazz = dbEntity.getClass();
|
||||
Field[] fields = CommonUtil.getFields(clazz);
|
||||
if (fields != null && fields.length > 0) {
|
||||
List<Field> fields = CommonUtil.getAllFields(clazz);
|
||||
if (fields != null && fields.size() > 0) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("UPDATE ").append(CommonUtil.getClassName(dbEntity)).append(" SET ");
|
||||
int i = 0;
|
||||
@ -293,8 +293,8 @@ final class SqlHelper extends SQLiteOpenHelper {
|
||||
static synchronized void insertData(SQLiteDatabase db, DbEntity dbEntity) {
|
||||
db = checkDb(db);
|
||||
Class<?> clazz = dbEntity.getClass();
|
||||
Field[] fields = CommonUtil.getFields(clazz);
|
||||
if (fields != null && fields.length > 0) {
|
||||
List<Field> fields = CommonUtil.getAllFields(clazz);
|
||||
if (fields != null && fields.size() > 0) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("INSERT INTO ").append(CommonUtil.getClassName(dbEntity)).append("(");
|
||||
int i = 0;
|
||||
@ -369,9 +369,7 @@ final class SqlHelper extends SQLiteOpenHelper {
|
||||
*/
|
||||
static synchronized void createTable(SQLiteDatabase db, Class clazz, String tableName) {
|
||||
db = checkDb(db);
|
||||
//Field[] fields = CommonUtil.getFields(clazz);
|
||||
List<Field> fields = CommonUtil.getAllFields(clazz);
|
||||
//Field[] fields = CommonUtil.getAllFields(clazz);
|
||||
if (fields != null && fields.size() > 0) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("create table ")
|
||||
@ -451,9 +449,9 @@ final class SqlHelper extends SQLiteOpenHelper {
|
||||
*/
|
||||
static synchronized <T extends DbEntity> List<T> newInstanceEntity(Class<T> clazz,
|
||||
Cursor cursor) {
|
||||
Field[] fields = CommonUtil.getFields(clazz);
|
||||
List<Field> fields = CommonUtil.getAllFields(clazz);
|
||||
List<T> entitys = new ArrayList<>();
|
||||
if (fields != null && fields.length > 0) {
|
||||
if (fields != null && fields.size() > 0) {
|
||||
try {
|
||||
while (cursor.moveToNext()) {
|
||||
T entity = clazz.newInstance();
|
||||
|
@ -20,7 +20,7 @@ import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.core.download.DownloadEntity;
|
||||
import com.arialyy.aria.core.download.DownloadTaskEntity;
|
||||
import com.arialyy.aria.core.inf.ITaskEntity;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
import com.arialyy.aria.core.upload.UploadEntity;
|
||||
import com.arialyy.aria.core.upload.UploadTaskEntity;
|
||||
import com.arialyy.aria.exception.FileException;
|
||||
@ -86,7 +86,7 @@ public class CheckUtil {
|
||||
/**
|
||||
* 检查任务实体
|
||||
*/
|
||||
public static void checkTaskEntity(ITaskEntity entity) {
|
||||
public static void checkTaskEntity(AbsTaskEntity entity) {
|
||||
if (entity instanceof DownloadTaskEntity) {
|
||||
checkDownloadTaskEntity(((DownloadTaskEntity) entity).downloadEntity);
|
||||
} else if (entity instanceof UploadTaskEntity) {
|
||||
@ -100,7 +100,7 @@ public class CheckUtil {
|
||||
* @param checkType 删除命令和停止命令不需要检查下载链接和保存路径
|
||||
* @return {@code false}实体无效
|
||||
*/
|
||||
public static boolean checkCmdEntity(ITaskEntity entity, boolean checkType) {
|
||||
public static boolean checkCmdEntity(AbsTaskEntity entity, boolean checkType) {
|
||||
boolean b = false;
|
||||
if (entity instanceof DownloadTaskEntity) {
|
||||
DownloadEntity entity1 = ((DownloadTaskEntity) entity).downloadEntity;
|
||||
|
@ -24,7 +24,7 @@ import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.core.command.CmdFactory;
|
||||
import com.arialyy.aria.core.command.AbsCmd;
|
||||
import com.arialyy.aria.core.inf.ITaskEntity;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
@ -149,7 +149,7 @@ public class CommonUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public static <T extends ITaskEntity> AbsCmd createCmd(String target, T entity, int cmd) {
|
||||
public static <T extends AbsTaskEntity> AbsCmd createCmd(String target, T entity, int cmd) {
|
||||
return CmdFactory.getInstance().createCmd(target, entity, cmd);
|
||||
}
|
||||
|
||||
@ -206,7 +206,7 @@ public class CommonUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取类里面的所在字段
|
||||
* 获取当前类里面的所在字段
|
||||
*/
|
||||
public static Field[] getFields(Class clazz) {
|
||||
Field[] fields = null;
|
||||
|
Reference in New Issue
Block a user