diff --git a/Aria/src/main/java/com/arialyy/aria/core/Aria.java b/Aria/src/main/java/com/arialyy/aria/core/Aria.java
index 5272b2a8..99ab3d33 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/Aria.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/Aria.java
@@ -29,6 +29,7 @@ import android.widget.PopupWindow;
import com.arialyy.aria.core.download.DownloadReceiver;
import com.arialyy.aria.core.scheduler.OnSchedulerListener;
import com.arialyy.aria.core.download.DownloadTask;
+import com.arialyy.aria.core.upload.UploadReceiver;
import com.arialyy.aria.core.upload.UploadTask;
/**
@@ -37,7 +38,7 @@ import com.arialyy.aria.core.upload.UploadTask;
* Aria启动,管理全局任务
*
*
- * //启动下载
+ * //下载
* Aria.download(this)
* .load(DOWNLOAD_URL) //下载地址,必填
* //文件保存路径,必填
@@ -45,6 +46,15 @@ import com.arialyy.aria.core.upload.UploadTask;
* .setDownloadName("test.apk") //文件名,必填
* .start();
*
+ *
+ * //上传
+ * Aria.upload(this)
+ * .load(filePath) //文件路径,必填
+ * .setUploadUrl(uploadUrl) //上传路径,必填
+ * .setFileName(fileName) //文件名
+ * .setAttachment(fileKey) //服务器读取文件的key
+ * .start();
+ *
*
*/
@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);
}
+ /**
+ * 初始化上传
+ *
+ * @param obj 支持类型有【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】
+ */
+ public static UploadReceiver upload(Object obj) {
+ return get(obj).upload(obj);
+ }
+
/**
* 处理通用事件
*
diff --git a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
index 6dbfb75b..db0e4cd4 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
@@ -57,13 +57,12 @@ import java.util.Map;
private static final Object LOCK = new Object();
@SuppressLint("StaticFieldLeak") private static volatile AriaManager INSTANCE = null;
private Map mReceivers = new HashMap<>();
-
public static Context APP;
private List mCommands = new ArrayList<>();
private AriaManager(Context context) {
DbUtil.init(context.getApplicationContext());
- APP = context;
+ APP = context.getApplicationContext();
regAppLifeCallback(context);
}
@@ -80,10 +79,6 @@ import java.util.Map;
return mReceivers;
}
- public List getAllDownloadEntity() {
- return DbEntity.findAllData(DownloadEntity.class);
- }
-
/**
* 设置命令
*/
@@ -123,6 +118,17 @@ import java.util.Map;
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证书信息
*
@@ -205,7 +211,8 @@ import java.util.Map;
receiver = dReceiver;
} else {
UploadReceiver uReceiver = new UploadReceiver();
-
+ uReceiver.targetName = obj.getClass().getName();
+ mReceivers.put(key, uReceiver);
receiver = uReceiver;
}
}
diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
index 0bf74b62..508949d1 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadReceiver.java
@@ -22,6 +22,7 @@ import com.arialyy.aria.core.command.CmdFactory;
import com.arialyy.aria.core.command.AbsCmd;
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
import com.arialyy.aria.core.scheduler.OnSchedulerListener;
+import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
import java.util.ArrayList;
@@ -32,10 +33,10 @@ import java.util.Set;
* Created by lyy on 2016/12/5.
* 下载功能接收器
*/
-public class DownloadReceiver implements IReceiver {
+public class DownloadReceiver implements IReceiver {
private static final String TAG = "DownloadReceiver";
public String targetName;
- public OnSchedulerListener listener;
+ public OnSchedulerListener listener;
/**
* {@link #load(String)},请使用该方法
@@ -61,7 +62,7 @@ public class DownloadReceiver implements IReceiver {
/**
* 添加调度器回调
*/
- public DownloadReceiver addSchedulerListener(OnSchedulerListener listener) {
+ public DownloadReceiver addSchedulerListener(OnSchedulerListener listener) {
this.listener = listener;
DownloadSchedulers.getInstance().addSchedulerListener(targetName, listener);
return this;
@@ -81,13 +82,6 @@ public class DownloadReceiver implements IReceiver {
listener = null;
}
- /**
- * 获取下载列表
- */
- public List 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;
}
+ @Override public List getTaskList() {
+ return DownloadEntity.findAllData(DownloadEntity.class);
+ }
+
/**
* 停止所有正在下载的任务
*/
- public void stopAllTask() {
+ @Override public void stopAllTask() {
final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP);
- List allEntity = ariaManager.getAllDownloadEntity();
+ List allEntity = DbEntity.findAllData(DownloadEntity.class);
List stopCmds = new ArrayList<>();
for (DownloadEntity entity : allEntity) {
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));
}
}
@@ -122,12 +120,12 @@ public class DownloadReceiver implements IReceiver {
/**
* 删除所有任务
*/
- public void cancelAllTask() {
+ @Override public void removeAllTask() {
final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP);
- List allEntity = ariaManager.getAllDownloadEntity();
+ List allEntity = DbEntity.findAllData(DownloadEntity.class);
List cancelCmds = new ArrayList<>();
for (DownloadEntity entity : allEntity) {
- cancelCmds.add(CommonUtil.createDownloadCmd(targetName, new DownloadTaskEntity(entity),
+ cancelCmds.add(CommonUtil.createCmd(targetName, new DownloadTaskEntity(entity),
CmdFactory.TASK_CANCEL));
}
ariaManager.setCmds(cancelCmds).exe();
diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java
index 25f19e10..e50738f8 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java
@@ -17,27 +17,19 @@ package com.arialyy.aria.core.download;
import android.support.annotation.NonNull;
import android.text.TextUtils;
-import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.RequestEnum;
-import com.arialyy.aria.core.command.CmdFactory;
-import com.arialyy.aria.core.command.AbsCmd;
+import com.arialyy.aria.core.inf.AbsTarget;
+import com.arialyy.aria.core.queue.DownloadTaskQueue;
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.Set;
/**
* Created by lyy on 2016/12/5.
* https://github.com/AriaLyy/Aria
*/
-public class DownloadTarget {
- DownloadEntity entity;
- String targetName;
- DownloadTaskEntity taskEntity;
+public class DownloadTarget extends AbsTarget {
- public DownloadTarget(DownloadEntity entity, String targetName) {
+ DownloadTarget(DownloadEntity entity, String targetName) {
this.entity = entity;
this.targetName = targetName;
taskEntity = new DownloadTaskEntity(entity);
@@ -50,7 +42,7 @@ public class DownloadTarget {
* @param header 头部value
*/
public DownloadTarget addHeader(@NonNull String key, @NonNull String header) {
- taskEntity.headers.put(key, header);
+ super._addHeader(key, header);
return this;
}
@@ -60,12 +52,17 @@ public class DownloadTarget {
* @param headers Map
*/
public DownloadTarget addHeaders(Map headers) {
- if (headers != null && headers.size() > 0) {
- Set keys = headers.keySet();
- for (String key : keys) {
- taskEntity.headers.put(key, headers.get(key));
- }
- }
+ super._addHeaders(headers);
+ return this;
+ }
+
+ /**
+ * 设置请求类型
+ *
+ * @param requestEnum {@link RequestEnum}
+ */
+ public DownloadTarget setRequestMode(RequestEnum requestEnum) {
+ super._setRequestMode(requestEnum);
return this;
}
@@ -80,16 +77,6 @@ public class DownloadTarget {
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;
}
- /**
- * 获取下载文件大小
- */
- 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) {
CheckUtil.checkDownloadUrl(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 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() {
- return AriaManager.getInstance(AriaManager.APP).getDownloadTaskQueue().getTask(entity).isDownloading();
- }
-
- /**
- * 重新下载
- */
- public void reStart() {
- cancel();
- start();
+ return DownloadTaskQueue.getInstance().getTask(entity).isRunning();
}
}
diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java
index c5a61b92..16cc1d93 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTaskEntity.java
@@ -15,21 +15,16 @@
*/
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.ITaskEntity;
-import java.util.HashMap;
-import java.util.Map;
/**
* Created by Aria.Lao on 2017/1/23.
* 下载任务实体
*/
-public class DownloadTaskEntity implements ITaskEntity {
+public class DownloadTaskEntity extends ITaskEntity {
public DownloadEntity downloadEntity;
- public RequestEnum requestEnum = RequestEnum.GET;
- public Map headers = new HashMap<>();
public DownloadTaskEntity(DownloadEntity downloadEntity) {
this.downloadEntity = downloadEntity;
diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java
new file mode 100644
index 00000000..be0ca5fa
--- /dev/null
+++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java
@@ -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 {
+ 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
+ */
+ protected void _addHeaders(Map headers) {
+ if (headers != null && headers.size() > 0) {
+ Set 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 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();
+ }
+}
diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/IReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/inf/IReceiver.java
index 8ead3163..28c5de6f 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/inf/IReceiver.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/inf/IReceiver.java
@@ -15,10 +15,12 @@
*/
package com.arialyy.aria.core.inf;
+import java.util.List;
+
/**
* Created by Aria.Lao on 2017/2/6.
*/
-public interface IReceiver {
+public interface IReceiver {
/**
* Receiver 销毁
*/
@@ -28,4 +30,26 @@ public interface IReceiver {
* 移除事件回调
*/
public void removeSchedulerListener();
+
+ /**
+ * 停止所有任务
+ */
+ public void stopAllTask();
+
+ /**
+ * 删除所有任务
+ */
+ public void removeAllTask();
+
+ /**
+ * 任务是否存在
+ *
+ * @param key 下载时为下载路径,上传时为文件路径
+ */
+ public boolean taskExists(String key);
+
+ /**
+ * 获取任务列表
+ */
+ public List getTaskList();
}
diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/ITaskEntity.java b/Aria/src/main/java/com/arialyy/aria/core/inf/ITaskEntity.java
index 0780b0bc..e9f621a2 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/inf/ITaskEntity.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/inf/ITaskEntity.java
@@ -15,12 +15,24 @@
*/
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.
*/
-public interface ITaskEntity {
+public abstract class ITaskEntity {
+ /**
+ * http 请求头
+ */
+ public Map headers = new HashMap<>();
- public IEntity getEntity();
+ /**
+ * 网络请求类型
+ */
+ public RequestEnum requestEnum = RequestEnum.GET;
+ public abstract IEntity getEntity();
}
diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java
index 86930b68..81bd6cbf 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java
@@ -18,8 +18,6 @@ package com.arialyy.aria.core.scheduler;
import android.os.CountDownTimer;
import android.os.Message;
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.queue.UploadTaskQueue;
import com.arialyy.aria.core.upload.UploadEntity;
diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java
index aa012d22..4ebee3c7 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java
@@ -15,19 +15,110 @@
*/
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.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.
* 上传功能接收器
*/
-public class UploadReceiver implements IReceiver {
+public class UploadReceiver implements IReceiver {
+ private static final String TAG = "DownloadReceiver";
+ public String targetName;
+ public OnSchedulerListener 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 getTaskList() {
+ return DbEntity.findAllData(UploadEntity.class);
+ }
+
+ @Override public void stopAllTask() {
+ List allEntity = DbEntity.findAllData(UploadEntity.class);
+ List 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 allEntity = DbEntity.findAllData(UploadEntity.class);
+ List cancelCmds = new ArrayList<>();
+ for (UploadEntity entity : allEntity) {
+ cancelCmds.add(
+ CommonUtil.createCmd(targetName, new UploadTaskEntity(entity), CmdFactory.TASK_CANCEL));
+ }
+ am.setCmds(cancelCmds).exe();
+ Set keys = am.getReceiver().keySet();
+ for (String key : keys) {
+ IReceiver receiver = am.getReceiver().get(key);
+ receiver.removeSchedulerListener();
+ am.getReceiver().remove(key);
+ }
+ }
@Override public void destroy() {
+ targetName = null;
+ listener = null;
+ }
+ /**
+ * 添加调度器回调
+ */
+ public UploadReceiver addSchedulerListener(OnSchedulerListener listener) {
+ this.listener = listener;
+ UploadSchedulers.getInstance().addSchedulerListener(targetName, listener);
+ return this;
}
@Override public void removeSchedulerListener() {
-
+ if (listener != null) {
+ UploadSchedulers.getInstance().removeSchedulerListener(targetName, listener);
+ }
}
}
\ No newline at end of file
diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java
new file mode 100644
index 00000000..4ecd1ad1
--- /dev/null
+++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java
@@ -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 {
+
+ 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 "multipart/form-data"
+ */
+ 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
+ */
+ public UploadTarget addHeaders(Map 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();
+ }
+}
diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java
index 030789ea..50e7a013 100644
--- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java
+++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java
@@ -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;
-import com.arialyy.aria.core.RequestEnum;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.ITaskEntity;
import java.util.HashMap;
@@ -8,19 +22,15 @@ import java.util.Map;
/**
* Created by Aria.Lao on 2017/2/9.
+ * 上传任务实体
*/
-
-public class UploadTaskEntity implements ITaskEntity {
+public class UploadTaskEntity extends ITaskEntity {
public UploadEntity uploadEntity;
- public RequestEnum requestEnum = RequestEnum.GET;
public String uploadUrl; //上传路径
public String attachment; //文件上传需要的key
public String contentType = "multipart/form-data"; //上传的文件类型
public String charset = "utf-8";
- /**
- * http 请求头
- */
- public Map headers = new HashMap<>();
+
/**
* 文件上传表单
*/
diff --git a/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java b/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java
index 3f9a6745..901282fc 100644
--- a/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java
+++ b/Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java
@@ -77,6 +77,13 @@ public class CheckUtil {
if (TextUtils.isEmpty(downloadUrl)) throw new IllegalArgumentException("下载链接不能为null");
}
+ /**
+ * 检测上传地址是否为null
+ */
+ public static void checkUploadPath(String uploadPath) {
+ if (TextUtils.isEmpty(uploadPath)) throw new IllegalArgumentException("上传地址不能为null");
+ }
+
/**
* 检查任务实体
*/
diff --git a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
index c15260e5..be37e2dd 100644
--- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
+++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java
@@ -40,7 +40,7 @@ import java.util.Properties;
public class CommonUtil {
private static final String TAG = "util";
- public static AbsCmd createDownloadCmd(String target, T entity, int cmd) {
+ public static AbsCmd createCmd(String target, T entity, int cmd) {
return CmdFactory.getInstance().createCmd(target, entity, cmd);
}
diff --git a/app/src/main/java/com/arialyy/simple/single_task/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/single_task/SingleTaskActivity.java
index be786867..a398b82b 100644
--- a/app/src/main/java/com/arialyy/simple/single_task/SingleTaskActivity.java
+++ b/app/src/main/java/com/arialyy/simple/single_task/SingleTaskActivity.java
@@ -42,26 +42,26 @@ import com.arialyy.simple.databinding.ActivitySingleBinding;
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
public class SingleTaskActivity extends BaseActivity {
- public static final int DOWNLOAD_PRE = 0x01;
- public static final int DOWNLOAD_STOP = 0x02;
- public static final int DOWNLOAD_FAILE = 0x03;
- public static final int DOWNLOAD_CANCEL = 0x04;
- public static final int DOWNLOAD_RESUME = 0x05;
- public static final int DOWNLOAD_COMPLETE = 0x06;
- public static final int DOWNLOAD_RUNNING = 0x07;
-
- private static final String DOWNLOAD_URL =
+ public static final int DOWNLOAD_PRE = 0x01;
+ public static final int DOWNLOAD_STOP = 0x02;
+ public static final int DOWNLOAD_FAILE = 0x03;
+ public static final int DOWNLOAD_CANCEL = 0x04;
+ public static final int DOWNLOAD_RESUME = 0x05;
+ public static final int DOWNLOAD_COMPLETE = 0x06;
+ public static final int DOWNLOAD_RUNNING = 0x07;
+
+ private static final String DOWNLOAD_URL =
//"http://kotlinlang.org/docs/kotlin-docs.pdf";
"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe";
@Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb;
- @Bind(R.id.start) Button mStart;
- @Bind(R.id.stop) Button mStop;
- @Bind(R.id.cancel) Button mCancel;
- @Bind(R.id.size) TextView mSize;
- @Bind(R.id.toolbar) Toolbar toolbar;
- @Bind(R.id.speed) TextView mSpeed;
- @Bind(R.id.img) ImageView mImg;
- private DownloadEntity mEntity;
+ @Bind(R.id.start) Button mStart;
+ @Bind(R.id.stop) Button mStop;
+ @Bind(R.id.cancel) Button mCancel;
+ @Bind(R.id.size) TextView mSize;
+ @Bind(R.id.toolbar) Toolbar toolbar;
+ @Bind(R.id.speed) TextView mSpeed;
+ @Bind(R.id.img) ImageView mImg;
+ private DownloadEntity mEntity;
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
@@ -163,7 +163,7 @@ public class SingleTaskActivity extends BaseActivity {
private void init() {
if (Aria.download(this).taskExists(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);
}
}