From e68cca5da3d8ed7a1276da3c707713c75b5ea6ca Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Sat, 2 Jun 2018 15:57:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BB=BB=E5=8A=A1=E7=BB=84?= =?UTF-8?q?=E4=B8=8D=E8=83=BD=E8=87=AA=E5=8A=A8=E8=B0=83=E5=BA=A6=E9=97=AE?= =?UTF-8?q?=E9=A2=98=EF=BC=8C=E4=BB=BB=E5=8A=A1=E7=BB=84=E8=BF=9B=E5=BA=A6?= =?UTF-8?q?=E8=B6=85=E5=87=BA=E9=97=AE=E9=A2=98=EF=BC=8C=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E7=BB=84=E6=97=A0=E6=B3=95=E5=9B=9E=E8=B0=83onResume=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=9B=E6=99=AE=E9=80=9A=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BF=AE=E6=AD=A3=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../arialyy/aria/core/common/AbsFileer.java | 9 +++-- .../aria/core/download/BaseDListener.java | 4 +-- .../core/download/DownloadGroupListener.java | 8 +++++ .../download/downloader/AbsGroupUtil.java | 34 +++++++++++++------ .../download/downloader/ConnectionHelp.java | 7 ++-- .../downloader/DownloadGroupUtil.java | 8 ++--- .../download/downloader/HttpThreadTask.java | 2 +- .../com/arialyy/aria/orm/AbsDelegate.java | 2 +- .../com/arialyy/aria/util/CommonUtil.java | 7 ++-- app/src/main/AndroidManifest.xml | 4 +-- app/src/main/assets/aria_config.xml | 2 +- .../simple/download/SingleTaskActivity.java | 9 ++--- 12 files changed, 62 insertions(+), 34 deletions(-) diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java index a8af4f7d..628f2114 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java @@ -16,6 +16,7 @@ package com.arialyy.aria.core.common; import android.content.Context; +import android.util.Log; import android.util.SparseArray; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.download.DownloadEntity; @@ -67,10 +68,8 @@ public abstract class AbsFileer mTask = new SparseArray<>(); - private Timer mTimer; - @Deprecated - private File mConfigFile; + @Deprecated private File mConfigFile; /** * 进度刷新间隔 */ @@ -465,6 +464,10 @@ public abstract class AbsFileer, TASK extends AbsTask> implements IDownloadListener { private static final String TAG = "BaseDListener"; - protected WeakReference outHandler; + WeakReference outHandler; private int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度 private long mLastLen = 0; //上一次发送长度 private boolean isFirst = true; protected ENTITY mEntity; protected TASK_ENTITY mTaskEntity; - protected TASK mTask; + private TASK mTask; private boolean isConvertSpeed = false; boolean isWait = false; private long mLastSaveTime; diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java index 806d341f..d029b959 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupListener.java @@ -19,6 +19,7 @@ import android.os.Handler; import com.arialyy.aria.core.download.downloader.IDownloadGroupListener; import com.arialyy.aria.core.inf.GroupSendParams; import com.arialyy.aria.core.scheduler.ISchedulers; +import com.arialyy.aria.util.ALog; /** * Created by Aria.Lao on 2017/7/20. @@ -85,10 +86,17 @@ class DownloadGroupListener } private void saveCurrentLocation() { + if (mEntity.getSubEntities() == null || mEntity.getSubEntities().isEmpty()) { + ALog.w(TAG, "保存进度失败,子任务为null"); + return; + } long location = 0; for (DownloadEntity e : mEntity.getSubEntities()) { location += e.getCurrentProgress(); } + if (location > mEntity.getFileSize()) { + location = mEntity.getFileSize(); + } mEntity.setCurrentProgress(location); mEntity.update(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java index 8bc5bd04..aba4e232 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/AbsGroupUtil.java @@ -43,11 +43,11 @@ public abstract class AbsGroupUtil implements IUtil { /** * FTP文件夹 */ - protected int FTP_DIR = 0xa1; + int FTP_DIR = 0xa1; /** * HTTP 任务组 */ - protected int HTTP_GROUP = 0xa2; + int HTTP_GROUP = 0xa2; /** * 任务组所有任务总长度 @@ -56,7 +56,7 @@ public abstract class AbsGroupUtil implements IUtil { long mCurrentLocation = 0; private ExecutorService mExePool; protected IDownloadGroupListener mListener; - protected DownloadGroupTaskEntity mGTEntity; + DownloadGroupTaskEntity mGTEntity; private boolean isRunning = false; private Timer mTimer; /** @@ -262,7 +262,7 @@ public abstract class AbsGroupUtil implements IUtil { } /** - * 预处理操作,由于属性的不同,http任务组在构造函数中就可以完成了 + * 预处理操作 * 而FTP文件夹的,需要获取完成所有子任务信息才算预处理完成 */ protected void onPre() { @@ -298,14 +298,13 @@ public abstract class AbsGroupUtil implements IUtil { @Override public void resume() { start(); - mListener.onResume(mCurrentLocation); } @Override public void setMaxSpeed(double maxSpeed) { } - private void clearState(){ + private void clearState() { mDownloaderMap.clear(); mFailMap.clear(); } @@ -325,7 +324,11 @@ public abstract class AbsGroupUtil implements IUtil { void startRunningFlow() { closeTimer(true); mListener.onPostPre(mTotalLen); - mListener.onStart(mCurrentLocation); + if (mCurrentLocation > 0) { + mListener.onResume(mCurrentLocation); + } else { + mListener.onStart(mCurrentLocation); + } startTimer(); } @@ -336,7 +339,16 @@ public abstract class AbsGroupUtil implements IUtil { if (!isRunning) { closeTimer(false); } else if (mCurrentLocation >= 0) { - mListener.onProgress(mCurrentLocation); + long t = 0; + for (DownloadTaskEntity te : mGTEntity.getSubTaskEntities()) { + if (te.getState() == IEntity.STATE_COMPLETE) { + t += te.getEntity().getFileSize(); + } else { + t += te.getEntity().getCurrentProgress(); + } + } + mCurrentLocation = t; + mListener.onProgress(t); } } }, 0, mUpdateInterval); @@ -345,8 +357,8 @@ public abstract class AbsGroupUtil implements IUtil { /** * 创建子任务下载器,默认创建完成自动启动 */ - Downloader createChildDownload(DownloadTaskEntity taskEntity) { - return createChildDownload(taskEntity, true); + void createChildDownload(DownloadTaskEntity taskEntity) { + createChildDownload(taskEntity, true); } /** @@ -408,7 +420,7 @@ public abstract class AbsGroupUtil implements IUtil { @Override public void onProgress(long currentLocation) { long speed = currentLocation - lastLen; - mCurrentLocation += speed; + //mCurrentLocation += speed; subEntity.setCurrentProgress(currentLocation); handleSpeed(speed); mListener.onSubRunning(subEntity); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java index 0803249a..58c37fd9 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/ConnectionHelp.java @@ -16,6 +16,7 @@ package com.arialyy.aria.core.download.downloader; import android.text.TextUtils; +import com.arialyy.aria.core.common.RequestEnum; import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.util.SSLContextUtil; import java.io.IOException; @@ -44,7 +45,7 @@ class ConnectionHelp { * @throws IOException */ static InputStream convertInputStream(HttpURLConnection connection) throws IOException { - String encoding = connection.getContentEncoding(); + String encoding = connection.getHeaderField("Content-Encoding"); if (TextUtils.isEmpty(encoding)) { return connection.getInputStream(); } @@ -88,7 +89,9 @@ class ConnectionHelp { */ static HttpURLConnection setConnectParam(DownloadTaskEntity entity, HttpURLConnection conn) throws ProtocolException { - conn.setRequestMethod(entity.getRequestEnum().name); + if (entity.getRequestEnum() == RequestEnum.POST){ + conn.setDoInput(true); + } Set keys = null; if (entity.getHeaders() != null && entity.getHeaders().size() > 0) { keys = entity.getHeaders().keySet(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java index ac9b1cea..1431026a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/DownloadGroupUtil.java @@ -35,7 +35,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { private final String TAG = "DownloadGroupUtil"; private ExecutorService mInfoPool; private int mInitCompleteNum, mInitFailNum; - private boolean isStop = false; + private boolean isStop = false, isStart = false; /** * 文件信息回调组 @@ -45,7 +45,6 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { public DownloadGroupUtil(IDownloadGroupListener listener, DownloadGroupTaskEntity taskEntity) { super(listener, taskEntity); mInfoPool = Executors.newCachedThreadPool(); - onPre(); } @Override int getTaskType() { @@ -69,7 +68,7 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { } @Override protected void onStart() { - super.onStart(); + onPre(); isStop = false; if (mCompleteNum == mGroupSize) { mListener.onComplete(); @@ -154,9 +153,10 @@ public class DownloadGroupUtil extends AbsGroupUtil implements IUtil { */ private void checkStartFlow() { synchronized (DownloadGroupUtil.class) { - if (mInitCompleteNum + mInitFailNum >= mGroupSize || !isNeedLoadFileSize) { + if (!isStart && (mInitCompleteNum + mInitFailNum >= mGroupSize || !isNeedLoadFileSize)) { startRunningFlow(); updateFileSize(); + isStart = true; } } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java index 6e290f81..296b7b13 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/downloader/HttpThreadTask.java @@ -85,7 +85,7 @@ final class HttpThreadTask extends AbsThreadTask - + diff --git a/app/src/main/assets/aria_config.xml b/app/src/main/assets/aria_config.xml index de9e2732..1feaa308 100644 --- a/app/src/main/assets/aria_config.xml +++ b/app/src/main/assets/aria_config.xml @@ -25,7 +25,7 @@ - + diff --git a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java index f1a9dc69..9e93f01a 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -49,8 +49,9 @@ public class SingleTaskActivity extends BaseActivity { //"http://58.210.9.131/tpk/sipgt//TDLYZTGH.tpk"; //chunked 下载 //"https://static.donguo.me//video/ip/course/pfys_1.mp4"; //"https://www.baidu.com/link?url=_LFCuTPtnzFxVJByJ504QymRywIA1Z_T5xUxe9ZLuxcGM0C_RcdpWyB1eGjbJC-e5wv5wAKM4WmLMAS5KeF6EZJHB8Va3YqZUiaErqK_pxm&wd=&eqid=e8583fe70002d126000000065a99f864"; - "https://d.pcs.baidu.com/file/a02c89a2d479d4fd2756f3313d42491d?fid=4232431903-250528-1114369760340736&dstime=1525491372&rt=sh&sign=FDtAERVY-DCb740ccc5511e5e8fedcff06b081203-3C13vkOkuk4TqXvVYW05zj1K0ao%3D&expires=8h&chkv=1&chkbd=0&chkpc=et&dp-logid=8651730921842106225&dp-callid=0&r=165533013"; - + //"https://d.pcs.baidu.com/file/a02c89a2d479d4fd2756f3313d42491d?fid=4232431903-250528-1114369760340736&dstime=1525491372&rt=sh&sign=FDtAERVY-DCb740ccc5511e5e8fedcff06b081203-3C13vkOkuk4TqXvVYW05zj1K0ao%3D&expires=8h&chkv=1&chkbd=0&chkpc=et&dp-logid=8651730921842106225&dp-callid=0&r=165533013"; + //"http://apk500.bce.baidu-mgame.com/game/67000/67734/20170622040827_oem_5502845.apk?r=1"; + "https://dl.genymotion.com/releases/genymotion-2.12.1/genymotion-2.12.1-vbox.exe"; @Bind(R.id.start) Button mStart; @Bind(R.id.stop) Button mStop; @Bind(R.id.cancel) Button mCancel; @@ -238,8 +239,8 @@ public class SingleTaskActivity extends BaseActivity { //.addHeader("Accept-Encoding", "gzip, deflate") //.addHeader("DNT", "1") //.addHeader("Cookie", "BAIDUID=648E5FF020CC69E8DD6F492D1068AAA9:FG=1; BIDUPSID=648E5FF020CC69E8DD6F492D1068AAA9; PSTM=1519099573; BD_UPN=12314753; locale=zh; BDSVRTM=0") - .useServerFileName(true) - .setRequestMode(RequestEnum.GET) + //.useServerFileName(true) + //.setRequestMode(RequestEnum.GET) .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg3.apk") //.resetState() .start();