From 59b983eaec0bdbf81fb6a99ac009b350257f1c97 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Sat, 3 Jun 2017 19:01:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aria/core/download/DownloadTarget.java | 15 ++-- .../aria/core/download/SingleThreadTask.java | 7 +- .../com/arialyy/aria/core/inf/AbsTarget.java | 54 +++++++------- .../aria/core/queue/DownloadTaskQueue.java | 24 ++++-- .../aria/core/upload/UploadTarget.java | 11 +-- .../download/HighestPriorityActivity.java | 26 +++++-- .../simple/download/SingleTaskActivity.java | 74 ++++++++++++++----- app/src/main/res/layout/dialog_msg.xml | 6 +- .../res/menu/menu_single_task_activity.xml | 12 +++ 9 files changed, 145 insertions(+), 84 deletions(-) create mode 100644 app/src/main/res/menu/menu_single_task_activity.xml diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java index ca2b48b8..4d6c8ab7 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java @@ -21,7 +21,6 @@ import android.util.Log; import com.arialyy.aria.core.RequestEnum; import com.arialyy.aria.core.inf.AbsTarget; import com.arialyy.aria.core.queue.DownloadTaskQueue; -import com.arialyy.aria.util.CheckUtil; import java.io.File; import java.util.Map; @@ -67,8 +66,6 @@ public class DownloadTarget extends AbsTarget keys = exeTasks.keySet(); for (String key : keys) { DownloadTask temp = exeTasks.get(key); - if (temp != null && temp.isRunning() && temp.isHighestPriorityTask() && !temp.getKey() + if (temp != null && temp.isRunning() && temp.isHighestPriorityTask() && !temp.getKey() .equals(task.getKey())) { Log.e(TAG, "设置最高优先级任务失败,失败原因【任务中已经有最高优先级任务,请等待上一个最高优先级任务完成,或手动暂停该任务】"); task.setHighestPriority(false); @@ -76,18 +76,26 @@ public class DownloadTaskQueue for (int i = 0; i < maxSize; i++) { DownloadTask oldTsk = mExecutePool.pollTask(); if (oldTsk != null && oldTsk.isRunning()) { - oldTsk.stop(); + if (i == maxSize - 1) { + oldTsk.stop(); + break; + } tempTasks.add(oldTsk); } } startTask(task); - int i = 0, len = tempTasks.size() - 1; - for (DownloadTask oldTask : tempTasks) { - if (i < len) { - startTask(oldTask); - } - i++; + + for (DownloadTask temp : tempTasks){ + mExecutePool.putTask(temp); } + + //int i = 0, len = tempTasks.size() - 1; + //for (DownloadTask oldTask : tempTasks) { + // if (i < len) { + // startTask(oldTask); + // } + // i++; + //} } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java index b0ad23c1..1868d51c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java @@ -103,8 +103,8 @@ public class UploadTarget extends AbsTarget { /** * 下载任务是否存在 */ - @Override public boolean taskExists(String downloadUrl) { - return UploadTaskQueue.getInstance().getTask(downloadUrl) != null; + @Override public boolean taskExists() { + return UploadTaskQueue.getInstance().getTask(entity.getFilePath()) != null; } /** @@ -117,14 +117,15 @@ public class UploadTarget extends AbsTarget { return this; } - private UploadEntity getDownloadEntity(@NonNull String filePath) { - return DbEntity.findData(UploadEntity.class, "filePath=?", filePath); + private UploadEntity getDownloadEntity() { + return entity; } /** * 是否在下载 */ public boolean isUploading() { - return UploadTaskQueue.getInstance().getTask(entity).isRunning(); + UploadTask task = UploadTaskQueue.getInstance().getTask(entity); + return task != null && task.isRunning(); } } diff --git a/app/src/main/java/com/arialyy/simple/download/HighestPriorityActivity.java b/app/src/main/java/com/arialyy/simple/download/HighestPriorityActivity.java index 0fca613c..bfcdd0c0 100644 --- a/app/src/main/java/com/arialyy/simple/download/HighestPriorityActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/HighestPriorityActivity.java @@ -13,6 +13,7 @@ import android.widget.TextView; import butterknife.Bind; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.download.DownloadEntity; +import com.arialyy.aria.core.download.DownloadTarget; import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.aria.core.inf.IEntity; import com.arialyy.frame.util.show.L; @@ -52,18 +53,22 @@ public class HighestPriorityActivity extends BaseActivity temp = Aria.download(this).getTaskList(); if (temp != null && !temp.isEmpty()) { for (DownloadEntity entity : temp) { @@ -83,8 +88,7 @@ public class HighestPriorityActivity extends BaseActivity { public static final int DOWNLOAD_RESUME = 0x05; public static final int DOWNLOAD_COMPLETE = 0x06; public static final int DOWNLOAD_RUNNING = 0x07; + public static final int DOWNLOAD_START = 0x08; private static final String DOWNLOAD_URL = //"http://kotlinlang.org/docs/kotlin-docs.pdf"; @@ -95,9 +99,10 @@ public class SingleTaskActivity extends BaseActivity { mSpeed.setText(task.getConvertSpeed()); break; case DOWNLOAD_PRE: - mSize.setText(CommonUtil.formatFileSize((Long) msg.obj)); setBtState(false); - //mStart.setText("暂停"); + break; + case DOWNLOAD_START: + mSize.setText(CommonUtil.formatFileSize((Long) msg.obj)); break; case DOWNLOAD_FAILE: Toast.makeText(SingleTaskActivity.this, "下载失败", Toast.LENGTH_SHORT).show(); @@ -115,9 +120,6 @@ public class SingleTaskActivity extends BaseActivity { setBtState(true); break; case DOWNLOAD_RESUME: - //Toast.makeText(SingleTaskActivity.this, - // "恢复下载,恢复位置 ==> " + CommonUtil.formatFileSize((Long) msg.obj), Toast.LENGTH_SHORT) - // .show(); mStart.setText("暂停"); setBtState(false); break; @@ -146,6 +148,22 @@ public class SingleTaskActivity extends BaseActivity { //registerReceiver(mReceiver, getModule(DownloadModule.class).getDownloadFilter()); } + @Override public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.menu_single_task_activity, menu); + return super.onCreateOptionsMenu(menu); + } + + @Override public boolean onMenuItemClick(MenuItem item) { + if (item.getItemId() == R.id.help) { + String msg = "一些小知识点:\n" + + "1、你可以通过task.getKey().equals(DOWNLOAD_URL)判断是否是当前页面的下载,以防止progress乱跳\n" + + "2、当遇到网络慢的情况时,你可以先使用onPre()更新UI界面,待连接成功时,再在onTaskPre()获取完整的task数据,然后给UI界面设置正确的数据\n" + + "3、你可以在界面初始化时通过Aria.download(this).load(DOWNLOAD_URL).getPercent()等方法快速获取相关任务的一些数据"; + showMsgDialog("tip", msg); + } + return true; + } + @Override protected int setLayoutId() { return R.layout.activity_single; } @@ -153,12 +171,16 @@ public class SingleTaskActivity extends BaseActivity { @Override protected void init(Bundle savedInstanceState) { super.init(savedInstanceState); setTitle("单任务下载"); - if (Aria.download(this).taskExists(DOWNLOAD_URL)) { - mPb.setProgress(Aria.download(this).load(DOWNLOAD_URL).getPercent()); - if (Aria.download(this).load(DOWNLOAD_URL).getTaskState() == IEntity.STATE_STOP) { - mStart.setText("恢复"); - } + DownloadTarget target = Aria.download(this).load(DOWNLOAD_URL); + mPb.setProgress(target.getPercent()); + if (target.getTaskState() == IEntity.STATE_STOP) { + mStart.setText("恢复"); + mStart.setTextColor(getResources().getColor(android.R.color.holo_blue_light)); + setBtState(true); + } else if (target.isDownloading()) { + setBtState(false); } + mSize.setText(target.getConvertFileSize()); Aria.get(this).getDownloadConfig().setOpenBreadCast(true); } @@ -187,7 +209,10 @@ public class SingleTaskActivity extends BaseActivity { private class MySchedulerListener extends Aria.DownloadSchedulerListener { @Override public void onPre(DownloadTask task) { - super.onPre(task); + if (task.getKey().equals(DOWNLOAD_URL)) { + mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize()) + .sendToTarget(); + } } @Override public void onNoSupportBreakPoint(DownloadTask task) { @@ -198,34 +223,45 @@ public class SingleTaskActivity extends BaseActivity { @Override public void onTaskStart(DownloadTask task) { //通过下载地址可以判断任务是否是你指定的任务 if (task.getKey().equals(DOWNLOAD_URL)) { - mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize()) + mUpdateHandler.obtainMessage(DOWNLOAD_START, task.getDownloadEntity().getFileSize()) .sendToTarget(); } } @Override public void onTaskResume(DownloadTask task) { - super.onTaskResume(task); - mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getFileSize()).sendToTarget(); + if (task.getKey().equals(DOWNLOAD_URL)) { + mUpdateHandler.obtainMessage(DOWNLOAD_START, task.getFileSize()).sendToTarget(); + } } @Override public void onTaskStop(DownloadTask task) { - mUpdateHandler.sendEmptyMessage(DOWNLOAD_STOP); + if (task.getKey().equals(DOWNLOAD_URL)) { + mUpdateHandler.sendEmptyMessage(DOWNLOAD_STOP); + } } @Override public void onTaskCancel(DownloadTask task) { - mUpdateHandler.sendEmptyMessage(DOWNLOAD_CANCEL); + if (task.getKey().equals(DOWNLOAD_URL)) { + mUpdateHandler.sendEmptyMessage(DOWNLOAD_CANCEL); + } } @Override public void onTaskFail(DownloadTask task) { - mUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILE); + if (task.getKey().equals(DOWNLOAD_URL)) { + mUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILE); + } } @Override public void onTaskComplete(DownloadTask task) { - mUpdateHandler.sendEmptyMessage(DOWNLOAD_COMPLETE); + if (task.getKey().equals(DOWNLOAD_URL)) { + mUpdateHandler.sendEmptyMessage(DOWNLOAD_COMPLETE); + } } @Override public void onTaskRunning(DownloadTask task) { - mUpdateHandler.obtainMessage(DOWNLOAD_RUNNING, task).sendToTarget(); + if (task.getKey().equals(DOWNLOAD_URL)) { + mUpdateHandler.obtainMessage(DOWNLOAD_RUNNING, task).sendToTarget(); + } } } } \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_msg.xml b/app/src/main/res/layout/dialog_msg.xml index 61a9dd99..8aa7dbc2 100644 --- a/app/src/main/res/layout/dialog_msg.xml +++ b/app/src/main/res/layout/dialog_msg.xml @@ -17,14 +17,16 @@ android:orientation="vertical" > - + + + + \ No newline at end of file