Merge branch 'v_3.0' of https://github.com/AriaLyy/Aria into v_3.0
commit
31dfc5a234
@ -1,65 +0,0 @@ |
||||
/* |
||||
* 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.download; |
||||
|
||||
import android.content.Context; |
||||
import com.arialyy.aria.core.scheduler.IDownloadSchedulers; |
||||
|
||||
/** |
||||
* Created by lyy on 2016/8/18. |
||||
* 任务工厂 |
||||
*/ |
||||
public class DownloadTaskFactory { |
||||
private static final String TAG = "DownloadTaskFactory"; |
||||
|
||||
private static final Object LOCK = new Object(); |
||||
private static volatile DownloadTaskFactory INSTANCE = null; |
||||
|
||||
private DownloadTaskFactory() { |
||||
|
||||
} |
||||
|
||||
public static DownloadTaskFactory getInstance() { |
||||
if (INSTANCE == null) { |
||||
synchronized (LOCK) { |
||||
INSTANCE = new DownloadTaskFactory(); |
||||
} |
||||
} |
||||
return INSTANCE; |
||||
} |
||||
|
||||
/** |
||||
* 创建普通下载任务 |
||||
* |
||||
* @param entity 下载任务实体{@link DownloadTaskEntity} |
||||
* @param schedulers {@link IDownloadSchedulers} |
||||
*/ |
||||
public DownloadTask createTask(DownloadTaskEntity entity, IDownloadSchedulers schedulers) { |
||||
return createTask("", entity, schedulers); |
||||
} |
||||
|
||||
/** |
||||
* @param entity 下载任务实体{@link DownloadTaskEntity} |
||||
* @param schedulers {@link IDownloadSchedulers} |
||||
*/ |
||||
public DownloadTask createTask(String targetName, DownloadTaskEntity entity, |
||||
IDownloadSchedulers schedulers) { |
||||
DownloadTask.Builder builder = new DownloadTask.Builder(targetName, entity); |
||||
builder.setOutHandler(schedulers); |
||||
return builder.build(); |
||||
} |
||||
} |
@ -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(); |
||||
} |
||||
} |
@ -1,9 +1,38 @@ |
||||
/* |
||||
* 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 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<String, String> headers = new HashMap<>(); |
||||
|
||||
/** |
||||
* 网络请求类型 |
||||
*/ |
||||
public RequestEnum requestEnum = RequestEnum.GET; |
||||
|
||||
public abstract IEntity getEntity(); |
||||
} |
||||
|
@ -0,0 +1,92 @@ |
||||
/* |
||||
* 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.queue; |
||||
|
||||
import com.arialyy.aria.core.download.DownloadTask; |
||||
import com.arialyy.aria.core.download.DownloadTaskEntity; |
||||
import com.arialyy.aria.core.inf.ITask; |
||||
import com.arialyy.aria.core.inf.ITaskEntity; |
||||
import com.arialyy.aria.core.scheduler.DownloadSchedulers; |
||||
import com.arialyy.aria.core.scheduler.ISchedulers; |
||||
import com.arialyy.aria.core.upload.UploadTask; |
||||
import com.arialyy.aria.core.upload.UploadTaskEntity; |
||||
|
||||
/** |
||||
* Created by lyy on 2016/8/18. |
||||
* 任务工厂 |
||||
*/ |
||||
public class TaskFactory { |
||||
private static final String TAG = "TaskFactory"; |
||||
|
||||
private static final Object LOCK = new Object(); |
||||
private static volatile TaskFactory INSTANCE = null; |
||||
|
||||
private TaskFactory() { |
||||
|
||||
} |
||||
|
||||
public static TaskFactory getInstance() { |
||||
if (INSTANCE == null) { |
||||
synchronized (LOCK) { |
||||
INSTANCE = new TaskFactory(); |
||||
} |
||||
} |
||||
return INSTANCE; |
||||
} |
||||
|
||||
/** |
||||
* 创建任务 |
||||
* |
||||
* @param entity 下载实体 |
||||
* @param schedulers 对应的任务调度器 |
||||
* @param <ENTITY> {@link DownloadTaskEntity}、{@link UploadTaskEntity} |
||||
* @param <SCHEDULER> {@link DownloadSchedulers} |
||||
* @return {@link DownloadTask}、{@link UploadTask} |
||||
*/ |
||||
<ENTITY extends ITaskEntity, SCHEDULER extends ISchedulers> ITask createTask( |
||||
String targetName, ENTITY entity, SCHEDULER schedulers) { |
||||
if (entity instanceof DownloadTaskEntity) { |
||||
return createDownloadTask(targetName, (DownloadTaskEntity) entity, schedulers); |
||||
} else if (entity instanceof UploadTaskEntity) { |
||||
return createUploadTask(targetName, (UploadTaskEntity) entity, schedulers); |
||||
} return null; |
||||
} |
||||
|
||||
/** |
||||
* @param entity 上传任务实体{@link UploadTaskEntity} |
||||
* @param schedulers {@link ISchedulers} |
||||
*/ |
||||
private UploadTask createUploadTask(String targetName, UploadTaskEntity entity, |
||||
ISchedulers schedulers) { |
||||
UploadTask.Builder builder = new UploadTask.Builder(); |
||||
builder.setTargetName(targetName); |
||||
builder.setUploadTaskEntity(entity); |
||||
builder.setOutHandler(schedulers); |
||||
return builder.build(); |
||||
} |
||||
|
||||
/** |
||||
* @param entity 下载任务实体{@link DownloadTaskEntity} |
||||
* @param schedulers {@link ISchedulers} |
||||
*/ |
||||
private DownloadTask createDownloadTask(String targetName, DownloadTaskEntity entity, |
||||
ISchedulers schedulers) { |
||||
DownloadTask.Builder builder = new DownloadTask.Builder(targetName, entity); |
||||
builder.setOutHandler(schedulers); |
||||
return builder.build(); |
||||
} |
||||
} |
@ -0,0 +1,120 @@ |
||||
/* |
||||
* 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.queue; |
||||
|
||||
import android.text.TextUtils; |
||||
import android.util.Log; |
||||
import com.arialyy.aria.core.scheduler.UploadSchedulers; |
||||
import com.arialyy.aria.core.upload.UploadEntity; |
||||
import com.arialyy.aria.core.upload.UploadTask; |
||||
import com.arialyy.aria.core.upload.UploadTaskEntity; |
||||
|
||||
/** |
||||
* Created by Aria.Lao on 2017/2/27. |
||||
* 上传任务队列 |
||||
*/ |
||||
public class UploadTaskQueue extends AbsTaskQueue<UploadTask, UploadTaskEntity, UploadEntity> { |
||||
private static final String TAG = "UploadTaskQueue"; |
||||
private static volatile UploadTaskQueue INSTANCE = null; |
||||
private static final Object LOCK = new Object(); |
||||
|
||||
public static UploadTaskQueue getInstance() { |
||||
if (INSTANCE == null) { |
||||
synchronized (LOCK) { |
||||
INSTANCE = new UploadTaskQueue(); |
||||
} |
||||
} |
||||
return INSTANCE; |
||||
} |
||||
|
||||
@Override public void startTask(UploadTask task) { |
||||
if (mExecutePool.putTask(task)) { |
||||
mCachePool.removeTask(task); |
||||
//task.getEntity().setFailNum(0);
|
||||
task.start(); |
||||
} |
||||
} |
||||
|
||||
@Override public void stopTask(UploadTask task) { |
||||
if (!task.isRunning()) Log.w(TAG, "停止任务失败,【任务已经停止】"); |
||||
if (mExecutePool.removeTask(task)) { |
||||
task.stop(); |
||||
} else { |
||||
task.stop(); |
||||
Log.w(TAG, "停止任务失败,【任务已经停止】"); |
||||
} |
||||
} |
||||
|
||||
@Override public void cancelTask(UploadTask task) { |
||||
task.cancel(); |
||||
} |
||||
|
||||
@Override public void reTryStart(UploadTask task) { |
||||
if (task == null) { |
||||
Log.w(TAG, "重试下载失败,task 为null"); |
||||
return; |
||||
} |
||||
if (!task.isRunning()) { |
||||
task.start(); |
||||
} else { |
||||
Log.w(TAG, "任务没有完全停止,重试下载失败"); |
||||
} |
||||
} |
||||
|
||||
@Override public int size() { |
||||
return mExecutePool.size(); |
||||
} |
||||
|
||||
@Override public void setDownloadNum(int downloadNum) { |
||||
|
||||
} |
||||
|
||||
@Override public UploadTask createTask(String targetName, UploadTaskEntity entity) { |
||||
UploadTask task = null; |
||||
if (!TextUtils.isEmpty(targetName)) { |
||||
task = (UploadTask) TaskFactory.getInstance() |
||||
.createTask(targetName, entity, UploadSchedulers.getInstance()); |
||||
mCachePool.putTask(task); |
||||
} else { |
||||
Log.e(TAG, "target name 为 null是!!"); |
||||
} |
||||
return task; |
||||
} |
||||
|
||||
@Override public UploadTask getTask(UploadEntity entity) { |
||||
UploadTask task = mExecutePool.getTask(entity.getFilePath()); |
||||
if (task == null) { |
||||
task = mCachePool.getTask(entity.getFilePath()); |
||||
} |
||||
return task; |
||||
} |
||||
|
||||
@Override public void removeTask(UploadEntity entity) { |
||||
UploadTask task = mExecutePool.getTask(entity.getFilePath()); |
||||
if (task != null) { |
||||
Log.d(TAG, "从执行池删除任务,删除" + (mExecutePool.removeTask(task) ? "成功" : "失败")); |
||||
} |
||||
task = mCachePool.getTask(entity.getFilePath()); |
||||
if (task != null) { |
||||
Log.d(TAG, "从缓存池删除任务,删除" + (mCachePool.removeTask(task) ? "成功" : "失败")); |
||||
} |
||||
} |
||||
|
||||
@Override public UploadTask getNextTask() { |
||||
return mCachePool.pollTask(); |
||||
} |
||||
} |
@ -0,0 +1,185 @@ |
||||
/* |
||||
* 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.scheduler; |
||||
|
||||
import android.os.CountDownTimer; |
||||
import android.os.Message; |
||||
import android.util.Log; |
||||
import com.arialyy.aria.core.inf.IEntity; |
||||
import com.arialyy.aria.core.queue.UploadTaskQueue; |
||||
import com.arialyy.aria.core.upload.UploadEntity; |
||||
import com.arialyy.aria.core.upload.UploadTask; |
||||
import com.arialyy.aria.util.Configuration; |
||||
import java.util.Iterator; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
import java.util.concurrent.ConcurrentHashMap; |
||||
|
||||
/** |
||||
* Created by Aria.Lao on 2017/2/27. |
||||
* 上传任务调度器 |
||||
*/ |
||||
public class UploadSchedulers implements ISchedulers<UploadTask> { |
||||
private static final String TAG = "UploadSchedulers"; |
||||
private static final Object LOCK = new Object(); |
||||
private static volatile UploadSchedulers INSTANCE = null; |
||||
private Map<String, OnSchedulerListener<UploadTask>> mSchedulerListeners = |
||||
new ConcurrentHashMap<>(); |
||||
private UploadTaskQueue mQueue; |
||||
|
||||
private UploadSchedulers() { |
||||
mQueue = UploadTaskQueue.getInstance(); |
||||
} |
||||
|
||||
public static UploadSchedulers getInstance() { |
||||
if (INSTANCE == null) { |
||||
synchronized (LOCK) { |
||||
INSTANCE = new UploadSchedulers(); |
||||
} |
||||
} |
||||
|
||||
return INSTANCE; |
||||
} |
||||
|
||||
@Override public void addSchedulerListener(String targetName, |
||||
OnSchedulerListener<UploadTask> schedulerListener) { |
||||
mSchedulerListeners.put(targetName, schedulerListener); |
||||
} |
||||
|
||||
@Override public void removeSchedulerListener(String targetName, |
||||
OnSchedulerListener<UploadTask> schedulerListener) { |
||||
for (Iterator<Map.Entry<String, OnSchedulerListener<UploadTask>>> iter = |
||||
mSchedulerListeners.entrySet().iterator(); iter.hasNext(); ) { |
||||
Map.Entry<String, OnSchedulerListener<UploadTask>> entry = iter.next(); |
||||
if (entry.getKey().equals(targetName)) iter.remove(); |
||||
} |
||||
} |
||||
|
||||
private void handleFailTask(final UploadEntity entity) { |
||||
final Configuration config = Configuration.getInstance(); |
||||
CountDownTimer timer = new CountDownTimer(config.getReTryInterval(), 1000) { |
||||
@Override public void onTick(long millisUntilFinished) { |
||||
|
||||
} |
||||
|
||||
@Override public void onFinish() { |
||||
if (entity.getFailNum() <= config.getReTryNum()) { |
||||
UploadTask task = mQueue.getTask(entity); |
||||
mQueue.reTryStart(task); |
||||
try { |
||||
Thread.sleep(config.getReTryInterval()); |
||||
} catch (InterruptedException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} else { |
||||
startNextTask(); |
||||
} |
||||
} |
||||
}; |
||||
timer.start(); |
||||
} |
||||
|
||||
private void startNextTask() { |
||||
UploadTask newTask = mQueue.getNextTask(); |
||||
if (newTask == null) { |
||||
Log.w(TAG, "没有下一任务"); |
||||
return; |
||||
} |
||||
if (newTask.getUploadEntity().getState() == IEntity.STATE_WAIT) { |
||||
mQueue.startTask(newTask); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 回调 |
||||
* |
||||
* @param state 状态 |
||||
*/ |
||||
private void callback(int state, UploadTask task) { |
||||
if (mSchedulerListeners.size() > 0) { |
||||
//if (!TextUtils.isEmpty(task.getTargetName())) {
|
||||
// callback(state, task, mSchedulerListeners.get(task.getTargetName()));
|
||||
//}
|
||||
Set<String> keys = mSchedulerListeners.keySet(); |
||||
for (String key : keys) { |
||||
callback(state, task, mSchedulerListeners.get(key)); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private void callback(int state, UploadTask task, OnSchedulerListener<UploadTask> listener) { |
||||
if (listener != null) { |
||||
if (task == null) { |
||||
Log.e(TAG, "TASK 为null,回调失败"); |
||||
return; |
||||
} |
||||
switch (state) { |
||||
case RUNNING: |
||||
listener.onTaskRunning(task); |
||||
break; |
||||
case START: |
||||
listener.onTaskStart(task); |
||||
break; |
||||
case STOP: |
||||
listener.onTaskStop(task); |
||||
break; |
||||
case RESUME: |
||||
listener.onTaskResume(task); |
||||
break; |
||||
case PRE: |
||||
listener.onTaskPre(task); |
||||
break; |
||||
case CANCEL: |
||||
listener.onTaskCancel(task); |
||||
break; |
||||
case COMPLETE: |
||||
listener.onTaskComplete(task); |
||||
break; |
||||
case FAIL: |
||||
listener.onTaskFail(task); |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
|
||||
@Override public boolean handleMessage(Message msg) { |
||||
UploadTask task = (UploadTask) msg.obj; |
||||
if (task == null) { |
||||
Log.e(TAG, "请传入上传任务"); |
||||
return true; |
||||
} |
||||
callback(msg.what, task); |
||||
UploadEntity entity = task.getUploadEntity(); |
||||
switch (msg.what) { |
||||
case STOP: |
||||
case CANCEL: |
||||
mQueue.removeTask(entity); |
||||
if (mQueue.size() < Configuration.getInstance().getDownloadNum()) { |
||||
startNextTask(); |
||||
} |
||||
break; |
||||
case COMPLETE: |
||||
mQueue.removeTask(entity); |
||||
startNextTask(); |
||||
break; |
||||
case FAIL: |
||||
mQueue.removeTask(entity); |
||||
handleFailTask(entity); |
||||
break; |
||||
} |
||||
return true; |
||||
} |
||||
} |
@ -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,49 +0,0 @@ |
||||
package com.arialyy.aria.core.upload; |
||||
|
||||
import com.arialyy.aria.core.queue.ITaskQueue; |
||||
|
||||
/** |
||||
* Created by Aria.Lao on 2017/2/23. |
||||
*/ |
||||
|
||||
public class UploadTaskQueue implements ITaskQueue<UploadTask, UploadTaskEntity, UploadEntity>{ |
||||
@Override public void startTask(UploadTask task) { |
||||
|
||||
} |
||||
|
||||
@Override public void stopTask(UploadTask task) { |
||||
|
||||
} |
||||
|
||||
@Override public void cancelTask(UploadTask task) { |
||||
|
||||
} |
||||
|
||||
@Override public void reTryStart(UploadTask task) { |
||||
|
||||
} |
||||
|
||||
@Override public int size() { |
||||
return 0; |
||||
} |
||||
|
||||
@Override public void setDownloadNum(int downloadNum) { |
||||
|
||||
} |
||||
|
||||
@Override public UploadTask createTask(String targetName, UploadTaskEntity entity) { |
||||
return null; |
||||
} |
||||
|
||||
@Override public UploadTask getTask(UploadEntity entity) { |
||||
return null; |
||||
} |
||||
|
||||
@Override public void removeTask(UploadEntity entity) { |
||||
|
||||
} |
||||
|
||||
@Override public UploadTask getNextTask() { |
||||
return null; |
||||
} |
||||
} |
Loading…
Reference in new issue