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);
switch (action) {
case DownloadManager.ACTION_PRE:
len = entity.getFileSize();
L.d(TAG, "download pre");
mAdapter.updateState(entity);
break;
case DownloadManager.ACTION_POST_PRE:
len = entity.getFileSize();
L.d(TAG, "download post pre");
break;
case DownloadManager.ACTION_START:
L.d(TAG, "download start");

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

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

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

@ -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();

Loading…
Cancel
Save