merge v_3.0
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
apply plugin: 'com.android.library'
|
||||
apply plugin: 'bintray-release'
|
||||
|
||||
android {
|
||||
compileSdkVersion 23
|
||||
@ -7,8 +8,8 @@ android {
|
||||
defaultConfig {
|
||||
minSdkVersion 9
|
||||
targetSdkVersion 23
|
||||
versionCode 315
|
||||
versionName "3.1.5"
|
||||
versionCode 317
|
||||
versionName "3.1.7"
|
||||
}
|
||||
buildTypes {
|
||||
release {
|
||||
@ -19,8 +20,57 @@ android {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile fileTree(dir: 'libs', include: ['*.jar'])
|
||||
compile fileTree(include: ['*.jar'], dir: 'libs')
|
||||
testCompile 'junit:junit:4.12'
|
||||
compile 'com.android.support:appcompat-v7:23.1.1'
|
||||
// compile project(':AriaCompiler')
|
||||
compile project(':AriaAnnotations')
|
||||
}
|
||||
apply from: 'jcenter.gradle'
|
||||
//apply from: 'jcenter.gradle'
|
||||
|
||||
// Jar
|
||||
//task androidJar(type: Jar) {
|
||||
// dependsOn assemble
|
||||
// group 'Build'
|
||||
// description 'blah blah'
|
||||
// from zipTree(
|
||||
// 'build/intermediates/bundles/release/classes.jar')
|
||||
// from zipTree(
|
||||
// '../AriaCompiler/build/libs/AriaCompiler.jar')
|
||||
// from zipTree(
|
||||
// '../AriaAnnotation/build/libs/AriaAnnotation.jar')
|
||||
//
|
||||
//}
|
||||
//
|
||||
//
|
||||
//// javadoc tasks
|
||||
//android.libraryVariants.all { variant ->
|
||||
// task("javadoc${variant.name.capitalize()}", type: Javadoc) {
|
||||
// description "Generates Javadoc for $variant.name."
|
||||
// group 'Docs'
|
||||
// source = variant.javaCompile.source
|
||||
// source "../AriaAnnotation/src/main/java"
|
||||
//
|
||||
// exclude '**/BuildConfig.java'
|
||||
// exclude '**/R.java'
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
publish {
|
||||
artifactId = 'aria-core'
|
||||
userOrg = rootProject.userOrg
|
||||
groupId = rootProject.groupId
|
||||
// uploadName = rootProject.uploadName
|
||||
uploadName = 'AriaApi'
|
||||
publishVersion = rootProject.publishVersion
|
||||
description = rootProject.description
|
||||
website = rootProject.website
|
||||
licences = rootProject.licences
|
||||
}
|
||||
|
||||
//task copyJar(type: Copy) {
|
||||
// from('build/libs/permission-lib.jar')
|
||||
// into('../output/')
|
||||
// rename ('permission-lib.jar', 'mpermissions.jar')
|
||||
//}
|
||||
|
@ -26,6 +26,8 @@ import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.widget.PopupWindow;
|
||||
import com.arialyy.annotations.Download;
|
||||
import com.arialyy.annotations.Upload;
|
||||
import com.arialyy.aria.core.download.DownloadReceiver;
|
||||
import com.arialyy.aria.core.scheduler.IDownloadSchedulerListener;
|
||||
import com.arialyy.aria.core.scheduler.ISchedulerListener;
|
||||
@ -171,8 +173,12 @@ import com.arialyy.aria.core.upload.UploadTask;
|
||||
|
||||
/**
|
||||
* 上传任务状态监听
|
||||
*
|
||||
* @see Upload
|
||||
* @deprecated 请使用注解函数的方式来实现事件的获取
|
||||
*/
|
||||
public static class UploadSchedulerListener implements ISchedulerListener<UploadTask> {
|
||||
@Deprecated public static class UploadSchedulerListener
|
||||
implements ISchedulerListener<UploadTask> {
|
||||
|
||||
/**
|
||||
* 预处理,有时有些地址链接比较慢,这时可以先在这个地方出来一些界面上的UI,如按钮的状态。
|
||||
@ -218,8 +224,11 @@ import com.arialyy.aria.core.upload.UploadTask;
|
||||
|
||||
/**
|
||||
* 下载任务状态监听
|
||||
*
|
||||
* @see Download
|
||||
* @deprecated 请使用注解函数的方式来实现事件的获取
|
||||
*/
|
||||
public static class DownloadSchedulerListener
|
||||
@Deprecated public static class DownloadSchedulerListener
|
||||
implements IDownloadSchedulerListener<DownloadTask> {
|
||||
/**
|
||||
* 预处理,有时有些地址链接比较慢,这时可以先在这个地方出来一些界面上的UI,如按钮的状态。
|
||||
|
@ -176,11 +176,13 @@ import org.xml.sax.SAXException;
|
||||
if (isDownload) {
|
||||
DownloadReceiver dReceiver = new DownloadReceiver();
|
||||
dReceiver.targetName = obj.getClass().getName();
|
||||
dReceiver.obj = obj;
|
||||
mReceivers.put(key, dReceiver);
|
||||
receiver = dReceiver;
|
||||
} else {
|
||||
UploadReceiver uReceiver = new UploadReceiver();
|
||||
uReceiver.targetName = obj.getClass().getName();
|
||||
uReceiver.obj = obj;
|
||||
mReceivers.put(key, uReceiver);
|
||||
receiver = uReceiver;
|
||||
}
|
||||
@ -303,6 +305,7 @@ import org.xml.sax.SAXException;
|
||||
if (key.contains(clsName)) {
|
||||
IReceiver receiver = mReceivers.get(key);
|
||||
receiver.removeSchedulerListener();
|
||||
receiver.unRegister();
|
||||
receiver.destroy();
|
||||
iter.remove();
|
||||
break;
|
||||
|
@ -83,10 +83,23 @@ class ConfigHelper extends DefaultHandler {
|
||||
case "convertSpeed":
|
||||
loadConvertSpeed(value);
|
||||
break;
|
||||
case "maxSpeed":
|
||||
loadMaxSpeed(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void loadMaxSpeed(String value) {
|
||||
double maxSpeed = 0.0;
|
||||
if (!TextUtils.isEmpty(value)) {
|
||||
maxSpeed = Double.parseDouble(value);
|
||||
}
|
||||
if (isDownloadConfig){
|
||||
mDownloadConfig.msxSpeed = maxSpeed;
|
||||
}
|
||||
}
|
||||
|
||||
private void loadConvertSpeed(String value) {
|
||||
boolean open = Boolean.parseBoolean(value);
|
||||
if (isDownloadConfig) {
|
||||
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.arialyy.aria.core;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import com.arialyy.aria.core.queue.DownloadTaskQueue;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import java.io.File;
|
||||
@ -171,6 +172,9 @@ class Configuration {
|
||||
} else if (type == float.class || type == Float.class) {
|
||||
field.setFloat(this, Float.parseFloat(value));
|
||||
} else if (type == double.class || type == Double.class) {
|
||||
if (TextUtils.isEmpty(value)){
|
||||
value = "0.0";
|
||||
}
|
||||
field.setDouble(this, Double.parseDouble(value));
|
||||
} else if (type == long.class || type == Long.class) {
|
||||
field.setLong(this, Long.parseLong(value));
|
||||
@ -235,10 +239,26 @@ class Configuration {
|
||||
*/
|
||||
int threadNum = 3;
|
||||
|
||||
/**
|
||||
* 设置最大下载速度,单位:kb, 为0表示不限速
|
||||
*/
|
||||
double msxSpeed = 0.0;
|
||||
|
||||
public int getIOTimeOut() {
|
||||
return iOTimeOut;
|
||||
}
|
||||
|
||||
public double getMsxSpeed() {
|
||||
return msxSpeed;
|
||||
}
|
||||
|
||||
public DownloadConfig setMsxSpeed(double msxSpeed) {
|
||||
this.msxSpeed = msxSpeed;
|
||||
saveKey("msxSpeed", String.valueOf(msxSpeed));
|
||||
DownloadTaskQueue.getInstance().setMaxSpeed(msxSpeed);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DownloadConfig setIOTimeOut(int iOTimeOut) {
|
||||
this.iOTimeOut = iOTimeOut;
|
||||
saveKey("iOTimeOut", iOTimeOut + "");
|
||||
|
@ -71,9 +71,29 @@ class ConnectionHelp {
|
||||
}
|
||||
conn.setRequestProperty("Charset", "UTF-8");
|
||||
conn.setRequestProperty("User-Agent",
|
||||
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
|
||||
conn.setRequestProperty("Accept",
|
||||
"image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
|
||||
StringBuilder accept = new StringBuilder();
|
||||
accept.append("image/gif, ")
|
||||
.append("image/jpeg, ")
|
||||
.append("image/pjpeg, ")
|
||||
.append("image/webp, ")
|
||||
.append("image/*, ")
|
||||
.append("application/xml")
|
||||
.append("application/xaml+xml, ")
|
||||
.append("application/xhtml+xml, ")
|
||||
.append("application/x-shockwave-flash, ")
|
||||
.append("application/x-ms-xbap, ")
|
||||
.append("application/x-ms-application, ")
|
||||
.append("application/msword, ")
|
||||
.append("application/vnd.ms-excel, ")
|
||||
.append("application/vnd.ms-xpsdocument, ")
|
||||
.append("application/vnd.ms-powerpoint, ")
|
||||
.append("text/plain, ")
|
||||
.append("text/html, ")
|
||||
.append("*/*");
|
||||
conn.setRequestProperty("Accept", accept.toString());
|
||||
conn.setRequestProperty("Accept-Encoding", "identity");
|
||||
conn.setRequestProperty("Accept-Charset", "UTF-8");
|
||||
conn.setRequestProperty("Connection", "Keep-Alive");
|
||||
//302获取重定向地址
|
||||
conn.setInstanceFollowRedirects(false);
|
||||
|
@ -36,8 +36,19 @@ import java.util.Set;
|
||||
public class DownloadReceiver implements IReceiver<DownloadEntity> {
|
||||
private static final String TAG = "DownloadReceiver";
|
||||
public String targetName;
|
||||
public Object obj;
|
||||
public ISchedulerListener<DownloadTask> listener;
|
||||
|
||||
/**
|
||||
* 设置最大下载速度,单位:kb
|
||||
* 该方法为实验性功能,清不要轻易在生产环境中使用。
|
||||
*
|
||||
* @param maxSpeed 为0表示不限速
|
||||
*/
|
||||
@Deprecated public void setMaxSpeed(double maxSpeed) {
|
||||
AriaManager.getInstance(AriaManager.APP).getDownloadConfig().setMsxSpeed(maxSpeed);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link #load(String)},请使用该方法
|
||||
*/
|
||||
@ -61,17 +72,37 @@ public class DownloadReceiver implements IReceiver<DownloadEntity> {
|
||||
|
||||
/**
|
||||
* 添加调度器回调
|
||||
*
|
||||
* @see #register()
|
||||
*/
|
||||
public DownloadReceiver addSchedulerListener(ISchedulerListener<DownloadTask> listener) {
|
||||
@Deprecated public DownloadReceiver addSchedulerListener(
|
||||
ISchedulerListener<DownloadTask> listener) {
|
||||
this.listener = listener;
|
||||
DownloadSchedulers.getInstance().addSchedulerListener(targetName, listener);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除回调
|
||||
* 将当前类注册到Aria
|
||||
*/
|
||||
@Override public void removeSchedulerListener() {
|
||||
public DownloadReceiver register() {
|
||||
DownloadSchedulers.getInstance().register(obj);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消注册
|
||||
*/
|
||||
@Override public void unRegister() {
|
||||
DownloadSchedulers.getInstance().unRegister(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除回调
|
||||
*
|
||||
* @see #unRegister()
|
||||
*/
|
||||
@Deprecated @Override public void removeSchedulerListener() {
|
||||
if (listener != null) {
|
||||
DownloadSchedulers.getInstance().removeSchedulerListener(targetName, listener);
|
||||
}
|
||||
@ -133,6 +164,7 @@ public class DownloadReceiver implements IReceiver<DownloadEntity> {
|
||||
for (String key : keys) {
|
||||
IReceiver receiver = ariaManager.getReceiver().get(key);
|
||||
receiver.removeSchedulerListener();
|
||||
receiver.unRegister();
|
||||
ariaManager.getReceiver().remove(key);
|
||||
}
|
||||
}
|
||||
|
@ -28,6 +28,7 @@ import com.arialyy.aria.core.scheduler.DownloadSchedulers;
|
||||
import com.arialyy.aria.core.scheduler.ISchedulers;
|
||||
import com.arialyy.aria.util.CheckUtil;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import java.io.File;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
/**
|
||||
@ -50,6 +51,19 @@ public class DownloadTask extends AbsTask<DownloadTaskEntity, DownloadEntity> {
|
||||
mUtil = new DownloadUtil(mContext, taskEntity, mListener);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件保存路径
|
||||
*
|
||||
* @return 如果路径不存在,返回null
|
||||
*/
|
||||
public String getDownloadPath() {
|
||||
File file = new File(mEntity.getDownloadPath());
|
||||
if (!file.exists()) {
|
||||
return null;
|
||||
}
|
||||
return mEntity.getDownloadPath();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前下载任务的下载地址
|
||||
*
|
||||
@ -88,6 +102,15 @@ public class DownloadTask extends AbsTask<DownloadTaskEntity, DownloadEntity> {
|
||||
stop(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置最大下载速度,单位:kb
|
||||
*
|
||||
* @param maxSpeed 为0表示不限速
|
||||
*/
|
||||
public void setMaxSpeed(double maxSpeed) {
|
||||
mUtil.setMaxSpeed(maxSpeed);
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始下载
|
||||
*/
|
||||
|
@ -107,6 +107,15 @@ class DownloadUtil implements IDownloadUtil, Runnable {
|
||||
return CONSTANCE.isDownloading;
|
||||
}
|
||||
|
||||
public void setMaxSpeed(double maxSpeed) {
|
||||
for (int i = 0; i < THREAD_NUM; i++) {
|
||||
SingleThreadTask task = (SingleThreadTask) mTask.get(i);
|
||||
if (task != null) {
|
||||
task.setMaxSpeed(maxSpeed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消下载
|
||||
*/
|
||||
@ -323,7 +332,7 @@ class DownloadUtil implements IDownloadUtil, Runnable {
|
||||
rl++;
|
||||
}
|
||||
if (i == (THREAD_NUM - 1)) {
|
||||
//如果整个文件的大小不为线程个数的整数倍,则最后一个线程的结束位置即为文件的总长度
|
||||
//最后一个线程的结束位置即为文件的总长度
|
||||
endL = fileLength;
|
||||
}
|
||||
addSingleTask(i, startL, endL, fileLength);
|
||||
|
@ -63,4 +63,9 @@ interface IDownloadUtil {
|
||||
* 删除temp文件
|
||||
*/
|
||||
public void delTempFile();
|
||||
|
||||
/**
|
||||
* 设置最大下载速度
|
||||
*/
|
||||
public void setMaxSpeed(double maxSpeed);
|
||||
}
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.arialyy.aria.core.download;
|
||||
|
||||
import android.os.Build;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.core.AriaManager;
|
||||
@ -23,10 +24,13 @@ import com.arialyy.aria.util.CommonUtil;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.math.BigDecimal;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.HashSet;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2017/1/18.
|
||||
@ -41,6 +45,11 @@ final class SingleThreadTask implements Runnable {
|
||||
private IDownloadListener mListener;
|
||||
private DownloadStateConstance CONSTANCE;
|
||||
|
||||
/**
|
||||
* speed = (bufSize / 1024) * CoresNum / sleepTime; (8192 / 1024) * 4 / 1= 32 kb/s
|
||||
*/
|
||||
private long mSleepTime = 0;
|
||||
|
||||
SingleThreadTask(DownloadStateConstance constance, IDownloadListener listener,
|
||||
DownloadUtil.ConfigEntity downloadInfo) {
|
||||
AriaManager manager = AriaManager.getInstance(AriaManager.APP);
|
||||
@ -53,6 +62,22 @@ final class SingleThreadTask implements Runnable {
|
||||
mConfigFPath = downloadInfo.CONFIG_FILE_PATH;
|
||||
}
|
||||
mBufSize = manager.getDownloadConfig().getBuffSize();
|
||||
setMaxSpeed(AriaManager.getInstance(AriaManager.APP).getDownloadConfig().getMsxSpeed());
|
||||
}
|
||||
|
||||
void setMaxSpeed(double maxSpeed) {
|
||||
if (-0.9999 < maxSpeed && maxSpeed < 0.00001) {
|
||||
mSleepTime = 0;
|
||||
} else {
|
||||
BigDecimal db = new BigDecimal(
|
||||
((mBufSize / 1024) * (filterVersion() ? 1 : CONSTANCE.THREAD_NUM) / maxSpeed) * 1000);
|
||||
db.setScale(0, BigDecimal.ROUND_HALF_UP);
|
||||
mSleepTime = db.longValue();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean filterVersion() {
|
||||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
|
||||
}
|
||||
|
||||
@Override public void run() {
|
||||
@ -73,7 +98,7 @@ final class SingleThreadTask implements Runnable {
|
||||
+ "】");
|
||||
//在头里面请求下载开始位置和结束位置
|
||||
conn.setRequestProperty("Range",
|
||||
"bytes=" + mConfigEntity.START_LOCATION + "-" + mConfigEntity.END_LOCATION);
|
||||
"bytes=" + mConfigEntity.START_LOCATION + "-" + (mConfigEntity.END_LOCATION - 1));
|
||||
} else {
|
||||
Log.w(TAG, "该下载不支持断点");
|
||||
}
|
||||
@ -97,7 +122,7 @@ final class SingleThreadTask implements Runnable {
|
||||
if (CONSTANCE.isStop) {
|
||||
break;
|
||||
}
|
||||
//把下载数据数据写入文件
|
||||
Thread.sleep(mSleepTime);
|
||||
file.write(buffer, 0, len);
|
||||
progress(len);
|
||||
}
|
||||
|
@ -38,6 +38,16 @@ public abstract class AbsTarget<ENTITY extends AbsEntity, TASK_ENTITY extends Ab
|
||||
protected TASK_ENTITY taskEntity;
|
||||
protected String targetName;
|
||||
|
||||
/**
|
||||
* 设置扩展字段,用来保存你的其它数据,如果你的数据比较多,你可以把你的数据转换为JSON字符串,然后再存到Aria中
|
||||
*
|
||||
* @param str 扩展数据
|
||||
*/
|
||||
public AbsTarget setExtendField(String str) {
|
||||
entity.setStr(str);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取任务状态
|
||||
*
|
||||
|
@ -31,6 +31,11 @@ public interface IReceiver<ENTITY extends IEntity> {
|
||||
*/
|
||||
public void removeSchedulerListener();
|
||||
|
||||
/**
|
||||
* 移除观察者
|
||||
*/
|
||||
public void unRegister();
|
||||
|
||||
/**
|
||||
* 停止所有任务
|
||||
*/
|
||||
|
@ -91,6 +91,15 @@ public class DownloadTaskQueue
|
||||
}
|
||||
}
|
||||
|
||||
public void setMaxSpeed(double maxSpeed){
|
||||
Map<String, DownloadTask> tasks = mExecutePool.getAllTask();
|
||||
Set<String> keys = tasks.keySet();
|
||||
for (String key : keys){
|
||||
DownloadTask task = tasks.get(key);
|
||||
task.setMaxSpeed(maxSpeed);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void setMaxTaskNum(int downloadNum) {
|
||||
int oldMaxSize = AriaManager.getInstance(AriaManager.APP).getDownloadConfig().oldMaxTaskNum;
|
||||
int diff = downloadNum - oldMaxSize;
|
||||
|
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.arialyy.aria.core.scheduler;
|
||||
|
||||
import com.arialyy.aria.core.inf.ITask;
|
||||
|
||||
/**
|
||||
* Created by Aria.Lao on 2017/6/7.
|
||||
*/
|
||||
public class AbsSchedulerListener<TASK extends ITask> implements ISchedulerListener<TASK> {
|
||||
@Override public void onPre(TASK task) {
|
||||
|
||||
}
|
||||
|
||||
@Override public void onTaskPre(TASK task) {
|
||||
|
||||
}
|
||||
|
||||
@Override public void onTaskResume(TASK task) {
|
||||
|
||||
}
|
||||
|
||||
@Override public void onTaskStart(TASK task) {
|
||||
|
||||
}
|
||||
|
||||
@Override public void onTaskStop(TASK task) {
|
||||
|
||||
}
|
||||
|
||||
@Override public void onTaskCancel(TASK task) {
|
||||
|
||||
}
|
||||
|
||||
@Override public void onTaskFail(TASK task) {
|
||||
|
||||
}
|
||||
|
||||
@Override public void onTaskComplete(TASK task) {
|
||||
|
||||
}
|
||||
|
||||
@Override public void onTaskRunning(TASK task) {
|
||||
|
||||
}
|
||||
|
||||
public void onNoSupportBreakPoint(TASK task) {
|
||||
|
||||
}
|
||||
|
||||
public void setListener(Object obj) {
|
||||
|
||||
}
|
||||
}
|
@ -32,21 +32,43 @@ import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Created by AriaL on 2017/6/4.
|
||||
* Created by lyy on 2017/6/4.
|
||||
*/
|
||||
public abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends AbsEntity, TASK extends ITask<ENTITY>, QUEUE extends ITaskQueue<TASK, TASK_ENTITY, ENTITY>>
|
||||
implements ISchedulers<TASK> {
|
||||
private static final String TAG = "AbsSchedulers";
|
||||
|
||||
/**
|
||||
* 下载的动态生成的代理类后缀
|
||||
*/
|
||||
String DOWNLOAD_PROXY_CLASS_SUFFIX = "$$DownloadListenerProxy";
|
||||
|
||||
/**
|
||||
* 上传的动态生成的代理类后缀
|
||||
*/
|
||||
String UPLOAD_PROXY_CLASS_SUFFIX = "$$UploadListenerProxy";
|
||||
|
||||
protected QUEUE mQueue;
|
||||
protected boolean isDownload = true;
|
||||
|
||||
private Map<String, ISchedulerListener<TASK>> mSchedulerListeners = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public void addSchedulerListener(String targetName, ISchedulerListener<TASK> schedulerListener) {
|
||||
private Map<String, AbsSchedulerListener<TASK>> mObservers = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* @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
|
||||
@ -57,6 +79,52 @@ public abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY ex
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void register(Object obj) {
|
||||
String targetName = obj.getClass().getName();
|
||||
AbsSchedulerListener<TASK> listener = mObservers.get(targetName);
|
||||
if (listener == null) {
|
||||
listener = createListener(targetName);
|
||||
if (listener != null) {
|
||||
listener.setListener(obj);
|
||||
mObservers.put(targetName, listener);
|
||||
} else {
|
||||
Log.e(TAG, "注册错误,没有【" + targetName + "】观察者");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void unRegister(Object obj) {
|
||||
for (Iterator<Map.Entry<String, AbsSchedulerListener<TASK>>> iter =
|
||||
mObservers.entrySet().iterator(); iter.hasNext(); ) {
|
||||
Map.Entry<String, AbsSchedulerListener<TASK>> entry = iter.next();
|
||||
if (entry.getKey().equals(obj.getClass().getName())) iter.remove();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建代理类
|
||||
*
|
||||
* @param targetName 通过观察者创建对应的Aria事件代理
|
||||
*/
|
||||
private AbsSchedulerListener<TASK> createListener(String targetName) {
|
||||
AbsSchedulerListener<TASK> listener = null;
|
||||
try {
|
||||
Class clazz = Class.forName(
|
||||
targetName + (isDownload ? DOWNLOAD_PROXY_CLASS_SUFFIX : UPLOAD_PROXY_CLASS_SUFFIX));
|
||||
listener = (AbsSchedulerListener<TASK>) clazz.newInstance();
|
||||
} catch (ClassNotFoundException e) {
|
||||
Log.e(TAG, targetName + ",没有Aria的Download或Upload注解方法");
|
||||
//e.printStackTrace();
|
||||
} catch (InstantiationException e) {
|
||||
Log.e(TAG, e.getMessage());
|
||||
//e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
Log.e(TAG, e.getMessage());
|
||||
//e.printStackTrace();
|
||||
}
|
||||
return listener;
|
||||
}
|
||||
|
||||
@Override public boolean handleMessage(Message msg) {
|
||||
TASK task = (TASK) msg.obj;
|
||||
if (task == null) {
|
||||
@ -100,6 +168,11 @@ public abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY ex
|
||||
for (String key : keys) {
|
||||
callback(state, task, mSchedulerListeners.get(key));
|
||||
}
|
||||
} else if (mObservers.size() > 0) {
|
||||
Set<String> keys = mObservers.keySet();
|
||||
for (String key : keys) {
|
||||
callback(state, task, mObservers.get(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -34,6 +34,7 @@ public class DownloadSchedulers
|
||||
|
||||
private DownloadSchedulers() {
|
||||
mQueue = DownloadTaskQueue.getInstance();
|
||||
isDownload = true;
|
||||
}
|
||||
|
||||
public static DownloadSchedulers getInstance() {
|
||||
|
@ -80,4 +80,18 @@ public interface ISchedulers<Task extends ITask> extends Handler.Callback {
|
||||
*/
|
||||
public void removeSchedulerListener(String targetName,
|
||||
ISchedulerListener<Task> schedulerListener);
|
||||
|
||||
/**
|
||||
* 将当前类注册到Aria
|
||||
*
|
||||
* @param obj 观察者类
|
||||
*/
|
||||
public void register(Object obj);
|
||||
|
||||
/**
|
||||
* 移除注册
|
||||
*
|
||||
* @param obj 观察者类
|
||||
*/
|
||||
public void unRegister(Object obj);
|
||||
}
|
@ -32,6 +32,7 @@ public class UploadSchedulers
|
||||
|
||||
private UploadSchedulers() {
|
||||
mQueue = UploadTaskQueue.getInstance();
|
||||
isDownload = false;
|
||||
}
|
||||
|
||||
public static UploadSchedulers getInstance() {
|
||||
|
@ -19,8 +19,10 @@ import android.support.annotation.NonNull;
|
||||
import com.arialyy.aria.core.AriaManager;
|
||||
import com.arialyy.aria.core.command.AbsCmd;
|
||||
import com.arialyy.aria.core.command.CmdFactory;
|
||||
import com.arialyy.aria.core.download.DownloadReceiver;
|
||||
import com.arialyy.aria.core.inf.IEntity;
|
||||
import com.arialyy.aria.core.inf.IReceiver;
|
||||
import com.arialyy.aria.core.scheduler.DownloadSchedulers;
|
||||
import com.arialyy.aria.core.scheduler.ISchedulerListener;
|
||||
import com.arialyy.aria.core.scheduler.UploadSchedulers;
|
||||
import com.arialyy.aria.orm.DbEntity;
|
||||
@ -38,6 +40,7 @@ import java.util.regex.Pattern;
|
||||
public class UploadReceiver implements IReceiver<UploadEntity> {
|
||||
private static final String TAG = "DownloadReceiver";
|
||||
public String targetName;
|
||||
public Object obj;
|
||||
public ISchedulerListener<UploadTask> listener;
|
||||
|
||||
/**
|
||||
@ -127,4 +130,16 @@ public class UploadReceiver implements IReceiver<UploadEntity> {
|
||||
UploadSchedulers.getInstance().removeSchedulerListener(targetName, listener);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将当前类注册到Aria
|
||||
*/
|
||||
public UploadReceiver register() {
|
||||
UploadSchedulers.getInstance().register(obj);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override public void unRegister() {
|
||||
UploadSchedulers.getInstance().unRegister(obj);
|
||||
}
|
||||
}
|
@ -30,7 +30,7 @@ public class UploadTaskEntity extends AbsTaskEntity {
|
||||
public String contentType = "multipart/form-data"; //上传的文件类型
|
||||
public String userAgent = "User-Agent";
|
||||
public String charset = "utf-8";
|
||||
|
||||
|
||||
/**
|
||||
* 文件上传表单
|
||||
*/
|
||||
|
@ -26,6 +26,7 @@ import com.arialyy.aria.core.command.CmdFactory;
|
||||
import com.arialyy.aria.core.command.AbsCmd;
|
||||
import com.arialyy.aria.core.inf.AbsTaskEntity;
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
@ -40,6 +41,7 @@ import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2016/1/22.
|
||||
@ -47,6 +49,35 @@ import java.util.Properties;
|
||||
public class CommonUtil {
|
||||
private static final String TAG = "CommonUtil";
|
||||
|
||||
/**
|
||||
* 获取CPU核心数
|
||||
*/
|
||||
public static int getNumCores() {
|
||||
//Private Class to display only CPU devices in the directory listing
|
||||
class CpuFilter implements FileFilter {
|
||||
@Override public boolean accept(File pathname) {
|
||||
//Check if filename is "cpu", followed by a single digit number
|
||||
return Pattern.matches("cpu[0-9]", pathname.getName());
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
//Get directory containing CPU info
|
||||
File dir = new File("/sys/devices/system/cpu/");
|
||||
//Filter to only list the devices we care about
|
||||
File[] files = dir.listFiles(new CpuFilter());
|
||||
Log.d(TAG, "CPU Count: " + files.length);
|
||||
//Return the number of cores (virtual CPU devices)
|
||||
return files.length;
|
||||
} catch (Exception e) {
|
||||
//Print exception
|
||||
Log.d(TAG, "CPU Count: Failed.");
|
||||
e.printStackTrace();
|
||||
//Default to return 1 core
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过流创建文件
|
||||
*/
|
||||
|
Reference in New Issue
Block a user