AriaManager 逻辑重写,api接口优化

pull/330/head
AriaLyy 8 years ago
parent 62d6434914
commit 90c7cd78ff
  1. 1
      Aria/src/main/java/com/arialyy/aria/core/Aria.java
  2. 272
      Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
  3. 107
      Aria/src/main/java/com/arialyy/aria/core/DownloadManager.java
  4. 141
      Aria/src/main/java/com/arialyy/aria/core/DownloadReceiver.java
  5. 36
      Aria/src/main/java/com/arialyy/aria/core/DownloadTarget.java
  6. 15
      Aria/src/main/java/com/arialyy/aria/core/DownloadTaskEntity.java
  7. 13
      Aria/src/main/java/com/arialyy/aria/core/IReceiver.java
  8. 17
      Aria/src/main/java/com/arialyy/aria/core/RequestEnum.java
  9. 11
      Aria/src/main/java/com/arialyy/aria/core/UploadReceiver.java
  10. 111
      Aria/src/main/java/com/arialyy/aria/core/WidgetLiftManager.java
  11. 5
      Aria/src/main/java/com/arialyy/aria/core/command/download/IDownloadCmd.java
  12. 77
      Aria/src/main/java/com/arialyy/aria/core/receiver/DownloadReceiver.java
  13. 6
      Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java
  14. 4
      Aria/src/main/java/com/arialyy/aria/util/Configuration.java
  15. 9
      Aria/src/main/java/com/arialyy/aria/util/SSLContextUtil.java
  16. 2
      app/src/main/java/com/arialyy/simple/base/BaseApplication.java
  17. 8
      app/src/main/java/com/arialyy/simple/dialog_task/DownloadDialog.java
  18. 8
      app/src/main/java/com/arialyy/simple/fragment_task/DownloadFragment.java
  19. 2
      app/src/main/java/com/arialyy/simple/module/DownloadModule.java
  20. 2
      app/src/main/java/com/arialyy/simple/multi_task/DownloadActivity.java
  21. 2
      app/src/main/java/com/arialyy/simple/multi_task/MultiTaskActivity.java
  22. 8
      app/src/main/java/com/arialyy/simple/pop_task/DownloadPopupWindow.java
  23. 6
      app/src/main/java/com/arialyy/simple/single_task/SingleTaskActivity.java

@ -26,7 +26,6 @@ import android.app.Service;
import android.content.Context;
import android.os.Build;
import android.widget.PopupWindow;
import com.arialyy.aria.core.receiver.DownloadReceiver;
import com.arialyy.aria.core.scheduler.OnSchedulerListener;
import com.arialyy.aria.core.task.Task;

@ -15,33 +15,30 @@
*/
package com.arialyy.aria.core;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Application;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.widget.PopupWindow;
import com.arialyy.aria.core.command.download.CmdFactory;
import com.arialyy.aria.core.receiver.DownloadReceiver;
import com.arialyy.aria.util.CAConfiguration;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.core.command.download.IDownloadCmd;
import com.arialyy.aria.core.queue.DownloadTaskQueue;
import com.arialyy.aria.core.queue.ITaskQueue;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.orm.DbUtil;
import com.arialyy.aria.util.CAConfiguration;
import com.arialyy.aria.util.Configuration;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Created by lyy on 2016/12/1.
@ -50,18 +47,26 @@ import java.util.Set;
*/
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public class AriaManager {
private static final String TAG = "AriaManager";
private static final String DOWNLOAD = "_download";
private static final String UPLOAD = "_upload";
private static final Object LOCK = new Object();
private static volatile AriaManager INSTANCE = null;
private Map<String, DownloadReceiver> mDownloadTargets = new HashMap<>();
private DownloadManager mManager;
@SuppressLint("StaticFieldLeak") private static volatile AriaManager INSTANCE = null;
Map<String, IReceiver> mReceivers = new HashMap<>();
private LifeCallback mLifeCallback;
public static Context APP;
private ITaskQueue mTaskQueue;
private List<IDownloadCmd> mCommands = new ArrayList<>();
private AriaManager(Context context) {
DbUtil.init(context.getApplicationContext());
APP = context;
DownloadTaskQueue.Builder builder = new DownloadTaskQueue.Builder(context);
mTaskQueue = builder.build();
regAppLifeCallback(context);
mManager = DownloadManager.init(context);
}
static AriaManager getInstance(Context context) {
public static AriaManager getInstance(Context context) {
if (INSTANCE == null) {
synchronized (LOCK) {
INSTANCE = new AriaManager(context);
@ -70,63 +75,72 @@ import java.util.Set;
return INSTANCE;
}
DownloadReceiver download(Object obj) {
return getDownloadTarget(obj);
List<DownloadEntity> getAllDownloadEntity() {
return DbEntity.findAllData(DownloadEntity.class);
}
/**
* 设置CA证书信息
*
* @param caAlias ca证书别名
* @param caPath assets 文件夹下的ca证书完整路径
* 获取任务队列
*/
public void setCAInfo(String caAlias, String caPath) {
if (TextUtils.isEmpty(caAlias)) {
Log.e(TAG, "ca证书别名不能为null");
return;
} else if (TextUtils.isEmpty(caPath)) {
Log.e(TAG, "ca证书路径不能为null");
return;
}
CAConfiguration.CA_ALIAS = caAlias;
CAConfiguration.CA_PATH = caPath;
public ITaskQueue getTaskQueue() {
return mTaskQueue;
}
/**
* 设置命令
*/
AriaManager setCmd(IDownloadCmd command) {
mCommands.add(command);
return this;
}
/**
* 获取下载列表
* 设置一组命令
*/
public List<DownloadEntity> getDownloadList() {
return DownloadEntity.findAllData(DownloadEntity.class);
AriaManager setCmds(List<IDownloadCmd> commands) {
if (commands != null && commands.size() > 0) {
mCommands.addAll(commands);
}
return this;
}
/**
* 通过下载链接获取下载实体
* 执行所有设置的命令
*/
public DownloadEntity getDownloadEntity(String downloadUrl) {
CheckUtil.checkDownloadUrl(downloadUrl);
return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
synchronized void exe() {
for (IDownloadCmd command : mCommands) {
command.executeCmd();
}
mCommands.clear();
}
/**
* 下载任务是否存在
* 处理下载操作
*/
public boolean taskExists(String downloadUrl) {
return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl) != null;
DownloadReceiver download(Object obj) {
IReceiver receiver = mReceivers.get(getKey(true, obj));
if (receiver == null) {
receiver = putReceiver(true, obj);
}
return (receiver instanceof DownloadReceiver) ? (DownloadReceiver) receiver : null;
}
/**
* 停止所有正在下载的任务
* 设置CA证书信息
*
* @param caAlias ca证书别名
* @param caPath assets 文件夹下的ca证书完整路径
*/
public void stopAllTask() {
List<DownloadEntity> allEntity = mManager.getAllDownloadEntity();
List<IDownloadCmd> stopCmds = new ArrayList<>();
for (DownloadEntity entity : allEntity) {
if (entity.getState() == DownloadEntity.STATE_DOWNLOAD_ING) {
stopCmds.add(
CommonUtil.createDownloadCmd(new DownloadTaskEntity(entity), CmdFactory.TASK_STOP));
}
public void setCAInfo(String caAlias, String caPath) {
if (TextUtils.isEmpty(caAlias)) {
Log.e(TAG, "ca证书别名不能为null");
return;
} else if (TextUtils.isEmpty(caPath)) {
Log.e(TAG, "ca证书路径不能为null");
return;
}
mManager.setCmds(stopCmds).exe();
CAConfiguration.CA_ALIAS = caAlias;
CAConfiguration.CA_PATH = caPath;
}
/**
@ -138,7 +152,7 @@ import java.util.Set;
}
/**
* 设置下载失败重试次数
* 设置失败重试次数
*/
public AriaManager setReTryNum(int reTryNum) {
Configuration.getInstance().setReTryNum(reTryNum);
@ -146,7 +160,7 @@ import java.util.Set;
}
/**
* 设置下载失败重试间隔
* 设置失败重试间隔
*/
public AriaManager setReTryInterval(int interval) {
Configuration.getInstance().setReTryInterval(interval);
@ -171,32 +185,40 @@ import java.util.Set;
Log.w(TAG, "最大任务数不能小于 1");
return this;
}
mManager.getTaskQueue().setDownloadNum(maxDownloadNum);
mTaskQueue.setDownloadNum(maxDownloadNum);
return this;
}
/**
* 删除所有任务
*/
public void cancelAllTask() {
List<DownloadEntity> allEntity = mManager.getAllDownloadEntity();
List<IDownloadCmd> cancelCmds = new ArrayList<>();
for (DownloadEntity entity : allEntity) {
cancelCmds.add(
CommonUtil.createDownloadCmd(new DownloadTaskEntity(entity), CmdFactory.TASK_CANCEL));
private IReceiver putReceiver(boolean isDownload, Object obj) {
final String key = getKey(isDownload, obj);
IReceiver receiver = mReceivers.get(key);
final WidgetLiftManager widgetLiftManager = new WidgetLiftManager();
if (obj instanceof Dialog) {
widgetLiftManager.handleDialogLift((Dialog) obj);
} else if (obj instanceof PopupWindow) {
widgetLiftManager.handlePopupWindowLift((PopupWindow) obj);
}
mManager.setCmds(cancelCmds).exe();
Set<String> keys = mDownloadTargets.keySet();
for (String key : keys) {
DownloadReceiver target = mDownloadTargets.get(key);
target.removeSchedulerListener();
mDownloadTargets.remove(key);
if (receiver == null) {
if (isDownload) {
DownloadReceiver dReceiver = new DownloadReceiver();
dReceiver.targetName = obj.getClass().getName();
mReceivers.put(key, dReceiver);
receiver = dReceiver;
} else {
UploadReceiver uReceiver = new UploadReceiver();
receiver = uReceiver;
}
}
return receiver;
}
private DownloadReceiver putTarget(Object obj) {
/**
* 根据功能类型和控件类型获取对应的key
*/
private String getKey(boolean isDownload, Object obj) {
String clsName = obj.getClass().getName();
DownloadReceiver target = null;
String key = "";
if (!(obj instanceof Activity)) {
if (obj instanceof android.support.v4.app.Fragment) {
@ -210,7 +232,6 @@ import java.util.Set;
} else {
key = clsName;
}
handleDialogLift((Dialog) obj);
} else if (obj instanceof PopupWindow) {
Context context = ((PopupWindow) obj).getContentView().getContext();
if (context instanceof Activity) {
@ -218,7 +239,6 @@ import java.util.Set;
} else {
key = clsName;
}
handlePopupWindowLift((PopupWindow) obj);
}
} else {
key = clsName;
@ -226,76 +246,8 @@ import java.util.Set;
if (TextUtils.isEmpty(key)) {
throw new IllegalArgumentException("未知类型");
}
target = mDownloadTargets.get(key);
if (target == null) {
target = new DownloadReceiver();
target.targetName = obj.getClass().getName();
mDownloadTargets.put(key, target);
}
return target;
}
/**
* 出来悬浮框取消或dismiss
*/
private void handlePopupWindowLift(PopupWindow popupWindow) {
try {
Field dismissField = CommonUtil.getField(popupWindow.getClass(), "mOnDismissListener");
PopupWindow.OnDismissListener listener =
(PopupWindow.OnDismissListener) dismissField.get(popupWindow);
if (listener != null) {
Log.e(TAG, "你已经对PopupWindow设置了Dismiss事件。为了防止内存泄露,"
+ "请在dismiss方法中调用Aria.download(this).removeSchedulerListener();来注销事件");
} else {
popupWindow.setOnDismissListener(createPopupWindowListener(popupWindow));
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
/**
* 创建popupWindow dismiss事件
*/
private PopupWindow.OnDismissListener createPopupWindowListener(final PopupWindow popupWindow) {
return new PopupWindow.OnDismissListener() {
@Override public void onDismiss() {
destroySchedulerListener(popupWindow);
}
};
}
/**
* 处理对话框取消或dismiss
*/
private void handleDialogLift(Dialog dialog) {
try {
Field dismissField = CommonUtil.getField(dialog.getClass(), "mDismissMessage");
Message dismissMsg = (Message) dismissField.get(dialog);
//如果Dialog已经设置Dismiss事件,则查找cancel事件
if (dismissMsg != null) {
Field cancelField = CommonUtil.getField(dialog.getClass(), "mCancelMessage");
Message cancelMsg = (Message) cancelField.get(dialog);
if (cancelMsg != null) {
Log.e(TAG, "你已经对Dialog设置了Dismiss和cancel事件。为了防止内存泄露,"
+ "请在dismiss方法中调用Aria.download(this).removeSchedulerListener();来注销事件");
} else {
dialog.setOnCancelListener(createCancelListener());
}
} else {
dialog.setOnDismissListener(createDismissListener());
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
private DownloadReceiver getDownloadTarget(Object obj) {
DownloadReceiver target = mDownloadTargets.get(obj.getClass().getName());
if (target == null) {
target = putTarget(obj);
}
return target;
key += isDownload ? DOWNLOAD : UPLOAD;
return key;
}
/**
@ -309,43 +261,17 @@ import java.util.Set;
}
}
/**
* 创建Dialog取消事件
*/
private Dialog.OnCancelListener createCancelListener() {
return new Dialog.OnCancelListener() {
@Override public void onCancel(DialogInterface dialog) {
destroySchedulerListener(dialog);
}
};
}
/**
* 创建Dialog dismiss取消事件
*/
private Dialog.OnDismissListener createDismissListener() {
return new Dialog.OnDismissListener() {
@Override public void onDismiss(DialogInterface dialog) {
destroySchedulerListener(dialog);
}
};
}
/**
* onDestroy
*/
private void destroySchedulerListener(Object obj) {
Set<String> keys = mDownloadTargets.keySet();
void destroySchedulerListener(Object obj) {
String clsName = obj.getClass().getName();
for (
Iterator<Map.Entry<String, DownloadReceiver>> iter = mDownloadTargets.entrySet().iterator();
for (Iterator<Map.Entry<String, IReceiver>> iter = mReceivers.entrySet().iterator();
iter.hasNext(); ) {
Map.Entry<String, DownloadReceiver> entry = iter.next();
Map.Entry<String, IReceiver> entry = iter.next();
String key = entry.getKey();
if (key.equals(clsName) || key.contains(clsName)) {
DownloadReceiver receiver = mDownloadTargets.get(key);
if (key.contains(clsName)) {
IReceiver receiver = mReceivers.get(key);
receiver.removeSchedulerListener();
receiver.destroy();
iter.remove();

@ -1,107 +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;
import android.content.Context;
import com.arialyy.aria.core.queue.ITaskQueue;
import com.arialyy.aria.orm.DbUtil;
import com.arialyy.aria.core.command.download.IDownloadCmd;
import com.arialyy.aria.core.queue.DownloadTaskQueue;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.Configuration;
import java.util.ArrayList;
import java.util.List;
/**
* Created by lyy on 2016/8/11.
* 下载管理器通过命令的方式控制下载
*/
public class DownloadManager {
private static final String TAG = "DownloadManager";
private static final Object LOCK = new Object();
private static volatile DownloadManager INSTANCE = null;
private List<IDownloadCmd> mCommands = new ArrayList<>();
public static Context APP;
private ITaskQueue mTaskQueue;
private static Configuration mConfig;
private DownloadManager() {
}
private DownloadManager(Context context) {
APP = context;
DownloadTaskQueue.Builder builder = new DownloadTaskQueue.Builder(context);
mTaskQueue = builder.build();
DbUtil.init(context);
}
static DownloadManager init(Context context) {
if (INSTANCE == null) {
synchronized (LOCK) {
INSTANCE = new DownloadManager(context.getApplicationContext());
}
}
return INSTANCE;
}
public static DownloadManager getInstance() {
if (INSTANCE == null) {
throw new NullPointerException("请在Application中调用init进行下载器注册");
}
return INSTANCE;
}
List<DownloadEntity> getAllDownloadEntity() {
return DbEntity.findAllData(DownloadEntity.class);
}
/**
* 获取任务队列
*/
public ITaskQueue getTaskQueue() {
return mTaskQueue;
}
/**
* 设置命令
*/
DownloadManager setCmd(IDownloadCmd command) {
mCommands.add(command);
return this;
}
/**
* 设置一组命令
*/
DownloadManager setCmds(List<IDownloadCmd> commands) {
if (commands != null && commands.size() > 0) {
mCommands.addAll(commands);
}
return this;
}
/**
* 执行所有设置的命令
*/
synchronized void exe() {
for (IDownloadCmd command : mCommands) {
command.executeCmd();
}
mCommands.clear();
}
}

@ -0,0 +1,141 @@
/*
* 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;
import android.support.annotation.NonNull;
import com.arialyy.aria.core.command.download.CmdFactory;
import com.arialyy.aria.core.command.download.IDownloadCmd;
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
import com.arialyy.aria.core.scheduler.OnSchedulerListener;
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 lyy on 2016/12/5.
* 下载功能接收器
*/
public class DownloadReceiver implements IReceiver{
private static final String TAG = "DownloadReceiver";
public String targetName;
public OnSchedulerListener listener;
/**
* {@link #load(String)}请使用该方法
*/
@Deprecated public DownloadTarget load(DownloadEntity entity) {
return new DownloadTarget(entity, targetName);
}
/**
* 读取下载链接
*/
public DownloadTarget load(@NonNull String downloadUrl) {
CheckUtil.checkDownloadUrl(downloadUrl);
DownloadEntity entity =
DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
if (entity == null) {
entity = new DownloadEntity();
}
entity.setDownloadUrl(downloadUrl);
return new DownloadTarget(entity, targetName);
}
/**
* 添加调度器回调
*/
public DownloadReceiver addSchedulerListener(OnSchedulerListener listener) {
this.listener = listener;
DownloadSchedulers.getInstance().addSchedulerListener(targetName, listener);
return this;
}
/**
* 移除回调
*/
@Override
public void removeSchedulerListener() {
if (listener != null) {
DownloadSchedulers.getInstance().removeSchedulerListener(targetName, listener);
}
}
@Override
public void destroy() {
targetName = null;
listener = null;
}
/**
* 获取下载列表
*/
public List<DownloadEntity> getDownloadList() {
return DownloadEntity.findAllData(DownloadEntity.class);
}
/**
* 通过下载链接获取下载实体
*/
public DownloadEntity getDownloadEntity(String downloadUrl) {
CheckUtil.checkDownloadUrl(downloadUrl);
return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
}
/**
* 下载任务是否存在
*/
public boolean taskExists(String downloadUrl) {
return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl) != null;
}
/**
* 停止所有正在下载的任务
*/
public void stopAllTask() {
final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP);
List<DownloadEntity> allEntity = ariaManager.getAllDownloadEntity();
List<IDownloadCmd> stopCmds = new ArrayList<>();
for (DownloadEntity entity : allEntity) {
if (entity.getState() == DownloadEntity.STATE_DOWNLOAD_ING) {
stopCmds.add(
CommonUtil.createDownloadCmd(new DownloadTaskEntity(entity), CmdFactory.TASK_STOP));
}
}
ariaManager.setCmds(stopCmds).exe();
}
/**
* 删除所有任务
*/
public void cancelAllTask() {
final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP);
List<DownloadEntity> allEntity = ariaManager.getAllDownloadEntity();
List<IDownloadCmd> cancelCmds = new ArrayList<>();
for (DownloadEntity entity : allEntity) {
cancelCmds.add(
CommonUtil.createDownloadCmd(new DownloadTaskEntity(entity), CmdFactory.TASK_CANCEL));
}
ariaManager.setCmds(cancelCmds).exe();
Set<String> keys = ariaManager.mReceivers.keySet();
for (String key : keys) {
IReceiver receiver = ariaManager.mReceivers.get(key);
receiver.removeSchedulerListener();
ariaManager.mReceivers.remove(key);
}
}
}

@ -22,22 +22,20 @@ import com.arialyy.aria.core.command.download.IDownloadCmd;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
/**
* Created by lyy on 2016/12/5.
* https://github.com/AriaLyy/Aria
*/
public class AMTarget {
public class DownloadTarget {
DownloadEntity entity;
String targetName;
DownloadTaskEntity taskEntity;
public AMTarget(DownloadEntity entity, String targetName) {
public DownloadTarget(DownloadEntity entity, String targetName) {
this.entity = entity;
this.targetName = targetName;
taskEntity = new DownloadTaskEntity(entity);
@ -49,7 +47,7 @@ public class AMTarget {
* @param key 头部key
* @param header 头部value
*/
public AMTarget addHeader(@NonNull String key, @NonNull String header) {
public DownloadTarget addHeader(@NonNull String key, @NonNull String header) {
taskEntity.headers.put(key, header);
return this;
}
@ -59,7 +57,7 @@ public class AMTarget {
*
* @param headers Map<Key, Value>
*/
public AMTarget addHeaders(Map<String, String> headers) {
public DownloadTarget addHeaders(Map<String, String> headers) {
if (headers != null && headers.size() > 0) {
Set<String> keys = headers.keySet();
for (String key : keys) {
@ -72,7 +70,7 @@ public class AMTarget {
/**
* 设置文件存储路径
*/
public AMTarget setDownloadPath(@NonNull String downloadPath) {
public DownloadTarget setDownloadPath(@NonNull String downloadPath) {
if (TextUtils.isEmpty(downloadPath)) {
throw new IllegalArgumentException("文件保持路径不能为null");
}
@ -80,10 +78,20 @@ public class AMTarget {
return this;
}
/**
* 设置请求类型
*
* @param requestEnum {@link RequestEnum}
*/
public DownloadTarget setRequestMode(RequestEnum requestEnum) {
taskEntity.requestEnum = requestEnum;
return this;
}
/**
* 设置文件名
*/
public AMTarget setDownloadName(@NonNull String downloadName) {
public DownloadTarget setDownloadName(@NonNull String downloadName) {
if (TextUtils.isEmpty(downloadName)) {
throw new IllegalArgumentException("文件名不能为null");
}
@ -122,7 +130,7 @@ public class AMTarget {
* 添加任务
*/
public void add() {
DownloadManager.getInstance()
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createDownloadCmd(targetName, taskEntity, CmdFactory.TASK_CREATE))
.exe();
}
@ -134,7 +142,7 @@ public class AMTarget {
List<IDownloadCmd> cmds = new ArrayList<>();
cmds.add(CommonUtil.createDownloadCmd(targetName, taskEntity, CmdFactory.TASK_CREATE));
cmds.add(CommonUtil.createDownloadCmd(targetName, taskEntity, CmdFactory.TASK_START));
DownloadManager.getInstance().setCmds(cmds).exe();
AriaManager.getInstance(AriaManager.APP).setCmds(cmds).exe();
cmds.clear();
}
@ -142,7 +150,7 @@ public class AMTarget {
* 停止下载
*/
public void stop() {
DownloadManager.getInstance()
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createDownloadCmd(targetName, taskEntity, CmdFactory.TASK_STOP))
.exe();
}
@ -151,7 +159,7 @@ public class AMTarget {
* 恢复下载
*/
public void resume() {
DownloadManager.getInstance()
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createDownloadCmd(targetName, taskEntity, CmdFactory.TASK_START))
.exe();
}
@ -160,7 +168,7 @@ public class AMTarget {
* 取消下载
*/
public void cancel() {
DownloadManager.getInstance()
AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createDownloadCmd(targetName, taskEntity, CmdFactory.TASK_CANCEL))
.exe();
}
@ -169,7 +177,7 @@ public class AMTarget {
* 是否在下载
*/
public boolean isDownloading() {
return DownloadManager.getInstance().getTaskQueue().getTask(entity).isDownloading();
return AriaManager.getInstance(AriaManager.APP).getTaskQueue().getTask(entity).isDownloading();
}
/**

@ -1,3 +1,18 @@
/*
* 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;
import java.util.HashMap;

@ -13,12 +13,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.receiver;
package com.arialyy.aria.core;
/**
* Created by Aria.Lao on 2017/2/6.
*/
interface IReceiver {
/**
* Receiver 销毁
*/
public void destroy();
public interface IReceiver {
/**
* 移除事件回调
*/
public void removeSchedulerListener();
}

@ -1,9 +1,24 @@
/*
* 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;
/**
* Created by Aria.Lao on 2017/1/23.
* url请求方式目前支持GETPOST
*/
public enum RequestEnum {
GET("GET"), POST("POST");

@ -13,12 +13,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.receiver;
package com.arialyy.aria.core;
/**
* Created by Aria.Lao on 2017/2/6.
* 上传功能接收器
*/
public class UploadReceiver implements IReceiver {
@Override public void destroy() {
}
@Override public void removeSchedulerListener() {
}
}

@ -0,0 +1,111 @@
/*
* 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;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Message;
import android.util.Log;
import android.widget.PopupWindow;
import com.arialyy.aria.util.CommonUtil;
import java.lang.reflect.Field;
/**
* Created by Aria.Lao on 2017/2/7.
* 为组件添加生命周期
*/
final class WidgetLiftManager {
private final String TAG = "WidgetLiftManager";
/**
* 处理悬浮框取消或dismiss事件
*/
void handlePopupWindowLift(PopupWindow popupWindow) {
try {
Field dismissField = CommonUtil.getField(popupWindow.getClass(), "mOnDismissListener");
PopupWindow.OnDismissListener listener =
(PopupWindow.OnDismissListener) dismissField.get(popupWindow);
if (listener != null) {
Log.e(TAG, "你已经对PopupWindow设置了Dismiss事件。为了防止内存泄露,"
+ "请在dismiss方法中调用Aria.download(this).removeSchedulerListener();来注销事件");
} else {
popupWindow.setOnDismissListener(createPopupWindowListener(popupWindow));
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
/**
* 创建popupWindow dismiss事件
*/
private PopupWindow.OnDismissListener createPopupWindowListener(final PopupWindow popupWindow) {
return new PopupWindow.OnDismissListener() {
@Override public void onDismiss() {
AriaManager.getInstance(AriaManager.APP).destroySchedulerListener(popupWindow);
}
};
}
/**
* 处理对话框取消或dismiss
*/
void handleDialogLift(Dialog dialog) {
try {
Field dismissField = CommonUtil.getField(dialog.getClass(), "mDismissMessage");
Message dismissMsg = (Message) dismissField.get(dialog);
//如果Dialog已经设置Dismiss事件,则查找cancel事件
if (dismissMsg != null) {
Field cancelField = CommonUtil.getField(dialog.getClass(), "mCancelMessage");
Message cancelMsg = (Message) cancelField.get(dialog);
if (cancelMsg != null) {
Log.e(TAG, "你已经对Dialog设置了Dismiss和cancel事件。为了防止内存泄露,"
+ "请在dismiss方法中调用Aria.download(this).removeSchedulerListener();来注销事件");
} else {
dialog.setOnCancelListener(createCancelListener());
}
} else {
dialog.setOnDismissListener(createDismissListener());
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
/**
* 创建Dialog取消事件
*/
private Dialog.OnCancelListener createCancelListener() {
return new Dialog.OnCancelListener() {
@Override public void onCancel(DialogInterface dialog) {
AriaManager.getInstance(AriaManager.APP).destroySchedulerListener(dialog);
}
};
}
/**
* 创建Dialog dismiss取消事件
*/
private Dialog.OnDismissListener createDismissListener() {
return new Dialog.OnDismissListener() {
@Override public void onDismiss(DialogInterface dialog) {
AriaManager.getInstance(AriaManager.APP).destroySchedulerListener(dialog);
}
};
}
}

@ -16,12 +16,11 @@
package com.arialyy.aria.core.command.download;
import com.arialyy.aria.core.DownloadManager;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.DownloadTaskEntity;
import com.arialyy.aria.core.queue.ITaskQueue;
import com.arialyy.aria.util.CheckUtil;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.core.DownloadEntity;
/**
* Created by lyy on 2016/8/22.
@ -50,7 +49,7 @@ public abstract class IDownloadCmd {
mTargetName = targetName;
mEntity = entity;
TAG = CommonUtil.getClassName(this);
mQueue = DownloadManager.getInstance().getTaskQueue();
mQueue = AriaManager.getInstance(AriaManager.APP).getTaskQueue();
}
/**

@ -1,77 +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.receiver;
import android.support.annotation.NonNull;
import com.arialyy.aria.core.AMTarget;
import com.arialyy.aria.core.DownloadEntity;
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
import com.arialyy.aria.core.scheduler.OnSchedulerListener;
import com.arialyy.aria.util.CheckUtil;
/**
* Created by lyy on 2016/12/5.
* AM 接收器
*/
public class DownloadReceiver {
public String targetName;
public OnSchedulerListener listener;
/**
* {@link #load(String)}请使用该方法
*/
@Deprecated public AMTarget load(DownloadEntity entity) {
return new AMTarget(entity, targetName);
}
/**
* 读取下载链接
*/
public AMTarget load(@NonNull String downloadUrl) {
CheckUtil.checkDownloadUrl(downloadUrl);
DownloadEntity entity =
DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
if (entity == null) {
entity = new DownloadEntity();
}
entity.setDownloadUrl(downloadUrl);
return new AMTarget(entity, targetName);
}
/**
* 添加调度器回调
*/
public DownloadReceiver addSchedulerListener(OnSchedulerListener listener) {
this.listener = listener;
DownloadSchedulers.getInstance().addSchedulerListener(targetName, listener);
return this;
}
/**
* 移除回调
*/
public DownloadReceiver removeSchedulerListener() {
if (listener != null) {
DownloadSchedulers.getInstance().removeSchedulerListener(targetName, listener);
}
return this;
}
public void destroy() {
targetName = null;
listener = null;
}
}

@ -18,9 +18,8 @@ package com.arialyy.aria.core.scheduler;
import android.os.CountDownTimer;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.DownloadManager;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.queue.ITaskQueue;
import com.arialyy.aria.core.DownloadEntity;
import com.arialyy.aria.core.task.Task;
@ -75,11 +74,10 @@ public class DownloadSchedulers implements IDownloadSchedulers {
* 下载器任务监听
*/
Map<String, OnSchedulerListener> mSchedulerListeners = new ConcurrentHashMap<>();
DownloadManager mManager = DownloadManager.getInstance();
ITaskQueue mQueue;
private DownloadSchedulers() {
mQueue = mManager.getTaskQueue();
mQueue = AriaManager.getInstance(AriaManager.APP).getTaskQueue();
}
public static DownloadSchedulers getInstance() {

@ -16,7 +16,7 @@
package com.arialyy.aria.util;
import android.util.Log;
import com.arialyy.aria.core.DownloadManager;
import com.arialyy.aria.core.AriaManager;
import java.io.File;
import java.io.IOException;
import java.util.Map;
@ -67,7 +67,7 @@ public class Configuration {
}
private Configuration() {
mConfigFile = new File(DownloadManager.APP.getFilesDir().getPath() + CONFIG_FILE);
mConfigFile = new File(AriaManager.APP.getFilesDir().getPath() + CONFIG_FILE);
try {
if (!mConfigFile.exists()) {
mConfigFile.getParentFile().mkdirs();

@ -16,10 +16,7 @@
package com.arialyy.aria.util;
import android.text.TextUtils;
import com.arialyy.aria.core.DownloadManager;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import com.arialyy.aria.core.AriaManager;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
@ -42,8 +39,8 @@ import javax.net.ssl.X509TrustManager;
/**
* Created by Aria.Lao on 2017/1/11.
* SSL证书工具
*/
public class SSLContextUtil {
/**
@ -61,7 +58,7 @@ public class SSLContextUtil {
CertificateFactory cf = null;
try {
cf = CertificateFactory.getInstance("X.509");
InputStream caInput = DownloadManager.APP.getAssets().open(caPath);
InputStream caInput = AriaManager.APP.getAssets().open(caPath);
Certificate ca;
ca = cf.generateCertificate(caInput);
System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());

@ -18,7 +18,6 @@
package com.arialyy.simple.base;
import android.app.Application;
import com.arialyy.aria.core.DownloadManager;
import com.arialyy.frame.core.AbsFrame;
/**
@ -28,6 +27,5 @@ public class BaseApplication extends Application {
@Override public void onCreate() {
super.onCreate();
AbsFrame.init(this);
//DownloadManager.init(this);
}
}

@ -7,7 +7,7 @@ import android.widget.Button;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;
import com.arialyy.aria.core.AMTarget;
import com.arialyy.aria.core.DownloadTarget;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.DownloadEntity;
import com.arialyy.aria.core.task.Task;
@ -40,13 +40,13 @@ public class DownloadDialog extends AbsDialog {
}
private void init() {
if (Aria.get(this).taskExists(DOWNLOAD_URL)) {
AMTarget target = Aria.download(this).load(DOWNLOAD_URL);
if (Aria.download(this).taskExists(DOWNLOAD_URL)) {
DownloadTarget target = Aria.download(this).load(DOWNLOAD_URL);
int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize());
mPb.setProgress(p);
}
Aria.download(this).addSchedulerListener(new MyDialogDownloadCallback());
DownloadEntity entity = Aria.get(this).getDownloadEntity(DOWNLOAD_URL);
DownloadEntity entity = Aria.download(this).getDownloadEntity(DOWNLOAD_URL);
if (entity != null) {
mSize.setText(CommonUtil.formatFileSize(entity.getFileSize()));
int state = entity.getState();

@ -7,7 +7,7 @@ import android.widget.Button;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;
import com.arialyy.aria.core.AMTarget;
import com.arialyy.aria.core.DownloadTarget;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.DownloadEntity;
import com.arialyy.aria.core.task.Task;
@ -32,12 +32,12 @@ public class DownloadFragment extends AbsFragment<FragmentDownloadBinding> {
"http://static.gaoshouyou.com/d/3a/93/573ae1db9493a801c24bf66128b11e39.apk";
@Override protected void init(Bundle savedInstanceState) {
if (Aria.get(this).taskExists(DOWNLOAD_URL)) {
AMTarget target = Aria.download(this).load(DOWNLOAD_URL);
if (Aria.download(this).taskExists(DOWNLOAD_URL)) {
DownloadTarget target = Aria.download(this).load(DOWNLOAD_URL);
int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize());
mPb.setProgress(p);
}
DownloadEntity entity = Aria.get(this).getDownloadEntity(DOWNLOAD_URL);
DownloadEntity entity = Aria.download(this).getDownloadEntity(DOWNLOAD_URL);
if (entity != null) {
mSize.setText(CommonUtil.formatFileSize(entity.getFileSize()));
int state = entity.getState();

@ -86,7 +86,7 @@ public class DownloadModule extends BaseModule {
String[] urls = getContext().getResources().getStringArray(R.array.test_apk_download_url);
List<DownloadEntity> list = new ArrayList<>();
for (String url : urls) {
DownloadEntity entity = Aria.get(getContext()).getDownloadEntity(url);
DownloadEntity entity = Aria.download(getContext()).getDownloadEntity(url);
if (entity == null) {
entity = createDownloadEntity(url);
}

@ -25,7 +25,7 @@ public class DownloadActivity extends BaseActivity<ActivityDownloadBinding> {
@Override protected void init(Bundle savedInstanceState) {
super.init(savedInstanceState);
mAdapter = new DownloadAdapter(this, Aria.get(this).getDownloadList());
mAdapter = new DownloadAdapter(this, Aria.download(this).getDownloadList());
mList.setLayoutManager(new LinearLayoutManager(this));
mList.setAdapter(mAdapter);
}

@ -62,7 +62,7 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
dialog.show(getSupportFragmentManager(), "download_num");
break;
case R.id.stop_all:
Aria.get(this).stopAllTask();
Aria.download(this).stopAllTask();
break;
case R.id.turn:
startActivity(new Intent(this, DownloadActivity.class));

@ -9,7 +9,7 @@ import android.widget.Button;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;
import com.arialyy.aria.core.AMTarget;
import com.arialyy.aria.core.DownloadTarget;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.DownloadEntity;
import com.arialyy.aria.core.task.Task;
@ -42,13 +42,13 @@ public class DownloadPopupWindow extends AbsPopupWindow {
}
private void initWidget() {
if (Aria.get(this).taskExists(DOWNLOAD_URL)) {
AMTarget target = Aria.download(this).load(DOWNLOAD_URL);
if (Aria.download(this).taskExists(DOWNLOAD_URL)) {
DownloadTarget target = Aria.download(this).load(DOWNLOAD_URL);
int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize());
mPb.setProgress(p);
}
Aria.download(this).addSchedulerListener(new MyDialogDownloadCallback());
DownloadEntity entity = Aria.get(this).getDownloadEntity(DOWNLOAD_URL);
DownloadEntity entity = Aria.download(this).getDownloadEntity(DOWNLOAD_URL);
if (entity != null) {
mSize.setText(CommonUtil.formatFileSize(entity.getFileSize()));
int state = entity.getState();

@ -30,7 +30,7 @@ import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import com.arialyy.aria.core.AMTarget;
import com.arialyy.aria.core.DownloadTarget;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.DownloadEntity;
import com.arialyy.aria.core.task.Task;
@ -161,8 +161,8 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
}
private void init() {
if (Aria.get(this).taskExists(DOWNLOAD_URL)) {
AMTarget target = Aria.download(this).load(DOWNLOAD_URL);
if (Aria.download(this).taskExists(DOWNLOAD_URL)) {
DownloadTarget target = Aria.download(this).load(DOWNLOAD_URL);
int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize());
mPb.setProgress(p);
}

Loading…
Cancel
Save