Skip to content
Permalink
Browse files

1、修复restart的一个空指针问题

2、优化Ftp demo的代码
  • Loading branch information...
AriaLyy committed May 29, 2019
1 parent 7b15d4c commit f0bc470b75ff3b40ef39c140c7ea4bbc8f880f39
@@ -81,7 +81,11 @@
* 发送等待状态
*/
void sendWaitState() {
sendWaitState(getTask());
AbsTask task = getTask();
if (task == null) {
task = createTask();
}
sendWaitState(task);
}

/**
@@ -105,14 +109,22 @@ void stopAll() {
* 停止任务
*/
void stopTask() {
mQueue.stopTask(getTask());
AbsTask task = getTask();
if (task == null) {
task = createTask();
}
mQueue.stopTask(task);
}

/**
* 删除任务
*/
void removeTask() {
mQueue.cancelTask(getTask());
AbsTask task = getTask();
if (task == null) {
task = createTask();
}
mQueue.cancelTask(task);
}

/**
@@ -130,14 +142,22 @@ void removeTask(AbsTaskWrapper taskEntity) {
* 启动任务
*/
void startTask() {
mQueue.startTask(getTask());
AbsTask task = getTask();
if (task == null){
task = createTask();
}
mQueue.startTask(task);
}

/**
* 恢复任务
*/
void resumeTask() {
mQueue.resumeTask(getTask());
AbsTask task = getTask();
if (task == null){
task = createTask();
}
mQueue.resumeTask(task);
}

/**
@@ -36,6 +36,7 @@
AbsTask task = getTask();
if (task == null) {
if (mTaskWrapper.getEntity().getState() == IEntity.STATE_RUNNING) {
createTask();
stopTask();
} else {
ALog.w(TAG, "停止命令执行失败,【调度器中没有该任务】");
@@ -56,8 +56,15 @@ protected void saveData(int state, long location) {
mEntity.setState(state);

if (state == IEntity.STATE_CANCEL) {
CommonUtil.delTaskRecord(mEntity.getDownloadPath(), 1, mTaskWrapper.isRemoveFile(),
getTask().getSchedulerType() != TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY);
int sType = getTask().getSchedulerType();
if (sType == TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY) {
mEntity.setComplete(false);
mEntity.setState(IEntity.STATE_WAIT);
CommonUtil.delTaskRecord(mEntity.getFilePath(), 1, mTaskWrapper.isRemoveFile(), false);
} else {
CommonUtil.delTaskRecord(mEntity.getFilePath(), 1, mTaskWrapper.isRemoveFile(), true);
}

return;
} else if (state == IEntity.STATE_STOP) {
mEntity.setStopTime(System.currentTimeMillis());
@@ -128,7 +128,7 @@ TARGET updateUrl(String newUrl) {
}

//设置文件保存路径,如果新文件路径和旧文件路径不同,则修改路径
if (!filePath.equals(mEntity.getDownloadPath())) {
if (!filePath.equals(mEntity.getFilePath())) {
// 检查路径冲突
if (DbEntity.checkDataExist(DownloadEntity.class, "downloadPath=?", filePath)) {
if (!forceDownload) {
@@ -111,8 +111,16 @@ public String getFilePath() {
return downloadPath;
}

/**
* 后面会删除该方法,请使用{@link #setFilePath(String)}
*/
@Deprecated
public DownloadEntity setDownloadPath(String downloadPath) {
this.downloadPath = downloadPath;
return setFilePath(downloadPath);
}

public DownloadEntity setFilePath(String filePath) {
this.downloadPath = filePath;
return this;
}

@@ -135,8 +135,14 @@ protected void saveData(int state, long location) {
mTaskWrapper.setState(state);
mEntity.setState(state);
if (state == IEntity.STATE_CANCEL) {
CommonUtil.delGroupTaskRecord(mEntity, mTaskWrapper.isRemoveFile(),
getTask().getSchedulerType() != TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY);
int sType = getTask().getSchedulerType();
if (sType == TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY) {
mEntity.setComplete(false);
mEntity.setState(IEntity.STATE_WAIT);
CommonUtil.delGroupTaskRecord(mEntity, mTaskWrapper.isRemoveFile(), false);
} else {
CommonUtil.delGroupTaskRecord(mEntity, mTaskWrapper.isRemoveFile(), true);
}
return;
} else if (state == IEntity.STATE_STOP) {
mEntity.setStopTime(System.currentTimeMillis());
@@ -0,0 +1,22 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed 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.core.download.m3u8;

/**
*
*/
public class M3U8Delegate {
}
@@ -71,6 +71,10 @@
* @param task 需要恢复的任务
*/
@Override public void resumeTask(TASK task) {
if (task == null){
ALog.w(TAG, "resume task fail, task is null");
return;
}
if (mExecutePool.size() >= getMaxTaskNum()) {
task.getTaskWrapper().getEntity().setState(IEntity.STATE_WAIT);
mCachePool.putTaskToFirst(task);
@@ -204,12 +208,19 @@ public BaseCachePool getCachePool() {
* @param task {@link DownloadTask}、{@link UploadTask}、{@link DownloadGroupTask}
*/
void addTask(TASK task) {
if (task == null){
ALog.w(TAG, "add task fail, task is null");
return;
}
if (!mCachePool.taskExits(task.getKey())) {
mCachePool.putTask(task);
}
}

@Override public void startTask(TASK task) {
if (task == null){
ALog.w(TAG, "start fail, task is null");
}
if (mExecutePool.taskExits(task.getKey())) {
ALog.w(TAG, String.format("任务【%s】执行中", task.getKey()));
return;
@@ -221,6 +232,10 @@ void addTask(TASK task) {
}

@Override public void stopTask(TASK task) {
if (task == null) {
ALog.w(TAG, "stop fail, task is null");
return;
}
int state = task.getState();
boolean canStop = false;
switch (state) {
@@ -267,7 +282,7 @@ void addTask(TASK task) {

@Override public void reTryStart(TASK task) {
if (task == null) {
ALog.e(TAG, "任务重试失败,原因:task 为null");
ALog.e(TAG, "reTry fail, task is null");
return;
}

@@ -37,8 +37,14 @@ protected void saveData(int state, long location) {
mTaskWrapper.setState(state);
mEntity.setState(state);
if (state == IEntity.STATE_CANCEL) {
CommonUtil.delTaskRecord(mEntity.getFilePath(), 2, mTaskWrapper.isRemoveFile(),
getTask().getSchedulerType() != TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY);
int sType = getTask().getSchedulerType();
if (sType == TaskSchedulerType.TYPE_CANCEL_AND_NOT_NOTIFY) {
mEntity.setComplete(false);
mEntity.setState(IEntity.STATE_WAIT);
CommonUtil.delTaskRecord(mEntity.getFilePath(), 2, mTaskWrapper.isRemoveFile(), false);
} else {
CommonUtil.delTaskRecord(mEntity.getFilePath(), 2, mTaskWrapper.isRemoveFile(), true);
}
} else if (state == IEntity.STATE_STOP) {
mEntity.setStopTime(System.currentTimeMillis());
} else if (state == IEntity.STATE_COMPLETE) {
@@ -19,7 +19,7 @@
public class FtpDownload extends BaseActivity<ActivityTestBinding> {
String TAG = "TestFTPActivity";
private final String URL = "ftp://192.168.1.3:21/download//AriaPrj.rar";
private final String FILE_PATH = "/mnt/sdcard/AriaPrj.rar";
private final String FILE_PATH = "/mnt/sdcard/";

@Override protected int setLayoutId() {
return R.layout.activity_test;
@@ -15,64 +15,110 @@
*/
package com.arialyy.simple.core.download;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import com.arialyy.annotations.Download;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.download.FtpDownloadTarget;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.frame.util.show.L;
import com.arialyy.frame.util.show.T;
import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.common.DirChooseDialog;
import com.arialyy.simple.common.ModifyUrlDialog;
import com.arialyy.simple.databinding.ActivityFtpDownloadBinding;
import com.arialyy.simple.util.AppUtil;
import java.io.File;
import java.io.IOException;

/**
* Created by lyy on 2017/7/25.
* Ftp下载测试
* Ftp下载
*/
public class FtpDownloadActivity extends BaseActivity<ActivityFtpDownloadBinding> {
private final String URL = "ftp://9.9.9.205:2121/Cyberduck-6.9.4.30164.zip";
//private final String URL = "ftp://182.92.180.213:21/video/572fed5c2ad48_1024.jpg";
//private final String URL = "ftp://182.92.180.213:21/DATA/20180205/rar/1111.rar";
//private final String URL = "ftp://d:d@dygodj8.com:12311/咖啡风暴HD大陆公映意语中字[飘花www.piaohua.com].mp4";
private String mUrl, mFilePath;
private FtpDownloadModule mModule;
private FtpDownloadTarget mTarget;

@Override protected void init(Bundle savedInstanceState) {
super.init(savedInstanceState);
setTitle("FTP文件下载");
Aria.download(this).register();
DownloadEntity entity = Aria.download(this).getDownloadEntity(URL);
if (entity != null) {
getBinding().setFileSize(entity.getConvertFileSize());
if (entity.getFileSize() == 0) {
getBinding().setProgress(0);
} else {
getBinding().setProgress(entity.isComplete() ? 100
: (int) (entity.getCurrentProgress() * 100 / entity.getFileSize()));
mModule = ViewModelProviders.of(this).get(FtpDownloadModule.class);

mModule.getFtpDownloadInfo(this).observe(this, new Observer<DownloadEntity>() {

@Override public void onChanged(@Nullable DownloadEntity entity) {
if (entity == null) {
return;
}
mTarget = Aria.download(FtpDownloadActivity.this).loadFtp(entity.getUrl());
if (mTarget.getTaskState() == IEntity.STATE_STOP) {
getBinding().setStateStr(getString(R.string.resume));
} else if (mTarget.isRunning()) {
getBinding().setStateStr(getString(R.string.stop));
}

if (entity.getFileSize() != 0) {
getBinding().setFileSize(CommonUtil.formatFileSize(entity.getFileSize()));
getBinding().setProgress(entity.isComplete() ? 100
: (int) (entity.getCurrentProgress() * 100 / entity.getFileSize()));
}
getBinding().setUrl(entity.getUrl());
getBinding().setFilePath(entity.getFilePath());
mUrl = entity.getUrl();
mFilePath = entity.getFilePath();
}
});
getBinding().setViewModel(this);
try {
getBinding().codeView.setSource(AppUtil.getHelpCode(this, "FtpDownload.java"));
} catch (IOException e) {
e.printStackTrace();
}
}

public void onClick(View view) {
switch (view.getId()) {
case R.id.start:
Aria.download(this).loadFtp(URL)
.login("N0rI", "0qcK")
.setFilePath("/mnt/sdcard/", true)
.start();
break;
case R.id.stop:
Aria.download(this).loadFtp(URL).stop();
if (mTarget.isRunning()) {
getBinding().setStateStr(getString(R.string.resume));
Aria.download(this).loadFtp(mUrl).stop();
} else {
getBinding().setStateStr(getString(R.string.stop));
Aria.download(this).loadFtp(mUrl).login("N0rI", "0qcK")
.setFilePath(mFilePath, true)
.start();
}
break;
case R.id.cancel:
Aria.download(this).loadFtp(URL).cancel();
Aria.download(this).loadFtp(mUrl).cancel();
break;
}
}

public void chooseUrl() {
ModifyUrlDialog dialog =
new ModifyUrlDialog(this, getString(R.string.modify_url_dialog_title), mUrl);
dialog.show(getSupportFragmentManager(), "ModifyUrlDialog");
}

public void chooseFilePath() {
DirChooseDialog dirChooseDialog = new DirChooseDialog(this);
dirChooseDialog.show(getSupportFragmentManager(), "DirChooseDialog");
}

@Download.onPre() protected void onPre(DownloadTask task) {
L.d(TAG, "ftp pre");
}
@@ -119,4 +165,14 @@ public void onClick(View view) {
@Override protected int setLayoutId() {
return R.layout.activity_ftp_download;
}

@Override protected void dataCallback(int result, Object data) {
super.dataCallback(result, data);
if (result == ModifyUrlDialog.MODIFY_URL_DIALOG_RESULT) {
mModule.uploadUrl(this, String.valueOf(data));
}else if (result == DirChooseDialog.DIR_CHOOSE_DIALOG_RESULT) {
mModule.updateFilePath(this, String.valueOf(data));
}
}

}

0 comments on commit f0bc470

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