diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java index aa2e068e..7890b3d6 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/AbsNormalCmd.java @@ -81,7 +81,11 @@ public abstract class AbsNormalCmd extends AbsCmd { * 发送等待状态 */ void sendWaitState() { - sendWaitState(getTask()); + AbsTask task = getTask(); + if (task == null) { + task = createTask(); + } + sendWaitState(task); } /** @@ -105,14 +109,22 @@ public abstract class AbsNormalCmd extends AbsCmd { * 停止任务 */ 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 @@ public abstract class AbsNormalCmd extends AbsCmd { * 启动任务 */ 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); } /** diff --git a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopCmd.java b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopCmd.java index 39658751..f8741a74 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopCmd.java +++ b/Aria/src/main/java/com/arialyy/aria/core/command/normal/StopCmd.java @@ -36,6 +36,7 @@ class StopCmd extends AbsNormalCmd { AbsTask task = getTask(); if (task == null) { if (mTaskWrapper.getEntity().getState() == IEntity.STATE_RUNNING) { + createTask(); stopTask(); } else { ALog.w(TAG, "停止命令执行失败,【调度器中没有该任务】"); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java b/Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java index 519c4a4b..d86714e3 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/BaseDListener.java @@ -56,8 +56,15 @@ public class BaseDListener extends BaseListener implements INormalTarget { } //设置文件保存路径,如果新文件路径和旧文件路径不同,则修改路径 - if (!filePath.equals(mEntity.getDownloadPath())) { + if (!filePath.equals(mEntity.getFilePath())) { // 检查路径冲突 if (DbEntity.checkDataExist(DownloadEntity.class, "downloadPath=?", filePath)) { if (!forceDownload) { diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java index 2768b77b..495df2a5 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java @@ -111,8 +111,16 @@ public class DownloadEntity extends AbsNormalEntity implements Parcelable { 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; } 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 b2590821..f636f6c5 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 @@ -135,8 +135,14 @@ class DownloadGroupListener 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()); diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8Delegate.java b/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8Delegate.java new file mode 100644 index 00000000..08a8a365 --- /dev/null +++ b/Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8Delegate.java @@ -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 { +} 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 1e5b79a1..a58ce821 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 @@ -71,6 +71,10 @@ public abstract class AbsTaskQueue= getMaxTaskNum()) { task.getTaskWrapper().getEntity().setState(IEntity.STATE_WAIT); mCachePool.putTaskToFirst(task); @@ -204,12 +208,19 @@ public abstract class AbsTaskQueue 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) { diff --git a/app/src/main/assets/help_code/FtpDownload.java b/app/src/main/assets/help_code/FtpDownload.java index 2aefed21..6e97d2d2 100644 --- a/app/src/main/assets/help_code/FtpDownload.java +++ b/app/src/main/assets/help_code/FtpDownload.java @@ -19,7 +19,7 @@ import java.io.File; public class FtpDownload extends BaseActivity { 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; diff --git a/app/src/main/java/com/arialyy/simple/core/download/FtpDownloadActivity.java b/app/src/main/java/com/arialyy/simple/core/download/FtpDownloadActivity.java index 6701ef19..6ea92029 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/FtpDownloadActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/download/FtpDownloadActivity.java @@ -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 { - 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() { + + @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 class FtpDownloadActivity extends BaseActivity liveData = new MutableLiveData<>(); + private DownloadEntity singDownloadInfo; + + /** + * xx + * 单任务下载的信息 + */ + LiveData getFtpDownloadInfo(Context context) { + String url = AppUtil.getConfigValue(context, FTP_URL_KEY, ftpDefUrl); + String filePath = AppUtil.getConfigValue(context, FTP_PATH_KEY, ftpDefPath); + + singDownloadInfo = Aria.download(context).getDownloadEntity(url); + if (singDownloadInfo == null) { + singDownloadInfo = new DownloadEntity(); + singDownloadInfo.setUrl(url); + String name = getFileName(ftpDefUrl); + singDownloadInfo.setFileName(name); + singDownloadInfo.setFilePath(filePath + name); + } else { + AppUtil.setConfigValue(context, FTP_PATH_KEY, singDownloadInfo.getFilePath()); + AppUtil.setConfigValue(context, FTP_URL_KEY, singDownloadInfo.getUrl()); + } + liveData.postValue(singDownloadInfo); + + return liveData; + } + + /** + * 更新文件保存路径 + * + * @param dirPath 文件保存文件夹 + */ + void updateFilePath(Context context, String dirPath) { + if (TextUtils.isEmpty(dirPath)) { + ALog.e(TAG, "文件保存路径为空"); + return; + } + AppUtil.setConfigValue(context, FTP_PATH_KEY, dirPath); + singDownloadInfo.setFilePath(dirPath + singDownloadInfo.getFileName()); + liveData.postValue(singDownloadInfo); + } + + /** + * 更新url + */ + void uploadUrl(Context context, String url) { + if (TextUtils.isEmpty(url)) { + ALog.e(TAG, "下载地址为空"); + return; + } + AppUtil.setConfigValue(context, FTP_URL_KEY, url); + File file = new File(singDownloadInfo.getFilePath()); + String name = getFileName(url); + singDownloadInfo.setFilePath(file.getParent() + name); + singDownloadInfo.setFileName(name); + singDownloadInfo.setUrl(url); + liveData.postValue(singDownloadInfo); + } + + /** + * 通过url获取文件名 + * + * @param url ftp地址 + * @return "/aria.text" + */ + private String getFileName(String url) { + Uri uri = Uri.parse(url); + String path = uri.getPath(); + int index = path.lastIndexOf("/"); + return path.substring(index); + } +} diff --git a/app/src/main/java/com/arialyy/simple/core/download/DownloadModule1.java b/app/src/main/java/com/arialyy/simple/core/download/HttpDownloadModule.java similarity index 77% rename from app/src/main/java/com/arialyy/simple/core/download/DownloadModule1.java rename to app/src/main/java/com/arialyy/simple/core/download/HttpDownloadModule.java index 8219f7ff..b32368e2 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/DownloadModule1.java +++ b/app/src/main/java/com/arialyy/simple/core/download/HttpDownloadModule.java @@ -28,14 +28,15 @@ import com.arialyy.frame.base.BaseViewModule; import com.arialyy.simple.util.AppUtil; import java.io.File; -public class DownloadModule1 extends BaseViewModule { - private final String DOWNLOAD_URL_KEY = "DOWNLOAD_URL_KEY"; - private final String DOWNLOAD_PATH_KEY = "DOWNLOAD_PATH_KEY"; +public class HttpDownloadModule extends BaseViewModule { + private final String HTTP_URL_KEY = "HTTP_URL_KEY"; + private final String HTTP_PATH_KEY = "HTTP_PATH_KEY"; private final String defUrl = "http://hzdown.muzhiwan.com/2017/05/08/nl.noio.kingdom_59104935e56f0.apk"; private final String defFilePath = - Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath(); + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + + "/test.apk"; private MutableLiveData liveData = new MutableLiveData<>(); private DownloadEntity singDownloadInfo; @@ -43,9 +44,9 @@ public class DownloadModule1 extends BaseViewModule { /** * 单任务下载的信息 */ - LiveData getSingDownloadInfo(Context context) { - String url = AppUtil.getConfigValue(context, DOWNLOAD_URL_KEY, defUrl); - String filePath = AppUtil.getConfigValue(context, DOWNLOAD_PATH_KEY, defFilePath); + LiveData getHttpDownloadInfo(Context context) { + String url = AppUtil.getConfigValue(context, HTTP_URL_KEY, defUrl); + String filePath = AppUtil.getConfigValue(context, HTTP_PATH_KEY, defFilePath); singDownloadInfo = Aria.download(context).getDownloadEntity(url); if (singDownloadInfo == null) { @@ -55,8 +56,8 @@ public class DownloadModule1 extends BaseViewModule { singDownloadInfo.setDownloadPath(filePath); singDownloadInfo.setFileName(temp.getName()); } else { - AppUtil.setConfigValue(context, DOWNLOAD_PATH_KEY, singDownloadInfo.getDownloadPath()); - AppUtil.setConfigValue(context, DOWNLOAD_URL_KEY, singDownloadInfo.getUrl()); + AppUtil.setConfigValue(context, HTTP_PATH_KEY, singDownloadInfo.getDownloadPath()); + AppUtil.setConfigValue(context, HTTP_URL_KEY, singDownloadInfo.getUrl()); } liveData.postValue(singDownloadInfo); @@ -74,7 +75,7 @@ public class DownloadModule1 extends BaseViewModule { return; } File temp = new File(filePath); - AppUtil.setConfigValue(context, DOWNLOAD_PATH_KEY, filePath); + AppUtil.setConfigValue(context, HTTP_PATH_KEY, filePath); singDownloadInfo.setFileName(temp.getName()); singDownloadInfo.setDownloadPath(filePath); liveData.postValue(singDownloadInfo); @@ -88,7 +89,7 @@ public class DownloadModule1 extends BaseViewModule { ALog.e(TAG, "下载地址为空"); return; } - AppUtil.setConfigValue(context, DOWNLOAD_URL_KEY, url); + AppUtil.setConfigValue(context, HTTP_URL_KEY, url); singDownloadInfo.setUrl(url); liveData.postValue(singDownloadInfo); } diff --git a/app/src/main/java/com/arialyy/simple/core/download/KotlinDownloadActivity.kt b/app/src/main/java/com/arialyy/simple/core/download/KotlinDownloadActivity.kt index 93297e02..fd93a74c 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/KotlinDownloadActivity.kt +++ b/app/src/main/java/com/arialyy/simple/core/download/KotlinDownloadActivity.kt @@ -53,7 +53,7 @@ class KotlinDownloadActivity : BaseActivity() { private var mUrl: String? = null private var mFilePath: String? = null - private var mModule: DownloadModule1? = null + private var mModule: HttpDownloadModule? = null private var mTarget: DownloadTarget? = null internal var receiver: BroadcastReceiver = object : BroadcastReceiver() { @@ -93,8 +93,8 @@ class KotlinDownloadActivity : BaseActivity() { Aria.download(this) .register() mModule = ViewModelProviders.of(this) - .get(DownloadModule1::class.java) - mModule!!.getSingDownloadInfo(this) + .get(HttpDownloadModule::class.java) + mModule!!.getHttpDownloadInfo(this) .observe(this, Observer { entity -> if (entity == null) { return@Observer diff --git a/app/src/main/java/com/arialyy/simple/core/download/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/core/download/SingleTaskActivity.java index 22aa11e6..01199ded 100644 --- a/app/src/main/java/com/arialyy/simple/core/download/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/core/download/SingleTaskActivity.java @@ -55,7 +55,7 @@ public class SingleTaskActivity extends BaseActivity { private String mUrl; private String mFilePath; - private DownloadModule1 mModule; + private HttpDownloadModule mModule; private DownloadTarget mTarget; BroadcastReceiver receiver = new BroadcastReceiver() { @@ -89,8 +89,8 @@ public class SingleTaskActivity extends BaseActivity { super.init(savedInstanceState); setTitle("单任务下载"); Aria.download(this).register(); - mModule = ViewModelProviders.of(this).get(DownloadModule1.class); - mModule.getSingDownloadInfo(this).observe(this, new Observer() { + mModule = ViewModelProviders.of(this).get(HttpDownloadModule.class); + mModule.getHttpDownloadInfo(this).observe(this, new Observer() { @Override public void onChanged(@Nullable DownloadEntity entity) { if (entity == null) { @@ -194,6 +194,7 @@ public class SingleTaskActivity extends BaseActivity { void taskStart(DownloadTask task) { if (task.getKey().equals(mUrl)) { getBinding().setFileSize(task.getConvertFileSize()); + ALog.d(TAG, "isComplete = " + task.isComplete() + ", state = " + task.getState()); } } @@ -236,9 +237,6 @@ public class SingleTaskActivity extends BaseActivity { } } - /** - * - */ @Download.onTaskFail void taskFail(DownloadTask task, Exception e) { if (task.getKey().equals(mUrl)) { @@ -274,9 +272,6 @@ public class SingleTaskActivity extends BaseActivity { startD(); } break; - case R.id.stop: - Aria.download(this).load(mUrl).stop(); - break; case R.id.cancel: Aria.download(this).load(mUrl).cancel(true); break; diff --git a/app/src/main/res/layout/activity_ftp_download.xml b/app/src/main/res/layout/activity_ftp_download.xml index 46ac3b31..b6f161b1 100644 --- a/app/src/main/res/layout/activity_ftp_download.xml +++ b/app/src/main/res/layout/activity_ftp_download.xml @@ -2,6 +2,7 @@ + + + + + + + + + + + + - + \ No newline at end of file diff --git a/build.gradle b/build.gradle index f9f4fbf7..fe7e1ccc 100644 --- a/build.gradle +++ b/build.gradle @@ -43,7 +43,7 @@ task clean(type: Delete) { ext { userOrg = 'arialyy' groupId = 'com.arialyy.aria' - publishVersion = '3.6.5_dev_1' + publishVersion = '3.6.5_dev_4' // publishVersion = '1.0.4' //FTP插件 repoName='maven' desc = 'android 下载框架'