任务组功能实现

This commit is contained in:
AriaLyy
2017-07-18 22:08:47 +08:00
152 changed files with 7074 additions and 2885 deletions

View File

@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '25.0.2'
buildToolsVersion '25.0.3'
defaultConfig {
applicationId "com.arialyy.simple"
@ -42,7 +42,6 @@ dependencies {
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.arialyy.frame:MVVM2:2.2.0'
compile project(':Aria')
// compile 'com.arialyy.aria:aria-core:3.1.8'
// annotationProcessor 'com.arialyy.aria:aria-compiler:3.1.8'
compile project(':AriaCompiler')
}

View File

@ -31,6 +31,7 @@
<activity android:name=".download.multi_download.MultiDownloadActivity"/>
<activity android:name=".download.HighestPriorityActivity"/>
<activity android:name=".test.TestMutilTaskSysDownload"/>
<activity android:name=".download.group.DownloadGroupActivity"/>
<service android:name=".download.service_download.DownloadService"/>
</application>

View File

@ -7,9 +7,6 @@
<!--设置下载线程线程下载数改变后新的下载任务才会生效如果任务大小小于1m该设置也不会生效-->
<threadNum value="4"/>
<!--是否打开下载广播默认为false不建议使用广播你可以使用Download注解来实现事件回调-->
<openBroadcast value="false"/>
<!--设置下载队列最大任务数, 默认为2-->
<maxTaskNum value="2"/>
@ -34,11 +31,12 @@
<!--是否需要转换速度单位转换完成后为1b/s、1kb/s、1mb/s、1gb/s、1tb/s如果不需要将返回byte长度-->
<convertSpeed value="true"/>
<!--执行队列类型见com.arialyy.aria.core.QueueMod默认类型为now-->
<queueMod value="wait"/>
</download>
<upload>
<!--是否打开上传广播默认为false不建议使用广播你可以使用Upload注解来实现事件回调-->
<openBroadcast value="false"/>
<!--设置上传队列最大任务数, 默认为2-->
<maxTaskNum value="2"/>

View File

@ -18,14 +18,12 @@ package com.arialyy.simple;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import butterknife.Bind;
import butterknife.OnClick;
import com.arialyy.aria.core.Aria;
import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.ActivityMainBinding;
import com.arialyy.simple.download.DownloadActivity;
import com.arialyy.simple.download.group.DownloadGroupActivity;
import com.arialyy.simple.test.TestMutilTaskSysDownload;
import com.arialyy.simple.upload.UploadActivity;
@ -45,15 +43,20 @@ public class MainActivity extends BaseActivity<ActivityMainBinding> {
return R.layout.activity_main;
}
@OnClick(R.id.download) public void downloadDemo() {
startActivity(new Intent(this, DownloadActivity.class));
}
@OnClick(R.id.upload) public void uploadDemo() {
startActivity(new Intent(this, UploadActivity.class));
}
@OnClick({R.id.download, R.id.upload, R.id.download_task_group})
public void funcation(View view){
switch (view.getId()){
case R.id.download:
startActivity(new Intent(this, DownloadActivity.class));
break;
case R.id.upload:
startActivity(new Intent(this, UploadActivity.class));
break;
case R.id.download_task_group:
startActivity(new Intent(this, DownloadGroupActivity.class));
break;
}
@OnClick(R.id.multi_test) public void mutliTest() {
startActivity(new Intent(this, TestMutilTaskSysDownload.class));
}
}

View File

@ -17,7 +17,10 @@
package com.arialyy.simple.base;
import android.app.Application;
import android.os.Build;
import android.os.StrictMode;
import com.arialyy.frame.core.AbsFrame;
import com.arialyy.simple.BuildConfig;
/**
* Created by Lyy on 2016/9/27.
@ -26,5 +29,11 @@ public class BaseApplication extends Application {
@Override public void onCreate() {
super.onCreate();
AbsFrame.init(this);
if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
StrictMode.setThreadPolicy(
new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build());
}
}
}

View File

@ -16,6 +16,8 @@
package com.arialyy.simple.base.adapter;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
@ -45,6 +47,9 @@ public abstract class AbsRVAdapter<T, Holder extends AbsHolder>
@Override public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View view =
LayoutInflater.from(parent.getContext()).inflate(setLayoutId(viewType), parent, false);
//LayoutInflater inflater = LayoutInflater.from(parent.getContext());
//VD binding = DataBindingUtil.inflate(inflater, setLayoutId(viewType), parent, false);
//;
holder = getViewHolder(view, viewType);
return holder;
}
@ -55,6 +60,14 @@ public abstract class AbsRVAdapter<T, Holder extends AbsHolder>
bindData(holder, position, mData.get(position));
}
@Override public void onBindViewHolder(Holder holder, int position, List<Object> payloads) {
if (payloads == null || payloads.isEmpty()) {
bindData(holder, position, mData.get(position));
} else {
bindData(holder, position, mData.get(position), payloads);
}
}
public Context getContext() {
return mContext;
}
@ -69,4 +82,8 @@ public abstract class AbsRVAdapter<T, Holder extends AbsHolder>
protected abstract int setLayoutId(int type);
protected abstract void bindData(Holder holder, int position, T item);
protected void bindData(Holder holder, int position, T item, List<Object> payloads) {
}
}

View File

@ -16,27 +16,16 @@
package com.arialyy.simple.download;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.Environment;
import android.os.Handler;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.frame.util.AndroidUtils;
import com.arialyy.frame.util.StringUtil;
import com.arialyy.frame.util.show.L;
import com.arialyy.simple.R;
import com.arialyy.simple.download.multi_download.FileListEntity;
import com.arialyy.simple.base.BaseModule;
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
/**
* Created by Lyy on 2016/9/27.
@ -92,69 +81,4 @@ public class DownloadModule extends BaseModule {
return entity;
}
/**
* 下载广播过滤器
*/
public IntentFilter getDownloadFilter() {
IntentFilter filter = new IntentFilter();
filter.addDataScheme(getContext().getPackageName());
filter.addAction(Aria.ACTION_PRE);
filter.addAction(Aria.ACTION_POST_PRE);
filter.addAction(Aria.ACTION_RESUME);
filter.addAction(Aria.ACTION_START);
filter.addAction(Aria.ACTION_RUNNING);
filter.addAction(Aria.ACTION_STOP);
filter.addAction(Aria.ACTION_CANCEL);
filter.addAction(Aria.ACTION_COMPLETE);
filter.addAction(Aria.ACTION_FAIL);
return filter;
}
/**
* 创建Receiver
*/
public BroadcastReceiver createReceiver(final Handler handler) {
return new BroadcastReceiver() {
long len = 0;
@Override public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case Aria.ACTION_POST_PRE:
DownloadEntity entity = intent.getParcelableExtra(Aria.DOWNLOAD_ENTITY);
len = entity.getFileSize();
L.d(TAG, "download onPre");
handler.obtainMessage(SingleTaskActivity.DOWNLOAD_PRE, len).sendToTarget();
break;
case Aria.ACTION_START:
L.d(TAG, "download start");
break;
case Aria.ACTION_RESUME:
L.d(TAG, "download resume");
long location = intent.getLongExtra(Aria.CURRENT_LOCATION, 1);
handler.obtainMessage(SingleTaskActivity.DOWNLOAD_RESUME, location).sendToTarget();
break;
case Aria.ACTION_RUNNING:
long current = intent.getLongExtra(Aria.CURRENT_LOCATION, 0);
int progress = len == 0 ? 0 : (int) ((current * 100) / len);
handler.obtainMessage(SingleTaskActivity.DOWNLOAD_RUNNING, progress).sendToTarget();
break;
case Aria.ACTION_STOP:
L.d(TAG, "download stop");
handler.sendEmptyMessage(SingleTaskActivity.DOWNLOAD_STOP);
break;
case Aria.ACTION_COMPLETE:
handler.sendEmptyMessage(SingleTaskActivity.DOWNLOAD_COMPLETE);
break;
case Aria.ACTION_CANCEL:
handler.sendEmptyMessage(SingleTaskActivity.DOWNLOAD_CANCEL);
break;
case Aria.ACTION_FAIL:
handler.sendEmptyMessage(SingleTaskActivity.DOWNLOAD_FAILE);
break;
}
}
};
}
}

View File

@ -29,6 +29,7 @@ import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadTarget;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.inf.AbsEntity;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.frame.util.show.L;
import com.arialyy.simple.R;
@ -58,7 +59,7 @@ public class HighestPriorityActivity extends BaseActivity<ActivityHighestPriorit
private static final String DOWNLOAD_URL =
"https://res5.d.cn/6f78ee3bcfdd033e64892a8553a95814cf5b4a62b12a76d9eb2a694905f0dc30fa5c7f728806a4ee0b3479e7b26a38707dac92b136add91191ac1219aadb4a3aa70bfa6d06d2d8db.apk";
private DownloadAdapter mAdapter;
private List<DownloadEntity> mData = new ArrayList<>();
private List<AbsEntity> mData = new ArrayList<>();
private Set<String> mRecord = new HashSet<>();
@Override protected int setLayoutId() {
@ -83,7 +84,7 @@ public class HighestPriorityActivity extends BaseActivity<ActivityHighestPriorit
setBtState(false);
}
mSize.setText(target.getConvertFileSize());
List<DownloadEntity> temp = Aria.download(this).getTaskList();
List<DownloadEntity> temp = Aria.download(this).getSimpleTaskList();
if (temp != null && !temp.isEmpty()) {
for (DownloadEntity entity : temp) {
if (entity.getDownloadUrl().equals(DOWNLOAD_URL)) continue;

View File

@ -16,15 +16,8 @@
package com.arialyy.simple.download;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
@ -36,33 +29,20 @@ import butterknife.Bind;
import com.arialyy.annotations.Download;
import com.arialyy.aria.core.download.DownloadTarget;
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.inf.IEntity;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.frame.util.FileUtil;
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.databinding.ActivitySingleBinding;
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
import java.io.File;
public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
public static final int DOWNLOAD_PRE = 0x01;
public static final int DOWNLOAD_STOP = 0x02;
public static final int DOWNLOAD_FAILE = 0x03;
public static final int DOWNLOAD_CANCEL = 0x04;
public static final int DOWNLOAD_RESUME = 0x05;
public static final int DOWNLOAD_COMPLETE = 0x06;
public static final int DOWNLOAD_RUNNING = 0x07;
public static final int DOWNLOAD_START = 0x08;
private static final String DOWNLOAD_URL =
//"http://kotlinlang.org/docs/kotlin-docs.pdf";
//"https://atom-installer.github.com/v1.13.0/AtomSetup.exe?s=1484074138&ext=.exe";
"http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk";
//"http://down2.xiaoshuofuwuqi.com/d/file/filetxt/20170608/14/%BA%DA%CE%D7%CA%A6%E1%C8%C6%F0.txt";
//"http://tinghuaapp.oss-cn-shanghai.aliyuncs.com/20170612201739607815";
//"http://static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk";
//"http://oqcpqqvuf.bkt.clouddn.com/ceshi.txt";
@ -70,65 +50,11 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
//不支持断点的链接
//"http://ox.konsung.net:5555/ksdc-web/download/downloadFile/?fileName=ksdc_1.0.2.apk&rRange=0-";
//"http://172.18.104.50:8080/download/_302turn";
@Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb;
@Bind(R.id.start) Button mStart;
@Bind(R.id.stop) Button mStop;
@Bind(R.id.cancel) Button mCancel;
@Bind(R.id.size) TextView mSize;
@Bind(R.id.speed) TextView mSpeed;
@Bind(R.id.speeds) RadioGroup mRg;
private Handler mUpdateHandler = new Handler() {
@Override public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case DOWNLOAD_RUNNING:
DownloadTask task = (DownloadTask) msg.obj;
long current = task.getCurrentProgress();
long len = task.getFileSize();
if (len == 0) {
mPb.setProgress(0);
} else {
mPb.setProgress((int) ((current * 100) / len));
}
mSpeed.setText(task.getConvertSpeed());
break;
case DOWNLOAD_PRE:
setBtState(false);
break;
case DOWNLOAD_START:
mSize.setText(CommonUtil.formatFileSize((Long) msg.obj));
break;
case DOWNLOAD_FAILE:
Toast.makeText(SingleTaskActivity.this, "下载失败", Toast.LENGTH_SHORT).show();
setBtState(true);
break;
case DOWNLOAD_STOP:
Toast.makeText(SingleTaskActivity.this, "暂停下载", Toast.LENGTH_SHORT).show();
mStart.setText("恢复");
setBtState(true);
break;
case DOWNLOAD_CANCEL:
mPb.setProgress(0);
Toast.makeText(SingleTaskActivity.this, "取消下载", Toast.LENGTH_SHORT).show();
mStart.setText("开始");
setBtState(true);
break;
case DOWNLOAD_RESUME:
mStart.setText("暂停");
setBtState(false);
break;
case DOWNLOAD_COMPLETE:
mPb.setProgress(100);
Toast.makeText(SingleTaskActivity.this, "下载完成", Toast.LENGTH_SHORT).show();
mStart.setText("重新开始?");
mCancel.setEnabled(false);
setBtState(true);
break;
}
}
};
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Aria.download(this).register();
@ -153,7 +79,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
switch (item.getItemId()) {
case R.id.help:
msg = "一些小知识点:\n"
+ "1、你可以通过task.getKey().equals(DOWNLOAD_URL)判断是否是当前页面的下载以防止progress乱跳\n"
+ "1、你可以在注解中增加链接,用于指定被注解的方法只能被特定的下载任务回调以防止progress乱跳\n"
+ "2、当遇到网络慢的情况时你可以先使用onPre()更新UI界面待连接成功时再在onTaskPre()获取完整的task数据然后给UI界面设置正确的数据\n"
+ "3、你可以在界面初始化时通过Aria.download(this).load(DOWNLOAD_URL).getPercent()等方法快速获取相关任务的一些数据";
showMsgDialog("tip", msg);
@ -183,40 +109,54 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
}
@Download.onPre(DOWNLOAD_URL) protected void onPre(DownloadTask task) {
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize())
.sendToTarget();
setBtState(false);
}
@Download.onTaskStart(DOWNLOAD_URL) void taskStart(DownloadTask task) {
mUpdateHandler.obtainMessage(DOWNLOAD_START, task.getDownloadEntity().getFileSize())
.sendToTarget();
getBinding().setFileSize(task.getConvertFileSize());
}
@Download.onTaskRunning(DOWNLOAD_URL) protected void running(DownloadTask task) {
mUpdateHandler.obtainMessage(DOWNLOAD_RUNNING, task).sendToTarget();
long len = task.getFileSize();
if (len == 0) {
getBinding().setProgress(0);
} else {
getBinding().setProgress(task.getPercent());
}
getBinding().setSpeed(task.getConvertSpeed());
}
@Download.onTaskResume(DOWNLOAD_URL) void taskResume(DownloadTask task) {
mUpdateHandler.obtainMessage(DOWNLOAD_START, task.getFileSize()).sendToTarget();
mStart.setText("暂停");
setBtState(false);
}
@Download.onTaskStop(DOWNLOAD_URL) void taskStop(DownloadTask task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_STOP);
L.d(TAG, "task__stop");
mStart.setText("恢复");
setBtState(true);
getBinding().setSpeed("");
}
@Download.onTaskCancel(DOWNLOAD_URL) void taskCancel(DownloadTask task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_CANCEL);
L.d(TAG, "task__cancel");
getBinding().setProgress(0);
Toast.makeText(SingleTaskActivity.this, "取消下载", Toast.LENGTH_SHORT).show();
mStart.setText("开始");
setBtState(true);
getBinding().setSpeed("");
}
@Download.onTaskFail(DOWNLOAD_URL) void taskFail(DownloadTask task) {
L.d(TAG, "task__fail");
mUpdateHandler.sendEmptyMessage(DOWNLOAD_FAILE);
Toast.makeText(SingleTaskActivity.this, "下载失败", Toast.LENGTH_SHORT).show();
setBtState(true);
}
@Download.onTaskComplete(DOWNLOAD_URL) void taskComplete(DownloadTask task) {
mUpdateHandler.sendEmptyMessage(DOWNLOAD_COMPLETE);
getBinding().setProgress(100);
Toast.makeText(SingleTaskActivity.this, "下载完成", Toast.LENGTH_SHORT).show();
mStart.setText("重新开始?");
mCancel.setEnabled(false);
setBtState(true);
getBinding().setSpeed("");
}
@Download.onNoSupportBreakPoint(DOWNLOAD_URL)
@ -232,7 +172,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
super.init(savedInstanceState);
setTitle("单任务下载");
DownloadTarget target = Aria.download(this).load(DOWNLOAD_URL);
mPb.setProgress(target.getPercent());
getBinding().setProgress(target.getPercent());
if (target.getTaskState() == IEntity.STATE_STOP) {
mStart.setText("恢复");
mStart.setTextColor(getResources().getColor(android.R.color.holo_blue_light));
@ -240,8 +180,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
} else if (target.isDownloading()) {
setBtState(false);
}
mSize.setText(target.getConvertFileSize());
Aria.get(this).getDownloadConfig().setOpenBreadCast(true);
getBinding().setFileSize(target.getConvertFileSize());
}
public void onClick(View view) {
@ -251,6 +190,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
if (text.equals("重新开始?") || text.equals("开始")) {
Aria.download(this)
.load(DOWNLOAD_URL)
.addHeader("groupName", "value")
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk")
.start();
} else if (text.equals("恢复")) {

View File

@ -38,25 +38,20 @@ import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
* Created by Aria.Lao on 2017/1/4.
*/
public class DownloadFragment extends AbsFragment<FragmentDownloadBinding> {
@Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb;
@Bind(R.id.start) Button mStart;
@Bind(R.id.stop) Button mStop;
@Bind(R.id.cancel) Button mCancel;
@Bind(R.id.size) TextView mSize;
@Bind(R.id.speed) TextView mSpeed;
private static final String DOWNLOAD_URL =
"http://static.ilongyuan.cn/rayark/RayarkFZ_2.0.7.apk";
private static final String DOWNLOAD_URL = "http://static.ilongyuan.cn/rayark/RayarkFZ_2.0.7.apk";
@Override protected void init(Bundle savedInstanceState) {
if (Aria.download(this).taskExists(DOWNLOAD_URL)) {
DownloadTarget target = Aria.download(this).load(DOWNLOAD_URL);
int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize());
mPb.setProgress(p);
getBinding().setProgress(target.getPercent());
}
DownloadEntity entity = Aria.download(this).getDownloadEntity(DOWNLOAD_URL);
if (entity != null) {
mSize.setText(CommonUtil.formatFileSize(entity.getFileSize()));
getBinding().setFileSize(CommonUtil.formatFileSize(entity.getFileSize()));
int state = entity.getState();
setBtState(state != DownloadEntity.STATE_RUNNING);
} else {
@ -83,30 +78,29 @@ public class DownloadFragment extends AbsFragment<FragmentDownloadBinding> {
}
@Download.onTaskPre public void onTaskPre(DownloadTask task) {
mSize.setText(CommonUtil.formatFileSize(task.getFileSize()));
getBinding().setFileSize(task.getConvertFileSize());
setBtState(false);
}
@Download.onTaskStop public void onTaskStop(DownloadTask task) {
setBtState(true);
mSpeed.setText("0.0kb/s");
getBinding().setSpeed("0.0kb/s");
}
@Download.onTaskCancel public void onTaskCancel(DownloadTask task) {
setBtState(true);
mPb.setProgress(0);
mSpeed.setText("0.0kb/s");
getBinding().setProgress(0);
getBinding().setSpeed("0.0kb/s");
}
@Download.onTaskRunning public void onTaskRunning(DownloadTask task) {
long current = task.getCurrentProgress();
long len = task.getFileSize();
if (len == 0) {
mPb.setProgress(0);
getBinding().setProgress(0);
} else {
mPb.setProgress((int) ((current * 100) / len));
getBinding().setProgress(task.getPercent());
}
mSpeed.setText(task.getConvertSpeed());
getBinding().setSpeed(task.getConvertSpeed());
}
@Override protected void onDelayLoad() {

View File

@ -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.simple.download.group;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import butterknife.Bind;
import com.arialyy.annotations.DownloadGroup;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.core.download.DownloadGroupTask;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
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.databinding.ActivityDownloadGroupBinding;
import com.arialyy.simple.widget.SubStateLinearLayout;
import java.util.List;
/**
* Created by Aria.Lao on 2017/7/6.
*/
public class DownloadGroupActivity extends BaseActivity<ActivityDownloadGroupBinding> {
@Bind(R.id.start) Button mStart;
@Bind(R.id.stop) Button mStop;
@Bind(R.id.cancel) Button mCancel;
@Bind(R.id.child_list) SubStateLinearLayout mChildList;
List<String> mUrls;
@Override protected void init(Bundle savedInstanceState) {
super.init(savedInstanceState);
Aria.download(this).register();
setTitle("任务组");
mUrls = getModule(GroupModule.class).getUrls();
DownloadGroupTaskEntity entity = Aria.download(this).getDownloadGroupTask(mUrls);
if (entity != null && entity.getEntity() != null) {
DownloadGroupEntity groupEntity = entity.getEntity();
mChildList.addData(groupEntity.getSubTask());
getBinding().setFileSize(groupEntity.getConvertFileSize());
if (groupEntity.getFileSize() == 0) {
getBinding().setProgress(0);
} else {
getBinding().setProgress(groupEntity.isComplete() ? 100
: (int) (groupEntity.getCurrentProgress() * 100 / groupEntity.getFileSize()));
}
}
}
@Override protected int setLayoutId() {
return R.layout.activity_download_group;
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.start:
Aria.download(this)
.load(mUrls)
.setDownloadDirPath(
Environment.getExternalStorageDirectory().getPath() + "/Download/group_test_3")
.setGroupAlias("任务组测试")
.setSubTaskFileName(getModule(GroupModule.class).getSubName())
.start();
break;
case R.id.stop:
Aria.download(this).load(mUrls).stop();
break;
case R.id.cancel:
Aria.download(this).load(mUrls).cancel();
break;
}
}
@DownloadGroup.onPre() protected void onPre(DownloadGroupTask task) {
L.d(TAG, "group pre");
}
@DownloadGroup.onTaskPre() protected void onTaskPre(DownloadGroupTask task) {
L.d(TAG, "group task pre");
getBinding().setFileSize(task.getConvertFileSize());
}
@DownloadGroup.onTaskStart() void taskStart(DownloadGroupTask task) {
L.d(TAG, "group task start");
}
@DownloadGroup.onTaskRunning() protected void running(DownloadGroupTask task) {
getBinding().setProgress(task.getPercent());
getBinding().setSpeed(task.getConvertSpeed());
mChildList.updateChildProgress(task.getEntity().getSubTask());
}
@DownloadGroup.onTaskResume() void taskResume(DownloadGroupTask task) {
L.d(TAG, "group task resume");
}
@DownloadGroup.onTaskStop() void taskStop(DownloadGroupTask task) {
L.d(TAG, "group task stop");
getBinding().setSpeed("");
}
@DownloadGroup.onTaskCancel() void taskCancel(DownloadGroupTask task) {
getBinding().setSpeed("");
getBinding().setProgress(0);
}
@DownloadGroup.onTaskFail() void taskFail(DownloadGroupTask task) {
L.d(TAG, "group task fail");
}
@DownloadGroup.onTaskComplete() void taskComplete(DownloadGroupTask task) {
getBinding().setProgress(100);
T.showShort(this, "任务组下载完成");
}
}

View File

@ -0,0 +1,54 @@
/*
* 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.simple.download.group;
import android.content.Context;
import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseModule;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Created by Aria.Lao on 2017/7/6.
*/
public class GroupModule extends BaseModule {
public GroupModule(Context context) {
super(context);
}
List<String> getUrls() {
List<String> urls = new ArrayList<>();
String[] str = getContext().getResources().getStringArray(R.array.group_urls);
Collections.addAll(urls, str);
return urls;
}
List<String> getSubName(){
List<String> names = new ArrayList<>();
String[] str = getContext().getResources().getStringArray(R.array.group_names);
Collections.addAll(names, str);
return names;
}
//NormalList<String> convertPath(NormalList<String> urls){
// NormalList<String> paths = new ArrayList<>();
//
// for (String url : urls){
//
// }
//}
}

View File

@ -16,6 +16,7 @@
package com.arialyy.simple.download.multi_download;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.view.View;
@ -24,12 +25,15 @@ import android.widget.TextView;
import butterknife.Bind;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.core.inf.AbsEntity;
import com.arialyy.aria.core.inf.IEntity;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.simple.R;
import com.arialyy.simple.base.adapter.AbsHolder;
import com.arialyy.simple.base.adapter.AbsRVAdapter;
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
import com.arialyy.simple.widget.SubStateLinearLayout;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -39,21 +43,32 @@ import java.util.concurrent.ConcurrentHashMap;
* Created by Lyy on 2016/9/27.
* 下载列表适配器
*/
public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapter.MyHolder> {
public class DownloadAdapter extends AbsRVAdapter<AbsEntity, DownloadAdapter.SimpleHolder> {
private static final String TAG = "DownloadAdapter";
private Map<String, Integer> mPositions = new ConcurrentHashMap<>();
public DownloadAdapter(Context context, List<DownloadEntity> data) {
public DownloadAdapter(Context context, List<AbsEntity> data) {
super(context, data);
int i = 0;
for (DownloadEntity entity : data) {
mPositions.put(entity.getDownloadUrl(), i);
for (AbsEntity entity : data) {
mPositions.put(getKey(entity), i);
i++;
}
}
@Override protected MyHolder getViewHolder(View convertView, int viewType) {
return new MyHolder(convertView);
private String getKey(AbsEntity entity) {
if (entity instanceof DownloadEntity) {
return ((DownloadEntity) entity).getDownloadUrl();
} else if (entity instanceof DownloadGroupEntity) {
return ((DownloadGroupEntity) entity).getGroupName();
}
return "";
}
@Override protected SimpleHolder getViewHolder(View convertView, int viewType) {
if (viewType == 1) return new SimpleHolder(convertView);
if (viewType == 2) return new GroupHolder(convertView);
return null;
}
public void addDownloadEntity(DownloadEntity entity) {
@ -61,21 +76,33 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
mPositions.put(entity.getDownloadUrl(), mPositions.size());
}
@Override protected int setLayoutId(int type) {
return R.layout.item_download;
@Override public int getItemViewType(int position) {
AbsEntity entity = mData.get(position);
if (entity instanceof DownloadEntity) return 1;
if (entity instanceof DownloadGroupEntity) return 2;
return -1;
}
public synchronized void updateState(DownloadEntity entity) {
@Override protected int setLayoutId(int type) {
if (type == 1) {
return R.layout.item_simple_download;
} else if (type == 2) {
return R.layout.item_group_download;
}
return android.R.layout.simple_list_item_2;
}
public synchronized void updateState(AbsEntity entity) {
if (entity.getState() == DownloadEntity.STATE_CANCEL) {
mPositions.clear();
int i = 0;
for (DownloadEntity entity_1 : mData) {
mPositions.put(entity_1.getDownloadUrl(), i);
for (AbsEntity entity_1 : mData) {
mPositions.put(getKey(entity_1), i);
i++;
}
notifyDataSetChanged();
} else {
int position = indexItem(entity.getDownloadUrl());
int position = indexItem(getKey(entity));
if (position == -1 || position >= mData.size()) {
return;
}
@ -84,15 +111,18 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
}
}
public synchronized void setProgress(DownloadEntity entity) {
String url = entity.getDownloadUrl();
/**
* 更新进度
*/
public synchronized void setProgress(AbsEntity entity) {
String url = entity.getKey();
int position = indexItem(url);
if (position == -1 || position >= mData.size()) {
return;
}
mData.set(position, entity);
notifyItemChanged(position);
notifyItemChanged(position, entity);
}
private synchronized int indexItem(String url) {
@ -105,51 +135,97 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
return -1;
}
@Override protected void bindData(MyHolder holder, int position, final DownloadEntity item) {
long size = item.getFileSize();
int current = 0;
long progress = item.getCurrentProgress();
current = size == 0 ? 0 : (int) (progress * 100 / size);
@Override protected void bindData(SimpleHolder holder, int position, final AbsEntity item) {
handleProgress(holder, item);
}
@Override protected void bindData(SimpleHolder holder, int position, AbsEntity item,
List<Object> payloads) {
AbsEntity entity = (AbsEntity) payloads.get(0);
updateSpeed(holder, entity);
}
/**
* 只更新速度
*/
private void updateSpeed(SimpleHolder holder, final AbsEntity entity) {
long size = entity.getFileSize();
long progress = entity.getCurrentProgress();
int current = size == 0 ? 0 : (int) (progress * 100 / size);
holder.speed.setText(entity.getConvertSpeed());
holder.fileSize.setText(covertCurrentSize(progress) + "/" + CommonUtil.formatFileSize(size));
holder.progress.setProgress(current);
BtClickListener listener = new BtClickListener(item);
holder.bt.setOnClickListener(listener);
holder.name.setText("文件名:" + item.getFileName());
holder.url.setText("下载地址:" + item.getDownloadUrl());
holder.path.setText("保持路径:" + item.getDownloadPath());
//if (holder instanceof GroupHolder){
// handleSubChild((GroupHolder) holder, entity);
//}
}
@SuppressLint("SetTextI18n")
private void handleProgress(SimpleHolder holder, final AbsEntity entity) {
String str = "";
int color = android.R.color.holo_green_light;
switch (item.getState()) {
case DownloadEntity.STATE_WAIT:
case DownloadEntity.STATE_OTHER:
case DownloadEntity.STATE_FAIL:
switch (entity.getState()) {
case IEntity.STATE_WAIT:
case IEntity.STATE_OTHER:
case IEntity.STATE_FAIL:
str = "开始";
break;
case DownloadEntity.STATE_STOP:
case IEntity.STATE_STOP:
str = "恢复";
color = android.R.color.holo_blue_light;
break;
case DownloadEntity.STATE_PRE:
case DownloadEntity.STATE_POST_PRE:
case DownloadEntity.STATE_RUNNING:
case IEntity.STATE_PRE:
case IEntity.STATE_POST_PRE:
case IEntity.STATE_RUNNING:
str = "暂停";
color = android.R.color.holo_red_light;
break;
case DownloadEntity.STATE_COMPLETE:
case IEntity.STATE_COMPLETE:
str = "重新开始?";
holder.progress.setProgress(100);
break;
}
long size = entity.getFileSize();
long progress = entity.getCurrentProgress();
int current = size == 0 ? 0 : (int) (progress * 100 / size);
holder.bt.setText(str);
holder.bt.setTextColor(getColor(color));
holder.speed.setText(item.getConvertSpeed());
holder.progress.setProgress(current);
BtClickListener listener = new BtClickListener(entity);
holder.bt.setOnClickListener(listener);
String name = (isSimpleDownload(entity) ? ((DownloadEntity) entity).getFileName()
: ((DownloadGroupEntity) entity).getAlias());
holder.name.setText("文件名:" + name);
holder.speed.setText(entity.getConvertSpeed());
holder.fileSize.setText(covertCurrentSize(progress) + "/" + CommonUtil.formatFileSize(size));
//删除按钮事件
holder.cancel.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
mData.remove(item);
mData.remove(entity);
notifyDataSetChanged();
Aria.download(getContext()).load(item).cancel();
if (isSimpleDownload(entity)) {
Aria.download(getContext()).load((DownloadEntity) entity).cancel();
} else {
Aria.download(getContext()).load((DownloadGroupEntity) entity).cancel();
}
}
});
//if (holder instanceof GroupHolder){
// handleSubChild((GroupHolder) holder, entity);
//}
}
private void handleSubChild(GroupHolder holder, final AbsEntity entity) {
if (holder.childList.getSubData().size() > 0){
holder.childList.updateChildProgress(((DownloadGroupEntity)entity).getSubTask());
}else {
holder.childList.addData(((DownloadGroupEntity)entity).getSubTask());
}
}
private boolean isSimpleDownload(AbsEntity entity) {
return entity instanceof DownloadEntity;
}
private String covertCurrentSize(long currentSize) {
@ -161,49 +237,67 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
return Resources.getSystem().getColor(color);
}
/**
* 按钮事件
*/
private class BtClickListener implements View.OnClickListener {
private DownloadEntity entity;
private AbsEntity entity;
BtClickListener(DownloadEntity entity) {
BtClickListener(AbsEntity entity) {
this.entity = entity;
}
@Override public void onClick(View v) {
switch (entity.getState()) {
case DownloadEntity.STATE_WAIT:
case DownloadEntity.STATE_OTHER:
case DownloadEntity.STATE_FAIL:
case DownloadEntity.STATE_STOP:
case DownloadEntity.STATE_COMPLETE:
case DownloadEntity.STATE_POST_PRE:
case IEntity.STATE_WAIT:
case IEntity.STATE_OTHER:
case IEntity.STATE_FAIL:
case IEntity.STATE_STOP:
case IEntity.STATE_COMPLETE:
case IEntity.STATE_PRE:
case IEntity.STATE_POST_PRE:
start(entity);
break;
case DownloadEntity.STATE_RUNNING:
case IEntity.STATE_RUNNING:
stop(entity);
break;
}
}
private void start(DownloadEntity entity) {
Aria.download(getContext()).load(entity).start();
private void start(AbsEntity entity) {
if (isSimpleDownload(entity)) {
Aria.download(getContext()).load((DownloadEntity) entity).start();
} else {
Aria.download(getContext()).load((DownloadGroupEntity) entity).start();
}
}
private void stop(DownloadEntity entity) {
Aria.download(getContext()).load(entity).pause();
private void stop(AbsEntity entity) {
if (isSimpleDownload(entity)) {
Aria.download(getContext()).load((DownloadEntity) entity).stop();
} else {
Aria.download(getContext()).load((DownloadGroupEntity) entity).stop();
}
}
}
class MyHolder extends AbsHolder {
class SimpleHolder extends AbsHolder {
@Bind(R.id.progressBar) HorizontalProgressBarWithNumber progress;
@Bind(R.id.bt) Button bt;
@Bind(R.id.speed) TextView speed;
@Bind(R.id.fileSize) TextView fileSize;
@Bind(R.id.del) TextView cancel;
@Bind(R.id.name) TextView name;
@Bind(R.id.download_url) TextView url;
@Bind(R.id.download_path) TextView path;
MyHolder(View itemView) {
SimpleHolder(View itemView) {
super(itemView);
}
}
class GroupHolder extends SimpleHolder {
@Bind(R.id.child_list) SubStateLinearLayout childList;
GroupHolder(View itemView) {
super(itemView);
}
}

View File

@ -68,7 +68,7 @@ final class FileListAdapter extends AbsRVAdapter<FileListEntity, FileListAdapter
Toast.makeText(getContext(), "开始下载:" + item.name, Toast.LENGTH_SHORT).show();
Aria.download(getContext())
.load(item.downloadUrl)
.setDownloadName(item.name)
.setFileName(item.name)
.setDownloadPath(item.downloadPath)
.start();
}

View File

@ -19,16 +19,17 @@ package com.arialyy.simple.download.multi_download;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import butterknife.Bind;
import com.arialyy.annotations.Download;
import com.arialyy.annotations.DownloadGroup;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadGroupTask;
import com.arialyy.aria.core.download.DownloadTask;
import com.arialyy.aria.core.inf.AbsEntity;
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.databinding.ActivityMultiDownloadBinding;
@ -41,12 +42,7 @@ import java.util.List;
public class MultiDownloadActivity extends BaseActivity<ActivityMultiDownloadBinding> {
@Bind(R.id.list) RecyclerView mList;
private DownloadAdapter mAdapter;
private List<DownloadEntity> mData = new ArrayList<>();
String[] mFilterStr = new String[] {
"https://g37.gdl.netease.com/onmyoji_netease_10_1.0.20.apk",
"http://static.gaoshouyou.com/d/eb/f2/dfeba30541f209ab8a50d847fc1661ce.apk"
};
private List<AbsEntity> mData = new ArrayList<>();
@Override protected int setLayoutId() {
return R.layout.activity_multi_download;
@ -56,7 +52,7 @@ public class MultiDownloadActivity extends BaseActivity<ActivityMultiDownloadBin
super.init(savedInstanceState);
Aria.download(this).register();
setTitle("下载列表");
List<DownloadEntity> temps = Aria.download(this).getTaskList();
List<AbsEntity> temps = Aria.download(this).getTotleTaskList();
if (temps != null && !temps.isEmpty()) {
mData.addAll(temps);
}
@ -76,40 +72,68 @@ public class MultiDownloadActivity extends BaseActivity<ActivityMultiDownloadBin
}
@Download.onPre void onPre(DownloadTask task) {
L.d(TAG, "download onPre");
mAdapter.updateState(task.getDownloadEntity());
mAdapter.updateState(task.getEntity());
}
@Download.onTaskStart void taskStart(DownloadTask task) {
L.d(TAG, "download start");
mAdapter.updateState(task.getDownloadEntity());
mAdapter.updateState(task.getEntity());
}
@Download.onTaskResume void taskResume(DownloadTask task) {
L.d(TAG, "download resume");
mAdapter.updateState(task.getDownloadEntity());
mAdapter.updateState(task.getEntity());
}
@Download.onTaskStop void taskStop(DownloadTask task) {
mAdapter.updateState(task.getDownloadEntity());
mAdapter.updateState(task.getEntity());
}
@Download.onTaskCancel void taskCancel(DownloadTask task) {
mAdapter.updateState(task.getDownloadEntity());
mAdapter.updateState(task.getEntity());
}
@Download.onTaskFail void taskFail(DownloadTask task) {
mAdapter.updateState(task.getDownloadEntity());
mAdapter.updateState(task.getEntity());
}
@Download.onTaskComplete void taskComplete(DownloadTask task) {
mAdapter.updateState(task.getDownloadEntity());
mAdapter.updateState(task.getEntity());
}
@Download.onTaskRunning({
"https://g37.gdl.netease.com/onmyoji_netease_10_1.0.20.apk",
"http://static.gaoshouyou.com/d/eb/f2/dfeba30541f209ab8a50d847fc1661ce.apk"
}) void taskRunning(DownloadTask task) {
mAdapter.setProgress(task.getDownloadEntity());
@Download.onTaskRunning() void taskRunning(DownloadTask task) {
mAdapter.setProgress(task.getEntity());
}
//////////////////////////////////// 下面为任务组的处理 /////////////////////////////////////////
@DownloadGroup.onPre void onGroupPre(DownloadGroupTask task) {
mAdapter.updateState(task.getEntity());
}
@DownloadGroup.onTaskStart void groupTaskStart(DownloadGroupTask task) {
mAdapter.updateState(task.getEntity());
}
@DownloadGroup.onTaskResume void groupTaskResume(DownloadGroupTask task) {
mAdapter.updateState(task.getEntity());
}
@DownloadGroup.onTaskStop void groupTaskStop(DownloadGroupTask task) {
mAdapter.updateState(task.getEntity());
}
@DownloadGroup.onTaskCancel void groupTaskCancel(DownloadGroupTask task) {
mAdapter.updateState(task.getEntity());
}
@DownloadGroup.onTaskFail void groupTaskFail(DownloadGroupTask task) {
mAdapter.updateState(task.getEntity());
}
@DownloadGroup.onTaskComplete void groupTaskComplete(DownloadGroupTask task) {
mAdapter.updateState(task.getEntity());
}
@DownloadGroup.onTaskRunning() void groupTaskRunning(DownloadGroupTask task) {
mAdapter.setProgress(task.getEntity());
}
}

View File

@ -0,0 +1,33 @@
/*
* 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.simple.download.multi_download;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.download.DownloadGroupEntity;
/**
* Created by Aria.Lao on 2017/7/14.
*/
public class MultiEntity {
public static final int SIMPLE_DOWNLOAD = 0xa1;
public static final int GROUP_DOWNLOAD = 0xa2;
DownloadEntity simpleEntity;
DownloadGroupEntity groupEntity;
String key;
int type = -1;
}

View File

@ -0,0 +1,29 @@
/*
* 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.simple.download.multi_download;
import android.content.Context;
import com.arialyy.simple.base.BaseModule;
/**
* Created by Aria.Lao on 2017/7/14.
*/
public class MultiModule extends BaseModule{
public MultiModule(Context context) {
super(context);
}
}

View File

@ -3,6 +3,7 @@ package com.arialyy.simple.test;
import android.os.Environment;
import android.view.View;
import com.arialyy.aria.core.Aria;
import com.arialyy.aria.core.QueueMod;
import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.TestActivityMultiBinding;
@ -54,21 +55,26 @@ public class TestMutilTaskSysDownload extends BaseActivity<TestActivityMultiBind
"M02/3B/A5/oYYBAFaOeaSAdFyoAACaxVxgUJA092.jpg"
};
int maxNum = Aria.get(this).getDownloadConfig().getMaxTaskNum();
Aria.get(this).setDownloadQueueMod(QueueMod.NOW);
for (int i = 0; i < urlArray.length; i++) {
if (i < maxNum) {
Aria.download(this)
.load(baseUrl + urlArray[i])
.setDownloadPath(Environment.getExternalStorageDirectory() + "/test/" + i + ".jpg")
//.addHeader("Accept-Encoding", "gzip,deflate,sdcn")
.start();
} else {
Aria.download(this)
.load(baseUrl + urlArray[i])
.setDownloadPath(Environment.getExternalStorageDirectory() + "/test/" + i + ".jpg")
//.addHeader("Accept-Encoding", "gzip,deflate,sdcn")
.add();
}
Aria.download(this)
.load(baseUrl + urlArray[i])
.setDownloadPath(Environment.getExternalStorageDirectory() + "/test/" + i + ".jpg")
//.addHeader("Accept-Encoding", "gzip,deflate,sdcn")
.start();
//if (i < maxNum) {
// Aria.download(this)
// .load(baseUrl + urlArray[i])
// .setDownloadPath(Environment.getExternalStorageDirectory() + "/test/" + i + ".jpg")
// //.addHeader("Accept-Encoding", "gzip,deflate,sdcn")
// .start();
//} else {
// Aria.download(this)
// .load(baseUrl + urlArray[i])
// .setDownloadPath(Environment.getExternalStorageDirectory() + "/test/" + i + ".jpg")
// //.addHeader("Accept-Encoding", "gzip,deflate,sdcn")
// .add();
//}
}
}
}

View File

@ -85,7 +85,7 @@ public class UploadActivity extends BaseActivity<ActivityUploadMeanBinding> {
@OnClick(R.id.upload) void upload() {
Aria.upload(this)
.load(FILE_PATH)
.setUploadUrl("http://172.18.104.228:8080/upload/sign_file")
.setUploadUrl("http://172.18.104.129:8080/upload/sign_file")
.setAttachment("file")
.start();
}

View File

@ -0,0 +1,43 @@
/*
* 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.simple.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
/**
* Created by lyy on 2015/4/22.
* 不带有滑动功能的ListView
*/
public class NoScrollListView extends ListView {
public NoScrollListView(Context context) {
super(context);
}
public NoScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NoScrollListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}

View File

@ -0,0 +1,121 @@
package com.arialyy.simple.widget;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.simple.R;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
/**
* Created by Aria.Lao on 2017/7/17.
*/
public class SubStateLinearLayout extends LinearLayout {
interface OnShowCallback {
void onShow(boolean visibility);
}
OnShowCallback callback;
List<DownloadEntity> mSubData = new LinkedList<>();
Map<String, Integer> mPosition = new WeakHashMap<>();
public SubStateLinearLayout(Context context) {
super(context);
setOrientation(VERTICAL);
}
public SubStateLinearLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
setOrientation(VERTICAL);
}
public SubStateLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOrientation(VERTICAL);
}
public void addData(List<DownloadEntity> datas) {
removeAllViews();
mSubData.clear();
mSubData.addAll(datas);
createShowView();
int i = 1;
for (DownloadEntity entity : datas) {
TextView view = createView(entity);
mPosition.put(entity.getDownloadPath(), i);
addView(view, i);
i++;
}
}
public void setOnShowCallback(OnShowCallback callback) {
this.callback = callback;
}
public List<DownloadEntity> getSubData() {
return mSubData;
}
public void updateChildProgress(List<DownloadEntity> entities) {
for (DownloadEntity entity : entities) {
Integer i = mPosition.get(entity.getDownloadPath());
if (i == null) return;
int position = i;
if (position != -1) {
TextView child = ((TextView) getChildAt(position));
int p = getPercent(entity);
child.setText(entity.getFileName() + ": " + p + "%" + " | " + entity.getConvertSpeed());
child.invalidate();
}
}
}
private TextView createView(DownloadEntity entity) {
TextView view =
(TextView) LayoutInflater.from(getContext()).inflate(R.layout.layout_child_state, null);
view.setText(entity.getFileName() + ": " + getPercent(entity) + "%");
return view;
}
private void createShowView() {
TextView view =
(TextView) LayoutInflater.from(getContext()).inflate(R.layout.layout_child_state, null);
view.setText("点击显示子任务");
view.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {
int visibility = getChildAt(1).getVisibility();
if (visibility == GONE) {
showChild(true);
((TextView) v).setText("点击隐藏子任务");
} else {
showChild(false);
((TextView) v).setText("点击显示子任务");
}
}
});
addView(view, 0);
}
private void showChild(boolean show) {
for (int i = 1, count = getChildCount(); i < count; i++) {
getChildAt(i).setVisibility(show ? VISIBLE : GONE);
invalidate();
}
}
private int getPercent(DownloadEntity entity) {
long size = entity.getFileSize();
long progress = entity.getCurrentProgress();
return size == 0 ? 0 : (int) (progress * 100 / size);
}
}

View File

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto"
>
<data>
<variable
name="fileSize"
type="String"
/>
<variable
name="speed"
type="String"
/>
<variable
name="progress"
type="int"
/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="@layout/layout_bar"/>
<include
layout="@layout/content_single"
bind:fileSize="@{fileSize}"
bind:progress="@{progress}"
bind:speed="@{speed}"
/>
<com.arialyy.simple.widget.SubStateLinearLayout
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:id="@+id/child_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</layout>

View File

@ -13,7 +13,7 @@
android:id="@+id/download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下载 demo"
android:text="下载"
style="?buttonBarButtonStyle"
/>
@ -21,15 +21,15 @@
android:id="@+id/upload"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="上传 demo"
android:text="上传"
style="?buttonBarButtonStyle"
/>
<Button
android:id="@+id/multi_test"
android:id="@+id/download_task_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多任务测试"
android:text="下载任务组"
style="?buttonBarButtonStyle"
/>

View File

@ -1,5 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto"
>
<data>
<variable
name="fileSize"
type="String"
/>
<variable
name="speed"
type="String"
/>
<variable
name="progress"
type="int"
/>
</data>
<android.support.design.widget.CoordinatorLayout
xmlns:tools="http://schemas.android.com/tools"
@ -18,7 +35,12 @@
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_single"/>
<include
layout="@layout/content_single"
bind:fileSize="@{fileSize}"
bind:progress="@{progress}"
bind:speed="@{speed}"
/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"

View File

@ -1,135 +1,155 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.arialyy.simple.download.SingleTaskActivity"
tools:showIn="@layout/activity_single"
>
<com.arialyy.simple.widget.HorizontalProgressBarWithNumber
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_margin="16dp"
android:layout_toLeftOf="@+id/size"
android:max="100"
style="?android:attr/progressBarStyleHorizontal"
/>
<data>
<variable
name="fileSize"
type="String"
/>
<variable
name="speed"
type="String"
/>
<variable
name="progress"
type="int"
/>
</data>
<TextView
android:id="@+id/size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/progressBar"
android:layout_marginRight="16dp"
android:text="0mb"
android:textSize="16sp"
/>
<LinearLayout
android:id="@+id/handle_bar"
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBar"
android:orientation="horizontal"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.arialyy.simple.download.SingleTaskActivity"
tools:showIn="@layout/activity_single"
>
<com.arialyy.simple.widget.HorizontalProgressBarWithNumber
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_margin="16dp"
android:layout_toLeftOf="@+id/size"
android:max="100"
android:progress="@{progress}"
style="?android:attr/progressBarStyleHorizontal"
/>
<TextView
android:id="@+id/speed"
android:id="@+id/size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="0kb/s"
android:textColor="@color/black"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/progressBar"
android:layout_marginRight="16dp"
android:text="@{fileSize}"
android:textSize="16sp"
/>
<Button
android:id="@+id/start"
android:layout_width="0dp"
<LinearLayout
android:id="@+id/handle_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="开始"
style="?buttonBarButtonStyle"
/>
android:layout_below="@+id/progressBar"
android:orientation="horizontal"
>
<TextView
android:id="@+id/speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text="@{speed}"
android:textColor="@color/black"
/>
<Button
android:id="@+id/stop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="暂停"
style="?buttonBarButtonStyle"
/>
<Button
android:id="@+id/start"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="开始"
style="?buttonBarButtonStyle"
/>
<Button
android:id="@+id/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="删除任务"
style="?buttonBarButtonStyle"
/>
</LinearLayout>
<Button
android:id="@+id/stop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="暂停"
style="?buttonBarButtonStyle"
/>
<TextView
android:id="@+id/speed_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/handle_bar"
/>
<Button
android:id="@+id/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="删除任务"
style="?buttonBarButtonStyle"
/>
</LinearLayout>
<RadioGroup
android:visibility="gone"
android:id="@+id/speeds"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/speed_hint"
android:orientation="horizontal"
>
<RadioButton
<TextView
android:id="@+id/speed_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="256kb"
android:layout_below="@+id/handle_bar"
/>
<RadioButton
android:layout_width="wrap_content"
<RadioGroup
android:id="@+id/speeds"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="512kb"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1mb"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2mb"
/>
android:layout_below="@+id/speed_hint"
android:orientation="horizontal"
android:visibility="gone"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="max"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="256kb"
/>
</RadioGroup>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="512kb"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1mb"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2mb"
/>
</RelativeLayout>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="max"
/>
</RadioGroup>
</RelativeLayout>
</layout>

View File

@ -1,12 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto"
>
<data>
<variable
name="fileSize"
type="String"
/>
<variable
name="speed"
type="String"
/>
<variable
name="progress"
type="int"
/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="@layout/dialog_download"/>
<include
layout="@layout/dialog_download"
bind:fileSize="@{fileSize}"
bind:progress="@{progress}"
bind:speed="@{speed}"
/>
</LinearLayout>
</layout>

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<include
layout="@layout/layout_item_progress"
android:id="@+id/include"
/>
<com.arialyy.simple.widget.SubStateLinearLayout
android:visibility="gone"
android:id="@+id/child_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/include"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
/>
</RelativeLayout>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<include layout="@layout/layout_item_progress"/>
</RelativeLayout>

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/black"
android:textSize="14sp"
/>

View File

@ -65,7 +65,6 @@
android:textColor="@color/bt_selector_cancel"
/>
</RelativeLayout>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
@ -75,22 +74,4 @@
android:text="name"
/>
<TextView
android:id="@+id/download_url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:layout_marginTop="5dp"
android:ellipsize="end"
android:maxLines="2"
/>
<TextView
android:id="@+id/download_path"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/download_url"
android:layout_marginTop="5dp"
/>
</RelativeLayout>
</RelativeLayout>

View File

@ -22,6 +22,7 @@
<item>幻影纹章</item>
<item>史上最坑爹的游戏10</item>
<item>鲜果消消乐</item>
<item>航海奇迹</item>
</string-array>
<string-array name="download_url">
@ -30,6 +31,7 @@
<item>http://rs.0.gaoshouyou.com/d/51/46/58514d126c46b8a3f27fc8c7db3b09ec.apk</item>
<item>http://rs.0.gaoshouyou.com/d/23/69/07238f952669727878d7a0e180534c8b.apk</item>
<item>http://rs.0.gaoshouyou.com/d/e7/3d/73e716d3353de5b479fcf7da8d36a5ef.apk</item>
<item>http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk</item>
</string-array>
<string-array name="highest_names">
@ -44,4 +46,20 @@
<item>https://res5.d.cn/6f78ee3bcfdd033ef8e38596afb298d87de07e5f0f1f91f22acd57750f8ae68270531e2b266ea41c86cd196da839a0afef1952dde89773c7e26b9019249503174ca0513fa0a36a6472c4202bbf94da382964a0478471b753ebd95b67aac7ad89.apk</item>
</string-array>
<string-array name="group_urls">
<item>https://res5.d.cn/5a6a3384c1b2be1a65d84b914e6a6fef697637578b6db2eb1056d50b09cf1dcf289d4045df7ef95746e498e3d6a848ab84c89b77aa60194e2c48e5a7cb748265.apk</item>
<item>https://res5.d.cn/5a6a3384c1b2be1a52034c72752e8475414630ebc69318b84ef584115ebf5eaaab945ae07b7fe3596afc72a7940ff328d4a9553f6ae92d6c09ba4bfb533137f6.apk</item>
<item>https://res5.d.cn/5a6a3384c1b2be1a426f06bfc69034d69c44ae1a01da180cab8e59bd1a5e1a784bac46ba0c64579d14f0e80a4ce4f068af89b0369a393456f4f449a8829cad5c.apk</item>
<!--<item>http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk</item>-->
<!--<item>http://static.gaoshouyou.com/d/d4/4f/d6d48db3794fb9ecf47e83c346570881.apk</item>-->
</string-array>
<string-array name="group_names">
<item>王者荣耀.apk</item>
<item>战斗吧剑灵.apk</item>
<item>天魔幻想.apk</item>
</string-array>
</resources>