diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/HighestPriorityCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/HighestPriorityCmd.java index 836b37df..61f454f8 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/HighestPriorityCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/HighestPriorityCmd.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 extends AbsNormalCmd @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(); diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/NormalCmdFactory.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/NormalCmdFactory.java index 69c10cf9..2ec194ed 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/NormalCmdFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/NormalCmdFactory.java @@ -28,39 +28,39 @@ public class NormalCmdFactory extends AbsCmdFactory /** * 创建任务 */ - 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() { diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java index 11247523..cec2011c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/ResumeAllCmd.java @@ -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 extends AbsNormalCmd { } @Override public void executeCmd() { + if (!NetUtils.isConnected(AriaManager.APP)){ + Log.w(TAG, "恢复任务失败,网络未连接"); + return; + } if (isDownloadCmd) { resumeDownload(); } else { diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java index bacc7658..62f4982c 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StartCmd.java @@ -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 extends AbsNormalCmd { @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); diff --git a/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java b/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java index 2417e098..71ecc289 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java @@ -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 { 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 { */ @Override public void cancel() { if (!mUtil.isRunning()) { - if (mOutHandler != null) { - mOutHandler.obtainMessage(ISchedulers.CANCEL, this).sendToTarget(); - } + mListener.onCancel(); } mUtil.cancel(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsEntity.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsEntity.java index 1caf0d17..8d0a9a21 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsEntity.java @@ -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. diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java index 882be469..5b0c014b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java @@ -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 implements ITask { + /** * 是否需要重试,默认为true */ diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java index 50f659ad..c4be525b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/AbsTaskQueue.java @@ -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 { 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> mObservers = new ConcurrentHashMap<>(); - /** - * 设置调度器类型 - */ - abstract int getSchedulerType(); - /** * 设置代理类后缀名 */ @@ -282,7 +275,7 @@ abstract class AbsSchedulers 0; - } - - /** - * 获取正在执行的队列数 - */ - int getExeTaskNum() { - return mQueue.getCurrentExePoolNum(); - } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadGroupSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadGroupSchedulers.java index 9d2a6d11..79c8679f 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadGroupSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadGroupSchedulers.java @@ -43,10 +43,6 @@ public class DownloadGroupSchedulers extends return INSTANCE; } - @Override int getSchedulerType() { - return DOWNLOAD_GROUP; - } - @Override String getProxySuffix() { return "$$DownloadGroupListenerProxy"; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java index 3c492174..250c75e6 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/DownloadSchedulers.java @@ -45,10 +45,6 @@ public class DownloadSchedulers return INSTANCE; } - @Override int getSchedulerType() { - return DOWNLOAD; - } - @Override String getProxySuffix() { return "$$DownloadListenerProxy"; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java b/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java index be260d57..893915b4 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java +++ b/Aria/src/main/java/com/arialyy/aria/core/scheduler/UploadSchedulers.java @@ -44,10 +44,6 @@ public class UploadSchedulers return INSTANCE; } - @Override int getSchedulerType() { - return UPLOAD; - } - @Override String getProxySuffix() { return "$$UploadListenerProxy"; } diff --git a/Aria/src/main/java/com/arialyy/aria/util/NetUtils.java b/Aria/src/main/java/com/arialyy/aria/util/NetUtils.java new file mode 100644 index 00000000..95942a5d --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/util/NetUtils.java @@ -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; + } + } +} \ No newline at end of file diff --git a/README.md b/README.md index b1476551..d6851a95 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ annotationProcessor 'com.arialyy.aria:aria-compiler:3.2.22' + ``` ## 使用Aria diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3848e920..00d1d08c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -5,6 +5,7 @@ + { - //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); 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 86f428a0..306ff9fb 100644 --- a/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -47,7 +47,7 @@ public class SingleTaskActivity extends BaseActivity { //"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"; diff --git a/aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java b/aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java index 832f9521..d5d6e08a 100644 --- a/aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java +++ b/aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java @@ -60,19 +60,13 @@ public class UploadTask extends AbsNormalTask { 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(); }