优化代码,优化demo

pull/330/head
AriaLyy 8 years ago
parent 1764f50258
commit 59b983eaec
  1. 15
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java
  2. 7
      Aria/src/main/java/com/arialyy/aria/core/download/SingleThreadTask.java
  3. 54
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java
  4. 20
      Aria/src/main/java/com/arialyy/aria/core/queue/DownloadTaskQueue.java
  5. 11
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java
  6. 24
      app/src/main/java/com/arialyy/simple/download/HighestPriorityActivity.java
  7. 62
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  8. 6
      app/src/main/res/layout/dialog_msg.xml
  9. 12
      app/src/main/res/menu/menu_single_task_activity.xml

@ -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<DownloadEntity, DownloadTaskEntity
}
@Override public int getPercent() {
DownloadEntity entity = DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?",
this.entity.getDownloadUrl());
if (entity == null) {
Log.e("DownloadTarget", "下载管理器中没有该任务");
return 0;
@ -103,8 +100,8 @@ public class DownloadTarget extends AbsTarget<DownloadEntity, DownloadTaskEntity
/**
* 下载任务是否存在
*/
@Override public boolean taskExists(String downloadUrl) {
return DownloadTaskQueue.getInstance().getTask(downloadUrl) != null;
@Override public boolean taskExists() {
return DownloadTaskQueue.getInstance().getTask(entity.getDownloadUrl()) != null;
}
/**
@ -141,15 +138,15 @@ public class DownloadTarget extends AbsTarget<DownloadEntity, DownloadTaskEntity
return this;
}
private DownloadEntity getDownloadEntity(String downloadUrl) {
CheckUtil.checkDownloadUrl(downloadUrl);
return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);
private DownloadEntity getDownloadEntity() {
return entity;
}
/**
* 是否在下载
*/
public boolean isDownloading() {
return DownloadTaskQueue.getInstance().getTask(entity).isRunning();
DownloadTask task = DownloadTaskQueue.getInstance().getTask(entity);
return task != null && task.isRunning();
}
}

@ -112,7 +112,8 @@ final class SingleThreadTask implements Runnable {
}
//支持断点的处理
if (mConfigEntity.isSupportBreakpoint) {
Log.i(TAG, "线程【" + mConfigEntity.THREAD_ID + "】下载完毕");
Log.i(TAG,
"任务【" + mConfigEntity.TEMP_FILE.getName() + "】线程【" + mConfigEntity.THREAD_ID + "】下载完毕");
writeConfig(mConfigEntity.TEMP_FILE.getName() + "_state_" + mConfigEntity.THREAD_ID, 1);
mListener.onChildComplete(mConfigEntity.END_LOCATION);
CONSTANCE.COMPLETE_THREAD_NUM++;
@ -130,14 +131,11 @@ final class SingleThreadTask implements Runnable {
mListener.onComplete();
}
} catch (MalformedURLException e) {
CONSTANCE.FAIL_NUM++;
failDownload(mConfigEntity, mChildCurrentLocation, "下载链接异常", e);
} catch (IOException e) {
CONSTANCE.FAIL_NUM++;
failDownload(mConfigEntity, mChildCurrentLocation, "下载失败【" + mConfigEntity.DOWNLOAD_URL + "】",
e);
} catch (Exception e) {
CONSTANCE.FAIL_NUM++;
failDownload(mConfigEntity, mChildCurrentLocation, "获取流失败", e);
}
}
@ -218,6 +216,7 @@ final class SingleThreadTask implements Runnable {
Exception ex) {
synchronized (LOCK) {
try {
CONSTANCE.FAIL_NUM++;
CONSTANCE.isDownloading = false;
CONSTANCE.isStop = true;
if (ex != null) {

@ -23,9 +23,7 @@ import com.arialyy.aria.core.RequestEnum;
import com.arialyy.aria.core.command.AbsCmd;
import com.arialyy.aria.core.command.CmdFactory;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.queue.DownloadTaskQueue;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.CommonUtil;
import java.util.ArrayList;
import java.util.List;
@ -89,31 +87,39 @@ public abstract class AbsTarget<ENTITY extends IEntity, TASK_ENTITY extends ITas
/**
* 获取任务文件大小
*
* @return -1没有找到该任务
* @return 文件大小
*/
public long getFileSize() {
if (entity instanceof DownloadEntity) {
DownloadEntity entity = DbEntity.findData(DownloadEntity.class, "downloadUrl=?",
((DownloadEntity) this.entity).getDownloadUrl());
if (entity == null) {
throw new NullPointerException("没有找到该任务");
}
DownloadEntity entity = (DownloadEntity) this.entity;
return entity.getFileSize();
} else if (entity instanceof UploadEntity) {
UploadEntity entity = DbEntity.findData(UploadEntity.class, "filePath=?",
((UploadEntity) this.entity).getFilePath());
if (entity == null) {
throw new NullPointerException("没有找到该任务");
}
UploadEntity entity = (UploadEntity) this.entity;
return entity.getFileSize();
}
return -1;
return 0;
}
/**
* 获取单位转换后的文件大小
*
* @return 文件大小{@code xxx mb}
*/
public String getConvertFileSize() {
if (entity instanceof DownloadEntity) {
DownloadEntity entity = (DownloadEntity) this.entity;
return CommonUtil.formatFileSize(entity.getFileSize());
} else if (entity instanceof UploadEntity) {
UploadEntity entity = (UploadEntity) this.entity;
return CommonUtil.formatFileSize(entity.getFileSize());
}
return "0b";
}
/**
* 下载任务是否存在
*/
public boolean taskExists(String downloadUrl) {
public boolean taskExists() {
return false;
}
@ -127,26 +133,16 @@ public abstract class AbsTarget<ENTITY extends IEntity, TASK_ENTITY extends ITas
}
/**
* 获取当前任务进度如果任务存在则返回当前进度
* 获取任务进度如果任务存在则返回当前进度
*
* @return -1没有找到该任务
* @return 该任务进度
*/
public long getCurrentProgress() {
if (entity instanceof DownloadEntity) {
DownloadEntity entity = DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?",
((DownloadEntity) this.entity).getDownloadUrl());
if (entity == null) {
Log.e("DownloadTarget", "下载管理器中没有该任务");
return -1;
}
DownloadEntity entity = (DownloadEntity) this.entity;
return entity.getCurrentProgress();
} else if (entity instanceof UploadEntity) {
UploadEntity entity = DbEntity.findData(UploadEntity.class, "filePath=?",
((UploadEntity) this.entity).getFilePath());
if (entity == null) {
Log.e("DownloadTarget", "下载管理器中没有该任务");
return -1;
}
UploadEntity entity = (UploadEntity) this.entity;
return entity.getCurrentProgress();
}
return -1;

@ -76,18 +76,26 @@ public class DownloadTaskQueue
for (int i = 0; i < maxSize; i++) {
DownloadTask oldTsk = mExecutePool.pollTask();
if (oldTsk != null && oldTsk.isRunning()) {
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++;
//}
}
}

@ -103,8 +103,8 @@ public class UploadTarget extends AbsTarget<UploadEntity, UploadTaskEntity> {
/**
* 下载任务是否存在
*/
@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<UploadEntity, UploadTaskEntity> {
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();
}
}

@ -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<ActivityHighestPriorit
@Override protected void init(Bundle savedInstanceState) {
super.init(savedInstanceState);
setTitle("最高优先级任务演示");
setTitle("最高优先级任务");
getBinding().setTaskName("任务名:" + mTaskName + " (最高优先级任务)");
initWidget();
}
private void initWidget() {
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) {
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());
List<DownloadEntity> temp = Aria.download(this).getTaskList();
if (temp != null && !temp.isEmpty()) {
for (DownloadEntity entity : temp) {
@ -83,8 +88,7 @@ public class HighestPriorityActivity extends BaseActivity<ActivityHighestPriorit
}
@Override public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_highest_priority, menu);
getMenuInflater().inflate(R.menu.menu_highest_priority, menu);
return super.onCreateOptionsMenu(menu);
}
@ -183,6 +187,7 @@ public class HighestPriorityActivity extends BaseActivity<ActivityHighestPriorit
if (task.getKey().equals(DOWNLOAD_URL)) {
setBtState(true);
mStart.setText("恢复");
mStart.setTextColor(getResources().getColor(android.R.color.holo_blue_light));
}
mAdapter.updateState(task.getDownloadEntity());
}
@ -190,6 +195,8 @@ public class HighestPriorityActivity extends BaseActivity<ActivityHighestPriorit
@Override public void onTaskCancel(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
setBtState(true);
mStart.setText("开始");
mPb.setProgress(0);
}
mAdapter.updateState(task.getDownloadEntity());
}
@ -205,6 +212,9 @@ public class HighestPriorityActivity extends BaseActivity<ActivityHighestPriorit
@Override public void onTaskComplete(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
setBtState(true);
mStart.setText("重新开始");
mStart.setTextColor(getResources().getColor(android.R.color.holo_green_light));
mPb.setProgress(100);
}
mAdapter.updateState(task.getDownloadEntity());
}

@ -25,6 +25,8 @@ import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
@ -42,6 +44,7 @@ import com.arialyy.frame.util.show.L;
import com.arialyy.frame.util.show.T;
import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.common.MsgDialog;
import com.arialyy.simple.databinding.ActivitySingleBinding;
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
@ -53,6 +56,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
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<ActivitySingleBinding> {
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<ActivitySingleBinding> {
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<ActivitySingleBinding> {
//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<ActivitySingleBinding> {
@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) {
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<ActivitySingleBinding> {
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<ActivitySingleBinding> {
@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) {
if (task.getKey().equals(DOWNLOAD_URL)) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_STOP);
}
}
@Override public void onTaskCancel(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_CANCEL);
}
}
@Override public void onTaskFail(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILE);
}
}
@Override public void onTaskComplete(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_COMPLETE);
}
}
@Override public void onTaskRunning(DownloadTask task) {
if (task.getKey().equals(DOWNLOAD_URL)) {
mUpdateHandler.obtainMessage(DOWNLOAD_RUNNING, task).sendToTarget();
}
}
}
}

@ -17,14 +17,16 @@
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background_color"
android:gravity="center|left"
android:maxHeight="400dp"
android:padding="8dp"
android:paddingBottom="8dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:text="@{title}"
android:textColor="@android:color/black"
android:textSize="22sp"

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/help"
android:icon="@mipmap/ic_help_black_48dp"
android:orderInCategory="90"
android:title="tip"
app:showAsAction="ifRoom"
/>
</menu>
Loading…
Cancel
Save