大量bug修复,单任务下载例子编写
This commit is contained in:
@ -5,6 +5,8 @@ import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.arialyy.downloadutil.core.command.IDownloadCommand;
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
import com.arialyy.downloadutil.orm.DbEntity;
|
||||
import com.arialyy.downloadutil.orm.DbUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -102,6 +104,10 @@ public class DownloadManager {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public List<DownloadEntity> getAllDownloadEntity(){
|
||||
return DbEntity.findAllData(DownloadEntity.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置命令
|
||||
*/
|
||||
|
@ -4,6 +4,7 @@ import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
import com.arialyy.downloadutil.util.Task;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2016/8/17.
|
||||
@ -53,13 +54,13 @@ public class DownloadTarget extends IDownloadTarget {
|
||||
}
|
||||
|
||||
@Override public void cancelTask(Task task) {
|
||||
if (mExecutePool.removeTask(task)) {
|
||||
if (mExecutePool.removeTask(task) || mCachePool.removeTask(task)) {
|
||||
task.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void reTryStart(Task task) {
|
||||
if (!task.getDownloadUtil().isDownloading()) {
|
||||
if (!task.isDownloading()) {
|
||||
task.start();
|
||||
} else {
|
||||
Log.w(TAG, "任务没有完全停止,重试下载失败");
|
||||
@ -77,9 +78,6 @@ public class DownloadTarget extends IDownloadTarget {
|
||||
if (task == null) {
|
||||
task = mCachePool.getTask(entity.getDownloadUrl());
|
||||
}
|
||||
if (task == null) {
|
||||
task = createTask(entity);
|
||||
}
|
||||
return task;
|
||||
}
|
||||
|
||||
|
@ -9,6 +9,7 @@ import com.arialyy.downloadutil.core.inf.ITask;
|
||||
import com.arialyy.downloadutil.core.pool.CachePool;
|
||||
import com.arialyy.downloadutil.core.pool.ExecutePool;
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
import com.arialyy.downloadutil.util.Task;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2016/8/16.
|
||||
@ -210,7 +211,7 @@ public abstract class IDownloadTarget implements IDownloader, ITask {
|
||||
target.removeTask(entity);
|
||||
Task newTask = target.getNextTask();
|
||||
if (newTask == null) {
|
||||
Log.e(TAG, "没有下一任务");
|
||||
Log.w(TAG, "没有下一任务");
|
||||
return;
|
||||
}
|
||||
target.startTask(newTask);
|
||||
|
@ -4,6 +4,7 @@ import android.content.Context;
|
||||
import android.os.Handler;
|
||||
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
import com.arialyy.downloadutil.util.Task;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2016/8/18.
|
||||
@ -37,6 +38,6 @@ public class TaskFactory {
|
||||
public Task createTask(Context context, DownloadEntity entity, Handler handler) {
|
||||
Task.Builder builder = new Task.Builder(context, entity);
|
||||
builder.setOutHandler(handler);
|
||||
return builder.builder();
|
||||
return builder.build();
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
package com.arialyy.downloadutil.core.command;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.arialyy.downloadutil.util.Task;
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
|
||||
/**
|
||||
@ -15,6 +17,11 @@ class AddCommand extends IDownloadCommand {
|
||||
}
|
||||
|
||||
@Override public void executeComment() {
|
||||
target.createTask(mEntity);
|
||||
Task task = target.getTask(mEntity);
|
||||
if (task == null) {
|
||||
target.createTask(mEntity);
|
||||
}else {
|
||||
Log.w(TAG, "添加命令执行失败,【该任务已经存在】");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package com.arialyy.downloadutil.core.command;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.arialyy.downloadutil.util.Task;
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
|
||||
/**
|
||||
@ -15,6 +16,12 @@ class CancelCommand extends IDownloadCommand {
|
||||
}
|
||||
|
||||
@Override public void executeComment() {
|
||||
target.cancelTask(target.getTask(mEntity));
|
||||
Task task = target.getTask(mEntity);
|
||||
if (task == null){
|
||||
task = target.createTask(mEntity);
|
||||
}
|
||||
if (task != null) {
|
||||
target.cancelTask(task);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import com.arialyy.downloadutil.core.DownloadTarget;
|
||||
import com.arialyy.downloadutil.core.IDownloadTarget;
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
import com.arialyy.downloadutil.help.CheckHelp;
|
||||
import com.arialyy.downloadutil.util.Util;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2016/8/22.
|
||||
@ -15,6 +16,7 @@ public abstract class IDownloadCommand {
|
||||
protected IDownloadTarget target;
|
||||
protected Context mContext;
|
||||
protected DownloadEntity mEntity;
|
||||
protected String TAG;
|
||||
|
||||
/**
|
||||
* @param context context
|
||||
@ -27,6 +29,7 @@ public abstract class IDownloadCommand {
|
||||
target = DownloadTarget.getInstance();
|
||||
mContext = context;
|
||||
mEntity = entity;
|
||||
TAG = Util.getClassName(this);
|
||||
}
|
||||
|
||||
public Context getContext() {
|
||||
|
@ -2,6 +2,7 @@ package com.arialyy.downloadutil.core.command;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.arialyy.downloadutil.util.Task;
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
|
||||
/**
|
||||
@ -15,6 +16,12 @@ class StartCommand extends IDownloadCommand {
|
||||
}
|
||||
|
||||
@Override public void executeComment() {
|
||||
target.startTask(target.getTask(mEntity));
|
||||
Task task = target.getTask(mEntity);
|
||||
if (task == null) {
|
||||
task = target.createTask(mEntity);
|
||||
}
|
||||
if (task != null) {
|
||||
target.startTask(task);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ class StateCommand extends IDownloadCommand {
|
||||
}
|
||||
|
||||
@Override public void executeComment() {
|
||||
|
||||
target.getTaskState(mEntity);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
package com.arialyy.downloadutil.core.command;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.arialyy.downloadutil.util.Task;
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
|
||||
/**
|
||||
@ -19,6 +21,11 @@ class StopCommand extends IDownloadCommand {
|
||||
}
|
||||
|
||||
@Override public void executeComment() {
|
||||
target.stopTask(target.getTask(mEntity));
|
||||
Task task = target.getTask(mEntity);
|
||||
if (task != null) {
|
||||
target.stopTask(task);
|
||||
} else {
|
||||
Log.w(TAG, "停止命令执行失败,【调度器中没有该任务】");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
package com.arialyy.downloadutil.core.inf;
|
||||
|
||||
import com.arialyy.downloadutil.core.Task;
|
||||
import com.arialyy.downloadutil.util.Task;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2016/8/16.
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.arialyy.downloadutil.core.inf;
|
||||
|
||||
import com.arialyy.downloadutil.core.Task;
|
||||
import com.arialyy.downloadutil.util.Task;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2016/8/14.
|
||||
|
@ -1,6 +1,6 @@
|
||||
package com.arialyy.downloadutil.core.inf;
|
||||
|
||||
import com.arialyy.downloadutil.core.Task;
|
||||
import com.arialyy.downloadutil.util.Task;
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
|
||||
/**
|
||||
|
@ -3,7 +3,7 @@ package com.arialyy.downloadutil.core.pool;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.arialyy.downloadutil.core.Task;
|
||||
import com.arialyy.downloadutil.util.Task;
|
||||
import com.arialyy.downloadutil.core.inf.IPool;
|
||||
import com.arialyy.downloadutil.util.Util;
|
||||
|
||||
@ -45,14 +45,11 @@ public class CachePool implements IPool {
|
||||
}
|
||||
String url = task.getDownloadEntity().getDownloadUrl();
|
||||
if (mCacheQueue.contains(task)) {
|
||||
Log.e(TAG, "队列中已经包含了该任务,任务下载链接【" + url + "】");
|
||||
Log.w(TAG, "队列中已经包含了该任务,任务下载链接【" + url + "】");
|
||||
return false;
|
||||
} else {
|
||||
boolean s = mCacheQueue.offer(task);
|
||||
Log.w(TAG, "任务添加" + (s ?
|
||||
"成功" :
|
||||
"失败,【" + url + "】"
|
||||
));
|
||||
boolean s = mCacheQueue.offer(task);
|
||||
Log.d(TAG, "任务添加" + (s ? "成功" : "失败,【" + url + "】"));
|
||||
if (s) {
|
||||
mCacheArray.put(Util.keyToHashKey(url), task);
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package com.arialyy.downloadutil.core.pool;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.arialyy.downloadutil.core.Task;
|
||||
import com.arialyy.downloadutil.util.Task;
|
||||
import com.arialyy.downloadutil.core.inf.IPool;
|
||||
import com.arialyy.downloadutil.util.Util;
|
||||
|
||||
@ -70,10 +70,7 @@ public class ExecutePool implements IPool {
|
||||
private boolean putNewTask(Task newTask) {
|
||||
String url = newTask.getDownloadEntity().getDownloadUrl();
|
||||
boolean s = mExecuteQueue.offer(newTask);
|
||||
Log.w(TAG, "任务添加" + (s ?
|
||||
"成功" :
|
||||
"失败,【" + url + "】"
|
||||
));
|
||||
Log.w(TAG, "任务添加" + (s ? "成功" : "失败,【" + url + "】"));
|
||||
if (s) {
|
||||
newTask.start();
|
||||
mExecuteArray.put(Util.keyToHashKey(url), newTask);
|
||||
|
@ -38,14 +38,14 @@ public class DbEntity {
|
||||
* 删除当前数据
|
||||
*/
|
||||
public void deleteData() {
|
||||
mUtil.delData(this, new Object[]{"rowid"}, new Object[]{rowID});
|
||||
mUtil.delData(getClass(), new Object[]{"rowid"}, new Object[]{rowID});
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据条件删除数据
|
||||
*/
|
||||
public void deleteData(@NonNull Object[] wheres, @NonNull Object[] values) {
|
||||
mUtil.delData(this, wheres, values);
|
||||
mUtil.delData(getClass(), wheres, values);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -104,8 +104,9 @@ public class DbEntity {
|
||||
*
|
||||
* @return 没有数据返回null
|
||||
*/
|
||||
public <T extends DbEntity> List<T> findAllData(Class<T> clazz) {
|
||||
return mUtil.findAllData(clazz, this);
|
||||
public static <T extends DbEntity> List<T> findAllData(Class<T> clazz) {
|
||||
DbUtil util = DbUtil.getInstance();
|
||||
return util.findAllData(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -113,9 +114,10 @@ public class DbEntity {
|
||||
*
|
||||
* @return 没有数据返回null
|
||||
*/
|
||||
public <T extends DbEntity> List<T> findDatas(Class<T> clazz, @NonNull String[] wheres,
|
||||
@NonNull String[] values) {
|
||||
return mUtil.findData(clazz, this, wheres, values);
|
||||
public static <T extends DbEntity> List<T> findDatas(Class<T> clazz, @NonNull String[] wheres,
|
||||
@NonNull String[] values) {
|
||||
DbUtil util = DbUtil.getInstance();
|
||||
return util.findData(clazz, wheres, values);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -123,9 +125,10 @@ public class DbEntity {
|
||||
*
|
||||
* @return 没有数据返回null
|
||||
*/
|
||||
public <T extends DbEntity> T findData(Class<T> clazz, @NonNull String[] wheres,
|
||||
@NonNull String[] values) {
|
||||
List<T> datas = mUtil.findData(clazz, this, wheres, values);
|
||||
public static <T extends DbEntity> T findData(Class<T> clazz, @NonNull String[] wheres,
|
||||
@NonNull String[] values) {
|
||||
DbUtil util = DbUtil.getInstance();
|
||||
List<T> datas = util.findData(clazz, wheres, values);
|
||||
return datas == null ? null : datas.size() > 0 ? datas.get(0) : null;
|
||||
}
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ public class DbUtil {
|
||||
/**
|
||||
* 删除某条数据
|
||||
*/
|
||||
protected void delData(DbEntity dbEntity, @NonNull Object[] wheres, @NonNull Object[] values) {
|
||||
protected void delData(Class clazz, @NonNull Object[] wheres, @NonNull Object[] values) {
|
||||
mDb = mHelper.getWritableDatabase();
|
||||
if (wheres.length <= 0 || values.length <= 0) {
|
||||
Log.e(TAG, "输入删除条件");
|
||||
@ -72,7 +72,7 @@ public class DbUtil {
|
||||
return;
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("DELETE FROM ").append(Util.getClassName(dbEntity)).append(" WHERE ");
|
||||
sb.append("DELETE FROM ").append(Util.getClassName(clazz)).append(" WHERE ");
|
||||
int i = 0;
|
||||
for (Object where : wheres) {
|
||||
sb.append(where).append("=").append("'").append(values[i]).append("'");
|
||||
@ -104,9 +104,9 @@ public class DbUtil {
|
||||
sb.append(i > 0 ? ", " : "");
|
||||
try {
|
||||
sb.append(field.getName())
|
||||
.append(" = '")
|
||||
.append(field.get(dbEntity).toString())
|
||||
.append("'");
|
||||
.append(" = '")
|
||||
.append(field.get(dbEntity).toString())
|
||||
.append("'");
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -121,10 +121,10 @@ public class DbUtil {
|
||||
/**
|
||||
* 遍历所有数据
|
||||
*/
|
||||
protected <T extends DbEntity> List<T> findAllData(Class<T> clazz, DbEntity dbEntity) {
|
||||
protected <T extends DbEntity> List<T> findAllData(Class<T> clazz) {
|
||||
mDb = mHelper.getReadableDatabase();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("SELECT rowid, * FROM ").append(Util.getClassName(dbEntity));
|
||||
sb.append("SELECT rowid, * FROM ").append(Util.getClassName(clazz));
|
||||
print(FIND_ALL_DATA, sb.toString());
|
||||
Cursor cursor = mDb.rawQuery(sb.toString(), null);
|
||||
return cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null;
|
||||
@ -133,8 +133,7 @@ public class DbUtil {
|
||||
/**
|
||||
* 条件查寻数据
|
||||
*/
|
||||
protected <T extends DbEntity> List<T> findData(Class<T> clazz, DbEntity dbEntity,
|
||||
@NonNull String[] wheres,
|
||||
protected <T extends DbEntity> List<T> findData(Class<T> clazz, @NonNull String[] wheres,
|
||||
@NonNull String[] values) {
|
||||
mDb = mHelper.getReadableDatabase();
|
||||
if (wheres.length <= 0 || values.length <= 0) {
|
||||
@ -145,7 +144,7 @@ public class DbUtil {
|
||||
return null;
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("SELECT rowid, * FROM ").append(Util.getClassName(dbEntity)).append(" where ");
|
||||
sb.append("SELECT rowid, * FROM ").append(Util.getClassName(clazz)).append(" where ");
|
||||
int i = 0;
|
||||
for (Object where : wheres) {
|
||||
sb.append(where).append("=").append("'").append(values[i]).append("'");
|
||||
@ -161,10 +160,10 @@ public class DbUtil {
|
||||
* 插入数据
|
||||
*/
|
||||
protected void insertData(DbEntity dbEntity) {
|
||||
mDb = mHelper.getWritableDatabase();
|
||||
if (!tableExists(dbEntity)) {
|
||||
createTable(dbEntity);
|
||||
}
|
||||
mDb = mHelper.getWritableDatabase();
|
||||
Class<?> clazz = dbEntity.getClass();
|
||||
Field[] fields = Util.getFields(clazz);
|
||||
if (fields != null && fields.length > 0) {
|
||||
@ -209,6 +208,9 @@ public class DbUtil {
|
||||
* 查找某张表是否存在
|
||||
*/
|
||||
public synchronized boolean tableExists(DbEntity dbEntity) {
|
||||
if (mDb == null || !mDb.isOpen()) {
|
||||
mDb = mHelper.getReadableDatabase();
|
||||
}
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
@ -227,6 +229,7 @@ public class DbUtil {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (cursor != null) cursor.close();
|
||||
close();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@ -235,6 +238,9 @@ public class DbUtil {
|
||||
* 创建表
|
||||
*/
|
||||
private void createTable(DbEntity dbEntity) {
|
||||
if (mDb == null || !mDb.isOpen()) {
|
||||
mDb = mHelper.getWritableDatabase();
|
||||
}
|
||||
Field[] fields = Util.getFields(dbEntity.getClass());
|
||||
if (fields != null && fields.length > 0) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
@ -271,6 +277,7 @@ public class DbUtil {
|
||||
print(CREATE_TABLE, str);
|
||||
mDb.execSQL(str);
|
||||
}
|
||||
close();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -5,6 +5,8 @@ import android.support.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
import android.util.SparseArray;
|
||||
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@ -18,7 +20,7 @@ import java.util.Properties;
|
||||
* Created by lyy on 2015/8/25.
|
||||
* 下载工具类
|
||||
*/
|
||||
public class DownLoadUtil {
|
||||
final class DownLoadUtil {
|
||||
private static final String TAG = "DownLoadUtil";
|
||||
//下载监听
|
||||
private IDownloadListener mListener;
|
||||
@ -38,8 +40,13 @@ public class DownLoadUtil {
|
||||
boolean isNewTask = true;
|
||||
private int mCancelNum = 0;
|
||||
private int mStopNum = 0;
|
||||
private Context mContext;
|
||||
private DownloadEntity mDownloadEntity;
|
||||
|
||||
public DownLoadUtil() {
|
||||
|
||||
public DownLoadUtil(Context context, DownloadEntity entity) {
|
||||
mContext = context.getApplicationContext();
|
||||
mDownloadEntity = entity;
|
||||
}
|
||||
|
||||
public IDownloadListener getListener() {
|
||||
@ -71,27 +78,50 @@ public class DownLoadUtil {
|
||||
isStop = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除下载记录文件
|
||||
*/
|
||||
public void delConfigFile() {
|
||||
if (mContext != null && mDownloadEntity != null) {
|
||||
File dFile = new File(mDownloadEntity.getDownloadPath());
|
||||
File config = new File(
|
||||
mContext.getFilesDir().getPath() + "/temp/" + dFile.getName() + ".properties");
|
||||
if (config.exists()) {
|
||||
config.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除temp文件
|
||||
*/
|
||||
public void delTempFile() {
|
||||
if (mContext != null && mDownloadEntity != null) {
|
||||
File dFile = new File(mDownloadEntity.getDownloadPath());
|
||||
if (dFile.exists()) {
|
||||
dFile.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 多线程断点续传下载文件,暂停和继续
|
||||
*
|
||||
* @param context 必须添加该参数,不能使用全局变量的context
|
||||
* @param downloadUrl 下载路径
|
||||
* @param filePath 保存路径
|
||||
* @param downloadListener 下载进度监听 {@link DownloadListener}
|
||||
*/
|
||||
public void download(final Context context, @NonNull final String downloadUrl,
|
||||
@NonNull final String filePath,
|
||||
@NonNull final IDownloadListener downloadListener) {
|
||||
public void start(@NonNull final IDownloadListener downloadListener) {
|
||||
isDownloading = true;
|
||||
mCurrentLocation = 0;
|
||||
isStop = false;
|
||||
isCancel = false;
|
||||
mCancelNum = 0;
|
||||
mStopNum = 0;
|
||||
final File dFile = new File(filePath);
|
||||
final String filePath = mDownloadEntity.getDownloadPath();
|
||||
final String downloadUrl = mDownloadEntity.getDownloadUrl();
|
||||
final File dFile = new File(filePath);
|
||||
//读取已完成的线程数
|
||||
final File configFile = new File(
|
||||
context.getFilesDir().getPath() + "/temp/" + dFile.getName() + ".properties");
|
||||
mContext.getFilesDir().getPath() + "/temp/" + dFile.getName() + ".properties");
|
||||
try {
|
||||
if (!configFile.exists()) { //记录文件被删除,则重新下载
|
||||
isNewTask = true;
|
||||
@ -149,8 +179,8 @@ public class DownLoadUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
int blockSize = fileLength / THREAD_NUM;
|
||||
SparseArray<Thread> tasks = new SparseArray<>();
|
||||
int blockSize = fileLength / THREAD_NUM;
|
||||
int[] recordL = new int[THREAD_NUM];
|
||||
int rl = 0;
|
||||
for (int i = 0; i < THREAD_NUM; i++) {
|
||||
@ -194,11 +224,12 @@ public class DownLoadUtil {
|
||||
rl++;
|
||||
}
|
||||
if (i == (THREAD_NUM - 1)) {
|
||||
endL = fileLength;//如果整个文件的大小不为线程个数的整数倍,则最后一个线程的结束位置即为文件的总长度
|
||||
//如果整个文件的大小不为线程个数的整数倍,则最后一个线程的结束位置即为文件的总长度
|
||||
endL = fileLength;
|
||||
}
|
||||
DownloadEntity entity = new DownloadEntity(context, fileLength,
|
||||
downloadUrl, dFile, i,
|
||||
startL, endL);
|
||||
ConfigEntity entity = new ConfigEntity(mContext, fileLength,
|
||||
downloadUrl, dFile, i, startL,
|
||||
endL);
|
||||
DownLoadTask task = new DownLoadTask(entity);
|
||||
tasks.put(i, new Thread(task));
|
||||
}
|
||||
@ -238,13 +269,13 @@ public class DownLoadUtil {
|
||||
*/
|
||||
private class DownLoadTask implements Runnable {
|
||||
private static final String TAG = "DownLoadTask";
|
||||
private DownloadEntity dEntity;
|
||||
private String configFPath;
|
||||
private ConfigEntity dEntity;
|
||||
private String configFPath;
|
||||
|
||||
public DownLoadTask(DownloadEntity downloadInfo) {
|
||||
public DownLoadTask(ConfigEntity downloadInfo) {
|
||||
this.dEntity = downloadInfo;
|
||||
configFPath = dEntity.context.getFilesDir()
|
||||
.getPath() + "/temp/" + dEntity.tempFile.getName() + ".properties";
|
||||
.getPath() + "/temp/" + dEntity.tempFile.getName() + ".properties";
|
||||
}
|
||||
|
||||
@Override public void run() {
|
||||
@ -392,7 +423,7 @@ public class DownLoadUtil {
|
||||
/**
|
||||
* 子线程下载信息类
|
||||
*/
|
||||
private class DownloadEntity {
|
||||
private class ConfigEntity {
|
||||
//文件大小
|
||||
long fileSize;
|
||||
String downloadUrl;
|
||||
@ -402,8 +433,8 @@ public class DownLoadUtil {
|
||||
File tempFile;
|
||||
Context context;
|
||||
|
||||
public DownloadEntity(Context context, long fileSize, String downloadUrl, File file,
|
||||
int threadId, long startLocation, long endLocation) {
|
||||
public ConfigEntity(Context context, long fileSize, String downloadUrl, File file,
|
||||
int threadId, long startLocation, long endLocation) {
|
||||
this.fileSize = fileSize;
|
||||
this.downloadUrl = downloadUrl;
|
||||
this.tempFile = file;
|
||||
|
@ -7,7 +7,7 @@
|
||||
//import android.database.sqlite.SQLiteOpenHelper;
|
||||
//import android.support.annotation.NonNull;
|
||||
//
|
||||
//import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
//import com.arialyy.downloadutil.entity.ConfigEntity;
|
||||
//
|
||||
//import java.util.ArrayList;
|
||||
//import java.util.List;
|
||||
@ -50,8 +50,8 @@
|
||||
// /**
|
||||
// * 获取所有下载实体
|
||||
// */
|
||||
// public List<DownloadEntity> getAllEntity(@NonNull SQLiteDatabase db) {
|
||||
// List<DownloadEntity> list = new ArrayList<>();
|
||||
// public List<ConfigEntity> getAllEntity(@NonNull SQLiteDatabase db) {
|
||||
// List<ConfigEntity> list = new ArrayList<>();
|
||||
// Cursor c = db.query(TABLE_NAME, null, null, null, null, null, null);
|
||||
// if (c.moveToFirst()) {
|
||||
// while (c.moveToNext()) {
|
||||
@ -65,7 +65,7 @@
|
||||
// /**
|
||||
// * 更新下载实体
|
||||
// */
|
||||
// public void updateEntity(@NonNull SQLiteDatabase db, DownloadEntity entity) {
|
||||
// public void updateEntity(@NonNull SQLiteDatabase db, ConfigEntity entity) {
|
||||
// String whereClause = "url=?";
|
||||
// String[] whereArgs = {entity.getDownloadUrl()};
|
||||
// db.update(TABLE_NAME, createCv(entity), whereClause, whereArgs);
|
||||
@ -74,7 +74,7 @@
|
||||
// /**
|
||||
// * 删除下载实体
|
||||
// */
|
||||
// public void delEntity(@NonNull SQLiteDatabase db, DownloadEntity entity) {
|
||||
// public void delEntity(@NonNull SQLiteDatabase db, ConfigEntity entity) {
|
||||
// delEntity(db, entity.getDownloadUrl());
|
||||
// }
|
||||
//
|
||||
@ -93,8 +93,8 @@
|
||||
// * @param downloadUrl
|
||||
// * @return
|
||||
// */
|
||||
// public DownloadEntity findEntity(@NonNull SQLiteDatabase db, @NonNull String downloadUrl) {
|
||||
// DownloadEntity entity;
|
||||
// public ConfigEntity findEntity(@NonNull SQLiteDatabase db, @NonNull String downloadUrl) {
|
||||
// ConfigEntity entity;
|
||||
// String sql = "select * from " + TABLE_NAME + "where url=?";
|
||||
// Cursor c = db.rawQuery(sql, new String[]{downloadUrl});
|
||||
// if (c.getCount() <= 0) {
|
||||
@ -113,8 +113,8 @@
|
||||
// *
|
||||
// * @param entity
|
||||
// */
|
||||
// public void savaEntity(@NonNull SQLiteDatabase db, @NonNull DownloadEntity entity) {
|
||||
// DownloadEntity temp = findEntity(db, entity.getDownloadUrl());
|
||||
// public void savaEntity(@NonNull SQLiteDatabase db, @NonNull ConfigEntity entity) {
|
||||
// ConfigEntity temp = findEntity(db, entity.getDownloadUrl());
|
||||
// if (temp == null) {
|
||||
// db.insert(TABLE_NAME, null, createCv(entity));
|
||||
// } else {
|
||||
@ -128,9 +128,9 @@
|
||||
// * @param c
|
||||
// * @return
|
||||
// */
|
||||
// private DownloadEntity cursor2Entity(Cursor c) {
|
||||
// DownloadEntity entity;
|
||||
// entity = new DownloadEntity();
|
||||
// private ConfigEntity cursor2Entity(Cursor c) {
|
||||
// ConfigEntity entity;
|
||||
// entity = new ConfigEntity();
|
||||
// entity.setDownloadUrl(c.getString(c.getColumnIndex("url")));
|
||||
// entity.setDownloadPath(c.getString(c.getColumnIndex("path")));
|
||||
// entity.setCompleteTime(c.getLong(c.getColumnIndex("completeTime")));
|
||||
@ -148,7 +148,7 @@
|
||||
// * @param entity
|
||||
// * @return
|
||||
// */
|
||||
// private ContentValues createCv(@NonNull DownloadEntity entity) {
|
||||
// private ContentValues createCv(@NonNull ConfigEntity entity) {
|
||||
// ContentValues cv = new ContentValues();
|
||||
// cv.put("url", entity.getDownloadUrl());
|
||||
// cv.put("path", entity.getDownloadPath());
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.arialyy.downloadutil.core;
|
||||
package com.arialyy.downloadutil.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
@ -6,9 +6,9 @@ import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
|
||||
import com.arialyy.downloadutil.core.DownloadManager;
|
||||
import com.arialyy.downloadutil.core.IDownloadTarget;
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
import com.arialyy.downloadutil.util.DownLoadUtil;
|
||||
import com.arialyy.downloadutil.util.IDownloadListener;
|
||||
|
||||
import java.net.HttpURLConnection;
|
||||
|
||||
@ -19,41 +19,42 @@ import java.net.HttpURLConnection;
|
||||
public class Task {
|
||||
public static final String TAG = "Task";
|
||||
|
||||
private DownloadEntity downloadEntity;
|
||||
private IDownloadListener listener;
|
||||
private Handler outHandler;
|
||||
private Context context;
|
||||
private DownLoadUtil util;
|
||||
private DownloadEntity mEntity;
|
||||
private IDownloadListener mListener;
|
||||
private Handler mOutHandler;
|
||||
private Context mContext;
|
||||
private DownLoadUtil mUtil;
|
||||
|
||||
private Task() {
|
||||
util = new DownLoadUtil();
|
||||
private Task(Context context, DownloadEntity entity) {
|
||||
mContext = context.getApplicationContext();
|
||||
mEntity = entity;
|
||||
mUtil = new DownLoadUtil(context, entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始下载
|
||||
*/
|
||||
public void start() {
|
||||
if (util.isDownloading()) {
|
||||
if (mUtil.isDownloading()) {
|
||||
Log.d(TAG, "任务正在下载");
|
||||
} else {
|
||||
if (listener == null) {
|
||||
listener = new DownloadListener(context, downloadEntity, outHandler);
|
||||
if (mListener == null) {
|
||||
mListener = new DownloadListener(mContext, mEntity, mOutHandler);
|
||||
}
|
||||
util.download(context, downloadEntity.getDownloadUrl(),
|
||||
downloadEntity.getDownloadPath(), listener);
|
||||
mUtil.start(mListener);
|
||||
}
|
||||
}
|
||||
|
||||
public DownloadEntity getDownloadEntity() {
|
||||
return downloadEntity;
|
||||
return mEntity;
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止下载
|
||||
*/
|
||||
public void stop() {
|
||||
if (util.isDownloading()) {
|
||||
util.stopDownload();
|
||||
if (mUtil.isDownloading()) {
|
||||
mUtil.stopDownload();
|
||||
}
|
||||
}
|
||||
|
||||
@ -61,14 +62,38 @@ public class Task {
|
||||
* 获取下载工具
|
||||
*/
|
||||
public DownLoadUtil getDownloadUtil() {
|
||||
return util;
|
||||
return mUtil;
|
||||
}
|
||||
|
||||
/**
|
||||
* 任务下载状态
|
||||
*/
|
||||
public boolean isDownloading() {
|
||||
return mUtil.isDownloading();
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消下载
|
||||
*/
|
||||
public void cancel() {
|
||||
util.cancelDownload();
|
||||
if (mUtil.isDownloading()) {
|
||||
mUtil.cancelDownload();
|
||||
} else {
|
||||
// 如果任务不是下载状态
|
||||
mUtil.cancelDownload();
|
||||
mUtil.delConfigFile();
|
||||
mUtil.delTempFile();
|
||||
mEntity.deleteData();
|
||||
|
||||
//发送取消下载的广播
|
||||
Uri.Builder builder = new Uri.Builder();
|
||||
builder.scheme(mContext.getPackageName());
|
||||
Uri uri = builder.build();
|
||||
Intent intent = new Intent(DownloadManager.ACTION_CANCEL);
|
||||
intent.setData(uri);
|
||||
intent.putExtra(DownloadManager.ACTION_CANCEL, mEntity);
|
||||
mContext.sendBroadcast(intent);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -83,7 +108,7 @@ public class Task {
|
||||
DownloadEntity downloadEntity;
|
||||
|
||||
public DownloadListener(Context context, DownloadEntity downloadEntity,
|
||||
Handler outHandler) {
|
||||
Handler outHandler) {
|
||||
this.context = context;
|
||||
this.outHandler = outHandler;
|
||||
this.downloadEntity = downloadEntity;
|
||||
@ -167,7 +192,8 @@ public class Task {
|
||||
}
|
||||
|
||||
private void sendIntent(String action, long location) {
|
||||
downloadEntity.save();
|
||||
downloadEntity.setCurrentProgress(location);
|
||||
downloadEntity.update();
|
||||
Uri.Builder builder = new Uri.Builder();
|
||||
builder.scheme(context.getPackageName());
|
||||
Uri uri = builder.build();
|
||||
@ -202,12 +228,11 @@ public class Task {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Task builder() {
|
||||
Task task = new Task();
|
||||
task.context = context;
|
||||
task.downloadEntity = downloadEntity;
|
||||
task.listener = listener;
|
||||
task.outHandler = outHandler;
|
||||
public Task build() {
|
||||
Task task = new Task(context, downloadEntity);
|
||||
task.mListener = listener;
|
||||
task.mOutHandler = outHandler;
|
||||
downloadEntity.save();
|
||||
return task;
|
||||
}
|
||||
}
|
@ -103,6 +103,17 @@ public class Util {
|
||||
return arrays[arrays.length - 1];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取对象名
|
||||
*
|
||||
* @param clazz clazz
|
||||
* @return 对象名
|
||||
*/
|
||||
public static String getClassName(Class clazz) {
|
||||
String arrays[] = clazz.getName().split("\\.");
|
||||
return arrays[arrays.length - 1];
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化文件大小
|
||||
*
|
||||
|
Reference in New Issue
Block a user