没做啥

This commit is contained in:
lyy
2016-09-26 22:36:30 +08:00
parent e586168e30
commit e4e80048e7
20 changed files with 305 additions and 231 deletions

8
.idea/gradle.xml generated
View File

@ -13,7 +13,13 @@
<option value="$PROJECT_DIR$/downloadutil" /> <option value="$PROJECT_DIR$/downloadutil" />
</set> </set>
</option> </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> </GradleProjectSettings>
</option> </option>
</component> </component>

2
.idea/misc.xml generated
View File

@ -53,7 +53,7 @@
<ConfirmationsSetting value="0" id="Add" /> <ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" /> <ConfirmationsSetting value="0" id="Remove" />
</component> </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" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">

1
.idea/modules.xml generated
View File

@ -3,7 +3,6 @@
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/DownloadDemo.iml" filepath="$PROJECT_DIR$/DownloadDemo.iml" /> <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$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
<module fileurl="file://$PROJECT_DIR$/downloadutil/downloadutil.iml" filepath="$PROJECT_DIR$/downloadutil/downloadutil.iml" /> <module fileurl="file://$PROJECT_DIR$/downloadutil/downloadutil.iml" filepath="$PROJECT_DIR$/downloadutil/downloadutil.iml" />
</modules> </modules>

View File

@ -1,9 +0,0 @@
package com.arialyy.downloadutil.core;
/**
* Created by lyy on 2016/8/14.
* 命令抽象类
*/
public abstract class DownloadCommand {
}

View File

@ -2,11 +2,18 @@ package com.arialyy.downloadutil.core;
import android.content.Context; 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. * Created by lyy on 2016/8/11.
* 下载管理器,通过命令的方式控制下载 * 下载管理器,通过命令的方式控制下载
*/ */
public class DownloadManager { 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"; public static final String CURRENT_LOCATION = "CURRENT_LOCATION";
private List<IDownloadCommand> mCommands = new ArrayList<>();
private DownloadManager() {
}
private Context mContext; private Context mContext;
private DownloadManager(Context context) { private DownloadManager(Context context) {
@ -64,7 +77,42 @@ public class DownloadManager {
} }
public static DownloadManager getInstance(Context context) { 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();
} }
} }

View File

@ -30,6 +30,7 @@ public class TaskFactory {
/** /**
* 创建普通下载任务 * 创建普通下载任务
*
* @param context * @param context
* @param entity 下载实体 * @param entity 下载实体
* @param handler {@link com.arialyy.downloadutil.core.IDownloadTarget.AutoTaskHandler} * @param handler {@link com.arialyy.downloadutil.core.IDownloadTarget.AutoTaskHandler}

View File

@ -1,6 +1,7 @@
package com.arialyy.downloadutil.core.command; package com.arialyy.downloadutil.core.command;
import android.content.Context; import android.content.Context;
import com.arialyy.downloadutil.entity.DownloadEntity; import com.arialyy.downloadutil.entity.DownloadEntity;
/** /**

View File

@ -1,6 +1,7 @@
package com.arialyy.downloadutil.core.command; package com.arialyy.downloadutil.core.command;
import android.content.Context; import android.content.Context;
import com.arialyy.downloadutil.entity.DownloadEntity; import com.arialyy.downloadutil.entity.DownloadEntity;
/** /**
@ -13,7 +14,8 @@ class CancelCommand extends IDownloadCommand {
super(context, entity); super(context, entity);
} }
@Override public void executeComment() { @Override
public void executeComment() {
target.cancelTask(target.getTask(mEntity)); target.cancelTask(target.getTask(mEntity));
} }
} }

View File

@ -1,6 +1,7 @@
package com.arialyy.downloadutil.core.command; package com.arialyy.downloadutil.core.command;
import android.content.Context; import android.content.Context;
import com.arialyy.downloadutil.entity.DownloadEntity; import com.arialyy.downloadutil.entity.DownloadEntity;
/** /**

View File

@ -8,6 +8,7 @@ import com.arialyy.downloadutil.core.IDownloadTarget;
import com.arialyy.downloadutil.entity.DownloadEntity; import com.arialyy.downloadutil.entity.DownloadEntity;
import com.arialyy.downloadutil.help.CheckHelp; import com.arialyy.downloadutil.help.CheckHelp;
import java.util.List; import java.util.List;
/** /**

View File

@ -1,6 +1,7 @@
package com.arialyy.downloadutil.core.command; package com.arialyy.downloadutil.core.command;
import android.content.Context; import android.content.Context;
import com.arialyy.downloadutil.entity.DownloadEntity; import com.arialyy.downloadutil.entity.DownloadEntity;
/** /**

View File

@ -1,6 +1,7 @@
package com.arialyy.downloadutil.core.command; package com.arialyy.downloadutil.core.command;
import android.content.Context; import android.content.Context;
import com.arialyy.downloadutil.entity.DownloadEntity; import com.arialyy.downloadutil.entity.DownloadEntity;
/** /**
@ -17,7 +18,8 @@ class StateCommand extends IDownloadCommand {
super(context, entity); super(context, entity);
} }
@Override public void executeComment() { @Override
public void executeComment() {
target.getTaskState(mEntity); target.getTaskState(mEntity);
} }
} }

View File

@ -1,6 +1,7 @@
package com.arialyy.downloadutil.core.command; package com.arialyy.downloadutil.core.command;
import android.content.Context; import android.content.Context;
import com.arialyy.downloadutil.entity.DownloadEntity; import com.arialyy.downloadutil.entity.DownloadEntity;
/** /**
@ -17,7 +18,8 @@ class StopCommand extends IDownloadCommand {
super(context, entity); super(context, entity);
} }
@Override public void executeComment() { @Override
public void executeComment() {
target.stopTask(target.getTask(mEntity)); target.stopTask(target.getTask(mEntity));
} }
} }

View File

@ -35,6 +35,7 @@ public interface ITask {
/** /**
* 通过下载链接删除任务 * 通过下载链接删除任务
*
* @param entity 下载实体{@link DownloadEntity} * @param entity 下载实体{@link DownloadEntity}
*/ */
public void removeTask(DownloadEntity entity); public void removeTask(DownloadEntity entity);

View File

@ -4,8 +4,10 @@ import android.app.Application;
import android.content.res.Resources; 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.R;
import com.arialyy.downloadutil.entity.DownloadEntity; import com.arialyy.downloadutil.entity.DownloadEntity;
import java.io.File; import java.io.File;
/** /**

View File

@ -1,4 +1,5 @@
package com.arialyy.downloadutil.orm; package com.arialyy.downloadutil.orm;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;

View File

@ -13,6 +13,7 @@ import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.Properties; import java.util.Properties;
/** /**
* Created by lyy on 2015/8/25. * Created by lyy on 2015/8/25.
* 下载工具类 * 下载工具类
@ -37,11 +38,14 @@ public class DownLoadUtil {
boolean isNewTask = true; boolean isNewTask = true;
private int mCancelNum = 0; private int mCancelNum = 0;
private int mStopNum = 0; private int mStopNum = 0;
public DownLoadUtil() { public DownLoadUtil() {
} }
public IDownloadListener getListener() { public IDownloadListener getListener() {
return mListener; return mListener;
} }
/** /**
* 获取当前下载位置 * 获取当前下载位置
* *
@ -50,21 +54,25 @@ public class DownLoadUtil {
public long getCurrentLocation() { public long getCurrentLocation() {
return mCurrentLocation; return mCurrentLocation;
} }
public boolean isDownloading() { public boolean isDownloading() {
return isDownloading; return isDownloading;
} }
/** /**
* 取消下载 * 取消下载
*/ */
public void cancelDownload() { public void cancelDownload() {
isCancel = true; isCancel = true;
} }
/** /**
* 停止下载 * 停止下载
*/ */
public void stopDownload() { public void stopDownload() {
isStop = true; isStop = true;
} }
/** /**
* 多线程断点续传下载文件,暂停和继续 * 多线程断点续传下载文件,暂停和继续
* *
@ -211,6 +219,7 @@ public class DownLoadUtil {
} }
}).start(); }).start();
} }
private void failDownload(String msg) { private void failDownload(String msg) {
Log.e(TAG, msg); Log.e(TAG, msg);
isDownloading = false; isDownloading = false;
@ -218,6 +227,7 @@ public class DownLoadUtil {
mListener.onFail(); mListener.onFail();
System.gc(); System.gc();
} }
/** /**
* 多线程下载任务类,不能使用AsyncTask来进行多线程下载因为AsyncTask是串行执行的这种方式下载速度太慢了 * 多线程下载任务类,不能使用AsyncTask来进行多线程下载因为AsyncTask是串行执行的这种方式下载速度太慢了
*/ */
@ -225,10 +235,12 @@ public class DownLoadUtil {
private static final String TAG = "DownLoadTask"; private static final String TAG = "DownLoadTask";
private DownloadEntity dEntity; private DownloadEntity dEntity;
private String configFPath; private String configFPath;
public DownLoadTask(DownloadEntity downloadInfo) { public DownLoadTask(DownloadEntity downloadInfo) {
this.dEntity = downloadInfo; this.dEntity = downloadInfo;
configFPath = dEntity.context.getFilesDir().getPath() + "/temp/" + dEntity.tempFile.getName() + ".properties"; configFPath = dEntity.context.getFilesDir().getPath() + "/temp/" + dEntity.tempFile.getName() + ".properties";
} }
@Override @Override
public void run() { public void run() {
long currentLocation = 0; long currentLocation = 0;
@ -353,6 +365,7 @@ public class DownLoadUtil {
} }
} }
} }
/** /**
* 将记录写入到配置文件 * 将记录写入到配置文件
* *
@ -365,6 +378,7 @@ public class DownLoadUtil {
Util.saveConfig(configFile, pro); Util.saveConfig(configFile, pro);
} }
} }
/** /**
* 子线程下载信息类 * 子线程下载信息类
*/ */
@ -377,6 +391,7 @@ public class DownLoadUtil {
long endLocation; long endLocation;
File tempFile; File tempFile;
Context context; Context context;
public DownloadEntity(Context context, long fileSize, String downloadUrl, File file, int threadId, long startLocation, long endLocation) { public DownloadEntity(Context context, long fileSize, String downloadUrl, File file, int threadId, long startLocation, long endLocation) {
this.fileSize = fileSize; this.fileSize = fileSize;
this.downloadUrl = downloadUrl; this.downloadUrl = downloadUrl;