修改删除所有任务的命令

pull/330/head
AriaLyy 8 years ago
parent 3c876d8d52
commit ba7ad618be
  1. 69
      Aria/src/main/java/com/arialyy/aria/core/command/CancelAllCmd.java
  2. 7
      Aria/src/main/java/com/arialyy/aria/core/command/CmdFactory.java
  3. 37
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
  4. 8
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskGroup.java
  5. 5
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadUtil.java
  6. 27
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsReceiver.java
  7. 5
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTaskEntity.java
  8. 2
      Aria/src/main/java/com/arialyy/aria/core/inf/IReceiver.java
  9. 12
      Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java
  10. 5
      Aria/src/main/java/com/arialyy/aria/core/queue/ITaskQueue.java
  11. 31
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java
  12. 47
      Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java

@ -0,0 +1,69 @@
/*
* 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.command;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CommonUtil;
import java.io.File;
import java.util.List;
/**
* Created by AriaL on 2017/6/27.
* 删除所有任务并且删除所有回掉
*/
final class CancelAllCmd<T extends AbsTaskEntity> extends AbsCmd<T> {
/**
* @param targetName 产生任务的对象名
*/
CancelAllCmd(String targetName, T entity) {
super(targetName, entity);
}
@Override public void executeCmd() {
mQueue.removeAllTask();
if (mTaskEntity instanceof DownloadTaskEntity) {
handleDownloadRemove();
} else {
handleUploadRemove();
}
}
/**
* 处理上传的删除
*/
private void handleUploadRemove() {
List<UploadEntity> allEntity = DbEntity.findAllData(UploadEntity.class);
for (UploadEntity entity : allEntity) {
CommonUtil.delUploadTaskConfig(mTaskEntity.removeFile, entity);
}
}
/**
* 处理下载的删除
*/
private void handleDownloadRemove() {
List<DownloadEntity> allEntity = DbEntity.findAllData(DownloadEntity.class);
for (DownloadEntity entity : allEntity) {
CommonUtil.delDownloadTaskConfig(mTaskEntity.removeFile, entity);
}
}
}

@ -56,7 +56,10 @@ public class CmdFactory {
* 恢复所有停止的任务
*/
public static final int TASK_RESUME_ALL = 0x130;
/**
* 删除所有任务
*/
public static final int TASK_CANCEL_ALL = 0x131;
private static volatile CmdFactory INSTANCE = null;
private CmdFactory() {
@ -95,6 +98,8 @@ public class CmdFactory {
return new StopAllCmd<>(target, entity);
case TASK_RESUME_ALL:
return new ResumeAllCmd<>(target, entity);
case TASK_CANCEL_ALL:
return new CancelAllCmd<>(target, entity);
default:
return null;
}

@ -17,7 +17,7 @@ package com.arialyy.aria.core.download;
import android.support.annotation.NonNull;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.inf.ICmd;
import com.arialyy.aria.core.inf.AbsReceiver;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.IReceiver;
import com.arialyy.aria.core.command.CmdFactory;
@ -36,10 +36,8 @@ import java.util.Set;
* Created by lyy on 2016/12/5.
* 下载功能接收器
*/
public class DownloadReceiver implements IReceiver<DownloadEntity> {
public class DownloadReceiver extends AbsReceiver<DownloadEntity> {
private static final String TAG = "DownloadReceiver";
public String targetName;
public Object obj;
public ISchedulerListener<DownloadTask> listener;
/**
@ -143,9 +141,10 @@ public class DownloadReceiver implements IReceiver<DownloadEntity> {
* 停止所有正在下载的任务并清空等待队列
*/
@Override public void stopAllTask() {
final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP);
ariaManager.setCmd(CmdFactory.getInstance()
.createCmd(targetName, new DownloadTaskEntity(), CmdFactory.TASK_STOP_ALL)).exe();
AriaManager.getInstance(AriaManager.APP)
.setCmd(CmdFactory.getInstance()
.createCmd(targetName, new DownloadTaskEntity(), CmdFactory.TASK_STOP_ALL))
.exe();
}
/**
@ -154,23 +153,25 @@ public class DownloadReceiver implements IReceiver<DownloadEntity> {
* 2.如果队列执行队列已经满了则将所有任务添加到等待队列中
*/
public void resumeAllTask() {
final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP);
ariaManager.setCmd(CmdFactory.getInstance()
.createCmd(targetName, new DownloadTaskEntity(), CmdFactory.TASK_RESUME_ALL)).exe();
AriaManager.getInstance(AriaManager.APP)
.setCmd(CmdFactory.getInstance()
.createCmd(targetName, new DownloadTaskEntity(), CmdFactory.TASK_RESUME_ALL))
.exe();
}
/**
* 删除所有任务
*
* @param removeFile {@code true} 删除已经下载完成的任务不仅删除下载记录还会删除已经下载完成的文件{@code false}
* 如果文件已经下载完成只删除下载记录
*/
@Override public void removeAllTask() {
@Override public void removeAllTask(boolean removeFile) {
final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP);
List<DownloadEntity> allEntity = DbEntity.findAllData(DownloadEntity.class);
List<AbsCmd> cancelCmds = new ArrayList<>();
for (DownloadEntity entity : allEntity) {
cancelCmds.add(
CommonUtil.createCmd(targetName, new DownloadTaskEntity(entity), CmdFactory.TASK_CANCEL));
}
ariaManager.setCmds(cancelCmds).exe();
AriaManager.getInstance(AriaManager.APP)
.setCmd(
CommonUtil.createCmd(targetName, new DownloadTaskEntity(), CmdFactory.TASK_CANCEL_ALL))
.exe();
Set<String> keys = ariaManager.getReceiver().keySet();
for (String key : keys) {
IReceiver receiver = ariaManager.getReceiver().get(key);

@ -0,0 +1,8 @@
package com.arialyy.aria.core.download;
/**
* Created by AriaL on 2017/6/27.
*/
public class DownloadTaskGroup {
}

@ -101,7 +101,7 @@ class DownloadUtil implements IDownloadUtil, Runnable {
mDownloadEntity.getDownloadUrl()) == null) {
isNewTask = true;
} else {
isNewTask = false;
isNewTask = !mDownloadFile.exists();
}
} catch (Exception e) {
e.printStackTrace();
@ -342,7 +342,8 @@ class DownloadUtil implements IDownloadUtil, Runnable {
recordL[rl] = i;
rl++;
} else {
handleNewTask(fileLength);
//handleNewTask(fileLength);
isNewTask = true;
}
if (isNewTask) {
recordL[rl] = i;

@ -0,0 +1,27 @@
/*
* 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;
/**
* Created by AriaL on 2017/6/27.
*/
public abstract class AbsReceiver<ENTITY extends AbsEntity> implements IReceiver<ENTITY>{
public String targetName;
public Object obj;
}

@ -39,5 +39,10 @@ public abstract class AbsTaskEntity {
*/
public String redirectUrlKey = "location";
/**
* 用于判断删除任务时是否需要删除文件{@code true}删除
*/
public boolean removeFile = false;
public abstract AbsEntity getEntity();
}

@ -44,7 +44,7 @@ public interface IReceiver<ENTITY extends IEntity> {
/**
* 删除所有任务
*/
public void removeAllTask();
public void removeAllTask(boolean removeFile);
/**
* 任务是否存在

@ -41,6 +41,18 @@ abstract class AbsTaskQueue<TASK extends AbsTask, TASK_ENTITY extends AbsTaskEnt
return mExecutePool.getTask(key) != null;
}
@Override public void removeAllTask() {
Set<String> exeKeys = mExecutePool.getAllTask().keySet();
for (String key : exeKeys) {
TASK task = mExecutePool.getAllTask().get(key);
if (task != null) task.cancel();
}
Set<String> cacheKeys = mCachePool.getAllTask().keySet();
for (String key : cacheKeys) {
mExecutePool.removeTask(key);
}
}
/**
* 停止所有任务
*/

@ -46,6 +46,11 @@ public interface ITaskQueue<TASK extends AbsTask, TASK_ENTITY extends AbsTaskEnt
*/
void stopAllTask();
/**
* 删除所有任务
*/
void removeAllTask();
/**
* 设置任务为最高优先级任务
*

@ -20,6 +20,8 @@ import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.command.AbsCmd;
import com.arialyy.aria.core.command.CmdFactory;
import com.arialyy.aria.core.download.DownloadReceiver;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.inf.AbsReceiver;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.IReceiver;
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
@ -37,10 +39,8 @@ import java.util.regex.Pattern;
* Created by lyy on 2017/2/6.
* 上传功能接收器
*/
public class UploadReceiver implements IReceiver<UploadEntity> {
public class UploadReceiver extends AbsReceiver<UploadEntity> {
private static final String TAG = "DownloadReceiver";
public String targetName;
public Object obj;
public ISchedulerListener<UploadTask> listener;
/**
@ -94,15 +94,20 @@ public class UploadReceiver implements IReceiver<UploadEntity> {
AriaManager.getInstance(AriaManager.APP).setCmds(stopCmds).exe();
}
@Override public void removeAllTask() {
/**
* 删除所有任务
*
* @param removeFile {@code true} 删除已经上传完成的任务不仅删除上传记录还会删除已经上传完成的文件{@code false}
* 如果文件已经上传完成只删除上传记录
*/
@Override public void removeAllTask(boolean removeFile) {
final AriaManager am = AriaManager.getInstance(AriaManager.APP);
List<UploadEntity> allEntity = DbEntity.findAllData(UploadEntity.class);
List<AbsCmd> cancelCmds = new ArrayList<>();
for (UploadEntity entity : allEntity) {
cancelCmds.add(
CommonUtil.createCmd(targetName, new UploadTaskEntity(entity), CmdFactory.TASK_CANCEL));
}
am.setCmds(cancelCmds).exe();
AriaManager.getInstance(AriaManager.APP)
.setCmd(
CommonUtil.createCmd(targetName, new DownloadTaskEntity(), CmdFactory.TASK_CANCEL_ALL))
.exe();
Set<String> keys = am.getReceiver().keySet();
for (String key : keys) {
IReceiver receiver = am.getReceiver().get(key);
@ -118,8 +123,10 @@ public class UploadReceiver implements IReceiver<UploadEntity> {
/**
* 添加调度器回调
*
* @see #register()
*/
public UploadReceiver addSchedulerListener(ISchedulerListener<UploadTask> listener) {
@Deprecated public UploadReceiver addSchedulerListener(ISchedulerListener<UploadTask> listener) {
this.listener = listener;
UploadSchedulers.getInstance().addSchedulerListener(targetName, listener);
return this;

@ -22,9 +22,12 @@ import android.content.SharedPreferences;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.command.CmdFactory;
import com.arialyy.aria.core.command.AbsCmd;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.upload.UploadEntity;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
@ -49,10 +52,52 @@ import java.util.regex.Pattern;
public class CommonUtil {
private static final String TAG = "CommonUtil";
/**
* 删除上传任务的配置包括
*
* @param removeFile {@code true} 删除已经上传完成的任务不仅删除上传记录还会删除已经上传完成的文件{@code false}
* 如果文件已经上传完成只删除上传记录
*/
public static void delUploadTaskConfig(boolean removeFile, UploadEntity entity) {
if (removeFile) {
File file = new File(entity.getFilePath());
if (file.exists()) {
file.delete();
}
}
File config = new File(
AriaManager.APP.getFilesDir().getPath() + "/temp/" + entity.getFileName() + ".properties");
if (config.exists()) {
config.delete();
}
entity.deleteData();
}
/**
* 删除下载任务的配置包括
*
* @param removeFile{@code true} 删除已经下载完成的任务不仅删除下载记录还会删除已经下载完成的文件{@code false}
* 如果文件已经下载完成只删除下载记录
*/
public static void delDownloadTaskConfig(boolean removeFile, DownloadEntity entity) {
if (removeFile) {
File file = new File(entity.getDownloadPath());
if (file.exists()) {
file.delete();
}
}
File config = new File(
AriaManager.APP.getFilesDir().getPath() + "/temp/" + entity.getFileName() + ".properties");
if (config.exists()) {
config.delete();
}
entity.deleteData();
}
/**
* 获取CPU核心数
*/
public static int getNumCores() {
public static int getCoresNum() {
//Private Class to display only CPU devices in the directory listing
class CpuFilter implements FileFilter {
@Override public boolean accept(File pathname) {

Loading…
Cancel
Save