compiler 重构

This commit is contained in:
AriaLyy
2017-09-07 21:43:47 +08:00
parent ac99cc581f
commit 3574531d8d
31 changed files with 1103 additions and 912 deletions

View File

@@ -114,110 +114,4 @@ import com.arialyy.aria.core.upload.UploadTask;
throw new IllegalArgumentException("不支持的类型");
}
}
/**
* 上传任务状态监听
*
* @see Upload
* @deprecated 请使用注解函数的方式来实现事件的获取
*/
@Deprecated public static class UploadSchedulerListener
implements ISchedulerListener<UploadTask> {
/**
* 预处理有时有些地址链接比较慢这时可以先在这个地方出来一些界面上的UI如按钮的状态。
*
* @param task 上传文物实体
*/
@Override public void onPre(UploadTask task) {
}
@Override public void onTaskPre(UploadTask task) {
}
@Override public void onTaskResume(UploadTask task) {
}
@Override public void onTaskStart(UploadTask task) {
}
@Override public void onTaskStop(UploadTask task) {
}
@Override public void onTaskCancel(UploadTask task) {
}
@Override public void onTaskFail(UploadTask task) {
}
@Override public void onTaskComplete(UploadTask task) {
}
@Override public void onTaskRunning(UploadTask task) {
}
}
/**
* 下载任务状态监听
*
* @see Download
* @deprecated 请使用注解函数的方式来实现事件的获取
*/
@Deprecated public static class DownloadSchedulerListener
implements IDownloadSchedulerListener<DownloadTask> {
/**
* 预处理有时有些地址链接比较慢这时可以先在这个地方出来一些界面上的UI如按钮的状态。
* 需要注意的是在该回调中是得不到文件长度的如果需要获取文件长度需要在onTaskPre中获取
*
* @param task 下载任务
*/
@Override public void onPre(DownloadTask task) {
}
@Override public void onTaskPre(DownloadTask task) {
}
@Override public void onTaskResume(DownloadTask task) {
}
@Override public void onTaskStart(DownloadTask task) {
}
@Override public void onTaskStop(DownloadTask task) {
}
@Override public void onTaskCancel(DownloadTask task) {
}
@Override public void onTaskFail(DownloadTask task) {
}
@Override public void onTaskComplete(DownloadTask task) {
}
@Override public void onTaskRunning(DownloadTask task) {
}
@Override public void onNoSupportBreakPoint(DownloadTask task) {
}
}
}

View File

@@ -398,7 +398,6 @@ import org.xml.sax.SAXException;
if (key.contains(clsName)) {
IReceiver receiver = mReceivers.get(key);
if (receiver != null) {
receiver.removeSchedulerListener();
receiver.unRegister();
receiver.destroy();
}

View File

@@ -30,7 +30,7 @@ import java.lang.ref.WeakReference;
*/
class BaseDListener<ENTITY extends AbsEntity, TASK extends AbsTask<ENTITY>>
implements IDownloadListener {
private WeakReference<Handler> outHandler;
protected WeakReference<Handler> outHandler;
private long mLastLen = 0; //上一次发送长度
private boolean isFirst = true;
protected ENTITY mEntity;
@@ -117,6 +117,7 @@ class BaseDListener<ENTITY extends AbsEntity, TASK extends AbsTask<ENTITY>>
} else {
mEntity.setSpeed(speed < 0 ? 0 : speed);
}
mEntity.setPercent((int) (mEntity.getCurrentProgress() * 100 / mEntity.getFileSize()));
}
/**

View File

@@ -17,6 +17,7 @@ package com.arialyy.aria.core.download;
import android.os.Handler;
import com.arialyy.aria.core.download.downloader.IDownloadGroupListener;
import com.arialyy.aria.core.scheduler.ISchedulers;
/**
* Created by Aria.Lao on 2017/7/20.
@@ -30,28 +31,51 @@ class DownloadGroupListener extends BaseDListener<DownloadGroupEntity, DownloadG
super(task, outHandler);
}
@Override public void onSubPre(DownloadEntity subEntity) {
sendInState2Target(ISchedulers.SUB_PRE);
}
@Override public void supportBreakpoint(boolean support, DownloadEntity subEntity) {
}
@Override public void onSubStart(DownloadEntity subEntity) {
sendInState2Target(ISchedulers.SUB_START);
}
@Override public void onSubStop(DownloadEntity subEntity) {
saveCurrentLocation();
sendInState2Target(ISchedulers.SUB_STOP);
}
@Override public void onSubComplete(DownloadEntity subEntity) {
saveCurrentLocation();
sendInState2Target(ISchedulers.SUB_COMPLETE);
}
@Override public void onSubFail(DownloadEntity subEntity) {
saveCurrentLocation();
sendInState2Target(ISchedulers.SUB_FAIL);
}
@Override public void onSubCancel(DownloadEntity entity) {
saveCurrentLocation();
sendInState2Target(ISchedulers.SUB_CANCEL);
}
@Override public void onSubRunning(DownloadEntity subEntity) {
sendInState2Target(ISchedulers.SUB_RUNNING);
}
/**
* 将任务状态发送给下载器
*
* @param state {@link ISchedulers#START}
*/
private void sendInState2Target(int state) {
if (outHandler.get() != null) {
outHandler.get().obtainMessage(state, ISchedulers.IS_SUB_TASK, 0, mTask).sendToTarget();
}
}
private void saveCurrentLocation() {

View File

@@ -166,29 +166,6 @@ public class DownloadReceiver extends AbsReceiver {
}
}
/**
* 添加调度器回调
*
* @see #register()
*/
@Deprecated public DownloadReceiver addSchedulerListener(
ISchedulerListener<DownloadTask> listener) {
this.listener = listener;
DownloadSchedulers.getInstance().addSchedulerListener(targetName, listener);
return this;
}
/**
* 移除回调
*
* @see #unRegister()
*/
@Deprecated @Override public void removeSchedulerListener() {
if (listener != null) {
DownloadSchedulers.getInstance().removeSchedulerListener(targetName, listener);
}
}
@Override public void destroy() {
targetName = null;
listener = null;

View File

@@ -127,6 +127,7 @@ public abstract class AbsGroupUtil implements IUtil {
mTaskEntity.getEntity().update();
}
}
/**
* 启动子任务下载
*
@@ -457,6 +458,7 @@ public abstract class AbsGroupUtil implements IUtil {
private void handleSpeed(long speed) {
entity.setSpeed(speed);
entity.setConvertSpeed(speed <= 0 ? "" : CommonUtil.formatFileSize(speed) + "/s");
entity.setPercent((int) (entity.getCurrentProgress() * 100 / entity.getFileSize()));
}
private void saveData(int state, long location) {

View File

@@ -1,118 +0,0 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.download.downloader;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
/**
* Created by Aria.Lao on 2017/7/26.
*/
public class FtpClientHelp {
private final String TAG = "FtpClientHelp";
private static volatile FtpClientHelp INSTANCE = null;
private FTPClient client;
private String serverIp, user, pw, account;
private int port;
private FtpClientHelp() {
}
public static FtpClientHelp getInstnce() {
if (INSTANCE == null) {
synchronized (AriaManager.LOCK) {
INSTANCE = new FtpClientHelp();
}
}
return INSTANCE;
}
public FTPClient getClient() {
if (client == null || !client.isConnected()) {
createClient();
}
return client;
}
/**
* 登录到FTP服务器当客户端为null或客户端没有连接到FTP服务器时才会执行登录操作
*/
public FTPClient login(String serverIp, int port, String user, String pw, String account) {
this.serverIp = serverIp;
this.port = port;
this.user = user;
this.pw = pw;
this.account = account;
if (client == null || !client.isConnected()) {
createClient();
}
return client;
}
/**
* 登出
*/
public void logout() {
try {
if (client != null && client.isConnected()) {
client.logout();
}
} catch (IOException e) {
e.printStackTrace();
}
}
FTPClient createClient() {
new Thread(new Runnable() {
@Override public void run() {
client = new FTPClient();
try {
client.connect(serverIp, port);
if (!TextUtils.isEmpty(account)) {
client.login(user, pw);
} else {
client.login(user, pw, account);
}
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
Log.e(TAG, "无法连接到ftp服务器错误码为" + reply);
}
} catch (IOException e) {
Log.d(TAG, e.getMessage());
} finally {
synchronized (FtpClientHelp.this) {
FtpClientHelp.this.notify();
}
}
}
}).start();
synchronized (FtpClientHelp.this) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return client;
}
}

View File

@@ -17,7 +17,6 @@ package com.arialyy.aria.core.download.downloader;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.inf.IDownloadListener;
import com.arialyy.aria.core.inf.IEventListener;
/**
* Created by Aria.Lao on 2017/7/20.
@@ -25,6 +24,11 @@ import com.arialyy.aria.core.inf.IEventListener;
*/
public interface IDownloadGroupListener extends IDownloadListener {
/**
* 子任务预处理
*/
void onSubPre(DownloadEntity subEntity);
/**
* 子任务支持断点回调
*
@@ -56,4 +60,9 @@ public interface IDownloadGroupListener extends IDownloadListener {
* 子任务取消下载
*/
void onSubCancel(DownloadEntity subEntity);
/**
* 子任务执行中
*/
void onSubRunning(DownloadEntity subEntity);
}

View File

@@ -60,6 +60,11 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
*/
private long completeTime;
/**
* 进度百分比
*/
@Ignore private int percent;
private boolean isComplete = false;
public boolean isComplete() {
@@ -142,6 +147,14 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
this.completeTime = completeTime;
}
public int getPercent() {
return percent;
}
public void setPercent(int percent) {
this.percent = percent;
}
/**
* 实体唯一标识符
*/
@@ -165,6 +178,7 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
dest.writeLong(this.currentProgress);
dest.writeLong(this.completeTime);
dest.writeByte(this.isComplete ? (byte) 1 : (byte) 0);
dest.writeInt(this.percent);
}
protected AbsEntity(Parcel in) {
@@ -178,5 +192,6 @@ public abstract class AbsEntity extends DbEntity implements IEntity, Parcelable
this.currentProgress = in.readLong();
this.completeTime = in.readLong();
this.isComplete = in.readByte() != 0;
this.percent = in.readInt();
}
}

View File

@@ -26,10 +26,6 @@ public interface IReceiver<ENTITY extends IEntity> {
*/
void destroy();
/**
* 移除事件回调
*/
void removeSchedulerListener();
/**
* 移除观察者

View File

@@ -15,12 +15,14 @@
*/
package com.arialyy.aria.core.scheduler;
import com.arialyy.aria.core.inf.AbsNormalTask;
import com.arialyy.aria.core.inf.ITask;
/**
* Created by Aria.Lao on 2017/6/7.
*/
public class AbsSchedulerListener<TASK extends ITask> implements ISchedulerListener<TASK> {
public class AbsSchedulerListener<TASK extends ITask, SUB_TASK extends AbsNormalTask> implements ISchedulerListener<TASK> {
@Override public void onPre(TASK task) {
}
@@ -64,4 +66,32 @@ public class AbsSchedulerListener<TASK extends ITask> implements ISchedulerListe
public void setListener(Object obj) {
}
public void onSubTaskPre(TASK task, SUB_TASK subTask) {
}
public void onSubTaskStart(TASK task, SUB_TASK subTask) {
}
public void onSubTaskStop(TASK task, SUB_TASK subTask) {
}
public void onSubTaskCancel(TASK task, SUB_TASK subTask) {
}
public void onSubTaskComplete(TASK task, SUB_TASK subTask) {
}
public void onSubTaskFail(TASK task, SUB_TASK subTask) {
}
public void onSubTaskRunning(TASK task, SUB_TASK subTask) {
}
}

View File

@@ -21,6 +21,7 @@ import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.inf.AbsEntity;
import com.arialyy.aria.core.inf.AbsNormalTask;
import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.inf.IEntity;
@@ -45,9 +46,7 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
protected QUEUE mQueue;
private Map<String, ISchedulerListener<TASK>> mSchedulerListeners = new ConcurrentHashMap<>();
private Map<String, AbsSchedulerListener<TASK>> mObservers = new ConcurrentHashMap<>();
private Map<String, AbsSchedulerListener<TASK, AbsNormalTask>> mObservers = new ConcurrentHashMap<>();
/**
* 设置调度器类型
@@ -59,33 +58,9 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
*/
abstract String getProxySuffix();
/**
* @param targetName 观察者,创建该监听器的对象类名
* @param schedulerListener {@link ISchedulerListener}
* @see #register(Object)
*/
@Deprecated @Override public void addSchedulerListener(String targetName,
ISchedulerListener<TASK> schedulerListener) {
mSchedulerListeners.put(targetName, schedulerListener);
}
/**
* @param targetName 观察者,创建该监听器的对象类名
* @see #unRegister(Object)
*/
@Override public void removeSchedulerListener(String targetName,
ISchedulerListener<TASK> schedulerListener) {
//该内存泄露解决方案http://stackoverflow.com/questions/14585829/how-safe-is-to-delete-already-removed-concurrenthashmap-element
for (Iterator<Map.Entry<String, ISchedulerListener<TASK>>> iter =
mSchedulerListeners.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry<String, ISchedulerListener<TASK>> entry = iter.next();
if (entry.getKey().equals(targetName)) iter.remove();
}
}
@Override public void register(Object obj) {
String targetName = obj.getClass().getName();
AbsSchedulerListener<TASK> listener = mObservers.get(targetName);
AbsSchedulerListener<TASK, AbsNormalTask> listener = mObservers.get(targetName);
if (listener == null) {
listener = createListener(targetName);
if (listener != null) {
@@ -98,9 +73,9 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
}
@Override public void unRegister(Object obj) {
for (Iterator<Map.Entry<String, AbsSchedulerListener<TASK>>> iter =
for (Iterator<Map.Entry<String, AbsSchedulerListener<TASK, AbsNormalTask>>> iter =
mObservers.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry<String, AbsSchedulerListener<TASK>> entry = iter.next();
Map.Entry<String, AbsSchedulerListener<TASK, AbsNormalTask>> entry = iter.next();
if (entry.getKey().equals(obj.getClass().getName())) iter.remove();
}
}
@@ -110,11 +85,11 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
*
* @param targetName 通过观察者创建对应的Aria事件代理
*/
private AbsSchedulerListener<TASK> createListener(String targetName) {
AbsSchedulerListener<TASK> listener = null;
private AbsSchedulerListener<TASK, AbsNormalTask> createListener(String targetName) {
AbsSchedulerListener<TASK, AbsNormalTask> listener = null;
try {
Class clazz = Class.forName(targetName + getProxySuffix());
listener = (AbsSchedulerListener<TASK>) clazz.newInstance();
listener = (AbsSchedulerListener<TASK, AbsNormalTask>) clazz.newInstance();
} catch (ClassNotFoundException e) {
Log.e(TAG, targetName + "没有Aria的Download或Upload注解方法");
} catch (InstantiationException e) {
@@ -131,8 +106,52 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
Log.e(TAG, "请传入下载任务");
return true;
}
if (msg.arg1 == IS_SUB_TASK) {
handleSubEvent(task, msg.what);
} else {
handleNormalEvent(task, msg.what);
}
return true;
}
/**
* 处理任务组子任务事件
*/
private void handleSubEvent(TASK task, int what) {
ENTITY entity = task.getEntity();
switch (msg.what) {
if (mObservers.size() > 0) {
Set<String> keys = mObservers.keySet();
for (String key : keys) {
AbsSchedulerListener<TASK, AbsNormalTask> listener = mObservers.get(key);
switch (what) {
case SUB_PRE:
//listener.onSubTaskPre(task, );
break;
case SUB_START:
break;
case SUB_STOP:
break;
case SUB_FAIL:
break;
case SUB_RUNNING:
break;
case SUB_CANCEL:
break;
case SUB_COMPLETE:
break;
}
}
}
}
/**
* 处理普通任务事件
*/
private void handleNormalEvent(TASK task, int what) {
ENTITY entity = task.getEntity();
switch (what) {
case STOP:
if (task.getEntity().getState() == IEntity.STATE_WAIT) {
break;
@@ -153,8 +172,7 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
handleFailTask(task);
break;
}
callback(msg.what, task);
return true;
callback(what, task);
}
/**
@@ -163,12 +181,7 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
* @param state 状态
*/
private void callback(int state, TASK task) {
if (mSchedulerListeners.size() > 0) {
Set<String> keys = mSchedulerListeners.keySet();
for (String key : keys) {
callback(state, task, mSchedulerListeners.get(key));
}
} else if (mObservers.size() > 0) {
if (mObservers.size() > 0) {
Set<String> keys = mObservers.keySet();
for (String key : keys) {
callback(state, task, mObservers.get(key));
@@ -176,7 +189,7 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
}
}
private void callback(int state, TASK task, ISchedulerListener<TASK> listener) {
private void callback(int state, TASK task, AbsSchedulerListener<TASK, AbsNormalTask> listener) {
if (listener != null) {
if (task == null) {
Log.e(TAG, "TASK 为null回调失败");
@@ -211,9 +224,7 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
listener.onTaskFail(task);
break;
case SUPPORT_BREAK_POINT:
if (listener instanceof IDownloadSchedulerListener) {
((IDownloadSchedulerListener<TASK>) listener).onNoSupportBreakPoint(task);
}
listener.onNoSupportBreakPoint(task);
break;
}
}

View File

@@ -66,4 +66,8 @@ public interface ISchedulerListener<TASK extends ITask> {
* 任务执行中
*/
void onTaskRunning(TASK task);
}

View File

@@ -24,6 +24,11 @@ import com.arialyy.aria.core.inf.AbsTask;
* 调度器功能接口
*/
public interface ISchedulers<Task extends AbsTask> extends Handler.Callback {
/**
* 为任务组任务
*/
int IS_SUB_TASK = 0xd1;
/**
* 断点支持
*/
@@ -67,18 +72,39 @@ public interface ISchedulers<Task extends AbsTask> extends Handler.Callback {
int RESUME = 8;
/**
* 注册下载器监听,一个观察者只能注册一次监听
*
* @param targetName 观察者,创建该监听器的对象类名
* @param schedulerListener {@link ISchedulerListener}
* 任务组子任务预处理
*/
void addSchedulerListener(String targetName, ISchedulerListener<Task> schedulerListener);
int SUB_PRE = 0xa1;
/**
* @param targetName 观察者,创建该监听器的对象类名
* 取消注册监听器
* 任务组子任务开始
*/
void removeSchedulerListener(String targetName, ISchedulerListener<Task> schedulerListener);
int SUB_START = 0xa2;
/**
* 任务组子任务停止
*/
int SUB_STOP = 0xa3;
/**
* 任务组子任务取消
*/
int SUB_CANCEL = 0xa4;
/**
* 任务组子任务失败
*/
int SUB_FAIL = 0xa5;
/**
* 任务组子任务执行执行中
*/
int SUB_RUNNING = 0xa6;
/**
* 任务组子任务完成
*/
int SUB_COMPLETE = 0xa7;
/**
* 将当前类注册到Aria

View File

@@ -106,6 +106,7 @@ class BaseUListener<ENTITY extends AbsEntity, TASK extends AbsTask<ENTITY>>
} else {
mEntity.setSpeed(speed < 0 ? 0 : speed);
}
mEntity.setPercent((int) (mEntity.getCurrentProgress() * 100 / mEntity.getFileSize()));
}
/**

View File

@@ -97,8 +97,6 @@ public class UploadReceiver extends AbsReceiver<UploadEntity> {
Set<String> keys = am.getReceiver().keySet();
for (String key : keys) {
IReceiver receiver = am.getReceiver().get(key);
receiver.removeSchedulerListener();
am.getReceiver().remove(key);
}
}
@@ -108,26 +106,6 @@ public class UploadReceiver extends AbsReceiver<UploadEntity> {
listener = null;
}
/**
* 添加调度器回调
*
* @see #register()
*/
@Deprecated public UploadReceiver addSchedulerListener(ISchedulerListener<UploadTask> listener) {
this.listener = listener;
UploadSchedulers.getInstance().addSchedulerListener(targetName, listener);
return this;
}
/**
* @see #unRegister()
*/
@Deprecated @Override public void removeSchedulerListener() {
if (listener != null) {
UploadSchedulers.getInstance().removeSchedulerListener(targetName, listener);
}
}
/**
* 将当前类注册到Aria
*/