fix bug
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
package com.arialyy.downloadutil.core;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
import android.util.SparseArray;
|
||||
import com.arialyy.downloadutil.util.Util;
|
||||
@ -128,7 +127,8 @@ final class DownLoadUtil {
|
||||
*
|
||||
* @param downloadListener 下载进度监听 {@link DownloadListener}
|
||||
*/
|
||||
public void start(@NonNull final IDownloadListener downloadListener) {
|
||||
public void start(IDownloadListener downloadListener) {
|
||||
mListener = downloadListener;
|
||||
isDownloading = true;
|
||||
mCurrentLocation = 0;
|
||||
isStop = false;
|
||||
@ -153,13 +153,13 @@ final class DownLoadUtil {
|
||||
failDownload("下载失败,记录文件被删除");
|
||||
return;
|
||||
}
|
||||
mListener.onPre();
|
||||
new Thread(new Runnable() {
|
||||
@Override public void run() {
|
||||
try {
|
||||
mListener = downloadListener;
|
||||
URL url = new URL(downloadUrl);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
setCommadParam(conn);
|
||||
setConnectParam(conn);
|
||||
conn.setConnectTimeout(TIME_OUT * 4);
|
||||
conn.connect();
|
||||
int len = conn.getContentLength();
|
||||
@ -169,14 +169,13 @@ final class DownLoadUtil {
|
||||
}
|
||||
int code = conn.getResponseCode();
|
||||
if (code == 200) {
|
||||
|
||||
int fileLength = conn.getContentLength();
|
||||
//必须建一个文件
|
||||
Util.createFile(filePath);
|
||||
RandomAccessFile file = new RandomAccessFile(filePath, "rwd");
|
||||
//设置文件长度
|
||||
file.setLength(fileLength);
|
||||
mListener.onPreDownload(conn.getContentLength());
|
||||
mListener.onPostPre(fileLength);
|
||||
//分配每条线程的下载区间
|
||||
Properties pro = null;
|
||||
pro = Util.loadConfig(configFile);
|
||||
@ -281,7 +280,7 @@ final class DownLoadUtil {
|
||||
mListener.onFail();
|
||||
}
|
||||
|
||||
private void setCommadParam(HttpURLConnection conn) throws ProtocolException {
|
||||
private void setConnectParam(HttpURLConnection conn) throws ProtocolException {
|
||||
conn.setRequestMethod("GET");
|
||||
conn.setRequestProperty("Charset", "UTF-8");
|
||||
conn.setRequestProperty("User-Agent",
|
||||
@ -323,7 +322,7 @@ final class DownLoadUtil {
|
||||
//在头里面请求下载开始位置和结束位置
|
||||
conn.setRequestProperty("Range",
|
||||
"bytes=" + dEntity.startLocation + "-" + dEntity.endLocation);
|
||||
setCommadParam(conn);
|
||||
setConnectParam(conn);
|
||||
conn.setConnectTimeout(TIME_OUT * 4);
|
||||
conn.setReadTimeout(TIME_OUT * 24); //设置读取流的等待时间,必须设置该参数
|
||||
is = conn.getInputStream();
|
||||
@ -506,7 +505,11 @@ final class DownLoadUtil {
|
||||
|
||||
}
|
||||
|
||||
@Override public void onPreDownload(long fileSize) {
|
||||
@Override public void onPre() {
|
||||
|
||||
}
|
||||
|
||||
@Override public void onPostPre(long fileSize) {
|
||||
|
||||
}
|
||||
|
||||
|
@ -8,8 +8,10 @@ import com.arialyy.downloadutil.orm.Ignore;
|
||||
/**
|
||||
* Created by lyy on 2015/12/25.
|
||||
* 下载实体
|
||||
* !!! 注意:CREATOR要进行@Ignore注解
|
||||
* !!!并且需要Parcelable时需要手动填写rowID;
|
||||
*/
|
||||
public class DownloadEntity extends DbEntity implements Parcelable, Cloneable {
|
||||
public class DownloadEntity extends DbEntity implements Parcelable {
|
||||
/**
|
||||
* 其它状态
|
||||
*/
|
||||
@ -34,10 +36,18 @@ public class DownloadEntity extends DbEntity implements Parcelable, Cloneable {
|
||||
* 下载中
|
||||
*/
|
||||
@Ignore public static final int STATE_DOWNLOAD_ING = 4;
|
||||
/**
|
||||
* 预处理
|
||||
*/
|
||||
@Ignore public static final int STATE_PRE = 5;
|
||||
/**
|
||||
* 预处理完成
|
||||
*/
|
||||
@Ignore public static final int STATE_POST_PRE = 6;
|
||||
/**
|
||||
* 取消下载
|
||||
*/
|
||||
@Ignore public static final int STATE_CANCEL = 5;
|
||||
@Ignore public static final int STATE_CANCEL = 7;
|
||||
|
||||
@Ignore private long speed = 0; //下载速度
|
||||
private String downloadUrl = ""; //下载路径
|
||||
@ -175,6 +185,7 @@ public class DownloadEntity extends DbEntity implements Parcelable, Cloneable {
|
||||
dest.writeLong(this.currentProgress);
|
||||
dest.writeInt(this.failNum);
|
||||
dest.writeLong(this.speed);
|
||||
dest.writeInt(this.rowID);
|
||||
}
|
||||
|
||||
protected DownloadEntity(Parcel in) {
|
||||
@ -189,6 +200,7 @@ public class DownloadEntity extends DbEntity implements Parcelable, Cloneable {
|
||||
this.currentProgress = in.readLong();
|
||||
this.failNum = in.readInt();
|
||||
this.speed = in.readLong();
|
||||
this.rowID = in.readInt();
|
||||
}
|
||||
|
||||
@Ignore public static final Creator<DownloadEntity> CREATOR = new Creator<DownloadEntity>() {
|
||||
|
@ -3,7 +3,7 @@ package com.arialyy.downloadutil.core;
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
import com.arialyy.downloadutil.core.command.IDownloadCommand;
|
||||
import com.arialyy.downloadutil.core.command.IDownloadCmd;
|
||||
import com.arialyy.downloadutil.orm.DbEntity;
|
||||
import com.arialyy.downloadutil.orm.DbUtil;
|
||||
import java.util.ArrayList;
|
||||
@ -14,13 +14,19 @@ import java.util.List;
|
||||
* 下载管理器,通过命令的方式控制下载
|
||||
*/
|
||||
public class DownloadManager {
|
||||
private static final String TAG = "DownloadManager";
|
||||
private static final Object LOCK = new Object();
|
||||
private static volatile DownloadManager INSTANCE = null;
|
||||
private static final String TAG = "DownloadManager";
|
||||
private static final Object LOCK = new Object();
|
||||
private static volatile DownloadManager INSTANCE = null;
|
||||
|
||||
/**
|
||||
* 预处理完成
|
||||
*/
|
||||
public static final String ACTION_PRE = "ACTION_PRE";
|
||||
|
||||
/**
|
||||
* 下载开始前事件
|
||||
*/
|
||||
public static final String ACTION_PRE = "ACTION_PRE";
|
||||
public static final String ACTION_POST_PRE = "ACTION_POST_PRE";
|
||||
|
||||
/**
|
||||
* 开始下载事件
|
||||
@ -72,7 +78,7 @@ public class DownloadManager {
|
||||
*/
|
||||
public static final String CURRENT_SPEED = "CURRENT_SPEED";
|
||||
|
||||
private List<IDownloadCommand> mCommands = new ArrayList<>();
|
||||
private List<IDownloadCmd> mCommands = new ArrayList<>();
|
||||
|
||||
private DownloadManager() {
|
||||
|
||||
@ -113,7 +119,7 @@ public class DownloadManager {
|
||||
/**
|
||||
* 设置命令
|
||||
*/
|
||||
public DownloadManager setCommand(IDownloadCommand command) {
|
||||
public DownloadManager setCmd(IDownloadCmd command) {
|
||||
mCommands.add(command);
|
||||
return this;
|
||||
}
|
||||
@ -121,7 +127,7 @@ public class DownloadManager {
|
||||
/**
|
||||
* 设置一组命令
|
||||
*/
|
||||
public DownloadManager setCommands(List<IDownloadCommand> commands) {
|
||||
public DownloadManager setCmds(List<IDownloadCmd> commands) {
|
||||
if (commands != null && commands.size() > 0) {
|
||||
mCommands.addAll(commands);
|
||||
}
|
||||
@ -132,7 +138,7 @@ public class DownloadManager {
|
||||
* 执行所有设置的命令
|
||||
*/
|
||||
public synchronized void exe() {
|
||||
for (IDownloadCommand command : mCommands) {
|
||||
for (IDownloadCmd command : mCommands) {
|
||||
command.executeComment();
|
||||
}
|
||||
mCommands.clear();
|
||||
|
@ -40,6 +40,7 @@ public class DownloadTarget extends IDownloadTarget {
|
||||
|
||||
@Override public void startTask(Task task) {
|
||||
if (mExecutePool.putTask(task)) {
|
||||
mCachePool.removeTask(task);
|
||||
task.start();
|
||||
}
|
||||
}
|
||||
|
@ -15,9 +15,14 @@ public interface IDownloadListener {
|
||||
public void onFail();
|
||||
|
||||
/**
|
||||
* 下载预处理,可通过HttpURLConnection获取文件长度
|
||||
* 预处理
|
||||
*/
|
||||
public void onPreDownload(long fileSize);
|
||||
public void onPre();
|
||||
|
||||
/**
|
||||
* 预处理完成,准备下载---开始下载之间
|
||||
*/
|
||||
public void onPostPre(long fileSize);
|
||||
|
||||
/**
|
||||
* 下载监听
|
||||
|
@ -149,6 +149,10 @@ public abstract class IDownloadTarget implements IDownloader, ITask {
|
||||
switch (msg.what) {
|
||||
case STOP:
|
||||
case CANCEL:
|
||||
if (target.mExecutePool.size() != ExecutePool.SIZE) {
|
||||
startNextTask(entity);
|
||||
}
|
||||
break;
|
||||
case COMPLETE:
|
||||
startNextTask(entity);
|
||||
break;
|
||||
|
@ -142,13 +142,19 @@ public class Task {
|
||||
sendIntent.putExtra(DownloadManager.ENTITY, downloadEntity);
|
||||
}
|
||||
|
||||
@Override public void onPreDownload(long fileSize) {
|
||||
super.onPreDownload(fileSize);
|
||||
downloadEntity.setFileSize(fileSize);
|
||||
downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING);
|
||||
@Override public void onPre() {
|
||||
super.onPre();
|
||||
downloadEntity.setState(DownloadEntity.STATE_PRE);
|
||||
sendIntent(DownloadManager.ACTION_PRE, -1);
|
||||
}
|
||||
|
||||
@Override public void onPostPre(long fileSize) {
|
||||
super.onPostPre(fileSize);
|
||||
downloadEntity.setFileSize(fileSize);
|
||||
downloadEntity.setState(DownloadEntity.STATE_POST_PRE);
|
||||
sendIntent(DownloadManager.ACTION_POST_PRE, -1);
|
||||
}
|
||||
|
||||
@Override public void onResume(long resumeLocation) {
|
||||
super.onResume(resumeLocation);
|
||||
downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING);
|
||||
|
@ -9,9 +9,9 @@ import com.arialyy.downloadutil.core.Task;
|
||||
* Created by lyy on 2016/8/22.
|
||||
* 添加任务的命令
|
||||
*/
|
||||
class AddCommand extends IDownloadCommand {
|
||||
class AddCmd extends IDownloadCmd {
|
||||
|
||||
AddCommand(Context context, DownloadEntity entity) {
|
||||
AddCmd(Context context, DownloadEntity entity) {
|
||||
super(context, entity);
|
||||
}
|
||||
|
@ -8,9 +8,9 @@ import com.arialyy.downloadutil.core.Task;
|
||||
* Created by lyy on 2016/9/20.
|
||||
* 取消命令
|
||||
*/
|
||||
class CancelCommand extends IDownloadCommand {
|
||||
class CancelCmd extends IDownloadCmd {
|
||||
|
||||
CancelCommand(Context context, DownloadEntity entity) {
|
||||
CancelCmd(Context context, DownloadEntity entity) {
|
||||
super(context, entity);
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import com.arialyy.downloadutil.core.DownloadEntity;
|
||||
* Created by Lyy on 2016/9/23.
|
||||
* 命令工厂
|
||||
*/
|
||||
public class CommandFactory {
|
||||
public class CmdFactory {
|
||||
/**
|
||||
* 创建任务
|
||||
*/
|
||||
@ -33,17 +33,17 @@ public class CommandFactory {
|
||||
*/
|
||||
public static final int TASK_STATE = 0x126;
|
||||
|
||||
private static final Object LOCK = new Object();
|
||||
private static volatile CommandFactory INSTANCE = null;
|
||||
private static final Object LOCK = new Object();
|
||||
private static volatile CmdFactory INSTANCE = null;
|
||||
|
||||
private CommandFactory() {
|
||||
private CmdFactory() {
|
||||
|
||||
}
|
||||
|
||||
public static CommandFactory getInstance() {
|
||||
public static CmdFactory getInstance() {
|
||||
if (INSTANCE == null) {
|
||||
synchronized (LOCK) {
|
||||
INSTANCE = new CommandFactory();
|
||||
INSTANCE = new CmdFactory();
|
||||
}
|
||||
}
|
||||
return INSTANCE;
|
||||
@ -55,19 +55,19 @@ public class CommandFactory {
|
||||
* @param type 命令类型{@link #TASK_CREATE}、{@link #TASK_START}、{@link #TASK_CANCEL}、{@link
|
||||
* #TASK_STOP}、{@link #TASK_STATE}
|
||||
*/
|
||||
public IDownloadCommand createCommand(Context context, DownloadEntity entity, int type) {
|
||||
public IDownloadCmd createCmd(Context context, DownloadEntity entity, int type) {
|
||||
switch (type) {
|
||||
case TASK_CREATE:
|
||||
return createAddCommand(context, entity);
|
||||
return createAddCmd(context, entity);
|
||||
case TASK_RESUME:
|
||||
case TASK_START:
|
||||
return createStartCommand(context, entity);
|
||||
return createStartCmd(context, entity);
|
||||
case TASK_CANCEL:
|
||||
return createCancelCommand(context, entity);
|
||||
return createCancelCmd(context, entity);
|
||||
case TASK_STOP:
|
||||
return createStopCommand(context, entity);
|
||||
return createStopCmd(context, entity);
|
||||
case TASK_STATE:
|
||||
return createStateCommand(context, entity);
|
||||
return createStateCmd(context, entity);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
@ -76,45 +76,45 @@ public class CommandFactory {
|
||||
/**
|
||||
* 创建获取任务状态的命令
|
||||
*
|
||||
* @return {@link StateCommand}
|
||||
* @return {@link StateCmd}
|
||||
*/
|
||||
private StateCommand createStateCommand(Context context, DownloadEntity entity) {
|
||||
return new StateCommand(context, entity);
|
||||
private StateCmd createStateCmd(Context context, DownloadEntity entity) {
|
||||
return new StateCmd(context, entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建停止命令
|
||||
*
|
||||
* @return {@link StopCommand}
|
||||
* @return {@link StopCmd}
|
||||
*/
|
||||
private StopCommand createStopCommand(Context context, DownloadEntity entity) {
|
||||
return new StopCommand(context, entity);
|
||||
private StopCmd createStopCmd(Context context, DownloadEntity entity) {
|
||||
return new StopCmd(context, entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建下载任务命令
|
||||
*
|
||||
* @return {@link AddCommand}
|
||||
* @return {@link AddCmd}
|
||||
*/
|
||||
private AddCommand createAddCommand(Context context, DownloadEntity entity) {
|
||||
return new AddCommand(context, entity);
|
||||
private AddCmd createAddCmd(Context context, DownloadEntity entity) {
|
||||
return new AddCmd(context, entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建启动下载命令
|
||||
*
|
||||
* @return {@link StartCommand}
|
||||
* @return {@link StartCmd}
|
||||
*/
|
||||
private StartCommand createStartCommand(Context context, DownloadEntity entity) {
|
||||
return new StartCommand(context, entity);
|
||||
private StartCmd createStartCmd(Context context, DownloadEntity entity) {
|
||||
return new StartCmd(context, entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 取消下载的命令
|
||||
*
|
||||
* @return {@link CancelCommand}
|
||||
* @return {@link CancelCmd}
|
||||
*/
|
||||
private CancelCommand createCancelCommand(Context context, DownloadEntity entity) {
|
||||
return new CancelCommand(context, entity);
|
||||
private CancelCmd createCancelCmd(Context context, DownloadEntity entity) {
|
||||
return new CancelCmd(context, entity);
|
||||
}
|
||||
}
|
@ -11,7 +11,7 @@ import com.arialyy.downloadutil.util.Util;
|
||||
* Created by lyy on 2016/8/22.
|
||||
* 下载命令
|
||||
*/
|
||||
public abstract class IDownloadCommand {
|
||||
public abstract class IDownloadCmd {
|
||||
protected IDownloadTarget target;
|
||||
protected Context mContext;
|
||||
protected DownloadEntity mEntity;
|
||||
@ -21,7 +21,7 @@ public abstract class IDownloadCommand {
|
||||
* @param context context
|
||||
* @param entity 下载实体
|
||||
*/
|
||||
protected IDownloadCommand(Context context, DownloadEntity entity) {
|
||||
protected IDownloadCmd(Context context, DownloadEntity entity) {
|
||||
if (!CheckHelp.checkDownloadEntity(entity)) {
|
||||
return;
|
||||
}
|
@ -8,9 +8,9 @@ import com.arialyy.downloadutil.core.Task;
|
||||
* Created by lyy on 2016/8/22.
|
||||
* 开始命令
|
||||
*/
|
||||
class StartCommand extends IDownloadCommand {
|
||||
class StartCmd extends IDownloadCmd {
|
||||
|
||||
StartCommand(Context context, DownloadEntity entity) {
|
||||
StartCmd(Context context, DownloadEntity entity) {
|
||||
super(context, entity);
|
||||
}
|
||||
|
@ -7,13 +7,13 @@ import com.arialyy.downloadutil.core.DownloadEntity;
|
||||
* Created by lyy on 2016/9/20.
|
||||
* 获取下载状态的命令
|
||||
*/
|
||||
class StateCommand extends IDownloadCommand {
|
||||
class StateCmd extends IDownloadCmd {
|
||||
|
||||
/**
|
||||
* @param context context
|
||||
* @param entity 下载实体
|
||||
*/
|
||||
StateCommand(Context context, DownloadEntity entity) {
|
||||
StateCmd(Context context, DownloadEntity entity) {
|
||||
super(context, entity);
|
||||
}
|
||||
|
@ -9,13 +9,13 @@ import com.arialyy.downloadutil.core.Task;
|
||||
* Created by lyy on 2016/9/20.
|
||||
* 停止命令
|
||||
*/
|
||||
class StopCommand extends IDownloadCommand {
|
||||
class StopCmd extends IDownloadCmd {
|
||||
|
||||
/**
|
||||
* @param context context
|
||||
* @param entity 下载实体
|
||||
*/
|
||||
StopCommand(Context context, DownloadEntity entity) {
|
||||
StopCmd(Context context, DownloadEntity entity) {
|
||||
super(context, entity);
|
||||
}
|
||||
|
@ -2,8 +2,8 @@ package com.arialyy.downloadutil.core.pool;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import com.arialyy.downloadutil.core.inf.IPool;
|
||||
import com.arialyy.downloadutil.core.Task;
|
||||
import com.arialyy.downloadutil.core.inf.IPool;
|
||||
import com.arialyy.downloadutil.util.Util;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@ -19,7 +19,7 @@ public class ExecutePool implements IPool {
|
||||
private static final Object LOCK = new Object();
|
||||
private static final long TIME_OUT = 1000;
|
||||
private static volatile ExecutePool INSTANCE = null;
|
||||
private static int SIZE = 2;
|
||||
public static int SIZE = 2;
|
||||
private ArrayBlockingQueue<Task> mExecuteQueue;
|
||||
private Map<String, Task> mExecuteArray;
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.arialyy.downloadutil.orm;
|
||||
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
import com.arialyy.downloadutil.util.Util;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
@ -11,8 +12,9 @@ import java.util.List;
|
||||
* 所有数据库实体父类
|
||||
*/
|
||||
public class DbEntity {
|
||||
protected int rowID = -1;
|
||||
private DbUtil mUtil = DbUtil.getInstance();
|
||||
protected int rowID = -1;
|
||||
private DbUtil mUtil = DbUtil.getInstance();
|
||||
private static final Object LOCK = new Object();
|
||||
|
||||
protected DbEntity() {
|
||||
|
||||
@ -56,11 +58,13 @@ public class DbEntity {
|
||||
/**
|
||||
* 保存自身,如果表中已经有数据,则更新数据,否则插入数据
|
||||
*/
|
||||
public synchronized void save() {
|
||||
if (mUtil.tableExists(getClass()) && thisIsExist()) {
|
||||
update();
|
||||
} else {
|
||||
insert();
|
||||
public void save() {
|
||||
synchronized (LOCK) {
|
||||
if (mUtil.tableExists(getClass()) && thisIsExist()) {
|
||||
update();
|
||||
} else {
|
||||
insert();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -292,6 +292,9 @@ public class DbUtil {
|
||||
* @param type {@link DbUtil}
|
||||
*/
|
||||
private void print(int type, String sql) {
|
||||
if (true){
|
||||
return;
|
||||
}
|
||||
String str = "";
|
||||
switch (type) {
|
||||
case 0:
|
||||
@ -409,6 +412,7 @@ public class DbUtil {
|
||||
}
|
||||
entity.rowID = cursor.getInt(cursor.getColumnIndex("rowid"));
|
||||
entitys.add(entity);
|
||||
Log.d(TAG, "rowid ==> " + entity.rowID);
|
||||
}
|
||||
} catch (InstantiationException e) {
|
||||
e.printStackTrace();
|
||||
|
Reference in New Issue
Block a user