逻辑重构

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");
}
}
}

View File

@ -28,11 +28,10 @@ import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import com.arialyy.downloadutil.core.AMReceiver;
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.command.CmdFactory;
import com.arialyy.downloadutil.core.command.IDownloadCmd;
import com.arialyy.downloadutil.core.scheduler.OnSchedulerListener;
import com.arialyy.downloadutil.core.task.Task;
import com.arialyy.downloadutil.orm.DbEntity;
import com.arialyy.downloadutil.util.CommonUtil;
@ -54,8 +53,6 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
private Button mStart, mStop, mCancel;
private TextView mSize;
@Bind(R.id.toolbar) Toolbar toolbar;
private CmdFactory mFactory;
private DownloadManager mManager;
private DownloadEntity mEntity;
private BroadcastReceiver mReceiver;
@ -69,6 +66,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
case DOWNLOAD_PRE:
mSize.setText(CommonUtil.formatFileSize((Long) msg.obj));
setBtState(false);
mStart.setText("暂停");
break;
case DOWNLOAD_FAILE:
Toast.makeText(SingleTaskActivity.this, "下载失败", Toast.LENGTH_SHORT).show();
@ -89,6 +87,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
//Toast.makeText(SingleTaskActivity.this,
// "恢复下载,恢复位置 ==> " + CommonUtil.formatFileSize((Long) msg.obj), Toast.LENGTH_SHORT)
// .show();
mStart.setText("暂停");
setBtState(false);
break;
case DOWNLOAD_COMPLETE:
@ -119,6 +118,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
//IntentFilter filter = getModule(DownloadModule.class).getDownloadFilter();
//mReceiver = getModule(DownloadModule.class).createReceiver(mUpdateHandler);
//registerReceiver(mReceiver, filter);
Aria.whit(this).addSchedulerListener(new MySchedulerListener());
}
@Override protected void onDestroy() {
@ -143,8 +143,8 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
mStop = (Button) findViewById(R.id.stop);
mCancel = (Button) findViewById(R.id.cancel);
mSize = (TextView) findViewById(R.id.size);
mFactory = CmdFactory.getInstance();
mManager = DownloadManager.getInstance();
//mFactory = CmdFactory.getInstance();
//mManager = DownloadManager.getInstance();
mEntity = DbEntity.findData(DownloadEntity.class, new String[] { "downloadUrl" },
new String[] { mDownloadUrl });
if (mEntity != null) {
@ -184,65 +184,61 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
}
private void resume(){
IDownloadCmd startCmd = mFactory.createCmd(mEntity, CmdFactory.TASK_START);
mManager.setCmd(startCmd).exe();
mUpdateHandler.obtainMessage(DOWNLOAD_RESUME, mEntity.getCurrentProgress()).sendToTarget();
Aria.whit(this).load(mEntity).resume();
}
private void start() {
mEntity.setFileName("test.apk");
mEntity.setDownloadUrl(mDownloadUrl);
mEntity.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk");
//List<IDownloadCmd> commands = new ArrayList<>();
//IDownloadCmd addCMD = mFactory.createCmd(mEntity, CmdFactory.TASK_CREATE);
//IDownloadCmd startCmd = mFactory.createCmd(mEntity, CmdFactory.TASK_START);
//commands.add(addCMD);
//commands.add(startCmd);
//mManager.setCmds(commands).exe();
mManager.setCmd(CmdFactory.getInstance().createCmd(mEntity, CmdFactory.TASK_SINGLE))
.addSchedulerListener(new OnSchedulerListener() {
@Override public void onTaskStart(Task task) {
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize())
.sendToTarget();
}
@Override public void onTaskStop(Task task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_STOP);
}
@Override public void onTaskCancel(Task task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_CANCEL);
}
@Override public void onTaskFail(Task task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILE);
}
@Override public void onTaskComplete(Task task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_COMPLETE);
}
@Override public void onTaskRunning(Task task) {
//L.d(TAG, task.getDownloadEntity().getCurrentProgress() + "");
long current = task.getDownloadEntity().getCurrentProgress();
long len = task.getDownloadEntity().getFileSize();
if (len == 0) {
mPb.setProgress(0);
} else {
mPb.setProgress((int) ((current * 100) / len));
}
}
})
.exe();
Aria.whit(this).load(mEntity).start();
}
private void stop() {
IDownloadCmd stopCmd = mFactory.createCmd(mEntity, CmdFactory.TASK_STOP);
mManager.setCmd(stopCmd).exe();
Aria.whit(this).load(mEntity).stop();
}
private void cancel() {
IDownloadCmd cancelCmd = mFactory.createCmd(mEntity, CmdFactory.TASK_CANCEL);
mManager.setCmd(cancelCmd).exe();
Aria.whit(this).load(mEntity).cancel();
}
private class MySchedulerListener extends AMTarget.SimpleSchedulerListener{
@Override public void onTaskStart(Task task) {
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize())
.sendToTarget();
}
@Override public void onTaskResume(Task task) {
super.onTaskResume(task);
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize())
.sendToTarget();
}
@Override public void onTaskStop(Task task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_STOP);
}
@Override public void onTaskCancel(Task task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_CANCEL);
}
@Override public void onTaskFail(Task task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILE);
}
@Override public void onTaskComplete(Task task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_COMPLETE);
}
@Override public void onTaskRunning(Task task) {
long current = task.getDownloadEntity().getCurrentProgress();
long len = task.getDownloadEntity().getFileSize();
if (len == 0) {
mPb.setProgress(0);
} else {
mPb.setProgress((int) ((current * 100) / len));
}
}
}
}

View File

@ -25,6 +25,7 @@ import android.widget.TextView;
import butterknife.Bind;
import com.arialyy.absadapter.common.AbsHolder;
import com.arialyy.absadapter.recycler_view.AbsRVAdapter;
import com.arialyy.downloadutil.core.Aria;
import com.arialyy.downloadutil.core.DownloadEntity;
import com.arialyy.downloadutil.core.DownloadManager;
import com.arialyy.downloadutil.core.command.CmdFactory;
@ -188,13 +189,15 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
}
private void start(DownloadEntity entity) {
IDownloadCmd startCmd = mFactory.createCmd(entity, CmdFactory.TASK_START);
mManager.setCmd(startCmd).exe();
//IDownloadCmd startCmd = mFactory.createCmd(entity, CmdFactory.TASK_START);
//mManager.setCmd(startCmd).exe();
Aria.whit(getContext()).load(entity).start();
}
private void stop(DownloadEntity entity) {
IDownloadCmd stopCmd = mFactory.createCmd(entity, CmdFactory.TASK_STOP);
mManager.setCmd(stopCmd).exe();
//IDownloadCmd stopCmd = mFactory.createCmd(entity, CmdFactory.TASK_STOP);
//mManager.setCmd(stopCmd).exe();
Aria.whit(getContext()).load(entity).stop();
}
}