target、receiver

pull/330/head
AriaLyy 8 years ago
parent 9bc87d75df
commit 9241c17513
  1. 2
      .idea/misc.xml
  2. 21
      Aria/src/main/java/com/arialyy/aria/core/Aria.java
  3. 21
      Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
  4. 32
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
  5. 123
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java
  6. 7
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java
  7. 175
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java
  8. 26
      Aria/src/main/java/com/arialyy/aria/core/inf/IReceiver.java
  9. 16
      Aria/src/main/java/com/arialyy/aria/core/inf/ITaskEntity.java
  10. 2
      Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java
  11. 95
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java
  12. 115
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java
  13. 26
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java
  14. 7
      Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java
  15. 2
      Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
  16. 36
      app/src/main/java/com/arialyy/simple/single_task/SingleTaskActivity.java

@ -37,7 +37,7 @@
<ConfirmationsSetting value="0" id="Add" /> <ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" /> <ConfirmationsSetting value="0" id="Remove" />
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">

@ -29,6 +29,7 @@ import android.widget.PopupWindow;
import com.arialyy.aria.core.download.DownloadReceiver; import com.arialyy.aria.core.download.DownloadReceiver;
import com.arialyy.aria.core.scheduler.OnSchedulerListener; import com.arialyy.aria.core.scheduler.OnSchedulerListener;
import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.upload.UploadReceiver;
import com.arialyy.aria.core.upload.UploadTask; import com.arialyy.aria.core.upload.UploadTask;
/** /**
@ -37,7 +38,7 @@ import com.arialyy.aria.core.upload.UploadTask;
* Aria启动管理全局任务 * Aria启动管理全局任务
* <pre> * <pre>
* <code> * <code>
* //启动下载 * //下载
* Aria.download(this) * Aria.download(this)
* .load(DOWNLOAD_URL) //下载地址,必填 * .load(DOWNLOAD_URL) //下载地址,必填
* //文件保存路径,必填 * //文件保存路径,必填
@ -45,6 +46,15 @@ import com.arialyy.aria.core.upload.UploadTask;
* .setDownloadName("test.apk") //文件名,必填 * .setDownloadName("test.apk") //文件名,必填
* .start(); * .start();
* </code> * </code>
* <code>
* //上传
* Aria.upload(this)
* .load(filePath) //文件路径,必填
* .setUploadUrl(uploadUrl) //上传路径,必填
* .setFileName(fileName) //文件名
* .setAttachment(fileKey) //服务器读取文件的key
* .start();
* </code>
* </pre> * </pre>
*/ */
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) public class Aria { @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) public class Aria {
@ -109,6 +119,15 @@ import com.arialyy.aria.core.upload.UploadTask;
return get(obj).download(obj); return get(obj).download(obj);
} }
/**
* 初始化上传
*
* @param obj 支持类型有ActivityServiceApplicationDialogFragmentFragmentPopupWindowDialog
*/
public static UploadReceiver upload(Object obj) {
return get(obj).upload(obj);
}
/** /**
* 处理通用事件 * 处理通用事件
* *

@ -57,13 +57,12 @@ 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<>();
public static Context APP; public static Context APP;
private List<ICmd> mCommands = new ArrayList<>(); private List<ICmd> mCommands = new ArrayList<>();
private AriaManager(Context context) { private AriaManager(Context context) {
DbUtil.init(context.getApplicationContext()); DbUtil.init(context.getApplicationContext());
APP = context; APP = context.getApplicationContext();
regAppLifeCallback(context); regAppLifeCallback(context);
} }
@ -80,10 +79,6 @@ import java.util.Map;
return mReceivers; return mReceivers;
} }
public List<DownloadEntity> getAllDownloadEntity() {
return DbEntity.findAllData(DownloadEntity.class);
}
/** /**
* 设置命令 * 设置命令
*/ */
@ -123,6 +118,17 @@ import java.util.Map;
return (receiver instanceof DownloadReceiver) ? (DownloadReceiver) receiver : null; return (receiver instanceof DownloadReceiver) ? (DownloadReceiver) receiver : null;
} }
/**
* 处理上传操作
*/
UploadReceiver upload(Object obj) {
IReceiver receiver = mReceivers.get(getKey(false, obj));
if (receiver == null) {
receiver = putReceiver(true, obj);
}
return (receiver instanceof UploadReceiver) ? (UploadReceiver) receiver : null;
}
/** /**
* 设置CA证书信息 * 设置CA证书信息
* *
@ -205,7 +211,8 @@ import java.util.Map;
receiver = dReceiver; receiver = dReceiver;
} else { } else {
UploadReceiver uReceiver = new UploadReceiver(); UploadReceiver uReceiver = new UploadReceiver();
uReceiver.targetName = obj.getClass().getName();
mReceivers.put(key, uReceiver);
receiver = uReceiver; receiver = uReceiver;
} }
} }

@ -22,6 +22,7 @@ import com.arialyy.aria.core.command.CmdFactory;
import com.arialyy.aria.core.command.AbsCmd; 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.orm.DbEntity;
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;
@ -32,10 +33,10 @@ import java.util.Set;
* Created by lyy on 2016/12/5. * Created by lyy on 2016/12/5.
* 下载功能接收器 * 下载功能接收器
*/ */
public class DownloadReceiver implements IReceiver { public class DownloadReceiver implements IReceiver<DownloadEntity> {
private static final String TAG = "DownloadReceiver"; private static final String TAG = "DownloadReceiver";
public String targetName; public String targetName;
public OnSchedulerListener listener; public OnSchedulerListener<DownloadTask> listener;
/** /**
* {@link #load(String)}请使用该方法 * {@link #load(String)}请使用该方法
@ -61,7 +62,7 @@ public class DownloadReceiver implements IReceiver {
/** /**
* 添加调度器回调 * 添加调度器回调
*/ */
public DownloadReceiver addSchedulerListener(OnSchedulerListener listener) { public DownloadReceiver addSchedulerListener(OnSchedulerListener<DownloadTask> listener) {
this.listener = listener; this.listener = listener;
DownloadSchedulers.getInstance().addSchedulerListener(targetName, listener); DownloadSchedulers.getInstance().addSchedulerListener(targetName, listener);
return this; return this;
@ -81,13 +82,6 @@ public class DownloadReceiver implements IReceiver {
listener = null; listener = null;
} }
/**
* 获取下载列表
*/
public List<DownloadEntity> getDownloadList() {
return DownloadEntity.findAllData(DownloadEntity.class);
}
/** /**
* 通过下载链接获取下载实体 * 通过下载链接获取下载实体
*/ */
@ -99,20 +93,24 @@ public class DownloadReceiver implements IReceiver {
/** /**
* 下载任务是否存在 * 下载任务是否存在
*/ */
public boolean taskExists(String downloadUrl) { @Override public boolean taskExists(String downloadUrl) {
return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl) != null; return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl) != null;
} }
@Override public List<DownloadEntity> getTaskList() {
return DownloadEntity.findAllData(DownloadEntity.class);
}
/** /**
* 停止所有正在下载的任务 * 停止所有正在下载的任务
*/ */
public void stopAllTask() { @Override 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 = DbEntity.findAllData(DownloadEntity.class);
List<AbsCmd> 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.createCmd(targetName, new DownloadTaskEntity(entity),
CmdFactory.TASK_STOP)); CmdFactory.TASK_STOP));
} }
} }
@ -122,12 +120,12 @@ public class DownloadReceiver implements IReceiver {
/** /**
* 删除所有任务 * 删除所有任务
*/ */
public void cancelAllTask() { @Override public void removeAllTask() {
final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP); final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP);
List<DownloadEntity> allEntity = ariaManager.getAllDownloadEntity(); List<DownloadEntity> allEntity = DbEntity.findAllData(DownloadEntity.class);
List<AbsCmd> 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.createCmd(targetName, new DownloadTaskEntity(entity),
CmdFactory.TASK_CANCEL)); CmdFactory.TASK_CANCEL));
} }
ariaManager.setCmds(cancelCmds).exe(); ariaManager.setCmds(cancelCmds).exe();

@ -17,27 +17,19 @@ package com.arialyy.aria.core.download;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.text.TextUtils; import android.text.TextUtils;
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.inf.AbsTarget;
import com.arialyy.aria.core.command.AbsCmd; import com.arialyy.aria.core.queue.DownloadTaskQueue;
import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
/** /**
* Created by lyy on 2016/12/5. * Created by lyy on 2016/12/5.
* https://github.com/AriaLyy/Aria * https://github.com/AriaLyy/Aria
*/ */
public class DownloadTarget { public class DownloadTarget extends AbsTarget<DownloadEntity, DownloadTaskEntity> {
DownloadEntity entity;
String targetName;
DownloadTaskEntity taskEntity;
public DownloadTarget(DownloadEntity entity, String targetName) { DownloadTarget(DownloadEntity entity, String targetName) {
this.entity = entity; this.entity = entity;
this.targetName = targetName; this.targetName = targetName;
taskEntity = new DownloadTaskEntity(entity); taskEntity = new DownloadTaskEntity(entity);
@ -50,7 +42,7 @@ public class DownloadTarget {
* @param header 头部value * @param header 头部value
*/ */
public DownloadTarget addHeader(@NonNull String key, @NonNull String header) { public DownloadTarget addHeader(@NonNull String key, @NonNull String header) {
taskEntity.headers.put(key, header); super._addHeader(key, header);
return this; return this;
} }
@ -60,12 +52,17 @@ public class DownloadTarget {
* @param headers Map<Key, Value> * @param headers Map<Key, Value>
*/ */
public DownloadTarget addHeaders(Map<String, String> headers) { public DownloadTarget addHeaders(Map<String, String> headers) {
if (headers != null && headers.size() > 0) { super._addHeaders(headers);
Set<String> keys = headers.keySet(); return this;
for (String key : keys) { }
taskEntity.headers.put(key, headers.get(key));
} /**
} * 设置请求类型
*
* @param requestEnum {@link RequestEnum}
*/
public DownloadTarget setRequestMode(RequestEnum requestEnum) {
super._setRequestMode(requestEnum);
return this; return this;
} }
@ -80,16 +77,6 @@ public class DownloadTarget {
return this; return this;
} }
/**
* 设置请求类型
*
* @param requestEnum {@link RequestEnum}
*/
public DownloadTarget setRequestMode(RequestEnum requestEnum) {
taskEntity.requestEnum = requestEnum;
return this;
}
/** /**
* 设置文件名 * 设置文件名
*/ */
@ -101,92 +88,16 @@ public class DownloadTarget {
return this; return this;
} }
/**
* 获取下载文件大小
*/
public long getFileSize() {
DownloadEntity entity = getDownloadEntity(this.entity.getDownloadUrl());
if (entity == null) {
throw new NullPointerException("下载管理器中没有改任务");
}
return entity.getFileSize();
}
/**
* 获取当前下载进度如果下載实体存在则返回当前进度
*/
public long getCurrentProgress() {
DownloadEntity entity = getDownloadEntity(this.entity.getDownloadUrl());
if (entity == null) {
throw new NullPointerException("下载管理器中没有改任务");
}
return entity.getCurrentProgress();
}
private DownloadEntity getDownloadEntity(String downloadUrl) { private DownloadEntity getDownloadEntity(String downloadUrl) {
CheckUtil.checkDownloadUrl(downloadUrl); CheckUtil.checkDownloadUrl(downloadUrl);
return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl); return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
} }
/**
* 添加任务
*/
public void add() {
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createDownloadCmd(targetName, taskEntity, CmdFactory.TASK_CREATE))
.exe();
}
/**
* 开始下载
*/
public void start() {
List<AbsCmd> cmds = new ArrayList<>();
cmds.add(CommonUtil.createDownloadCmd(targetName, taskEntity, CmdFactory.TASK_CREATE));
cmds.add(CommonUtil.createDownloadCmd(targetName, taskEntity, CmdFactory.TASK_START));
AriaManager.getInstance(AriaManager.APP).setCmds(cmds).exe();
cmds.clear();
}
/**
* 停止下载
*/
public void stop() {
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createDownloadCmd(targetName, taskEntity, CmdFactory.TASK_STOP))
.exe();
}
/**
* 恢复下载
*/
public void resume() {
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createDownloadCmd(targetName, taskEntity, CmdFactory.TASK_START))
.exe();
}
/**
* 取消下载
*/
public void cancel() {
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createDownloadCmd(targetName, taskEntity, CmdFactory.TASK_CANCEL))
.exe();
}
/** /**
* 是否在下载 * 是否在下载
*/ */
public boolean isDownloading() { public boolean isDownloading() {
return AriaManager.getInstance(AriaManager.APP).getDownloadTaskQueue().getTask(entity).isDownloading(); return DownloadTaskQueue.getInstance().getTask(entity).isRunning();
}
/**
* 重新下载
*/
public void reStart() {
cancel();
start();
} }
} }

@ -15,21 +15,16 @@
*/ */
package com.arialyy.aria.core.download; package com.arialyy.aria.core.download;
import com.arialyy.aria.core.RequestEnum;
import com.arialyy.aria.core.inf.IEntity; 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.Map;
/** /**
* Created by Aria.Lao on 2017/1/23. * Created by Aria.Lao on 2017/1/23.
* 下载任务实体 * 下载任务实体
*/ */
public class DownloadTaskEntity implements ITaskEntity { public class DownloadTaskEntity extends ITaskEntity {
public DownloadEntity downloadEntity; public DownloadEntity downloadEntity;
public RequestEnum requestEnum = RequestEnum.GET;
public Map<String, String> headers = new HashMap<>();
public DownloadTaskEntity(DownloadEntity downloadEntity) { public DownloadTaskEntity(DownloadEntity downloadEntity) {
this.downloadEntity = downloadEntity; this.downloadEntity = downloadEntity;

@ -0,0 +1,175 @@
/*
* 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;
import android.support.annotation.NonNull;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.RequestEnum;
import com.arialyy.aria.core.command.AbsCmd;
import com.arialyy.aria.core.command.CmdFactory;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CommonUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Created by Aria.Lao on 2017/2/28.
*/
public class AbsTarget<ENTITY extends IEntity, TASK_ENTITY extends ITaskEntity> {
protected ENTITY entity;
protected TASK_ENTITY taskEntity;
protected String targetName;
/**
* 获取任务文件大小
*
* @return -1没有找到该任务
*/
public long getFileSize() {
if (entity instanceof DownloadEntity) {
DownloadEntity entity = DbEntity.findData(DownloadEntity.class, "downloadUrl=?",
((DownloadEntity) this.entity).getDownloadUrl());
if (entity == null) {
throw new NullPointerException("没有找到该任务");
}
return entity.getFileSize();
} else if (entity instanceof UploadEntity) {
UploadEntity entity = DbEntity.findData(UploadEntity.class, "filePath=?",
((UploadEntity) this.entity).getFilePath());
if (entity == null) {
throw new NullPointerException("没有找到该任务");
}
return entity.getFileSize();
}
return -1;
}
/**
* 获取当前任务进度如果任务存在则返回当前进度
*
* @return -1没有找到该任务
*/
public long getCurrentProgress() {
if (entity instanceof DownloadEntity) {
DownloadEntity entity = DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?",
((DownloadEntity) this.entity).getDownloadUrl());
if (entity == null) {
throw new NullPointerException("下载管理器中没有该任务");
}
return entity.getCurrentProgress();
} else if (entity instanceof UploadEntity) {
UploadEntity entity = DbEntity.findData(UploadEntity.class, "filePath=?",
((UploadEntity) this.entity).getFilePath());
if (entity == null) {
throw new NullPointerException("没有找到该任务");
}
return entity.getCurrentProgress();
}
return -1;
}
/**
* 给url请求添加头部
*
* @param key 头部key
* @param header 头部value
*/
protected void _addHeader(@NonNull String key, @NonNull String header) {
taskEntity.headers.put(key, header);
}
/**
* 给url请求添加头部
*
* @param headers Map<Key, Value>
*/
protected void _addHeaders(Map<String, String> headers) {
if (headers != null && headers.size() > 0) {
Set<String> keys = headers.keySet();
for (String key : keys) {
taskEntity.headers.put(key, headers.get(key));
}
}
}
/**
* 设置请求类型
*
* @param requestEnum {@link RequestEnum}
*/
protected void _setRequestMode(RequestEnum requestEnum) {
taskEntity.requestEnum = requestEnum;
}
/**
* 添加任务
*/
public void add() {
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createCmd(targetName, taskEntity, CmdFactory.TASK_CREATE))
.exe();
}
/**
* 开始下载
*/
public void start() {
List<AbsCmd> cmds = new ArrayList<>();
cmds.add(CommonUtil.createCmd(targetName, taskEntity, CmdFactory.TASK_CREATE));
cmds.add(CommonUtil.createCmd(targetName, taskEntity, CmdFactory.TASK_START));
AriaManager.getInstance(AriaManager.APP).setCmds(cmds).exe();
cmds.clear();
}
/**
* 停止下载
*/
public void stop() {
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createCmd(targetName, taskEntity, CmdFactory.TASK_STOP))
.exe();
}
/**
* 恢复下载
*/
public void resume() {
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createCmd(targetName, taskEntity, CmdFactory.TASK_START))
.exe();
}
/**
* 取消下载
*/
public void cancel() {
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createCmd(targetName, taskEntity, CmdFactory.TASK_CANCEL))
.exe();
}
/**
* 重新下载
*/
public void reStart() {
cancel();
start();
}
}

@ -15,10 +15,12 @@
*/ */
package com.arialyy.aria.core.inf; package com.arialyy.aria.core.inf;
import java.util.List;
/** /**
* Created by Aria.Lao on 2017/2/6. * Created by Aria.Lao on 2017/2/6.
*/ */
public interface IReceiver { public interface IReceiver<ENTITY extends IEntity> {
/** /**
* Receiver 销毁 * Receiver 销毁
*/ */
@ -28,4 +30,26 @@ public interface IReceiver {
* 移除事件回调 * 移除事件回调
*/ */
public void removeSchedulerListener(); public void removeSchedulerListener();
/**
* 停止所有任务
*/
public void stopAllTask();
/**
* 删除所有任务
*/
public void removeAllTask();
/**
* 任务是否存在
*
* @param key 下载时为下载路径上传时为文件路径
*/
public boolean taskExists(String key);
/**
* 获取任务列表
*/
public List<ENTITY> getTaskList();
} }

@ -15,12 +15,24 @@
*/ */
package com.arialyy.aria.core.inf; package com.arialyy.aria.core.inf;
import com.arialyy.aria.core.RequestEnum;
import java.util.HashMap;
import java.util.Map;
/** /**
* Created by Aria.Lao on 2017/2/23. * Created by Aria.Lao on 2017/2/23.
*/ */
public interface ITaskEntity { public abstract class ITaskEntity {
/**
* http 请求头
*/
public Map<String, String> headers = new HashMap<>();
public IEntity getEntity(); /**
* 网络请求类型
*/
public RequestEnum requestEnum = RequestEnum.GET;
public abstract IEntity getEntity();
} }

@ -18,8 +18,6 @@ package com.arialyy.aria.core.scheduler;
import android.os.CountDownTimer; import android.os.CountDownTimer;
import android.os.Message; import android.os.Message;
import android.util.Log; import android.util.Log;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.queue.UploadTaskQueue; import com.arialyy.aria.core.queue.UploadTaskQueue;
import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.core.upload.UploadEntity;

@ -15,19 +15,110 @@
*/ */
package com.arialyy.aria.core.upload; package com.arialyy.aria.core.upload;
import android.support.annotation.NonNull;
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.inf.IEntity;
import com.arialyy.aria.core.inf.IReceiver; import com.arialyy.aria.core.inf.IReceiver;
import com.arialyy.aria.core.scheduler.OnSchedulerListener;
import com.arialyy.aria.core.scheduler.UploadSchedulers;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/** /**
* Created by Aria.Lao on 2017/2/6. * Created by Aria.Lao on 2017/2/6.
* 上传功能接收器 * 上传功能接收器
*/ */
public class UploadReceiver implements IReceiver { public class UploadReceiver implements IReceiver<UploadEntity> {
private static final String TAG = "DownloadReceiver";
public String targetName;
public OnSchedulerListener<UploadTask> listener;
/**
* 加载任务
*
* @param filePath 文件地址
*/
public UploadTarget load(@NonNull String filePath) {
CheckUtil.checkUploadPath(filePath);
UploadEntity entity = UploadEntity.findData(UploadEntity.class, "filePath=?", filePath);
if (entity == null) {
entity = new UploadEntity();
}
entity.setFilePath(filePath);
return new UploadTarget(entity, targetName);
}
/**
* 通过上传路径获取上传实体
*/
public UploadEntity getUploadEntity(String filePath) {
CheckUtil.checkUploadPath(filePath);
return DbEntity.findData(UploadEntity.class, "filePath=?", filePath);
}
/**
* 下载任务是否存在
*/
@Override public boolean taskExists(String filePath) {
return DbEntity.findData(UploadEntity.class, "filePath=?", filePath) != null;
}
@Override public List<UploadEntity> getTaskList() {
return DbEntity.findAllData(UploadEntity.class);
}
@Override public void stopAllTask() {
List<UploadEntity> allEntity = DbEntity.findAllData(UploadEntity.class);
List<AbsCmd> stopCmds = new ArrayList<>();
for (UploadEntity entity : allEntity) {
if (entity.getState() == IEntity.STATE_RUNNING) {
stopCmds.add(
CommonUtil.createCmd(targetName, new UploadTaskEntity(entity), CmdFactory.TASK_STOP));
}
}
AriaManager.getInstance(AriaManager.APP).setCmds(stopCmds).exe();
}
@Override public void removeAllTask() {
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();
Set<String> keys = am.getReceiver().keySet();
for (String key : keys) {
IReceiver receiver = am.getReceiver().get(key);
receiver.removeSchedulerListener();
am.getReceiver().remove(key);
}
}
@Override public void destroy() { @Override public void destroy() {
targetName = null;
listener = null;
}
/**
* 添加调度器回调
*/
public UploadReceiver addSchedulerListener(OnSchedulerListener<UploadTask> listener) {
this.listener = listener;
UploadSchedulers.getInstance().addSchedulerListener(targetName, listener);
return this;
} }
@Override public void removeSchedulerListener() { @Override public void removeSchedulerListener() {
if (listener != null) {
UploadSchedulers.getInstance().removeSchedulerListener(targetName, listener);
}
} }
} }

@ -0,0 +1,115 @@
/*
* 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.upload;
import android.support.annotation.NonNull;
import com.arialyy.aria.core.RequestEnum;
import com.arialyy.aria.core.inf.AbsTarget;
import com.arialyy.aria.core.queue.UploadTaskQueue;
import com.arialyy.aria.orm.DbEntity;
import java.util.Map;
/**
* Created by Aria.Lao on 2017/2/28.
*/
public class UploadTarget extends AbsTarget<UploadEntity, UploadTaskEntity> {
UploadTarget(UploadEntity entity, String targetName) {
this.entity = entity;
this.targetName = targetName;
taskEntity = new UploadTaskEntity(entity);
}
/**
* 设置上传路径
*
* @param uploadUrl 上传路径
*/
public UploadTarget setUploadUrl(@NonNull String uploadUrl) {
taskEntity.uploadUrl = uploadUrl;
return this;
}
/**
* 设置服务器需要的附件key
*
* @param attachment 附件key
*/
public UploadTarget setAttachment(@NonNull String attachment) {
taskEntity.attachment = attachment;
return this;
}
/**
* 设置文件名
*/
public UploadTarget setFileName(String fileName) {
entity.setFileName(fileName);
return this;
}
/**
* 设置上传文件类型
*
* @param contentType <code>"multipart/form-data"<code/>
*/
public UploadTarget setContentType(String contentType) {
taskEntity.contentType = contentType;
return this;
}
/**
* 给url请求添加头部
*
* @param key 头部key
* @param header 头部value
*/
public UploadTarget addHeader(@NonNull String key, @NonNull String header) {
super._addHeader(key, header);
return this;
}
/**
* 给url请求添加头部
*
* @param headers Map<Key, Value>
*/
public UploadTarget addHeaders(Map<String, String> headers) {
super._addHeaders(headers);
return this;
}
/**
* 设置请求类型
*
* @param requestEnum {@link RequestEnum}
*/
public UploadTarget setRequestMode(RequestEnum requestEnum) {
super._setRequestMode(requestEnum);
return this;
}
private UploadEntity getDownloadEntity(@NonNull String filePath) {
return DbEntity.findData(UploadEntity.class, "filePath=?", filePath);
}
/**
* 是否在下载
*/
public boolean isUploading() {
return UploadTaskQueue.getInstance().getTask(entity).isRunning();
}
}

@ -1,6 +1,20 @@
/*
* 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.upload; package com.arialyy.aria.core.upload;
import com.arialyy.aria.core.RequestEnum;
import com.arialyy.aria.core.inf.IEntity; 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;
@ -8,19 +22,15 @@ import java.util.Map;
/** /**
* Created by Aria.Lao on 2017/2/9. * Created by Aria.Lao on 2017/2/9.
* 上传任务实体
*/ */
public class UploadTaskEntity extends ITaskEntity {
public class UploadTaskEntity implements ITaskEntity {
public UploadEntity uploadEntity; public UploadEntity uploadEntity;
public RequestEnum requestEnum = RequestEnum.GET;
public String uploadUrl; //上传路径 public String uploadUrl; //上传路径
public String attachment; //文件上传需要的key public String attachment; //文件上传需要的key
public String contentType = "multipart/form-data"; //上传的文件类型 public String contentType = "multipart/form-data"; //上传的文件类型
public String charset = "utf-8"; public String charset = "utf-8";
/**
* http 请求头
*/
public Map<String, String> headers = new HashMap<>();
/** /**
* 文件上传表单 * 文件上传表单
*/ */

@ -77,6 +77,13 @@ public class CheckUtil {
if (TextUtils.isEmpty(downloadUrl)) throw new IllegalArgumentException("下载链接不能为null"); if (TextUtils.isEmpty(downloadUrl)) throw new IllegalArgumentException("下载链接不能为null");
} }
/**
* 检测上传地址是否为null
*/
public static void checkUploadPath(String uploadPath) {
if (TextUtils.isEmpty(uploadPath)) throw new IllegalArgumentException("上传地址不能为null");
}
/** /**
* 检查任务实体 * 检查任务实体
*/ */

@ -40,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> AbsCmd createDownloadCmd(String target, T entity, int cmd) { public static <T extends ITaskEntity> AbsCmd createCmd(String target, T entity, int cmd) {
return CmdFactory.getInstance().createCmd(target, entity, cmd); return CmdFactory.getInstance().createCmd(target, entity, cmd);
} }

@ -42,26 +42,26 @@ import com.arialyy.simple.databinding.ActivitySingleBinding;
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> { public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
public static final int DOWNLOAD_PRE = 0x01; public static final int DOWNLOAD_PRE = 0x01;
public static final int DOWNLOAD_STOP = 0x02; public static final int DOWNLOAD_STOP = 0x02;
public static final int DOWNLOAD_FAILE = 0x03; public static final int DOWNLOAD_FAILE = 0x03;
public static final int DOWNLOAD_CANCEL = 0x04; public static final int DOWNLOAD_CANCEL = 0x04;
public static final int DOWNLOAD_RESUME = 0x05; public static final int DOWNLOAD_RESUME = 0x05;
public static final int DOWNLOAD_COMPLETE = 0x06; public static final int DOWNLOAD_COMPLETE = 0x06;
public static final int DOWNLOAD_RUNNING = 0x07; public static final int DOWNLOAD_RUNNING = 0x07;
private static final String DOWNLOAD_URL = private static final String DOWNLOAD_URL =
//"http://kotlinlang.org/docs/kotlin-docs.pdf"; //"http://kotlinlang.org/docs/kotlin-docs.pdf";
"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe"; "https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe";
@Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb; @Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb;
@Bind(R.id.start) Button mStart; @Bind(R.id.start) Button mStart;
@Bind(R.id.stop) Button mStop; @Bind(R.id.stop) Button mStop;
@Bind(R.id.cancel) Button mCancel; @Bind(R.id.cancel) Button mCancel;
@Bind(R.id.size) TextView mSize; @Bind(R.id.size) TextView mSize;
@Bind(R.id.toolbar) Toolbar toolbar; @Bind(R.id.toolbar) Toolbar toolbar;
@Bind(R.id.speed) TextView mSpeed; @Bind(R.id.speed) TextView mSpeed;
@Bind(R.id.img) ImageView mImg; @Bind(R.id.img) ImageView mImg;
private DownloadEntity mEntity; private DownloadEntity mEntity;
private BroadcastReceiver mReceiver = new BroadcastReceiver() { private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override public void onReceive(Context context, Intent intent) { @Override public void onReceive(Context context, Intent intent) {
String action = intent.getAction(); String action = intent.getAction();
@ -163,7 +163,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
private void init() { private void init() {
if (Aria.download(this).taskExists(DOWNLOAD_URL)) { if (Aria.download(this).taskExists(DOWNLOAD_URL)) {
DownloadTarget target = Aria.download(this).load(DOWNLOAD_URL); DownloadTarget target = Aria.download(this).load(DOWNLOAD_URL);
int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize()); int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize());
mPb.setProgress(p); mPb.setProgress(p);
} }
} }

Loading…
Cancel
Save