pull/2/head
AriaLyy 8 years ago
parent 6d696ccfa2
commit c1de3b8419
  1. 33
      app/src/main/java/com/arialyy/simple/activity/MainActivity.java
  2. 7
      app/src/main/java/com/arialyy/simple/activity/MultiTaskActivity.java
  3. 38
      app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java
  4. 28
      app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java
  5. 38
      app/src/main/java/com/arialyy/simple/module/DownloadModule.java
  6. 1
      app/src/main/res/layout/activity_main.xml
  7. 2
      downloadutil/src/main/java/com/arialyy/downloadutil/core/Aria.java
  8. 2
      downloadutil/src/main/java/com/arialyy/downloadutil/core/DownloadEntity.java
  9. 1
      downloadutil/src/main/java/com/arialyy/downloadutil/core/queue/DownloadTaskQueue.java
  10. 5
      downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/DownloadSchedulers.java
  11. 1
      downloadutil/src/main/java/com/arialyy/downloadutil/core/scheduler/IDownloadSchedulers.java
  12. 22
      downloadutil/src/main/java/com/arialyy/downloadutil/core/task/Task.java

@ -14,7 +14,6 @@
* limitations under the License.
*/
package com.arialyy.simple.activity;
import android.Manifest;
@ -48,22 +47,26 @@ public class MainActivity extends BaseActivity<ActivityMainBinding> {
super.init(savedInstanceState);
setSupportActionBar(mBar);
mBar.setTitle("多线程多任务下载");
boolean hasPermission = PermissionManager.getInstance()
.checkPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (hasPermission || Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
setEnable(true);
} else {
setEnable(false);
PermissionManager.getInstance().requestPermission(this, new OnPermissionCallback() {
@Override public void onSuccess(String... permissions) {
setEnable(true);
}
} else { //6.0处理
boolean hasPermission = PermissionManager.getInstance()
.checkPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (hasPermission) {
setEnable(true);
} else {
setEnable(false);
PermissionManager.getInstance().requestPermission(this, new OnPermissionCallback() {
@Override public void onSuccess(String... permissions) {
setEnable(true);
}
@Override public void onFail(String... permissions) {
T.showShort(MainActivity.this, "没有文件读写权限");
setEnable(false);
}
}, Manifest.permission.WRITE_EXTERNAL_STORAGE);
@Override public void onFail(String... permissions) {
T.showShort(MainActivity.this, "没有文件读写权限");
setEnable(false);
}
}, Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
}
}

@ -17,20 +17,14 @@
package com.arialyy.simple.activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
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.View;
import android.widget.Button;
import butterknife.Bind;
import com.arialyy.downloadutil.core.AMTarget;
import com.arialyy.downloadutil.core.Aria;
import com.arialyy.downloadutil.core.DownloadEntity;
import com.arialyy.downloadutil.core.DownloadManager;
import com.arialyy.downloadutil.core.task.Task;
import com.arialyy.frame.util.show.L;
import com.arialyy.simple.R;
@ -116,7 +110,6 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
@Override protected void onResume() {
super.onResume();
//registerReceiver(mReceiver, getModule(DownloadModule.class).getDownloadFilter());
Aria.whit(this).addSchedulerListener(new MySchedulerListener());
}

@ -28,7 +28,6 @@ import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import com.arialyy.downloadutil.core.AMReceiver;
import com.arialyy.downloadutil.core.AMTarget;
import com.arialyy.downloadutil.core.Aria;
import com.arialyy.downloadutil.core.DownloadEntity;
@ -47,11 +46,13 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
public static final int DOWNLOAD_RESUME = 0x05;
public static final int DOWNLOAD_COMPLETE = 0x06;
public static final int DOWNLOAD_RUNNING = 0x07;
private ProgressBar mPb;
private String mDownloadUrl =
"http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk";
private Button mStart, mStop, mCancel;
private TextView mSize;
private static final String DOWNLOAD_URL =
"http://static.gaoshouyou.com/d/3a/93/573ae1db9493a801c24bf66128b11e39.apk";
@Bind(R.id.progressBar) ProgressBar 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.toolbar) Toolbar toolbar;
private DownloadEntity mEntity;
private BroadcastReceiver mReceiver;
@ -115,15 +116,11 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
@Override protected void onResume() {
super.onResume();
//IntentFilter filter = getModule(DownloadModule.class).getDownloadFilter();
//mReceiver = getModule(DownloadModule.class).createReceiver(mUpdateHandler);
//registerReceiver(mReceiver, filter);
Aria.whit(this).addSchedulerListener(new MySchedulerListener());
}
@Override protected void onDestroy() {
super.onDestroy();
//unregisterReceiver(mReceiver);
}
@Override protected int setLayoutId() {
@ -138,15 +135,8 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
}
private void init() {
mPb = (ProgressBar) findViewById(R.id.progressBar);
mStart = (Button) findViewById(R.id.start);
mStop = (Button) findViewById(R.id.stop);
mCancel = (Button) findViewById(R.id.cancel);
mSize = (TextView) findViewById(R.id.size);
//mFactory = CmdFactory.getInstance();
//mManager = DownloadManager.getInstance();
mEntity = DbEntity.findData(DownloadEntity.class, new String[] { "downloadUrl" },
new String[] { mDownloadUrl });
new String[] { DOWNLOAD_URL });
if (mEntity != null) {
mPb.setProgress((int) ((mEntity.getCurrentProgress() * 100) / mEntity.getFileSize()));
mSize.setText(CommonUtil.formatFileSize(mEntity.getFileSize()));
@ -159,7 +149,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
} else {
mEntity = new DownloadEntity();
mEntity.setFileName("test.apk");
mEntity.setDownloadUrl(mDownloadUrl);
mEntity.setDownloadUrl(DOWNLOAD_URL);
mEntity.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk");
}
}
@ -170,7 +160,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
String text = ((TextView) view).getText().toString();
if (text.equals("重新开始?") || text.equals("开始")) {
start();
}else if (text.equals("恢复")){
} else if (text.equals("恢复")) {
resume();
}
break;
@ -183,14 +173,11 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
}
}
private void resume(){
private void resume() {
Aria.whit(this).load(mEntity).resume();
}
private void start() {
mEntity.setFileName("test.apk");
mEntity.setDownloadUrl(mDownloadUrl);
mEntity.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk");
Aria.whit(this).load(mEntity).start();
}
@ -202,7 +189,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
Aria.whit(this).load(mEntity).cancel();
}
private class MySchedulerListener extends AMTarget.SimpleSchedulerListener{
private class MySchedulerListener extends AMTarget.SimpleSchedulerListener {
@Override public void onTaskStart(Task task) {
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize())
.sendToTarget();
@ -240,5 +227,4 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
}
}
}
}

@ -38,6 +38,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* Created by Lyy on 2016/9/27.
@ -47,7 +48,7 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
private static final String TAG = "DownloadAdapter";
private DownloadManager mManager;
private CmdFactory mFactory;
private Map<String, Integer> mPositions = new HashMap<>();
private Map<String, Integer> mPositions = new ConcurrentHashMap<>();
public DownloadAdapter(Context context, List<DownloadEntity> data) {
super(context, data);
@ -82,6 +83,9 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
notifyDataSetChanged();
} else {
int position = indexItem(entity.getDownloadUrl());
if (position == -1){
return;
}
mData.set(position, entity);
notifyItemChanged(position);
}
@ -90,16 +94,18 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
public synchronized void setProgress(DownloadEntity entity) {
String url = entity.getDownloadUrl();
int position = indexItem(url);
if (position == -1){
return;
}
mData.set(position, entity);
notifyItemChanged(position);
}
private synchronized int indexItem(String url) {
Set set = mPositions.entrySet();
for (Object aSet : set) {
Map.Entry entry = (Map.Entry) aSet;
if (entry.getKey().equals(url)) {
return (int) entry.getValue();
Set<String> keys = mPositions.keySet();
for (String key : keys){
if (key.equals(url)){
return mPositions.get(key);
}
}
return -1;
@ -112,7 +118,7 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
long speed = item.getSpeed();
current = size == 0 ? 0 : (int) (progress * 100 / size);
holder.progress.setProgress(current);
BtClickListener listener = new BtClickListener(position, item);
BtClickListener listener = new BtClickListener(item);
holder.bt.setOnClickListener(listener);
String str = "";
int color = android.R.color.holo_green_light;
@ -166,11 +172,9 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
private class BtClickListener implements View.OnClickListener {
private DownloadEntity entity;
private int position;
BtClickListener(int position, DownloadEntity entity) {
BtClickListener(DownloadEntity entity) {
this.entity = entity;
this.position = position;
}
@Override public void onClick(View v) {
@ -189,14 +193,10 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
}
private void start(DownloadEntity entity) {
//IDownloadCmd startCmd = mFactory.createCmd(entity, CmdFactory.TASK_START);
//mManager.setCmd(startCmd).exe();
Aria.whit(getContext()).load(entity).start();
}
private void stop(DownloadEntity entity) {
//IDownloadCmd stopCmd = mFactory.createCmd(entity, CmdFactory.TASK_STOP);
//mManager.setCmd(stopCmd).exe();
Aria.whit(getContext()).load(entity).stop();
}
}

@ -14,7 +14,6 @@
* limitations under the License.
*/
package com.arialyy.simple.module;
import android.content.BroadcastReceiver;
@ -48,9 +47,16 @@ public class DownloadModule extends BaseModule {
* 设置下载数据
*/
public List<DownloadEntity> getDownloadData() {
List<DownloadEntity> list = DownloadEntity.findAllData(DownloadEntity.class);
if (list == null || list.size() == 0) {
list = createNewDownload();
String[] urls = getContext().getResources().getStringArray(R.array.test_apk_download_url);
List<DownloadEntity> list = new ArrayList<>();
for (String url : urls) {
DownloadEntity entity =
DownloadEntity.findData(DownloadEntity.class, new String[] { "downloadUrl" },
new String[] { url });
if (entity == null) {
entity = createDownloadEntity(url);
}
list.add(entity);
}
return list;
}
@ -80,6 +86,17 @@ public class DownloadModule extends BaseModule {
return list;
}
private DownloadEntity createDownloadEntity(String url) {
String fileName = CommonUtil.keyToHashCode(url) + ".apk";
DownloadEntity entity = new DownloadEntity();
entity.setDownloadUrl(url);
entity.setDownloadPath(getDownloadPath(url));
entity.setFileName(fileName);
//entity.setFileName("taskName_________" + i);
entity.save();
return entity;
}
/**
* 创建下载列表
*/
@ -88,14 +105,7 @@ public class DownloadModule extends BaseModule {
String[] urls = getContext().getResources().getStringArray(R.array.test_apk_download_url);
int i = 0;
for (String url : urls) {
String fileName = CommonUtil.keyToHashCode(url) + ".apk";
DownloadEntity entity = new DownloadEntity();
entity.setDownloadUrl(url);
entity.setDownloadPath(getDownloadPath(url));
//entity.setFileName(fileName);
entity.setFileName("taskName_________" + i);
entity.save();
list.add(entity);
list.add(createDownloadEntity(url));
i++;
}
return list;
@ -122,7 +132,7 @@ public class DownloadModule extends BaseModule {
/**
* 创建Receiver
*/
public BroadcastReceiver createReceiver(final Handler handler){
public BroadcastReceiver createReceiver(final Handler handler) {
return new BroadcastReceiver() {
long len = 0;
@ -146,7 +156,7 @@ public class DownloadModule extends BaseModule {
break;
case DownloadManager.ACTION_RUNNING:
long current = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 0);
int progress = len ==0 ? 0 : (int) ((current * 100) / len);
int progress = len == 0 ? 0 : (int) ((current * 100) / len);
handler.obtainMessage(SingleTaskActivity.DOWNLOAD_RUNNING, progress).sendToTarget();
break;
case DownloadManager.ACTION_STOP:

@ -3,6 +3,7 @@
>
<LinearLayout
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"

@ -22,9 +22,7 @@ import android.app.Application;
import android.app.Fragment;
import android.app.Service;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Build;
import com.bumptech.glide.Glide;
/**
* Created by lyy on 2016/12/1.

@ -75,6 +75,7 @@ public class DownloadEntity extends DbEntity implements Parcelable {
}
};
@Ignore private long speed = 0; //下载速度
@Ignore private int failNum = 0;
private String downloadUrl = ""; //下载路径
private String downloadPath = ""; //保存路径
private String fileName = ""; //文件名
@ -83,7 +84,6 @@ public class DownloadEntity extends DbEntity implements Parcelable {
private int state = STATE_WAIT;
private boolean isDownloadComplete = false; //是否下载完成
private long currentProgress = 0; //当前下载进度
private int failNum = 0;
private long completeTime; //完成时间
public DownloadEntity() {

@ -82,6 +82,7 @@ public class DownloadTaskQueue implements ITaskQueue {
@Override public void startTask(Task task) {
if (mExecutePool.putTask(task)) {
mCachePool.removeTask(task);
task.getDownloadEntity().setFailNum(0);
task.start();
}
}

@ -186,6 +186,11 @@ public class DownloadSchedulers implements IDownloadSchedulers {
if (entity.getFailNum() <= mFailNum) {
Task task = mQueue.getTask(entity);
mQueue.reTryStart(task);
try {
Thread.currentThread().sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
startNextTask(entity);
}

@ -16,7 +16,6 @@
package com.arialyy.downloadutil.core.scheduler;
import android.content.Context;
import android.os.Handler;
import com.arialyy.downloadutil.core.DownloadEntity;

@ -218,7 +218,7 @@ public class Task {
@Override public void onPre() {
super.onPre();
downloadEntity.setState(DownloadEntity.STATE_PRE);
//sendIntent(DownloadManager.ACTION_PRE, -1);
sendIntent(DownloadManager.ACTION_PRE, -1);
}
@Override public void onPostPre(long fileSize) {
@ -226,21 +226,22 @@ public class Task {
downloadEntity.setFileSize(fileSize);
downloadEntity.setState(DownloadEntity.STATE_POST_PRE);
sendInState2Target(DownloadSchedulers.PRE);
//sendIntent(DownloadManager.ACTION_POST_PRE, -1);
sendIntent(DownloadManager.ACTION_POST_PRE, -1);
}
@Override public void onResume(long resumeLocation) {
super.onResume(resumeLocation);
downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING);
sendInState2Target(DownloadSchedulers.RESUME);
//sendIntent(DownloadManager.ACTION_RESUME, resumeLocation);
sendIntent(DownloadManager.ACTION_RESUME, resumeLocation);
}
@Override public void onStart(long startLocation) {
super.onStart(startLocation);
downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING);
downloadEntity.setFailNum(0);
sendInState2Target(DownloadSchedulers.START);
//sendIntent(DownloadManager.ACTION_START, startLocation);
sendIntent(DownloadManager.ACTION_START, startLocation);
}
@Override public void onProgress(long currentLocation) {
@ -259,7 +260,7 @@ public class Task {
downloadEntity.setCurrentProgress(currentLocation);
lastLen = currentLocation;
sendInState2Target(DownloadSchedulers.RUNNING);
//context.sendBroadcast(sendIntent);
context.sendBroadcast(sendIntent);
}
}
@ -268,14 +269,14 @@ public class Task {
downloadEntity.setState(DownloadEntity.STATE_STOP);
downloadEntity.setSpeed(0);
sendInState2Target(DownloadSchedulers.STOP);
//sendIntent(DownloadManager.ACTION_STOP, stopLocation);
sendIntent(DownloadManager.ACTION_STOP, stopLocation);
}
@Override public void onCancel() {
super.onCancel();
downloadEntity.setState(DownloadEntity.STATE_CANCEL);
sendInState2Target(DownloadSchedulers.CANCEL);
//sendIntent(DownloadManager.ACTION_CANCEL, -1);
sendIntent(DownloadManager.ACTION_CANCEL, -1);
downloadEntity.deleteData();
}
@ -285,15 +286,16 @@ public class Task {
downloadEntity.setDownloadComplete(true);
downloadEntity.setSpeed(0);
sendInState2Target(DownloadSchedulers.COMPLETE);
//sendIntent(DownloadManager.ACTION_COMPLETE, downloadEntity.getFileSize());
sendIntent(DownloadManager.ACTION_COMPLETE, downloadEntity.getFileSize());
}
@Override public void onFail() {
super.onFail();
downloadEntity.setFailNum(downloadEntity.getFailNum() + 1);
downloadEntity.setState(DownloadEntity.STATE_FAIL);
downloadEntity.setSpeed(0);
sendInState2Target(DownloadSchedulers.FAIL);
//sendIntent(DownloadManager.ACTION_FAIL, -1);
sendIntent(DownloadManager.ACTION_FAIL, -1);
}
private void sendIntent(String action, long location) {
@ -305,7 +307,7 @@ public class Task {
if (location != -1) {
intent.putExtra(DownloadManager.CURRENT_LOCATION, location);
}
context.sendBroadcast(intent);
//context.sendBroadcast(intent);
}
}
}
Loading…
Cancel
Save