例子编写,bug fix

This commit is contained in:
AriaLyy
2017-03-01 17:17:44 +08:00
parent f107a9852b
commit 42c374e275
32 changed files with 484 additions and 291 deletions

View File

@ -51,8 +51,7 @@ import com.arialyy.aria.core.upload.UploadTask;
* Aria.upload(this)
* .load(filePath) //文件路径,必填
* .setUploadUrl(uploadUrl) //上传路径,必填
* .setFileName(fileName) //文件名
* .setAttachment(fileKey) //服务器读取文件的key
* .setAttachment(fileKey) //服务器读取文件的key必填
* .start();
* </code>
* </pre>

View File

@ -124,7 +124,7 @@ import java.util.Map;
UploadReceiver upload(Object obj) {
IReceiver receiver = mReceivers.get(getKey(false, obj));
if (receiver == null) {
receiver = putReceiver(true, obj);
receiver = putReceiver(false, obj);
}
return (receiver instanceof UploadReceiver) ? (UploadReceiver) receiver : null;
}

View File

@ -47,12 +47,12 @@ public class ExecutePool<TASK extends ITask> implements IPool<TASK> {
@Override public boolean putTask(TASK task) {
synchronized (LOCK) {
if (task == null) {
Log.e(TAG, "下载任务不能为空!!");
Log.e(TAG, "任务不能为空!!");
return false;
}
String url = task.getKey();
if (mExecuteQueue.contains(task)) {
Log.e(TAG, "队列中已经包含了该任务,任务下载链接" + url + "");
Log.e(TAG, "队列中已经包含了该任务,任务key" + url + "");
return false;
} else {
if (mExecuteQueue.size() >= mSize) {
@ -144,7 +144,7 @@ public class ExecutePool<TASK extends ITask> implements IPool<TASK> {
@Override public TASK getTask(String downloadUrl) {
synchronized (LOCK) {
if (TextUtils.isEmpty(downloadUrl)) {
Log.e(TAG, "请传入有效的下载链接");
Log.e(TAG, "请传入有效的任务key");
return null;
}
String key = CommonUtil.keyToHashKey(downloadUrl);
@ -168,7 +168,7 @@ public class ExecutePool<TASK extends ITask> implements IPool<TASK> {
@Override public boolean removeTask(String downloadUrl) {
synchronized (LOCK) {
if (TextUtils.isEmpty(downloadUrl)) {
Log.e(TAG, "请传入有效的下载链接");
Log.e(TAG, "请传入有效的任务key");
return false;
}
String key = CommonUtil.keyToHashKey(downloadUrl);

View File

@ -16,6 +16,7 @@
package com.arialyy.aria.core.upload;
import android.support.annotation.NonNull;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.command.AbsCmd;
import com.arialyy.aria.core.command.CmdFactory;
@ -29,6 +30,8 @@ import com.arialyy.aria.util.CommonUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by Aria.Lao on 2017/2/6.
@ -50,6 +53,11 @@ public class UploadReceiver implements IReceiver<UploadEntity> {
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);
}

View File

@ -41,11 +41,11 @@ public class UploadTask implements ITask {
}
@Override public String getKey() {
return null;
return mUploadEntity.getFilePath();
}
@Override public boolean isRunning() {
return false;
return mUtil.isRunning();
}
public UploadEntity getUploadEntity() {
@ -197,7 +197,7 @@ public class UploadTask implements ITask {
*/
private void sendInState2Target(int state) {
if (outHandler.get() != null) {
outHandler.get().obtainMessage(state, task).sendToTarget();
outHandler.get().obtainMessage(state, task.get()).sendToTarget();
}
}

View File

@ -1,3 +1,18 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.upload;
import android.util.Log;
@ -21,7 +36,7 @@ import java.util.UUID;
* Created by Aria.Lao on 2017/2/9.
* 上传工具
*/
public class UploadUtil implements Runnable {
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";
@ -35,7 +50,7 @@ public class UploadUtil implements Runnable {
private boolean isCancel = false;
private boolean isRunning = false;
public UploadUtil(UploadTaskEntity taskEntity, IUploadListener listener) {
UploadUtil(UploadTaskEntity taskEntity, IUploadListener listener) {
mTaskEntity = taskEntity;
CheckUtil.checkUploadEntity(taskEntity.uploadEntity);
mUploadEntity = taskEntity.uploadEntity;
@ -51,7 +66,7 @@ public class UploadUtil implements Runnable {
new Thread(this).start();
}
public void cancel(){
public void cancel() {
isCancel = true;
isRunning = false;
}
@ -96,16 +111,13 @@ public class UploadUtil implements Runnable {
mListener.onStart(uploadFile.length());
addFilePart(mTaskEntity.attachment, uploadFile);
Log.d(TAG, finish() + "");
} catch (MalformedURLException e) {
e.printStackTrace();
fail();
} catch (IOException e) {
e.printStackTrace();
fail();
}
}
public boolean isRunning() {
boolean isRunning() {
return isRunning;
}
@ -200,8 +212,6 @@ public class UploadUtil implements Runnable {
}
reader.close();
mHttpConn.disconnect();
} else {
throw new IOException("Server returned non-OK status: " + status);
}
mWriter.flush();