没做啥
This commit is contained in:
8
.idea/gradle.xml
generated
8
.idea/gradle.xml
generated
@ -13,7 +13,13 @@
|
||||
<option value="$PROJECT_DIR$/downloadutil" />
|
||||
</set>
|
||||
</option>
|
||||
<option name="resolveModulePerSourceSet" value="false" />
|
||||
<option name="myModules">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
<option value="$PROJECT_DIR$/app" />
|
||||
<option value="$PROJECT_DIR$/downloadutil" />
|
||||
</set>
|
||||
</option>
|
||||
</GradleProjectSettings>
|
||||
</option>
|
||||
</component>
|
||||
|
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@ -53,7 +53,7 @@
|
||||
<ConfirmationsSetting value="0" id="Add" />
|
||||
<ConfirmationsSetting value="0" id="Remove" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||
</component>
|
||||
<component name="ProjectType">
|
||||
|
1
.idea/modules.xml
generated
1
.idea/modules.xml
generated
@ -3,7 +3,6 @@
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/DownloadDemo.iml" filepath="$PROJECT_DIR$/DownloadDemo.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/DownloadUtil.iml" filepath="$PROJECT_DIR$/DownloadUtil.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/downloadutil/downloadutil.iml" filepath="$PROJECT_DIR$/downloadutil/downloadutil.iml" />
|
||||
</modules>
|
||||
|
@ -1,9 +0,0 @@
|
||||
package com.arialyy.downloadutil.core;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2016/8/14.
|
||||
* 命令抽象类
|
||||
*/
|
||||
public abstract class DownloadCommand {
|
||||
|
||||
}
|
@ -2,11 +2,18 @@ package com.arialyy.downloadutil.core;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.arialyy.downloadutil.core.command.IDownloadCommand;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2016/8/11.
|
||||
* 下载管理器,通过命令的方式控制下载
|
||||
*/
|
||||
public class DownloadManager {
|
||||
private static final Object LOCK = new Object();
|
||||
private static volatile DownloadManager INSTANCE = null;
|
||||
/**
|
||||
* 下载开始前事件
|
||||
*/
|
||||
@ -57,6 +64,12 @@ public class DownloadManager {
|
||||
*/
|
||||
public static final String CURRENT_LOCATION = "CURRENT_LOCATION";
|
||||
|
||||
private List<IDownloadCommand> mCommands = new ArrayList<>();
|
||||
|
||||
private DownloadManager() {
|
||||
|
||||
}
|
||||
|
||||
private Context mContext;
|
||||
|
||||
private DownloadManager(Context context) {
|
||||
@ -64,7 +77,42 @@ public class DownloadManager {
|
||||
}
|
||||
|
||||
public static DownloadManager getInstance(Context context) {
|
||||
return new DownloadManager(context);
|
||||
if (INSTANCE == null) {
|
||||
synchronized (LOCK) {
|
||||
INSTANCE = new DownloadManager(context.getApplicationContext());
|
||||
}
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置命令
|
||||
*
|
||||
* @param command
|
||||
*/
|
||||
public void setCommant(IDownloadCommand command) {
|
||||
mCommands.add(command);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置一组命令
|
||||
*
|
||||
* @param commands
|
||||
*/
|
||||
public void setCommands(List<IDownloadCommand> commands) {
|
||||
if (commands != null && commands.size() > 0) {
|
||||
mCommands.addAll(commands);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行所有设置的命令
|
||||
*/
|
||||
public synchronized void exe() {
|
||||
for (IDownloadCommand command : mCommands) {
|
||||
command.executeComment();
|
||||
}
|
||||
mCommands.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -77,7 +77,7 @@ public class DownloadTarget extends IDownloadTarget {
|
||||
if (task == null) {
|
||||
task = mCachePool.getTask(entity.getDownloadUrl());
|
||||
}
|
||||
if (task == null){
|
||||
if (task == null) {
|
||||
task = createTask(entity);
|
||||
}
|
||||
return task;
|
||||
|
@ -30,6 +30,7 @@ public class TaskFactory {
|
||||
|
||||
/**
|
||||
* 创建普通下载任务
|
||||
*
|
||||
* @param context
|
||||
* @param entity 下载实体
|
||||
* @param handler {@link com.arialyy.downloadutil.core.IDownloadTarget.AutoTaskHandler}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.arialyy.downloadutil.core.command;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
|
||||
/**
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.arialyy.downloadutil.core.command;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
|
||||
/**
|
||||
@ -13,7 +14,8 @@ class CancelCommand extends IDownloadCommand {
|
||||
super(context, entity);
|
||||
}
|
||||
|
||||
@Override public void executeComment() {
|
||||
@Override
|
||||
public void executeComment() {
|
||||
target.cancelTask(target.getTask(mEntity));
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.arialyy.downloadutil.core.command;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
|
||||
/**
|
||||
|
@ -8,6 +8,7 @@ import com.arialyy.downloadutil.core.IDownloadTarget;
|
||||
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
import com.arialyy.downloadutil.help.CheckHelp;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -23,8 +24,8 @@ public abstract class IDownloadCommand {
|
||||
* @param context context
|
||||
* @param entity 下载实体
|
||||
*/
|
||||
protected IDownloadCommand(Context context, DownloadEntity entity){
|
||||
if (!CheckHelp.checkDownloadEntity(entity)){
|
||||
protected IDownloadCommand(Context context, DownloadEntity entity) {
|
||||
if (!CheckHelp.checkDownloadEntity(entity)) {
|
||||
return;
|
||||
}
|
||||
target = DownloadTarget.getInstance(context);
|
||||
@ -32,7 +33,7 @@ public abstract class IDownloadCommand {
|
||||
mEntity = entity;
|
||||
}
|
||||
|
||||
public Context getContext(){
|
||||
public Context getContext() {
|
||||
return mContext;
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,14 @@
|
||||
package com.arialyy.downloadutil.core.command;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2016/8/22.
|
||||
* 开始命令
|
||||
*/
|
||||
class StartCommand extends IDownloadCommand{
|
||||
class StartCommand extends IDownloadCommand {
|
||||
|
||||
StartCommand(Context context, DownloadEntity entity) {
|
||||
super(context, entity);
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.arialyy.downloadutil.core.command;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
|
||||
/**
|
||||
@ -17,7 +18,8 @@ class StateCommand extends IDownloadCommand {
|
||||
super(context, entity);
|
||||
}
|
||||
|
||||
@Override public void executeComment() {
|
||||
@Override
|
||||
public void executeComment() {
|
||||
target.getTaskState(mEntity);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.arialyy.downloadutil.core.command;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
|
||||
/**
|
||||
@ -17,7 +18,8 @@ class StopCommand extends IDownloadCommand {
|
||||
super(context, entity);
|
||||
}
|
||||
|
||||
@Override public void executeComment() {
|
||||
@Override
|
||||
public void executeComment() {
|
||||
target.stopTask(target.getTask(mEntity));
|
||||
}
|
||||
}
|
||||
|
@ -35,6 +35,7 @@ public interface ITask {
|
||||
|
||||
/**
|
||||
* 通过下载链接删除任务
|
||||
*
|
||||
* @param entity 下载实体{@link DownloadEntity}
|
||||
*/
|
||||
public void removeTask(DownloadEntity entity);
|
||||
|
@ -4,8 +4,10 @@ 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;
|
||||
|
||||
/**
|
||||
@ -28,20 +30,20 @@ public class CheckHelp {
|
||||
} 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())){
|
||||
} 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())){
|
||||
} 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(" ")){
|
||||
if (fileName.contains(" ")) {
|
||||
fileName = fileName.replace(" ", "_");
|
||||
}
|
||||
String dPath = entity.getDownloadPath();
|
||||
File file = new File(dPath);
|
||||
if (file.isDirectory()){
|
||||
if (file.isDirectory()) {
|
||||
dPath += fileName;
|
||||
entity.setDownloadPath(dPath);
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
package com.arialyy.downloadutil.orm;
|
||||
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.support.annotation.NonNull;
|
||||
|
@ -13,6 +13,7 @@ import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2015/8/25.
|
||||
* 下载工具类
|
||||
@ -37,11 +38,14 @@ public class DownLoadUtil {
|
||||
boolean isNewTask = true;
|
||||
private int mCancelNum = 0;
|
||||
private int mStopNum = 0;
|
||||
|
||||
public DownLoadUtil() {
|
||||
}
|
||||
public IDownloadListener getListener(){
|
||||
|
||||
public IDownloadListener getListener() {
|
||||
return mListener;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前下载位置
|
||||
*
|
||||
@ -50,21 +54,25 @@ public class DownLoadUtil {
|
||||
public long getCurrentLocation() {
|
||||
return mCurrentLocation;
|
||||
}
|
||||
|
||||
public boolean isDownloading() {
|
||||
return isDownloading;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消下载
|
||||
*/
|
||||
public void cancelDownload() {
|
||||
isCancel = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止下载
|
||||
*/
|
||||
public void stopDownload() {
|
||||
isStop = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 多线程断点续传下载文件,暂停和继续
|
||||
*
|
||||
@ -176,7 +184,7 @@ public class DownLoadUtil {
|
||||
startL = r;
|
||||
recordL[rl] = i;
|
||||
rl++;
|
||||
}else {
|
||||
} else {
|
||||
isNewTask = true;
|
||||
}
|
||||
if (isNewTask) {
|
||||
@ -211,13 +219,15 @@ public class DownLoadUtil {
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
private void failDownload(String msg){
|
||||
|
||||
private void failDownload(String msg) {
|
||||
Log.e(TAG, msg);
|
||||
isDownloading = false;
|
||||
stopDownload();
|
||||
mListener.onFail();
|
||||
System.gc();
|
||||
}
|
||||
|
||||
/**
|
||||
* 多线程下载任务类,不能使用AsyncTask来进行多线程下载,因为AsyncTask是串行执行的,这种方式下载速度太慢了
|
||||
*/
|
||||
@ -225,10 +235,12 @@ public class DownLoadUtil {
|
||||
private static final String TAG = "DownLoadTask";
|
||||
private DownloadEntity dEntity;
|
||||
private String configFPath;
|
||||
|
||||
public DownLoadTask(DownloadEntity downloadInfo) {
|
||||
this.dEntity = downloadInfo;
|
||||
configFPath = dEntity.context.getFilesDir().getPath() + "/temp/" + dEntity.tempFile.getName() + ".properties";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
long currentLocation = 0;
|
||||
@ -353,6 +365,7 @@ public class DownLoadUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将记录写入到配置文件
|
||||
*
|
||||
@ -365,6 +378,7 @@ public class DownLoadUtil {
|
||||
Util.saveConfig(configFile, pro);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 子线程下载信息类
|
||||
*/
|
||||
@ -377,6 +391,7 @@ public class DownLoadUtil {
|
||||
long endLocation;
|
||||
File tempFile;
|
||||
Context context;
|
||||
|
||||
public DownloadEntity(Context context, long fileSize, String downloadUrl, File file, int threadId, long startLocation, long endLocation) {
|
||||
this.fileSize = fileSize;
|
||||
this.downloadUrl = downloadUrl;
|
||||
|
Reference in New Issue
Block a user