diff --git a/app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java index 872d8c49..31ddca59 100644 --- a/app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java @@ -19,7 +19,7 @@ import com.arialyy.downloadutil.core.DownloadManager; import com.arialyy.downloadutil.core.command.CmdFactory; import com.arialyy.downloadutil.core.command.IDownloadCmd; import com.arialyy.downloadutil.orm.DbEntity; -import com.arialyy.downloadutil.util.Util; +import com.arialyy.downloadutil.util.CommonUtil; import com.arialyy.frame.util.show.L; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; @@ -50,7 +50,7 @@ public class SingleTaskActivity extends BaseActivity { super.handleMessage(msg); switch (msg.what) { case DOWNLOAD_PRE: - mSize.setText(Util.formatFileSize((Long) msg.obj)); + mSize.setText(CommonUtil.formatFileSize((Long) msg.obj)); setBtState(false); break; case DOWNLOAD_FAILE: @@ -70,7 +70,7 @@ public class SingleTaskActivity extends BaseActivity { break; case DOWNLOAD_RESUME: Toast.makeText(SingleTaskActivity.this, - "恢复下载,恢复位置 ==> " + Util.formatFileSize((Long) msg.obj), Toast.LENGTH_SHORT).show(); + "恢复下载,恢复位置 ==> " + CommonUtil.formatFileSize((Long) msg.obj), Toast.LENGTH_SHORT).show(); setBtState(false); break; case DOWNLOAD_COMPLETE: @@ -174,7 +174,7 @@ public class SingleTaskActivity extends BaseActivity { new String[] { mDownloadUrl }); if (mEntity != null) { mPb.setProgress((int) ((mEntity.getCurrentProgress() * 100) / mEntity.getFileSize())); - mSize.setText(Util.formatFileSize(mEntity.getFileSize())); + mSize.setText(CommonUtil.formatFileSize(mEntity.getFileSize())); if (mEntity.getState() == DownloadEntity.STATE_DOWNLOAD_ING) { setBtState(false); } else if (mEntity.isDownloadComplete()) { diff --git a/app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java b/app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java index 50c8d3c5..5ef1bf35 100644 --- a/app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java +++ b/app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java @@ -12,7 +12,7 @@ import com.arialyy.downloadutil.core.DownloadEntity; import com.arialyy.downloadutil.core.DownloadManager; import com.arialyy.downloadutil.core.command.CmdFactory; import com.arialyy.downloadutil.core.command.IDownloadCmd; -import com.arialyy.downloadutil.util.Util; +import com.arialyy.downloadutil.util.CommonUtil; import com.arialyy.simple.R; import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; import java.util.ArrayList; @@ -121,8 +121,8 @@ public class DownloadAdapter extends AbsRVAdapter mExecuteQueue; private Map mExecuteArray; private ExecutePool() { - mExecuteQueue = new ArrayBlockingQueue<>(SIZE); + mExecuteQueue = new ArrayBlockingQueue<>(mSize); mExecuteArray = new HashMap<>(); } @@ -48,7 +48,7 @@ public class ExecutePool implements IPool { Log.e(TAG, "队列中已经包含了该任务,任务下载链接【" + url + "】"); return false; } else { - if (mExecuteQueue.size() >= SIZE) { + if (mExecuteQueue.size() >= mSize) { if (pollFirstTask()) { return putNewTask(task); } @@ -60,6 +60,25 @@ public class ExecutePool implements IPool { return false; } + /** + * 设置执行任务数 + * + * @param downloadNum 下载数 + */ + private void setDownloadNum(int downloadNum) { + try { + ArrayBlockingQueue temp = new ArrayBlockingQueue<>(downloadNum); + Task task; + while ((task = mExecuteQueue.poll(TIME_OUT, TimeUnit.MICROSECONDS)) != null) { + temp.offer(task); + } + mExecuteQueue = temp; + mSize = downloadNum; + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + /** * 添加新任务 * @@ -71,7 +90,7 @@ public class ExecutePool implements IPool { Log.w(TAG, "任务添加" + (s ? "成功" : "失败,【" + url + "】")); if (s) { newTask.start(); - mExecuteArray.put(Util.keyToHashKey(url), newTask); + mExecuteArray.put(CommonUtil.keyToHashKey(url), newTask); } return s; } @@ -88,7 +107,7 @@ public class ExecutePool implements IPool { } oldTask.stop(); // wait(200); - String key = Util.keyToHashKey(oldTask.getDownloadEntity().getDownloadUrl()); + String key = CommonUtil.keyToHashKey(oldTask.getDownloadEntity().getDownloadUrl()); mExecuteArray.remove(key); } catch (InterruptedException e) { e.printStackTrace(); @@ -102,7 +121,7 @@ public class ExecutePool implements IPool { Task task = mExecuteQueue.poll(); if (task != null) { String url = task.getDownloadEntity().getDownloadUrl(); - mExecuteArray.remove(Util.keyToHashKey(url)); + mExecuteArray.remove(CommonUtil.keyToHashKey(url)); } return task; } @@ -114,7 +133,7 @@ public class ExecutePool implements IPool { Log.e(TAG, "请传入有效的下载链接"); return null; } - String key = Util.keyToHashKey(downloadUrl); + String key = CommonUtil.keyToHashKey(downloadUrl); return mExecuteArray.get(key); } } @@ -125,7 +144,7 @@ public class ExecutePool implements IPool { Log.e(TAG, "任务不能为空"); return false; } else { - String key = Util.keyToHashKey(task.getDownloadEntity().getDownloadUrl()); + String key = CommonUtil.keyToHashKey(task.getDownloadEntity().getDownloadUrl()); mExecuteArray.remove(key); return mExecuteQueue.remove(task); } @@ -138,7 +157,7 @@ public class ExecutePool implements IPool { Log.e(TAG, "请传入有效的下载链接"); return false; } - String key = Util.keyToHashKey(downloadUrl); + String key = CommonUtil.keyToHashKey(downloadUrl); Task task = mExecuteArray.get(key); mExecuteArray.remove(key); return mExecuteQueue.remove(task); diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/DownloadSchedulers.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/DownloadSchedulers.java index 85e2235a..a7b98158 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/DownloadSchedulers.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/DownloadSchedulers.java @@ -73,7 +73,7 @@ public class DownloadSchedulers implements IDownloadSchedulers { switch (msg.what) { case STOP: case CANCEL: - if (mQueue.getExecutePool().size() != ExecutePool.SIZE) { + if (mQueue.getExecutePool().size() != ExecutePool.mSize) { startNextTask(entity); } break; diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/task/DownloadUtil.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/task/DownloadUtil.java index 70e77d97..630e2abb 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/core/task/DownloadUtil.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/task/DownloadUtil.java @@ -4,7 +4,7 @@ import android.content.Context; import android.util.Log; import android.util.SparseArray; import com.arialyy.downloadutil.core.DownloadEntity; -import com.arialyy.downloadutil.util.Util; +import com.arialyy.downloadutil.util.CommonUtil; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -165,7 +165,7 @@ final class DownloadUtil implements IDownloadUtil { try { if (!configFile.exists()) { //记录文件被删除,则重新下载 isNewTask = true; - Util.createFile(configFile.getPath()); + CommonUtil.createFile(configFile.getPath()); } else { isNewTask = !dFile.exists(); } @@ -192,14 +192,14 @@ final class DownloadUtil implements IDownloadUtil { if (code == 200) { int fileLength = conn.getContentLength(); //必须建一个文件 - Util.createFile(filePath); + CommonUtil.createFile(filePath); RandomAccessFile file = new RandomAccessFile(filePath, "rwd"); //设置文件长度 file.setLength(fileLength); mListener.onPostPre(fileLength); //分配每条线程的下载区间 Properties pro = null; - pro = Util.loadConfig(configFile); + pro = CommonUtil.loadConfig(configFile); if (pro.isEmpty()) { isNewTask = true; } else { @@ -288,7 +288,7 @@ final class DownloadUtil implements IDownloadUtil { + "】\n【filePath:" + filePath + "】" - + Util.getPrintException(e)); + + CommonUtil.getPrintException(e)); } } }).start(); @@ -486,9 +486,9 @@ final class DownloadUtil implements IDownloadUtil { */ private void writeConfig(String key, String record) throws IOException { File configFile = new File(configFPath); - Properties pro = Util.loadConfig(configFile); + Properties pro = CommonUtil.loadConfig(configFile); pro.setProperty(key, record); - Util.saveConfig(configFile, pro); + CommonUtil.saveConfig(configFile, pro); } /** @@ -502,7 +502,7 @@ final class DownloadUtil implements IDownloadUtil { isStop = true; Log.e(TAG, msg); if (ex != null) { - Log.e(TAG, Util.getPrintException(ex)); + Log.e(TAG, CommonUtil.getPrintException(ex)); } if (currentLocation != -1) { String location = String.valueOf(currentLocation); diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/help/Lockhelp.java b/downloadutil/src/main/java/com/arialyy/downloadutil/help/Lockhelp.java deleted file mode 100644 index f4eaebb6..00000000 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/help/Lockhelp.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.arialyy.downloadutil.help; - -/** - * Created by “AriaLyy@outlook.com” on 2016/11/2. - */ -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 6fc7bcd7..5fb90e2e 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,7 @@ package com.arialyy.downloadutil.orm; import android.support.annotation.NonNull; -import com.arialyy.downloadutil.util.Util; +import com.arialyy.downloadutil.util.CommonUtil; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; @@ -117,7 +117,7 @@ public class DbEntity { private void updateRowID() { try { - Field[] fields = Util.getFields(getClass()); + Field[] fields = CommonUtil.getFields(getClass()); List where = new ArrayList<>(); List values = new ArrayList<>(); for (Field field : fields) { 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 256887a5..890ad662 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbUtil.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbUtil.java @@ -6,7 +6,7 @@ import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.annotation.NonNull; import android.util.Log; -import com.arialyy.downloadutil.util.Util; +import com.arialyy.downloadutil.util.CommonUtil; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Date; @@ -71,7 +71,7 @@ public class DbUtil { return; } StringBuilder sb = new StringBuilder(); - sb.append("DELETE FROM ").append(Util.getClassName(clazz)).append(" WHERE "); + sb.append("DELETE FROM ").append(CommonUtil.getClassName(clazz)).append(" WHERE "); int i = 0; for (Object where : wheres) { sb.append(where).append("=").append("'").append(values[i]).append("'"); @@ -89,10 +89,10 @@ public class DbUtil { synchronized void modifyData(DbEntity dbEntity) { mDb = mHelper.getWritableDatabase(); Class clazz = dbEntity.getClass(); - Field[] fields = Util.getFields(clazz); + Field[] fields = CommonUtil.getFields(clazz); if (fields != null && fields.length > 0) { StringBuilder sb = new StringBuilder(); - sb.append("UPDATE ").append(Util.getClassName(dbEntity)).append(" SET "); + sb.append("UPDATE ").append(CommonUtil.getClassName(dbEntity)).append(" SET "); int i = 0; for (Field field : fields) { field.setAccessible(true); @@ -127,7 +127,7 @@ public class DbUtil { } mDb = mHelper.getReadableDatabase(); StringBuilder sb = new StringBuilder(); - sb.append("SELECT rowid, * FROM ").append(Util.getClassName(clazz)); + sb.append("SELECT rowid, * FROM ").append(CommonUtil.getClassName(clazz)); print(FIND_ALL_DATA, sb.toString()); Cursor cursor = mDb.rawQuery(sb.toString(), null); return cursor.getCount() > 0 ? newInstanceEntity(clazz, cursor) : null; @@ -150,7 +150,7 @@ public class DbUtil { return null; } StringBuilder sb = new StringBuilder(); - sb.append("SELECT rowid, * FROM ").append(Util.getClassName(clazz)).append(" where "); + sb.append("SELECT rowid, * FROM ").append(CommonUtil.getClassName(clazz)).append(" where "); int i = 0; for (Object where : wheres) { sb.append(where).append("=").append("'").append(values[i]).append("'"); @@ -171,10 +171,10 @@ public class DbUtil { createTable(clazz); } mDb = mHelper.getWritableDatabase(); - Field[] fields = Util.getFields(clazz); + Field[] fields = CommonUtil.getFields(clazz); if (fields != null && fields.length > 0) { StringBuilder sb = new StringBuilder(); - sb.append("INSERT INTO ").append(Util.getClassName(dbEntity)).append("("); + sb.append("INSERT INTO ").append(CommonUtil.getClassName(dbEntity)).append("("); int i = 0; for (Field field : fields) { field.setAccessible(true); @@ -221,7 +221,7 @@ public class DbUtil { try { StringBuilder sb = new StringBuilder(); sb.append("SELECT COUNT(*) AS c FROM sqlite_master WHERE type='table' AND name='"); - sb.append(Util.getClassName(clazz)); + sb.append(CommonUtil.getClassName(clazz)); sb.append("'"); print(TABLE_EXISTS, sb.toString()); cursor = mDb.rawQuery(sb.toString(), null); @@ -247,10 +247,10 @@ public class DbUtil { if (mDb == null || !mDb.isOpen()) { mDb = mHelper.getWritableDatabase(); } - Field[] fields = Util.getFields(clazz); + Field[] fields = CommonUtil.getFields(clazz); if (fields != null && fields.length > 0) { StringBuilder sb = new StringBuilder(); - sb.append("create table ").append(Util.getClassName(clazz)).append("("); + sb.append("create table ").append(CommonUtil.getClassName(clazz)).append("("); for (Field field : fields) { field.setAccessible(true); Ignore ignore = field.getAnnotation(Ignore.class); @@ -333,7 +333,7 @@ public class DbUtil { */ synchronized int[] getRowId(Class clazz) { mDb = mHelper.getReadableDatabase(); - Cursor cursor = mDb.rawQuery("SELECT rowid, * FROM " + Util.getClassName(clazz), null); + Cursor cursor = mDb.rawQuery("SELECT rowid, * FROM " + CommonUtil.getClassName(clazz), null); int[] ids = new int[cursor.getCount()]; int i = 0; while (cursor.moveToNext()) { @@ -358,7 +358,7 @@ public class DbUtil { return -1; } StringBuilder sb = new StringBuilder(); - sb.append("SELECT rowid FROM ").append(Util.getClassName(clazz)).append(" WHERE "); + sb.append("SELECT rowid FROM ").append(CommonUtil.getClassName(clazz)).append(" WHERE "); int i = 0; for (Object where : wheres) { sb.append(where).append("=").append("'").append(values[i]).append("'"); @@ -378,7 +378,7 @@ public class DbUtil { */ private synchronized List newInstanceEntity(Class clazz, Cursor cursor) { - Field[] fields = Util.getFields(clazz); + Field[] fields = CommonUtil.getFields(clazz); List entitys = new ArrayList<>(); if (fields != null && fields.length > 0) { try { diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/help/CheckHelp.java b/downloadutil/src/main/java/com/arialyy/downloadutil/util/CheckUtil.java similarity index 92% rename from downloadutil/src/main/java/com/arialyy/downloadutil/help/CheckHelp.java rename to downloadutil/src/main/java/com/arialyy/downloadutil/util/CheckUtil.java index 321754da..e2f85b9c 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/help/CheckHelp.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/util/CheckUtil.java @@ -1,4 +1,4 @@ -package com.arialyy.downloadutil.help; +package com.arialyy.downloadutil.util; import android.content.res.Resources; import android.text.TextUtils; @@ -11,8 +11,8 @@ import java.io.File; * Created by Lyy on 2016/9/23. * 检查帮助类 */ -public class CheckHelp { - private static final String TAG = "CheckHelp"; +public class CheckUtil { + private static final String TAG = "CheckUtil"; /** * 检测下载实体是否合法 diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/util/Util.java b/downloadutil/src/main/java/com/arialyy/downloadutil/util/CommonUtil.java similarity index 99% rename from downloadutil/src/main/java/com/arialyy/downloadutil/util/Util.java rename to downloadutil/src/main/java/com/arialyy/downloadutil/util/CommonUtil.java index b437fe17..0d0f951a 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/util/Util.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/util/CommonUtil.java @@ -14,7 +14,7 @@ import java.util.Properties; /** * Created by lyy on 2016/1/22. */ -public class Util { +public class CommonUtil { private static final String TAG = "util"; /** diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/help/PathHelp.java b/downloadutil/src/main/java/com/arialyy/downloadutil/util/PathUtil.java similarity index 95% rename from downloadutil/src/main/java/com/arialyy/downloadutil/help/PathHelp.java rename to downloadutil/src/main/java/com/arialyy/downloadutil/util/PathUtil.java index 6d47fb13..262146ef 100644 --- a/downloadutil/src/main/java/com/arialyy/downloadutil/help/PathHelp.java +++ b/downloadutil/src/main/java/com/arialyy/downloadutil/util/PathUtil.java @@ -1,4 +1,4 @@ -package com.arialyy.downloadutil.help; +package com.arialyy.downloadutil.util; import android.os.Environment; import java.security.MessageDigest; @@ -8,7 +8,7 @@ import java.security.NoSuchAlgorithmException; * Created by lyy on 2016/9/27. * 下载路径帮助类 */ -public class PathHelp { +public class PathUtil { /** * 下载链接转换保存路径