修复任务未下载时,直接删除任务导致的注解回调方法中任务状态错误

https://github.com/AriaLyy/Aria/issues/123
,重试失败任务添加网络状态判断,网络未连接,将会重试失败
https://github.com/AriaLyy/Aria/issues/119
pull/330/head
AriaLyy 7 years ago
parent 29aba07205
commit 21594d736c
  1. 7
      Aria/src/main/java/com/arialyy/aria/core/command/normal/HighestPriorityCmd.java
  2. 18
      Aria/src/main/java/com/arialyy/aria/core/command/normal/NormalCmdFactory.java
  3. 8
      Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java
  4. 6
      Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java
  5. 12
      Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java
  6. 8
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java
  7. 10
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java
  8. 2
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsEntity.java
  9. 2
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java
  10. 13
      Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java
  11. 29
      Aria/src/main/java/com/arialyy/aria/core/scheduler/AbsSchedulers.java
  12. 4
      Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadGroupSchedulers.java
  13. 4
      Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java
  14. 4
      Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java
  15. 136
      Aria/src/main/java/com/arialyy/aria/util/NetUtils.java
  16. 1
      README.md
  17. 1
      app/src/main/AndroidManifest.xml
  18. 4
      app/src/main/java/com/arialyy/simple/download/FtpDownloadActivity.java
  19. 2
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  20. 10
      aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java

@ -16,10 +16,13 @@
package com.arialyy.aria.core.command.normal;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.inf.AbsNormalTask;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.queue.DownloadTaskQueue;
import com.arialyy.aria.util.NetUtils;
/**
* Created by lyy on 2017/6/2.
@ -43,6 +46,10 @@ final class HighestPriorityCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T>
@Override public void executeCmd() {
if (!canExeCmd) return;
if (!NetUtils.isConnected(AriaManager.APP)){
Log.w(TAG, "启动任务失败,网络未连接");
return;
}
DownloadTask task = (DownloadTask) getTask();
if (task == null) {
task = (DownloadTask) createTask();

@ -28,39 +28,39 @@ public class NormalCmdFactory extends AbsCmdFactory<AbsTaskEntity, AbsNormalCmd>
/**
* 创建任务
*/
public static final int TASK_CREATE = 0x122;
public static final int TASK_CREATE = 0xb1;
/**
* 启动任务
*/
public static final int TASK_START = 0x123;
public static final int TASK_START = 0xb2;
/**
* 恢复任务
*/
public static final int TASK_RESUME = 0x127;
public static final int TASK_RESUME = 0xb3;
/**
* 取消任务
*/
public static final int TASK_CANCEL = 0x124;
public static final int TASK_CANCEL = 0xb4;
/**
* 停止任务
*/
public static final int TASK_STOP = 0x125;
public static final int TASK_STOP = 0xb5;
/**
* 设置任务为最高优先级
*/
public static final int TASK_HIGHEST_PRIORITY = 0x128;
public static final int TASK_HIGHEST_PRIORITY = 0xb6;
/**
* 停止所有任务
*/
public static final int TASK_STOP_ALL = 0x129;
public static final int TASK_STOP_ALL = 0xb7;
/**
* 恢复所有停止的任务
*/
public static final int TASK_RESUME_ALL = 0x130;
public static final int TASK_RESUME_ALL = 0xb8;
/**
* 删除所有任务
*/
public static final int TASK_CANCEL_ALL = 0x131;
public static final int TASK_CANCEL_ALL = 0xb9;
private static volatile NormalCmdFactory INSTANCE = null;
private NormalCmdFactory() {

@ -1,5 +1,8 @@
package com.arialyy.aria.core.command.normal;
import android.util.Log;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity;
@ -9,6 +12,7 @@ import com.arialyy.aria.core.queue.DownloadTaskQueue;
import com.arialyy.aria.core.queue.UploadTaskQueue;
import com.arialyy.aria.core.upload.UploadTaskEntity;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.util.NetUtils;
import java.util.List;
/**
@ -26,6 +30,10 @@ final class ResumeAllCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
}
@Override public void executeCmd() {
if (!NetUtils.isConnected(AriaManager.APP)){
Log.w(TAG, "恢复任务失败,网络未连接");
return;
}
if (isDownloadCmd) {
resumeDownload();
} else {

@ -17,11 +17,13 @@
package com.arialyy.aria.core.command.normal;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.common.QueueMod;
import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.util.NetUtils;
/**
* Created by lyy on 2016/8/22.
@ -36,6 +38,10 @@ class StartCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
@Override public void executeCmd() {
if (!canExeCmd) return;
if (!NetUtils.isConnected(AriaManager.APP)) {
Log.w(TAG, "启动任务失败,网络未连接");
return;
}
String mod;
int maxTaskNum;
AriaManager manager = AriaManager.getInstance(AriaManager.APP);

@ -25,6 +25,7 @@ import com.arialyy.aria.core.inf.IEventListener;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.util.ErrorHelp;
import com.arialyy.aria.util.NetUtils;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
@ -172,9 +173,6 @@ public abstract class AbsThreadTask<ENTITY extends AbsEntity, TASK_ENTITY extend
protected void fail(long currentLocation, String msg, Exception ex) {
synchronized (AriaManager.LOCK) {
try {
//STATE.FAIL_NUM++;
//STATE.isRunning = false;
//STATE.isStop = true;
if (ex != null) {
Log.e(TAG, msg + "\n" + CommonUtil.getPrintException(ex));
} else {
@ -182,10 +180,6 @@ public abstract class AbsThreadTask<ENTITY extends AbsEntity, TASK_ENTITY extend
}
if (mConfig.SUPPORT_BP) {
writeConfig(false, currentLocation);
//if (STATE.isFail()) {
// Log.e(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】执行失败");
// mListener.onFail(true);
//}
retryThis(true);
} else {
Log.e(TAG, "任务【" + mConfig.TEMP_FILE.getName() + "】执行失败");
@ -204,6 +198,10 @@ public abstract class AbsThreadTask<ENTITY extends AbsEntity, TASK_ENTITY extend
* @param needRetry 是否可以重试
*/
private void retryThis(boolean needRetry) {
if (!NetUtils.isConnected(AriaManager.APP)){
Log.w(TAG, "重试线程失败,网络未连接");
return;
}
if (mFailNum < RETRY_NUM && needRetry) {
if (mFailTimer != null) {
mFailTimer.purge();

@ -61,18 +61,14 @@ public class DownloadGroupTask extends AbsGroupTask<DownloadGroupTaskEntity, Dow
@Override public void stop() {
if (!mUtil.isRunning()) {
if (mOutHandler != null) {
mOutHandler.obtainMessage(ISchedulers.STOP, this).sendToTarget();
}
mListener.onStop(mEntity.getCurrentProgress());
}
mUtil.stop();
}
@Override public void cancel() {
if (!mUtil.isRunning()) {
if (mOutHandler != null) {
mOutHandler.obtainMessage(ISchedulers.CANCEL, this).sendToTarget();
}
mListener.onCancel();
}
mUtil.cancel();
}

@ -128,11 +128,7 @@ public class DownloadTask extends AbsNormalTask<DownloadEntity> {
if (mUtil.isRunning()) {
mUtil.stop();
} else {
mEntity.setState(isWait ? IEntity.STATE_WAIT : IEntity.STATE_STOP);
mEntity.update();
if (mOutHandler != null) {
mOutHandler.obtainMessage(ISchedulers.STOP, this).sendToTarget();
}
mListener.onStop(mEntity.getCurrentProgress());
}
}
@ -141,9 +137,7 @@ public class DownloadTask extends AbsNormalTask<DownloadEntity> {
*/
@Override public void cancel() {
if (!mUtil.isRunning()) {
if (mOutHandler != null) {
mOutHandler.obtainMessage(ISchedulers.CANCEL, this).sendToTarget();
}
mListener.onCancel();
}
mUtil.cancel();
}

@ -19,6 +19,8 @@ import android.os.Parcel;
import android.os.Parcelable;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.orm.Ignore;
import java.util.LinkedList;
import java.util.List;
/**
* Created by AriaL on 2017/6/29.

@ -17,13 +17,13 @@ package com.arialyy.aria.core.inf;
import android.content.Context;
import android.os.Handler;
import com.arialyy.aria.orm.Ignore;
import com.arialyy.aria.util.CommonUtil;
/**
* Created by AriaL on 2017/6/29.
*/
public abstract class AbsTask<ENTITY extends AbsEntity> implements ITask<ENTITY> {
/**
* 是否需要重试默认为true
*/

@ -18,18 +18,13 @@ package com.arialyy.aria.core.queue;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.inf.AbsEntity;
import com.arialyy.aria.core.inf.AbsTask;
import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.queue.pool.BaseCachePool;
import com.arialyy.aria.core.queue.pool.BaseExecutePool;
import java.security.Key;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import com.arialyy.aria.util.NetUtils;
/**
* Created by lyy on 2017/2/23.
@ -187,7 +182,11 @@ abstract class AbsTaskQueue<TASK extends AbsTask, TASK_ENTITY extends AbsTaskEnt
@Override public void reTryStart(TASK task) {
if (task == null) {
Log.w(TAG, "重试下载失败,task 为null");
Log.w(TAG, "重试失败,task 为null");
return;
}
if (!NetUtils.isConnected(AriaManager.APP)){
Log.w(TAG, "重试失败,网络未连接");
return;
}
if (!task.isRunning()) {

@ -19,6 +19,7 @@ import android.os.CountDownTimer;
import android.os.Message;
import android.util.Log;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.command.normal.NormalCmdFactory;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.inf.AbsEntity;
import com.arialyy.aria.core.inf.AbsNormalEntity;
@ -28,6 +29,7 @@ import com.arialyy.aria.core.inf.GroupSendParams;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.core.queue.ITaskQueue;
import com.arialyy.aria.core.upload.UploadTask;
import com.arialyy.aria.util.NetUtils;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
@ -41,20 +43,11 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
implements ISchedulers<TASK> {
private final String TAG = "AbsSchedulers";
static final int DOWNLOAD = 0xa1;
static final int UPLOAD = 0xa2;
static final int DOWNLOAD_GROUP = 0xa3;
protected QUEUE mQueue;
private Map<String, AbsSchedulerListener<TASK, AbsNormalEntity>> mObservers =
new ConcurrentHashMap<>();
/**
* 设置调度器类型
*/
abstract int getSchedulerType();
/**
* 设置代理类后缀名
*/
@ -282,7 +275,7 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
/**
* 启动下一个任务条件任务停止取消下载任务完成
*/
protected void startNextTask() {
private void startNextTask() {
TASK newTask = mQueue.getNextTask();
if (newTask == null) {
Log.w(TAG, "没有下一任务");
@ -292,20 +285,4 @@ abstract class AbsSchedulers<TASK_ENTITY extends AbsTaskEntity, ENTITY extends A
mQueue.startTask(newTask);
}
}
/**
* 是否有下一任务
*
* @return {@code true} {@code false}
*/
boolean hasNextTask() {
return mQueue.getCurrentCachePoolNum() > 0;
}
/**
* 获取正在执行的队列数
*/
int getExeTaskNum() {
return mQueue.getCurrentExePoolNum();
}
}

@ -43,10 +43,6 @@ public class DownloadGroupSchedulers extends
return INSTANCE;
}
@Override int getSchedulerType() {
return DOWNLOAD_GROUP;
}
@Override String getProxySuffix() {
return "$$DownloadGroupListenerProxy";
}

@ -45,10 +45,6 @@ public class DownloadSchedulers
return INSTANCE;
}
@Override int getSchedulerType() {
return DOWNLOAD;
}
@Override String getProxySuffix() {
return "$$DownloadListenerProxy";
}

@ -44,10 +44,6 @@ public class UploadSchedulers
return INSTANCE;
}
@Override int getSchedulerType() {
return UPLOAD;
}
@Override String getProxySuffix() {
return "$$UploadListenerProxy";
}

@ -0,0 +1,136 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.util;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
/**
* 跟网络相关的工具类
*/
public class NetUtils {
/**
* 没有网络
*/
public static final int NETWORK_TYPE_INVALID = 0;
/**
* wap网络
*/
public static final int NETWORK_TYPE_WAP = 1;
/**
* 2G网络
*/
public static final int NETWORK_TYPE_2G = 2;
/**
* 3G和3G以上网络或统称为快速网络
*/
public static final int NETWORK_TYPE_3G = 3;
/**
* wifi网络
*/
public static final int NETWORK_TYPE_WIFI = 4;
/**
* 判断网络是否连接
*/
public static boolean isConnected(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
return ni != null && ni.isConnectedOrConnecting();
}
/**
* 判断是否是wifi连接
*/
public static boolean isWifi(Context context) {
return getNetWorkType(context) == NETWORK_TYPE_WIFI;
}
/**
* 获取网络状态wifi,wap,2g,3g.
*
* @param context 上下文
* @return int 网络状态 {@link #NETWORK_TYPE_2G},{@link #NETWORK_TYPE_3G},
* {@link #NETWORK_TYPE_INVALID},{@link #NETWORK_TYPE_WAP},{@link #NETWORK_TYPE_WIFI}
*/
public static int getNetWorkType(Context context) {
int netWorkType = -1;
ConnectivityManager manager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
String type = networkInfo.getTypeName();
if (type.equalsIgnoreCase("WIFI")) {
netWorkType = NETWORK_TYPE_WIFI;
} else if (type.equalsIgnoreCase("MOBILE")) {
String proxyHost = android.net.Proxy.getDefaultHost();
netWorkType = TextUtils.isEmpty(proxyHost) ? (isFastMobileNetwork(context) ? NETWORK_TYPE_3G
: NETWORK_TYPE_2G) : NETWORK_TYPE_WAP;
}
} else {
netWorkType = NETWORK_TYPE_INVALID;
}
return netWorkType;
}
private static boolean isFastMobileNetwork(Context context) {
TelephonyManager telephonyManager =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
switch (telephonyManager.getNetworkType()) {
case TelephonyManager.NETWORK_TYPE_1xRTT:
return false; // ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_CDMA:
return false; // ~ 14-64 kbps
case TelephonyManager.NETWORK_TYPE_EDGE:
return false; // ~ 50-100 kbps
case TelephonyManager.NETWORK_TYPE_EVDO_0:
return true; // ~ 400-1000 kbps
case TelephonyManager.NETWORK_TYPE_EVDO_A:
return true; // ~ 600-1400 kbps
case TelephonyManager.NETWORK_TYPE_GPRS:
return false; // ~ 100 kbps
case TelephonyManager.NETWORK_TYPE_HSDPA:
return true; // ~ 2-14 Mbps
case TelephonyManager.NETWORK_TYPE_HSPA:
return true; // ~ 700-1700 kbps
case TelephonyManager.NETWORK_TYPE_HSUPA:
return true; // ~ 1-23 Mbps
case TelephonyManager.NETWORK_TYPE_UMTS:
return true; // ~ 400-7000 kbps
case TelephonyManager.NETWORK_TYPE_EHRPD:
return true; // ~ 1-2 Mbps
case TelephonyManager.NETWORK_TYPE_EVDO_B:
return true; // ~ 5 Mbps
case TelephonyManager.NETWORK_TYPE_HSPAP:
return true; // ~ 10-20 Mbps
case TelephonyManager.NETWORK_TYPE_IDEN:
return false; // ~25 kbps
case TelephonyManager.NETWORK_TYPE_LTE:
return true; // ~ 10+ Mbps
case TelephonyManager.NETWORK_TYPE_UNKNOWN:
return false;
default:
return false;
}
}
}

@ -39,6 +39,7 @@ annotationProcessor 'com.arialyy.aria:aria-compiler:3.2.22'
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
```
## 使用Aria

@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:name=".base.BaseApplication"

@ -35,9 +35,7 @@ import java.io.File;
* Ftp下载测试
*/
public class FtpDownloadActivity extends BaseActivity<ActivityFtpDownloadBinding> {
//private final String URL = "ftp://172.18.104.129:21/haha/large.rar";
//private final String URL = "ftp://172.18.104.129:21/haha/large.rar";
private final String URL = "ftp://172.18.104.66:21/haha/成都.mp3";
private final String URL = "ftp://192.168.8.2:21/test.apk";
@Override protected void init(Bundle savedInstanceState) {
super.init(savedInstanceState);

@ -47,7 +47,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
//"http://kotlinlang.org/docs/kotlin-docs.pdf";
//"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe";
//"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk";
"http://sitcac.daxincf.cn/wp-content/uploads/swift_vido/01/element.mp4";
"http://sitcac.daxincf.cn/wp-content/uploads/swift_vido/01/element.mp4_1";
//"http://120.25.196.56:8000/filereq?id=15692406294&ipncid=105635&client=android&filename=20170819185541.avi";
//"http://down2.xiaoshuofuwuqi.com/d/file/filetxt/20170608/14/%BA%DA%CE%D7%CA%A6%E1%C8%C6%F0.txt";
//"http://tinghuaapp.oss-cn-shanghai.aliyuncs.com/20170612201739607815";

@ -60,19 +60,13 @@ public class UploadTask extends AbsNormalTask<UploadEntity> {
if (mUtil.isRunning()) {
mUtil.stop();
} else {
mEntity.setState(IEntity.STATE_STOP);
mEntity.update();
if (mOutHandler != null) {
mOutHandler.obtainMessage(ISchedulers.STOP, this).sendToTarget();
}
mListener.onStop(mEntity.getCurrentProgress());
}
}
@Override public void cancel() {
if (!mUtil.isRunning()) {
if (mOutHandler != null) {
mOutHandler.obtainMessage(ISchedulers.CANCEL, this).sendToTarget();
}
mListener.onCancel();
}
mUtil.cancel();
}

Loading…
Cancel
Save