This commit is contained in:
lyy
2016-10-19 18:19:33 +08:00
parent 08d662e053
commit a52338c5d3
31 changed files with 239 additions and 327 deletions

View File

@ -8,8 +8,8 @@ import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import butterknife.Bind;
import com.arialyy.downloadutil.core.DownloadEntity;
import com.arialyy.downloadutil.core.DownloadManager;
import com.arialyy.downloadutil.entity.DownloadEntity;
import com.arialyy.frame.util.show.L;
import com.arialyy.simple.R;
import com.arialyy.simple.adapter.DownloadAdapter;
@ -59,7 +59,9 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
break;
case DownloadManager.ACTION_RUNNING:
long current = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 0);
mAdapter.setProgress(entity.getDownloadUrl(), current);
long speed = intent.getLongExtra(DownloadManager.CURRENT_SPEED, 0);
//mAdapter.setProgress(entity.getDownloadUrl(), current, speed);
mAdapter.setProgress(entity);
break;
case DownloadManager.ACTION_STOP:
L.d(TAG, "download stop");
@ -71,6 +73,7 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
break;
case DownloadManager.ACTION_CANCEL:
L.d(TAG, "download cancel");
mAdapter.updateState(entity);
break;
case DownloadManager.ACTION_FAIL:
L.d(TAG, "download fail");

View File

@ -17,7 +17,7 @@ import butterknife.Bind;
import com.arialyy.downloadutil.core.DownloadManager;
import com.arialyy.downloadutil.core.command.CommandFactory;
import com.arialyy.downloadutil.core.command.IDownloadCommand;
import com.arialyy.downloadutil.entity.DownloadEntity;
import com.arialyy.downloadutil.core.DownloadEntity;
import com.arialyy.downloadutil.orm.DbEntity;
import com.arialyy.downloadutil.util.Util;
import com.arialyy.frame.util.show.L;

View File

@ -4,14 +4,15 @@ import android.content.Context;
import android.content.res.Resources;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import butterknife.Bind;
import com.arialyy.absadapter.common.AbsHolder;
import com.arialyy.absadapter.recycler_view.AbsRVAdapter;
import com.arialyy.downloadutil.core.DownloadEntity;
import com.arialyy.downloadutil.core.DownloadManager;
import com.arialyy.downloadutil.core.command.CommandFactory;
import com.arialyy.downloadutil.core.command.IDownloadCommand;
import com.arialyy.downloadutil.entity.DownloadEntity;
import com.arialyy.frame.util.show.L;
import com.arialyy.downloadutil.util.Util;
import com.arialyy.simple.R;
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
import java.util.ArrayList;
@ -28,14 +29,12 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
private static final String TAG = "DownloadAdapter";
private DownloadManager mManager;
private CommandFactory mFactory;
private Map<String, Long> mProgress = new HashMap<>();
private Map<String, Integer> mPositions = new HashMap<>();
public DownloadAdapter(Context context, List<DownloadEntity> data) {
super(context, data);
int i = 0;
for (DownloadEntity entity : data) {
mProgress.put(entity.getDownloadUrl(), entity.getCurrentProgress());
mPositions.put(entity.getDownloadUrl(), i);
i++;
}
@ -52,15 +51,26 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
}
public synchronized void updateState(DownloadEntity entity) {
notifyItemChanged(indexItem(entity.getDownloadUrl()));
if (entity.getState() == DownloadEntity.STATE_CANCEL) {
mPositions.clear();
int i = 0;
for (DownloadEntity entity_1 : mData) {
mPositions.put(entity_1.getDownloadUrl(), i);
i++;
}
notifyDataSetChanged();
} else {
int position = indexItem(entity.getDownloadUrl());
mData.set(position, entity);
notifyItemChanged(position);
}
}
public synchronized void setProgress(String url, long currentPosition) {
mProgress.put(url, currentPosition);
// int index = indexItem(url);
// L.d(TAG, "index ==> " + index);
// notifyItemChanged(index);
notifyItemChanged(indexItem(url));
public synchronized void setProgress(DownloadEntity entity) {
String url = entity.getDownloadUrl();
int position = indexItem(url);
mData.set(position, entity);
notifyItemChanged(position);
}
private synchronized int indexItem(String url) {
@ -74,14 +84,12 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
return -1;
}
@Override protected void bindData(MyHolder holder, int position, DownloadEntity item) {
//holder.progress.setProgress(item.getCurrentProgress());
long size = item.getFileSize();
int current = 0;
if (size == 0) {
current = 0;
}
current = (int) (mProgress.get(item.getDownloadUrl()) * 100 / item.getFileSize());
@Override protected void bindData(MyHolder holder, int position, final DownloadEntity item) {
long size = item.getFileSize();
int current = 0;
long progress = item.getCurrentProgress();
long speed = item.getSpeed();
current = size == 0 ? 0 : (int) (progress * 100 / size);
holder.progress.setProgress(current);
BtClickListener listener = new BtClickListener(position, item);
holder.bt.setOnClickListener(listener);
@ -108,6 +116,22 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
}
holder.bt.setText(str);
holder.bt.setTextColor(getColor(color));
holder.speed.setText(Util.formatFileSize(speed) + "/s");
holder.fileSize.setText(covertCurrentSize(progress) + "/" + Util.formatFileSize(size));
holder.cancel.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
mData.remove(item);
notifyDataSetChanged();
IDownloadCommand cancelCommand =
mFactory.createCommand(getContext(), item, CommandFactory.TASK_CANCEL);
mManager.setCommand(cancelCommand).exe();
}
});
}
private String covertCurrentSize(long currentSize) {
String size = Util.formatFileSize(currentSize);
return size.substring(0, size.length() - 1);
}
private int getColor(int color) {
@ -124,7 +148,6 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
}
@Override public void onClick(View v) {
L.d(TAG, "position ==> " + position);
switch (entity.getState()) {
case DownloadEntity.STATE_WAIT:
case DownloadEntity.STATE_OTHER:
@ -140,7 +163,6 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
}
private void start(DownloadEntity entity) {
List<IDownloadCommand> commands = new ArrayList<>();
IDownloadCommand addCommand =
mFactory.createCommand(getContext(), entity, CommandFactory.TASK_CREATE);
@ -156,17 +178,14 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
mFactory.createCommand(getContext(), entity, CommandFactory.TASK_STOP);
mManager.setCommand(stopCommand).exe();
}
private void cancel(DownloadEntity entity) {
IDownloadCommand cancelCommand =
mFactory.createCommand(getContext(), entity, CommandFactory.TASK_CANCEL);
mManager.setCommand(cancelCommand).exe();
}
}
class MyHolder 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;
MyHolder(View itemView) {
super(itemView);

View File

@ -4,7 +4,7 @@ import android.content.Context;
import android.content.IntentFilter;
import android.os.Environment;
import com.arialyy.downloadutil.core.DownloadManager;
import com.arialyy.downloadutil.entity.DownloadEntity;
import com.arialyy.downloadutil.core.DownloadEntity;
import com.arialyy.downloadutil.util.Util;
import com.arialyy.frame.util.AndroidUtils;
import com.arialyy.frame.util.StringUtil;
@ -27,11 +27,8 @@ public class DownloadModule extends BaseModule {
*/
public List<DownloadEntity> getDownloadData() {
List<DownloadEntity> list = DownloadEntity.findAllData(DownloadEntity.class);
List<DownloadEntity> newDownload = createNewDownload();
if (list == null) {
list = newDownload;
} else {
list = filter(list, newDownload);
if (list == null || list.size() == 0) {
list = createNewDownload();
}
return list;
}
@ -53,7 +50,7 @@ public class DownloadModule extends BaseModule {
break;
}
count++;
if (count == sqlEntity.size()) {
if (count == createdEntity.size()) {
list.add(cEntity);
}
}
@ -74,6 +71,7 @@ public class DownloadModule extends BaseModule {
entity.setDownloadUrl(url);
entity.setDownloadPath(getDownloadPath(url));
entity.setFileName(fileName);
entity.save();
list.add(entity);
}
return list;

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/black" android:state_pressed="true"/>
<item android:color="@color/colorAccent" android:state_pressed="false"/>
</selector>

View File

@ -9,19 +9,50 @@
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:layout_toLeftOf="@+id/bt"
android:max="100"
/>
<TextView
android:id="@+id/fileSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBar"
android:layout_marginTop="8dp"
android:text="0mb/0mb"
android:textColor="@color/black"
/>
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="开始"
style="?buttonBarButtonStyle"
/>
<TextView
android:id="@+id/speed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/fileSize"
android:layout_centerHorizontal="true"
android:text="0kb/s"
android:textColor="@color/black"
/>
<TextView
android:id="@+id/del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/progressBar"
android:layout_alignTop="@+id/fileSize"
android:text="删除"
android:textColor="@color/bt_selector_cancel"
/>
</RelativeLayout>

View File

@ -3,4 +3,5 @@
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="black">#2B2B2B</color>
</resources>