From d1b48b2f64a5b3755bc5a54600b05a5b1ad4ff24 Mon Sep 17 00:00:00 2001 From: lyy <511455842@qq.com> Date: Wed, 2 Nov 2016 17:41:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- downloadutil/build.gradle | 32 +-- downloadutil/jcenter.gradle | 224 +++++++++--------- .../downloadutil/core/DownLoadUtil.java | 54 ++--- .../downloadutil/core/DownloadEntity.java | 56 +++-- .../downloadutil/core/DownloadManager.java | 22 +- .../downloadutil/core/DownloadSchedulers.java | 75 +++--- .../downloadutil/core/DownloadTaskQueue.java | 16 +- .../com/arialyy/downloadutil/core/Task.java | 96 ++++---- .../downloadutil/core/pool/CachePool.java | 12 +- .../downloadutil/core/pool/ExecutePool.java | 12 +- .../arialyy/downloadutil/help/Lockhelp.java | 2 - .../arialyy/downloadutil/orm/DbEntity.java | 69 +++--- .../com/arialyy/downloadutil/orm/DbUtil.java | 12 +- .../com/arialyy/downloadutil/util/Util.java | 1 - 14 files changed, 332 insertions(+), 351 deletions(-) diff --git a/downloadutil/build.gradle b/downloadutil/build.gradle index 6648a5ae..f6fe271a 100644 --- a/downloadutil/build.gradle +++ b/downloadutil/build.gradle @@ -1,26 +1,26 @@ apply plugin: 'com.android.library' android { - compileSdkVersion 23 - buildToolsVersion "23.0.2" + compileSdkVersion 23 + buildToolsVersion "23.0.2" - defaultConfig { - minSdkVersion 9 - targetSdkVersion 23 - versionCode 58 - versionName "2.1.0" - } - buildTypes { - release { - minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' - } + defaultConfig { + minSdkVersion 9 + targetSdkVersion 23 + versionCode 58 + versionName "2.1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } + } } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:23.1.1' + compile fileTree(dir: 'libs', include: ['*.jar']) + testCompile 'junit:junit:4.12' + compile 'com.android.support:appcompat-v7:23.1.1' } apply from: 'jcenter.gradle' \ No newline at end of file diff --git a/downloadutil/jcenter.gradle b/downloadutil/jcenter.gradle index 2b186301..3d3197d1 100644 --- a/downloadutil/jcenter.gradle +++ b/downloadutil/jcenter.gradle @@ -6,30 +6,30 @@ apply plugin: "com.jfrog.artifactory" apply plugin: 'maven-publish' task sourcesJar(type: Jar) { - from android.sourceSets.main.java.srcDirs - classifier = 'sources' + from android.sourceSets.main.java.srcDirs + classifier = 'sources' } task javadoc(type: Javadoc) { - source = android.sourceSets.main.java.srcDirs - classpath += configurations.compile - classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) + source = android.sourceSets.main.java.srcDirs + classpath += configurations.compile + classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) } task javadocJar(type: Jar, dependsOn: javadoc) { - classifier = 'javadoc' - from javadoc.destinationDir + classifier = 'javadoc' + from javadoc.destinationDir } javadoc { - options { - encoding "UTF-8" - charSet 'UTF-8' - author true - version true - links "http://docs.oracle.com/javase/7/docs/api" - title PROJ_ARTIFACTID - } + options { + encoding "UTF-8" + charSet 'UTF-8' + author true + version true + links "http://docs.oracle.com/javase/7/docs/api" + title PROJ_ARTIFACTID + } } //添加以下信息避免JAVADOC打包时引用其它类库而出现问题,比如出现以下错误 @@ -39,128 +39,130 @@ javadoc { // 符号: 类 JsonElement // 位置: 类 xxxx android.libraryVariants.all { variant -> - println variant.javaCompile.classpath.files - if (variant.name == 'release') { //我们只需 release 的 javadoc - task("generate${variant.name.capitalize()}Javadoc", type: Javadoc) { - // title = '' - // description = '' - source = variant.javaCompile.source - classpath = files(variant.javaCompile.classpath.files, project.android.getBootClasspath()) - options { - encoding "utf-8" - links "http://docs.oracle.com/javase/7/docs/api/" - linksOffline "http://d.android.com/reference", "${android.sdkDirectory}/docs/reference" - } - exclude '**/BuildConfig.java' - exclude '**/R.java' - } - task("javadoc${variant.name.capitalize()}Jar", type: Jar, dependsOn: "generate${variant.name.capitalize()}Javadoc") { - classifier = 'javadoc' - from tasks.getByName("generate${variant.name.capitalize()}Javadoc").destinationDir - } - artifacts { - archives tasks.getByName("javadoc${variant.name.capitalize()}Jar") - } + println variant.javaCompile.classpath.files + if (variant.name == 'release') { + //我们只需 release 的 javadoc + task("generate${variant.name.capitalize()}Javadoc", type: Javadoc) { + // title = '' + // description = '' + source = variant.javaCompile.source + classpath = files(variant.javaCompile.classpath.files, project.android.getBootClasspath()) + options { + encoding "utf-8" + links "http://docs.oracle.com/javase/7/docs/api/" + linksOffline "http://d.android.com/reference", "${android.sdkDirectory}/docs/reference" + } + exclude '**/BuildConfig.java' + exclude '**/R.java' + } + task("javadoc${variant.name.capitalize()}Jar", type: Jar, + dependsOn: "generate${variant.name.capitalize()}Javadoc") { + classifier = 'javadoc' + from tasks.getByName("generate${variant.name.capitalize()}Javadoc").destinationDir } + artifacts { + archives tasks.getByName("javadoc${variant.name.capitalize()}Jar") + } + } } artifacts { - archives javadocJar - archives sourcesJar + archives javadocJar + archives sourcesJar } def pomConfig = { - scm { - connection PROJ_VCSURL - developerConnection PROJ_VCSURL - url PROJ_WEBSITEURL - } - licenses { - license { - name "The Apache Software License, Version 2.0" - url "http://www.apache.org/licenses/LICENSE-2.0.txt" - distribution "repo" - } + scm { + connection PROJ_VCSURL + developerConnection PROJ_VCSURL + url PROJ_WEBSITEURL + } + licenses { + license { + name "The Apache Software License, Version 2.0" + url "http://www.apache.org/licenses/LICENSE-2.0.txt" + distribution "repo" } - developers { - developer { - id DEVELOPER_ID - name DEVELOPER_NAME - email DEVELOPER_EMAIL - } - } - "dependencies " { -// dependency { -// groupId "com.alibaba" -// artifactId "fastjson" -// "version " "1.2.6" -// //同dependencies -// } -// } + } + developers { + developer { + id DEVELOPER_ID + name DEVELOPER_NAME + email DEVELOPER_EMAIL } + } + "dependencies " { + // dependency { + // groupId "com.alibaba" + // artifactId "fastjson" + // "version " "1.2.6" + // //同dependencies + // } + // } + } } publishing { - publications { - mavenJava(MavenPublication) { - artifactId PROJ_ARTIFACTID - artifact javadocJar - artifact sourcesJar + publications { + mavenJava(MavenPublication) { + artifactId PROJ_ARTIFACTID + artifact javadocJar + artifact sourcesJar - pom { - packaging 'aar' - } - pom.withXml { - def root = asNode() - root.appendNode('description', PROJ_DESCRIPTION) - root.children().last() + pomConfig - } - } + pom { + packaging 'aar' + } + pom.withXml { + def root = asNode() + root.appendNode('description', PROJ_DESCRIPTION) + root.children().last() + pomConfig + } } + } } afterEvaluate { - publishing.publications.mavenJava.artifact(bundleRelease) + publishing.publications.mavenJava.artifact(bundleRelease) } bintray { - user = hasProperty("bintrayUser") ? getProperty("bintrayUser") : getProperty("BINTRAY_USER") - key = hasProperty("bintrayKey") ? getProperty("bintrayKey") : getProperty("BINTRAY_KEY") + user = hasProperty("bintrayUser") ? getProperty("bintrayUser") : getProperty("BINTRAY_USER") + key = hasProperty("bintrayKey") ? getProperty("bintrayKey") : getProperty("BINTRAY_KEY") - configurations = ['archives'] - publications = ['mavenJava'] - publish = true + configurations = ['archives'] + publications = ['mavenJava'] + publish = true - pkg { - repo = 'maven' - name = PROJ_NAME - desc = PROJ_DESCRIPTION - websiteUrl = PROJ_WEBSITEURL - issueTrackerUrl = PROJ_ISSUETRACKERURL - vcsUrl = PROJ_VCSURL - licenses = ['Apache-2.0'] - publicDownloadNumbers = true - } + pkg { + repo = 'maven' + name = PROJ_NAME + desc = PROJ_DESCRIPTION + websiteUrl = PROJ_WEBSITEURL + issueTrackerUrl = PROJ_ISSUETRACKERURL + vcsUrl = PROJ_VCSURL + licenses = ['Apache-2.0'] + publicDownloadNumbers = true + } } artifactory { - contextUrl = 'http://oss.jfrog.org/artifactory' - resolve { - repository { - repoKey = 'libs-release' - } + contextUrl = 'http://oss.jfrog.org/artifactory' + resolve { + repository { + repoKey = 'libs-release' + } + } + publish { + repository { + repoKey = 'oss-snapshot-local' //The Artifactory repository key to publish to + username = bintray.user + password = bintray.key + maven = true } - publish { - repository { - repoKey = 'oss-snapshot-local' //The Artifactory repository key to publish to - username = bintray.user - password = bintray.key - maven = true - } - defaults { - publications('mavenJava') - publishArtifacts = true - } + defaults { + publications('mavenJava') + publishArtifacts = true } + } } \ No newline at end of file diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownLoadUtil.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownLoadUtil.java index a921a768..5578f224 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownLoadUtil.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownLoadUtil.java @@ -24,12 +24,12 @@ import java.util.concurrent.Executors; final class DownloadUtil implements IDownloadUtil { private static final String TAG = "DownloadUtil"; private static final Object LOCK = new Object(); - //下载监听 - private IDownloadListener mListener; /** * 线程数 */ private final int THREAD_NUM; + //下载监听 + private IDownloadListener mListener; private int mConnectTimeOut = 5000 * 4; //连接超时时间 private int mReadTimeOut = 5000 * 20; //流读取的超时时间 /** @@ -314,6 +314,31 @@ final class DownloadUtil implements IDownloadUtil { "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, */*"); } + /** + * 子线程下载信息类 + */ + private static class ConfigEntity { + //文件大小 + long fileSize; + String downloadUrl; + int threadId; + long startLocation; + long endLocation; + File tempFile; + Context context; + + private ConfigEntity(Context context, long fileSize, String downloadUrl, File file, + int threadId, long startLocation, long endLocation) { + this.fileSize = fileSize; + this.downloadUrl = downloadUrl; + this.tempFile = file; + this.threadId = threadId; + this.startLocation = startLocation; + this.endLocation = endLocation; + this.context = context; + } + } + /** * 单个线程的下载任务 */ @@ -490,29 +515,4 @@ final class DownloadUtil implements IDownloadUtil { } } } - - /** - * 子线程下载信息类 - */ - private static class ConfigEntity { - //文件大小 - long fileSize; - String downloadUrl; - int threadId; - long startLocation; - long endLocation; - File tempFile; - Context context; - - private ConfigEntity(Context context, long fileSize, String downloadUrl, File file, - int threadId, long startLocation, long endLocation) { - this.fileSize = fileSize; - this.downloadUrl = downloadUrl; - this.tempFile = file; - this.threadId = threadId; - this.startLocation = startLocation; - this.endLocation = endLocation; - this.context = context; - } - } } \ No newline at end of file diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadEntity.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadEntity.java index da8544cb..057007bd 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadEntity.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadEntity.java @@ -48,7 +48,15 @@ public class DownloadEntity extends DbEntity implements Parcelable { * 取消下载 */ @Ignore public static final int STATE_CANCEL = 7; + @Ignore public static final Creator CREATOR = new Creator() { + @Override public DownloadEntity createFromParcel(Parcel source) { + return new DownloadEntity(source); + } + @Override public DownloadEntity[] newArray(int size) { + return new DownloadEntity[size]; + } + }; @Ignore private long speed = 0; //下载速度 private String downloadUrl = ""; //下载路径 private String downloadPath = ""; //保存路径 @@ -64,6 +72,21 @@ public class DownloadEntity extends DbEntity implements Parcelable { public DownloadEntity() { } + protected DownloadEntity(Parcel in) { + this.downloadUrl = in.readString(); + this.downloadPath = in.readString(); + this.fileName = in.readString(); + this.str = in.readString(); + this.completeTime = in.readLong(); + this.fileSize = in.readLong(); + this.state = in.readInt(); + this.isDownloadComplete = in.readByte() != 0; + this.currentProgress = in.readLong(); + this.failNum = in.readInt(); + this.speed = in.readLong(); + this.rowID = in.readInt(); + } + public String getStr() { return str; } @@ -92,6 +115,10 @@ public class DownloadEntity extends DbEntity implements Parcelable { return downloadUrl; } + public void setDownloadUrl(String downloadUrl) { + this.downloadUrl = downloadUrl; + } + public long getCompleteTime() { return completeTime; } @@ -100,10 +127,6 @@ public class DownloadEntity extends DbEntity implements Parcelable { this.completeTime = completeTime; } - public void setDownloadUrl(String downloadUrl) { - this.downloadUrl = downloadUrl; - } - public String getDownloadPath() { return downloadPath; } @@ -187,29 +210,4 @@ public class DownloadEntity extends DbEntity implements Parcelable { dest.writeLong(this.speed); dest.writeInt(this.rowID); } - - protected DownloadEntity(Parcel in) { - this.downloadUrl = in.readString(); - this.downloadPath = in.readString(); - this.fileName = in.readString(); - this.str = in.readString(); - this.completeTime = in.readLong(); - this.fileSize = in.readLong(); - this.state = in.readInt(); - this.isDownloadComplete = in.readByte() != 0; - this.currentProgress = in.readLong(); - this.failNum = in.readInt(); - this.speed = in.readLong(); - this.rowID = in.readInt(); - } - - @Ignore public static final Creator CREATOR = new Creator() { - @Override public DownloadEntity createFromParcel(Parcel source) { - return new DownloadEntity(source); - } - - @Override public DownloadEntity[] newArray(int size) { - return new DownloadEntity[size]; - } - }; } \ No newline at end of file diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadManager.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadManager.java index 55cfacef..74ee9b81 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadManager.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadManager.java @@ -14,78 +14,64 @@ import java.util.List; * 下载管理器,通过命令的方式控制下载 */ public class DownloadManager { - private static final String TAG = "DownloadManager"; - private static final Object LOCK = new Object(); - private static volatile DownloadManager INSTANCE = null; - /** * 预处理完成 */ public static final String ACTION_PRE = "ACTION_PRE"; - /** * 下载开始前事件 */ public static final String ACTION_POST_PRE = "ACTION_POST_PRE"; - /** * 开始下载事件 */ public static final String ACTION_START = "ACTION_START"; - /** * 恢复下载事件 */ public static final String ACTION_RESUME = "ACTION_RESUME"; - /** * 正在下载事件 */ public static final String ACTION_RUNNING = "ACTION_RUNNING"; - /** * 停止下载事件 */ public static final String ACTION_STOP = "ACTION_STOP"; - /** * 取消下载事件 */ public static final String ACTION_CANCEL = "ACTION_CANCEL"; - /** * 下载完成事件 */ public static final String ACTION_COMPLETE = "ACTION_COMPLETE"; - /** * 下载失败事件 */ public static final String ACTION_FAIL = "ACTION_FAIL"; - /** * 下载实体 */ public static final String ENTITY = "DOWNLOAD_ENTITY"; - /** * 位置 */ public static final String CURRENT_LOCATION = "CURRENT_LOCATION"; - /** * 速度 */ public static final String CURRENT_SPEED = "CURRENT_SPEED"; - + private static final String TAG = "DownloadManager"; + private static final Object LOCK = new Object(); + private static volatile DownloadManager INSTANCE = null; private List mCommands = new ArrayList<>(); + private Context mContext; private DownloadManager() { } - private Context mContext; - private DownloadManager(Context context) { mContext = context; DownloadTaskQueue.init(context); diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadSchedulers.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadSchedulers.java index 3956f758..3c1ea3f9 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadSchedulers.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadSchedulers.java @@ -10,9 +10,6 @@ import com.arialyy.downloadutil.core.pool.ExecutePool; * 任务下载器,提供抽象的方法供具体的实现类操作 */ public class DownloadSchedulers implements IDownloadSchedulers { - private static final String TAG = "DownloadSchedulers"; - private static final Object LOCK = new Object(); - private static volatile DownloadSchedulers INSTANCE = null; /** * 任务开始 */ @@ -33,7 +30,9 @@ public class DownloadSchedulers implements IDownloadSchedulers { * 任务完成 */ public static final int COMPLETE = 5; - + private static final String TAG = "DownloadSchedulers"; + private static final Object LOCK = new Object(); + private static volatile DownloadSchedulers INSTANCE = null; /** * 下载失败次数 */ @@ -50,6 +49,10 @@ public class DownloadSchedulers implements IDownloadSchedulers { OnTargetListener mTargetListener; DownloadTaskQueue mQueue; + public DownloadSchedulers(DownloadTaskQueue downloadTaskQueue) { + mQueue = downloadTaskQueue; + } + public static DownloadSchedulers getInstance(DownloadTaskQueue queue) { if (INSTANCE == null) { synchronized (LOCK) { @@ -59,40 +62,6 @@ public class DownloadSchedulers implements IDownloadSchedulers { return INSTANCE; } - /** - * Target处理任务监听 - */ - public interface OnTargetListener { - /** - * 任务开始 - */ - public void onTaskStart(Task task); - - /** - * 任务停止 - */ - public void onTaskStop(Task task); - - /** - * 任务取消 - */ - public void onTaskCancel(Task task); - - /** - * 任务下载失败 - */ - public void onTaskFail(Task task); - - /** - * 任务完成 - */ - public void onTaskComplete(Task task); - } - - public DownloadSchedulers(DownloadTaskQueue downloadTaskQueue) { - mQueue = downloadTaskQueue; - } - @Override public boolean handleMessage(Message msg) { DownloadEntity entity = (DownloadEntity) msg.obj; if (entity == null) { @@ -193,4 +162,34 @@ public class DownloadSchedulers implements IDownloadSchedulers { public void setTimeOut(long timeOut) { this.mTimeOut = timeOut; } + + /** + * Target处理任务监听 + */ + public interface OnTargetListener { + /** + * 任务开始 + */ + public void onTaskStart(Task task); + + /** + * 任务停止 + */ + public void onTaskStop(Task task); + + /** + * 任务取消 + */ + public void onTaskCancel(Task task); + + /** + * 任务下载失败 + */ + public void onTaskFail(Task task); + + /** + * 任务完成 + */ + public void onTaskComplete(Task task); + } } diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadTaskQueue.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadTaskQueue.java index 998d3d79..4daf4235 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadTaskQueue.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadTaskQueue.java @@ -20,6 +20,14 @@ public class DownloadTaskQueue implements ITaskQueue, IDownloader { private ExecutePool mExecutePool = ExecutePool.getInstance(); private Context mContext; + private DownloadTaskQueue() { + } + + private DownloadTaskQueue(Context context) { + super(); + mContext = context; + } + public static DownloadTaskQueue getInstance() { if (INSTANCE == null) { throw new NullPointerException("请在Application中调用init进行注册"); @@ -36,14 +44,6 @@ public class DownloadTaskQueue implements ITaskQueue, IDownloader { return INSTANCE; } - private DownloadTaskQueue() { - } - - private DownloadTaskQueue(Context context) { - super(); - mContext = context; - } - /** * 获取任务执行池 */ diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/Task.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/Task.java index 53eec7aa..654c6093 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/Task.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/Task.java @@ -127,6 +127,54 @@ public class Task { } } + public static class Builder { + DownloadEntity downloadEntity; + Handler outHandler; + Context context; + int threadNum = 3; + IDownloadUtil downloadUtil; + + public Builder(Context context, DownloadEntity downloadEntity) { + this.context = context; + this.downloadEntity = downloadEntity; + } + + /** + * 设置自定义Handler处理下载状态时间 + * + * @param schedulers {@link IDownloadSchedulers} + */ + public Builder setOutHandler(IDownloadSchedulers schedulers) { + this.outHandler = new Handler(schedulers); + return this; + } + + /** + * 设置线程数 + */ + public Builder setThreadNum(int threadNum) { + this.threadNum = threadNum; + return this; + } + + ///** + // * 设置自定义下载工具 + // * + // * @param downloadUtil {@link IDownloadUtil} + // */ + //public Builder setDownloadUtil(IDownloadUtil downloadUtil) { + // this.downloadUtil = downloadUtil; + // return this; + //} + + public Task build() { + Task task = new Task(context, downloadEntity); + task.mOutHandler = outHandler; + downloadEntity.save(); + return task; + } + } + /** * 下载监听类 */ @@ -239,52 +287,4 @@ public class Task { context.sendBroadcast(intent); } } - - public static class Builder { - DownloadEntity downloadEntity; - Handler outHandler; - Context context; - int threadNum = 3; - IDownloadUtil downloadUtil; - - public Builder(Context context, DownloadEntity downloadEntity) { - this.context = context; - this.downloadEntity = downloadEntity; - } - - /** - * 设置自定义Handler处理下载状态时间 - * - * @param schedulers {@link IDownloadSchedulers} - */ - public Builder setOutHandler(IDownloadSchedulers schedulers) { - this.outHandler = new Handler(schedulers); - return this; - } - - /** - * 设置线程数 - */ - public Builder setThreadNum(int threadNum) { - this.threadNum = threadNum; - return this; - } - - ///** - // * 设置自定义下载工具 - // * - // * @param downloadUtil {@link IDownloadUtil} - // */ - //public Builder setDownloadUtil(IDownloadUtil downloadUtil) { - // this.downloadUtil = downloadUtil; - // return this; - //} - - public Task build() { - Task task = new Task(context, downloadEntity); - task.mOutHandler = outHandler; - downloadEntity.save(); - return task; - } - } } diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/pool/CachePool.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/pool/CachePool.java index c754690e..7639bebe 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/pool/CachePool.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/pool/CachePool.java @@ -2,8 +2,8 @@ package com.arialyy.downloadutil.core.pool; import android.text.TextUtils; import android.util.Log; -import com.arialyy.downloadutil.core.inf.IPool; import com.arialyy.downloadutil.core.Task; +import com.arialyy.downloadutil.core.inf.IPool; import com.arialyy.downloadutil.util.Util; import java.util.HashMap; import java.util.Map; @@ -21,6 +21,11 @@ public class CachePool implements IPool { private Map mCacheArray; private LinkedBlockingQueue mCacheQueue; + private CachePool() { + mCacheQueue = new LinkedBlockingQueue<>(MAX_NUM); + mCacheArray = new HashMap<>(); + } + public static CachePool getInstance() { if (INSTANCE == null) { synchronized (LOCK) { @@ -30,11 +35,6 @@ public class CachePool implements IPool { return INSTANCE; } - private CachePool() { - mCacheQueue = new LinkedBlockingQueue<>(MAX_NUM); - mCacheArray = new HashMap<>(); - } - @Override public boolean putTask(Task task) { synchronized (LOCK) { if (task == null) { diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/pool/ExecutePool.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/pool/ExecutePool.java index fc662daa..0ac1a5f0 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/pool/ExecutePool.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/pool/ExecutePool.java @@ -18,11 +18,16 @@ public class ExecutePool implements IPool { private static final String TAG = "ExecutePool"; private static final Object LOCK = new Object(); private static final long TIME_OUT = 1000; - private static volatile ExecutePool INSTANCE = null; public static int SIZE = 2; + private static volatile ExecutePool INSTANCE = null; private ArrayBlockingQueue mExecuteQueue; private Map mExecuteArray; + private ExecutePool() { + mExecuteQueue = new ArrayBlockingQueue<>(SIZE); + mExecuteArray = new HashMap<>(); + } + public static ExecutePool getInstance() { if (INSTANCE == null) { synchronized (LOCK) { @@ -32,11 +37,6 @@ public class ExecutePool implements IPool { return INSTANCE; } - private ExecutePool() { - mExecuteQueue = new ArrayBlockingQueue<>(SIZE); - mExecuteArray = new HashMap<>(); - } - @Override public boolean putTask(Task task) { synchronized (LOCK) { if (task == null) { diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/help/Lockhelp.java b/downloadutil/src/main/java/com/arialyy/downloadutil/help/Lockhelp.java index d04b0fdc..f4eaebb6 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/help/Lockhelp.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/help/Lockhelp.java @@ -5,6 +5,4 @@ package com.arialyy.downloadutil.help; */ public class LockHelp { public static final Object LOCK = new Object(); - - } diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbEntity.java b/downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbEntity.java index 067059c6..6fc7bcd7 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbEntity.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbEntity.java @@ -1,7 +1,6 @@ package com.arialyy.downloadutil.orm; import android.support.annotation.NonNull; -import android.util.Log; import com.arialyy.downloadutil.util.Util; import java.lang.reflect.Field; import java.util.ArrayList; @@ -12,14 +11,47 @@ import java.util.List; * 所有数据库实体父类 */ public class DbEntity { + private static final Object LOCK = new Object(); protected int rowID = -1; private DbUtil mUtil = DbUtil.getInstance(); - private static final Object LOCK = new Object(); protected DbEntity() { } + /** + * 查询所有数据 + * + * @return 没有数据返回null + */ + public static List findAllData(Class clazz) { + DbUtil util = DbUtil.getInstance(); + return util.findAllData(clazz); + } + + /** + * 查询一组数据 + * + * @return 没有数据返回null + */ + public static List findDatas(Class clazz, @NonNull String[] wheres, + @NonNull String[] values) { + DbUtil util = DbUtil.getInstance(); + return util.findData(clazz, wheres, values); + } + + /** + * 查询一行数据 + * + * @return 没有数据返回null + */ + public static T findData(Class clazz, @NonNull String[] wheres, + @NonNull String[] values) { + DbUtil util = DbUtil.getInstance(); + List datas = util.findData(clazz, wheres, values); + return datas == null ? null : datas.size() > 0 ? datas.get(0) : null; + } + /** * 获取所有行的rowid */ @@ -106,37 +138,4 @@ public class DbEntity { e.printStackTrace(); } } - - /** - * 查询所有数据 - * - * @return 没有数据返回null - */ - public static List findAllData(Class clazz) { - DbUtil util = DbUtil.getInstance(); - return util.findAllData(clazz); - } - - /** - * 查询一组数据 - * - * @return 没有数据返回null - */ - public static List findDatas(Class clazz, @NonNull String[] wheres, - @NonNull String[] values) { - DbUtil util = DbUtil.getInstance(); - return util.findData(clazz, wheres, values); - } - - /** - * 查询一行数据 - * - * @return 没有数据返回null - */ - public static T findData(Class clazz, @NonNull String[] wheres, - @NonNull String[] values) { - DbUtil util = DbUtil.getInstance(); - List datas = util.findData(clazz, wheres, values); - return datas == null ? null : datas.size() > 0 ? datas.get(0) : null; - } } diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbUtil.java b/downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbUtil.java index 6dd9591a..256887a5 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbUtil.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbUtil.java @@ -18,8 +18,8 @@ import java.util.List; */ public class DbUtil { private static final String TAG = "DbUtil"; - private volatile static DbUtil INSTANCE = null; private static final Object LOCK = new Object(); + private volatile static DbUtil INSTANCE = null; private int CREATE_TABLE = 0; private int TABLE_EXISTS = 1; private int INSERT_DATA = 2; @@ -35,6 +35,10 @@ public class DbUtil { } + private DbUtil(Context context) { + mHelper = new SqlHelper(context.getApplicationContext()); + } + public static DbUtil init(Context context) { if (context instanceof Application) { synchronized (LOCK) { @@ -46,10 +50,6 @@ public class DbUtil { return INSTANCE; } - private DbUtil(Context context) { - mHelper = new SqlHelper(context.getApplicationContext()); - } - protected static DbUtil getInstance() { if (INSTANCE == null) { throw new NullPointerException("请在Application中调用init进行数据库工具注册注册"); @@ -292,7 +292,7 @@ public class DbUtil { * @param type {@link DbUtil} */ private void print(int type, String sql) { - if (true){ + if (true) { return; } String str = ""; diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/util/Util.java b/downloadutil/src/main/java/com/arialyy/downloadutil/util/Util.java index f1d68a7e..b437fe17 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/util/Util.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/util/Util.java @@ -159,7 +159,6 @@ public class Util { return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "t"; } - /** * 创建目录 当目录不存在的时候创建文件,否则返回false */