From d40f8fa385b56d1a82e6b7f9abcf2a8cffb8aa39 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Thu, 23 May 2019 19:52:12 +0800 Subject: [PATCH] fix bug https://github.com/AriaLyy/Aria/issues/403 --- .../com/arialyy/aria/core/AriaManager.java | 10 + .../core/command/normal/AbsNormalCmd.java | 24 +- .../core/command/normal/NormalCmdFactory.java | 7 + .../aria/core/command/normal/ReStartCmd.java | 41 +++ .../arialyy/aria/core/common/AbsFileer.java | 37 ++- .../aria/core/common/AbsThreadTask.java | 6 + .../aria/core/common/BaseListener.java | 9 +- .../core/common/ftp/AbsFtpInfoThread.java | 41 ++- .../core/common/ftp/FtpInterceptHandler.java | 100 ++++++++ .../aria/core/common/ftp/FtpTaskDelegate.java | 137 +++++----- .../common/ftp/IFtpUploadInterceptor.java | 54 ++++ .../aria/core/download/BaseDListener.java | 4 +- .../core/download/DownloadGroupListener.java | 4 +- .../aria/core/download/DownloadGroupTask.java | 19 +- .../aria/core/download/DownloadTask.java | 6 +- .../downloader/FtpFileInfoThread.java | 2 +- .../core/download/group/FtpDirInfoThread.java | 2 +- .../arialyy/aria/core/inf/AbsGroupTask.java | 8 +- .../com/arialyy/aria/core/inf/AbsTarget.java | 17 +- .../com/arialyy/aria/core/inf/AbsTask.java | 51 +++- .../java/com/arialyy/aria/core/inf/ITask.java | 23 +- .../aria/core/inf/TaskSchedulerType.java | 14 +- .../aria/core/manager/ThreadTaskManager.java | 124 +++++---- .../aria/core/scheduler/AbsSchedulers.java | 14 +- .../aria/core/upload/BaseUListener.java | 4 +- .../aria/core/upload/FtpUploadTarget.java | 10 + .../aria/core/upload/UNormalDelegate.java | 17 +- .../upload/uploader/FtpFileInfoThread.java | 72 +++++- .../upload/uploader/SimpleUploadUtil.java | 6 + .../aria/exception/FileNotFoundException.java | 32 +++ .../com/arialyy/aria/util/CommonUtil.java | 11 +- DEV_LOG.md | 2 + app/build.gradle | 5 +- app/src/main/AndroidManifest.xml | 1 + .../core/download/DownloadActivity.java | 1 + .../core/download/KotlinDownloadActivity.kt | 241 ++++++++++++------ .../core/download/SingleTaskActivity.java | 6 +- .../simple/core/upload/FtpUploadActivity.java | 239 +++++++++-------- .../arialyy/simple/modlue/CommonModule.java | 1 + app/src/main/res/drawable/ic_kotlin.xml | 4 + app/src/main/res/values/strings.xml | 1 + build.gradle | 2 +- 42 files changed, 1008 insertions(+), 401 deletions(-) create mode 100644 Aria/src/main/java/com/arialyy/aria/core/command/normal/ReStartCmd.java create mode 100644 Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpInterceptHandler.java create mode 100644 Aria/src/main/java/com/arialyy/aria/core/common/ftp/IFtpUploadInterceptor.java create mode 100644 Aria/src/main/java/com/arialyy/aria/exception/FileNotFoundException.java create mode 100644 app/src/main/res/drawable/ic_kotlin.xml diff --git a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java index 95419595..f1f4ffaf 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java @@ -23,6 +23,8 @@ import android.app.Dialog; import android.content.Context; import android.os.Build; import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.widget.PopupWindow; @@ -80,6 +82,7 @@ import org.xml.sax.SAXException; private UploadConfig mUConfig; private AppConfig mAConfig; private DGroupConfig mDGConfig; + private Handler mAriaHandler; private File[] files; private AriaManager(Context context) { @@ -141,6 +144,13 @@ import org.xml.sax.SAXException; } } + public synchronized Handler getAriaHandler() { + if (mAriaHandler == null){ + mAriaHandler = new Handler(Looper.getMainLooper()); + } + return mAriaHandler; + } + public Map getReceiver() { return mReceivers; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java index 88c55ab7..aa2e068e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java @@ -42,7 +42,6 @@ public abstract class AbsNormalCmd extends AbsCmd { */ boolean canExeCmd = true; - private AbsTask tempTask = null; int taskType; /** @@ -82,9 +81,7 @@ public abstract class AbsNormalCmd extends AbsCmd { * 发送等待状态 */ void sendWaitState() { - if (tempTask != null) { - sendWaitState(tempTask); - } + sendWaitState(getTask()); } /** @@ -108,16 +105,14 @@ public abstract class AbsNormalCmd extends AbsCmd { * 停止任务 */ void stopTask() { - if (tempTask == null) createTask(); - mQueue.stopTask(tempTask); + mQueue.stopTask(getTask()); } /** * 删除任务 */ void removeTask() { - if (tempTask == null) createTask(); - mQueue.cancelTask(tempTask); + mQueue.cancelTask(getTask()); } /** @@ -135,14 +130,14 @@ public abstract class AbsNormalCmd extends AbsCmd { * 启动任务 */ void startTask() { - mQueue.startTask(tempTask); + mQueue.startTask(getTask()); } /** * 恢复任务 */ void resumeTask() { - mQueue.resumeTask(tempTask); + mQueue.resumeTask(getTask()); } /** @@ -160,8 +155,7 @@ public abstract class AbsNormalCmd extends AbsCmd { * @return 执行任务 */ AbsTask getTask() { - tempTask = mQueue.getTask(mTaskWrapper.getEntity().getKey()); - return tempTask; + return mQueue.getTask(mTaskWrapper.getEntity().getKey()); } /** @@ -170,8 +164,7 @@ public abstract class AbsNormalCmd extends AbsCmd { * @return 执行任务 */ AbsTask getTask(AbsEntity entity) { - tempTask = mQueue.getTask(entity.getKey()); - return tempTask; + return mQueue.getTask(entity.getKey()); } /** @@ -180,8 +173,7 @@ public abstract class AbsNormalCmd extends AbsCmd { * @return 创建的任务 */ AbsTask createTask() { - tempTask = mQueue.createTask(mTaskWrapper); - return tempTask; + return mQueue.createTask(mTaskWrapper); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/NormalCmdFactory.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/NormalCmdFactory.java index 8cb11b25..aef2579a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/NormalCmdFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/NormalCmdFactory.java @@ -62,6 +62,11 @@ public class NormalCmdFactory extends AbsCmdFactory(entity, taskType); case TASK_CANCEL_ALL: return new CancelAllCmd<>(entity, taskType); + case TASK_RESTART: + return new ReStartCmd<>(entity, taskType); default: return null; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/ReStartCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/ReStartCmd.java new file mode 100644 index 00000000..3295e823 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/ReStartCmd.java @@ -0,0 +1,41 @@ +/* + * 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.normal; + +import com.arialyy.aria.core.inf.AbsTask; +import com.arialyy.aria.core.inf.AbsTaskWrapper; +import com.arialyy.aria.core.inf.TaskSchedulerType; + +/** + * 重新开始任务命令 + */ +public class ReStartCmd extends AbsNormalCmd { + + ReStartCmd(T entity, int taskType) { + super(entity, taskType); + } + + @Override public void executeCmd() { + AbsTask task = getTask(); + if (task == null) { + task = createTask(); + } + if (task != null) { + task.cancel(TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY); + task.start(TaskSchedulerType.TYPE_START_AND_RESET_STATE); + } + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java index 5960d330..6a59d127 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java @@ -258,23 +258,20 @@ public abstract class AbsFileer 0) { ALog.i(TAG, - String.format("路径【%s】下的文件列表 ===================================", setRemotePath())); + String.format("路径【%s】下的文件列表 ===================================", getRemotePath())); for (FTPFile file : files1) { ALog.d(TAG, file.toString()); } @@ -127,13 +124,22 @@ public abstract class AbsFtpInfoThread + * + * Aria.upload(this) + * .loadFtp(FILE_PATH) + * .setUploadUrl(URL) + * .setUploadInterceptor( + * new IFtpUploadInterceptor() { + * + * @Override + * public FtpInterceptHandler onIntercept(UploadEntity entity, List fileList) { + * FtpInterceptHandler.Builder builder = new FtpInterceptHandler.Builder(); + * builder.coverServerFile(); + * //builder.resetFileName("test"); + * return builder.build(); + * } + * }) + * .start(); + * + * + * + */ +public interface IFtpUploadInterceptor { + + /** + * 处理拦截事件 + * + * @param entity 上传信息实体 + * @param fileList ftp服务器端remotePath下的文件列表 + */ + FtpInterceptHandler onIntercept(UploadEntity entity, List fileList); +} diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java b/Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java index b022245b..519c4a4b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java @@ -20,6 +20,7 @@ import android.os.Handler; import com.arialyy.aria.core.common.BaseListener; import com.arialyy.aria.core.inf.IDownloadListener; import com.arialyy.aria.core.inf.IEntity; +import com.arialyy.aria.core.inf.TaskSchedulerType; import com.arialyy.aria.core.scheduler.ISchedulers; import com.arialyy.aria.util.CommonUtil; @@ -55,7 +56,8 @@ public class BaseDListener extends BaseListener { mOutHandler = outHandler; mContext = AriaManager.APP; mListener = new BaseDListener(this, mOutHandler); - mUtil = new SimpleDownloadUtil(taskWrapper, (IDownloadListener) mListener); mEntity = taskWrapper.getEntity(); } @@ -83,6 +83,10 @@ public class DownloadTask extends AbsNormalTask { return mEntity.getFileName(); } + @Override protected synchronized IUtil createUtil() { + return new SimpleDownloadUtil(mTaskWrapper, (IDownloadListener) mListener); + } + public static class Builder { DTaskWrapper taskEntity; Handler outHandler; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpFileInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpFileInfoThread.java index cbc8f18c..b0cc47bc 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpFileInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpFileInfoThread.java @@ -44,7 +44,7 @@ class FtpFileInfoThread extends AbsFtpInfoThread { } } - @Override protected String setRemotePath() { + @Override protected String getRemotePath() { return mTaskWrapper.asFtp().getUrlEntity().remotePath; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/group/FtpDirInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/download/group/FtpDirInfoThread.java index 69915616..0d8c217e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/group/FtpDirInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/group/FtpDirInfoThread.java @@ -38,7 +38,7 @@ class FtpDirInfoThread extends AbsFtpInfoThread implements ITargetHand if (mEntity instanceof AbsNormalEntity) { CommonUtil.delTaskRecord((AbsNormalEntity) mEntity, mTaskWrapper.isRemoveFile()); } else if (mEntity instanceof DownloadGroupEntity) { - CommonUtil.delGroupTaskRecord(((DownloadGroupEntity) mEntity), mTaskWrapper.isRemoveFile()); + CommonUtil.delGroupTaskRecord(((DownloadGroupEntity) mEntity), mTaskWrapper.isRemoveFile(), true); } TaskWrapperManager.getInstance().removeTaskWrapper(mEntity.getKey()); } @@ -370,8 +371,10 @@ public abstract class AbsTarget implements ITargetHand */ @Override public void reStart() { if (checkConfig()) { - cancel(); - start(); + ReStartCmd cmd = + (ReStartCmd) CommonUtil.createNormalCmd(mTaskWrapper, NormalCmdFactory.TASK_RESTART, + checkTaskType()); + AriaManager.getInstance(AriaManager.APP).setCmd(cmd).exe(); } } @@ -409,6 +412,10 @@ public abstract class AbsTarget implements ITargetHand } + @Override public void start(int type) { + + } + @Override public void stop() { } @@ -421,6 +428,10 @@ public abstract class AbsTarget implements ITargetHand } + @Override public void cancel(int type) { + + } + @Override public Object getExpand(String key) { return null; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java index 79c156dc..faabbd04 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java @@ -18,6 +18,8 @@ package com.arialyy.aria.core.inf; import android.content.Context; import android.os.Handler; import android.text.TextUtils; +import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.common.IUtil; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; @@ -40,7 +42,7 @@ public abstract class AbsTask { */ TASK_WRAPPER getTaskWrapper(); + /** + * 启动任务 + */ void start(); + /** + * 启动任务 + * + * @param type {@link TaskSchedulerType} + */ + void start(@TaskSchedulerType int type); + /** * 停止任务 */ @@ -89,15 +99,22 @@ public interface ITask { /** * 停止任务 * - * @param type {@code 0}默认操作,{@code 1}停止任务不自动执行下一任务 + * @param type {@link TaskSchedulerType} */ - void stop(int type); + void stop(@TaskSchedulerType int type); /** * 删除任务 */ void cancel(); + /** + * 停止任务 + * + * @param type {@link TaskSchedulerType} + */ + void cancel(@TaskSchedulerType int type); + /** * 读取扩展数据 */ @@ -134,6 +151,4 @@ public interface ITask { * {@link TaskSchedulerType} */ int getSchedulerType(); - - } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/TaskSchedulerType.java b/Aria/src/main/java/com/arialyy/aria/core/inf/TaskSchedulerType.java index b25496b7..e6f81599 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/TaskSchedulerType.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/TaskSchedulerType.java @@ -7,7 +7,9 @@ import java.lang.annotation.RetentionPolicy; @IntDef({ TaskSchedulerType.TYPE_DEFAULT, TaskSchedulerType.TYPE_STOP_NOT_NEXT, - TaskSchedulerType.TYPE_STOP_AND_WAIT + TaskSchedulerType.TYPE_STOP_AND_WAIT, + TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY, + TaskSchedulerType.TYPE_START_AND_RESET_STATE }) @Retention(RetentionPolicy.SOURCE) public @interface TaskSchedulerType { int TYPE_DEFAULT = 1; @@ -19,4 +21,14 @@ import java.lang.annotation.RetentionPolicy; * 停止任务并让当前任务处于等待状态 */ int TYPE_STOP_AND_WAIT = 3; + + /** + * 删除任务并且不通知回调 + */ + int TYPE_CANCEL_AND_NOT_NOTIFY = 4; + + /** + * 重置状态并启动任务 + */ + int TYPE_START_AND_RESET_STATE = 5; } \ No newline at end of file diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/ThreadTaskManager.java b/Aria/src/main/java/com/arialyy/aria/core/manager/ThreadTaskManager.java index 30b9cb9f..64d6614b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/ThreadTaskManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/ThreadTaskManager.java @@ -17,6 +17,7 @@ package com.arialyy.aria.core.manager; import com.arialyy.aria.core.common.AbsThreadTask; +import com.arialyy.aria.core.inf.AbsTask; import com.arialyy.aria.core.inf.AbsTaskWrapper; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; @@ -24,25 +25,26 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.ReentrantLock; /** * 线程任务管理器 */ public class ThreadTaskManager { private static volatile ThreadTaskManager INSTANCE = null; - private static final Object LOCK = new Object(); private final String TAG = "ThreadTaskManager"; private ExecutorService mExePool; - private Map> mThreadTasks = new HashMap<>(); + private Map> mThreadTasks = new ConcurrentHashMap<>(); + private static final ReentrantLock LOCK = new ReentrantLock(); - public static ThreadTaskManager getInstance() { + public static synchronized ThreadTaskManager getInstance() { if (INSTANCE == null) { - synchronized (LOCK) { - INSTANCE = new ThreadTaskManager(); - } + INSTANCE = new ThreadTaskManager(); } return INSTANCE; } @@ -57,21 +59,37 @@ public class ThreadTaskManager { * @param key 任务对应的key{@link AbsTaskWrapper#getKey()} * @param threadTask 线程任务{@link AbsThreadTask} */ - public synchronized void startThread(String key, AbsThreadTask threadTask) { - if (mExePool.isShutdown()) { - ALog.e(TAG, "线程池已经关闭"); - return; - } - key = getKey(key); - Set temp = mThreadTasks.get(key); - if (temp == null) { - temp = new HashSet<>(); - mThreadTasks.put(key, temp); + public void startThread(String key, AbsThreadTask threadTask) { + try { + LOCK.tryLock(2, TimeUnit.SECONDS); + if (mExePool.isShutdown()) { + ALog.e(TAG, "线程池已经关闭"); + return; + } + key = getKey(key); + Set temp = mThreadTasks.get(key); + if (temp == null) { + temp = new HashSet<>(); + mThreadTasks.put(key, temp); + } + FutureContainer container = new FutureContainer(); + container.threadTask = threadTask; + container.future = mExePool.submit(threadTask); + temp.add(container); + } catch (Exception e) { + e.printStackTrace(); + } finally { + LOCK.unlock(); } - temp.add(mExePool.submit(threadTask)); } - public synchronized boolean taskIsRunning(String key){ + /** + * 任务是否在执行 + * + * @param key 任务的key{@link AbsTask#getKey()} + * @return {@code true} 任务正在运行 + */ + public boolean taskIsRunning(String key) { return mThreadTasks.get(getKey(key)) != null; } @@ -80,29 +98,31 @@ public class ThreadTaskManager { * * @param key 任务对应的key{@link AbsTaskWrapper#getKey()} */ - public synchronized void removeTaskThread(String key) { - if (mExePool.isShutdown()) { - ALog.e(TAG, "线程池已经关闭"); - return; - } - key = getKey(key); - Set temp = mThreadTasks.get(key); - if (temp != null && temp.size() > 0) { - try { - for (Future future : temp) { - if (future.isDone() || future.isCancelled()) { + public void removeTaskThread(String key) { + try { + + LOCK.tryLock(2, TimeUnit.SECONDS); + if (mExePool.isShutdown()) { + ALog.e(TAG, "线程池已经关闭"); + return; + } + key = getKey(key); + Set temp = mThreadTasks.get(key); + if (temp != null && temp.size() > 0) { + for (FutureContainer container : temp) { + if (container.future.isDone() || container.future.isCancelled()) { continue; } - AbsThreadTask task = (AbsThreadTask) future.get(); - task.setInterrupted(true); - future.cancel(true); + container.threadTask.setInterrupted(true); } - } catch (Exception e) { - ALog.e(TAG, e); + temp.clear(); } - temp.clear(); + mThreadTasks.remove(key); + } catch (Exception e) { + e.printStackTrace(); + } finally { + LOCK.unlock(); } - mThreadTasks.remove(key); } /** @@ -110,21 +130,28 @@ public class ThreadTaskManager { * * @param task 线程任务 */ - public synchronized void retryThread(AbsThreadTask task) { - if (mExePool.isShutdown()) { - ALog.e(TAG, "线程池已经关闭"); - return; - } + public void retryThread(AbsThreadTask task) { try { - if (task == null || task.isInterrupted()) { - ALog.e(TAG, "线程为空或线程已经中断"); + LOCK.tryLock(2, TimeUnit.SECONDS); + if (mExePool.isShutdown()) { + ALog.e(TAG, "线程池已经关闭"); + return; + } + try { + if (task == null || task.isInterrupted()) { + ALog.e(TAG, "线程为空或线程已经中断"); + return; + } + } catch (Exception e) { + ALog.e(TAG, e); return; } + mExePool.submit(task); } catch (Exception e) { - ALog.e(TAG, e); - return; + e.printStackTrace(); + } finally { + LOCK.unlock(); } - mExePool.submit(task); } /** @@ -136,4 +163,9 @@ public class ThreadTaskManager { private String getKey(String key) { return CommonUtil.getStrMd5(key); } + + private class FutureContainer { + Future future; + AbsThreadTask threadTask; + } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java index 8c51e4c6..b51c14a7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java @@ -194,16 +194,26 @@ abstract class AbsSchedulers mTaskWrapper.setState(state); mEntity.setState(state); if (state == IEntity.STATE_CANCEL) { - CommonUtil.delTaskRecord(mEntity.getFilePath(), 2, mTaskWrapper.isRemoveFile(), true); + CommonUtil.delTaskRecord(mEntity.getFilePath(), 2, mTaskWrapper.isRemoveFile(), + getTask().getSchedulerType() != TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY); } else if (state == IEntity.STATE_STOP) { mEntity.setStopTime(System.currentTimeMillis()); } else if (state == IEntity.STATE_COMPLETE) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/FtpUploadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/upload/FtpUploadTarget.java index eaeb83c8..e9e5e81e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/FtpUploadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/FtpUploadTarget.java @@ -16,11 +16,13 @@ package com.arialyy.aria.core.upload; import android.support.annotation.CheckResult; +import android.support.annotation.NonNull; import com.arialyy.aria.core.FtpUrlEntity; import com.arialyy.aria.core.common.ftp.FTPSDelegate; import com.arialyy.aria.core.common.ftp.FtpDelegate; import com.arialyy.aria.core.inf.AbsTaskWrapper; import com.arialyy.aria.core.inf.IFtpTarget; +import com.arialyy.aria.core.common.ftp.IFtpUploadInterceptor; import com.arialyy.aria.util.CheckUtil; import java.net.Proxy; @@ -53,6 +55,14 @@ public class FtpUploadTarget extends AbsUploadTarget return this; } + /** + * FTP文件上传拦截器,如果远端已有同名文件,可使用该拦截器控制覆盖文件或修改该文件上传到服务器端端的文件名 + */ + @CheckResult + public FtpUploadTarget setUploadInterceptor(@NonNull IFtpUploadInterceptor uploadInterceptor) { + return mNormalDelegate.setUploadInterceptor(uploadInterceptor); + } + /** * 是否是FTPS协议 * 如果是FTPS协议,需要使用{@link FTPSDelegate#setStorePath(String)} 、{@link FTPSDelegate#setAlias(String)} diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UNormalDelegate.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UNormalDelegate.java index 8f7ecc58..ead46797 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UNormalDelegate.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UNormalDelegate.java @@ -17,6 +17,7 @@ package com.arialyy.aria.core.upload; import android.text.TextUtils; import com.arialyy.aria.core.inf.AbsEntity; +import com.arialyy.aria.core.common.ftp.IFtpUploadInterceptor; import com.arialyy.aria.core.inf.INormalTarget; import com.arialyy.aria.core.manager.TaskWrapperManager; import com.arialyy.aria.core.queue.UploadTaskQueue; @@ -61,6 +62,14 @@ class UNormalDelegate implements INormalTarget { return mTarget; } + TARGET setUploadInterceptor(IFtpUploadInterceptor uploadInterceptor) { + if (uploadInterceptor == null) { + throw new NullPointerException("ftp拦截器为空"); + } + mTarget.getTaskWrapper().asFtp().setUploadInterceptor(uploadInterceptor); + return mTarget; + } + @Override public AbsEntity getEntity() { return mEntity; } @@ -82,10 +91,10 @@ class UNormalDelegate implements INormalTarget { if (mTarget.getTaskWrapper().asFtp().getUrlEntity() != null && mTarget.getTaskWrapper() .asFtp() .getUrlEntity().isFtps) { - //if (TextUtils.isEmpty(mTaskWrapper.getUrlEntity().storePath)) { - // ALog.e(TAG, "证书路径为空"); - // return false; - //} + if (TextUtils.isEmpty(mTarget.getTaskWrapper().asFtp().getUrlEntity().storePath)) { + ALog.e(TAG, "证书路径为空"); + return false; + } if (TextUtils.isEmpty(mTarget.getTaskWrapper().asFtp().getUrlEntity().keyAlias)) { ALog.e(TAG, "证书别名为空"); return false; diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java index edd573a3..818a21cb 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/FtpFileInfoThread.java @@ -15,17 +15,25 @@ */ package com.arialyy.aria.core.upload.uploader; +import android.text.TextUtils; +import aria.apache.commons.net.ftp.FTPClient; import aria.apache.commons.net.ftp.FTPFile; import com.arialyy.aria.core.common.ftp.AbsFtpInfoThread; import com.arialyy.aria.core.common.CompleteInfo; import com.arialyy.aria.core.common.OnFileInfoCallback; import com.arialyy.aria.core.common.TaskRecord; import com.arialyy.aria.core.common.ThreadRecord; +import com.arialyy.aria.core.common.ftp.AbsFtpThreadTask; +import com.arialyy.aria.core.common.ftp.FtpInterceptHandler; +import com.arialyy.aria.core.common.ftp.IFtpUploadInterceptor; +import com.arialyy.aria.core.queue.UploadTaskQueue; import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.core.upload.UploadEntity; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.DbDataHelper; +import java.io.IOException; import java.util.ArrayList; +import java.util.List; /** * Created by Aria.Lao on 2017/9/26. @@ -35,13 +43,71 @@ class FtpFileInfoThread extends AbsFtpInfoThread { private static final String TAG = "FtpUploadFileInfoThread"; static final int CODE_COMPLETE = 0xab1; private boolean isComplete = false; + private String remotePath; + /** + * true 使用拦截器,false 不使用拦截器 + */ + private boolean useInterceptor = false; FtpFileInfoThread(UTaskWrapper taskEntity, OnFileInfoCallback callback) { super(taskEntity, callback); } - @Override protected String setRemotePath() { - return mTaskWrapper.asFtp().getUrlEntity().remotePath + "/" + mEntity.getFileName(); + @Override protected String getRemotePath() { + return remotePath == null ? + mTaskWrapper.asFtp().getUrlEntity().remotePath + "/" + mEntity.getFileName() : remotePath; + } + + @Override protected boolean onInterceptor(FTPClient client, FTPFile[] ftpFiles) { + try { + IFtpUploadInterceptor interceptor = mTaskWrapper.asFtp().getUploadInterceptor(); + if (interceptor != null) { + useInterceptor = true; + List files = new ArrayList<>(); + for (FTPFile ftpFile : ftpFiles) { + if (ftpFile.isDirectory()) { + continue; + } + files.add(ftpFile.getName()); + } + + FtpInterceptHandler interceptHandler = interceptor.onIntercept(mEntity, files); + + if (interceptHandler.isStopUpload()) { + // TODO: 2019-05-22 操作任务停止 + return true; + } + + /* + 处理远端有同名文件的情况 + */ + if (files.contains(mEntity.getFileName())) { + if (interceptHandler.isCoverServerFile()) { + ALog.i(TAG, String.format("远端已拥有同名文件,将覆盖该文件,文件名:%s", mEntity.getFileName())); + boolean b = client.deleteFile( + new String(getRemotePath().getBytes(charSet), AbsFtpThreadTask.SERVER_CHARSET)); + ALog.d(TAG, + String.format("删除文件%s,code: %s, msg: %s", b ? "成功" : "失败", client.getReplyCode(), + client.getReplyString())); + } else if (!TextUtils.isEmpty(interceptHandler.getNewFileName())) { + ALog.i(TAG, String.format("远端已拥有同名文件,将修改remotePath,文件名:%s,remotePath:%s", + mEntity.getFileName(), interceptHandler.getNewFileName())); + remotePath = mTaskWrapper.asFtp().getUrlEntity().remotePath + + "/" + + interceptHandler.getNewFileName(); + client.disconnect(); + run(); + } + } + } + } catch (IOException e) { + e.printStackTrace(); + return false; + } finally { + mTaskWrapper.asFtp().setUploadInterceptor(null); + } + + return true; } /** @@ -54,7 +120,7 @@ class FtpFileInfoThread extends AbsFtpInfoThread { */ @Override protected void handleFile(String remotePath, FTPFile ftpFile) { super.handleFile(remotePath, ftpFile); - if (ftpFile != null) { + if (ftpFile != null && !useInterceptor) { //远程文件已完成 if (ftpFile.getSize() == mEntity.getFileSize()) { isComplete = true; diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java index e7fc8162..8006667a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/uploader/SimpleUploadUtil.java @@ -37,6 +37,7 @@ public class SimpleUploadUtil implements IUtil, Runnable { private UTaskWrapper mTaskWrapper; private IUploadListener mListener; private Uploader mUploader; + private boolean isStop = false, isCancel = false; public SimpleUploadUtil(UTaskWrapper taskWrapper, IUploadListener listener) { mTaskWrapper = taskWrapper; @@ -90,14 +91,19 @@ public class SimpleUploadUtil implements IUtil, Runnable { } @Override public void cancel() { + isCancel = true; mUploader.cancel(); } @Override public void stop() { + isStop = true; mUploader.stop(); } @Override public void start() { + if (isStop || isCancel) { + return; + } new Thread(this).start(); } diff --git a/Aria/src/main/java/com/arialyy/aria/exception/FileNotFoundException.java b/Aria/src/main/java/com/arialyy/aria/exception/FileNotFoundException.java new file mode 100644 index 00000000..4d69707f --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/exception/FileNotFoundException.java @@ -0,0 +1,32 @@ +/* + * 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.exception; + +/** + * Created by lyy on 2017/1/18. + * Aria 文件异常 + */ +public class FileNotFoundException extends BaseException { + private static final String ARIA_FILE_EXCEPTION = "Aria File not found Exception:"; + + public FileNotFoundException(String tag, String detailMessage) { + super(tag, String.format("%s%s", ARIA_FILE_EXCEPTION, detailMessage)); + } + + public FileNotFoundException(String tag, String message, Exception e){ + super(tag, message, e); + } +} diff --git a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java index bc08a76a..fb7bca62 100644 --- a/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/util/CommonUtil.java @@ -600,7 +600,7 @@ public class CommonUtil { } DownloadGroupEntity groupEntity = DbDataHelper.getDGEntity(groupHash); - delGroupTaskRecord(groupEntity, removeFile); + delGroupTaskRecord(groupEntity, removeFile, true); } /** @@ -608,7 +608,8 @@ public class CommonUtil { * * @param removeFile {@code true} 不仅删除任务数据库记录,还会删除已经删除完成的文件 {@code false}如果任务已经完成,只删除任务数据库记录 */ - public static void delGroupTaskRecord(DownloadGroupEntity groupEntity, boolean removeFile) { + public static void delGroupTaskRecord(DownloadGroupEntity groupEntity, boolean removeFile, + boolean removeEntity) { if (groupEntity == null) { ALog.e(TAG, "删除下载任务组记录失败,任务组实体为null"); return; @@ -654,8 +655,10 @@ public class CommonUtil { dir.delete(); } } - DbEntity.deleteData(DownloadEntity.class, "groupHash=?", groupEntity.getGroupHash()); - DbEntity.deleteData(DownloadGroupEntity.class, "groupHash=?", groupEntity.getGroupHash()); + if (removeEntity) { + DbEntity.deleteData(DownloadEntity.class, "groupHash=?", groupEntity.getGroupHash()); + DbEntity.deleteData(DownloadGroupEntity.class, "groupHash=?", groupEntity.getGroupHash()); + } } /** diff --git a/DEV_LOG.md b/DEV_LOG.md index 08b35bd5..51b8385f 100644 --- a/DEV_LOG.md +++ b/DEV_LOG.md @@ -1,4 +1,6 @@ ## 开发日志 + + v_3.6.5 + - fix bug https://github.com/AriaLyy/Aria/issues/403 + v_3.6.4 (2019/5/16) - 优化任务接收器的代码结构 - 修复`DbEntity.saveAll()`失败的问题 diff --git a/app/build.gradle b/app/build.gradle index a2a0e16f..6b0b4ec1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,7 +1,7 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' -//apply plugin: 'kotlin-kapt' +apply plugin: 'kotlin-kapt' android { compileSdkVersion rootProject.ext.compileSdkVersion @@ -46,8 +46,9 @@ dependencies { testImplementation 'junit:junit:4.12' implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}" implementation "com.android.support:design:${rootProject.ext.supportLibVersion}" + implementation "org.jetbrains.kotlin:kotlin-stdlib:${rootProject.ext.kotlin_version}" api project(':Aria') - annotationProcessor project(':AriaCompiler') + kapt project(':AriaCompiler') // compile 'com.arialyy.aria:aria-core:3.4.0' // annotationProcessor 'com.arialyy.aria:aria-compiler:3.4' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index db48c15c..0cf6ddf8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -41,6 +41,7 @@ + diff --git a/app/src/main/java/com/arialyy/simple/core/download/DownloadActivity.java b/app/src/main/java/com/arialyy/simple/core/download/DownloadActivity.java index 9156047e..43d32d7b 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/DownloadActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/download/DownloadActivity.java @@ -72,6 +72,7 @@ public class DownloadActivity extends BaseActivity module.startNextActivity(data.get(position), HighestPriorityActivity.class); break; case 3: + module.startNextActivity(data.get(position), KotlinDownloadActivity.class); break; case 4: break; diff --git a/app/src/main/java/com/arialyy/simple/core/download/KotlinDownloadActivity.kt b/app/src/main/java/com/arialyy/simple/core/download/KotlinDownloadActivity.kt index 59414607..25a8cc59 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/KotlinDownloadActivity.kt +++ b/app/src/main/java/com/arialyy/simple/core/download/KotlinDownloadActivity.kt @@ -1,75 +1,166 @@ -//package com.arialyy.simple.core.download -// -//import android.os.Bundle -//import android.os.Environment -//import android.support.v7.app.AppCompatActivity -//import android.util.Log -//import android.view.View -//import com.arialyy.annotations.Download -//import com.arialyy.aria.core.Aria -//import com.arialyy.aria.core.download.DownloadTask -//import com.arialyy.simple.R -//import com.arialyy.simple.base.BaseActivity -// -///** -// * Created by lyy on 2017/10/23. -// */ -//class KotlinDownloadActivity : AppCompatActivity() { -// -// private val DOWNLOAD_URL = "http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk" -// -// private val TAG = "KotlinDownloadActivity"; -// -// override fun onCreate(savedInstanceState: Bundle?) { -// super.onCreate(savedInstanceState) -// setContentView(setLayoutId()) -// } -// -// fun setLayoutId(): Int { -// return R.layout.activity_single -// } -// -// fun init(savedInstanceState: Bundle?) { -// title = "单任务下载" -//// val target = Aria.download(this).load(DOWNLOAD_URL) -//// binding.progress = target.getPercent() -//// if (target.getTaskState() == IEntity.STATE_STOP) { -//// mStart.setText("恢复") -//// mStart.setTextColor(resources.getColor(android.R.color.holo_blue_light)) -//// setBtState(true) -//// } else if (target.isDownloading()) { -//// setBtState(false) -//// } -//// binding.fileSize = target.getConvertFileSize() -// Aria.get(this).downloadConfig.maxTaskNum = 2 -// Aria.download(this).register() -// } -// -// @Download.onTaskRunning -// protected fun running(task: DownloadTask) { -// Log.d(TAG, task.percent.toString() + "") -//// val len = task.fileSize -//// if (len == 0L) { -//// binding.progress = 0 -//// } else { -//// binding.progress = task.percent -//// } -//// binding.speed = task.convertSpeed -// } -// -// fun onClick(view: View) { -// when (view.id) { -// R.id.start -> startD() -// R.id.stop -> Aria.download(this).load(DOWNLOAD_URL).stop() -// R.id.cancel -> Aria.download(this).load(DOWNLOAD_URL).cancel() -// } -// } -// -// private fun startD() { -// Aria.download(this) -// .load(DOWNLOAD_URL) -// .addHeader("groupHash", "value") -// .setDownloadPath(Environment.getExternalStorageDirectory().path + "/hhhhhhhh.apk") -// .start() -// } -//} \ No newline at end of file +package com.arialyy.simple.core.download + +import android.os.Bundle +import android.os.Environment +import android.util.Log +import android.view.View +import android.widget.Button +import android.widget.Toast +import com.arialyy.annotations.Download +import com.arialyy.aria.core.Aria +import com.arialyy.aria.core.download.DownloadTarget +import com.arialyy.aria.core.download.DownloadTask +import com.arialyy.aria.core.inf.IEntity +import com.arialyy.frame.util.show.T +import com.arialyy.simple.R +import com.arialyy.simple.base.BaseActivity +import com.arialyy.simple.databinding.ActivitySingleBinding + +/** + * Created by lyy on 2017/10/23. + */ +class KotlinDownloadActivity : BaseActivity() { + + private val DOWNLOAD_URL = + "http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk" + + private lateinit var mStart: Button + private lateinit var mStop: Button + private lateinit var mCancel: Button + private lateinit var target: DownloadTarget + + override fun setLayoutId(): Int { + return R.layout.activity_single + } + + override fun init(savedInstanceState: Bundle?) { + title = "kotlin测试" + Aria.get(this) + .downloadConfig.maxTaskNum = 2 + Aria.download(this) + .register() + mStart = findViewById(R.id.start) + mStop = findViewById(R.id.stop) + mCancel = findViewById(R.id.cancel) + mStop.visibility = View.GONE + + target = Aria.download(this) + .load(DOWNLOAD_URL) + binding.progress = target.percent + if (target.taskState == IEntity.STATE_STOP) { + mStart.text = "恢复" + } else if (target.isRunning) { + mStart.text = "停止" + } + binding.fileSize = target.convertFileSize + } + + /** + * 注解方法不能添加internal修饰符,否则会出现e: [kapt] An exception occurred: java.lang.IllegalArgumentException: index 1 for '$a' not in range (received 0 arguments)错误 + */ + @Download.onTaskRunning + fun running(task: DownloadTask) { + Log.d(TAG, task.percent.toString()) + val len = task.fileSize + if (len == 0L) { + binding.progress = 0 + } else { + binding.progress = task.percent + } + binding.speed = task.convertSpeed + } + + @Download.onWait + fun onWait(task: DownloadTask) { + if (task.key == DOWNLOAD_URL) { + Log.d(TAG, "wait ==> " + task.downloadEntity.fileName) + } + } + + @Download.onPre + fun onPre(task: DownloadTask) { + if (task.key == DOWNLOAD_URL) { + mStart.text = "停止" + } + } + + @Download.onTaskStart + fun taskStart(task: DownloadTask) { + if (task.key == DOWNLOAD_URL) { + binding.fileSize = task.convertFileSize + } + } + + @Download.onTaskComplete + fun complete(task: DownloadTask) { + Log.d(TAG, "完成") + } + + @Download.onTaskResume + fun taskResume(task: DownloadTask) { + if (task.key == DOWNLOAD_URL) { + mStart.text = "停止" + } + } + + @Download.onTaskStop + fun taskStop(task: DownloadTask) { + if (task.key == DOWNLOAD_URL) { + mStart.text = "恢复" + binding.speed = "" + } + } + + @Download.onTaskCancel + fun taskCancel(task: DownloadTask) { + if (task.key == DOWNLOAD_URL) { + binding.progress = 0 + Toast.makeText(this@KotlinDownloadActivity, "取消下载", Toast.LENGTH_SHORT) + .show() + mStart.text = "开始" + binding.speed = "" + Log.d(TAG, "cancel") + } + } + + @Download.onTaskFail + fun taskFail(task: DownloadTask) { + if (task.key == DOWNLOAD_URL) { + Toast.makeText(this@KotlinDownloadActivity, "下载失败", Toast.LENGTH_SHORT) + .show() + mStart.text = "开始" + } + } + + @Download.onNoSupportBreakPoint + fun onNoSupportBreakPoint(task: DownloadTask) { + Log.d(TAG, "该下载链接不支持断点") + if (task.key == DOWNLOAD_URL) { + T.showShort(this@KotlinDownloadActivity, "该下载链接不支持断点") + } + } + + fun onClick(view: View) { + when (view.id) { + R.id.start -> { + if (target.isRunning) { + Aria.download(this) + .load(DOWNLOAD_URL) + .stop() + } else { + startD() + } + } + R.id.stop -> Aria.download(this).load(DOWNLOAD_URL).stop() + R.id.cancel -> Aria.download(this).load(DOWNLOAD_URL).cancel() + } + } + + private fun startD() { + Aria.download(this) + .load(DOWNLOAD_URL) + .addHeader("groupHash", "value") + .setFilePath(Environment.getExternalStorageDirectory().path + "/kotlin.apk") + .start() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/arialyy/simple/core/download/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/core/download/SingleTaskActivity.java index 8432a86c..d0d72cee 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/download/SingleTaskActivity.java @@ -64,9 +64,10 @@ public class SingleTaskActivity extends BaseActivity { //"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/22/94/822260b849944492caadd2983f9bb624.apks"; + "http://hzdown.muzhiwan.com/2017/05/08/nl.noio.kingdom_59104935e56f0.apk"; //"http://120.55.95.61:8811/ghcg/zg/武义总规纲要成果.zip"; //"https://yizi-kejian.oss-cn-beijing.aliyuncs.com/qimeng/package1/qmtable11.zip"; - "http://rs.0.gaoshouyou.com/d/04/1e/400423a7551e1f3f0eb1812afa1f9b44.apk"; + //"http://rs.0.gaoshouyou.com/d/04/1e/400423a7551e1f3f0eb1812afa1f9b44.apk"; //"http://chargepile2.techsum.net/car-manage/file/download?path=2019-04-26/c0242efd18be4ecbb23911b1c509dcad--掌通各系统汇总.xls"; // 无长度的chunked //"http://58.210.9.131/tpk/sipgt//TDLYZTGH.tpk"; //chunked 下载 //"http://apk500.bce.baidu-mgame.com/game/67000/67734/20170622040827_oem_5502845.apk?r=1"; @@ -299,7 +300,8 @@ public class SingleTaskActivity extends BaseActivity { Aria.download(this).load(DOWNLOAD_URL).stop(); break; case R.id.cancel: - Aria.download(this).load(DOWNLOAD_URL).cancel(true); + Aria.download(this).load(DOWNLOAD_URL).reStart(); + //Aria.download(this).load(DOWNLOAD_URL).cancel(true); //Aria.download(this).load(DOWNLOAD_URL).removeRecord(); break; } diff --git a/app/src/main/java/com/arialyy/simple/core/upload/FtpUploadActivity.java b/app/src/main/java/com/arialyy/simple/core/upload/FtpUploadActivity.java index cfc4bebc..8f58cfa3 100644 --- a/app/src/main/java/com/arialyy/simple/core/upload/FtpUploadActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/upload/FtpUploadActivity.java @@ -1,113 +1,126 @@ -/* - * 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.core.upload; - -import android.os.Bundle; -import android.util.Log; -import android.view.View; -import com.arialyy.annotations.Upload; -import com.arialyy.aria.core.Aria; -import com.arialyy.aria.core.upload.UploadEntity; -import com.arialyy.aria.core.upload.UploadTask; -import com.arialyy.aria.util.CommonUtil; -import com.arialyy.frame.util.FileUtil; -import com.arialyy.frame.util.show.T; -import com.arialyy.simple.R; -import com.arialyy.simple.base.BaseActivity; -import com.arialyy.simple.databinding.ActivityFtpUploadBinding; -import java.io.File; - -/** - * Created by lyy on 2017/7/28. Ftp 文件上传demo - */ -public class FtpUploadActivity extends BaseActivity { - private final String FILE_PATH = "/mnt/sdcard/AriaPrj.rar"; - private final String URL = "ftp://9.9.9.205:2121/aa/你好"; - - @Override protected void init(Bundle savedInstanceState) { - setTile("D_FTP 文件上传"); - super.init(savedInstanceState); - Aria.upload(this).register(); - UploadEntity entity = Aria.upload(this).getUploadEntity(FILE_PATH); - if (entity != null) { - getBinding().setFileSize(CommonUtil.formatFileSize(entity.getFileSize())); - getBinding().setProgress(entity.isComplete() ? 100 - : (int) (entity.getCurrentProgress() * 100 / entity.getFileSize())); - } - } - - @Override protected int setLayoutId() { - return R.layout.activity_ftp_upload; - } - - public void onClick(View view) { - switch (view.getId()) { - case R.id.start: - Aria.upload(this).loadFtp(FILE_PATH).setUploadUrl(URL).login("lao", "123456").start(); - break; - case R.id.stop: - Aria.upload(this).loadFtp(FILE_PATH).stop(); - break; - case R.id.cancel: - Aria.upload(this).loadFtp(FILE_PATH).cancel(); - break; - } - } - - @Upload.onWait void onWait(UploadTask task) { - Log.d(TAG, task.getTaskName() + "_wait"); - } - - @Upload.onPre public void onPre(UploadTask task) { - getBinding().setFileSize(task.getConvertFileSize()); - } - - @Upload.onTaskStart public void taskStart(UploadTask task) { - Log.d(TAG, "开始上传,md5:" + FileUtil.getFileMD5(new File(task.getEntity().getFilePath()))); - } - - @Upload.onTaskResume public void taskResume(UploadTask task) { - Log.d(TAG, "恢复上传"); - } - - @Upload.onTaskStop public void taskStop(UploadTask task) { - getBinding().setSpeed(""); - Log.d(TAG, "停止上传"); - } - - @Upload.onTaskCancel public void taskCancel(UploadTask task) { - getBinding().setSpeed(""); - getBinding().setFileSize(""); - getBinding().setProgress(0); - Log.d(TAG, "删除任务"); - } - - @Upload.onTaskFail public void taskFail(UploadTask task) { - Log.d(TAG, "上传失败"); - } - - @Upload.onTaskRunning public void taskRunning(UploadTask task) { - Log.d(TAG, "PP = " + task.getPercent()); - getBinding().setProgress(task.getPercent()); - getBinding().setSpeed(task.getConvertSpeed()); - } - - @Upload.onTaskComplete public void taskComplete(UploadTask task) { - getBinding().setProgress(100); - getBinding().setSpeed(""); - T.showShort(this, "文件:" + task.getEntity().getFileName() + ",上传完成"); - } -} +/* + * 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.core.upload; + +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import com.arialyy.annotations.Upload; +import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.common.ftp.FtpInterceptHandler; +import com.arialyy.aria.core.common.ftp.IFtpUploadInterceptor; +import com.arialyy.aria.core.upload.UploadEntity; +import com.arialyy.aria.core.upload.UploadTask; +import com.arialyy.aria.util.CommonUtil; +import com.arialyy.frame.util.FileUtil; +import com.arialyy.frame.util.show.T; +import com.arialyy.simple.R; +import com.arialyy.simple.base.BaseActivity; +import com.arialyy.simple.databinding.ActivityFtpUploadBinding; +import java.io.File; +import java.util.List; + +/** + * Created by lyy on 2017/7/28. Ftp 文件上传demo + */ +public class FtpUploadActivity extends BaseActivity { + private final String FILE_PATH = "/mnt/sdcard/AriaPrj.rar"; + private final String URL = "ftp://9.9.9.205:2121/aa/你好"; + + @Override protected void init(Bundle savedInstanceState) { + setTile("D_FTP 文件上传"); + super.init(savedInstanceState); + Aria.upload(this).register(); + UploadEntity entity = Aria.upload(this).getUploadEntity(FILE_PATH); + if (entity != null) { + getBinding().setFileSize(CommonUtil.formatFileSize(entity.getFileSize())); + getBinding().setProgress(entity.isComplete() ? 100 + : (int) (entity.getCurrentProgress() * 100 / entity.getFileSize())); + } + } + + @Override protected int setLayoutId() { + return R.layout.activity_ftp_upload; + } + + public void onClick(View view) { + switch (view.getId()) { + case R.id.start: + Aria.upload(this).loadFtp(FILE_PATH).setUploadUrl(URL).setUploadInterceptor( + new IFtpUploadInterceptor() { + + @Override + public FtpInterceptHandler onIntercept(UploadEntity entity, List fileList) { + FtpInterceptHandler.Builder builder = new FtpInterceptHandler.Builder(); + builder.coverServerFile(); + //builder.resetFileName("test"); + return builder.build(); + } + }).login("lao", "123456").start(); + break; + case R.id.stop: + Aria.upload(this).loadFtp(FILE_PATH).stop(); + break; + case R.id.cancel: + Aria.upload(this).loadFtp(FILE_PATH).cancel(); + break; + } + } + + @Upload.onWait void onWait(UploadTask task) { + Log.d(TAG, task.getTaskName() + "_wait"); + } + + @Upload.onPre public void onPre(UploadTask task) { + getBinding().setFileSize(task.getConvertFileSize()); + } + + @Upload.onTaskStart public void taskStart(UploadTask task) { + Log.d(TAG, "开始上传,md5:" + FileUtil.getFileMD5(new File(task.getEntity().getFilePath()))); + } + + @Upload.onTaskResume public void taskResume(UploadTask task) { + Log.d(TAG, "恢复上传"); + } + + @Upload.onTaskStop public void taskStop(UploadTask task) { + getBinding().setSpeed(""); + Log.d(TAG, "停止上传"); + } + + @Upload.onTaskCancel public void taskCancel(UploadTask task) { + getBinding().setSpeed(""); + getBinding().setFileSize(""); + getBinding().setProgress(0); + Log.d(TAG, "删除任务"); + } + + @Upload.onTaskFail public void taskFail(UploadTask task) { + Log.d(TAG, "上传失败"); + } + + @Upload.onTaskRunning public void taskRunning(UploadTask task) { + Log.d(TAG, "PP = " + task.getPercent()); + getBinding().setProgress(task.getPercent()); + getBinding().setSpeed(task.getConvertSpeed()); + } + + @Upload.onTaskComplete public void taskComplete(UploadTask task) { + getBinding().setProgress(100); + getBinding().setSpeed(""); + T.showShort(this, "文件:" + task.getEntity().getFileName() + ",上传完成"); + } +} diff --git a/app/src/main/java/com/arialyy/simple/modlue/CommonModule.java b/app/src/main/java/com/arialyy/simple/modlue/CommonModule.java index 1024701d..f5a2c451 100644 --- a/app/src/main/java/com/arialyy/simple/modlue/CommonModule.java +++ b/app/src/main/java/com/arialyy/simple/modlue/CommonModule.java @@ -65,6 +65,7 @@ public class CommonModule extends AbsModule { R.drawable.ic_http, R.drawable.ic_http_group, R.drawable.ic_top, + R.drawable.ic_kotlin, R.drawable.ic_server, R.drawable.ic_windows }; diff --git a/app/src/main/res/drawable/ic_kotlin.xml b/app/src/main/res/drawable/ic_kotlin.xml new file mode 100644 index 00000000..83822839 --- /dev/null +++ b/app/src/main/res/drawable/ic_kotlin.xml @@ -0,0 +1,4 @@ + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 58039153..924c6dba 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -24,6 +24,7 @@ 单任务下载 多任务下载 高优先级任务 + kotlin Service中使用 组件中使用 diff --git a/build.gradle b/build.gradle index a989f0ad..43720267 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.3.11' + ext.kotlin_version = '1.3.20' repositories { jcenter() mavenCentral()