diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/HttpOption.java b/Aria/src/main/java/com/arialyy/aria/core/common/HttpOption.java index 16f7d4e2..f46ff768 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/HttpOption.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/HttpOption.java @@ -35,6 +35,7 @@ public class HttpOption extends BaseOption { private Proxy proxy; private boolean useServerFileName = false; private IHttpFileLenAdapter fileLenAdapter; + private String attachment; public HttpOption() { super(); @@ -84,6 +85,19 @@ public class HttpOption extends BaseOption { return this; } + /** + * 设置文件上传需要的key + * + * @param attachment 如果为空,默认为"file" + */ + public HttpOption setAttachment(String attachment) { + if (TextUtils.isEmpty(attachment)) { + attachment = "file"; + } + this.attachment = attachment; + return this; + } + /** * 给url请求添加Header数据 * 如果新的header数据和数据保存的不一致,则更新数据库中对应的header数据 diff --git a/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpSubDLoaderUtil.java b/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpSubDLoaderUtil.java index aac29a2d..0ef7b755 100644 --- a/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpSubDLoaderUtil.java +++ b/FtpComponent/src/main/java/com/arialyy/aria/ftp/download/FtpSubDLoaderUtil.java @@ -18,6 +18,7 @@ package com.arialyy.aria.ftp.download; import android.os.Handler; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.group.AbsSubDLoadUtil; +import com.arialyy.aria.core.group.SubRecordHandler; import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.loader.SubLoader; @@ -46,7 +47,7 @@ final class FtpSubDLoaderUtil extends AbsSubDLoadUtil { @Override protected LoaderStructure buildLoaderStructure() { LoaderStructure structure = new LoaderStructure(); - structure.addComponent(new FtpDRecordHandler(getWrapper())) + structure.addComponent(new SubRecordHandler(getWrapper())) .addComponent(new FtpDTTBuilder(getWrapper())) .addComponent(new FtpDFileInfoTask(getWrapper())); structure.accept(getLoader()); diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/HttpRecordHandler.java b/HttpComponent/src/main/java/com/arialyy/aria/http/HttpRecordHandler.java index 997770c8..e0472b86 100644 --- a/HttpComponent/src/main/java/com/arialyy/aria/http/HttpRecordHandler.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/HttpRecordHandler.java @@ -85,8 +85,7 @@ public final class HttpRecordHandler extends RecordHandler { record.threadNum = threadNum; int requestType = getWrapper().getRequestType(); - if (requestType == ITaskWrapper.D_FTP || requestType == ITaskWrapper.D_FTP_DIR - || requestType == ITaskWrapper.D_HTTP || requestType == ITaskWrapper.DG_HTTP) { + if (requestType == ITaskWrapper.D_HTTP || requestType == ITaskWrapper.DG_HTTP) { record.isBlock = Configuration.getInstance().downloadCfg.isUseBlock(); } else { record.isBlock = false; diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/HttpTaskOption.java b/HttpComponent/src/main/java/com/arialyy/aria/http/HttpTaskOption.java index 116365f5..7c30d6eb 100644 --- a/HttpComponent/src/main/java/com/arialyy/aria/http/HttpTaskOption.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/HttpTaskOption.java @@ -101,7 +101,7 @@ public final class HttpTaskOption implements ITaskOption { } public String getAttachment() { - return attachment; + return TextUtils.isEmpty(attachment) ? "file" : attachment; } public void setAttachment(String attachment) { diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/HttpFileInfoTask.java b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDFileInfoTask.java similarity index 98% rename from HttpComponent/src/main/java/com/arialyy/aria/http/HttpFileInfoTask.java rename to HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDFileInfoTask.java index 29fd34ac..cc9f1bc1 100644 --- a/HttpComponent/src/main/java/com/arialyy/aria/http/HttpFileInfoTask.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDFileInfoTask.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.arialyy.aria.http; +package com.arialyy.aria.http.download; import android.net.TrafficStats; import android.net.Uri; @@ -30,6 +30,8 @@ import com.arialyy.aria.core.processor.IHttpFileLenAdapter; import com.arialyy.aria.exception.AriaIOException; import com.arialyy.aria.exception.BaseException; import com.arialyy.aria.exception.TaskException; +import com.arialyy.aria.http.ConnectionHelp; +import com.arialyy.aria.http.HttpTaskOption; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CommonUtil; @@ -55,7 +57,7 @@ import java.util.UUID; /** * 下载文件信息获取 */ -public final class HttpFileInfoTask implements IInfoTask, Runnable { +final class HttpDFileInfoTask implements IInfoTask, Runnable { private static final String TAG = "HttpFileInfoThread"; private DownloadEntity mEntity; private DTaskWrapper mTaskWrapper; @@ -63,7 +65,7 @@ public final class HttpFileInfoTask implements IInfoTask, Runnable { private Callback callback; private HttpTaskOption taskOption; - public HttpFileInfoTask(DTaskWrapper taskWrapper) { + HttpDFileInfoTask(DTaskWrapper taskWrapper) { this.mTaskWrapper = taskWrapper; mEntity = taskWrapper.getEntity(); mConnectTimeOut = AriaConfig.getInstance().getDConfig().getConnectTimeOut(); diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGInfoTask.java b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGInfoTask.java index 32f1308b..5698df6c 100644 --- a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGInfoTask.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDGInfoTask.java @@ -25,7 +25,6 @@ import com.arialyy.aria.core.loader.IInfoTask; import com.arialyy.aria.core.loader.ILoaderVisitor; import com.arialyy.aria.exception.AriaIOException; import com.arialyy.aria.exception.BaseException; -import com.arialyy.aria.http.HttpFileInfoTask; import com.arialyy.aria.http.HttpTaskOption; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; @@ -98,7 +97,7 @@ public final class HttpDGInfoTask implements IInfoTask { } } - /*z + /* * 获取组合任务大小,使用该方式获取到的组合任务大小,子任务不需要再重新获取文件大小 */ private void getGroupSize() { @@ -106,7 +105,7 @@ public final class HttpDGInfoTask implements IInfoTask { @Override public void run() { for (DTaskWrapper dTaskWrapper : wrapper.getSubTaskWrapper()) { cloneHeader(dTaskWrapper); - HttpFileInfoTask infoTask = new HttpFileInfoTask(dTaskWrapper); + HttpDFileInfoTask infoTask = new HttpDFileInfoTask(dTaskWrapper); infoTask.setCallback(subCallback); mPool.execute(infoTask); } diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDLoaderUtil.java b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDLoaderUtil.java index 7843c596..cda63845 100644 --- a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDLoaderUtil.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpDLoaderUtil.java @@ -23,7 +23,6 @@ import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.loader.NormalLoader; import com.arialyy.aria.core.loader.NormalThreadStateManager; import com.arialyy.aria.core.wrapper.AbsTaskWrapper; -import com.arialyy.aria.http.HttpFileInfoTask; import com.arialyy.aria.http.HttpRecordHandler; import com.arialyy.aria.http.HttpTaskOption; @@ -45,7 +44,7 @@ public final class HttpDLoaderUtil extends AbsNormalLoaderUtil { LoaderStructure structure = new LoaderStructure(); structure.addComponent(new HttpRecordHandler(getTaskWrapper())) .addComponent(new NormalThreadStateManager(getListener())) - .addComponent(new HttpFileInfoTask((DTaskWrapper) getTaskWrapper())) + .addComponent(new HttpDFileInfoTask((DTaskWrapper) getTaskWrapper())) .addComponent(new HttpDTTBuilder(getTaskWrapper())); structure.accept(getLoader()); return structure; diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpSubDLoaderUtil.java b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpSubDLoaderUtil.java index a7079691..78f483e0 100644 --- a/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpSubDLoaderUtil.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/download/HttpSubDLoaderUtil.java @@ -18,10 +18,9 @@ package com.arialyy.aria.http.download; import android.os.Handler; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.group.AbsSubDLoadUtil; +import com.arialyy.aria.core.group.SubRecordHandler; import com.arialyy.aria.core.loader.LoaderStructure; import com.arialyy.aria.core.loader.SubLoader; -import com.arialyy.aria.http.HttpFileInfoTask; -import com.arialyy.aria.http.HttpRecordHandler; /** * @Author lyy @@ -48,9 +47,9 @@ final class HttpSubDLoaderUtil extends AbsSubDLoadUtil { @Override protected LoaderStructure buildLoaderStructure() { LoaderStructure structure = new LoaderStructure(); - structure.addComponent(new HttpRecordHandler(getWrapper())) + structure.addComponent(new SubRecordHandler(getWrapper())) .addComponent(new HttpDTTBuilder(getWrapper())) - .addComponent(new HttpFileInfoTask(getWrapper())); + .addComponent(new HttpDFileInfoTask(getWrapper())); structure.accept(getLoader()); return structure; } diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoader.java b/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoader.java new file mode 100644 index 00000000..6e1bc4f4 --- /dev/null +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoader.java @@ -0,0 +1,94 @@ +/* + * 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.http.upload; + +import android.os.Handler; +import android.os.Looper; +import com.arialyy.aria.core.inf.IThreadStateManager; +import com.arialyy.aria.core.listener.IEventListener; +import com.arialyy.aria.core.loader.AbsNormalLoader; +import com.arialyy.aria.core.loader.IInfoTask; +import com.arialyy.aria.core.loader.IRecordHandler; +import com.arialyy.aria.core.loader.IThreadTaskBuilder; +import com.arialyy.aria.core.manager.ThreadTaskManager; +import com.arialyy.aria.core.task.IThreadTask; +import com.arialyy.aria.core.upload.UTaskWrapper; +import com.arialyy.aria.exception.AriaIOException; +import com.arialyy.aria.util.ALog; +import java.util.List; + +final class HttpULoader extends AbsNormalLoader { + HttpULoader(UTaskWrapper wrapper, IEventListener listener) { + super(wrapper, listener); + } + + @Override public void addComponent(IRecordHandler recordHandler) { + mRecordHandler = recordHandler; + } + + /** + * @deprecated http 上传任务不需要设置这个 + */ + @Deprecated + @Override public void addComponent(IInfoTask infoTask) { + + } + + @Override public void addComponent(IThreadStateManager threadState) { + mStateManager = threadState; + } + + @Override public void addComponent(IThreadTaskBuilder builder) { + mTTBuilder = builder; + } + + @Override protected void handleTask(Looper looper) { + mRecord = mRecordHandler.getRecord(getFileSize()); + mStateManager.setLooper(mRecord, looper); + List tt = mTTBuilder.buildThreadTask(mRecord, + new Handler(looper, mStateManager.getHandlerCallback())); + if (tt == null || tt.isEmpty()) { + ALog.e(TAG, "创建线程任务失败"); + getListener().onFail(false, new AriaIOException(TAG, "创建线程任务失败")); + return; + } + + getListener().onStart(0); + ThreadTaskManager.getInstance().startThread(mTaskWrapper.getKey(), tt.get(0)); + + startTimer(); + } + + @Override public long getFileSize() { + return mTaskWrapper.getEntity().getFileSize(); + } + + @Override protected void checkComponent() { + if (mRecordHandler == null) { + throw new NullPointerException("任务记录组件为空"); + } + if (mStateManager == null) { + throw new NullPointerException("任务状态管理组件为空"); + } + if (mTTBuilder == null) { + throw new NullPointerException("线程任务组件为空"); + } + } + + @Override public long getCurrentProgress() { + return mStateManager.getCurrentProgress(); + } +} diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoaderUtil.java b/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoaderUtil.java index 3f3faaa2..f969390d 100644 --- a/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoaderUtil.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpULoaderUtil.java @@ -15,15 +15,13 @@ */ package com.arialyy.aria.http.upload; -import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.loader.AbsNormalLoader; import com.arialyy.aria.core.loader.AbsNormalLoaderUtil; import com.arialyy.aria.core.loader.LoaderStructure; -import com.arialyy.aria.core.loader.NormalLoader; import com.arialyy.aria.core.loader.NormalThreadStateManager; +import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.core.wrapper.AbsTaskWrapper; -import com.arialyy.aria.http.HttpFileInfoTask; import com.arialyy.aria.http.HttpRecordHandler; import com.arialyy.aria.http.HttpTaskOption; @@ -38,14 +36,14 @@ public final class HttpULoaderUtil extends AbsNormalLoaderUtil { } @Override public AbsNormalLoader getLoader() { - return mLoader == null ? new NormalLoader(getTaskWrapper(), getListener()) : mLoader; + return mLoader == null ? new HttpULoader((UTaskWrapper) getTaskWrapper(), getListener()) + : mLoader; } @Override public LoaderStructure BuildLoaderStructure() { LoaderStructure structure = new LoaderStructure(); structure.addComponent(new HttpRecordHandler(getTaskWrapper())) .addComponent(new NormalThreadStateManager(getListener())) - .addComponent(new HttpFileInfoTask((DTaskWrapper) getTaskWrapper())) .addComponent(new HttpUTTBuilder(getTaskWrapper())); structure.accept(getLoader()); return structure; diff --git a/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpUThreadTaskAdapter.java b/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpUThreadTaskAdapter.java index 3b20ba79..bfa972cc 100644 --- a/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpUThreadTaskAdapter.java +++ b/HttpComponent/src/main/java/com/arialyy/aria/http/upload/HttpUThreadTaskAdapter.java @@ -18,6 +18,7 @@ package com.arialyy.aria.http.upload; import android.text.TextUtils; import com.arialyy.aria.core.common.SubThreadConfig; import com.arialyy.aria.core.upload.UploadEntity; +import com.arialyy.aria.exception.AriaIOException; import com.arialyy.aria.exception.BaseException; import com.arialyy.aria.exception.TaskException; import com.arialyy.aria.http.BaseHttpThreadTaskAdapter; @@ -102,7 +103,7 @@ final class HttpUThreadTaskAdapter extends BaseHttpThreadTaskAdapter { } uploadFile(writer, mTaskOption.getAttachment(), uploadFile); - complete(); + getEntity().setResponseStr(finish(writer)); } catch (Exception e) { e.printStackTrace(); fail(new TaskException(TAG, @@ -193,11 +194,6 @@ final class HttpUThreadTaskAdapter extends BaseHttpThreadTaskAdapter { inputStream.close(); writer.append(LINE_END); writer.flush(); - //if (getState().isCancel) { - // getState().isRunning = false; - // return; - //} - //getState().isRunning = false; } /** @@ -210,7 +206,7 @@ final class HttpUThreadTaskAdapter extends BaseHttpThreadTaskAdapter { writer.append(LINE_END).flush(); writer.append(PREFIX).append(BOUNDARY).append(PREFIX).append(LINE_END); - writer.close(); + writer.flush(); int status = mHttpConn.getResponseCode(); @@ -222,9 +218,11 @@ final class HttpUThreadTaskAdapter extends BaseHttpThreadTaskAdapter { } reader.close(); mHttpConn.disconnect(); + complete(); } else { - ALog.e(TAG, "response msg: " + mHttpConn.getResponseMessage() + ",code: " + status); - // fail(); + String msg = "response msg: " + mHttpConn.getResponseMessage() + ",code: " + status; + ALog.e(TAG, msg); + fail(new AriaIOException(TAG, msg), false); } writer.flush(); writer.close(); diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/event/EventMsgUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/core/event/EventMsgUtil.java index 7e3cb342..f4a27dd1 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/event/EventMsgUtil.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/event/EventMsgUtil.java @@ -38,7 +38,7 @@ public class EventMsgUtil { private Map> mEventMethods = new ConcurrentHashMap<>(); private ArrayBlockingQueue mEventQueue = new ArrayBlockingQueue<>(10); - private ExecutorService mPool = Executors.newFixedThreadPool(10); + private ExecutorService mPool = Executors.newFixedThreadPool(5); private EventMsgUtil() { ExecutorService pool = Executors.newSingleThreadExecutor(); diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoader.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoader.java index 4dbb02c1..bcde1e48 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoader.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoader.java @@ -217,36 +217,14 @@ public abstract class AbsGroupLoader implements ILoaderVisitor, ILoader { @Override public void cancel() { isCancel = true; closeTimer(); - onPreCancel(); - mSubQueue.removeAllTask(); mListener.onCancel(); } - /** - * onCancel前的操作 - */ - protected void onPreCancel() { - - } - @Override public void stop() { isStop = true; - closeTimer(); - if (onPreStop()) { - return; - } mSubQueue.stopAllTask(); - } - - /** - * onStop前的操作 - * - * @return 返回{@code true},直接回调{@link IDGroupListener#onStop(long)} - */ - protected boolean onPreStop() { - - return false; + closeTimer(); } @Override public void run() { @@ -265,6 +243,9 @@ public abstract class AbsGroupLoader implements ILoaderVisitor, ILoader { closeTimer(); Looper.prepare(); Looper looper = Looper.myLooper(); + if (looper == Looper.getMainLooper()) { + throw new IllegalThreadStateException("不能在主线程程序中调用Loader"); + } initState(looper); getState().setSubSize(getWrapper().getSubTaskWrapper().size()); if (getState().getCompleteNum() != 0 diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoaderUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoaderUtil.java index 520486e2..3cde8aa8 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoaderUtil.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/AbsGroupLoaderUtil.java @@ -99,6 +99,8 @@ public abstract class AbsGroupLoaderUtil implements IUtil { } buildLoaderStructure(); - new Thread(mLoader).start(); + // MsgEvent 已经是在线程中使用了,不需要重开线程 + mLoader.run(); + //new Thread(mLoader).start(); } } diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSchedulers.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSchedulers.java index 7bf10c0f..d7192d6f 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSchedulers.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSchedulers.java @@ -26,6 +26,7 @@ import com.arialyy.aria.core.loader.IRecordHandler; import com.arialyy.aria.core.manager.ThreadTaskManager; import com.arialyy.aria.exception.TaskException; import com.arialyy.aria.util.ALog; +import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.NetUtils; import java.io.File; @@ -34,7 +35,7 @@ import java.io.File; * 该调度器生命周期和{@link AbsGroupLoaderUtil}生命周期一致 */ final class SimpleSchedulers implements Handler.Callback { - private static final String TAG = "SimpleSchedulers"; + private final String TAG = CommonUtil.getClassName(this); private SimpleSubQueue mQueue; private GroupRunState mGState; private String mKey; // 组合任务的key diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSubQueue.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSubQueue.java index b50c7758..3a6dae12 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSubQueue.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/SimpleSubQueue.java @@ -58,11 +58,7 @@ final class SimpleSubQueue implements ISubQueue { return new SimpleSubQueue(); } - Map getExec() { - return mExec; - } - - AbsSubDLoadUtil getLoaderUtil(String key) { + synchronized AbsSubDLoadUtil getLoaderUtil(String key) { AbsSubDLoadUtil sub = mExec.get(key); if (sub != null) { return sub; diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/group/SubRecordHandler.java b/PublicComponent/src/main/java/com/arialyy/aria/core/group/SubRecordHandler.java new file mode 100644 index 00000000..b977a60b --- /dev/null +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/group/SubRecordHandler.java @@ -0,0 +1,73 @@ +/* + * 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.group; + +import com.arialyy.aria.core.TaskRecord; +import com.arialyy.aria.core.ThreadRecord; +import com.arialyy.aria.core.common.RecordHandler; +import com.arialyy.aria.core.common.RecordHelper; +import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.wrapper.AbsTaskWrapper; +import java.util.ArrayList; + +/** + * 子任务记录处理 + */ +public class SubRecordHandler extends RecordHandler { + public SubRecordHandler(AbsTaskWrapper wrapper) { + super(wrapper); + } + + @Override public void handlerTaskRecord(TaskRecord record) { + RecordHelper helper = new RecordHelper(getWrapper(), record); + helper.handleSingleThreadRecord(); + } + + @Override + public ThreadRecord createThreadRecord(TaskRecord record, int threadId, long startL, long endL) { + ThreadRecord tr; + tr = new ThreadRecord(); + tr.taskKey = record.filePath; + tr.threadId = threadId; + tr.startLocation = startL; + tr.isComplete = false; + tr.threadType = getEntity().getTaskType(); + tr.endLocation = getFileSize(); + tr.blockLen = getFileSize(); + return tr; + } + + @Override public TaskRecord createTaskRecord(int threadNum) { + TaskRecord record = new TaskRecord(); + record.fileName = getEntity().getFileName(); + record.filePath = getEntity().getFilePath(); + record.fileLength = getFileSize(); + record.threadRecords = new ArrayList<>(); + record.threadNum = threadNum; + record.isBlock = false; + record.taskType = getEntity().getTaskType(); + record.isGroupRecord = true; + if (getEntity() instanceof DownloadEntity) { + record.dGroupHash = ((DownloadEntity) getEntity()).getGroupHash(); + } + + return record; + } + + @Override public int initTaskThreadNum() { + return 1; + } +} diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsNormalLoader.java b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsNormalLoader.java index 612e586d..a1918314 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsNormalLoader.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsNormalLoader.java @@ -123,6 +123,9 @@ public abstract class AbsNormalLoader implements ILoaderVisitor, ILoader { } Looper.prepare(); Looper looper = Looper.myLooper(); + if (looper == Looper.getMainLooper()){ + throw new IllegalThreadStateException("不能在主线程程序中调用Loader"); + } isRuning = true; resetState(); onPostPre(); diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsNormalLoaderUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsNormalLoaderUtil.java index ab03df26..c2570d51 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsNormalLoaderUtil.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/loader/AbsNormalLoaderUtil.java @@ -115,7 +115,9 @@ public abstract class AbsNormalLoaderUtil implements IUtil { //} BuildLoaderStructure(); - new Thread(mLoader).start(); + // MsgEvent 已经是在线程中使用了,不需要重开线程 + mLoader.run(); + //new Thread(mLoader).start(); onStart(); } diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsTask.java b/PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsTask.java index f3e04445..22b79825 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsTask.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/AbsTask.java @@ -26,6 +26,7 @@ import com.arialyy.aria.core.listener.IEventListener; import com.arialyy.aria.core.wrapper.AbsTaskWrapper; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; +import com.arialyy.aria.util.ComponentUtil; import java.util.HashMap; import java.util.Map; @@ -63,11 +64,9 @@ public abstract class AbsTask return mOutHandler; } - protected abstract IUtil createUtil(); - protected synchronized IUtil getUtil() { if (mUtil == null) { - mUtil = createUtil(); + mUtil = ComponentUtil.getInstance().buildUtil(mTaskWrapper, mListener); } return mUtil; } @@ -214,8 +213,8 @@ public abstract class AbsTask @Override public void start(int type) { mSchedulerType = type; - mUtil = createUtil(); - if (mUtil == null){ + mUtil = getUtil(); + if (mUtil == null) { ALog.e(TAG, "任务工具创建失败"); return; } @@ -240,8 +239,8 @@ public abstract class AbsTask } @Override public void stop(int type) { - mUtil = createUtil(); - if (mUtil == null){ + mUtil = getUtil(); + if (mUtil == null) { ALog.e(TAG, "任务工具创建失败"); return; } @@ -255,8 +254,8 @@ public abstract class AbsTask } @Override public void cancel(int type) { - mUtil = createUtil(); - if (mUtil == null){ + mUtil = getUtil(); + if (mUtil == null) { ALog.e(TAG, "任务工具创建失败"); return; } diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadGroupTask.java b/PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadGroupTask.java index 2b820db8..ac5820d3 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadGroupTask.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadGroupTask.java @@ -21,9 +21,7 @@ import android.text.TextUtils; import com.arialyy.aria.core.AriaConfig; import com.arialyy.aria.core.download.DGTaskWrapper; import com.arialyy.aria.core.download.DownloadGroupEntity; -import com.arialyy.aria.core.inf.IUtil; import com.arialyy.aria.core.listener.ISchedulers; -import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.ComponentUtil; /** @@ -53,10 +51,6 @@ public class DownloadGroupTask extends AbsGroupTask { return DOWNLOAD_GROUP; } - @Override protected synchronized IUtil createUtil() { - return ComponentUtil.getInstance().buildUtil(mTaskWrapper, mListener); - } - public static class Builder { DGTaskWrapper taskEntity; Handler outHandler; diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadTask.java b/PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadTask.java index 002fdbff..90c32083 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadTask.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/DownloadTask.java @@ -21,7 +21,6 @@ import android.os.Looper; import com.arialyy.aria.core.AriaConfig; import com.arialyy.aria.core.download.DTaskWrapper; import com.arialyy.aria.core.download.DownloadEntity; -import com.arialyy.aria.core.inf.IUtil; import com.arialyy.aria.core.listener.ISchedulers; import com.arialyy.aria.util.ComponentUtil; @@ -75,10 +74,6 @@ public class DownloadTask extends AbsTask { return mTaskWrapper.getEntity().getFileName(); } - @Override protected synchronized IUtil createUtil() { - return ComponentUtil.getInstance().buildUtil(mTaskWrapper, mListener); - } - public static class Builder { DTaskWrapper taskEntity; Handler outHandler; diff --git a/PublicComponent/src/main/java/com/arialyy/aria/core/task/UploadTask.java b/PublicComponent/src/main/java/com/arialyy/aria/core/task/UploadTask.java index 9a2adcb8..54805198 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/core/task/UploadTask.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/core/task/UploadTask.java @@ -17,7 +17,6 @@ package com.arialyy.aria.core.task; import android.os.Handler; import android.os.Looper; -import com.arialyy.aria.core.inf.IUtil; import com.arialyy.aria.core.listener.ISchedulers; import com.arialyy.aria.core.upload.UTaskWrapper; import com.arialyy.aria.core.upload.UploadEntity; @@ -51,10 +50,6 @@ public class UploadTask extends AbsTask { return mTaskWrapper.getEntity().getFileName(); } - @Override protected synchronized IUtil createUtil() { - return ComponentUtil.getInstance().buildUtil(mTaskWrapper, mListener); - } - public static class Builder { private Handler mOutHandler; private UTaskWrapper mTaskEntity; diff --git a/PublicComponent/src/main/java/com/arialyy/aria/util/ComponentUtil.java b/PublicComponent/src/main/java/com/arialyy/aria/util/ComponentUtil.java index 30fb69fd..d3310acf 100644 --- a/PublicComponent/src/main/java/com/arialyy/aria/util/ComponentUtil.java +++ b/PublicComponent/src/main/java/com/arialyy/aria/util/ComponentUtil.java @@ -98,7 +98,8 @@ public class ComponentUtil { * * @return 返回下载工具,创建失败返回null */ - public T buildUtil(AbsTaskWrapper wrapper, IEventListener listener) { + public synchronized T buildUtil(AbsTaskWrapper wrapper, + IEventListener listener) { int requestType = wrapper.getRequestType(); String className = null; switch (requestType) { @@ -158,7 +159,7 @@ public class ComponentUtil { * @param wrapperType 任务类型{@link ITaskWrapper} * @return 返回事件监听,如果创建失败返回null */ - public T buildListener(int wrapperType, AbsTask task, + public synchronized T buildListener(int wrapperType, AbsTask task, Handler outHandler) { String className = null, errorStr = "请添加FTP插件"; switch (wrapperType) { @@ -211,7 +212,8 @@ public class ComponentUtil { * @param params 任务配置信息参数 * @return 构建失败,返回null */ - public T buildTaskOption(Class clazz, TaskOptionParams params) { + public synchronized T buildTaskOption(Class clazz, + TaskOptionParams params) { List fields = CommonUtil.getAllFields(clazz); T taskOption = null; try { diff --git a/app/src/main/java/com/arialyy/simple/core/download/HttpDownloadModule.java b/app/src/main/java/com/arialyy/simple/core/download/HttpDownloadModule.java index 869febdc..a5c311a1 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/HttpDownloadModule.java +++ b/app/src/main/java/com/arialyy/simple/core/download/HttpDownloadModule.java @@ -59,8 +59,9 @@ public class HttpDownloadModule extends BaseViewModule { //String url = "https://ss1.baidu.com/-4o3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=a9e671b9a551f3dedcb2bf64a4eff0ec/4610b912c8fcc3cef70d70409845d688d53f20f7.jpg"; //String filePath = AppUtil.getConfigValue(context, HTTP_PATH_KEY, defFilePath); String url = "https://y.qq.com/download/import/QQMusic-import-1.2.1.zip"; - String filePath = "/mnt/sdcard/QQMusic-import-1.2.1.zip"; - //String filePath = "/mnt/sdcard/update.zip"; + String filePath = "/mnt/sdcard/update.zip"; + //String url = "https://dhfspace.360drm.com/1_12809_1543904946_VID_20180808_212829.vep?e=1578554567&token=gUBmfZgZS5wy4wdQIDZG8UVxlNCyVSjvksIb13K5:WYSZRgmLbH1_9hjgqOAGmqR27JM="; + //String filePath = "/mnt/sdcard/sssss.zip"; singDownloadInfo = Aria.download(context).getFirstDownloadEntity(url); if (singDownloadInfo == null) { diff --git a/app/src/main/java/com/arialyy/simple/core/upload/HttpUploadActivity.java b/app/src/main/java/com/arialyy/simple/core/upload/HttpUploadActivity.java index 944a8c8c..99853e7b 100644 --- a/app/src/main/java/com/arialyy/simple/core/upload/HttpUploadActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/upload/HttpUploadActivity.java @@ -41,9 +41,9 @@ public class HttpUploadActivity extends BaseActivity { private static final String TAG = "HttpUploadActivity"; HorizontalProgressBarWithNumber mPb; - //private final String FILE_PATH = "/mnt/sdcard/ggsg14.apk"; - private final String FILE_PATH = - Environment.getExternalStorageDirectory().getPath() + "/Download/QQMusic-import-1.2.1.zip"; + private final String FILE_PATH = "/mnt/sdcard/QQMusic-import-1.2.1.zip"; + //private final String FILE_PATH = + // Environment.getExternalStorageDirectory().getPath() + "/Download/QQMusic-import-1.2.1.zip"; private UploadEntity mEntity; @Override protected int setLayoutId() { @@ -78,11 +78,13 @@ public class HttpUploadActivity extends BaseActivity { void upload() { HttpOption option = new HttpOption(); - option.setRequestType(RequestEnum.POST) - .setParam("params", "bbbbbbbb"); + option.setRequestType(RequestEnum.POST); + option.setParam("params", "bbbbbbbb"); + option.setAttachment("file"); Aria.upload(HttpUploadActivity.this).load(FILE_PATH) //.setUploadUrl("http://lib-test.xzxyun.com:8042/Api/upload?data={\"type\":\"1\",\"fileType\":\".apk\"}") - .setUploadUrl("http://9.9.12.210:5000/upload/") + .setUploadUrl("http://9.9.9.28:5000/upload/") + .ignoreFilePathOccupy() //.setTempUrl("http://192.168.1.6:8080/upload/sign_file/").setAttachment("file") //.addHeader("iplanetdirectorypro", "11a09102fb934ad0bc206f9c611d7933") .option(option) @@ -124,7 +126,7 @@ public class HttpUploadActivity extends BaseActivity { @Upload.onTaskRunning public void taskRunning(UploadTask task) { getBinding().setSpeed(task.getConvertSpeed()); getBinding().setProgress(task.getPercent()); - L.d(TAG, "P => " + task.getPercent()); + L.d(TAG, "running, P = " + task.getPercent()); } @Upload.onTaskComplete public void taskComplete(UploadTask task) { diff --git a/build.gradle b/build.gradle index 92fb3cba..2320bab2 100644 --- a/build.gradle +++ b/build.gradle @@ -45,7 +45,7 @@ task clean(type: Delete) { ext { versionCode = 383 - versionName = '3.8.3_beta_1' + versionName = '3.8.3_beta_2' userOrg = 'arialyy' groupId = 'com.arialyy.aria' publishVersion = versionName diff --git a/py/upload.py b/py/upload.py index eda21a2c..586a14e0 100644 --- a/py/upload.py +++ b/py/upload.py @@ -3,7 +3,7 @@ import os from flask import Flask, request, url_for, send_from_directory from werkzeug import secure_filename -ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif', 'rar', 'apk']) +ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif', 'rar', 'apk', 'zip']) app = Flask(__name__) app.config['UPLOAD_FOLDER'] = '/Users/aria/temp/test/' @@ -49,13 +49,13 @@ def upload_file(): file = request.files['file'] print(file) - if file and allowed_file(file.filename): - print('start save') - filename = secure_filename(file.filename) - file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) + #if file and allowed_file(file.filename): + print('start save') + filename = secure_filename(file.filename) + file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) # file_url = url_for('uploaded_file', filename=filename) # return html + '
' - return '200' + return '200' return '405'