parent
7ed5d7f449
commit
0279bd019b
@ -1 +0,0 @@ |
||||
DownloadDemo |
@ -1,30 +1,20 @@ |
||||
package com.arialyy.downloadutil.core.command; |
||||
|
||||
import android.support.annotation.NonNull; |
||||
|
||||
import com.arialyy.downloadutil.core.IDownloadTarget; |
||||
import android.content.Context; |
||||
import com.arialyy.downloadutil.entity.DownloadEntity; |
||||
|
||||
/** |
||||
* Created by lyy on 2016/8/22. |
||||
* 添加任务的命令 |
||||
*/ |
||||
public class AddCommand extends IDownloadCommand { |
||||
String mDownloadUrl, mDownloadPath; |
||||
class AddCommand extends IDownloadCommand { |
||||
|
||||
/** |
||||
* |
||||
* @param target 下载调度器 |
||||
* @param downloadUrl 下载链接 |
||||
* @param downloadPath 文件保存地址 |
||||
*/ |
||||
public AddCommand(@NonNull IDownloadTarget target, String downloadUrl, String downloadPath) { |
||||
super(target); |
||||
mDownloadUrl = downloadUrl; |
||||
mDownloadPath = downloadPath; |
||||
AddCommand(Context context, DownloadEntity entity) { |
||||
super(context, entity); |
||||
} |
||||
|
||||
@Override |
||||
public void executeComment() { |
||||
target.createTask(mDownloadUrl, mDownloadPath); |
||||
target.createTask(mEntity); |
||||
} |
||||
} |
||||
|
@ -1,18 +1,19 @@ |
||||
package com.arialyy.downloadutil.core.command; |
||||
|
||||
import com.arialyy.downloadutil.core.IDownloadTarget; |
||||
import android.content.Context; |
||||
import com.arialyy.downloadutil.entity.DownloadEntity; |
||||
|
||||
/** |
||||
* Created by lyy on 2016/9/20. |
||||
* 取消命令 |
||||
*/ |
||||
public class CancelCommand extends IDownloadCommand{ |
||||
public CancelCommand(IDownloadTarget target) { |
||||
super(target); |
||||
} |
||||
public class CancelCommand extends IDownloadCommand { |
||||
|
||||
@Override |
||||
public void executeComment() { |
||||
protected CancelCommand(Context context, DownloadEntity entity) { |
||||
super(context, entity); |
||||
} |
||||
|
||||
} |
||||
@Override public void executeComment() { |
||||
target.cancelTask(target.getTask(mEntity)); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,115 @@ |
||||
package com.arialyy.downloadutil.core.command; |
||||
|
||||
import android.content.Context; |
||||
import com.arialyy.downloadutil.entity.DownloadEntity; |
||||
|
||||
/** |
||||
* Created by Lyy on 2016/9/23. |
||||
* 命令工厂 |
||||
*/ |
||||
public class CommandFactory { |
||||
/** |
||||
* 创建任务 |
||||
*/ |
||||
public static final int TASK_CREATE = 0x122; |
||||
/** |
||||
* 启动任务 |
||||
*/ |
||||
public static final int TASK_START = 0x123; |
||||
/** |
||||
* 取消任务 |
||||
*/ |
||||
public static final int TASK_CANCEL = 0x124; |
||||
/** |
||||
* 停止任务 |
||||
*/ |
||||
public static final int TASK_STOP = 0x125; |
||||
/** |
||||
* 获取任务状态 |
||||
*/ |
||||
public static final int TASK_STATE = 0x126; |
||||
|
||||
private static final Object LOCK = new Object(); |
||||
private static volatile CommandFactory INSTANCE = null; |
||||
|
||||
private CommandFactory() { |
||||
|
||||
} |
||||
|
||||
public static CommandFactory getInstance() { |
||||
if (INSTANCE == null) { |
||||
synchronized (LOCK) { |
||||
INSTANCE = new CommandFactory(); |
||||
} |
||||
} |
||||
return INSTANCE; |
||||
} |
||||
|
||||
/** |
||||
* @param context context |
||||
* @param entity 下载实体 |
||||
* @param type 命令类型{@link #TASK_CREATE}、{@link #TASK_START}、{@link #TASK_CANCEL}、{@link |
||||
* #TASK_STOP}、{@link #TASK_STATE} |
||||
*/ |
||||
public IDownloadCommand createCommand(Context context, DownloadEntity entity, int type) { |
||||
switch (type) { |
||||
case TASK_CREATE: |
||||
return createAddCommand(context, entity); |
||||
case TASK_START: |
||||
return createStartCommand(context, entity); |
||||
case TASK_CANCEL: |
||||
return createCancelCommand(context, entity); |
||||
case TASK_STOP: |
||||
return createStopCommand(context, entity); |
||||
case TASK_STATE: |
||||
return createStateCommand(context, entity); |
||||
default: |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 创建获取任务状态的命令 |
||||
* |
||||
* @return {@link StateCommand} |
||||
*/ |
||||
private StateCommand createStateCommand(Context context, DownloadEntity entity) { |
||||
return new StateCommand(context, entity); |
||||
} |
||||
|
||||
/** |
||||
* 创建停止命令 |
||||
* |
||||
* @return {@link StopCommand} |
||||
*/ |
||||
private StopCommand createStopCommand(Context context, DownloadEntity entity) { |
||||
return new StopCommand(context, entity); |
||||
} |
||||
|
||||
/** |
||||
* 创建下载任务命令 |
||||
* |
||||
* @return {@link AddCommand} |
||||
*/ |
||||
private AddCommand createAddCommand(Context context, DownloadEntity entity) { |
||||
return new AddCommand(context, entity); |
||||
} |
||||
|
||||
/** |
||||
* 创建启动下载命令 |
||||
* |
||||
* @return {@link StartCommand} |
||||
*/ |
||||
private StartCommand createStartCommand(Context context, DownloadEntity entity) { |
||||
return new StartCommand(context, entity); |
||||
} |
||||
|
||||
/** |
||||
* 创建 取消下载的命令 |
||||
* |
||||
* @return {@link CancelCommand} |
||||
*/ |
||||
private CancelCommand createCancelCommand(Context context, DownloadEntity entity) { |
||||
return new CancelCommand(context, entity); |
||||
} |
||||
} |
@ -1,18 +0,0 @@ |
||||
package com.arialyy.downloadutil.core.command; |
||||
|
||||
import com.arialyy.downloadutil.core.IDownloadTarget; |
||||
|
||||
/** |
||||
* Created by lyy on 2016/9/20. |
||||
* 获取下载状态的命令 |
||||
*/ |
||||
public class GetStateCommand extends IDownloadCommand { |
||||
public GetStateCommand(IDownloadTarget target) { |
||||
super(target); |
||||
} |
||||
|
||||
@Override |
||||
public void executeComment() { |
||||
|
||||
} |
||||
} |
@ -1,21 +1,20 @@ |
||||
package com.arialyy.downloadutil.core.command; |
||||
|
||||
import android.support.annotation.NonNull; |
||||
|
||||
import com.arialyy.downloadutil.core.IDownloadTarget; |
||||
import com.arialyy.downloadutil.core.Task; |
||||
import android.content.Context; |
||||
import com.arialyy.downloadutil.entity.DownloadEntity; |
||||
|
||||
/** |
||||
* Created by lyy on 2016/8/22. |
||||
* 开始命令 |
||||
*/ |
||||
public class StartCommand extends IDownloadCommand{ |
||||
public StartCommand(@NonNull IDownloadTarget target) { |
||||
super(target); |
||||
class StartCommand extends IDownloadCommand{ |
||||
|
||||
StartCommand(Context context, DownloadEntity entity) { |
||||
super(context, entity); |
||||
} |
||||
|
||||
@Override |
||||
public void executeComment() { |
||||
// target.startTask();
|
||||
target.startTask(target.getTask(mEntity)); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,23 @@ |
||||
package com.arialyy.downloadutil.core.command; |
||||
|
||||
import android.content.Context; |
||||
import com.arialyy.downloadutil.entity.DownloadEntity; |
||||
|
||||
/** |
||||
* Created by lyy on 2016/9/20. |
||||
* 获取下载状态的命令 |
||||
*/ |
||||
public class StateCommand extends IDownloadCommand { |
||||
|
||||
/** |
||||
* @param context context |
||||
* @param entity 下载实体 |
||||
*/ |
||||
protected StateCommand(Context context, DownloadEntity entity) { |
||||
super(context, entity); |
||||
} |
||||
|
||||
@Override public void executeComment() { |
||||
target.getTaskState(mEntity); |
||||
} |
||||
} |
@ -1,18 +1,23 @@ |
||||
package com.arialyy.downloadutil.core.command; |
||||
|
||||
import com.arialyy.downloadutil.core.IDownloadTarget; |
||||
import android.content.Context; |
||||
import com.arialyy.downloadutil.entity.DownloadEntity; |
||||
|
||||
/** |
||||
* Created by lyy on 2016/9/20. |
||||
* 停止命令 |
||||
*/ |
||||
public class StopCommand extends IDownloadCommand { |
||||
public StopCommand(IDownloadTarget target) { |
||||
super(target); |
||||
} |
||||
|
||||
@Override |
||||
public void executeComment() { |
||||
/** |
||||
* @param context context |
||||
* @param entity 下载实体 |
||||
*/ |
||||
protected StopCommand(Context context, DownloadEntity entity) { |
||||
super(context, entity); |
||||
} |
||||
|
||||
} |
||||
@Override public void executeComment() { |
||||
target.stopTask(target.getTask(mEntity)); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,50 @@ |
||||
package com.arialyy.downloadutil.help; |
||||
|
||||
import android.app.Application; |
||||
import android.content.res.Resources; |
||||
import android.text.TextUtils; |
||||
import android.util.Log; |
||||
import com.arialyy.downloadutil.R; |
||||
import com.arialyy.downloadutil.entity.DownloadEntity; |
||||
import java.io.File; |
||||
|
||||
/** |
||||
* Created by Lyy on 2016/9/23. |
||||
* 检查帮助类 |
||||
*/ |
||||
public class CheckHelp { |
||||
private static final String TAG = "CheckHelp"; |
||||
|
||||
/** |
||||
* 检测下载实体是否合法 |
||||
* |
||||
* @param entity 下载实体 |
||||
* @return 合法(true) |
||||
*/ |
||||
public static boolean checkDownloadEntity(DownloadEntity entity) { |
||||
if (entity == null) { |
||||
Log.w(TAG, Resources.getSystem().getString(R.string.error_entity_null)); |
||||
return false; |
||||
} else if (TextUtils.isEmpty(entity.getDownloadUrl())) { |
||||
Log.w(TAG, Resources.getSystem().getString(R.string.error_download_url_null)); |
||||
return false; |
||||
} else if (TextUtils.isEmpty(entity.getFileName())){ |
||||
Log.w(TAG, Resources.getSystem().getString(R.string.error_file_name_null)); |
||||
return false; |
||||
} else if (TextUtils.isEmpty(entity.getDownloadPath())){ |
||||
Log.w(TAG, Resources.getSystem().getString(R.string.error_file_name_null)); |
||||
return false; |
||||
} |
||||
String fileName = entity.getFileName(); |
||||
if (fileName.contains(" ")){ |
||||
fileName = fileName.replace(" ", "_"); |
||||
} |
||||
String dPath = entity.getDownloadPath(); |
||||
File file = new File(dPath); |
||||
if (file.isDirectory()){ |
||||
dPath += fileName; |
||||
entity.setDownloadPath(dPath); |
||||
} |
||||
return true; |
||||
} |
||||
} |
@ -1,3 +1,8 @@ |
||||
<resources> |
||||
<string name="app_name">DownloadUtil</string> |
||||
|
||||
<string name="error_entity_null">下载实体不能为空</string> |
||||
<string name="error_download_url_null">下载链接不能为空</string> |
||||
<string name="error_download_path_null">存储地址不能为空</string> |
||||
<string name="error_file_name_null">文件名不能为空</string> |
||||
</resources> |
||||
|
Loading…
Reference in new issue