commit
						99d857f338
					
				| @ -1,76 +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.support.annotation.NonNull; |  | ||||||
| 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 AMReceiver { |  | ||||||
|   String              targetName; |  | ||||||
|   OnSchedulerListener listener; |  | ||||||
|   Object              obj; |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * {@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 AMReceiver addSchedulerListener(OnSchedulerListener listener) { |  | ||||||
|     this.listener = listener; |  | ||||||
|     DownloadSchedulers.getInstance().addSchedulerListener(targetName, listener); |  | ||||||
|     return this; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * 移除回调 |  | ||||||
|    */ |  | ||||||
|   public AMReceiver removeSchedulerListener() { |  | ||||||
|     if (listener != null) { |  | ||||||
|       DownloadSchedulers.getInstance().removeSchedulerListener(targetName, listener); |  | ||||||
|     } |  | ||||||
|     return this; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   void destroy() { |  | ||||||
|     targetName = null; |  | ||||||
|     listener = null; |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| @ -1,151 +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.support.annotation.NonNull; |  | ||||||
| import android.text.TextUtils; |  | ||||||
| import com.arialyy.aria.core.command.CmdFactory; |  | ||||||
| import com.arialyy.aria.core.command.IDownloadCmd; |  | ||||||
| import com.arialyy.aria.util.CheckUtil; |  | ||||||
| import com.arialyy.aria.util.CommonUtil; |  | ||||||
| import java.util.ArrayList; |  | ||||||
| import java.util.List; |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * Created by lyy on 2016/12/5. |  | ||||||
|  * https://github.com/AriaLyy/Aria
 |  | ||||||
|  */ |  | ||||||
| public class AMTarget { |  | ||||||
|   //private AMReceiver mReceiver;
 |  | ||||||
|   DownloadEntity entity; |  | ||||||
|   String targetName; |  | ||||||
| 
 |  | ||||||
|   AMTarget(DownloadEntity entity, String targetName) { |  | ||||||
|     this.entity = entity; |  | ||||||
|     this.targetName = targetName; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * 设置文件存储路径 |  | ||||||
|    */ |  | ||||||
|   public AMTarget setDownloadPath(@NonNull String downloadPath) { |  | ||||||
|     if (TextUtils.isEmpty(downloadPath)) { |  | ||||||
|       throw new IllegalArgumentException("文件保持路径不能为null"); |  | ||||||
|     } |  | ||||||
|     entity.setDownloadPath(downloadPath); |  | ||||||
|     return this; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * 设置文件名 |  | ||||||
|    */ |  | ||||||
|   public AMTarget setDownloadName(@NonNull String downloadName) { |  | ||||||
|     if (TextUtils.isEmpty(downloadName)) { |  | ||||||
|       throw new IllegalArgumentException("文件名不能为null"); |  | ||||||
|     } |  | ||||||
|     entity.setFileName(downloadName); |  | ||||||
|     return this; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * 获取下载文件大小 |  | ||||||
|    */ |  | ||||||
|   public long getFileSize() { |  | ||||||
|     DownloadEntity entity = getDownloadEntity(this.entity.getDownloadUrl()); |  | ||||||
|     if (entity == null) { |  | ||||||
|       throw new NullPointerException("下载管理器中没有改任务"); |  | ||||||
|     } |  | ||||||
|     return entity.getFileSize(); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * 获取当前下载进度,如果下載实体存在,则返回当前进度 |  | ||||||
|    */ |  | ||||||
|   public long getCurrentProgress() { |  | ||||||
|     DownloadEntity entity = getDownloadEntity(this.entity.getDownloadUrl()); |  | ||||||
|     if (entity == null) { |  | ||||||
|       throw new NullPointerException("下载管理器中没有改任务"); |  | ||||||
|     } |  | ||||||
|     return entity.getCurrentProgress(); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   private DownloadEntity getDownloadEntity(String downloadUrl) { |  | ||||||
|     CheckUtil.checkDownloadUrl(downloadUrl); |  | ||||||
|     return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * 添加任务 |  | ||||||
|    */ |  | ||||||
|   public void add() { |  | ||||||
|     DownloadManager.getInstance() |  | ||||||
|         .setCmd(CommonUtil.createCmd(targetName, entity, CmdFactory.TASK_CREATE)) |  | ||||||
|         .exe(); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * 开始下载 |  | ||||||
|    */ |  | ||||||
|   public void start() { |  | ||||||
|     List<IDownloadCmd> cmds = new ArrayList<>(); |  | ||||||
|     cmds.add(CommonUtil.createCmd(targetName, entity, CmdFactory.TASK_CREATE)); |  | ||||||
|     cmds.add(CommonUtil.createCmd(targetName, entity, CmdFactory.TASK_START)); |  | ||||||
|     DownloadManager.getInstance().setCmds(cmds).exe(); |  | ||||||
|     cmds.clear(); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * 停止下载 |  | ||||||
|    */ |  | ||||||
|   public void stop() { |  | ||||||
|     DownloadManager.getInstance() |  | ||||||
|         .setCmd(CommonUtil.createCmd(targetName, entity, CmdFactory.TASK_STOP)) |  | ||||||
|         .exe(); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * 恢复下载 |  | ||||||
|    */ |  | ||||||
|   public void resume() { |  | ||||||
|     DownloadManager.getInstance() |  | ||||||
|         .setCmd(CommonUtil.createCmd(targetName, entity, CmdFactory.TASK_START)) |  | ||||||
|         .exe(); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * 取消下载 |  | ||||||
|    */ |  | ||||||
|   public void cancel() { |  | ||||||
|     DownloadManager.getInstance() |  | ||||||
|         .setCmd(CommonUtil.createCmd(targetName, entity, CmdFactory.TASK_CANCEL)) |  | ||||||
|         .exe(); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * 是否在下载 |  | ||||||
|    */ |  | ||||||
|   public boolean isDownloading() { |  | ||||||
|     return DownloadManager.getInstance().getTaskQueue().getTask(entity).isDownloading(); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * 重新下载 |  | ||||||
|    */ |  | ||||||
|   public void reStart() { |  | ||||||
|     cancel(); |  | ||||||
|     start(); |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| @ -1,8 +0,0 @@ | |||||||
| package com.arialyy.aria.core; |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * Created by Aria.Lao on 2017/1/18. |  | ||||||
|  * AM 上传文件接收器 |  | ||||||
|  */ |  | ||||||
| public class AMUplodReceiver { |  | ||||||
| } |  | ||||||
| @ -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.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(); |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| @ -1,10 +0,0 @@ | |||||||
| package com.arialyy.aria.core; |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * Created by Aria.Lao on 2017/1/23. |  | ||||||
|  * 任务实体 |  | ||||||
|  */ |  | ||||||
| public class TaskEntity { |  | ||||||
|   public DownloadEntity downloadEntity; |  | ||||||
|   public RequestEnum requestEnum = RequestEnum.GET; |  | ||||||
| } |  | ||||||
| @ -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,48 +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.command; |  | ||||||
| 
 |  | ||||||
| import android.util.Log; |  | ||||||
| import com.arialyy.aria.core.DownloadEntity; |  | ||||||
| import com.arialyy.aria.core.task.Task; |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * Created by lyy on 2016/11/30. |  | ||||||
|  * 获取任务状态命令 |  | ||||||
|  */ |  | ||||||
| class SingleCmd extends IDownloadCmd { |  | ||||||
|   /** |  | ||||||
|    * @param entity 下载实体 |  | ||||||
|    */ |  | ||||||
|   SingleCmd(String target, DownloadEntity entity) { |  | ||||||
|     super(target, entity); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   SingleCmd(DownloadEntity entity) { |  | ||||||
|     super(entity); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   @Override public void executeCmd() { |  | ||||||
|     Task task = mQueue.getTask(mEntity); |  | ||||||
|     if (task == null) { |  | ||||||
|       task = mQueue.createTask(mTargetName, mEntity); |  | ||||||
|     } else { |  | ||||||
|       Log.w(TAG, "添加命令执行失败,【该任务已经存在】"); |  | ||||||
|     } |  | ||||||
|     task.setTargetName(mTargetName); |  | ||||||
|     mQueue.startTask(task); |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| @ -0,0 +1,139 @@ | |||||||
|  | /* | ||||||
|  |  * 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.support.annotation.NonNull; | ||||||
|  | import com.arialyy.aria.core.AriaManager; | ||||||
|  | import com.arialyy.aria.core.inf.IReceiver; | ||||||
|  | import com.arialyy.aria.core.command.CmdFactory; | ||||||
|  | import com.arialyy.aria.core.command.AbsCmd; | ||||||
|  | import com.arialyy.aria.core.scheduler.DownloadSchedulers; | ||||||
|  | import com.arialyy.aria.core.scheduler.ISchedulerListener; | ||||||
|  | import com.arialyy.aria.orm.DbEntity; | ||||||
|  | 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<DownloadEntity> { | ||||||
|  |   private static final String TAG = "DownloadReceiver"; | ||||||
|  |   public String targetName; | ||||||
|  |   public ISchedulerListener<DownloadTask> 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(ISchedulerListener<DownloadTask> 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 DownloadEntity getDownloadEntity(String downloadUrl) { | ||||||
|  |     CheckUtil.checkDownloadUrl(downloadUrl); | ||||||
|  |     return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 下载任务是否存在 | ||||||
|  |    */ | ||||||
|  |   @Override public boolean taskExists(String downloadUrl) { | ||||||
|  |     return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl) != null; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public List<DownloadEntity> getTaskList() { | ||||||
|  |     return DownloadEntity.findAllData(DownloadEntity.class); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 停止所有正在下载的任务 | ||||||
|  |    */ | ||||||
|  |   @Override public void stopAllTask() { | ||||||
|  |     final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP); | ||||||
|  |     List<DownloadEntity> allEntity = DbEntity.findAllData(DownloadEntity.class); | ||||||
|  |     List<AbsCmd> stopCmds = new ArrayList<>(); | ||||||
|  |     for (DownloadEntity entity : allEntity) { | ||||||
|  |       if (entity.getState() == DownloadEntity.STATE_RUNNING) { | ||||||
|  |         stopCmds.add( | ||||||
|  |             CommonUtil.createCmd(targetName, new DownloadTaskEntity(entity), CmdFactory.TASK_STOP)); | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |     ariaManager.setCmds(stopCmds).exe(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 删除所有任务 | ||||||
|  |    */ | ||||||
|  |   @Override public void removeAllTask() { | ||||||
|  |     final AriaManager ariaManager = AriaManager.getInstance(AriaManager.APP); | ||||||
|  |     List<DownloadEntity> allEntity = DbEntity.findAllData(DownloadEntity.class); | ||||||
|  |     List<AbsCmd> cancelCmds = new ArrayList<>(); | ||||||
|  |     for (DownloadEntity entity : allEntity) { | ||||||
|  |       cancelCmds.add( | ||||||
|  |           CommonUtil.createCmd(targetName, new DownloadTaskEntity(entity), CmdFactory.TASK_CANCEL)); | ||||||
|  |     } | ||||||
|  |     ariaManager.setCmds(cancelCmds).exe(); | ||||||
|  |     Set<String> keys = ariaManager.getReceiver().keySet(); | ||||||
|  |     for (String key : keys) { | ||||||
|  |       IReceiver receiver = ariaManager.getReceiver().get(key); | ||||||
|  |       receiver.removeSchedulerListener(); | ||||||
|  |       ariaManager.getReceiver().remove(key); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -0,0 +1,113 @@ | |||||||
|  | /* | ||||||
|  |  * 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.support.annotation.NonNull; | ||||||
|  | import android.text.TextUtils; | ||||||
|  | import com.arialyy.aria.core.RequestEnum; | ||||||
|  | import com.arialyy.aria.core.inf.AbsTarget; | ||||||
|  | import com.arialyy.aria.core.queue.DownloadTaskQueue; | ||||||
|  | import com.arialyy.aria.util.CheckUtil; | ||||||
|  | import java.io.File; | ||||||
|  | import java.util.Map; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by lyy on 2016/12/5. | ||||||
|  |  * https://github.com/AriaLyy/Aria
 | ||||||
|  |  */ | ||||||
|  | public class DownloadTarget extends AbsTarget<DownloadEntity, DownloadTaskEntity> { | ||||||
|  | 
 | ||||||
|  |   DownloadTarget(DownloadEntity entity, String targetName) { | ||||||
|  |     this.entity = entity; | ||||||
|  |     this.targetName = targetName; | ||||||
|  |     taskEntity = new DownloadTaskEntity(entity); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public void pause() { | ||||||
|  |     super.pause(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public void resume() { | ||||||
|  |     super.resume(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 给url请求添加头部 | ||||||
|  |    * | ||||||
|  |    * @param key 头部key | ||||||
|  |    * @param header 头部value | ||||||
|  |    */ | ||||||
|  |   public DownloadTarget addHeader(@NonNull String key, @NonNull String header) { | ||||||
|  |     super._addHeader(key, header); | ||||||
|  |     return this; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 给url请求添加头部 | ||||||
|  |    * | ||||||
|  |    * @param headers key为http头部的key,Value为http头对应的配置 | ||||||
|  |    */ | ||||||
|  |   public DownloadTarget addHeaders(Map<String, String> headers) { | ||||||
|  |     super._addHeaders(headers); | ||||||
|  |     return this; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 设置请求类型 | ||||||
|  |    * | ||||||
|  |    * @param requestEnum {@link RequestEnum} | ||||||
|  |    */ | ||||||
|  |   public DownloadTarget setRequestMode(RequestEnum requestEnum) { | ||||||
|  |     super._setRequestMode(requestEnum); | ||||||
|  |     return this; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 设置文件存储路径 | ||||||
|  |    */ | ||||||
|  |   public DownloadTarget setDownloadPath(@NonNull String downloadPath) { | ||||||
|  |     if (TextUtils.isEmpty(downloadPath)) { | ||||||
|  |       throw new IllegalArgumentException("文件保持路径不能为null"); | ||||||
|  |     } | ||||||
|  |     File file = new File(downloadPath); | ||||||
|  |     entity.setDownloadPath(downloadPath); | ||||||
|  |     entity.setFileName(file.getName()); | ||||||
|  |     return this; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 设置文件名 | ||||||
|  |    */ | ||||||
|  |   @Deprecated public DownloadTarget setDownloadName(@NonNull String downloadName) { | ||||||
|  |     if (TextUtils.isEmpty(downloadName)) { | ||||||
|  |       throw new IllegalArgumentException("文件名不能为null"); | ||||||
|  |     } | ||||||
|  |     entity.setFileName(downloadName); | ||||||
|  |     return this; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   private DownloadEntity getDownloadEntity(String downloadUrl) { | ||||||
|  |     CheckUtil.checkDownloadUrl(downloadUrl); | ||||||
|  |     return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 是否在下载 | ||||||
|  |    */ | ||||||
|  |   public boolean isDownloading() { | ||||||
|  |     return DownloadTaskQueue.getInstance().getTask(entity).isRunning(); | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -0,0 +1,36 @@ | |||||||
|  | /* | ||||||
|  |  * 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 com.arialyy.aria.core.inf.IEntity; | ||||||
|  | import com.arialyy.aria.core.inf.ITaskEntity; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by Aria.Lao on 2017/1/23. | ||||||
|  |  * 下载任务实体 | ||||||
|  |  */ | ||||||
|  | public class DownloadTaskEntity extends ITaskEntity { | ||||||
|  | 
 | ||||||
|  |   public DownloadEntity downloadEntity; | ||||||
|  | 
 | ||||||
|  |   public DownloadTaskEntity(DownloadEntity downloadEntity) { | ||||||
|  |     this.downloadEntity = downloadEntity; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public IEntity getEntity() { | ||||||
|  |     return downloadEntity; | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -0,0 +1,173 @@ | |||||||
|  | /* | ||||||
|  |  * 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请求添加头部 | ||||||
|  |    */ | ||||||
|  |   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(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 停止下载 | ||||||
|  |    */ | ||||||
|  |   protected void pause() { | ||||||
|  |     AriaManager.getInstance(AriaManager.APP) | ||||||
|  |         .setCmd(CommonUtil.createCmd(targetName, taskEntity, CmdFactory.TASK_STOP)) | ||||||
|  |         .exe(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 恢复下载 | ||||||
|  |    */ | ||||||
|  |   protected 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(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 重新下载 | ||||||
|  |    */ | ||||||
|  |   void reStart() { | ||||||
|  |     cancel(); | ||||||
|  |     start(); | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -0,0 +1,27 @@ | |||||||
|  | /* | ||||||
|  |  * 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; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by Aria.Lao on 2017/2/9. | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | public interface ICmd { | ||||||
|  |   /** | ||||||
|  |    * 执行命令 | ||||||
|  |    */ | ||||||
|  |   public abstract void executeCmd(); | ||||||
|  | } | ||||||
| @ -0,0 +1,63 @@ | |||||||
|  | /* | ||||||
|  |  * 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.orm.Ignore; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by Aria.Lao on 2017/2/23. | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | public interface IEntity { | ||||||
|  |   /** | ||||||
|  |    * 其它状态 | ||||||
|  |    */ | ||||||
|  |   @Ignore public static final int STATE_OTHER = -1; | ||||||
|  |   /** | ||||||
|  |    * 失败状态 | ||||||
|  |    */ | ||||||
|  |   @Ignore public static final int STATE_FAIL = 0; | ||||||
|  |   /** | ||||||
|  |    * 完成状态 | ||||||
|  |    */ | ||||||
|  |   @Ignore public static final int STATE_COMPLETE = 1; | ||||||
|  |   /** | ||||||
|  |    * 停止状态 | ||||||
|  |    */ | ||||||
|  |   @Ignore public static final int STATE_STOP = 2; | ||||||
|  |   /** | ||||||
|  |    * 未开始状态 | ||||||
|  |    */ | ||||||
|  |   @Ignore public static final int STATE_WAIT = 3; | ||||||
|  |   /** | ||||||
|  |    * 下载中 | ||||||
|  |    */ | ||||||
|  |   @Ignore public static final int STATE_RUNNING = 4; | ||||||
|  |   /** | ||||||
|  |    * 预处理 | ||||||
|  |    */ | ||||||
|  |   @Ignore public static final int STATE_PRE = 5; | ||||||
|  |   /** | ||||||
|  |    * 预处理完成 | ||||||
|  |    */ | ||||||
|  |   @Ignore public static final int STATE_POST_PRE = 6; | ||||||
|  |   /** | ||||||
|  |    * 取消下载 | ||||||
|  |    */ | ||||||
|  |   @Ignore public static final int STATE_CANCEL = 7; | ||||||
|  | 
 | ||||||
|  |   public int getState(); | ||||||
|  | } | ||||||
| @ -0,0 +1,55 @@ | |||||||
|  | /* | ||||||
|  |  * 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 java.util.List; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by Aria.Lao on 2017/2/6. | ||||||
|  |  */ | ||||||
|  | public interface IReceiver<ENTITY extends IEntity> { | ||||||
|  |   /** | ||||||
|  |    * Receiver 销毁 | ||||||
|  |    */ | ||||||
|  |   public void destroy(); | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 移除事件回调 | ||||||
|  |    */ | ||||||
|  |   public void removeSchedulerListener(); | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 停止所有任务 | ||||||
|  |    */ | ||||||
|  |   public void stopAllTask(); | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 删除所有任务 | ||||||
|  |    */ | ||||||
|  |   public void removeAllTask(); | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 任务是否存在 | ||||||
|  |    * | ||||||
|  |    * @param key 下载时为下载路径,上传时为文件路径 | ||||||
|  |    */ | ||||||
|  |   public boolean taskExists(String key); | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 获取任务列表 | ||||||
|  |    */ | ||||||
|  |   public List<ENTITY> getTaskList(); | ||||||
|  | } | ||||||
| @ -0,0 +1,54 @@ | |||||||
|  | /* | ||||||
|  |  * 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; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by Aria.Lao on 2017/2/13. | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | public interface ITask { | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 唯一标识符,DownloadTask 为下载地址,UploadTask 为文件路径 | ||||||
|  |    */ | ||||||
|  |   public String getKey(); | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 是否真正执行 | ||||||
|  |    * | ||||||
|  |    * @return true,正在执行; | ||||||
|  |    */ | ||||||
|  |   public boolean isRunning(); | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 获取工具实体 | ||||||
|  |    */ | ||||||
|  |   public IEntity getEntity(); | ||||||
|  | 
 | ||||||
|  |   public void start(); | ||||||
|  | 
 | ||||||
|  |   public void stop(); | ||||||
|  | 
 | ||||||
|  |   public void cancel(); | ||||||
|  | 
 | ||||||
|  |   public long getSpeed(); | ||||||
|  | 
 | ||||||
|  |   public long getFileSize(); | ||||||
|  | 
 | ||||||
|  |   public long getCurrentProgress(); | ||||||
|  | 
 | ||||||
|  |   public void setTargetName(String targetName); | ||||||
|  | } | ||||||
| @ -0,0 +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 abstract class ITaskEntity { | ||||||
|  |   /** | ||||||
|  |    * http 请求头 | ||||||
|  |    */ | ||||||
|  |   public Map<String, String> headers = new HashMap<>(); | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 网络请求类型 | ||||||
|  |    */ | ||||||
|  |   public RequestEnum requestEnum = RequestEnum.GET; | ||||||
|  | 
 | ||||||
|  |   public abstract IEntity getEntity(); | ||||||
|  | } | ||||||
| @ -0,0 +1,93 @@ | |||||||
|  | /* | ||||||
|  |  * 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,29 @@ | |||||||
|  | /* | ||||||
|  |  * 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 com.arialyy.aria.core.inf.ITask; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by Aria.Lao on 2017/4/5. | ||||||
|  |  */ | ||||||
|  | public interface IDownloadSchedulerListener<TASK extends ITask> extends ISchedulerListener<TASK> { | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 支持断点的回调 | ||||||
|  |    */ | ||||||
|  |   public void onNoSupportBreakPoint(TASK task); | ||||||
|  | } | ||||||
| @ -0,0 +1,186 @@ | |||||||
|  | /* | ||||||
|  |  * 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, ISchedulerListener<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, | ||||||
|  |       ISchedulerListener<UploadTask> schedulerListener) { | ||||||
|  |     mSchedulerListeners.put(targetName, schedulerListener); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public void removeSchedulerListener(String targetName, | ||||||
|  |       ISchedulerListener<UploadTask> schedulerListener) { | ||||||
|  |     for (Iterator<Map.Entry<String, ISchedulerListener<UploadTask>>> iter = | ||||||
|  |         mSchedulerListeners.entrySet().iterator(); iter.hasNext(); ) { | ||||||
|  |       Map.Entry<String, ISchedulerListener<UploadTask>> entry = iter.next(); | ||||||
|  |       if (entry.getKey().equals(targetName)) iter.remove(); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   private void handleFailTask(final UploadTask task) { | ||||||
|  |     final Configuration config = Configuration.getInstance(); | ||||||
|  |     CountDownTimer timer = new CountDownTimer(config.getReTryInterval(), 1000) { | ||||||
|  |       @Override public void onTick(long millisUntilFinished) { | ||||||
|  | 
 | ||||||
|  |       } | ||||||
|  | 
 | ||||||
|  |       @Override public void onFinish() { | ||||||
|  |         UploadEntity entity = task.getUploadEntity(); | ||||||
|  |         if (entity.getFailNum() <= config.getReTryNum()) { | ||||||
|  |           UploadTask task = mQueue.getTask(entity); | ||||||
|  |           mQueue.reTryStart(task); | ||||||
|  |           try { | ||||||
|  |             Thread.sleep(config.getReTryInterval()); | ||||||
|  |           } catch (InterruptedException e) { | ||||||
|  |             e.printStackTrace(); | ||||||
|  |           } | ||||||
|  |         } else { | ||||||
|  |           mQueue.removeTask(entity); | ||||||
|  |           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, ISchedulerListener<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: | ||||||
|  |         handleFailTask(task); | ||||||
|  |         break; | ||||||
|  |     } | ||||||
|  |     return true; | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -1,62 +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.task; |  | ||||||
| 
 |  | ||||||
| import android.content.Context; |  | ||||||
| import com.arialyy.aria.core.DownloadEntity; |  | ||||||
| import com.arialyy.aria.core.scheduler.IDownloadSchedulers; |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * 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 {@link IDownloadSchedulers} |  | ||||||
|    */ |  | ||||||
|   public Task createTask(Context context, DownloadEntity entity, IDownloadSchedulers schedulers) { |  | ||||||
|     return createTask("", context, entity, schedulers); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   public Task createTask(String targetName, Context context, DownloadEntity entity, |  | ||||||
|       IDownloadSchedulers schedulers) { |  | ||||||
|     Task.Builder builder = new Task.Builder(targetName, context, entity); |  | ||||||
|     builder.setOutHandler(schedulers); |  | ||||||
|     return builder.build(); |  | ||||||
|   } |  | ||||||
| } |  | ||||||
| @ -0,0 +1,69 @@ | |||||||
|  | /* | ||||||
|  |  * 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; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by Aria.Lao on 2017/2/9. | ||||||
|  |  * 上传监听 | ||||||
|  |  */ | ||||||
|  | public interface IUploadListener { | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 预处理 | ||||||
|  |    */ | ||||||
|  |   public void onPre(); | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 开始上传 | ||||||
|  |    */ | ||||||
|  |   public void onStart(long fileSize); | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 恢复上传 | ||||||
|  |    * | ||||||
|  |    * @param resumeLocation 上次上传停止位置 | ||||||
|  |    */ | ||||||
|  |   public void onResume(long resumeLocation); | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 停止上传 | ||||||
|  |    * | ||||||
|  |    * @param stopLocation 上传停止位置 | ||||||
|  |    */ | ||||||
|  |   public void onStop(long stopLocation); | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 上传进度 | ||||||
|  |    * | ||||||
|  |    * @param currentLocation 当前进度 | ||||||
|  |    */ | ||||||
|  |   public void onProgress(long currentLocation); | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 取消上传 | ||||||
|  |    */ | ||||||
|  |   public void onCancel(); | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 上传成功 | ||||||
|  |    */ | ||||||
|  |   public void onComplete(); | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 上传失败 | ||||||
|  |    */ | ||||||
|  |   public void onFail(); | ||||||
|  | } | ||||||
| @ -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.upload; | ||||||
|  | 
 | ||||||
|  | import android.os.Parcel; | ||||||
|  | import android.os.Parcelable; | ||||||
|  | import com.arialyy.aria.core.inf.IEntity; | ||||||
|  | import com.arialyy.aria.orm.DbEntity; | ||||||
|  | import com.arialyy.aria.orm.Ignore; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by Aria.Lao on 2017/2/9. | ||||||
|  |  * 上传文件实体 | ||||||
|  |  */ | ||||||
|  | public class UploadEntity extends DbEntity implements IEntity, Parcelable { | ||||||
|  | 
 | ||||||
|  |   private String filePath;  //文件路径
 | ||||||
|  |   private String fileName;  //文件名
 | ||||||
|  |   private long fileSize;    //文件大小
 | ||||||
|  |   private int state = STATE_WAIT; | ||||||
|  |   private long currentProgress = 0; | ||||||
|  |   private boolean isComplete = false; | ||||||
|  |   @Ignore private long speed = 0; //下载速度
 | ||||||
|  |   @Ignore private int failNum = 0; | ||||||
|  | 
 | ||||||
|  |   public boolean isComplete() { | ||||||
|  |     return isComplete; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public void setComplete(boolean complete) { | ||||||
|  |     isComplete = complete; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public long getCurrentProgress() { | ||||||
|  |     return currentProgress; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public void setCurrentProgress(long currentProgress) { | ||||||
|  |     this.currentProgress = currentProgress; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public long getFileSize() { | ||||||
|  |     return fileSize; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public void setFileSize(long fileSize) { | ||||||
|  |     this.fileSize = fileSize; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public long getSpeed() { | ||||||
|  |     return speed; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public void setSpeed(long speed) { | ||||||
|  |     this.speed = speed; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public int getFailNum() { | ||||||
|  |     return failNum; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public void setFailNum(int failNum) { | ||||||
|  |     this.failNum = failNum; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public int getState() { | ||||||
|  |     return state; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public void setState(int state) { | ||||||
|  |     this.state = state; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public String getFilePath() { | ||||||
|  |     return filePath; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public void setFilePath(String filePath) { | ||||||
|  |     this.filePath = filePath; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public String getFileName() { | ||||||
|  |     return fileName; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public void setFileName(String fileName) { | ||||||
|  |     this.fileName = fileName; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public UploadEntity() { | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public int describeContents() { | ||||||
|  |     return 0; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public void writeToParcel(Parcel dest, int flags) { | ||||||
|  |     dest.writeString(this.filePath); | ||||||
|  |     dest.writeString(this.fileName); | ||||||
|  |     dest.writeLong(this.fileSize); | ||||||
|  |     dest.writeInt(this.state); | ||||||
|  |     dest.writeLong(this.currentProgress); | ||||||
|  |     dest.writeByte(this.isComplete ? (byte) 1 : (byte) 0); | ||||||
|  |     dest.writeLong(this.speed); | ||||||
|  |     dest.writeInt(this.failNum); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   protected UploadEntity(Parcel in) { | ||||||
|  |     this.filePath = in.readString(); | ||||||
|  |     this.fileName = in.readString(); | ||||||
|  |     this.fileSize = in.readLong(); | ||||||
|  |     this.state = in.readInt(); | ||||||
|  |     this.currentProgress = in.readLong(); | ||||||
|  |     this.isComplete = in.readByte() != 0; | ||||||
|  |     this.speed = in.readLong(); | ||||||
|  |     this.failNum = in.readInt(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Ignore public static final Creator<UploadEntity> CREATOR = new Creator<UploadEntity>() { | ||||||
|  |     @Override public UploadEntity createFromParcel(Parcel source) { | ||||||
|  |       return new UploadEntity(source); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public UploadEntity[] newArray(int size) { | ||||||
|  |       return new UploadEntity[size]; | ||||||
|  |     } | ||||||
|  |   }; | ||||||
|  | } | ||||||
| @ -0,0 +1,54 @@ | |||||||
|  | /* | ||||||
|  |  * 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; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by Aria.Lao on 2017/2/23. | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | public class UploadListener implements IUploadListener { | ||||||
|  |   @Override public void onPre() { | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public void onStart(long fileSize) { | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public void onResume(long resumeLocation) { | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public void onStop(long stopLocation) { | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public void onProgress(long currentLocation) { | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public void onCancel() { | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public void onComplete() { | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public void onFail() { | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -0,0 +1,130 @@ | |||||||
|  | /* | ||||||
|  |  * 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.AriaManager; | ||||||
|  | import com.arialyy.aria.core.command.AbsCmd; | ||||||
|  | import com.arialyy.aria.core.command.CmdFactory; | ||||||
|  | import com.arialyy.aria.core.inf.IEntity; | ||||||
|  | import com.arialyy.aria.core.inf.IReceiver; | ||||||
|  | import com.arialyy.aria.core.scheduler.ISchedulerListener; | ||||||
|  | import com.arialyy.aria.core.scheduler.UploadSchedulers; | ||||||
|  | import com.arialyy.aria.orm.DbEntity; | ||||||
|  | import com.arialyy.aria.util.CheckUtil; | ||||||
|  | import com.arialyy.aria.util.CommonUtil; | ||||||
|  | import java.util.ArrayList; | ||||||
|  | import java.util.List; | ||||||
|  | import java.util.Set; | ||||||
|  | import java.util.regex.Pattern; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by Aria.Lao on 2017/2/6. | ||||||
|  |  * 上传功能接收器 | ||||||
|  |  */ | ||||||
|  | public class UploadReceiver implements IReceiver<UploadEntity> { | ||||||
|  |   private static final String TAG = "DownloadReceiver"; | ||||||
|  |   public String targetName; | ||||||
|  |   public ISchedulerListener<UploadTask> listener; | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 加载任务 | ||||||
|  |    * | ||||||
|  |    * @param filePath 文件地址 | ||||||
|  |    */ | ||||||
|  |   public UploadTarget load(@NonNull String filePath) { | ||||||
|  |     CheckUtil.checkUploadPath(filePath); | ||||||
|  |     UploadEntity entity = UploadEntity.findData(UploadEntity.class, "filePath=?", filePath); | ||||||
|  |     if (entity == null) { | ||||||
|  |       entity = new UploadEntity(); | ||||||
|  |     } | ||||||
|  |     String regex = "[/|\\\\|//]"; | ||||||
|  |     Pattern p = Pattern.compile(regex); | ||||||
|  |     String[] strs = p.split(filePath); | ||||||
|  |     String fileName = strs[strs.length - 1]; | ||||||
|  |     entity.setFileName(fileName); | ||||||
|  |     entity.setFilePath(filePath); | ||||||
|  |     return new UploadTarget(entity, targetName); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 通过上传路径获取上传实体 | ||||||
|  |    */ | ||||||
|  |   public UploadEntity getUploadEntity(String filePath) { | ||||||
|  |     CheckUtil.checkUploadPath(filePath); | ||||||
|  |     return DbEntity.findData(UploadEntity.class, "filePath=?", filePath); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 下载任务是否存在 | ||||||
|  |    */ | ||||||
|  |   @Override public boolean taskExists(String filePath) { | ||||||
|  |     return DbEntity.findData(UploadEntity.class, "filePath=?", filePath) != null; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public List<UploadEntity> getTaskList() { | ||||||
|  |     return DbEntity.findAllData(UploadEntity.class); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public void stopAllTask() { | ||||||
|  |     List<UploadEntity> allEntity = DbEntity.findAllData(UploadEntity.class); | ||||||
|  |     List<AbsCmd> stopCmds = new ArrayList<>(); | ||||||
|  |     for (UploadEntity entity : allEntity) { | ||||||
|  |       if (entity.getState() == IEntity.STATE_RUNNING) { | ||||||
|  |         stopCmds.add( | ||||||
|  |             CommonUtil.createCmd(targetName, new UploadTaskEntity(entity), CmdFactory.TASK_STOP)); | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |     AriaManager.getInstance(AriaManager.APP).setCmds(stopCmds).exe(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public void removeAllTask() { | ||||||
|  |     final AriaManager am = AriaManager.getInstance(AriaManager.APP); | ||||||
|  |     List<UploadEntity> allEntity = DbEntity.findAllData(UploadEntity.class); | ||||||
|  |     List<AbsCmd> cancelCmds = new ArrayList<>(); | ||||||
|  |     for (UploadEntity entity : allEntity) { | ||||||
|  |       cancelCmds.add( | ||||||
|  |           CommonUtil.createCmd(targetName, new UploadTaskEntity(entity), CmdFactory.TASK_CANCEL)); | ||||||
|  |     } | ||||||
|  |     am.setCmds(cancelCmds).exe(); | ||||||
|  |     Set<String> keys = am.getReceiver().keySet(); | ||||||
|  |     for (String key : keys) { | ||||||
|  |       IReceiver receiver = am.getReceiver().get(key); | ||||||
|  |       receiver.removeSchedulerListener(); | ||||||
|  |       am.getReceiver().remove(key); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public void destroy() { | ||||||
|  |     targetName = null; | ||||||
|  |     listener = null; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 添加调度器回调 | ||||||
|  |    */ | ||||||
|  |   public UploadReceiver addSchedulerListener(ISchedulerListener<UploadTask> listener) { | ||||||
|  |     this.listener = listener; | ||||||
|  |     UploadSchedulers.getInstance().addSchedulerListener(targetName, listener); | ||||||
|  |     return this; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public void removeSchedulerListener() { | ||||||
|  |     if (listener != null) { | ||||||
|  |       UploadSchedulers.getInstance().removeSchedulerListener(targetName, listener); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -0,0 +1,123 @@ | |||||||
|  | /* | ||||||
|  |  * 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); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 设置userAgent | ||||||
|  |    */ | ||||||
|  |   public UploadTarget setUserAngent(@NonNull String userAgent) { | ||||||
|  |     taskEntity.userAgent = userAgent; | ||||||
|  |     return this; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 设置上传路径 | ||||||
|  |    * | ||||||
|  |    * @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 tip:multipart/form-data | ||||||
|  |    */ | ||||||
|  |   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 key为http头部的key,Value为http头对应的配置 | ||||||
|  |    */ | ||||||
|  |   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(); | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -0,0 +1,260 @@ | |||||||
|  | /* | ||||||
|  |  * 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.content.Intent; | ||||||
|  | import android.os.Handler; | ||||||
|  | import android.util.Log; | ||||||
|  | import com.arialyy.aria.core.Aria; | ||||||
|  | import com.arialyy.aria.core.AriaManager; | ||||||
|  | import com.arialyy.aria.core.download.DownloadEntity; | ||||||
|  | import com.arialyy.aria.core.inf.IEntity; | ||||||
|  | import com.arialyy.aria.core.inf.ITask; | ||||||
|  | import com.arialyy.aria.core.scheduler.DownloadSchedulers; | ||||||
|  | import com.arialyy.aria.core.scheduler.ISchedulers; | ||||||
|  | import com.arialyy.aria.util.CommonUtil; | ||||||
|  | import com.arialyy.aria.util.Configuration; | ||||||
|  | import java.lang.ref.WeakReference; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by Aria.Lao on 2017/2/23. | ||||||
|  |  * 上传任务 | ||||||
|  |  */ | ||||||
|  | public class UploadTask implements ITask { | ||||||
|  |   private static final String TAG = "UploadTask"; | ||||||
|  |   private Handler mOutHandler; | ||||||
|  |   private UploadTaskEntity mTaskEntity; | ||||||
|  |   private UploadEntity mUploadEntity; | ||||||
|  |   private String mTargetName; | ||||||
|  | 
 | ||||||
|  |   private UploadUtil mUtil; | ||||||
|  |   private UListener mListener; | ||||||
|  | 
 | ||||||
|  |   UploadTask(UploadTaskEntity taskEntity, Handler outHandler) { | ||||||
|  |     mTaskEntity = taskEntity; | ||||||
|  |     mOutHandler = outHandler; | ||||||
|  |     mUploadEntity = mTaskEntity.uploadEntity; | ||||||
|  |     mListener = new UListener(mOutHandler, this); | ||||||
|  |     mUtil = new UploadUtil(mTaskEntity, mListener); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public void setTargetName(String targetName) { | ||||||
|  |     mTargetName = targetName; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public String getKey() { | ||||||
|  |     return mUploadEntity.getFilePath(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public boolean isRunning() { | ||||||
|  |     return mUtil.isRunning(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public UploadEntity getUploadEntity() { | ||||||
|  |     return mUploadEntity; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public IEntity getEntity() { | ||||||
|  |     return mUploadEntity; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public void start() { | ||||||
|  |     if (mUtil.isRunning()) { | ||||||
|  |       Log.d(TAG, "任务正在下载"); | ||||||
|  |     } else { | ||||||
|  |       if (mListener == null) { | ||||||
|  |         mListener = new UploadTask.UListener(mOutHandler, this); | ||||||
|  |       } | ||||||
|  |       mUtil.start(); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public void stop() { | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public void cancel() { | ||||||
|  |     if (mUtil.isRunning()) { | ||||||
|  |       mUtil.cancel(); | ||||||
|  |     } else { | ||||||
|  |       // 如果任务不是下载状态
 | ||||||
|  |       mUtil.cancel(); | ||||||
|  |       mUploadEntity.deleteData(); | ||||||
|  |       if (mOutHandler != null) { | ||||||
|  |         mOutHandler.obtainMessage(DownloadSchedulers.CANCEL, this).sendToTarget(); | ||||||
|  |       } | ||||||
|  |       //发送取消下载的广播
 | ||||||
|  |       Intent intent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), Aria.ACTION_CANCEL); | ||||||
|  |       intent.putExtra(Aria.ENTITY, mUploadEntity); | ||||||
|  |       AriaManager.APP.sendBroadcast(intent); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public String getTargetName() { | ||||||
|  |     return mTargetName; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public long getSpeed() { | ||||||
|  |     return mUploadEntity.getSpeed(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public long getFileSize() { | ||||||
|  |     return mUploadEntity.getFileSize(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public long getCurrentProgress() { | ||||||
|  |     return mUploadEntity.getCurrentProgress(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   private static class UListener extends UploadListener { | ||||||
|  |     WeakReference<Handler> outHandler; | ||||||
|  |     WeakReference<UploadTask> task; | ||||||
|  |     long lastLen = 0;   //上一次发送长度
 | ||||||
|  |     long lastTime = 0; | ||||||
|  |     long INTERVAL_TIME = 1000;   //1m更新周期
 | ||||||
|  |     boolean isFirst = true; | ||||||
|  |     UploadEntity entity; | ||||||
|  |     Intent sendIntent; | ||||||
|  | 
 | ||||||
|  |     UListener(Handler outHandle, UploadTask task) { | ||||||
|  |       this.outHandler = new WeakReference<>(outHandle); | ||||||
|  |       this.task = new WeakReference<>(task); | ||||||
|  |       entity = this.task.get().getUploadEntity(); | ||||||
|  |       sendIntent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), Aria.ACTION_RUNNING); | ||||||
|  |       sendIntent.putExtra(Aria.ENTITY, entity); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public void onPre() { | ||||||
|  |       entity.setState(IEntity.STATE_PRE); | ||||||
|  |       sendIntent(Aria.ACTION_PRE, -1); | ||||||
|  |       sendInState2Target(ISchedulers.PRE); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public void onStart(long fileSize) { | ||||||
|  |       entity.setFileSize(fileSize); | ||||||
|  |       entity.setState(IEntity.STATE_RUNNING); | ||||||
|  |       sendIntent(Aria.ACTION_PRE, -1); | ||||||
|  |       sendInState2Target(ISchedulers.START); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public void onResume(long resumeLocation) { | ||||||
|  |       entity.setState(DownloadEntity.STATE_RUNNING); | ||||||
|  |       sendInState2Target(DownloadSchedulers.RESUME); | ||||||
|  |       sendIntent(Aria.ACTION_RESUME, resumeLocation); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public void onStop(long stopLocation) { | ||||||
|  |       entity.setState(DownloadEntity.STATE_STOP); | ||||||
|  |       entity.setSpeed(0); | ||||||
|  |       sendInState2Target(DownloadSchedulers.STOP); | ||||||
|  |       sendIntent(Aria.ACTION_STOP, stopLocation); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public void onProgress(long currentLocation) { | ||||||
|  |       if (System.currentTimeMillis() - lastTime > INTERVAL_TIME) { | ||||||
|  |         long speed = currentLocation - lastLen; | ||||||
|  |         sendIntent.putExtra(Aria.CURRENT_LOCATION, currentLocation); | ||||||
|  |         sendIntent.putExtra(Aria.CURRENT_SPEED, speed); | ||||||
|  |         lastTime = System.currentTimeMillis(); | ||||||
|  |         if (isFirst) { | ||||||
|  |           entity.setSpeed(0); | ||||||
|  |           isFirst = false; | ||||||
|  |         } else { | ||||||
|  |           entity.setSpeed(speed); | ||||||
|  |         } | ||||||
|  |         entity.setCurrentProgress(currentLocation); | ||||||
|  |         lastLen = currentLocation; | ||||||
|  |         sendInState2Target(DownloadSchedulers.RUNNING); | ||||||
|  |         AriaManager.APP.sendBroadcast(sendIntent); | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public void onCancel() { | ||||||
|  |       entity.setState(DownloadEntity.STATE_CANCEL); | ||||||
|  |       sendInState2Target(DownloadSchedulers.CANCEL); | ||||||
|  |       sendIntent(Aria.ACTION_CANCEL, -1); | ||||||
|  |       entity.deleteData(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public void onComplete() { | ||||||
|  |       entity.setState(DownloadEntity.STATE_COMPLETE); | ||||||
|  |       entity.setComplete(true); | ||||||
|  |       entity.setSpeed(0); | ||||||
|  |       sendInState2Target(DownloadSchedulers.COMPLETE); | ||||||
|  |       sendIntent(Aria.ACTION_COMPLETE, entity.getFileSize()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public void onFail() { | ||||||
|  |       entity.setFailNum(entity.getFailNum() + 1); | ||||||
|  |       entity.setState(DownloadEntity.STATE_FAIL); | ||||||
|  |       entity.setSpeed(0); | ||||||
|  |       sendInState2Target(DownloadSchedulers.FAIL); | ||||||
|  |       sendIntent(Aria.ACTION_FAIL, -1); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * 将任务状态发送给下载器 | ||||||
|  |      * | ||||||
|  |      * @param state {@link DownloadSchedulers#START} | ||||||
|  |      */ | ||||||
|  |     private void sendInState2Target(int state) { | ||||||
|  |       if (outHandler.get() != null) { | ||||||
|  |         outHandler.get().obtainMessage(state, task.get()).sendToTarget(); | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private void sendIntent(String action, long location) { | ||||||
|  |       entity.setComplete(action.equals(Aria.ACTION_COMPLETE)); | ||||||
|  |       entity.setCurrentProgress(location); | ||||||
|  |       entity.update(); | ||||||
|  |       if (!Configuration.isOpenBreadCast) return; | ||||||
|  |       Intent intent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), action); | ||||||
|  |       intent.putExtra(Aria.ENTITY, entity); | ||||||
|  |       if (location != -1) { | ||||||
|  |         intent.putExtra(Aria.CURRENT_LOCATION, location); | ||||||
|  |       } | ||||||
|  |       AriaManager.APP.sendBroadcast(intent); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public static class Builder { | ||||||
|  |     private Handler mOutHandler; | ||||||
|  |     private UploadTaskEntity mTaskEntity; | ||||||
|  |     private String mTargetName; | ||||||
|  | 
 | ||||||
|  |     public void setOutHandler(ISchedulers outHandler) { | ||||||
|  |       mOutHandler = new Handler(outHandler); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setUploadTaskEntity(UploadTaskEntity taskEntity) { | ||||||
|  |       mTaskEntity = taskEntity; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public void setTargetName(String targetName) { | ||||||
|  |       mTargetName = targetName; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public Builder() { | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public UploadTask build() { | ||||||
|  |       UploadTask task = new UploadTask(mTaskEntity, mOutHandler); | ||||||
|  |       task.setTargetName(mTargetName); | ||||||
|  |       return task; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -0,0 +1,47 @@ | |||||||
|  | /* | ||||||
|  |  * 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 com.arialyy.aria.core.inf.IEntity; | ||||||
|  | import com.arialyy.aria.core.inf.ITaskEntity; | ||||||
|  | import java.util.HashMap; | ||||||
|  | import java.util.Map; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by Aria.Lao on 2017/2/9. | ||||||
|  |  * 上传任务实体 | ||||||
|  |  */ | ||||||
|  | public class UploadTaskEntity extends ITaskEntity { | ||||||
|  |   public UploadEntity uploadEntity; | ||||||
|  |   public String uploadUrl; //上传路径
 | ||||||
|  |   public String attachment;  //文件上传需要的key
 | ||||||
|  |   public String contentType = "multipart/form-data"; //上传的文件类型
 | ||||||
|  |   public String userAgent = "User-Agent"; | ||||||
|  |   public String charset = "utf-8"; | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 文件上传表单 | ||||||
|  |    */ | ||||||
|  |   public Map<String, String> formFields = new HashMap<>(); | ||||||
|  | 
 | ||||||
|  |   public UploadTaskEntity(UploadEntity downloadEntity) { | ||||||
|  |     this.uploadEntity = downloadEntity; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public IEntity getEntity() { | ||||||
|  |     return uploadEntity; | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -0,0 +1,230 @@ | |||||||
|  | /* | ||||||
|  |  * 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.util.Log; | ||||||
|  | import com.arialyy.aria.util.CheckUtil; | ||||||
|  | import java.io.BufferedReader; | ||||||
|  | import java.io.File; | ||||||
|  | import java.io.FileInputStream; | ||||||
|  | import java.io.IOException; | ||||||
|  | import java.io.InputStreamReader; | ||||||
|  | import java.io.OutputStream; | ||||||
|  | import java.io.OutputStreamWriter; | ||||||
|  | import java.io.PrintWriter; | ||||||
|  | import java.net.HttpURLConnection; | ||||||
|  | import java.net.URL; | ||||||
|  | import java.net.URLConnection; | ||||||
|  | import java.util.Set; | ||||||
|  | import java.util.UUID; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by Aria.Lao on 2017/2/9. | ||||||
|  |  * 上传工具 | ||||||
|  |  */ | ||||||
|  | final class UploadUtil implements Runnable { | ||||||
|  |   private static final String TAG = "UploadUtil"; | ||||||
|  |   private final String BOUNDARY = UUID.randomUUID().toString(); // 边界标识 随机生成
 | ||||||
|  |   private final String PREFIX = "--", LINE_END = "\r\n"; | ||||||
|  |   private UploadEntity mUploadEntity; | ||||||
|  |   private UploadTaskEntity mTaskEntity; | ||||||
|  |   private IUploadListener mListener; | ||||||
|  |   private HttpURLConnection mHttpConn; | ||||||
|  |   private long mCurrentLocation = 0; | ||||||
|  |   private boolean isCancel = false; | ||||||
|  |   private boolean isRunning = false; | ||||||
|  |   private OutputStream mOutputStream; | ||||||
|  | 
 | ||||||
|  |   UploadUtil(UploadTaskEntity taskEntity, IUploadListener listener) { | ||||||
|  |     mTaskEntity = taskEntity; | ||||||
|  |     CheckUtil.checkUploadTaskEntity(taskEntity.uploadEntity); | ||||||
|  |     mUploadEntity = taskEntity.uploadEntity; | ||||||
|  |     if (listener == null) { | ||||||
|  |       throw new IllegalArgumentException("上传监听不能为空"); | ||||||
|  |     } | ||||||
|  |     mListener = listener; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public void start() { | ||||||
|  |     isCancel = false; | ||||||
|  |     isRunning = false; | ||||||
|  |     new Thread(this).start(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public void cancel() { | ||||||
|  |     isCancel = true; | ||||||
|  |     isRunning = false; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public void run() { | ||||||
|  |     File uploadFile = new File(mUploadEntity.getFilePath()); | ||||||
|  |     if (!uploadFile.exists()) { | ||||||
|  |       Log.e(TAG, "【" + mUploadEntity.getFilePath() + "】,文件不存在。"); | ||||||
|  |       fail(); | ||||||
|  |       return; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     mListener.onPre(); | ||||||
|  |     URL url; | ||||||
|  |     try { | ||||||
|  |       url = new URL(mTaskEntity.uploadUrl); | ||||||
|  |       mHttpConn = (HttpURLConnection) url.openConnection(); | ||||||
|  |       mHttpConn.setUseCaches(false); | ||||||
|  |       mHttpConn.setDoOutput(true); | ||||||
|  |       mHttpConn.setDoInput(true); | ||||||
|  |       mHttpConn.setRequestProperty("Content-Type", | ||||||
|  |           mTaskEntity.contentType + "; boundary=" + BOUNDARY); | ||||||
|  |       mHttpConn.setRequestProperty("User-Agent", mTaskEntity.userAgent); | ||||||
|  |       //mHttpConn.setRequestProperty("Range", "bytes=" + 0 + "-" + "100");
 | ||||||
|  |       //内部缓冲区---分段上传防止oom
 | ||||||
|  |       mHttpConn.setChunkedStreamingMode(1024); | ||||||
|  | 
 | ||||||
|  |       //添加Http请求头部
 | ||||||
|  |       Set<String> keys = mTaskEntity.headers.keySet(); | ||||||
|  |       for (String key : keys) { | ||||||
|  |         mHttpConn.setRequestProperty(key, mTaskEntity.headers.get(key)); | ||||||
|  |       } | ||||||
|  | 
 | ||||||
|  |       mOutputStream = mHttpConn.getOutputStream(); | ||||||
|  |       PrintWriter writer = | ||||||
|  |           new PrintWriter(new OutputStreamWriter(mOutputStream, mTaskEntity.charset), true); | ||||||
|  | 
 | ||||||
|  |       //添加文件上传表单字段
 | ||||||
|  |       keys = mTaskEntity.formFields.keySet(); | ||||||
|  |       for (String key : keys) { | ||||||
|  |         addFormField(writer, key, mTaskEntity.formFields.get(key)); | ||||||
|  |       } | ||||||
|  |       mListener.onStart(uploadFile.length()); | ||||||
|  |       uploadFile(writer, mTaskEntity.attachment, uploadFile); | ||||||
|  |       Log.d(TAG, finish(writer) + ""); | ||||||
|  |     } catch (IOException e) { | ||||||
|  |       e.printStackTrace(); | ||||||
|  |       fail(); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   boolean isRunning() { | ||||||
|  |     return isRunning; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   private void fail() { | ||||||
|  |     try { | ||||||
|  |       mListener.onFail(); | ||||||
|  |       if (mOutputStream != null) { | ||||||
|  |         mOutputStream.close(); | ||||||
|  |       } | ||||||
|  |     } catch (IOException e) { | ||||||
|  |       e.printStackTrace(); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 添加文件上传表单字段 | ||||||
|  |    */ | ||||||
|  |   private void addFormField(PrintWriter writer, String name, String value) { | ||||||
|  |     writer.append(PREFIX).append(BOUNDARY).append(LINE_END); | ||||||
|  |     writer.append("Content-Disposition: form-data; name=\"") | ||||||
|  |         .append(name) | ||||||
|  |         .append("\"") | ||||||
|  |         .append(LINE_END); | ||||||
|  |     writer.append("Content-Type: text/plain; charset=") | ||||||
|  |         .append(mTaskEntity.charset) | ||||||
|  |         .append(LINE_END); | ||||||
|  |     writer.append(LINE_END); | ||||||
|  |     writer.append(value).append(LINE_END); | ||||||
|  |     writer.flush(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 上传文件 | ||||||
|  |    * | ||||||
|  |    * @param attachment 文件上传attachment | ||||||
|  |    * @throws IOException | ||||||
|  |    */ | ||||||
|  |   private void uploadFile(PrintWriter writer, String attachment, File uploadFile) | ||||||
|  |       throws IOException { | ||||||
|  |     writer.append(PREFIX).append(BOUNDARY).append(LINE_END); | ||||||
|  |     writer.append("Content-Disposition: form-data; name=\"") | ||||||
|  |         .append(attachment) | ||||||
|  |         .append("\"; filename=\"") | ||||||
|  |         .append(mTaskEntity.uploadEntity.getFileName()) | ||||||
|  |         .append("\"") | ||||||
|  |         .append(LINE_END); | ||||||
|  |     writer.append("Content-Type: ") | ||||||
|  |         .append(URLConnection.guessContentTypeFromName(mTaskEntity.uploadEntity.getFileName())) | ||||||
|  |         .append(LINE_END); | ||||||
|  |     writer.append("Content-Transfer-Encoding: binary").append(LINE_END); | ||||||
|  |     writer.append(LINE_END); | ||||||
|  |     writer.flush(); | ||||||
|  | 
 | ||||||
|  |     FileInputStream inputStream = new FileInputStream(uploadFile); | ||||||
|  |     byte[] buffer = new byte[4096]; | ||||||
|  |     int bytesRead; | ||||||
|  |     while ((bytesRead = inputStream.read(buffer)) != -1) { | ||||||
|  |       mCurrentLocation += bytesRead; | ||||||
|  |       mOutputStream.write(buffer, 0, bytesRead); | ||||||
|  |       if (isCancel) { | ||||||
|  |         break; | ||||||
|  |       } | ||||||
|  |       isRunning = true; | ||||||
|  |       mListener.onProgress(mCurrentLocation); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     mOutputStream.flush(); | ||||||
|  |     //outputStream.close(); //不能调用,否则服务器端异常
 | ||||||
|  |     inputStream.close(); | ||||||
|  |     writer.append(LINE_END); | ||||||
|  |     writer.flush(); | ||||||
|  |     isRunning = false; | ||||||
|  |     if (isCancel) { | ||||||
|  |       mListener.onCancel(); | ||||||
|  |       return; | ||||||
|  |     } | ||||||
|  |     mListener.onComplete(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 任务结束操作 | ||||||
|  |    * | ||||||
|  |    * @throws IOException | ||||||
|  |    */ | ||||||
|  |   private String finish(PrintWriter writer) throws IOException { | ||||||
|  |     StringBuilder response = new StringBuilder(); | ||||||
|  | 
 | ||||||
|  |     writer.append(LINE_END).flush(); | ||||||
|  |     writer.append(PREFIX).append(BOUNDARY).append(PREFIX).append(LINE_END); | ||||||
|  |     writer.close(); | ||||||
|  | 
 | ||||||
|  |     int status = mHttpConn.getResponseCode(); | ||||||
|  |     if (status == HttpURLConnection.HTTP_OK) { | ||||||
|  |       BufferedReader reader = new BufferedReader(new InputStreamReader(mHttpConn.getInputStream())); | ||||||
|  |       String line; | ||||||
|  |       while ((line = reader.readLine()) != null) { | ||||||
|  |         response.append(line); | ||||||
|  |       } | ||||||
|  |       reader.close(); | ||||||
|  |       mHttpConn.disconnect(); | ||||||
|  |     } else { | ||||||
|  |       Log.w(TAG, "state_code = " + status); | ||||||
|  |       mListener.onFail(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     writer.flush(); | ||||||
|  |     writer.close(); | ||||||
|  |     mOutputStream.close(); | ||||||
|  |     return response.toString(); | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -0,0 +1,119 @@ | |||||||
|  | /* | ||||||
|  |  * 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.util; | ||||||
|  | 
 | ||||||
|  | import android.content.Context; | ||||||
|  | import android.content.pm.ApplicationInfo; | ||||||
|  | import android.content.pm.PackageInfo; | ||||||
|  | import android.content.pm.PackageManager; | ||||||
|  | import android.graphics.drawable.Drawable; | ||||||
|  | import android.graphics.drawable.Icon; | ||||||
|  | import com.arialyy.aria.window.FileEntity; | ||||||
|  | import java.io.File; | ||||||
|  | import java.util.ArrayList; | ||||||
|  | import java.util.List; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by Aria.Lao on 2017/3/21. | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | public class FileUtil { | ||||||
|  | 
 | ||||||
|  |   Context mContext; | ||||||
|  | 
 | ||||||
|  |   public FileUtil(Context context) { | ||||||
|  |     mContext = context; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 文件列表 | ||||||
|  |    */ | ||||||
|  |   public List<FileEntity> loadFiles(String path) { | ||||||
|  |     File file = new File(path); | ||||||
|  |     File[] files = file.listFiles(); | ||||||
|  |     List<FileEntity> list = new ArrayList<>(); | ||||||
|  |     for (File f : files) { | ||||||
|  |       FileEntity entity = new FileEntity(); | ||||||
|  |       entity.fileName = f.getName(); | ||||||
|  |       //entity.fileInfo = getFileType(f.getPath());
 | ||||||
|  |       //entity.fileDrawable = getApkIcon(mContext, f.getPath());
 | ||||||
|  |       list.add(entity); | ||||||
|  |     } | ||||||
|  |     return list; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 获取文件类型 | ||||||
|  |    */ | ||||||
|  |   public FileType getFileType(String path) { | ||||||
|  |     String exName = getExName(path); | ||||||
|  |     String type = ""; | ||||||
|  |     FileType fType = null; | ||||||
|  |     if (exName.equalsIgnoreCase("apk")) { | ||||||
|  |       fType = new FileType("应用", getApkIcon(path)); | ||||||
|  |     } else if (exName.equalsIgnoreCase("img") | ||||||
|  |         || exName.equalsIgnoreCase("png") | ||||||
|  |         || exName.equalsIgnoreCase("jpg") | ||||||
|  |         || exName.equalsIgnoreCase("jepg")) { | ||||||
|  |       //fType = new FileType("图片", )
 | ||||||
|  |     } else if (exName.equalsIgnoreCase("mp3") || exName.equalsIgnoreCase("wm")) { | ||||||
|  |       //fType = new FileType("音乐", );
 | ||||||
|  |     } else if (exName.equalsIgnoreCase("mp4") | ||||||
|  |         || exName.equalsIgnoreCase("rm") | ||||||
|  |         || exName.equalsIgnoreCase("rmvb")) { | ||||||
|  |       //fType = new FileType("视频", );
 | ||||||
|  |     } | ||||||
|  |     return fType; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 获取扩展名 | ||||||
|  |    */ | ||||||
|  |   public String getExName(String path) { | ||||||
|  |     int separatorIndex = path.lastIndexOf("."); | ||||||
|  |     return (separatorIndex < 0) ? path : path.substring(separatorIndex + 1, path.length()); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 获取apk文件的icon | ||||||
|  |    * | ||||||
|  |    * @param path apk文件路径 | ||||||
|  |    */ | ||||||
|  |   public Drawable getApkIcon(String path) { | ||||||
|  |     PackageManager pm = mContext.getPackageManager(); | ||||||
|  |     PackageInfo info = pm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES); | ||||||
|  |     if (info != null) { | ||||||
|  |       ApplicationInfo appInfo = info.applicationInfo; | ||||||
|  |       //android有bug,需要下面这两句话来修复才能获取apk图片
 | ||||||
|  |       appInfo.sourceDir = path; | ||||||
|  |       appInfo.publicSourceDir = path; | ||||||
|  |       //			    String packageName = appInfo.packageName;  //得到安装包名称
 | ||||||
|  |       //	            String version=info.versionName;       //得到版本信息
 | ||||||
|  |       return pm.getApplicationIcon(appInfo); | ||||||
|  |     } | ||||||
|  |     return null; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   class FileType { | ||||||
|  |     String name; | ||||||
|  |     Drawable icon; | ||||||
|  | 
 | ||||||
|  |     public FileType(String name, Drawable icon) { | ||||||
|  |       this.name = name; | ||||||
|  |       this.icon = icon; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -0,0 +1,44 @@ | |||||||
|  | /* | ||||||
|  |  * 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.util; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by Aria.Lao on 2017/3/6. | ||||||
|  |  */ | ||||||
|  | public enum Speed { | ||||||
|  |   /** | ||||||
|  |    * 最大速度为256kb | ||||||
|  |    */ | ||||||
|  |   KB_256(64), /** | ||||||
|  |    * 最大速度为512kb | ||||||
|  |    */ | ||||||
|  |   KB_512(128), /** | ||||||
|  |    * 最大速度为1mb | ||||||
|  |    */ | ||||||
|  |   MB_1(256), /** | ||||||
|  |    * 最大速度为2mb | ||||||
|  |    */ | ||||||
|  |   MB_2(1024), /** | ||||||
|  |    * 最大速度为10mb | ||||||
|  |    */ | ||||||
|  |   MAX(8192); | ||||||
|  |   int buf; | ||||||
|  | 
 | ||||||
|  |   Speed(int buf) { | ||||||
|  |     this.buf = buf; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,67 @@ | |||||||
|  | /* | ||||||
|  |  * 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.window; | ||||||
|  | 
 | ||||||
|  | import android.os.Bundle; | ||||||
|  | import android.os.Environment; | ||||||
|  | import android.support.annotation.Nullable; | ||||||
|  | import android.support.v4.app.FragmentActivity; | ||||||
|  | import android.widget.AbsListView; | ||||||
|  | import android.widget.ListView; | ||||||
|  | import com.arialyy.aria.R; | ||||||
|  | import com.arialyy.aria.util.FileUtil; | ||||||
|  | import java.io.File; | ||||||
|  | import java.util.ArrayList; | ||||||
|  | import java.util.HashMap; | ||||||
|  | import java.util.List; | ||||||
|  | import java.util.Map; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by Aria.Lao on 2017/3/21. | ||||||
|  |  * 文件选择 | ||||||
|  |  */ | ||||||
|  | public class AriaFileChangeActivity extends FragmentActivity { | ||||||
|  |   final String ROOT_PAT = Environment.getExternalStorageDirectory().getPath(); | ||||||
|  |   ListView mList; | ||||||
|  |   FileChangeAdapter mAdapter; | ||||||
|  |   Map<String, List<FileEntity>> mData = new HashMap<>(); | ||||||
|  |   private String mCurrentPath = ROOT_PAT; | ||||||
|  | 
 | ||||||
|  |   @Override protected void onCreate(@Nullable Bundle savedInstanceState) { | ||||||
|  |     super.onCreate(savedInstanceState); | ||||||
|  |     setContentView(R.layout.activity_aria_file_shange); | ||||||
|  |     mList = (ListView) findViewById(R.id.list); | ||||||
|  |     mList.setOnScrollListener(new AbsListView.OnScrollListener() { | ||||||
|  |       int state; | ||||||
|  | 
 | ||||||
|  |       @Override public void onScrollStateChanged(AbsListView view, int scrollState) { | ||||||
|  |         state = scrollState; | ||||||
|  |       } | ||||||
|  | 
 | ||||||
|  |       @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, | ||||||
|  |           int totalItemCount) { | ||||||
|  |         if (state == AbsListView.OnScrollListener.SCROLL_STATE_IDLE | ||||||
|  |             && firstVisibleItem + visibleItemCount == totalItemCount) { | ||||||
|  |           loadMore(); | ||||||
|  |         } | ||||||
|  |       } | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   private void loadMore() { | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -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.window; | ||||||
|  | 
 | ||||||
|  | import android.content.Context; | ||||||
|  | import android.util.SparseBooleanArray; | ||||||
|  | import android.view.LayoutInflater; | ||||||
|  | import android.view.View; | ||||||
|  | import android.view.ViewGroup; | ||||||
|  | import android.widget.BaseAdapter; | ||||||
|  | import android.widget.CheckBox; | ||||||
|  | import android.widget.ImageView; | ||||||
|  | import android.widget.TextView; | ||||||
|  | import com.arialyy.aria.R; | ||||||
|  | import java.util.ArrayList; | ||||||
|  | import java.util.List; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by Aria.Lao on 2017/3/21. | ||||||
|  |  */ | ||||||
|  | final class FileChangeAdapter extends BaseAdapter { | ||||||
|  | 
 | ||||||
|  |   List<FileEntity> mData = new ArrayList<>(); | ||||||
|  |   SparseBooleanArray mCheck = new SparseBooleanArray(); | ||||||
|  |   Context mContext; | ||||||
|  | 
 | ||||||
|  |   public FileChangeAdapter(Context context, List<FileEntity> list) { | ||||||
|  |     mContext = context; | ||||||
|  |     mData.addAll(list); | ||||||
|  |     for (int i = 0, len = mData.size(); i < len; i++) { | ||||||
|  |       mCheck.append(i, false); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public int getCount() { | ||||||
|  |     return mData.size(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public Object getItem(int position) { | ||||||
|  |     return null; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public long getItemId(int position) { | ||||||
|  |     return 0; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public View getView(int position, View convertView, ViewGroup parent) { | ||||||
|  |     FileChangeHolder holder = null; | ||||||
|  |     if (convertView == null) { | ||||||
|  |       convertView = LayoutInflater.from(mContext).inflate(R.layout.item_file, null); | ||||||
|  |       holder = new FileChangeHolder(convertView); | ||||||
|  |       convertView.setTag(holder); | ||||||
|  |     } else { | ||||||
|  |       holder = (FileChangeHolder) convertView.getTag(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     holder.checkBox.setChecked(mCheck.get(position, false)); | ||||||
|  |     return convertView; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public void setCheck(int position, boolean check) { | ||||||
|  |     if (position >= mData.size()) return; | ||||||
|  |     mCheck.put(position, check); | ||||||
|  |     notifyDataSetChanged(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   private static class FileChangeHolder { | ||||||
|  |     TextView title, info; | ||||||
|  |     ImageView icon; | ||||||
|  |     CheckBox checkBox; | ||||||
|  | 
 | ||||||
|  |     FileChangeHolder(View view) { | ||||||
|  |       title = (TextView) view.findViewById(R.id.title); | ||||||
|  |       info = (TextView) view.findViewById(R.id.info); | ||||||
|  |       icon = (ImageView) view.findViewById(R.id.icon); | ||||||
|  |       checkBox = (CheckBox) view.findViewById(R.id.checkbox); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -0,0 +1,29 @@ | |||||||
|  | /* | ||||||
|  |  * 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.window; | ||||||
|  | 
 | ||||||
|  | import android.graphics.drawable.Drawable; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by Aria.Lao on 2017/3/21. | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | public class FileEntity { | ||||||
|  |   public String fileName; | ||||||
|  |   public String fileInfo; | ||||||
|  |   public int fileIcon; | ||||||
|  |   public Drawable fileDrawable; | ||||||
|  | } | ||||||
| @ -0,0 +1,40 @@ | |||||||
|  | ## 关于Aria,你还需要知道的一些东西 | ||||||
|  | - 设置下载任务数,Aria默认下载任务为**2** | ||||||
|  | 
 | ||||||
|  |   ```java | ||||||
|  |   Aria.get(getContext()).setMaxDownloadNum(num); | ||||||
|  |   ``` | ||||||
|  | - 停止所有下载 | ||||||
|  | 
 | ||||||
|  |   ```java | ||||||
|  |   Aria.get(this).stopAllTask(); | ||||||
|  |   ``` | ||||||
|  | - 设置失败重试次数,从事次数不能少于 1 | ||||||
|  | 
 | ||||||
|  |   ```java | ||||||
|  |   Aria.get(this).setReTryNum(10); | ||||||
|  |   ``` | ||||||
|  | - 设置失败重试间隔,重试间隔不能小于 5000ms | ||||||
|  | 
 | ||||||
|  |   ```java | ||||||
|  |   Aria.get(this).setReTryInterval(5000); | ||||||
|  |   ``` | ||||||
|  | - 设置是否打开广播,如果你需要在Service后台获取下载完成情况,那么你需要打开Aria广播,[Aria广播配置](https://github.com/AriaLyy/Aria/blob/v_2.0/BroadCast.md) | ||||||
|  | 
 | ||||||
|  |   ```java | ||||||
|  |   Aria.get(this).openBroadcast(true); | ||||||
|  |   ``` | ||||||
|  | 
 | ||||||
|  | ## https证书配置 | ||||||
|  |   + 将你的证书导入`assets`目录 | ||||||
|  |   + 调用以下代码配置ca证书相关信息 | ||||||
|  | 
 | ||||||
|  |   ```java | ||||||
|  |   /** | ||||||
|  |    * 设置CA证书信息 | ||||||
|  |    * | ||||||
|  |    * @param caAlias ca证书别名 | ||||||
|  |    * @param caPath assets 文件夹下的ca证书完整路径 | ||||||
|  |    */ | ||||||
|  |   Aria.get(this).setCAInfo("caAlias","caPath"); | ||||||
|  |   ``` | ||||||
| @ -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.simple.download; | ||||||
|  | 
 | ||||||
|  | import android.Manifest; | ||||||
|  | import android.content.Intent; | ||||||
|  | import android.os.Build; | ||||||
|  | import android.os.Bundle; | ||||||
|  | import android.support.v7.widget.Toolbar; | ||||||
|  | import android.view.Gravity; | ||||||
|  | import android.view.View; | ||||||
|  | import android.widget.Button; | ||||||
|  | import butterknife.Bind; | ||||||
|  | import com.arialyy.frame.permission.OnPermissionCallback; | ||||||
|  | import com.arialyy.frame.permission.PermissionManager; | ||||||
|  | import com.arialyy.frame.util.show.T; | ||||||
|  | import com.arialyy.simple.R; | ||||||
|  | import com.arialyy.simple.base.BaseActivity; | ||||||
|  | import com.arialyy.simple.databinding.ActivityDownloadMeanBinding; | ||||||
|  | import com.arialyy.simple.download.fragment_download.FragmentActivity; | ||||||
|  | import com.arialyy.simple.download.multi_download.MultiTaskActivity; | ||||||
|  | import com.arialyy.simple.download.service_download.DownloadService; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by Lyy on 2016/10/13. | ||||||
|  |  */ | ||||||
|  | public class DownloadActivity extends BaseActivity<ActivityDownloadMeanBinding> { | ||||||
|  |   @Bind(R.id.toolbar) Toolbar mBar; | ||||||
|  |   @Bind(R.id.single_task) Button mSigleBt; | ||||||
|  |   @Bind(R.id.multi_task) Button mMultiBt; | ||||||
|  |   @Bind(R.id.dialog_task) Button mDialogBt; | ||||||
|  |   @Bind(R.id.pop_task) Button mPopBt; | ||||||
|  | 
 | ||||||
|  |   @Override protected int setLayoutId() { | ||||||
|  |     return R.layout.activity_download_mean; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override protected void init(Bundle savedInstanceState) { | ||||||
|  |     super.init(savedInstanceState); | ||||||
|  |     setSupportActionBar(mBar); | ||||||
|  |     mBar.setTitle("多线程多任务下载"); | ||||||
|  |     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { | ||||||
|  |       setEnable(true); | ||||||
|  |     } else {  //6.0处理
 | ||||||
|  |       boolean hasPermission = PermissionManager.getInstance() | ||||||
|  |           .checkPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); | ||||||
|  |       if (hasPermission) { | ||||||
|  |         setEnable(true); | ||||||
|  |       } else { | ||||||
|  |         setEnable(false); | ||||||
|  |         PermissionManager.getInstance().requestPermission(this, new OnPermissionCallback() { | ||||||
|  |           @Override public void onSuccess(String... permissions) { | ||||||
|  |             setEnable(true); | ||||||
|  |           } | ||||||
|  | 
 | ||||||
|  |           @Override public void onFail(String... permissions) { | ||||||
|  |             T.showShort(DownloadActivity.this, "没有文件读写权限"); | ||||||
|  |             setEnable(false); | ||||||
|  |           } | ||||||
|  |         }, Manifest.permission.WRITE_EXTERNAL_STORAGE); | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   private void setEnable(boolean enable) { | ||||||
|  |     mSigleBt.setEnabled(enable); | ||||||
|  |     mMultiBt.setEnabled(enable); | ||||||
|  |     mDialogBt.setEnabled(enable); | ||||||
|  |     mPopBt.setEnabled(enable); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public void onClick(View view) { | ||||||
|  |     switch (view.getId()) { | ||||||
|  |       case R.id.service: | ||||||
|  |         startService(new Intent(this, DownloadService.class)); | ||||||
|  |         break; | ||||||
|  |       case R.id.single_task: | ||||||
|  |         startActivity(new Intent(this, SingleTaskActivity.class)); | ||||||
|  |         break; | ||||||
|  |       case R.id.multi_task: | ||||||
|  |         startActivity(new Intent(this, MultiTaskActivity.class)); | ||||||
|  |         break; | ||||||
|  |       case R.id.dialog_task: | ||||||
|  |         DownloadDialog dialog = new DownloadDialog(this); | ||||||
|  |         dialog.show(); | ||||||
|  |         break; | ||||||
|  |       case R.id.pop_task: | ||||||
|  |         DownloadPopupWindow pop = new DownloadPopupWindow(this); | ||||||
|  |         //pop.showAsDropDown(mRootView);
 | ||||||
|  |         pop.showAtLocation(mRootView, Gravity.CENTER_VERTICAL, 0, 0); | ||||||
|  |         break; | ||||||
|  |       case R.id.fragment_task: | ||||||
|  |         startActivity(new Intent(this, FragmentActivity.class)); | ||||||
|  |         break; | ||||||
|  |       case R.id.notification: | ||||||
|  |         SimpleNotification notification = new SimpleNotification(this); | ||||||
|  |         notification.start(); | ||||||
|  |         break; | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -0,0 +1,31 @@ | |||||||
|  | /* | ||||||
|  |  * 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.simple.download.fragment_download; | ||||||
|  | 
 | ||||||
|  | import com.arialyy.simple.R; | ||||||
|  | import com.arialyy.simple.base.BaseActivity; | ||||||
|  | import com.arialyy.simple.databinding.FragmentDownloadBinding; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by Aria.Lao on 2017/1/4. | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | public class FragmentActivity extends BaseActivity<FragmentDownloadBinding> { | ||||||
|  |   @Override protected int setLayoutId() { | ||||||
|  |     return R.layout.activity_fragment; | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -0,0 +1,25 @@ | |||||||
|  | /* | ||||||
|  |  * 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.simple.download.multi_download; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by AriaL on 2017/1/6. | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | public class FileListEntity { | ||||||
|  |   public String name, downloadUrl, downloadPath; | ||||||
|  | } | ||||||
| @ -0,0 +1,105 @@ | |||||||
|  | /* | ||||||
|  |  * 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.simple.download.multi_download; | ||||||
|  | 
 | ||||||
|  | import android.os.Bundle; | ||||||
|  | import android.support.v7.widget.LinearLayoutManager; | ||||||
|  | import android.support.v7.widget.RecyclerView; | ||||||
|  | import android.support.v7.widget.Toolbar; | ||||||
|  | import butterknife.Bind; | ||||||
|  | import com.arialyy.aria.core.Aria; | ||||||
|  | import com.arialyy.aria.core.download.DownloadTask; | ||||||
|  | import com.arialyy.frame.util.show.L; | ||||||
|  | import com.arialyy.simple.R; | ||||||
|  | import com.arialyy.simple.base.BaseActivity; | ||||||
|  | import com.arialyy.simple.databinding.ActivityMultiDownloadBinding; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by AriaL on 2017/1/6. | ||||||
|  |  */ | ||||||
|  | 
 | ||||||
|  | public class MultiDownloadActivity extends BaseActivity<ActivityMultiDownloadBinding> { | ||||||
|  |   @Bind(R.id.list) RecyclerView mList; | ||||||
|  |   @Bind(R.id.toolbar) Toolbar mBar; | ||||||
|  |   private DownloadAdapter mAdapter; | ||||||
|  | 
 | ||||||
|  |   @Override protected int setLayoutId() { | ||||||
|  |     return R.layout.activity_multi_download; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override protected void init(Bundle savedInstanceState) { | ||||||
|  |     super.init(savedInstanceState); | ||||||
|  |     mAdapter = new DownloadAdapter(this, Aria.download(this).getTaskList()); | ||||||
|  |     mList.setLayoutManager(new LinearLayoutManager(this)); | ||||||
|  |     mList.setAdapter(mAdapter); | ||||||
|  |     mBar.setTitle("多任务下载"); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override protected void dataCallback(int result, Object data) { | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override protected void onResume() { | ||||||
|  |     super.onResume(); | ||||||
|  |     Aria.download(this).addSchedulerListener(new MySchedulerListener()); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   private class MySchedulerListener extends Aria.DownloadSchedulerListener { | ||||||
|  |     @Override public void onTaskPre(DownloadTask task) { | ||||||
|  |       super.onTaskPre(task); | ||||||
|  |       L.d(TAG, "download pre"); | ||||||
|  |       mAdapter.updateState(task.getDownloadEntity()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public void onTaskStart(DownloadTask task) { | ||||||
|  |       super.onTaskStart(task); | ||||||
|  |       L.d(TAG, "download start"); | ||||||
|  |       mAdapter.updateState(task.getDownloadEntity()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public void onTaskResume(DownloadTask task) { | ||||||
|  |       super.onTaskResume(task); | ||||||
|  |       L.d(TAG, "download resume"); | ||||||
|  |       mAdapter.updateState(task.getDownloadEntity()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public void onTaskRunning(DownloadTask task) { | ||||||
|  |       super.onTaskRunning(task); | ||||||
|  |       mAdapter.setProgress(task.getDownloadEntity()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public void onTaskStop(DownloadTask task) { | ||||||
|  |       super.onTaskStop(task); | ||||||
|  |       mAdapter.updateState(task.getDownloadEntity()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public void onTaskCancel(DownloadTask task) { | ||||||
|  |       super.onTaskCancel(task); | ||||||
|  |       mAdapter.updateState(task.getDownloadEntity()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public void onTaskComplete(DownloadTask task) { | ||||||
|  |       super.onTaskComplete(task); | ||||||
|  |       mAdapter.updateState(task.getDownloadEntity()); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public void onTaskFail(DownloadTask task) { | ||||||
|  |       super.onTaskFail(task); | ||||||
|  |       L.d(TAG, "download fail"); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -0,0 +1,55 @@ | |||||||
|  | /* | ||||||
|  |  * 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.simple.download.service_download; | ||||||
|  | 
 | ||||||
|  | import android.app.NotificationManager; | ||||||
|  | import android.content.Context; | ||||||
|  | import android.support.v4.app.NotificationCompat; | ||||||
|  | import com.arialyy.simple.R; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by Aria.Lao on 2017/1/18. | ||||||
|  |  */ | ||||||
|  | public class DownloadNotification { | ||||||
|  | 
 | ||||||
|  |   private NotificationManager mManager; | ||||||
|  |   private Context mContext; | ||||||
|  |   private NotificationCompat.Builder mBuilder; | ||||||
|  |   private static final int mNotifiyId = 0; | ||||||
|  | 
 | ||||||
|  |   public DownloadNotification(Context context) { | ||||||
|  |     mContext = context; | ||||||
|  |     init(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   private void init() { | ||||||
|  |     mManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); | ||||||
|  |     mBuilder = new NotificationCompat.Builder(mContext); | ||||||
|  |     mBuilder.setContentTitle("Aria Download Test") | ||||||
|  |         .setContentText("进度条") | ||||||
|  |         .setProgress(100, 0, false) | ||||||
|  |         .setSmallIcon(R.mipmap.ic_launcher); | ||||||
|  |     mManager.notify(mNotifiyId, mBuilder.build()); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public void upload(int progress){ | ||||||
|  |     if (mBuilder != null) { | ||||||
|  |       mBuilder.setProgress(100, progress, false); | ||||||
|  |       mManager.notify(mNotifiyId, mBuilder.build()); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
| @ -0,0 +1,102 @@ | |||||||
|  | /* | ||||||
|  |  * 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.simple.download.service_download; | ||||||
|  | 
 | ||||||
|  | import android.app.Service; | ||||||
|  | import android.content.Intent; | ||||||
|  | import android.os.Environment; | ||||||
|  | import android.os.IBinder; | ||||||
|  | import android.support.annotation.Nullable; | ||||||
|  | import com.arialyy.aria.core.Aria; | ||||||
|  | import com.arialyy.aria.core.download.DownloadTask; | ||||||
|  | import com.arialyy.frame.util.show.T; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * Created by Aria.Lao on 2017/4/5. | ||||||
|  |  * 在服务中使用 Aria进行下载 | ||||||
|  |  */ | ||||||
|  | public class DownloadService extends Service { | ||||||
|  | 
 | ||||||
|  |   private static final String DOWNLOAD_URL = | ||||||
|  |       //"http://kotlinlang.org/docs/kotlin-docs.pdf";
 | ||||||
|  |       //"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe";
 | ||||||
|  |       //"http://static.gaoshouyou.com/d/21/e8/61218d78d0e8b79df68dbc18dd484c97.apk";
 | ||||||
|  |       //不支持断点的链接
 | ||||||
|  |       "http://ox.konsung.net:5555/ksdc-web/download/downloadFile/?fileName=ksdc_1.0.2.apk&rRange=0-"; | ||||||
|  |   private DownloadNotification mNotify; | ||||||
|  | 
 | ||||||
|  |   @Nullable @Override public IBinder onBind(Intent intent) { | ||||||
|  |     return null; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public int onStartCommand(Intent intent, int flags, int startId) { | ||||||
|  |     return super.onStartCommand(intent, flags, startId); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public void onCreate() { | ||||||
|  |     super.onCreate(); | ||||||
|  |     mNotify = new DownloadNotification(getApplicationContext()); | ||||||
|  |     Aria.download(this).addSchedulerListener(new MySchedulerListener()); | ||||||
|  |     Aria.download(this) | ||||||
|  |         .load(DOWNLOAD_URL) | ||||||
|  |         .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/service_task.apk") | ||||||
|  |         .start(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @Override public void onDestroy() { | ||||||
|  |     super.onDestroy(); | ||||||
|  |     Aria.download(this).removeSchedulerListener(); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   private class MySchedulerListener extends Aria.DownloadSchedulerListener { | ||||||
|  | 
 | ||||||
|  |     @Override public void onNoSupportBreakPoint(DownloadTask task) { | ||||||
|  |       super.onNoSupportBreakPoint(task); | ||||||
|  |       T.showShort(getApplicationContext(), "该下载链接不支持断点"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public void onTaskStart(DownloadTask task) { | ||||||
|  |       T.showShort(getApplicationContext(), task.getDownloadEntity().getFileName() + ",开始下载"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public void onTaskResume(DownloadTask task) { | ||||||
|  |       super.onTaskResume(task); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public void onTaskStop(DownloadTask task) { | ||||||
|  |       T.showShort(getApplicationContext(), task.getDownloadEntity().getFileName() + ",停止下载"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public void onTaskCancel(DownloadTask task) { | ||||||
|  |       T.showShort(getApplicationContext(), task.getDownloadEntity().getFileName() + ",取消下载"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public void onTaskFail(DownloadTask task) { | ||||||
|  |       T.showShort(getApplicationContext(), task.getDownloadEntity().getFileName() + ",下载失败"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public void onTaskComplete(DownloadTask task) { | ||||||
|  |       T.showShort(getApplicationContext(), task.getDownloadEntity().getFileName() + ",下载完成"); | ||||||
|  |       mNotify.upload(100); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override public void onTaskRunning(DownloadTask task) { | ||||||
|  |       long len = task.getFileSize(); | ||||||
|  |       int p = (int) (task.getCurrentProgress() * 100 / len); | ||||||
|  |       mNotify.upload(p); | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
Some files were not shown because too many files have changed in this diff Show More
					Loading…
					
					
				
		Reference in new issue