pull/2/head
lyy 9 years ago
parent e586168e30
commit e4e80048e7
  1. 8
      .idea/gradle.xml
  2. 2
      .idea/misc.xml
  3. 1
      .idea/modules.xml
  4. 9
      downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadCommand.java
  5. 50
      downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadManager.java
  6. 2
      downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadTarget.java
  7. 1
      downloadutil/src/main/java/com/arialyy/downloadutil/core/TaskFactory.java
  8. 1
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/AddCommand.java
  9. 4
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/CancelCommand.java
  10. 1
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/CommandFactory.java
  11. 7
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/IDownloadCommand.java
  12. 3
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/StartCommand.java
  13. 4
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/StateCommand.java
  14. 4
      downloadutil/src/main/java/com/arialyy/downloadutil/core/command/StopCommand.java
  15. 1
      downloadutil/src/main/java/com/arialyy/downloadutil/core/inf/ITask.java
  16. 10
      downloadutil/src/main/java/com/arialyy/downloadutil/help/CheckHelp.java
  17. 1
      downloadutil/src/main/java/com/arialyy/downloadutil/orm/DbUtil.java
  18. 21
      downloadutil/src/main/java/com/arialyy/downloadutil/util/DownLoadUtil.java

@ -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>

@ -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">

@ -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>

@ -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 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();
} }
} }

@ -77,7 +77,7 @@ public class DownloadTarget extends IDownloadTarget {
if (task == null) { if (task == null) {
task = mCachePool.getTask(entity.getDownloadUrl()); task = mCachePool.getTask(entity.getDownloadUrl());
} }
if (task == null){ if (task == null) {
task = createTask(entity); task = createTask(entity);
} }
return task; return task;

@ -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}

@ -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;
/** /**

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

@ -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;
/** /**

@ -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;
/** /**
@ -23,8 +24,8 @@ public abstract class IDownloadCommand {
* @param context context * @param context context
* @param entity 下载实体 * @param entity 下载实体
*/ */
protected IDownloadCommand(Context context, DownloadEntity entity){ protected IDownloadCommand(Context context, DownloadEntity entity) {
if (!CheckHelp.checkDownloadEntity(entity)){ if (!CheckHelp.checkDownloadEntity(entity)) {
return; return;
} }
target = DownloadTarget.getInstance(context); target = DownloadTarget.getInstance(context);
@ -32,7 +33,7 @@ public abstract class IDownloadCommand {
mEntity = entity; mEntity = entity;
} }
public Context getContext(){ public Context getContext() {
return mContext; return mContext;
} }

@ -1,13 +1,14 @@
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;
/** /**
* Created by lyy on 2016/8/22. * Created by lyy on 2016/8/22.
* 开始命令 * 开始命令
*/ */
class StartCommand extends IDownloadCommand{ class StartCommand extends IDownloadCommand {
StartCommand(Context context, DownloadEntity entity) { StartCommand(Context context, DownloadEntity entity) {
super(context, entity); super(context, entity);

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

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

@ -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);

@ -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;
/** /**
@ -28,20 +30,20 @@ public class CheckHelp {
} 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, Resources.getSystem().getString(R.string.error_download_url_null));
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, Resources.getSystem().getString(R.string.error_file_name_null));
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_file_name_null)); Log.w(TAG, Resources.getSystem().getString(R.string.error_file_name_null));
return false; return false;
} }
String fileName = entity.getFileName(); String fileName = entity.getFileName();
if (fileName.contains(" ")){ if (fileName.contains(" ")) {
fileName = fileName.replace(" ", "_"); fileName = fileName.replace(" ", "_");
} }
String dPath = entity.getDownloadPath(); String dPath = entity.getDownloadPath();
File file = new File(dPath); File file = new File(dPath);
if (file.isDirectory()){ if (file.isDirectory()) {
dPath += fileName; dPath += fileName;
entity.setDownloadPath(dPath); entity.setDownloadPath(dPath);
} }

@ -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;

@ -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;
} }
/** /**
* 多线程断点续传下载文件暂停和继续 * 多线程断点续传下载文件暂停和继续
* *
@ -176,7 +184,7 @@ public class DownLoadUtil {
startL = r; startL = r;
recordL[rl] = i; recordL[rl] = i;
rl++; rl++;
}else { } else {
isNewTask = true; isNewTask = true;
} }
if (isNewTask) { if (isNewTask) {
@ -211,13 +219,15 @@ 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;
stopDownload(); stopDownload();
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;

Loading…
Cancel
Save