From bba0ccbdb18d34a908557ac546ede41796629c43 Mon Sep 17 00:00:00 2001
From: lyy <511455842@qq.com>
Date: Thu, 11 Aug 2016 21:52:30 +0800
Subject: [PATCH] =?UTF-8?q?2.0=E7=89=88=E6=9C=AC=E5=BC=80=E5=8F=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.idea/misc.xml | 2 +-
.../arial/downloaddemo/MainActivity.java | 8 +-
.../arialyy/downloadutil/DownloadManager.java | 61 ++++++
.../com/arialyy/downloadutil/core/Task.java | 175 ++++++++++++++++++
.../downloadutil/{ => util}/DownLoadUtil.java | 8 +-
.../arialyy/downloadutil/{ => util}/Util.java | 2 +-
6 files changed, 244 insertions(+), 12 deletions(-)
create mode 100644 downloadutil/src/main/java/com/arialyy/downloadutil/DownloadManager.java
create mode 100644 downloadutil/src/main/java/com/arialyy/downloadutil/core/Task.java
rename downloadutil/src/main/java/com/arialyy/downloadutil/{ => util}/DownLoadUtil.java (98%)
rename downloadutil/src/main/java/com/arialyy/downloadutil/{ => util}/Util.java (99%)
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 04c63e41..ba75a422 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -53,7 +53,7 @@
-
+
diff --git a/app/src/main/java/com/example/arial/downloaddemo/MainActivity.java b/app/src/main/java/com/example/arial/downloaddemo/MainActivity.java
index b8ab673c..1cf79432 100644
--- a/app/src/main/java/com/example/arial/downloaddemo/MainActivity.java
+++ b/app/src/main/java/com/example/arial/downloaddemo/MainActivity.java
@@ -4,20 +4,16 @@ import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
-import android.support.design.widget.FloatingActionButton;
-import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
-import android.view.Menu;
-import android.view.MenuItem;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
-import com.arialyy.downloadutil.DownLoadUtil;
-import com.arialyy.downloadutil.Util;
+import com.arialyy.downloadutil.util.DownLoadUtil;
+import com.arialyy.downloadutil.util.Util;
import java.net.HttpURLConnection;
diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/DownloadManager.java b/downloadutil/src/main/java/com/arialyy/downloadutil/DownloadManager.java
new file mode 100644
index 00000000..4abc2670
--- /dev/null
+++ b/downloadutil/src/main/java/com/arialyy/downloadutil/DownloadManager.java
@@ -0,0 +1,61 @@
+package com.arialyy.downloadutil;
+
+import android.content.Context;
+
+/**
+ * Created by lyy on 2016/8/11.
+ * 下载管理器,通过命令的方式控制下载
+ */
+public class DownloadManager {
+ /**
+ * 下载开始前事件
+ */
+ public static final String ACTION_PRE = "ACTION_PRE";
+
+ /**
+ * 开始下载事件
+ */
+ public static final String ACTION_START = "ACTION_START";
+
+ /**
+ * 恢复下载事件
+ */
+ public static final String ACTION_RESUME = "ACTION_RESUME";
+
+ /**
+ * 正在下载事件
+ */
+ public static final String ACTION_RUNNING = "ACTION_RUNNING";
+
+ /**
+ * 停止下载事件
+ */
+ public static final String ACTION_STOP = "ACTION_STOP";
+
+ /**
+ * 取消下载事件
+ */
+ public static final String ACTION_CANCEL = "ACTION_CANCEL";
+
+ /**
+ * 下载完成事件
+ */
+ public static final String ACTION_COMPLETE = "ACTION_COMPLETE";
+
+ /**
+ * 下载失败事件
+ */
+ public static final String ACTION_FAIL = "ACTION_FAIL";
+
+ private Context mContext;
+
+ private DownloadManager(Context context) {
+ mContext = context;
+// ACTION_RUNNING = context.getPackageName();
+ }
+
+ public static DownloadManager getInstance(Context context) {
+ return new DownloadManager(context);
+ }
+
+}
diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/core/Task.java b/downloadutil/src/main/java/com/arialyy/downloadutil/core/Task.java
new file mode 100644
index 00000000..3543771f
--- /dev/null
+++ b/downloadutil/src/main/java/com/arialyy/downloadutil/core/Task.java
@@ -0,0 +1,175 @@
+package com.arialyy.downloadutil.core;
+
+import android.content.Context;
+import android.content.Intent;
+import android.os.Handler;
+import android.util.Log;
+
+import com.arialyy.downloadutil.DownloadManager;
+import com.arialyy.downloadutil.inf.IDownloadListener;
+import com.arialyy.downloadutil.util.DownLoadUtil;
+
+import java.net.HttpURLConnection;
+
+/**
+ * Created by lyy on 2016/8/11.
+ * 下载任务类
+ */
+public class Task {
+ public static final String TAG = "Task";
+ /**
+ * 下载路径
+ */
+ String downloadUrl;
+ /**
+ * 保存路径
+ */
+ String downloadPath;
+ /**
+ * 下载监听
+ */
+ IDownloadListener listener;
+ Handler outHandler;
+ Context context;
+ DownLoadUtil util;
+
+ private Task() {
+ util = new DownLoadUtil();
+ }
+
+ /**
+ * 开始下载
+ */
+ public void start() {
+ if (util.isDownloading()) {
+ Log.d(TAG, "任务正在下载");
+ } else {
+ if (listener == null) {
+ listener = new DownloadListener(context, outHandler);
+ }
+ util.download(context, downloadUrl, downloadPath, listener);
+ }
+ }
+
+ /**
+ * 停止下载
+ */
+ public void stop() {
+ if (util.isDownloading()) {
+ util.stopDownload();
+ }
+ }
+
+ /**
+ * 取消下载
+ */
+ public void cancel() {
+ util.cancelDownload();
+ }
+
+ /**
+ * 下载监听类
+ */
+ static class DownloadListener extends DownLoadUtil.DownloadListener {
+ Handler outHandler;
+ Context context;
+ Intent sendIntent;
+ long INTERVAL = 1024 * 10; //10k大小的间隔
+ long lastLen = 0; //上一次发送长度
+
+ public DownloadListener(Context context, Handler outHandler) {
+ this.context = context;
+ this.outHandler = outHandler;
+ sendIntent = new Intent();
+ sendIntent.addCategory(context.getPackageName());
+ }
+
+ @Override
+ public void onPreDownload(HttpURLConnection connection) {
+ super.onPreDownload(connection);
+ long len = connection.getContentLength();
+ Intent preIntent = new Intent();
+ preIntent.addCategory(context.getPackageName());
+
+ }
+
+ @Override
+ public void onResume(long resumeLocation) {
+ super.onResume(resumeLocation);
+ sendIntent.putExtra(DownloadManager.RESUME_LOCATION, resumeLocation);
+ }
+
+ @Override
+ public void onStart(long startLocation) {
+ super.onStart(startLocation);
+ sendIntent.putExtra(DownloadManager.START_LOCATION, startLocation);
+ }
+
+ @Override
+ public void onProgress(long currentLocation) {
+ super.onProgress(currentLocation);
+ if (currentLocation - lastLen > INTERVAL) { //不要太过于频繁发送广播
+ sendIntent.putExtra("progress", currentLocation);
+ sendBroadcast(sendIntent);
+ lastLen = currentLocation;
+ }
+ }
+
+ @Override
+ public void onStop(long stopLocation) {
+ super.onStop(stopLocation);
+ sendIntent.putExtra(DownloadManager.STOP_LOCATION, stopLocation);
+ }
+
+ @Override
+ public void onCancel() {
+ super.onCancel();
+ }
+
+ @Override
+ public void onComplete() {
+ super.onComplete();
+ }
+
+ @Override
+ public void onFail() {
+ super.onFail();
+ }
+ }
+
+ public static class Builder {
+ String downloadUrl;
+ String downloadPath;
+ IDownloadListener listener;
+ Handler outHandler;
+ Context context;
+
+ public Builder(Context context, String downloadUrl, String downloadPath) {
+ this.context = context;
+ this.downloadUrl = downloadUrl;
+ this.downloadPath = downloadPath;
+ }
+
+ public Builder setDownloadListener(IDownloadListener listener) {
+ this.listener = listener;
+ return this;
+ }
+
+ public Builder setOutHandler(Handler outHandler) {
+ this.outHandler = outHandler;
+ return this;
+ }
+
+ public Task builder() {
+ Task task = new Task();
+ task.context = context;
+ task.downloadUrl = downloadUrl;
+ task.downloadPath = downloadPath;
+ task.listener = listener;
+ task.outHandler = outHandler;
+ return task;
+ }
+
+ }
+
+}
diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/DownLoadUtil.java b/downloadutil/src/main/java/com/arialyy/downloadutil/util/DownLoadUtil.java
similarity index 98%
rename from downloadutil/src/main/java/com/arialyy/downloadutil/DownLoadUtil.java
rename to downloadutil/src/main/java/com/arialyy/downloadutil/util/DownLoadUtil.java
index 074d14f2..5e3edb94 100644
--- a/downloadutil/src/main/java/com/arialyy/downloadutil/DownLoadUtil.java
+++ b/downloadutil/src/main/java/com/arialyy/downloadutil/util/DownLoadUtil.java
@@ -1,4 +1,4 @@
-package com.arialyy.downloadutil;
+package com.arialyy.downloadutil.util;
import android.content.Context;
import android.support.annotation.NonNull;
@@ -22,7 +22,7 @@ import java.util.Properties;
public class DownLoadUtil {
private static final String TAG = "DownLoadUtil";
//下载监听
- private DownloadListener mListener;
+ private IDownloadListener mListener;
/**
* 线程数
*/
@@ -41,7 +41,7 @@ public class DownLoadUtil {
private int mStopNum = 0;
public DownLoadUtil() {
}
- public DownloadListener getListener(){
+ public IDownloadListener getListener(){
return mListener;
}
/**
@@ -76,7 +76,7 @@ public class DownLoadUtil {
* @param downloadListener 下载进度监听 {@link DownloadListener}
*/
public void download(final Context context, @NonNull final String downloadUrl, @NonNull final String filePath,
- @NonNull final DownloadListener downloadListener) {
+ @NonNull final IDownloadListener downloadListener) {
isDownloading = true;
mCurrentLocation = 0;
isStop = false;
diff --git a/downloadutil/src/main/java/com/arialyy/downloadutil/Util.java b/downloadutil/src/main/java/com/arialyy/downloadutil/util/Util.java
similarity index 99%
rename from downloadutil/src/main/java/com/arialyy/downloadutil/Util.java
rename to downloadutil/src/main/java/com/arialyy/downloadutil/util/Util.java
index aebab92b..5f062728 100644
--- a/downloadutil/src/main/java/com/arialyy/downloadutil/Util.java
+++ b/downloadutil/src/main/java/com/arialyy/downloadutil/util/Util.java
@@ -1,4 +1,4 @@
-package com.arialyy.downloadutil;
+package com.arialyy.downloadutil.util;
import android.util.Log;