parent
f8a249302f
commit
8eb1ba8477
@ -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,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,187 @@ |
||||
/* |
||||
* 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.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; |
||||
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; |
||||
} |
||||
} |
@ -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