pull/330/head
AriaLyy 8 years ago
parent 8eb1ba8477
commit 18032aa73a
  1. 11
      Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
  2. 15
      Aria/src/main/java/com/arialyy/aria/core/command/AbsCmd.java
  3. 16
      Aria/src/main/java/com/arialyy/aria/core/command/AddCmd.java
  4. 14
      Aria/src/main/java/com/arialyy/aria/core/command/CancelCmd.java
  5. 2
      Aria/src/main/java/com/arialyy/aria/core/command/CmdFactory.java
  6. 12
      Aria/src/main/java/com/arialyy/aria/core/command/StartCmd.java
  7. 21
      Aria/src/main/java/com/arialyy/aria/core/command/StopCmd.java
  8. 1
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java
  9. 6
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
  10. 6
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java
  11. 1
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java
  12. 5
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java
  13. 17
      Aria/src/main/java/com/arialyy/aria/core/inf/IEntity.java
  14. 2
      Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java
  15. 17
      Aria/src/main/java/com/arialyy/aria/core/inf/ITaskEntity.java
  16. 5
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java
  17. 2
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java
  18. 5
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java
  19. 5
      Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java

@ -33,6 +33,7 @@ import com.arialyy.aria.core.download.DownloadReceiver;
import com.arialyy.aria.core.inf.ICmd; import com.arialyy.aria.core.inf.ICmd;
import com.arialyy.aria.core.inf.IReceiver; import com.arialyy.aria.core.inf.IReceiver;
import com.arialyy.aria.core.queue.DownloadTaskQueue; import com.arialyy.aria.core.queue.DownloadTaskQueue;
import com.arialyy.aria.core.queue.UploadTaskQueue;
import com.arialyy.aria.core.upload.UploadReceiver; import com.arialyy.aria.core.upload.UploadReceiver;
import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.orm.DbUtil; import com.arialyy.aria.orm.DbUtil;
@ -56,7 +57,6 @@ import java.util.Map;
private static final Object LOCK = new Object(); private static final Object LOCK = new Object();
@SuppressLint("StaticFieldLeak") private static volatile AriaManager INSTANCE = null; @SuppressLint("StaticFieldLeak") private static volatile AriaManager INSTANCE = null;
private Map<String, IReceiver> mReceivers = new HashMap<>(); private Map<String, IReceiver> mReceivers = new HashMap<>();
private DownloadTaskQueue mDTaskQueue;
public static Context APP; public static Context APP;
private List<ICmd> mCommands = new ArrayList<>(); private List<ICmd> mCommands = new ArrayList<>();
@ -84,13 +84,6 @@ import java.util.Map;
return DbEntity.findAllData(DownloadEntity.class); return DbEntity.findAllData(DownloadEntity.class);
} }
/**
* 获取任务队列
*/
public DownloadTaskQueue getTaskQueue() {
return mDTaskQueue;
}
/** /**
* 设置命令 * 设置命令
*/ */
@ -190,7 +183,7 @@ import java.util.Map;
Log.w(TAG, "最大任务数不能小于 1"); Log.w(TAG, "最大任务数不能小于 1");
return this; return this;
} }
mDTaskQueue.setDownloadNum(maxDownloadNum); DownloadTaskQueue.getInstance().setDownloadNum(maxDownloadNum);
return this; return this;
} }

@ -16,10 +16,13 @@
package com.arialyy.aria.core.command; package com.arialyy.aria.core.command;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.inf.ITaskEntity; import com.arialyy.aria.core.inf.ITaskEntity;
import com.arialyy.aria.core.queue.DownloadTaskQueue;
import com.arialyy.aria.core.queue.ITaskQueue; import com.arialyy.aria.core.queue.ITaskQueue;
import com.arialyy.aria.core.inf.ICmd; import com.arialyy.aria.core.inf.ICmd;
import com.arialyy.aria.core.queue.UploadTaskQueue;
import com.arialyy.aria.core.upload.UploadTaskEntity;
import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
@ -27,7 +30,7 @@ import com.arialyy.aria.util.CommonUtil;
* Created by lyy on 2016/8/22. * Created by lyy on 2016/8/22.
* 下载命令 * 下载命令
*/ */
public abstract class IDownloadCmd<T extends ITaskEntity> implements ICmd { public abstract class AbsCmd<T extends ITaskEntity> implements ICmd {
ITaskQueue mQueue; ITaskQueue mQueue;
T mEntity; T mEntity;
String TAG; String TAG;
@ -36,11 +39,15 @@ public abstract class IDownloadCmd<T extends ITaskEntity> implements ICmd {
/** /**
* @param targetName 产生任务的对象名 * @param targetName 产生任务的对象名
*/ */
IDownloadCmd(String targetName, T entity) { AbsCmd(String targetName, T entity) {
CheckUtil.checkTaskEntity(entity); CheckUtil.checkTaskEntity(entity);
mTargetName = targetName; mTargetName = targetName;
mEntity = entity; mEntity = entity;
TAG = CommonUtil.getClassName(this); TAG = CommonUtil.getClassName(this);
mQueue = AriaManager.getInstance(AriaManager.APP).getTaskQueue(); if (entity instanceof DownloadTaskEntity) {
mQueue = DownloadTaskQueue.getInstance();
} else if (entity instanceof UploadTaskEntity) {
mQueue = UploadTaskQueue.getInstance();
}
} }
} }

@ -16,25 +16,27 @@
package com.arialyy.aria.core.command; package com.arialyy.aria.core.command;
import android.util.Log;
import com.arialyy.aria.core.inf.ITask;
import com.arialyy.aria.core.inf.ITaskEntity; import com.arialyy.aria.core.inf.ITaskEntity;
/** /**
* Created by lyy on 2016/8/22. * Created by lyy on 2016/8/22.
* 添加任务的命令 * 添加任务的命令
*/ */
class AddCmd<T extends ITaskEntity> extends IDownloadCmd<T> { class AddCmd<T extends ITaskEntity> extends AbsCmd<T> {
AddCmd(String targetName, T entity) { AddCmd(String targetName, T entity) {
super(targetName, entity); super(targetName, entity);
} }
@Override public void executeCmd() { @Override public void executeCmd() {
// DownloadTask task = mQueue.getTask(mEntity.downloadEntity); ITask task = mQueue.getTask(mEntity.getEntity());
// if (task == null) { if (task == null){
// mQueue.createTask(mTargetName, mEntity); mQueue.createTask(mTargetName, mEntity);
// } else { }else {
// Log.w(TAG, "添加命令执行失败,【该任务已经存在】"); Log.w(TAG, "添加命令执行失败,【该任务已经存在】");
// } }
} }
//AddCmd(DownloadTaskEntity entity) { //AddCmd(DownloadTaskEntity entity) {

@ -16,19 +16,29 @@
package com.arialyy.aria.core.command; package com.arialyy.aria.core.command;
import com.arialyy.aria.core.inf.ITask;
import com.arialyy.aria.core.inf.ITaskEntity; import com.arialyy.aria.core.inf.ITaskEntity;
/** /**
* Created by lyy on 2016/9/20. * Created by lyy on 2016/9/20.
* 取消命令 * 取消命令
*/ */
class CancelCmd<T extends ITaskEntity> extends IDownloadCmd<T> { class CancelCmd<T extends ITaskEntity> extends AbsCmd<T> {
CancelCmd(String targetName, T entity) { CancelCmd(String targetName, T entity) {
super(targetName, entity); super(targetName, entity);
} }
@Override public void executeCmd() { @Override public void executeCmd() {
ITask task = mQueue.getTask(mEntity.getEntity());
if (task == null) {
task = mQueue.createTask(mTargetName, mEntity);
}
if (task != null) {
if (mTargetName != null) {
task.setTargetName(mTargetName);
}
mQueue.cancelTask(task);
}
} }
//CancelCmd(DownloadTaskEntity entity) { //CancelCmd(DownloadTaskEntity entity) {

@ -67,7 +67,7 @@ public class CmdFactory {
* @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} * #TASK_STOP}
*/ */
public <T extends ITaskEntity> IDownloadCmd createCmd(String target, T entity, int type) { public <T extends ITaskEntity> AbsCmd createCmd(String target, T entity, int type) {
switch (type) { switch (type) {
case TASK_CREATE: case TASK_CREATE:
return createAddCmd(target, entity); return createAddCmd(target, entity);

@ -16,20 +16,28 @@
package com.arialyy.aria.core.command; package com.arialyy.aria.core.command;
import com.arialyy.aria.core.inf.ITask;
import com.arialyy.aria.core.inf.ITaskEntity; import com.arialyy.aria.core.inf.ITaskEntity;
/** /**
* Created by lyy on 2016/8/22. * Created by lyy on 2016/8/22.
* 开始命令 * 开始命令
*/ */
class StartCmd<T extends ITaskEntity> extends IDownloadCmd<T> { class StartCmd<T extends ITaskEntity> extends AbsCmd<T> {
StartCmd(String targetName, T entity) { StartCmd(String targetName, T entity) {
super(targetName, entity); super(targetName, entity);
} }
@Override public void executeCmd() { @Override public void executeCmd() {
ITask task = mQueue.getTask(mEntity.getEntity());
if (task == null) {
task = mQueue.createTask(mTargetName, mEntity);
}
if (task != null) {
task.setTargetName(mTargetName);
mQueue.startTask(task);
}
} }
//StartCmd(DownloadTaskEntity entity) { //StartCmd(DownloadTaskEntity entity) {

@ -16,20 +16,37 @@
package com.arialyy.aria.core.command; package com.arialyy.aria.core.command;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.ITask;
import com.arialyy.aria.core.inf.ITaskEntity; import com.arialyy.aria.core.inf.ITaskEntity;
/** /**
* Created by lyy on 2016/9/20. * Created by lyy on 2016/9/20.
* 停止命令 * 停止命令
*/ */
class StopCmd<T extends ITaskEntity> extends IDownloadCmd<T> { class StopCmd<T extends ITaskEntity> extends AbsCmd<T> {
StopCmd(String targetName, T entity) { StopCmd(String targetName, T entity) {
super(targetName, entity); super(targetName, entity);
} }
@Override public void executeCmd() { @Override public void executeCmd() {
ITask task = mQueue.getTask(mEntity.getEntity());
if (task == null) {
if (mEntity.getEntity().getState() == IEntity.STATE_RUNNING) {
task = mQueue.createTask(mTargetName, mEntity);
mQueue.stopTask(task);
} else {
Log.w(TAG, "停止命令执行失败,【调度器中没有该任务】");
}
} else {
if (!TextUtils.isEmpty(mTargetName)) {
task.setTargetName(mTargetName);
}
mQueue.stopTask(task);
}
} }
//StopCmd(DownloadTaskEntity entity) { //StopCmd(DownloadTaskEntity entity) {

@ -129,6 +129,7 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity{
this.fileSize = fileSize; this.fileSize = fileSize;
} }
@Override
public int getState() { public int getState() {
return state; return state;
} }

@ -19,7 +19,7 @@ import android.support.annotation.NonNull;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.inf.IReceiver; import com.arialyy.aria.core.inf.IReceiver;
import com.arialyy.aria.core.command.CmdFactory; import com.arialyy.aria.core.command.CmdFactory;
import com.arialyy.aria.core.command.IDownloadCmd; import com.arialyy.aria.core.command.AbsCmd;
import com.arialyy.aria.core.scheduler.DownloadSchedulers; import com.arialyy.aria.core.scheduler.DownloadSchedulers;
import com.arialyy.aria.core.scheduler.OnSchedulerListener; import com.arialyy.aria.core.scheduler.OnSchedulerListener;
import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CheckUtil;
@ -109,7 +109,7 @@ public class DownloadReceiver implements IReceiver {
public void stopAllTask() { public void stopAllTask() {
final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP); final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP);
List<DownloadEntity> allEntity = ariaManager.getAllDownloadEntity(); List<DownloadEntity> allEntity = ariaManager.getAllDownloadEntity();
List<IDownloadCmd> stopCmds = new ArrayList<>(); List<AbsCmd> stopCmds = new ArrayList<>();
for (DownloadEntity entity : allEntity) { for (DownloadEntity entity : allEntity) {
if (entity.getState() == DownloadEntity.STATE_RUNNING) { if (entity.getState() == DownloadEntity.STATE_RUNNING) {
stopCmds.add(CommonUtil.createDownloadCmd(targetName, new DownloadTaskEntity(entity), stopCmds.add(CommonUtil.createDownloadCmd(targetName, new DownloadTaskEntity(entity),
@ -125,7 +125,7 @@ public class DownloadReceiver implements IReceiver {
public void cancelAllTask() { public void cancelAllTask() {
final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP); final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP);
List<DownloadEntity> allEntity = ariaManager.getAllDownloadEntity(); List<DownloadEntity> allEntity = ariaManager.getAllDownloadEntity();
List<IDownloadCmd> cancelCmds = new ArrayList<>(); List<AbsCmd> cancelCmds = new ArrayList<>();
for (DownloadEntity entity : allEntity) { for (DownloadEntity entity : allEntity) {
cancelCmds.add(CommonUtil.createDownloadCmd(targetName, new DownloadTaskEntity(entity), cancelCmds.add(CommonUtil.createDownloadCmd(targetName, new DownloadTaskEntity(entity),
CmdFactory.TASK_CANCEL)); CmdFactory.TASK_CANCEL));

@ -20,7 +20,7 @@ import android.text.TextUtils;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.RequestEnum; import com.arialyy.aria.core.RequestEnum;
import com.arialyy.aria.core.command.CmdFactory; import com.arialyy.aria.core.command.CmdFactory;
import com.arialyy.aria.core.command.IDownloadCmd; import com.arialyy.aria.core.command.AbsCmd;
import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import java.util.ArrayList; import java.util.ArrayList;
@ -141,7 +141,7 @@ public class DownloadTarget {
* 开始下载 * 开始下载
*/ */
public void start() { public void start() {
List<IDownloadCmd> cmds = new ArrayList<>(); List<AbsCmd> cmds = new ArrayList<>();
cmds.add(CommonUtil.createDownloadCmd(targetName, taskEntity, CmdFactory.TASK_CREATE)); cmds.add(CommonUtil.createDownloadCmd(targetName, taskEntity, CmdFactory.TASK_CREATE));
cmds.add(CommonUtil.createDownloadCmd(targetName, taskEntity, CmdFactory.TASK_START)); cmds.add(CommonUtil.createDownloadCmd(targetName, taskEntity, CmdFactory.TASK_START));
AriaManager.getInstance(AriaManager.APP).setCmds(cmds).exe(); AriaManager.getInstance(AriaManager.APP).setCmds(cmds).exe();
@ -179,7 +179,7 @@ public class DownloadTarget {
* 是否在下载 * 是否在下载
*/ */
public boolean isDownloading() { public boolean isDownloading() {
return AriaManager.getInstance(AriaManager.APP).getTaskQueue().getTask(entity).isDownloading(); return AriaManager.getInstance(AriaManager.APP).getDownloadTaskQueue().getTask(entity).isDownloading();
} }
/** /**

@ -133,6 +133,7 @@ public class DownloadTask implements ITask {
return mTargetName; return mTargetName;
} }
@Override
public void setTargetName(String targetName) { public void setTargetName(String targetName) {
this.mTargetName = targetName; this.mTargetName = targetName;
} }

@ -16,6 +16,7 @@
package com.arialyy.aria.core.download; package com.arialyy.aria.core.download;
import com.arialyy.aria.core.RequestEnum; import com.arialyy.aria.core.RequestEnum;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.ITaskEntity; import com.arialyy.aria.core.inf.ITaskEntity;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -33,4 +34,8 @@ public class DownloadTaskEntity implements ITaskEntity {
public DownloadTaskEntity(DownloadEntity downloadEntity) { public DownloadTaskEntity(DownloadEntity downloadEntity) {
this.downloadEntity = downloadEntity; this.downloadEntity = downloadEntity;
} }
@Override public IEntity getEntity() {
return downloadEntity;
}
} }

@ -1,3 +1,18 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.inf; package com.arialyy.aria.core.inf;
import com.arialyy.aria.orm.Ignore; import com.arialyy.aria.orm.Ignore;
@ -44,4 +59,6 @@ public interface IEntity {
*/ */
@Ignore public static final int STATE_CANCEL = 7; @Ignore public static final int STATE_CANCEL = 7;
public int getState();
} }

@ -33,4 +33,6 @@ public interface ITask {
public long getFileSize(); public long getFileSize();
public long getCurrentProgress(); public long getCurrentProgress();
public void setTargetName(String targetName);
} }

@ -1,3 +1,18 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.inf; package com.arialyy.aria.core.inf;
/** /**
@ -6,4 +21,6 @@ package com.arialyy.aria.core.inf;
public interface ITaskEntity { public interface ITaskEntity {
public IEntity getEntity();
} }

@ -61,7 +61,7 @@ public class UploadEntity extends DbEntity implements IEntity, Parcelable {
this.failNum = failNum; this.failNum = failNum;
} }
public int getState() { @Override public int getState() {
return state; return state;
} }
@ -114,8 +114,7 @@ public class UploadEntity extends DbEntity implements IEntity, Parcelable {
this.failNum = in.readInt(); this.failNum = in.readInt();
} }
@Ignore @Ignore public static final Creator<UploadEntity> CREATOR = new Creator<UploadEntity>() {
public static final Creator<UploadEntity> CREATOR = new Creator<UploadEntity>() {
@Override public UploadEntity createFromParcel(Parcel source) { @Override public UploadEntity createFromParcel(Parcel source) {
return new UploadEntity(source); return new UploadEntity(source);
} }

@ -36,7 +36,7 @@ public class UploadTask implements ITask {
mUtil = new UploadUtil(mTaskEntity, mListener); mUtil = new UploadUtil(mTaskEntity, mListener);
} }
void setTargetName(String targetName) { @Override public void setTargetName(String targetName) {
mTargetName = targetName; mTargetName = targetName;
} }

@ -1,6 +1,7 @@
package com.arialyy.aria.core.upload; package com.arialyy.aria.core.upload;
import com.arialyy.aria.core.RequestEnum; import com.arialyy.aria.core.RequestEnum;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.ITaskEntity; import com.arialyy.aria.core.inf.ITaskEntity;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -28,4 +29,8 @@ public class UploadTaskEntity implements ITaskEntity {
public UploadTaskEntity(UploadEntity downloadEntity) { public UploadTaskEntity(UploadEntity downloadEntity) {
this.uploadEntity = downloadEntity; this.uploadEntity = downloadEntity;
} }
@Override public IEntity getEntity() {
return uploadEntity;
}
} }

@ -21,9 +21,8 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
import android.util.Log; import android.util.Log;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.command.CmdFactory; import com.arialyy.aria.core.command.CmdFactory;
import com.arialyy.aria.core.command.IDownloadCmd; import com.arialyy.aria.core.command.AbsCmd;
import com.arialyy.aria.core.inf.ITaskEntity; import com.arialyy.aria.core.inf.ITaskEntity;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -41,7 +40,7 @@ import java.util.Properties;
public class CommonUtil { public class CommonUtil {
private static final String TAG = "util"; private static final String TAG = "util";
public static <T extends ITaskEntity> IDownloadCmd createDownloadCmd(String target, T entity, int cmd) { public static <T extends ITaskEntity> AbsCmd createDownloadCmd(String target, T entity, int cmd) {
return CmdFactory.getInstance().createCmd(target, entity, cmd); return CmdFactory.getInstance().createCmd(target, entity, cmd);
} }

Loading…
Cancel
Save