添加单任务下载功能
This commit is contained in:
@ -14,12 +14,12 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
package com.arialyy.simple.activity;
|
package com.arialyy.simple.activity;
|
||||||
|
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.IntentFilter;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
@ -35,6 +35,8 @@ import com.arialyy.downloadutil.core.DownloadEntity;
|
|||||||
import com.arialyy.downloadutil.core.DownloadManager;
|
import com.arialyy.downloadutil.core.DownloadManager;
|
||||||
import com.arialyy.downloadutil.core.command.CmdFactory;
|
import com.arialyy.downloadutil.core.command.CmdFactory;
|
||||||
import com.arialyy.downloadutil.core.command.IDownloadCmd;
|
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.orm.DbEntity;
|
||||||
import com.arialyy.downloadutil.util.CommonUtil;
|
import com.arialyy.downloadutil.util.CommonUtil;
|
||||||
import com.arialyy.frame.util.show.L;
|
import com.arialyy.frame.util.show.L;
|
||||||
@ -46,12 +48,13 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
||||||
private static final int DOWNLOAD_PRE = 0x01;
|
public static final int DOWNLOAD_PRE = 0x01;
|
||||||
private static final int DOWNLOAD_STOP = 0x02;
|
public static final int DOWNLOAD_STOP = 0x02;
|
||||||
private static final int DOWNLOAD_FAILE = 0x03;
|
public static final int DOWNLOAD_FAILE = 0x03;
|
||||||
private static final int DOWNLOAD_CANCEL = 0x04;
|
public static final int DOWNLOAD_CANCEL = 0x04;
|
||||||
private static final int DOWNLOAD_RESUME = 0x05;
|
public static final int DOWNLOAD_RESUME = 0x05;
|
||||||
private static final int DOWNLOAD_COMPLETE = 0x06;
|
public static final int DOWNLOAD_COMPLETE = 0x06;
|
||||||
|
public static final int DOWNLOAD_RUNNING = 0x07;
|
||||||
private ProgressBar mPb;
|
private ProgressBar mPb;
|
||||||
private String mDownloadUrl =
|
private String mDownloadUrl =
|
||||||
"http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk";
|
"http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk";
|
||||||
@ -61,11 +64,15 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
|||||||
private CmdFactory mFactory;
|
private CmdFactory mFactory;
|
||||||
private DownloadManager mManager;
|
private DownloadManager mManager;
|
||||||
private DownloadEntity mEntity;
|
private DownloadEntity mEntity;
|
||||||
|
private BroadcastReceiver mReceiver;
|
||||||
|
|
||||||
private Handler mUpdateHandler = new Handler() {
|
private Handler mUpdateHandler = new Handler() {
|
||||||
@Override public void handleMessage(Message msg) {
|
@Override public void handleMessage(Message msg) {
|
||||||
super.handleMessage(msg);
|
super.handleMessage(msg);
|
||||||
switch (msg.what) {
|
switch (msg.what) {
|
||||||
|
case DOWNLOAD_RUNNING:
|
||||||
|
mPb.setProgress((Integer) msg.obj);
|
||||||
|
break;
|
||||||
case DOWNLOAD_PRE:
|
case DOWNLOAD_PRE:
|
||||||
mSize.setText(CommonUtil.formatFileSize((Long) msg.obj));
|
mSize.setText(CommonUtil.formatFileSize((Long) msg.obj));
|
||||||
setBtState(false);
|
setBtState(false);
|
||||||
@ -86,8 +93,9 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
|||||||
setBtState(true);
|
setBtState(true);
|
||||||
break;
|
break;
|
||||||
case DOWNLOAD_RESUME:
|
case DOWNLOAD_RESUME:
|
||||||
Toast.makeText(SingleTaskActivity.this,
|
//Toast.makeText(SingleTaskActivity.this,
|
||||||
"恢复下载,恢复位置 ==> " + CommonUtil.formatFileSize((Long) msg.obj), Toast.LENGTH_SHORT).show();
|
// "恢复下载,恢复位置 ==> " + CommonUtil.formatFileSize((Long) msg.obj), Toast.LENGTH_SHORT)
|
||||||
|
// .show();
|
||||||
setBtState(false);
|
setBtState(false);
|
||||||
break;
|
break;
|
||||||
case DOWNLOAD_COMPLETE:
|
case DOWNLOAD_COMPLETE:
|
||||||
@ -108,51 +116,6 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
|||||||
mStop.setEnabled(!state);
|
mStop.setEnabled(!state);
|
||||||
}
|
}
|
||||||
|
|
||||||
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
|
||||||
long len = 0;
|
|
||||||
|
|
||||||
@Override public void onReceive(Context context, Intent intent) {
|
|
||||||
String action = intent.getAction();
|
|
||||||
switch (action) {
|
|
||||||
case DownloadManager.ACTION_POST_PRE:
|
|
||||||
DownloadEntity entity = intent.getParcelableExtra(DownloadManager.ENTITY);
|
|
||||||
len = entity.getFileSize();
|
|
||||||
L.d(TAG, "download pre");
|
|
||||||
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, len).sendToTarget();
|
|
||||||
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);
|
|
||||||
mUpdateHandler.obtainMessage(DOWNLOAD_RESUME, location).sendToTarget();
|
|
||||||
break;
|
|
||||||
case DownloadManager.ACTION_RUNNING:
|
|
||||||
long current = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 0);
|
|
||||||
if (len == 0) {
|
|
||||||
mPb.setProgress(0);
|
|
||||||
} else {
|
|
||||||
mPb.setProgress((int) ((current * 100) / len));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case DownloadManager.ACTION_STOP:
|
|
||||||
L.d(TAG, "download stop");
|
|
||||||
mUpdateHandler.sendEmptyMessage(DOWNLOAD_STOP);
|
|
||||||
break;
|
|
||||||
case DownloadManager.ACTION_COMPLETE:
|
|
||||||
mUpdateHandler.sendEmptyMessage(DOWNLOAD_COMPLETE);
|
|
||||||
break;
|
|
||||||
case DownloadManager.ACTION_CANCEL:
|
|
||||||
mUpdateHandler.sendEmptyMessage(DOWNLOAD_CANCEL);
|
|
||||||
break;
|
|
||||||
case DownloadManager.ACTION_FAIL:
|
|
||||||
mUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILE);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
@Override protected void onCreate(Bundle savedInstanceState) {
|
@Override protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
init();
|
init();
|
||||||
@ -160,12 +123,14 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
|||||||
|
|
||||||
@Override protected void onResume() {
|
@Override protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
registerReceiver(mReceiver, getModule(DownloadModule.class).getDownloadFilter());
|
//IntentFilter filter = getModule(DownloadModule.class).getDownloadFilter();
|
||||||
|
//mReceiver = getModule(DownloadModule.class).createReceiver(mUpdateHandler);
|
||||||
|
//registerReceiver(mReceiver, filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override protected void onDestroy() {
|
@Override protected void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
unregisterReceiver(mReceiver);
|
//unregisterReceiver(mReceiver);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override protected int setLayoutId() {
|
@Override protected int setLayoutId() {
|
||||||
@ -200,13 +165,21 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
mEntity = new DownloadEntity();
|
mEntity = new DownloadEntity();
|
||||||
|
mEntity.setFileName("test.apk");
|
||||||
|
mEntity.setDownloadUrl(mDownloadUrl);
|
||||||
|
mEntity.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
switch (view.getId()) {
|
switch (view.getId()) {
|
||||||
case R.id.start:
|
case R.id.start:
|
||||||
|
String text = ((TextView) view).getText().toString();
|
||||||
|
if (text.equals("重新开始?") || text.equals("开始")) {
|
||||||
start();
|
start();
|
||||||
|
}else if (text.equals("恢复")){
|
||||||
|
resume();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case R.id.stop:
|
case R.id.stop:
|
||||||
stop();
|
stop();
|
||||||
@ -217,16 +190,57 @@ 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();
|
||||||
|
}
|
||||||
|
|
||||||
private void start() {
|
private void start() {
|
||||||
mEntity.setFileName("test.apk");
|
mEntity.setFileName("test.apk");
|
||||||
mEntity.setDownloadUrl(mDownloadUrl);
|
mEntity.setDownloadUrl(mDownloadUrl);
|
||||||
mEntity.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk");
|
mEntity.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk");
|
||||||
List<IDownloadCmd> commands = new ArrayList<>();
|
//List<IDownloadCmd> commands = new ArrayList<>();
|
||||||
IDownloadCmd addCMD = mFactory.createCmd(mEntity, CmdFactory.TASK_CREATE);
|
//IDownloadCmd addCMD = mFactory.createCmd(mEntity, CmdFactory.TASK_CREATE);
|
||||||
IDownloadCmd startCmd = mFactory.createCmd(mEntity, CmdFactory.TASK_START);
|
//IDownloadCmd startCmd = mFactory.createCmd(mEntity, CmdFactory.TASK_START);
|
||||||
commands.add(addCMD);
|
//commands.add(addCMD);
|
||||||
commands.add(startCmd);
|
//commands.add(startCmd);
|
||||||
mManager.setCmds(commands).exe();
|
//mManager.setCmds(commands).exe();
|
||||||
|
mManager.setCmd(CmdFactory.getInstance().createCmd(mEntity, CmdFactory.TASK_SINGLE))
|
||||||
|
.regSchedulerListener(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();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void stop() {
|
private void stop() {
|
||||||
|
@ -17,15 +17,20 @@
|
|||||||
|
|
||||||
package com.arialyy.simple.module;
|
package com.arialyy.simple.module;
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
|
import android.os.Handler;
|
||||||
import com.arialyy.downloadutil.core.DownloadEntity;
|
import com.arialyy.downloadutil.core.DownloadEntity;
|
||||||
import com.arialyy.downloadutil.core.DownloadManager;
|
import com.arialyy.downloadutil.core.DownloadManager;
|
||||||
import com.arialyy.downloadutil.util.CommonUtil;
|
import com.arialyy.downloadutil.util.CommonUtil;
|
||||||
import com.arialyy.frame.util.AndroidUtils;
|
import com.arialyy.frame.util.AndroidUtils;
|
||||||
import com.arialyy.frame.util.StringUtil;
|
import com.arialyy.frame.util.StringUtil;
|
||||||
|
import com.arialyy.frame.util.show.L;
|
||||||
import com.arialyy.simple.R;
|
import com.arialyy.simple.R;
|
||||||
|
import com.arialyy.simple.activity.SingleTaskActivity;
|
||||||
import com.arialyy.simple.base.BaseModule;
|
import com.arialyy.simple.base.BaseModule;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -114,6 +119,54 @@ public class DownloadModule extends BaseModule {
|
|||||||
return filter;
|
return filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建Receiver
|
||||||
|
*/
|
||||||
|
public BroadcastReceiver createReceiver(final Handler handler){
|
||||||
|
|
||||||
|
return new BroadcastReceiver() {
|
||||||
|
long len = 0;
|
||||||
|
|
||||||
|
@Override public void onReceive(Context context, Intent intent) {
|
||||||
|
String action = intent.getAction();
|
||||||
|
switch (action) {
|
||||||
|
case DownloadManager.ACTION_POST_PRE:
|
||||||
|
DownloadEntity entity = intent.getParcelableExtra(DownloadManager.ENTITY);
|
||||||
|
len = entity.getFileSize();
|
||||||
|
L.d(TAG, "download pre");
|
||||||
|
handler.obtainMessage(SingleTaskActivity.DOWNLOAD_PRE, len).sendToTarget();
|
||||||
|
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);
|
||||||
|
handler.obtainMessage(SingleTaskActivity.DOWNLOAD_RESUME, location).sendToTarget();
|
||||||
|
break;
|
||||||
|
case DownloadManager.ACTION_RUNNING:
|
||||||
|
long current = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 0);
|
||||||
|
int progress = len ==0 ? 0 : (int) ((current * 100) / len);
|
||||||
|
handler.obtainMessage(SingleTaskActivity.DOWNLOAD_RUNNING, progress).sendToTarget();
|
||||||
|
break;
|
||||||
|
case DownloadManager.ACTION_STOP:
|
||||||
|
L.d(TAG, "download stop");
|
||||||
|
handler.sendEmptyMessage(SingleTaskActivity.DOWNLOAD_STOP);
|
||||||
|
break;
|
||||||
|
case DownloadManager.ACTION_COMPLETE:
|
||||||
|
handler.sendEmptyMessage(SingleTaskActivity.DOWNLOAD_COMPLETE);
|
||||||
|
break;
|
||||||
|
case DownloadManager.ACTION_CANCEL:
|
||||||
|
handler.sendEmptyMessage(SingleTaskActivity.DOWNLOAD_CANCEL);
|
||||||
|
break;
|
||||||
|
case DownloadManager.ACTION_FAIL:
|
||||||
|
handler.sendEmptyMessage(SingleTaskActivity.DOWNLOAD_FAILE);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置下载队列
|
* 设置下载队列
|
||||||
*/
|
*/
|
||||||
|
@ -116,8 +116,9 @@ public class DownloadEntity extends DbEntity implements Parcelable {
|
|||||||
return fileName;
|
return fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFileName(String fileName) {
|
public DownloadEntity setFileName(String fileName) {
|
||||||
this.fileName = fileName;
|
this.fileName = fileName;
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getFailNum() {
|
public int getFailNum() {
|
||||||
@ -132,8 +133,9 @@ public class DownloadEntity extends DbEntity implements Parcelable {
|
|||||||
return downloadUrl;
|
return downloadUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDownloadUrl(String downloadUrl) {
|
public DownloadEntity setDownloadUrl(String downloadUrl) {
|
||||||
this.downloadUrl = downloadUrl;
|
this.downloadUrl = downloadUrl;
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getCompleteTime() {
|
public long getCompleteTime() {
|
||||||
@ -148,8 +150,9 @@ public class DownloadEntity extends DbEntity implements Parcelable {
|
|||||||
return downloadPath;
|
return downloadPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDownloadPath(String downloadPath) {
|
public DownloadEntity setDownloadPath(String downloadPath) {
|
||||||
this.downloadPath = downloadPath;
|
this.downloadPath = downloadPath;
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getFileSize() {
|
public long getFileSize() {
|
||||||
|
@ -14,7 +14,6 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
package com.arialyy.downloadutil.core;
|
package com.arialyy.downloadutil.core;
|
||||||
|
|
||||||
import android.app.Application;
|
import android.app.Application;
|
||||||
@ -23,6 +22,7 @@ import android.util.Log;
|
|||||||
import com.arialyy.downloadutil.core.command.IDownloadCmd;
|
import com.arialyy.downloadutil.core.command.IDownloadCmd;
|
||||||
import com.arialyy.downloadutil.core.queue.ITaskQueue;
|
import com.arialyy.downloadutil.core.queue.ITaskQueue;
|
||||||
import com.arialyy.downloadutil.core.queue.DownloadTaskQueue;
|
import com.arialyy.downloadutil.core.queue.DownloadTaskQueue;
|
||||||
|
import com.arialyy.downloadutil.core.scheduler.OnSchedulerListener;
|
||||||
import com.arialyy.downloadutil.orm.DbEntity;
|
import com.arialyy.downloadutil.orm.DbEntity;
|
||||||
import com.arialyy.downloadutil.orm.DbUtil;
|
import com.arialyy.downloadutil.orm.DbUtil;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -87,6 +87,7 @@ public class DownloadManager {
|
|||||||
private List<IDownloadCmd> mCommands = new ArrayList<>();
|
private List<IDownloadCmd> mCommands = new ArrayList<>();
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private ITaskQueue mTaskQueue;
|
private ITaskQueue mTaskQueue;
|
||||||
|
private DownloadEntity mTempDEntity;
|
||||||
|
|
||||||
private DownloadManager() {
|
private DownloadManager() {
|
||||||
|
|
||||||
@ -123,6 +124,15 @@ public class DownloadManager {
|
|||||||
return DbEntity.findAllData(DownloadEntity.class);
|
return DbEntity.findAllData(DownloadEntity.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册
|
||||||
|
* @param listener
|
||||||
|
*/
|
||||||
|
public DownloadManager regSchedulerListener(OnSchedulerListener listener) {
|
||||||
|
mTaskQueue.getDownloadSchedulers().regTargetListener(listener);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取任务队列
|
* 获取任务队列
|
||||||
*/
|
*/
|
||||||
|
@ -44,6 +44,7 @@ public class CmdFactory {
|
|||||||
* 停止任务
|
* 停止任务
|
||||||
*/
|
*/
|
||||||
public static final int TASK_STOP = 0x125;
|
public static final int TASK_STOP = 0x125;
|
||||||
|
public static final int TASK_SINGLE = 0x126;
|
||||||
|
|
||||||
private static final Object LOCK = new Object();
|
private static final Object LOCK = new Object();
|
||||||
private static volatile CmdFactory INSTANCE = null;
|
private static volatile CmdFactory INSTANCE = null;
|
||||||
@ -77,11 +78,14 @@ public class CmdFactory {
|
|||||||
return createCancelCmd(entity);
|
return createCancelCmd(entity);
|
||||||
case TASK_STOP:
|
case TASK_STOP:
|
||||||
return createStopCmd(entity);
|
return createStopCmd(entity);
|
||||||
|
case TASK_SINGLE:
|
||||||
|
return new SingleCmd(entity);
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建停止命令
|
* 创建停止命令
|
||||||
*
|
*
|
||||||
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.arialyy.downloadutil.core.command;
|
||||||
|
|
||||||
|
import android.util.Log;
|
||||||
|
import com.arialyy.downloadutil.core.DownloadEntity;
|
||||||
|
import com.arialyy.downloadutil.core.task.Task;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by lyy on 2016/11/30.
|
||||||
|
* 获取任务状态命令
|
||||||
|
*/
|
||||||
|
class SingleCmd extends IDownloadCmd {
|
||||||
|
/**
|
||||||
|
* @param entity 下载实体
|
||||||
|
*/
|
||||||
|
SingleCmd(DownloadEntity entity) {
|
||||||
|
super(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public void executeCmd() {
|
||||||
|
Task task = mQueue.getTask(mEntity);
|
||||||
|
if (task == null) {
|
||||||
|
task = mQueue.createTask(mEntity);
|
||||||
|
} else {
|
||||||
|
Log.w(TAG, "添加命令执行失败,【该任务已经存在】");
|
||||||
|
}
|
||||||
|
mQueue.startTask(task);
|
||||||
|
}
|
||||||
|
}
|
@ -111,6 +111,10 @@ public class DownloadTaskQueue implements ITaskQueue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override public IDownloadSchedulers getDownloadSchedulers() {
|
||||||
|
return mSchedulers;
|
||||||
|
}
|
||||||
|
|
||||||
@Override public int size() {
|
@Override public int size() {
|
||||||
return mExecutePool.size();
|
return mExecutePool.size();
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,11 @@ import com.arialyy.downloadutil.core.task.Task;
|
|||||||
*/
|
*/
|
||||||
public interface ITaskQueue extends IDownloader {
|
public interface ITaskQueue extends IDownloader {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取调度器
|
||||||
|
*/
|
||||||
|
public IDownloadSchedulers getDownloadSchedulers();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 任务池队列大小
|
* 任务池队列大小
|
||||||
*/
|
*/
|
||||||
|
@ -14,7 +14,6 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
package com.arialyy.downloadutil.core.scheduler;
|
package com.arialyy.downloadutil.core.scheduler;
|
||||||
|
|
||||||
import android.os.Message;
|
import android.os.Message;
|
||||||
@ -50,6 +49,10 @@ public class DownloadSchedulers implements IDownloadSchedulers {
|
|||||||
* 任务完成
|
* 任务完成
|
||||||
*/
|
*/
|
||||||
public static final int COMPLETE = 5;
|
public static final int COMPLETE = 5;
|
||||||
|
/**
|
||||||
|
* 下载中
|
||||||
|
*/
|
||||||
|
public static final int RUNNING = 6;
|
||||||
private static final String TAG = "DownloadSchedulers";
|
private static final String TAG = "DownloadSchedulers";
|
||||||
private static final Object LOCK = new Object();
|
private static final Object LOCK = new Object();
|
||||||
private static volatile DownloadSchedulers INSTANCE = null;
|
private static volatile DownloadSchedulers INSTANCE = null;
|
||||||
@ -66,7 +69,7 @@ public class DownloadSchedulers implements IDownloadSchedulers {
|
|||||||
/**
|
/**
|
||||||
* 下载器任务监听
|
* 下载器任务监听
|
||||||
*/
|
*/
|
||||||
OnTargetListener mTargetListener;
|
OnSchedulerListener mTargetListener;
|
||||||
ITaskQueue mQueue;
|
ITaskQueue mQueue;
|
||||||
|
|
||||||
public DownloadSchedulers(ITaskQueue downloadTaskQueue) {
|
public DownloadSchedulers(ITaskQueue downloadTaskQueue) {
|
||||||
@ -116,6 +119,9 @@ public class DownloadSchedulers implements IDownloadSchedulers {
|
|||||||
if (mTargetListener != null) {
|
if (mTargetListener != null) {
|
||||||
Task task = mQueue.getTask(entity);
|
Task task = mQueue.getTask(entity);
|
||||||
switch (state) {
|
switch (state) {
|
||||||
|
case RUNNING:
|
||||||
|
mTargetListener.onTaskRunning(task);
|
||||||
|
break;
|
||||||
case START:
|
case START:
|
||||||
mTargetListener.onTaskStart(task);
|
mTargetListener.onTaskStart(task);
|
||||||
break;
|
break;
|
||||||
@ -166,15 +172,14 @@ public class DownloadSchedulers implements IDownloadSchedulers {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override public void regTargetListener(OnSchedulerListener targetListener) {
|
||||||
* 设置下载器监听
|
|
||||||
*
|
|
||||||
* @param targetListener {@link OnTargetListener}
|
|
||||||
*/
|
|
||||||
public void setOnTargetListener(OnTargetListener targetListener) {
|
|
||||||
this.mTargetListener = targetListener;
|
this.mTargetListener = targetListener;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override public void unRegTargetListener(OnSchedulerListener targetListener) {
|
||||||
|
mTargetListener = null;
|
||||||
|
}
|
||||||
|
|
||||||
public void setFailNum(int mFailNum) {
|
public void setFailNum(int mFailNum) {
|
||||||
this.mFailNum = mFailNum;
|
this.mFailNum = mFailNum;
|
||||||
}
|
}
|
||||||
@ -182,34 +187,4 @@ public class DownloadSchedulers implements IDownloadSchedulers {
|
|||||||
public void setTimeOut(long timeOut) {
|
public void setTimeOut(long timeOut) {
|
||||||
this.mTimeOut = timeOut;
|
this.mTimeOut = timeOut;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Target处理任务监听
|
|
||||||
*/
|
|
||||||
public interface OnTargetListener {
|
|
||||||
/**
|
|
||||||
* 任务开始
|
|
||||||
*/
|
|
||||||
public void onTaskStart(Task task);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 任务停止
|
|
||||||
*/
|
|
||||||
public void onTaskStop(Task task);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 任务取消
|
|
||||||
*/
|
|
||||||
public void onTaskCancel(Task task);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 任务下载失败
|
|
||||||
*/
|
|
||||||
public void onTaskFail(Task task);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 任务完成
|
|
||||||
*/
|
|
||||||
public void onTaskComplete(Task task);
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -14,7 +14,6 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
package com.arialyy.downloadutil.core.scheduler;
|
package com.arialyy.downloadutil.core.scheduler;
|
||||||
|
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
@ -26,6 +25,18 @@ import com.arialyy.downloadutil.core.DownloadEntity;
|
|||||||
*/
|
*/
|
||||||
public interface IDownloadSchedulers extends Handler.Callback {
|
public interface IDownloadSchedulers extends Handler.Callback {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册下载器监听
|
||||||
|
*
|
||||||
|
* @param targetListener {@link OnSchedulerListener}
|
||||||
|
*/
|
||||||
|
public void regTargetListener(OnSchedulerListener targetListener);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 取消注册监听器
|
||||||
|
*/
|
||||||
|
public void unRegTargetListener(OnSchedulerListener targetListener);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理下载任务下载失败的情形
|
* 处理下载任务下载失败的情形
|
||||||
*
|
*
|
||||||
|
@ -0,0 +1,38 @@
|
|||||||
|
package com.arialyy.downloadutil.core.scheduler;
|
||||||
|
|
||||||
|
import com.arialyy.downloadutil.core.task.Task;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Target处理任务监听
|
||||||
|
*/
|
||||||
|
public interface OnSchedulerListener {
|
||||||
|
/**
|
||||||
|
* 任务开始
|
||||||
|
*/
|
||||||
|
public void onTaskStart(Task task);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 任务停止
|
||||||
|
*/
|
||||||
|
public void onTaskStop(Task task);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 任务取消
|
||||||
|
*/
|
||||||
|
public void onTaskCancel(Task task);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 任务下载失败
|
||||||
|
*/
|
||||||
|
public void onTaskFail(Task task);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 任务完成
|
||||||
|
*/
|
||||||
|
public void onTaskComplete(Task task);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 任务执行中
|
||||||
|
*/
|
||||||
|
public void onTaskRunning(Task task);
|
||||||
|
}
|
@ -257,6 +257,7 @@ public class Task {
|
|||||||
}
|
}
|
||||||
downloadEntity.setCurrentProgress(currentLocation);
|
downloadEntity.setCurrentProgress(currentLocation);
|
||||||
lastLen = currentLocation;
|
lastLen = currentLocation;
|
||||||
|
sendInState2Target(DownloadSchedulers.RUNNING);
|
||||||
context.sendBroadcast(sendIntent);
|
context.sendBroadcast(sendIntent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,10 +17,8 @@
|
|||||||
|
|
||||||
package com.arialyy.downloadutil.util;
|
package com.arialyy.downloadutil.util;
|
||||||
|
|
||||||
import android.content.res.Resources;
|
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import com.arialyy.downloadutil.R;
|
|
||||||
import com.arialyy.downloadutil.core.DownloadEntity;
|
import com.arialyy.downloadutil.core.DownloadEntity;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
@ -39,16 +37,16 @@ public class CheckUtil {
|
|||||||
*/
|
*/
|
||||||
public static boolean checkDownloadEntity(DownloadEntity entity) {
|
public static boolean checkDownloadEntity(DownloadEntity entity) {
|
||||||
if (entity == null) {
|
if (entity == null) {
|
||||||
Log.w(TAG, Resources.getSystem().getString(R.string.error_entity_null));
|
Log.w(TAG, "下载实体不能为空");
|
||||||
return false;
|
return false;
|
||||||
} else if (TextUtils.isEmpty(entity.getDownloadUrl())) {
|
} else if (TextUtils.isEmpty(entity.getDownloadUrl())) {
|
||||||
Log.w(TAG, Resources.getSystem().getString(R.string.error_download_url_null));
|
Log.w(TAG, "下载链接不能为空");
|
||||||
return false;
|
return false;
|
||||||
} else if (TextUtils.isEmpty(entity.getFileName())) {
|
} else if (TextUtils.isEmpty(entity.getFileName())) {
|
||||||
Log.w(TAG, Resources.getSystem().getString(R.string.error_file_name_null));
|
Log.w(TAG, "文件名不能为空");
|
||||||
return false;
|
return false;
|
||||||
} else if (TextUtils.isEmpty(entity.getDownloadPath())) {
|
} else if (TextUtils.isEmpty(entity.getDownloadPath())) {
|
||||||
Log.w(TAG, Resources.getSystem().getString(R.string.error_download_path_null));
|
Log.w(TAG, "存储地址不能为空");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
String fileName = entity.getFileName();
|
String fileName = entity.getFileName();
|
||||||
|
@ -251,6 +251,8 @@ public class CommonUtil {
|
|||||||
public static Properties loadConfig(File file) {
|
public static Properties loadConfig(File file) {
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
FileInputStream fis = null;
|
FileInputStream fis = null;
|
||||||
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fis = new FileInputStream(file);
|
fis = new FileInputStream(file);
|
||||||
properties.load(fis);
|
properties.load(fis);
|
||||||
|
Reference in New Issue
Block a user