pull/709/head
parent
0476c3a941
commit
1f2ac36580
@ -0,0 +1,125 @@ |
||||
/* |
||||
* 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.util; |
||||
|
||||
import android.text.TextUtils; |
||||
import com.arialyy.aria.core.TaskRecord; |
||||
import com.arialyy.aria.core.ThreadRecord; |
||||
import com.arialyy.aria.core.common.AbsEntity; |
||||
import com.arialyy.aria.core.download.DownloadEntity; |
||||
import com.arialyy.aria.core.download.DownloadGroupEntity; |
||||
import com.arialyy.aria.core.loader.IRecordHandler; |
||||
import com.arialyy.aria.core.wrapper.RecordWrapper; |
||||
import com.arialyy.aria.orm.DbEntity; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 删除组合任务记录 |
||||
*/ |
||||
public class DeleteDGRecord implements IDeleteRecord { |
||||
private String TAG = CommonUtil.getClassName(this); |
||||
|
||||
private static volatile DeleteDGRecord INSTANCE = null; |
||||
|
||||
private DeleteDGRecord(){ |
||||
|
||||
} |
||||
|
||||
public static DeleteDGRecord getInstance() { |
||||
if (INSTANCE == null){ |
||||
synchronized (DeleteDGRecord.class){ |
||||
if (INSTANCE == null){ |
||||
INSTANCE = new DeleteDGRecord(); |
||||
} |
||||
} |
||||
} |
||||
return INSTANCE; |
||||
} |
||||
|
||||
/** |
||||
* @param dirPath 组合任务保存路径 |
||||
* @param needRemoveFile true,无论下载成功以否,都将删除下载下来的文件。false,只会删除下载任务没有完成的文件 |
||||
* @param needRemoveEntity 是否需要删除实体,true 删除实体 |
||||
*/ |
||||
@Override |
||||
public void deleteRecord(String dirPath, boolean needRemoveFile, boolean needRemoveEntity) { |
||||
if (TextUtils.isEmpty(dirPath)) { |
||||
ALog.e(TAG, "删除下载任务组记录失败,组合任务路径为空"); |
||||
return; |
||||
} |
||||
deleteRecord(DbDataHelper.getDGEntityByPath(dirPath), needRemoveFile, needRemoveEntity); |
||||
} |
||||
|
||||
@Override |
||||
public void deleteRecord(AbsEntity absEntity, boolean needRemoveFile, boolean needRemoveEntity) { |
||||
if (absEntity == null) { |
||||
ALog.e(TAG, "删除组合任务记录失败,组合任务实体为空"); |
||||
return; |
||||
} |
||||
DownloadGroupEntity groupEntity = (DownloadGroupEntity) absEntity; |
||||
|
||||
List<RecordWrapper> records = |
||||
DbEntity.findRelationData(RecordWrapper.class, "dGroupHash=?", groupEntity.getGroupHash()); |
||||
|
||||
// 删除子任务记录
|
||||
if (records == null || records.isEmpty()) { |
||||
ALog.w(TAG, "组任务记录已删除"); |
||||
} else { |
||||
for (RecordWrapper record : records) { |
||||
if (record == null || record.taskRecord == null) { |
||||
continue; |
||||
} |
||||
// 删除分块文件
|
||||
if (record.taskRecord.isBlock) { |
||||
removeBlockFile(record.taskRecord); |
||||
} |
||||
DbEntity.deleteData(ThreadRecord.class, "taskKey=?", record.taskRecord.filePath); |
||||
record.taskRecord.deleteData(); |
||||
} |
||||
} |
||||
|
||||
// 删除组合任务子任务的文件
|
||||
List<DownloadEntity> subs = groupEntity.getSubEntities(); |
||||
if (subs != null) { |
||||
for (DownloadEntity sub : subs) { |
||||
if (needRemoveFile || !groupEntity.isComplete()) { |
||||
FileUtil.deleteFile(sub.getFilePath()); |
||||
} |
||||
} |
||||
} |
||||
|
||||
// 删除文件夹
|
||||
if (!TextUtils.isEmpty(groupEntity.getDirPath())) { |
||||
if (needRemoveFile || !groupEntity.isComplete()) { |
||||
FileUtil.deleteFile(groupEntity.getDirPath()); |
||||
} |
||||
} |
||||
|
||||
if (needRemoveEntity) { |
||||
DbEntity.deleteData(DownloadEntity.class, "groupHash=?", groupEntity.getGroupHash()); |
||||
DbEntity.deleteData(DownloadGroupEntity.class, "groupHash=?", groupEntity.getGroupHash()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 删除多线程分块下载的分块文件 |
||||
*/ |
||||
private void removeBlockFile(TaskRecord record) { |
||||
for (int i = 0, len = record.threadNum; i < len; i++) { |
||||
FileUtil.deleteFile(String.format(IRecordHandler.SUB_PATH, record.filePath, i)); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,121 @@ |
||||
/* |
||||
* 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.util; |
||||
|
||||
import android.text.TextUtils; |
||||
import com.arialyy.aria.core.TaskRecord; |
||||
import com.arialyy.aria.core.ThreadRecord; |
||||
import com.arialyy.aria.core.common.AbsEntity; |
||||
import com.arialyy.aria.core.download.DownloadEntity; |
||||
import com.arialyy.aria.core.loader.IRecordHandler; |
||||
import com.arialyy.aria.core.wrapper.ITaskWrapper; |
||||
import com.arialyy.aria.orm.DbEntity; |
||||
import java.io.File; |
||||
|
||||
/** |
||||
* 删除下载记录 |
||||
*/ |
||||
public class DeleteDRecord implements IDeleteRecord { |
||||
private String TAG = CommonUtil.getClassName(this); |
||||
private static volatile DeleteDRecord INSTANCE = null; |
||||
|
||||
private DeleteDRecord(){ |
||||
|
||||
} |
||||
|
||||
public static DeleteDRecord getInstance() { |
||||
if (INSTANCE == null){ |
||||
synchronized (DeleteDRecord.class){ |
||||
if (INSTANCE == null){ |
||||
INSTANCE = new DeleteDRecord(); |
||||
} |
||||
} |
||||
} |
||||
return INSTANCE; |
||||
} |
||||
|
||||
/** |
||||
* @param filePath 文件保存路径 |
||||
* @param removeTarget true,无论下载成功以否,都将删除下载下来的文件。false,只会删除下载任务没有完成的文件 |
||||
* @param needRemoveEntity 是否需要删除实体 |
||||
*/ |
||||
@Override public void deleteRecord(String filePath, boolean removeTarget, |
||||
boolean needRemoveEntity) { |
||||
if (TextUtils.isEmpty(filePath)) { |
||||
throw new NullPointerException("删除记录失败,文件路径为空"); |
||||
} |
||||
if (!filePath.startsWith("/")) { |
||||
throw new IllegalArgumentException(String.format("文件路径错误,filePath:%s", filePath)); |
||||
} |
||||
DownloadEntity entity = DbEntity.findFirst(DownloadEntity.class, "downloadPath=?", filePath); |
||||
if (entity == null) { |
||||
ALog.e(TAG, "删除下载记录失败,没有在数据库中找到对应的实体文件,filePath:" + filePath); |
||||
return; |
||||
} |
||||
deleteRecord(entity, removeTarget, needRemoveEntity); |
||||
} |
||||
|
||||
/** |
||||
* @param absEntity 记录关联的实体 |
||||
* @param needRemoveFile true,无论下载成功以否,都将删除下载下来的文件。false,只会删除下载任务没有完成的文件 |
||||
* @param needRemoveEntity 是否需要删除实体 |
||||
*/ |
||||
@Override |
||||
public void deleteRecord(AbsEntity absEntity, boolean needRemoveFile, boolean needRemoveEntity) { |
||||
if (absEntity == null) { |
||||
ALog.e(TAG, "删除下载记录失败,实体为空"); |
||||
return; |
||||
} |
||||
DownloadEntity entity = (DownloadEntity) absEntity; |
||||
final String filePath = entity.getFilePath(); |
||||
File targetFile = new File(filePath); |
||||
|
||||
// 兼容以前版本
|
||||
if (entity.getTaskType() == ITaskWrapper.M3U8_VOD || entity.getTaskType() == ITaskWrapper.M3U8_LIVE){ |
||||
DeleteM3u8Record.getInstance().deleteRecord(entity, needRemoveFile, needRemoveEntity); |
||||
return; |
||||
} |
||||
|
||||
TaskRecord record = DbDataHelper.getTaskRecord(entity.getFilePath(), entity.getTaskType()); |
||||
if (record == null) { |
||||
ALog.e(TAG, "删除下载记录失败,记录为空,filePath:" + entity.getFilePath()); |
||||
return; |
||||
} |
||||
|
||||
DbEntity.deleteData(ThreadRecord.class, "taskKey=? AND threadType=?", filePath, |
||||
String.valueOf(entity.getTaskType())); |
||||
|
||||
if (needRemoveEntity || !entity.isComplete()) { |
||||
FileUtil.deleteFile(targetFile); |
||||
if (record.isBlock) { |
||||
removeBlockFile(record); |
||||
} |
||||
} |
||||
|
||||
if (needRemoveEntity) { |
||||
DbEntity.deleteData(DownloadEntity.class, "downloadPath=?", filePath); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 删除多线程分块下载的分块文件 |
||||
*/ |
||||
private void removeBlockFile(TaskRecord record) { |
||||
for (int i = 0, len = record.threadNum; i < len; i++) { |
||||
FileUtil.deleteFile(String.format(IRecordHandler.SUB_PATH, record.filePath, i)); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,131 @@ |
||||
/* |
||||
* 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.util; |
||||
|
||||
import android.text.TextUtils; |
||||
import com.arialyy.aria.core.TaskRecord; |
||||
import com.arialyy.aria.core.common.AbsEntity; |
||||
import com.arialyy.aria.core.download.DownloadEntity; |
||||
import com.arialyy.aria.core.download.M3U8Entity; |
||||
import com.arialyy.aria.orm.DbEntity; |
||||
import java.io.File; |
||||
|
||||
/** |
||||
* 删除m3u8记录 |
||||
*/ |
||||
public class DeleteM3u8Record implements IDeleteRecord { |
||||
private String TAG = CommonUtil.getClassName(this); |
||||
|
||||
private static volatile DeleteM3u8Record INSTANCE = null; |
||||
|
||||
private DeleteM3u8Record() { |
||||
|
||||
} |
||||
|
||||
public static DeleteM3u8Record getInstance() { |
||||
if (INSTANCE == null) { |
||||
synchronized (DeleteM3u8Record.class) { |
||||
if (INSTANCE == null) { |
||||
INSTANCE = new DeleteM3u8Record(); |
||||
} |
||||
} |
||||
} |
||||
return INSTANCE; |
||||
} |
||||
|
||||
/** |
||||
* @param filePath 文件保存路径 |
||||
* @param removeTarget true,无论下载成功以否,都将删除下载下来的文件。false,只会删除下载任务没有完成的文件 |
||||
* @param needRemoveEntity 是否需要删除实体 |
||||
*/ |
||||
@Override public void deleteRecord(String filePath, boolean removeTarget, |
||||
boolean needRemoveEntity) { |
||||
if (TextUtils.isEmpty(filePath)) { |
||||
throw new NullPointerException("删除记录失败,文件路径为空"); |
||||
} |
||||
if (!filePath.startsWith("/")) { |
||||
throw new IllegalArgumentException(String.format("文件路径错误,filePath:%s", filePath)); |
||||
} |
||||
DownloadEntity entity = DbEntity.findFirst(DownloadEntity.class, "downloadPath=?", filePath); |
||||
if (entity == null) { |
||||
ALog.e(TAG, "删除下载记录失败,没有在数据库中找到对应的实体文件,filePath:" + filePath); |
||||
return; |
||||
} |
||||
deleteRecord(entity, removeTarget, needRemoveEntity); |
||||
} |
||||
|
||||
@Override |
||||
public void deleteRecord(AbsEntity absEntity, boolean needRemoveFile, boolean needRemoveEntity) { |
||||
if (absEntity == null) { |
||||
ALog.e(TAG, "删除下载记录失败,实体为空"); |
||||
return; |
||||
} |
||||
|
||||
DownloadEntity entity = (DownloadEntity) absEntity; |
||||
TaskRecord record = DbDataHelper.getTaskRecord(entity.getFilePath(), entity.getTaskType()); |
||||
if (record == null) { |
||||
ALog.e(TAG, "删除下载记录失败,记录为空,filePath:" + entity.getFilePath()); |
||||
return; |
||||
} |
||||
final String filePath = entity.getFilePath(); |
||||
|
||||
DbEntity.deleteData(M3U8Entity.class, "filePath=?", filePath); |
||||
|
||||
if (needRemoveFile || !entity.isComplete()) { |
||||
removeTsCache(new File(filePath), record.bandWidth); |
||||
FileUtil.deleteFile(filePath); |
||||
} |
||||
|
||||
if (needRemoveEntity) { |
||||
DbEntity.deleteData(DownloadEntity.class, "downloadPath=?", filePath); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 删除ts文件,和索引文件(如果有的话) |
||||
*/ |
||||
private static void removeTsCache(File targetFile, long bandWidth) { |
||||
|
||||
String cacheDir = null; |
||||
if (!targetFile.isDirectory()) { |
||||
cacheDir = |
||||
String.format("%s/.%s_%s", targetFile.getParent(), targetFile.getName(), bandWidth); |
||||
} |
||||
|
||||
if (!TextUtils.isEmpty(cacheDir)) { |
||||
File cacheDirF = new File(cacheDir); |
||||
if (!cacheDirF.exists()) { |
||||
return; |
||||
} |
||||
File[] files = cacheDirF.listFiles(); |
||||
for (File f : files) { |
||||
if (f.exists()) { |
||||
f.delete(); |
||||
} |
||||
} |
||||
File cDir = new File(cacheDir); |
||||
if (cDir.exists()) { |
||||
cDir.delete(); |
||||
} |
||||
} |
||||
|
||||
File indexFile = new File(String.format("%s.index", targetFile.getPath())); |
||||
|
||||
if (indexFile.exists()) { |
||||
indexFile.delete(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,90 @@ |
||||
/* |
||||
* 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.util; |
||||
|
||||
import android.text.TextUtils; |
||||
import com.arialyy.aria.core.TaskRecord; |
||||
import com.arialyy.aria.core.common.AbsEntity; |
||||
import com.arialyy.aria.core.upload.UploadEntity; |
||||
import com.arialyy.aria.orm.DbEntity; |
||||
|
||||
/** |
||||
* 删除上传记录 |
||||
*/ |
||||
public class DeleteURecord implements IDeleteRecord { |
||||
private String TAG = CommonUtil.getClassName(this); |
||||
|
||||
private static volatile DeleteURecord INSTANCE = null; |
||||
|
||||
private DeleteURecord() { |
||||
|
||||
} |
||||
|
||||
public static DeleteURecord getInstance() { |
||||
if (INSTANCE == null) { |
||||
synchronized (DeleteURecord.class) { |
||||
if (INSTANCE == null) { |
||||
INSTANCE = new DeleteURecord(); |
||||
} |
||||
} |
||||
} |
||||
return INSTANCE; |
||||
} |
||||
|
||||
/** |
||||
* 删除上传记录 |
||||
* |
||||
* @param filePath 上传文件的地址 |
||||
* @param needRemoveFile 上传完成后是否需要删除本地文件。true,上传完成后删除本地文件 |
||||
* @param needRemoveEntity 是否需要删除实体,true 删除实体 |
||||
*/ |
||||
@Override public void deleteRecord(String filePath, boolean needRemoveFile, |
||||
boolean needRemoveEntity) { |
||||
if (TextUtils.isEmpty(filePath)) { |
||||
throw new NullPointerException("删除记录失败,文件路径为空"); |
||||
} |
||||
if (!filePath.startsWith("/")) { |
||||
throw new IllegalArgumentException(String.format("文件路径错误,filePath:%s", filePath)); |
||||
} |
||||
|
||||
UploadEntity entity = DbEntity.findFirst(UploadEntity.class, "filePath=?", filePath); |
||||
if (entity == null) { |
||||
ALog.e(TAG, "删除上传记录失败,没有在数据库中找到对应的实体文件,filePath:" + filePath); |
||||
return; |
||||
} |
||||
deleteRecord(entity, needRemoveFile, needRemoveEntity); |
||||
} |
||||
|
||||
@Override |
||||
public void deleteRecord(AbsEntity absEntity, boolean needRemoveFile, boolean needRemoveEntity) { |
||||
if (absEntity == null) { |
||||
ALog.e(TAG, "删除上传记录失败,实体为空"); |
||||
return; |
||||
} |
||||
|
||||
UploadEntity entity = (UploadEntity) absEntity; |
||||
DbEntity.deleteData(TaskRecord.class, "filePath=? AND taskType=?", entity.getFilePath(), |
||||
String.valueOf(entity.getTaskType())); |
||||
|
||||
if (needRemoveFile) { |
||||
FileUtil.deleteFile(entity.getFilePath()); |
||||
} |
||||
|
||||
if (needRemoveEntity) { |
||||
DbEntity.deleteData(UploadEntity.class, "filePath=?", entity.getFilePath()); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,42 @@ |
||||
/* |
||||
* 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.util; |
||||
|
||||
import com.arialyy.aria.core.common.AbsEntity; |
||||
|
||||
/** |
||||
* 删除记录 |
||||
*/ |
||||
public interface IDeleteRecord { |
||||
|
||||
/** |
||||
* 删除记录 |
||||
* |
||||
* @param key 记录关联的key |
||||
* @param needRemoveFile 是否需要删除文件 |
||||
* @param needRemoveEntity 是否需要删除实体,true 删除实体 |
||||
*/ |
||||
void deleteRecord(String key, boolean needRemoveFile, boolean needRemoveEntity); |
||||
|
||||
/** |
||||
* 删除记录 |
||||
* |
||||
* @param absEntity 记录关联的实体 |
||||
* @param needRemoveFile 是否需要删除文件 |
||||
* @param needRemoveEntity 是否需要删除实体,true 删除实体 |
||||
*/ |
||||
void deleteRecord(AbsEntity absEntity, boolean needRemoveFile, boolean needRemoveEntity); |
||||
} |
@ -0,0 +1,18 @@ |
||||
package com.example.arial.downloaddemo |
||||
|
||||
import androidx.test.runner.AndroidJUnit4 |
||||
import com.arialyy.aria.util.CommonUtil |
||||
import org.junit.Test |
||||
import org.junit.runner.RunWith |
||||
import kotlin.math.pow |
||||
|
||||
@RunWith(AndroidJUnit4::class) |
||||
class ApiTest { |
||||
|
||||
@Test |
||||
fun testSpeed() { |
||||
val speed = 1024.0.pow(4.0) * 2 |
||||
println("speed = ${CommonUtil.formatFileSize(speed)}") |
||||
|
||||
} |
||||
} |
@ -1,30 +0,0 @@ |
||||
/* |
||||
* Copyright (C) 2016 AriaLyy(DownloadUtil) |
||||
* |
||||
* 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.example.arial.downloaddemo; |
||||
|
||||
import android.app.Application; |
||||
import android.test.ApplicationTestCase; |
||||
|
||||
/** |
||||
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a> |
||||
*/ |
||||
public class ApplicationTest extends ApplicationTestCase<Application> { |
||||
public ApplicationTest() { |
||||
super(Application.class); |
||||
} |
||||
} |
Loading…
Reference in new issue