任务组事件和普通任务事件同时使用

This commit is contained in:
AriaLyy
2017-07-14 17:49:07 +08:00
parent 71a0ae4492
commit 041df07617
44 changed files with 657 additions and 235 deletions

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;
}

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

@ -190,7 +190,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
if (text.equals("重新开始?") || text.equals("开始")) {
Aria.download(this)
.load(DOWNLOAD_URL)
.addHeader("key", "value")
.addHeader("groupName", "value")
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk")
.start();
} else if (text.equals("恢复")) {

View File

@ -38,8 +38,8 @@ public class GroupModule extends BaseModule {
return urls;
}
//List<String> convertPath(List<String> urls){
// List<String> paths = new ArrayList<>();
//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.NoScrollListView;
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,8 +111,8 @@ 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;
@ -105,53 +132,67 @@ 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);
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());
@Override protected void bindData(SimpleHolder holder, int position, final AbsEntity item) {
handleProgress(holder, item);
}
@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();
}
}
});
}
private boolean isSimpleDownload(AbsEntity entity) {
return entity instanceof DownloadEntity;
}
private String covertCurrentSize(long currentSize) {
if (currentSize < 0) return "0";
return CommonUtil.formatFileSize(currentSize);
@ -161,49 +202,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) NoScrollListView 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

@ -23,9 +23,12 @@ 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.simple.R;
import com.arialyy.simple.base.BaseActivity;
@ -39,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;
@ -54,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);
}
@ -74,37 +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() void taskRunning(DownloadTask task) {
mAdapter.setProgress(task.getDownloadEntity());
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

@ -97,9 +97,6 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
mAdapter.updateBtState(task.getKey(), true);
}
@Override protected void dataCallback(int result, Object data) {
super.dataCallback(result, data);
if (result == DownloadNumDialog.RESULT_CODE) {

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);
}
}