代码优化

pull/330/head
AriaLyy 8 years ago
parent 76b9987f46
commit 80f3a38fc6
  1. 2
      .idea/misc.xml
  2. 2
      .idea/modules.xml
  3. 1
      Aria/src/main/java/com/arialyy/aria/core/Aria.java
  4. 13
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTarget.java
  5. 6
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java
  6. 40
      DownloadApi.md
  7. 161
      README.md
  8. 3
      app/src/main/java/com/arialyy/simple/download/DownloadDialog.java
  9. 3
      app/src/main/java/com/arialyy/simple/download/DownloadPopupWindow.java
  10. 2
      app/src/main/java/com/arialyy/simple/download/SimpleNotification.java
  11. 3
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  12. 2
      app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java
  13. 2
      app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java
  14. 2
      app/src/main/java/com/arialyy/simple/upload/UploadActivity.java

@ -37,7 +37,7 @@
<ConfirmationsSetting value="0" id="Add" /> <ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" /> <ConfirmationsSetting value="0" id="Remove" />
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">

@ -2,8 +2,8 @@
<project version="4"> <project version="4">
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/Aria/Aria.iml" filepath="$PROJECT_DIR$/Aria/Aria.iml" />
<module fileurl="file://$PROJECT_DIR$/Aria.iml" filepath="$PROJECT_DIR$/Aria.iml" /> <module fileurl="file://$PROJECT_DIR$/Aria.iml" filepath="$PROJECT_DIR$/Aria.iml" />
<module fileurl="file://$PROJECT_DIR$/Aria/Aria.iml" filepath="$PROJECT_DIR$/Aria/Aria.iml" />
<module fileurl="file://$PROJECT_DIR$/Aria/Aria-Aria.iml" filepath="$PROJECT_DIR$/Aria/Aria-Aria.iml" /> <module fileurl="file://$PROJECT_DIR$/Aria/Aria-Aria.iml" filepath="$PROJECT_DIR$/Aria/Aria-Aria.iml" />
<module fileurl="file://$PROJECT_DIR$/AriaPrj.iml" filepath="$PROJECT_DIR$/AriaPrj.iml" /> <module fileurl="file://$PROJECT_DIR$/AriaPrj.iml" filepath="$PROJECT_DIR$/AriaPrj.iml" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" /> <module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />

@ -43,7 +43,6 @@ import com.arialyy.aria.core.upload.UploadTask;
* .load(DOWNLOAD_URL) //下载地址,必填 * .load(DOWNLOAD_URL) //下载地址,必填
* //文件保存路径,必填 * //文件保存路径,必填
* .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk") * .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk")
* .setDownloadName("test.apk") //文件名,必填
* .start(); * .start();
* </code> * </code>
* <code> * <code>

@ -21,6 +21,7 @@ import com.arialyy.aria.core.RequestEnum;
import com.arialyy.aria.core.inf.AbsTarget; import com.arialyy.aria.core.inf.AbsTarget;
import com.arialyy.aria.core.queue.DownloadTaskQueue; import com.arialyy.aria.core.queue.DownloadTaskQueue;
import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CheckUtil;
import java.io.File;
import java.util.Map; import java.util.Map;
/** /**
@ -35,6 +36,14 @@ public class DownloadTarget extends AbsTarget<DownloadEntity, DownloadTaskEntity
taskEntity = new DownloadTaskEntity(entity); taskEntity = new DownloadTaskEntity(entity);
} }
@Override public void pause() {
super.pause();
}
@Override public void resume() {
super.resume();
}
/** /**
* 给url请求添加头部 * 给url请求添加头部
* *
@ -73,13 +82,16 @@ public class DownloadTarget extends AbsTarget<DownloadEntity, DownloadTaskEntity
if (TextUtils.isEmpty(downloadPath)) { if (TextUtils.isEmpty(downloadPath)) {
throw new IllegalArgumentException("文件保持路径不能为null"); throw new IllegalArgumentException("文件保持路径不能为null");
} }
File file = new File(downloadPath);
entity.setDownloadPath(downloadPath); entity.setDownloadPath(downloadPath);
entity.setFileName(file.getName());
return this; return this;
} }
/** /**
* 设置文件名 * 设置文件名
*/ */
@Deprecated
public DownloadTarget setDownloadName(@NonNull String downloadName) { public DownloadTarget setDownloadName(@NonNull String downloadName) {
if (TextUtils.isEmpty(downloadName)) { if (TextUtils.isEmpty(downloadName)) {
throw new IllegalArgumentException("文件名不能为null"); throw new IllegalArgumentException("文件名不能为null");
@ -88,7 +100,6 @@ public class DownloadTarget extends AbsTarget<DownloadEntity, DownloadTaskEntity
return this; return this;
} }
private DownloadEntity getDownloadEntity(String downloadUrl) { private DownloadEntity getDownloadEntity(String downloadUrl) {
CheckUtil.checkDownloadUrl(downloadUrl); CheckUtil.checkDownloadUrl(downloadUrl);
return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl); return DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl);

@ -141,7 +141,7 @@ public class AbsTarget<ENTITY extends IEntity, TASK_ENTITY extends ITaskEntity>
/** /**
* 停止下载 * 停止下载
*/ */
public void stop() { protected void pause() {
AriaManager.getInstance(AriaManager.APP) AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createCmd(targetName, taskEntity, CmdFactory.TASK_STOP)) .setCmd(CommonUtil.createCmd(targetName, taskEntity, CmdFactory.TASK_STOP))
.exe(); .exe();
@ -150,7 +150,7 @@ public class AbsTarget<ENTITY extends IEntity, TASK_ENTITY extends ITaskEntity>
/** /**
* 恢复下载 * 恢复下载
*/ */
public void resume() { protected void resume() {
AriaManager.getInstance(AriaManager.APP) AriaManager.getInstance(AriaManager.APP)
.setCmd(CommonUtil.createCmd(targetName, taskEntity, CmdFactory.TASK_START)) .setCmd(CommonUtil.createCmd(targetName, taskEntity, CmdFactory.TASK_START))
.exe(); .exe();
@ -168,7 +168,7 @@ public class AbsTarget<ENTITY extends IEntity, TASK_ENTITY extends ITaskEntity>
/** /**
* 重新下载 * 重新下载
*/ */
public void reStart() { void reStart() {
cancel(); cancel();
start(); start();
} }

@ -0,0 +1,40 @@
## 关于Aria,你还需要知道的一些东西
- 设置下载任务数,Aria默认下载任务为**2**
```java
Aria.get(getContext()).setMaxDownloadNum(num);
```
- 停止所有下载
```java
Aria.get(this).stopAllTask();
```
- 设置失败重试次数,从事次数不能少于 1
```java
Aria.get(this).setReTryNum(10);
```
- 设置失败重试间隔,重试间隔不能小于 5000ms
```java
Aria.get(this).setReTryInterval(5000);
```
- 设置是否打开广播,如果你需要在Service后台获取下载完成情况,那么你需要打开Aria广播,[Aria广播配置](https://github.com/AriaLyy/Aria/blob/v_2.0/BroadCast.md)
```java
Aria.get(this).openBroadcast(true);
```
## https证书配置
+ 将你的证书导入`assets`目录
+ 调用以下代码配置ca证书相关信息
```java
/**
* 设置CA证书信息
*
* @param caAlias ca证书别名
* @param caPath assets 文件夹下的ca证书完整路径
*/
Aria.get(this).setCAInfo("caAlias","caPath");
```

@ -1,16 +1,17 @@
# Aria # Aria
![图标](https://github.com/AriaLyy/DownloadUtil/blob/v_2.0/app/src/main/res/mipmap-hdpi/ic_launcher.png)</br> ![图标](https://github.com/AriaLyy/DownloadUtil/blob/v_2.0/app/src/main/res/mipmap-hdpi/ic_launcher.png)</br>
Aria,致力于让下载傻瓜化</br> Aria,让上传、下载更容易实现</br>
+ Aria有以下特点: + Aria有以下特点:
- 简单 - 简单
- 可在Dialog、popupWindow等组件中使用 - 可在Dialog、popupWindow等组件中使用
- 可自定义是否使用广播
- 支持多线程、多任务下载 - 支持多线程、多任务下载
- 支持任务自动切换 - 支持多任务自动调度
- 支持下载速度直接获取 - 可以直接获取速度
- 支持https地址下载 - 支持https地址下载
[Aria怎样使用?](#使用) Aria怎样使用?
* [下载](#使用)
* [上传](#上传)
如果你觉得Aria对你有帮助,您的star和issues将是对我最大支持.`^_^` 如果你觉得Aria对你有帮助,您的star和issues将是对我最大支持.`^_^`
@ -24,113 +25,115 @@ compile 'com.arialyy.aria:Aria:2.4.0'
![多任务下载](https://github.com/AriaLyy/DownloadUtil/blob/v_2.0/img/download_img.gif) ![多任务下载](https://github.com/AriaLyy/DownloadUtil/blob/v_2.0/img/download_img.gif)
![Dialog使用](https://github.com/AriaLyy/DownloadUtil/blob/v_2.0/img/dialog_use.gif "") ![Dialog使用](https://github.com/AriaLyy/DownloadUtil/blob/v_2.0/img/dialog_use.gif "")
## 性能展示 ## 性能
![性能展示](https://github.com/AriaLyy/DownloadUtil/blob/v_2.0/img/performance.png) ![性能展示](https://github.com/AriaLyy/DownloadUtil/blob/master/img/performance.png)
*** ***
## 使用 ## 使用
### 一、添加权限 由于Aria涉及到文件和网络的操作,因此需要你在manifest文件中添加以下权限
```xml ```xml
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
``` ```
### 二、只需要以下参数,你便能很简单的使用Aria下载文件了
```java
Aria.whit(this)
.load(DOWNLOAD_URL) //下载地址,必填
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk") //文件保存路径,必填
.setDownloadName("test.apk") //文件名,必填
.start();
```
### 三、为了能接收到Aria传递的数据,你需要把你的Activity或fragment注册到Aria管理器中,注册的方式很简单,在onResume
```java
@Override protected void onResume() {
super.onResume();
Aria.whit(this).addSchedulerListener(new MySchedulerListener());
}
```
### 四、通过下载链接,你还能使用Aria执行很多操作,如:
Aria支持https下载,如果你希望使用自己的ca证书,那么你需要进行[Aria https证书配置](#https证书配置)
- 添加任务(不进行下载)
## 使用Aria进行下载
* 添加任务(不进行下载),当其他下载任务完成时,将自动下载等待中的任务
```java ```java
Aria.whit(this).load(DOWNLOAD_URL) Aria.download(this)
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk") //文件保存路径,必填 .load(DOWNLOAD_URL)
.setDownloadName("test.apk") //文件名,必填 .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk") //文件保存路径
.add(); .add();
```
- 启动下载
```java
Aria.whit(this).load(DOWNLOAD_URL).start();
```
- 暂停下载
```java
Aria.whit(this).load(DOWNLOAD_URL).stop();
``` ```
- 恢复下载
```java * 下载
Aria.whit(this).load(DOWNLOAD_URL).resume();
```
- 取消下载
```java ```java
Aria.whit(this).load(DOWNLOAD_URL).cancel(); Aria.download(this)
.load(DOWNLOAD_URL) //读取下载地址
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk") //设置文件保存的完整路径
.start(); //启动下载
``` ```
- 获取当前下载进度 * 暂停
```java ```java
Aria.whit(this).load(DOWNLOAD_URL).getCurrentProgress(); Aria.download(this).load(DOWNLOAD_URL).pause();
``` ```
- 获取文件大小 * 恢复下载
```java ```java
Aria.whit(this).load(DOWNLOAD_URL).getFileSize(); Aria.download(this).load(DOWNLOAD_URL).resume();
``` ```
* 取消下载
### 五、关于Aria,你还需要知道的一些东西
- 设置下载任务数,Aria默认下载任务为**2**
```java ```java
Aria.get(getContext()).setMaxDownloadNum(num); Aria.download(this).load(DOWNLOAD_URL).cancel();
``` ```
- 停止所有下载
```java ### 二、如果你希望读取下载进度或下载信息,那么你需要创建事件类,并在onResume(Activity、Fragment)或构造函数(Dialog、PopupWindow),将该事件类注册到Aria管理器。
Aria.get(this).stopAllTask(); * 创建事件类
```
- 设置失败重试次数,从事次数不能少于 1
```java ```java
Aria.get(this).setReTryNum(10); final static class MySchedulerListener extends Aria.DownloadSchedulerListener{
@Override public void onTaskPre(DownloadTask task) {
super.onTaskPre(task);
}
@Override public void onTaskStop(DownloadTask task) {
super.onTaskStop(task);
}
@Override public void onTaskCancel(DownloadTask task) {
super.onTaskCancel(task);
}
@Override public void onTaskRunning(DownloadTask task) {
super.onTaskRunning(task);
}
}
``` ```
- 设置失败重试间隔,重试间隔不能小于 5000ms
```java * 将事件注册到Aria
Aria.get(this).setReTryInterval(5000);
```
- 设置是否打开广播,如果你需要在Service后台获取下载完成情况,那么你需要打开Aria广播,[Aria广播配置](https://github.com/AriaLyy/Aria/blob/v_2.0/BroadCast.md)
```java ```java
Aria.get(this).openBroadcast(true); @Override protected void onResume() {
super.onResume();
Aria.whit(this).addSchedulerListener(new MySchedulerListener());
}
``` ```
### https证书配置 ### 关于下载的其它api
+ 将你的证书导入`assets`目录 [Download API]()
+ 调用以下代码配置ca证书相关信息
**tips:为了防止内存泄露的情况,事件类需要使用staic进行修饰**
```java
/** ## 上传
* 设置CA证书信息 * 添加任务(只添加,不上传)
*
* @param caAlias ca证书别名 ```java
* @param caPath assets 文件夹下的ca证书完整路径 Aria.upload(this)
*/ .load(filePath) //文件路径
Aria.get(this).setCAInfo("caAlias","caPath"); .setUploadUrl(uploadUrl) //上传路径
``` .setAttachment(fileKey) //服务器读取文件的key
.add();
```
* 上传
```java
Aria.upload(this)
.load(filePath) //文件路径
.setUploadUrl(uploadUrl) //上传路径
.setAttachment(fileKey) //服务器读取文件的key
.start();
```
* 取消上传
```java
Aria.upload(this)
.load(filePath)
.cancel();
```
*** ***
## 开发日志 ## 开发日志

@ -62,11 +62,10 @@ public class DownloadDialog extends AbsDialog {
Aria.download(this) Aria.download(this)
.load(DOWNLOAD_URL) .load(DOWNLOAD_URL)
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/daialog.apk") .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/daialog.apk")
.setDownloadName("daialog.apk")
.start(); .start();
break; break;
case R.id.stop: case R.id.stop:
Aria.download(this).load(DOWNLOAD_URL).stop(); Aria.download(this).load(DOWNLOAD_URL).pause();
break; break;
case R.id.cancel: case R.id.cancel:
Aria.download(this).load(DOWNLOAD_URL).cancel(); Aria.download(this).load(DOWNLOAD_URL).cancel();

@ -64,11 +64,10 @@ public class DownloadPopupWindow extends AbsPopupWindow {
Aria.download(this) Aria.download(this)
.load(DOWNLOAD_URL) .load(DOWNLOAD_URL)
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/daialog.apk") .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/daialog.apk")
.setDownloadName("daialog.apk")
.start(); .start();
break; break;
case R.id.stop: case R.id.stop:
Aria.download(this).load(DOWNLOAD_URL).stop(); Aria.download(this).load(DOWNLOAD_URL).pause();
break; break;
case R.id.cancel: case R.id.cancel:
Aria.download(this).load(DOWNLOAD_URL).cancel(); Aria.download(this).load(DOWNLOAD_URL).cancel();

@ -47,7 +47,7 @@ public class SimpleNotification {
} }
public void stop() { public void stop() {
Aria.download(mContext).load(DOWNLOAD_URL).stop(); Aria.download(mContext).load(DOWNLOAD_URL).pause();
} }
private static class DownloadCallback extends Aria.DownloadSchedulerListener { private static class DownloadCallback extends Aria.DownloadSchedulerListener {

@ -227,12 +227,11 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
Aria.download(this) Aria.download(this)
.load(DOWNLOAD_URL) .load(DOWNLOAD_URL)
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk") .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk")
.setDownloadName("test.apk")
.start(); .start();
} }
private void stop() { private void stop() {
Aria.download(this).load(DOWNLOAD_URL).stop(); Aria.download(this).load(DOWNLOAD_URL).pause();
} }
private void cancel() { private void cancel() {

@ -62,7 +62,7 @@ public class DownloadFragment extends AbsFragment<FragmentDownloadBinding> {
.start(); .start();
break; break;
case R.id.stop: case R.id.stop:
Aria.download(this).load(DOWNLOAD_URL).stop(); Aria.download(this).load(DOWNLOAD_URL).pause();
break; break;
case R.id.cancel: case R.id.cancel:
Aria.download(this).load(DOWNLOAD_URL).cancel(); Aria.download(this).load(DOWNLOAD_URL).cancel();

@ -187,7 +187,7 @@ final class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapter
} }
private void stop(DownloadEntity entity) { private void stop(DownloadEntity entity) {
Aria.download(getContext()).load(entity).stop(); Aria.download(getContext()).load(entity).pause();
} }
} }

@ -71,7 +71,7 @@ public class UploadActivity extends BaseActivity<ActivityUploadMeanBinding> {
} }
@OnClick(R.id.stop) void stop() { @OnClick(R.id.stop) void stop() {
Aria.upload(this).load(FILE_PATH).stop(); Aria.upload(this).load(FILE_PATH).cancel();
} }
@OnClick(R.id.remove) void remove() { @OnClick(R.id.remove) void remove() {

Loading…
Cancel
Save