parent
62d6434914
commit
90c7cd78ff
@ -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); |
||||
} |
||||
} |
||||
} |
@ -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); |
||||
} |
||||
}; |
||||
} |
||||
} |
@ -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; |
||||
} |
||||
} |
Loading…
Reference in new issue