Skip to content
Permalink
Browse files

修复任务记录删除失败的问题,优化网络连接状态获取的逻辑,修复配置文件的某些配置失效的问题

  • Loading branch information...
AriaLyy committed Jun 19, 2019
1 parent 8f8f8fa commit ae3d24f471dd37aded9c90056461e0d3585a8026
Showing with 461 additions and 295 deletions.
  1. +57 −1 Aria/src/main/java/com/arialyy/aria/core/AriaManager.java
  2. +24 −2 Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java
  3. +35 −38 Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java
  4. +3 −3 Aria/src/main/java/com/arialyy/aria/core/common/BaseListener.java
  5. +49 −32 Aria/src/main/java/com/arialyy/aria/core/common/RecordHandler.java
  6. +5 −0 Aria/src/main/java/com/arialyy/aria/core/common/TaskRecord.java
  7. +3 −3 Aria/src/main/java/com/arialyy/aria/core/common/ThreadRecord.java
  8. +70 −70 Aria/src/main/java/com/arialyy/aria/core/common/ftp/FtpDelegate.java
  9. +12 −5 Aria/src/main/java/com/arialyy/aria/core/config/DownloadConfig.java
  10. +21 −31 Aria/src/main/java/com/arialyy/aria/core/config/XMLReader.java
  11. +2 −0 Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java
  12. +4 −4 Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java
  13. +0 −4 Aria/src/main/java/com/arialyy/aria/core/download/FtpDirDownloadTarget.java
  14. +0 −5 Aria/src/main/java/com/arialyy/aria/core/download/FtpDownloadTarget.java
  15. +2 −2 Aria/src/main/java/com/arialyy/aria/core/download/downloader/FtpFileInfoThread.java
  16. +1 −0 Aria/src/main/java/com/arialyy/aria/core/download/m3u8/BaseM3U8Loader.java
  17. +1 −1 Aria/src/main/java/com/arialyy/aria/core/download/m3u8/ITsMergeHandler.java
  18. +36 −1 Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8KeyInfo.java
  19. +2 −2 ...src/main/java/com/arialyy/aria/core/download/m3u8/{M3U8LiveDownloadUtil.java → M3U8LiveUtil.java}
  20. +4 −14 Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8VodLoader.java
  21. +2 −2 Aria/src/main/java/com/arialyy/aria/core/download/m3u8/{M3U8VodDownloadUtil.java → M3U8VodUtil.java}
  22. +1 −1 Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java
  23. +57 −57 Aria/src/main/java/com/arialyy/aria/core/inf/IFtpTarget.java
  24. +0 −4 Aria/src/main/java/com/arialyy/aria/core/upload/FtpUploadTarget.java
  25. +1 −1 Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java
  26. +31 −3 Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java
  27. +7 −1 Aria/src/main/java/com/arialyy/aria/util/NetUtils.java
  28. +6 −1 Aria/src/main/java/com/arialyy/aria/util/RecordUtil.java
  29. +3 −0 DEV_LOG.md
  30. +2 −2 app/build.gradle
  31. +2 −2 app/src/main/assets/aria_config.xml
  32. +2 −1 app/src/main/java/com/arialyy/simple/core/download/HttpDownloadModule.java
  33. +5 −0 app/src/main/java/com/arialyy/simple/core/download/SingleTaskActivity.java
  34. +9 −0 app/src/main/java/com/arialyy/simple/core/download/m3u8/M3U8VodDownloadActivity.java
  35. +1 −1 aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java
  36. +1 −1 build.gradle
@@ -21,13 +21,20 @@
import android.app.Application;
import android.app.Dialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkRequest;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.RequiresApi;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.widget.PopupWindow;
import com.arialyy.aria.BuildConfig;
import com.arialyy.aria.core.command.ICmd;
import com.arialyy.aria.core.common.QueueMod;
import com.arialyy.aria.core.common.RecordHandler;
@@ -71,6 +78,10 @@
public static final Object LOCK = new Object();
public static final String DOWNLOAD_TEMP_DIR = "/Aria/temp/download/";
public static final String UPLOAD_TEMP_DIR = "/Aria/temp/upload/";
/**
* 是否已经联网,true 已经联网
*/
private static boolean isConnectedNet = true;

@SuppressLint("StaticFieldLeak") private static volatile AriaManager INSTANCE = null;
private Map<String, AbsReceiver> mReceivers = new ConcurrentHashMap<>();
@@ -85,7 +96,6 @@
private AppConfig mAConfig;
private DGroupConfig mDGConfig;
private Handler mAriaHandler;
private File[] files;

private AriaManager(Context context) {
APP = context.getApplicationContext();
@@ -94,6 +104,7 @@ private AriaManager(Context context) {
initConfig();
initAria();
amendTaskState();
regNetCallBack(context);
}

public static AriaManager getInstance(Context context) {
@@ -109,6 +120,47 @@ static AriaManager getInstance() {
return INSTANCE;
}

/**
* 注册网络监听,只有配置了检查网络{@link AppConfig#isNetCheck()}才会注册事件
*/
private void regNetCallBack(Context context) {
if (!getAppConfig().isNetCheck()) {
return;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return;
}
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) {
return;
}

NetworkRequest.Builder builder = new NetworkRequest.Builder();
NetworkRequest request = builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.build();

cm.registerNetworkCallback(request, new ConnectivityManager.NetworkCallback() {

@Override public void onLost(Network network) {
super.onLost(network);
isConnectedNet = false;
ALog.d(TAG, "onLost");
}

@Override public void onAvailable(Network network) {
super.onAvailable(network);
ALog.d(TAG, "onAvailable");
isConnectedNet = true;
}
});
}

/**
* 初始化数据库
*/
private void initDb(Context context) {
// 这个地方错误了,获取数据库路径见:https://www.jianshu.com/p/815c9efc5449
String oldDbName = "AriaLyyDb";
@@ -153,6 +205,10 @@ public synchronized Handler getAriaHandler() {
return mAriaHandler;
}

public boolean isConnectedNet() {
return isConnectedNet;
}

public Map<String, AbsReceiver> getReceiver() {
return mReceivers;
}
@@ -193,7 +193,7 @@ public synchronized boolean isRunning() {
return b;
}

public synchronized void cancel() {
final public synchronized void cancel() {
if (isCancel) {
ALog.d(TAG, String.format("任务【%s】正在删除,删除任务失败", mTaskWrapper.getKey()));
return;
@@ -208,13 +208,24 @@ public synchronized void cancel() {
}
}
ThreadTaskManager.getInstance().removeTaskThread(mTaskWrapper.getKey());
onPostCancel();
}

/**
* 删除线程任务前的操作
*/
protected void onCancel() {

}

public synchronized void stop() {
/**
* 删除操作处理完成
*/
protected void onPostCancel() {

}

final public synchronized void stop() {
if (isStop) {
return;
}
@@ -229,12 +240,23 @@ public synchronized void stop() {
}
}
ThreadTaskManager.getInstance().removeTaskThread(mTaskWrapper.getKey());
onPostStop();
}

/**
* 停止线程任务前的操作
*/
protected void onStop() {

}

/**
* 停止操作完成
*/
protected void onPostStop() {

}

/**
* 直接调用的时候会自动启动线程执行
*/
@@ -175,17 +175,15 @@ public void setMaxSpeed(int speed) {
* 中断任务
*/
void breakTask() {
synchronized (AriaManager.LOCK) {
taskBreak = true;
if (mTaskWrapper.isSupportBP()) {
final long currentTemp = mChildCurrentLocation;
sendState(IThreadState.STATE_STOP, null);
ALog.d(TAG, String.format("任务【%s】thread__%s__中断【停止位置:%s】", getFileName(),
mRecord.threadId, currentTemp));
writeConfig(false, currentTemp);
} else {
ALog.i(TAG, String.format("任务【%s】已中断", getFileName()));
}
taskBreak = true;
if (mTaskWrapper.isSupportBP()) {
final long currentTemp = mChildCurrentLocation;
sendState(IThreadState.STATE_STOP, null);
ALog.d(TAG, String.format("任务【%s】thread__%s__中断【停止位置:%s】", getFileName(),
mRecord.threadId, currentTemp));
writeConfig(false, currentTemp);
} else {
ALog.i(TAG, String.format("任务【%s】已中断", getFileName()));
}
}

@@ -276,28 +274,26 @@ public void stop() {
* 执行中
*/
protected void progress(long len) {
synchronized (AriaManager.LOCK) {
//if (getState().CURRENT_LOCATION > getEntity().getFileSize() && !getTaskWrapper().asHttp()
// .isChunked()) {
// String errorMsg =
// String.format("下载失败,下载长度超出文件真实长度;currentLocation=%s, fileSize=%s",
// getState().CURRENT_LOCATION,
// getEntity().getFileSize());
// taskBreak = true;
// fail(mChildCurrentLocation, new FileException(TAG, errorMsg), false);
// return;
//}
mChildCurrentLocation += len;
if (!mStateHandler.getLooper().getThread().isAlive()) {
return;
}
mStateHandler.obtainMessage(IThreadState.STATE_RUNNING, len).sendToTarget();
if (System.currentTimeMillis() - mLastSaveTime > 5000
&& mChildCurrentLocation < mRecord.endLocation) {
mLastSaveTime = System.currentTimeMillis();
if (!mConfigThreadPool.isShutdown()) {
mConfigThreadPool.execute(mConfigThread);
}
//if (getState().CURRENT_LOCATION > getEntity().getFileSize() && !getTaskWrapper().asHttp()
// .isChunked()) {
// String errorMsg =
// String.format("下载失败,下载长度超出文件真实长度;currentLocation=%s, fileSize=%s",
// getState().CURRENT_LOCATION,
// getEntity().getFileSize());
// taskBreak = true;
// fail(mChildCurrentLocation, new FileException(TAG, errorMsg), false);
// return;
//}
mChildCurrentLocation += len;
if (!mStateHandler.getLooper().getThread().isAlive()) {
return;
}
mStateHandler.obtainMessage(IThreadState.STATE_RUNNING, len).sendToTarget();
if (System.currentTimeMillis() - mLastSaveTime > 5000
&& mChildCurrentLocation < mRecord.endLocation) {
mLastSaveTime = System.currentTimeMillis();
if (!mConfigThreadPool.isShutdown()) {
mConfigThreadPool.execute(mConfigThread);
}
}
}
@@ -350,14 +346,15 @@ protected void fail(final long subCurrentLocation, BaseException ex, boolean nee
* 重试ts分片
*/
private void retryM3U8Peer(boolean needRetry) {
if (!NetUtils.isConnected(AriaManager.APP) && !isNotNetRetry) {
ALog.w(TAG, String.format("任务【%s】重试失败,网络未连接", getFileName()));
boolean isConnected = NetUtils.isConnected(AriaManager.APP);
if (!isConnected && !isNotNetRetry) {
ALog.w(TAG, String.format("ts切片【%s】重试失败,网络未连接", getFileName()));
sendFailMsg(null);
return;
}
if (mFailTimes < RETRY_NUM && needRetry && (NetUtils.isConnected(AriaManager.APP)
|| isNotNetRetry) && !isBreak()) {
ALog.w(TAG, String.format("任务【%s】正在重试", getFileName()));
ALog.w(TAG, String.format("ts切片【%s】正在重试", getFileName()));
mFailTimes++;
mConfig.tempFile.delete();
CommonUtil.createFile(mConfig.tempFile.getPath());
@@ -374,13 +371,13 @@ private void retryM3U8Peer(boolean needRetry) {
*/
private void retryBlockTask(boolean needRetry) {
if (!NetUtils.isConnected(AriaManager.APP) && !isNotNetRetry) {
ALog.w(TAG, String.format("任务【%s】重试失败,网络未连接", getFileName()));
ALog.w(TAG, String.format("分块【%s】重试失败,网络未连接", getFileName()));
sendFailMsg(null);
return;
}
if (mFailTimes < RETRY_NUM && needRetry && (NetUtils.isConnected(AriaManager.APP)
|| isNotNetRetry) && !isBreak()) {
ALog.w(TAG, String.format("任务【%s】正在重试", getFileName()));
ALog.w(TAG, String.format("分块【%s】正在重试", getFileName()));
mFailTimes++;
handleBlockRecord();
ThreadTaskManager.getInstance().retryThread(this);
@@ -156,13 +156,13 @@ private void handleComplete() {
mEntity.setCurrentProgress(mEntity.getFileSize());
mEntity.setPercent(100);
handleSpeed(0);
ALog.i(TAG, String.format("任务【%s】完成,将删除线程任务记录", mEntity.getKey()));
ALog.i(TAG, String.format("任务【%s】完成,将删除任务任务记录", mEntity.getKey()));
if (mEntity instanceof DownloadGroupEntity) {
RecordUtil.delGroupTaskRecord((DownloadGroupEntity) mEntity, false, false);
} else if (mEntity instanceof DownloadEntity) {
RecordUtil.delTaskRecord(mEntity.getKey(), RecordHandler.TYPE_DOWNLOAD, false, false);
RecordUtil.delTaskRecord(((DownloadEntity) mEntity).getFilePath(), RecordHandler.TYPE_DOWNLOAD, false, false);
} else if (mEntity instanceof UploadEntity) {
RecordUtil.delTaskRecord(mEntity.getKey(), RecordHandler.TYPE_UPLOAD, false, false);
RecordUtil.delTaskRecord(((UploadEntity) mEntity).getFilePath(), RecordHandler.TYPE_UPLOAD, false, false);
}
}

0 comments on commit ae3d24f

Please sign in to comment.
You can’t perform that action at this time.