pull/2/head
lyy 9 years ago
parent a52338c5d3
commit b43fa88aaf
  1. 6
      app/src/main/java/com/arialyy/simple/activity/MultiTaskActivity.java
  2. 49
      app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java
  3. 37
      app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java
  4. 13
      app/src/main/java/com/arialyy/simple/module/DownloadModule.java
  5. 21
      downloadutil/src/main/java/com/arialyy/downloadutil/core/DownLoadUtil.java
  6. 16
      downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadEntity.java
  7. 24
      downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadManager.java
  8. 1
      downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadTarget.java
  9. 9
      downloadutil/src/main/java/com/arialyy/downloadutil/core/IDownloadListener.java
  10. 4
      downloadutil/src/main/java/com/arialyy/downloadutil/core/IDownloadTarget.java
  11. 14
      downloadutil/src/main/java/com/arialyy/downloadutil/core/Task.java
  12. 4
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/AddCmd.java
  13. 4
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/CancelCmd.java
  14. 54
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/CmdFactory.java
  15. 4
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/IDownloadCmd.java
  16. 4
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/StartCmd.java
  17. 4
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/StateCmd.java
  18. 4
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/StopCmd.java
  19. 4
      downloadutil/src/main/java/com/arialyy/downloadutil/core/pool/ExecutePool.java
  20. 18
      downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbEntity.java
  21. 4
      downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbUtil.java

@ -46,8 +46,12 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
DownloadEntity entity = intent.getParcelableExtra(DownloadManager.ENTITY); DownloadEntity entity = intent.getParcelableExtra(DownloadManager.ENTITY);
switch (action) { switch (action) {
case DownloadManager.ACTION_PRE: case DownloadManager.ACTION_PRE:
len = entity.getFileSize();
L.d(TAG, "download pre"); L.d(TAG, "download pre");
mAdapter.updateState(entity);
break;
case DownloadManager.ACTION_POST_PRE:
len = entity.getFileSize();
L.d(TAG, "download post pre");
break; break;
case DownloadManager.ACTION_START: case DownloadManager.ACTION_START:
L.d(TAG, "download start"); L.d(TAG, "download start");

@ -14,10 +14,10 @@ import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import butterknife.Bind; import butterknife.Bind;
import com.arialyy.downloadutil.core.DownloadManager;
import com.arialyy.downloadutil.core.command.CommandFactory;
import com.arialyy.downloadutil.core.command.IDownloadCommand;
import com.arialyy.downloadutil.core.DownloadEntity; import com.arialyy.downloadutil.core.DownloadEntity;
import com.arialyy.downloadutil.core.DownloadManager;
import com.arialyy.downloadutil.core.command.CmdFactory;
import com.arialyy.downloadutil.core.command.IDownloadCmd;
import com.arialyy.downloadutil.orm.DbEntity; import com.arialyy.downloadutil.orm.DbEntity;
import com.arialyy.downloadutil.util.Util; import com.arialyy.downloadutil.util.Util;
import com.arialyy.frame.util.show.L; import com.arialyy.frame.util.show.L;
@ -41,7 +41,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
private Button mStart, mStop, mCancel; private Button mStart, mStop, mCancel;
private TextView mSize; private TextView mSize;
@Bind(R.id.toolbar) Toolbar toolbar; @Bind(R.id.toolbar) Toolbar toolbar;
private CommandFactory mFactory; private CmdFactory mFactory;
private DownloadManager mManager; private DownloadManager mManager;
private DownloadEntity mEntity; private DownloadEntity mEntity;
@ -97,7 +97,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
@Override public void onReceive(Context context, Intent intent) { @Override public void onReceive(Context context, Intent intent) {
String action = intent.getAction(); String action = intent.getAction();
switch (action) { switch (action) {
case DownloadManager.ACTION_PRE: case DownloadManager.ACTION_POST_PRE:
DownloadEntity entity = intent.getParcelableExtra(DownloadManager.ENTITY); DownloadEntity entity = intent.getParcelableExtra(DownloadManager.ENTITY);
len = entity.getFileSize(); len = entity.getFileSize();
L.d(TAG, "download pre"); L.d(TAG, "download pre");
@ -168,7 +168,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
mStop = (Button) findViewById(R.id.stop); mStop = (Button) findViewById(R.id.stop);
mCancel = (Button) findViewById(R.id.cancel); mCancel = (Button) findViewById(R.id.cancel);
mSize = (TextView) findViewById(R.id.size); mSize = (TextView) findViewById(R.id.size);
mFactory = CommandFactory.getInstance(); mFactory = CmdFactory.getInstance();
mManager = DownloadManager.getInstance(); mManager = DownloadManager.getInstance();
mEntity = DbEntity.findData(DownloadEntity.class, new String[] { "downloadUrl" }, mEntity = DbEntity.findData(DownloadEntity.class, new String[] { "downloadUrl" },
new String[] { mDownloadUrl }); new String[] { mDownloadUrl });
@ -190,21 +190,6 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
switch (view.getId()) { switch (view.getId()) {
case R.id.start: case R.id.start:
start(); start();
// if (PermissionManager.getInstance()
// .checkPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// start();
// } else {
// PermissionManager.getInstance()
// .requestPermission(this, new OnPermissionCallback() {
// @Override public void onSuccess(String... permissions) {
// start();
// }
//
// @Override public void onFail(String... permissions) {
//
// }
// }, Manifest.permission.WRITE_EXTERNAL_STORAGE);
// }
break; break;
case R.id.stop: case R.id.stop:
stop(); stop();
@ -219,23 +204,21 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
mEntity.setFileName("test.apk"); mEntity.setFileName("test.apk");
mEntity.setDownloadUrl(mDownloadUrl); mEntity.setDownloadUrl(mDownloadUrl);
mEntity.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk"); mEntity.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk");
List<IDownloadCommand> commands = new ArrayList<>(); List<IDownloadCmd> commands = new ArrayList<>();
IDownloadCommand addCommand = mFactory.createCommand(this, mEntity, CommandFactory.TASK_CREATE); IDownloadCmd addCMD = mFactory.createCmd(this, mEntity, CmdFactory.TASK_CREATE);
IDownloadCommand startCommand = IDownloadCmd startCmd = mFactory.createCmd(this, mEntity, CmdFactory.TASK_START);
mFactory.createCommand(this, mEntity, CommandFactory.TASK_START); commands.add(addCMD);
commands.add(addCommand); commands.add(startCmd);
commands.add(startCommand); mManager.setCmds(commands).exe();
mManager.setCommands(commands).exe();
} }
private void stop() { private void stop() {
IDownloadCommand stopCommand = mFactory.createCommand(this, mEntity, CommandFactory.TASK_STOP); IDownloadCmd stopCmd = mFactory.createCmd(this, mEntity, CmdFactory.TASK_STOP);
mManager.setCommand(stopCommand).exe(); mManager.setCmd(stopCmd).exe();
} }
private void cancel() { private void cancel() {
IDownloadCommand cancelCommand = IDownloadCmd cancelCmd = mFactory.createCmd(this, mEntity, CmdFactory.TASK_CANCEL);
mFactory.createCommand(this, mEntity, CommandFactory.TASK_CANCEL); mManager.setCmd(cancelCmd).exe();
mManager.setCommand(cancelCommand).exe();
} }
} }

@ -10,8 +10,8 @@ import com.arialyy.absadapter.common.AbsHolder;
import com.arialyy.absadapter.recycler_view.AbsRVAdapter; import com.arialyy.absadapter.recycler_view.AbsRVAdapter;
import com.arialyy.downloadutil.core.DownloadEntity; import com.arialyy.downloadutil.core.DownloadEntity;
import com.arialyy.downloadutil.core.DownloadManager; import com.arialyy.downloadutil.core.DownloadManager;
import com.arialyy.downloadutil.core.command.CommandFactory; import com.arialyy.downloadutil.core.command.CmdFactory;
import com.arialyy.downloadutil.core.command.IDownloadCommand; import com.arialyy.downloadutil.core.command.IDownloadCmd;
import com.arialyy.downloadutil.util.Util; import com.arialyy.downloadutil.util.Util;
import com.arialyy.simple.R; import com.arialyy.simple.R;
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
@ -28,18 +28,21 @@ import java.util.Set;
public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapter.MyHolder> { public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapter.MyHolder> {
private static final String TAG = "DownloadAdapter"; private static final String TAG = "DownloadAdapter";
private DownloadManager mManager; private DownloadManager mManager;
private CommandFactory mFactory; private CmdFactory mFactory;
private Map<String, Integer> mPositions = new HashMap<>(); private Map<String, Integer> mPositions = new HashMap<>();
public DownloadAdapter(Context context, List<DownloadEntity> data) { public DownloadAdapter(Context context, List<DownloadEntity> data) {
super(context, data); super(context, data);
int i = 0; mFactory = CmdFactory.getInstance();
mManager = DownloadManager.getInstance();
List<IDownloadCmd> addCmd = new ArrayList<>();
int i = 0;
for (DownloadEntity entity : data) { for (DownloadEntity entity : data) {
mPositions.put(entity.getDownloadUrl(), i); mPositions.put(entity.getDownloadUrl(), i);
addCmd.add(mFactory.createCmd(context, entity, CmdFactory.TASK_CREATE));
i++; i++;
} }
mFactory = CommandFactory.getInstance(); mManager.setCmds(addCmd).exe();
mManager = DownloadManager.getInstance();
} }
@Override protected MyHolder getViewHolder(View convertView, int viewType) { @Override protected MyHolder getViewHolder(View convertView, int viewType) {
@ -105,6 +108,8 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
str = "恢复"; str = "恢复";
color = android.R.color.holo_blue_light; color = android.R.color.holo_blue_light;
break; break;
case DownloadEntity.STATE_PRE:
case DownloadEntity.STATE_POST_PRE:
case DownloadEntity.STATE_DOWNLOAD_ING: case DownloadEntity.STATE_DOWNLOAD_ING:
str = "暂停"; str = "暂停";
color = android.R.color.holo_red_light; color = android.R.color.holo_red_light;
@ -122,9 +127,8 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
@Override public void onClick(View v) { @Override public void onClick(View v) {
mData.remove(item); mData.remove(item);
notifyDataSetChanged(); notifyDataSetChanged();
IDownloadCommand cancelCommand = IDownloadCmd cancelCmd = mFactory.createCmd(getContext(), item, CmdFactory.TASK_CANCEL);
mFactory.createCommand(getContext(), item, CommandFactory.TASK_CANCEL); mManager.setCmd(cancelCmd).exe();
mManager.setCommand(cancelCommand).exe();
} }
}); });
} }
@ -163,20 +167,13 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
} }
private void start(DownloadEntity entity) { private void start(DownloadEntity entity) {
List<IDownloadCommand> commands = new ArrayList<>(); IDownloadCmd startCmd = mFactory.createCmd(getContext(), entity, CmdFactory.TASK_START);
IDownloadCommand addCommand = mManager.setCmd(startCmd).exe();
mFactory.createCommand(getContext(), entity, CommandFactory.TASK_CREATE);
IDownloadCommand startCommand =
mFactory.createCommand(getContext(), entity, CommandFactory.TASK_START);
commands.add(addCommand);
commands.add(startCommand);
mManager.setCommands(commands).exe();
} }
private void stop(DownloadEntity entity) { private void stop(DownloadEntity entity) {
IDownloadCommand stopCommand = IDownloadCmd stopCmd = mFactory.createCmd(getContext(), entity, CmdFactory.TASK_STOP);
mFactory.createCommand(getContext(), entity, CommandFactory.TASK_STOP); mManager.setCmd(stopCmd).exe();
mManager.setCommand(stopCommand).exe();
} }
} }

@ -3,8 +3,8 @@ package com.arialyy.simple.module;
import android.content.Context; import android.content.Context;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.os.Environment; import android.os.Environment;
import com.arialyy.downloadutil.core.DownloadManager;
import com.arialyy.downloadutil.core.DownloadEntity; import com.arialyy.downloadutil.core.DownloadEntity;
import com.arialyy.downloadutil.core.DownloadManager;
import com.arialyy.downloadutil.util.Util; import com.arialyy.downloadutil.util.Util;
import com.arialyy.frame.util.AndroidUtils; import com.arialyy.frame.util.AndroidUtils;
import com.arialyy.frame.util.StringUtil; import com.arialyy.frame.util.StringUtil;
@ -26,7 +26,7 @@ public class DownloadModule extends BaseModule {
* 设置下载数据 * 设置下载数据
*/ */
public List<DownloadEntity> getDownloadData() { public List<DownloadEntity> getDownloadData() {
List<DownloadEntity> list = DownloadEntity.findAllData(DownloadEntity.class); List<DownloadEntity> list = DownloadEntity.findAllData(DownloadEntity.class);
if (list == null || list.size() == 0) { if (list == null || list.size() == 0) {
list = createNewDownload(); list = createNewDownload();
} }
@ -63,16 +63,18 @@ public class DownloadModule extends BaseModule {
*/ */
private List<DownloadEntity> createNewDownload() { private List<DownloadEntity> createNewDownload() {
List<DownloadEntity> list = new ArrayList<>(); List<DownloadEntity> list = new ArrayList<>();
String[] urls = String[] urls = getContext().getResources().getStringArray(R.array.test_apk_download_url);
getContext().getResources().getStringArray(R.array.test_apk_download_url); int i = 0;
for (String url : urls) { for (String url : urls) {
String fileName = Util.keyToHashCode(url) + ".apk"; String fileName = Util.keyToHashCode(url) + ".apk";
DownloadEntity entity = new DownloadEntity(); DownloadEntity entity = new DownloadEntity();
entity.setDownloadUrl(url); entity.setDownloadUrl(url);
entity.setDownloadPath(getDownloadPath(url)); entity.setDownloadPath(getDownloadPath(url));
entity.setFileName(fileName); //entity.setFileName(fileName);
entity.setFileName("taskName_________" + i);
entity.save(); entity.save();
list.add(entity); list.add(entity);
i++;
} }
return list; return list;
} }
@ -84,6 +86,7 @@ public class DownloadModule extends BaseModule {
IntentFilter filter = new IntentFilter(); IntentFilter filter = new IntentFilter();
filter.addDataScheme(getContext().getPackageName()); filter.addDataScheme(getContext().getPackageName());
filter.addAction(DownloadManager.ACTION_PRE); filter.addAction(DownloadManager.ACTION_PRE);
filter.addAction(DownloadManager.ACTION_POST_PRE);
filter.addAction(DownloadManager.ACTION_RESUME); filter.addAction(DownloadManager.ACTION_RESUME);
filter.addAction(DownloadManager.ACTION_START); filter.addAction(DownloadManager.ACTION_START);
filter.addAction(DownloadManager.ACTION_RUNNING); filter.addAction(DownloadManager.ACTION_RUNNING);

@ -1,7 +1,6 @@
package com.arialyy.downloadutil.core; package com.arialyy.downloadutil.core;
import android.content.Context; import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log; import android.util.Log;
import android.util.SparseArray; import android.util.SparseArray;
import com.arialyy.downloadutil.util.Util; import com.arialyy.downloadutil.util.Util;
@ -128,7 +127,8 @@ final class DownLoadUtil {
* *
* @param downloadListener 下载进度监听 {@link DownloadListener} * @param downloadListener 下载进度监听 {@link DownloadListener}
*/ */
public void start(@NonNull final IDownloadListener downloadListener) { public void start(IDownloadListener downloadListener) {
mListener = downloadListener;
isDownloading = true; isDownloading = true;
mCurrentLocation = 0; mCurrentLocation = 0;
isStop = false; isStop = false;
@ -153,13 +153,13 @@ final class DownLoadUtil {
failDownload("下载失败,记录文件被删除"); failDownload("下载失败,记录文件被删除");
return; return;
} }
mListener.onPre();
new Thread(new Runnable() { new Thread(new Runnable() {
@Override public void run() { @Override public void run() {
try { try {
mListener = downloadListener;
URL url = new URL(downloadUrl); URL url = new URL(downloadUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); HttpURLConnection conn = (HttpURLConnection) url.openConnection();
setCommadParam(conn); setConnectParam(conn);
conn.setConnectTimeout(TIME_OUT * 4); conn.setConnectTimeout(TIME_OUT * 4);
conn.connect(); conn.connect();
int len = conn.getContentLength(); int len = conn.getContentLength();
@ -169,14 +169,13 @@ final class DownLoadUtil {
} }
int code = conn.getResponseCode(); int code = conn.getResponseCode();
if (code == 200) { if (code == 200) {
int fileLength = conn.getContentLength(); int fileLength = conn.getContentLength();
//必须建一个文件 //必须建一个文件
Util.createFile(filePath); Util.createFile(filePath);
RandomAccessFile file = new RandomAccessFile(filePath, "rwd"); RandomAccessFile file = new RandomAccessFile(filePath, "rwd");
//设置文件长度 //设置文件长度
file.setLength(fileLength); file.setLength(fileLength);
mListener.onPreDownload(conn.getContentLength()); mListener.onPostPre(fileLength);
//分配每条线程的下载区间 //分配每条线程的下载区间
Properties pro = null; Properties pro = null;
pro = Util.loadConfig(configFile); pro = Util.loadConfig(configFile);
@ -281,7 +280,7 @@ final class DownLoadUtil {
mListener.onFail(); mListener.onFail();
} }
private void setCommadParam(HttpURLConnection conn) throws ProtocolException { private void setConnectParam(HttpURLConnection conn) throws ProtocolException {
conn.setRequestMethod("GET"); conn.setRequestMethod("GET");
conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("User-Agent", conn.setRequestProperty("User-Agent",
@ -323,7 +322,7 @@ final class DownLoadUtil {
//在头里面请求下载开始位置和结束位置 //在头里面请求下载开始位置和结束位置
conn.setRequestProperty("Range", conn.setRequestProperty("Range",
"bytes=" + dEntity.startLocation + "-" + dEntity.endLocation); "bytes=" + dEntity.startLocation + "-" + dEntity.endLocation);
setCommadParam(conn); setConnectParam(conn);
conn.setConnectTimeout(TIME_OUT * 4); conn.setConnectTimeout(TIME_OUT * 4);
conn.setReadTimeout(TIME_OUT * 24); //设置读取流的等待时间,必须设置该参数 conn.setReadTimeout(TIME_OUT * 24); //设置读取流的等待时间,必须设置该参数
is = conn.getInputStream(); 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. * 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_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; //下载速度 @Ignore private long speed = 0; //下载速度
private String downloadUrl = ""; //下载路径 private String downloadUrl = ""; //下载路径
@ -175,6 +185,7 @@ public class DownloadEntity extends DbEntity implements Parcelable, Cloneable {
dest.writeLong(this.currentProgress); dest.writeLong(this.currentProgress);
dest.writeInt(this.failNum); dest.writeInt(this.failNum);
dest.writeLong(this.speed); dest.writeLong(this.speed);
dest.writeInt(this.rowID);
} }
protected DownloadEntity(Parcel in) { protected DownloadEntity(Parcel in) {
@ -189,6 +200,7 @@ public class DownloadEntity extends DbEntity implements Parcelable, Cloneable {
this.currentProgress = in.readLong(); this.currentProgress = in.readLong();
this.failNum = in.readInt(); this.failNum = in.readInt();
this.speed = in.readLong(); this.speed = in.readLong();
this.rowID = in.readInt();
} }
@Ignore public static final Creator<DownloadEntity> CREATOR = new Creator<DownloadEntity>() { @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.app.Application;
import android.content.Context; import android.content.Context;
import android.util.Log; 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.DbEntity;
import com.arialyy.downloadutil.orm.DbUtil; import com.arialyy.downloadutil.orm.DbUtil;
import java.util.ArrayList; import java.util.ArrayList;
@ -14,13 +14,19 @@ import java.util.List;
* 下载管理器通过命令的方式控制下载 * 下载管理器通过命令的方式控制下载
*/ */
public class DownloadManager { public class DownloadManager {
private static final String TAG = "DownloadManager"; private static final String TAG = "DownloadManager";
private static final Object LOCK = new Object(); private static final Object LOCK = new Object();
private static volatile DownloadManager INSTANCE = null; 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"; public static final String CURRENT_SPEED = "CURRENT_SPEED";
private List<IDownloadCommand> mCommands = new ArrayList<>(); private List<IDownloadCmd> mCommands = new ArrayList<>();
private DownloadManager() { private DownloadManager() {
@ -113,7 +119,7 @@ public class DownloadManager {
/** /**
* 设置命令 * 设置命令
*/ */
public DownloadManager setCommand(IDownloadCommand command) { public DownloadManager setCmd(IDownloadCmd command) {
mCommands.add(command); mCommands.add(command);
return this; 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) { if (commands != null && commands.size() > 0) {
mCommands.addAll(commands); mCommands.addAll(commands);
} }
@ -132,7 +138,7 @@ public class DownloadManager {
* 执行所有设置的命令 * 执行所有设置的命令
*/ */
public synchronized void exe() { public synchronized void exe() {
for (IDownloadCommand command : mCommands) { for (IDownloadCmd command : mCommands) {
command.executeComment(); command.executeComment();
} }
mCommands.clear(); mCommands.clear();

@ -40,6 +40,7 @@ public class DownloadTarget extends IDownloadTarget {
@Override public void startTask(Task task) { @Override public void startTask(Task task) {
if (mExecutePool.putTask(task)) { if (mExecutePool.putTask(task)) {
mCachePool.removeTask(task);
task.start(); task.start();
} }
} }

@ -15,9 +15,14 @@ public interface IDownloadListener {
public void onFail(); 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) { switch (msg.what) {
case STOP: case STOP:
case CANCEL: case CANCEL:
if (target.mExecutePool.size() != ExecutePool.SIZE) {
startNextTask(entity);
}
break;
case COMPLETE: case COMPLETE:
startNextTask(entity); startNextTask(entity);
break; break;

@ -142,13 +142,19 @@ public class Task {
sendIntent.putExtra(DownloadManager.ENTITY, downloadEntity); sendIntent.putExtra(DownloadManager.ENTITY, downloadEntity);
} }
@Override public void onPreDownload(long fileSize) { @Override public void onPre() {
super.onPreDownload(fileSize); super.onPre();
downloadEntity.setFileSize(fileSize); downloadEntity.setState(DownloadEntity.STATE_PRE);
downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING);
sendIntent(DownloadManager.ACTION_PRE, -1); 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) { @Override public void onResume(long resumeLocation) {
super.onResume(resumeLocation); super.onResume(resumeLocation);
downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING); downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING);

@ -9,9 +9,9 @@ import com.arialyy.downloadutil.core.Task;
* Created by lyy on 2016/8/22. * 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); super(context, entity);
} }

@ -8,9 +8,9 @@ import com.arialyy.downloadutil.core.Task;
* Created by lyy on 2016/9/20. * 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); super(context, entity);
} }

@ -7,7 +7,7 @@ import com.arialyy.downloadutil.core.DownloadEntity;
* Created by Lyy on 2016/9/23. * 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; public static final int TASK_STATE = 0x126;
private static final Object LOCK = new Object(); private static final Object LOCK = new Object();
private static volatile CommandFactory INSTANCE = null; private static volatile CmdFactory INSTANCE = null;
private CommandFactory() { private CmdFactory() {
} }
public static CommandFactory getInstance() { public static CmdFactory getInstance() {
if (INSTANCE == null) { if (INSTANCE == null) {
synchronized (LOCK) { synchronized (LOCK) {
INSTANCE = new CommandFactory(); INSTANCE = new CmdFactory();
} }
} }
return INSTANCE; return INSTANCE;
@ -55,19 +55,19 @@ public class CommandFactory {
* @param type 命令类型{@link #TASK_CREATE}{@link #TASK_START}{@link #TASK_CANCEL}{@link * @param type 命令类型{@link #TASK_CREATE}{@link #TASK_START}{@link #TASK_CANCEL}{@link
* #TASK_STOP}{@link #TASK_STATE} * #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) { switch (type) {
case TASK_CREATE: case TASK_CREATE:
return createAddCommand(context, entity); return createAddCmd(context, entity);
case TASK_RESUME: case TASK_RESUME:
case TASK_START: case TASK_START:
return createStartCommand(context, entity); return createStartCmd(context, entity);
case TASK_CANCEL: case TASK_CANCEL:
return createCancelCommand(context, entity); return createCancelCmd(context, entity);
case TASK_STOP: case TASK_STOP:
return createStopCommand(context, entity); return createStopCmd(context, entity);
case TASK_STATE: case TASK_STATE:
return createStateCommand(context, entity); return createStateCmd(context, entity);
default: default:
return null; return null;
} }
@ -76,45 +76,45 @@ public class CommandFactory {
/** /**
* 创建获取任务状态的命令 * 创建获取任务状态的命令
* *
* @return {@link StateCommand} * @return {@link StateCmd}
*/ */
private StateCommand createStateCommand(Context context, DownloadEntity entity) { private StateCmd createStateCmd(Context context, DownloadEntity entity) {
return new StateCommand(context, entity); return new StateCmd(context, entity);
} }
/** /**
* 创建停止命令 * 创建停止命令
* *
* @return {@link StopCommand} * @return {@link StopCmd}
*/ */
private StopCommand createStopCommand(Context context, DownloadEntity entity) { private StopCmd createStopCmd(Context context, DownloadEntity entity) {
return new StopCommand(context, entity); return new StopCmd(context, entity);
} }
/** /**
* 创建下载任务命令 * 创建下载任务命令
* *
* @return {@link AddCommand} * @return {@link AddCmd}
*/ */
private AddCommand createAddCommand(Context context, DownloadEntity entity) { private AddCmd createAddCmd(Context context, DownloadEntity entity) {
return new AddCommand(context, entity); return new AddCmd(context, entity);
} }
/** /**
* 创建启动下载命令 * 创建启动下载命令
* *
* @return {@link StartCommand} * @return {@link StartCmd}
*/ */
private StartCommand createStartCommand(Context context, DownloadEntity entity) { private StartCmd createStartCmd(Context context, DownloadEntity entity) {
return new StartCommand(context, entity); return new StartCmd(context, entity);
} }
/** /**
* 创建 取消下载的命令 * 创建 取消下载的命令
* *
* @return {@link CancelCommand} * @return {@link CancelCmd}
*/ */
private CancelCommand createCancelCommand(Context context, DownloadEntity entity) { private CancelCmd createCancelCmd(Context context, DownloadEntity entity) {
return new CancelCommand(context, entity); return new CancelCmd(context, entity);
} }
} }

@ -11,7 +11,7 @@ import com.arialyy.downloadutil.util.Util;
* Created by lyy on 2016/8/22. * Created by lyy on 2016/8/22.
* 下载命令 * 下载命令
*/ */
public abstract class IDownloadCommand { public abstract class IDownloadCmd {
protected IDownloadTarget target; protected IDownloadTarget target;
protected Context mContext; protected Context mContext;
protected DownloadEntity mEntity; protected DownloadEntity mEntity;
@ -21,7 +21,7 @@ public abstract class IDownloadCommand {
* @param context context * @param context context
* @param entity 下载实体 * @param entity 下载实体
*/ */
protected IDownloadCommand(Context context, DownloadEntity entity) { protected IDownloadCmd(Context context, DownloadEntity entity) {
if (!CheckHelp.checkDownloadEntity(entity)) { if (!CheckHelp.checkDownloadEntity(entity)) {
return; return;
} }

@ -8,9 +8,9 @@ import com.arialyy.downloadutil.core.Task;
* Created by lyy on 2016/8/22. * 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); super(context, entity);
} }

@ -7,13 +7,13 @@ import com.arialyy.downloadutil.core.DownloadEntity;
* Created by lyy on 2016/9/20. * Created by lyy on 2016/9/20.
* 获取下载状态的命令 * 获取下载状态的命令
*/ */
class StateCommand extends IDownloadCommand { class StateCmd extends IDownloadCmd {
/** /**
* @param context context * @param context context
* @param entity 下载实体 * @param entity 下载实体
*/ */
StateCommand(Context context, DownloadEntity entity) { StateCmd(Context context, DownloadEntity entity) {
super(context, entity); super(context, entity);
} }

@ -9,13 +9,13 @@ import com.arialyy.downloadutil.core.Task;
* Created by lyy on 2016/9/20. * Created by lyy on 2016/9/20.
* 停止命令 * 停止命令
*/ */
class StopCommand extends IDownloadCommand { class StopCmd extends IDownloadCmd {
/** /**
* @param context context * @param context context
* @param entity 下载实体 * @param entity 下载实体
*/ */
StopCommand(Context context, DownloadEntity entity) { StopCmd(Context context, DownloadEntity entity) {
super(context, entity); super(context, entity);
} }

@ -2,8 +2,8 @@ package com.arialyy.downloadutil.core.pool;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.arialyy.downloadutil.core.inf.IPool;
import com.arialyy.downloadutil.core.Task; import com.arialyy.downloadutil.core.Task;
import com.arialyy.downloadutil.core.inf.IPool;
import com.arialyy.downloadutil.util.Util; import com.arialyy.downloadutil.util.Util;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -19,7 +19,7 @@ public class ExecutePool implements IPool {
private static final Object LOCK = new Object(); private static final Object LOCK = new Object();
private static final long TIME_OUT = 1000; private static final long TIME_OUT = 1000;
private static volatile ExecutePool INSTANCE = null; private static volatile ExecutePool INSTANCE = null;
private static int SIZE = 2; public static int SIZE = 2;
private ArrayBlockingQueue<Task> mExecuteQueue; private ArrayBlockingQueue<Task> mExecuteQueue;
private Map<String, Task> mExecuteArray; private Map<String, Task> mExecuteArray;

@ -1,6 +1,7 @@
package com.arialyy.downloadutil.orm; package com.arialyy.downloadutil.orm;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.util.Log;
import com.arialyy.downloadutil.util.Util; import com.arialyy.downloadutil.util.Util;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
@ -11,8 +12,9 @@ import java.util.List;
* 所有数据库实体父类 * 所有数据库实体父类
*/ */
public class DbEntity { public class DbEntity {
protected int rowID = -1; protected int rowID = -1;
private DbUtil mUtil = DbUtil.getInstance(); private DbUtil mUtil = DbUtil.getInstance();
private static final Object LOCK = new Object();
protected DbEntity() { protected DbEntity() {
@ -56,11 +58,13 @@ public class DbEntity {
/** /**
* 保存自身如果表中已经有数据则更新数据否则插入数据 * 保存自身如果表中已经有数据则更新数据否则插入数据
*/ */
public synchronized void save() { public void save() {
if (mUtil.tableExists(getClass()) && thisIsExist()) { synchronized (LOCK) {
update(); if (mUtil.tableExists(getClass()) && thisIsExist()) {
} else { update();
insert(); } else {
insert();
}
} }
} }

@ -292,6 +292,9 @@ public class DbUtil {
* @param type {@link DbUtil} * @param type {@link DbUtil}
*/ */
private void print(int type, String sql) { private void print(int type, String sql) {
if (true){
return;
}
String str = ""; String str = "";
switch (type) { switch (type) {
case 0: case 0:
@ -409,6 +412,7 @@ public class DbUtil {
} }
entity.rowID = cursor.getInt(cursor.getColumnIndex("rowid")); entity.rowID = cursor.getInt(cursor.getColumnIndex("rowid"));
entitys.add(entity); entitys.add(entity);
Log.d(TAG, "rowid ==> " + entity.rowID);
} }
} catch (InstantiationException e) { } catch (InstantiationException e) {
e.printStackTrace(); e.printStackTrace();

Loading…
Cancel
Save