逻辑修改

pull/2/head
AriaLyy 8 years ago
parent f1e28fe491
commit 8a49447fa3
  1. 9
      app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java
  2. 9
      downloadutil/src/main/java/com/arialyy/downloadutil/core/AMEntity.java
  3. 105
      downloadutil/src/main/java/com/arialyy/downloadutil/core/Aria.java
  4. 36
      downloadutil/src/main/java/com/arialyy/downloadutil/core/AriaManager.java
  5. 10
      downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadManager.java
  6. 43
      downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/DownloadSchedulers.java
  7. 7
      downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/IDownloadSchedulers.java

@ -17,9 +17,6 @@
package com.arialyy.simple.activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
@ -39,13 +36,9 @@ import com.arialyy.downloadutil.core.scheduler.OnSchedulerListener;
import com.arialyy.downloadutil.core.task.Task;
import com.arialyy.downloadutil.orm.DbEntity;
import com.arialyy.downloadutil.util.CommonUtil;
import com.arialyy.frame.util.show.L;
import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.ActivitySingleBinding;
import com.arialyy.simple.module.DownloadModule;
import java.util.ArrayList;
import java.util.List;
public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
public static final int DOWNLOAD_PRE = 0x01;
@ -207,7 +200,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
//commands.add(startCmd);
//mManager.setCmds(commands).exe();
mManager.setCmd(CmdFactory.getInstance().createCmd(mEntity, CmdFactory.TASK_SINGLE))
.regSchedulerListener(new OnSchedulerListener() {
.addSchedulerListener(new OnSchedulerListener() {
@Override public void onTaskStart(Task task) {
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize())
.sendToTarget();

@ -0,0 +1,9 @@
package com.arialyy.downloadutil.core;
import android.content.Context;
import com.arialyy.downloadutil.core.scheduler.OnSchedulerListener;
public class AMEntity {
Context context;
OnSchedulerListener listener;
}

@ -0,0 +1,105 @@
/*
* Copyright (C) 2016 AriaLyy(DownloadUtil)
*
* 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.downloadutil.core;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import com.arialyy.downloadutil.core.command.CmdFactory;
import com.arialyy.downloadutil.core.command.IDownloadCmd;
import com.arialyy.downloadutil.core.scheduler.OnSchedulerListener;
import java.util.ArrayList;
import java.util.List;
/**
* Created by lyy on 2016/12/1.
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
public class Aria {
private static final Object LOCK = new Object();
private static volatile Aria INSTANCE = null;
private DownloadManager mDownloadManager;
private Aria() {
//mDownloadManager = DownloadManager.getInstance();
}
private Aria(Context context) {
mDownloadManager = DownloadManager.init(context);
}
public static Aria get(Context context) {
if (INSTANCE == null) {
synchronized (LOCK) {
INSTANCE = new Aria();
}
}
return INSTANCE;
}
/**
* 开始下载
*/
public Aria start(DownloadEntity entity) {
List<IDownloadCmd> cmds = new ArrayList<>();
cmds.add(createCmd(entity, CmdFactory.TASK_CREATE));
cmds.add(createCmd(entity, CmdFactory.TASK_START));
mDownloadManager.setCmds(cmds).exe();
return this;
}
/**
* 停止下载
*/
public void stop(DownloadEntity entity) {
mDownloadManager.setCmd(createCmd(entity, CmdFactory.TASK_STOP)).exe();
}
/**
* 恢复下载
*/
public void resume(DownloadEntity entity) {
mDownloadManager.setCmd(createCmd(entity, CmdFactory.TASK_START)).exe();
}
/**
* 取消下载
*/
public void cancel(DownloadEntity entity) {
mDownloadManager.setCmd(createCmd(entity, CmdFactory.TASK_CANCEL)).exe();
}
/**
* 添加调度器回调
*/
public Aria addSchedulerListener(Context context, OnSchedulerListener listener) {
//mDownloadManager.getTaskQueue().getDownloadSchedulers().addSchedulerListener(listener);
return this;
}
/**
* 移除回调
*/
public Aria removeSchedulerListener(Context context) {
//mDownloadManager.getTaskQueue().getDownloadSchedulers().removeSchedulerListener(listener);
return this;
}
private IDownloadCmd createCmd(DownloadEntity entity, int cmd) {
return CmdFactory.getInstance().createCmd(entity, cmd);
}
}

@ -0,0 +1,36 @@
package com.arialyy.downloadutil.core;
import android.content.Context;
import com.arialyy.downloadutil.core.scheduler.OnSchedulerListener;
import java.util.HashMap;
import java.util.Map;
/**
* Created by lyy on 2016/12/1.
*/
public class AriaManager {
private Map<String, AMEntity> mAria = new HashMap<>();
private static final Object LOCK = new Object();
private static volatile AriaManager INSTANCE = null;
private AriaManager(){
}
public static AriaManager getInstance() {
if (INSTANCE == null) {
synchronized (LOCK) {
INSTANCE = new AriaManager();
}
}
return INSTANCE;
}
public synchronized void get(Context context){
}
}

@ -22,7 +22,6 @@ import android.util.Log;
import com.arialyy.downloadutil.core.command.IDownloadCmd;
import com.arialyy.downloadutil.core.queue.ITaskQueue;
import com.arialyy.downloadutil.core.queue.DownloadTaskQueue;
import com.arialyy.downloadutil.core.scheduler.OnSchedulerListener;
import com.arialyy.downloadutil.orm.DbEntity;
import com.arialyy.downloadutil.orm.DbUtil;
import java.util.ArrayList;
@ -124,15 +123,6 @@ public class DownloadManager {
return DbEntity.findAllData(DownloadEntity.class);
}
/**
* 注册
* @param listener
*/
public DownloadManager regSchedulerListener(OnSchedulerListener listener) {
mTaskQueue.getDownloadSchedulers().regTargetListener(listener);
return this;
}
/**
* 获取任务队列
*/

@ -16,13 +16,19 @@
package com.arialyy.downloadutil.core.scheduler;
import android.content.Context;
import android.os.Message;
import android.util.Log;
import android.util.SparseArray;
import android.util.SparseIntArray;
import com.arialyy.downloadutil.core.DownloadEntity;
import com.arialyy.downloadutil.core.queue.ITaskQueue;
import com.arialyy.downloadutil.core.task.Task;
import com.arialyy.downloadutil.core.queue.pool.ExecutePool;
import com.arialyy.downloadutil.core.queue.DownloadTaskQueue;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* Created by lyy on 2016/8/16.
@ -69,7 +75,8 @@ public class DownloadSchedulers implements IDownloadSchedulers {
/**
* 下载器任务监听
*/
OnSchedulerListener mTargetListener;
OnSchedulerListener mSchedulerListener;
Map<Integer, OnSchedulerListener> mSchedulerListeners = new HashMap<>();
ITaskQueue mQueue;
public DownloadSchedulers(ITaskQueue downloadTaskQueue) {
@ -116,26 +123,38 @@ public class DownloadSchedulers implements IDownloadSchedulers {
* @param entity 下载实体
*/
private void callback(int state, DownloadEntity entity) {
if (mTargetListener != null) {
if (mSchedulerListeners.size() > 0) {
//Set<Map.Entry<Integer, String>>
for (Map.Entry<Integer, OnSchedulerListener> entry : mSchedulerListeners.entrySet()) {
callback(state, entity, entry.getValue());
}
}
}
private void callback(int state, DownloadEntity entity, OnSchedulerListener listener) {
if (listener != null) {
Task task = mQueue.getTask(entity);
switch (state) {
case RUNNING:
mTargetListener.onTaskRunning(task);
listener.onTaskRunning(task);
break;
case START:
mTargetListener.onTaskStart(task);
listener.onTaskStart(task);
break;
case STOP:
mTargetListener.onTaskStop(task);
listener.onTaskStop(task);
break;
case CANCEL:
mTargetListener.onTaskCancel(task);
listener.onTaskCancel(task);
removeSchedulerListener(listener);
break;
case COMPLETE:
mTargetListener.onTaskComplete(task);
listener.onTaskComplete(task);
removeSchedulerListener(listener);
break;
case FAIL:
mTargetListener.onTaskFail(task);
listener.onTaskFail(task);
removeSchedulerListener(listener);
break;
}
}
@ -172,12 +191,12 @@ public class DownloadSchedulers implements IDownloadSchedulers {
}
}
@Override public void regTargetListener(OnSchedulerListener targetListener) {
this.mTargetListener = targetListener;
@Override public void addSchedulerListener(Context context, OnSchedulerListener schedulerListener) {
mSchedulerListeners.put(schedulerListener.hashCode(), schedulerListener);
}
@Override public void unRegTargetListener(OnSchedulerListener targetListener) {
mTargetListener = null;
@Override public void removeSchedulerListener(OnSchedulerListener schedulerListener) {
mSchedulerListeners.remove(schedulerListener.hashCode());
}
public void setFailNum(int mFailNum) {

@ -16,6 +16,7 @@
package com.arialyy.downloadutil.core.scheduler;
import android.content.Context;
import android.os.Handler;
import com.arialyy.downloadutil.core.DownloadEntity;
@ -28,14 +29,14 @@ public interface IDownloadSchedulers extends Handler.Callback {
/**
* 注册下载器监听
*
* @param targetListener {@link OnSchedulerListener}
* @param schedulerListener {@link OnSchedulerListener}
*/
public void regTargetListener(OnSchedulerListener targetListener);
public void addSchedulerListener(Context context, OnSchedulerListener schedulerListener);
/**
* 取消注册监听器
*/
public void unRegTargetListener(OnSchedulerListener targetListener);
public void removeSchedulerListener(OnSchedulerListener schedulerListener);
/**
* 处理下载任务下载失败的情形

Loading…
Cancel
Save