逻辑重构

This commit is contained in:
AriaLyy
2016-12-05 18:23:32 +08:00
parent a842f74c6f
commit 374fdd12b9
12 changed files with 426 additions and 218 deletions

View File

@ -27,8 +27,11 @@ import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import butterknife.Bind;
import com.arialyy.downloadutil.core.AMTarget;
import com.arialyy.downloadutil.core.Aria;
import com.arialyy.downloadutil.core.DownloadEntity;
import com.arialyy.downloadutil.core.DownloadManager;
import com.arialyy.downloadutil.core.task.Task;
import com.arialyy.frame.util.show.L;
import com.arialyy.simple.R;
import com.arialyy.simple.adapter.DownloadAdapter;
@ -58,53 +61,53 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
mList.setAdapter(mAdapter);
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
long len = 0;
@Override public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
DownloadEntity entity = intent.getParcelableExtra(DownloadManager.ENTITY);
switch (action) {
case DownloadManager.ACTION_PRE:
L.d(TAG, "download pre");
mAdapter.updateState(entity);
break;
case DownloadManager.ACTION_POST_PRE:
len = entity.getFileSize();
L.d(TAG, "download post pre");
break;
case DownloadManager.ACTION_START:
L.d(TAG, "download start");
break;
case DownloadManager.ACTION_RESUME:
L.d(TAG, "download resume");
long location = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 1);
mAdapter.updateState(entity);
break;
case DownloadManager.ACTION_RUNNING:
long current = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 0);
long speed = intent.getLongExtra(DownloadManager.CURRENT_SPEED, 0);
//mAdapter.setProgress(entity.getDownloadUrl(), current, speed);
mAdapter.setProgress(entity);
break;
case DownloadManager.ACTION_STOP:
L.d(TAG, "download stop");
mAdapter.updateState(entity);
break;
case DownloadManager.ACTION_COMPLETE:
L.d(TAG, "download complete");
mAdapter.updateState(entity);
break;
case DownloadManager.ACTION_CANCEL:
L.d(TAG, "download cancel");
mAdapter.updateState(entity);
break;
case DownloadManager.ACTION_FAIL:
L.d(TAG, "download fail");
break;
}
}
};
//private BroadcastReceiver mReceiver = new BroadcastReceiver() {
// long len = 0;
//
// @Override public void onReceive(Context context, Intent intent) {
// String action = intent.getAction();
// DownloadEntity entity = intent.getParcelableExtra(DownloadManager.ENTITY);
// switch (action) {
// case DownloadManager.ACTION_PRE:
// L.d(TAG, "download pre");
// mAdapter.updateState(entity);
// break;
// case DownloadManager.ACTION_POST_PRE:
// len = entity.getFileSize();
// L.d(TAG, "download post pre");
// break;
// case DownloadManager.ACTION_START:
// L.d(TAG, "download start");
// break;
// case DownloadManager.ACTION_RESUME:
// L.d(TAG, "download resume");
// long location = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 1);
// mAdapter.updateState(entity);
// break;
// case DownloadManager.ACTION_RUNNING:
// long current = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 0);
// long speed = intent.getLongExtra(DownloadManager.CURRENT_SPEED, 0);
// //mAdapter.setProgress(entity.getDownloadUrl(), current, speed);
// mAdapter.setProgress(entity);
// break;
// case DownloadManager.ACTION_STOP:
// L.d(TAG, "download stop");
// mAdapter.updateState(entity);
// break;
// case DownloadManager.ACTION_COMPLETE:
// L.d(TAG, "download complete");
// mAdapter.updateState(entity);
// break;
// case DownloadManager.ACTION_CANCEL:
// L.d(TAG, "download cancel");
// mAdapter.updateState(entity);
// break;
// case DownloadManager.ACTION_FAIL:
// L.d(TAG, "download fail");
// break;
// }
// }
//};
public void onClick(View view){
DownloadNumDialog dialog = new DownloadNumDialog(this);
@ -113,12 +116,13 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
@Override protected void onResume() {
super.onResume();
registerReceiver(mReceiver, getModule(DownloadModule.class).getDownloadFilter());
//registerReceiver(mReceiver, getModule(DownloadModule.class).getDownloadFilter());
Aria.whit(this).addSchedulerListener(new MySchedulerListener());
}
@Override protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
//unregisterReceiver(mReceiver);
}
@Override protected void dataCallback(int result, Object data) {
@ -127,4 +131,48 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
mAdapter.setDownloadNum(Integer.parseInt(data + ""));
}
}
private class MySchedulerListener extends AMTarget.SimpleSchedulerListener{
@Override public void onTaskPre(Task task) {
super.onTaskPre(task);
L.d(TAG, "download pre");
mAdapter.updateState(task.getDownloadEntity());
}
@Override public void onTaskStart(Task task) {
super.onTaskStart(task);
L.d(TAG, "download start");
}
@Override public void onTaskResume(Task task) {
super.onTaskResume(task);
L.d(TAG, "download resume");
mAdapter.updateState(task.getDownloadEntity());
}
@Override public void onTaskRunning(Task task) {
super.onTaskRunning(task);
mAdapter.setProgress(task.getDownloadEntity());
}
@Override public void onTaskStop(Task task) {
super.onTaskStop(task);
mAdapter.updateState(task.getDownloadEntity());
}
@Override public void onTaskCancel(Task task) {
super.onTaskCancel(task);
mAdapter.updateState(task.getDownloadEntity());
}
@Override public void onTaskComplete(Task task) {
super.onTaskComplete(task);
mAdapter.updateState(task.getDownloadEntity());
}
@Override public void onTaskFail(Task task) {
super.onTaskFail(task);
L.d(TAG, "download fail");
}
}
}