fix bug
This commit is contained in:
@ -14,7 +14,6 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
package com.arialyy.simple.activity;
|
package com.arialyy.simple.activity;
|
||||||
|
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
@ -48,22 +47,26 @@ public class MainActivity extends BaseActivity<ActivityMainBinding> {
|
|||||||
super.init(savedInstanceState);
|
super.init(savedInstanceState);
|
||||||
setSupportActionBar(mBar);
|
setSupportActionBar(mBar);
|
||||||
mBar.setTitle("多线程多任务下载");
|
mBar.setTitle("多线程多任务下载");
|
||||||
boolean hasPermission = PermissionManager.getInstance()
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
|
||||||
.checkPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
|
||||||
if (hasPermission || Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
|
|
||||||
setEnable(true);
|
setEnable(true);
|
||||||
} else {
|
} else { //6.0处理
|
||||||
setEnable(false);
|
boolean hasPermission = PermissionManager.getInstance()
|
||||||
PermissionManager.getInstance().requestPermission(this, new OnPermissionCallback() {
|
.checkPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
||||||
@Override public void onSuccess(String... permissions) {
|
if (hasPermission) {
|
||||||
setEnable(true);
|
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) {
|
@Override public void onFail(String... permissions) {
|
||||||
T.showShort(MainActivity.this, "没有文件读写权限");
|
T.showShort(MainActivity.this, "没有文件读写权限");
|
||||||
setEnable(false);
|
setEnable(false);
|
||||||
}
|
}
|
||||||
}, Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
}, Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,20 +17,14 @@
|
|||||||
|
|
||||||
package com.arialyy.simple.activity;
|
package com.arialyy.simple.activity;
|
||||||
|
|
||||||
import android.content.BroadcastReceiver;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v7.widget.LinearLayoutManager;
|
import android.support.v7.widget.LinearLayoutManager;
|
||||||
import android.support.v7.widget.RecyclerView;
|
import android.support.v7.widget.RecyclerView;
|
||||||
import android.support.v7.widget.Toolbar;
|
import android.support.v7.widget.Toolbar;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
|
||||||
import butterknife.Bind;
|
import butterknife.Bind;
|
||||||
import com.arialyy.downloadutil.core.AMTarget;
|
import com.arialyy.downloadutil.core.AMTarget;
|
||||||
import com.arialyy.downloadutil.core.Aria;
|
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.downloadutil.core.task.Task;
|
||||||
import com.arialyy.frame.util.show.L;
|
import com.arialyy.frame.util.show.L;
|
||||||
import com.arialyy.simple.R;
|
import com.arialyy.simple.R;
|
||||||
@ -116,7 +110,6 @@ public class MultiTaskActivity extends BaseActivity<ActivityMultiBinding> {
|
|||||||
|
|
||||||
@Override protected void onResume() {
|
@Override protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
//registerReceiver(mReceiver, getModule(DownloadModule.class).getDownloadFilter());
|
|
||||||
Aria.whit(this).addSchedulerListener(new MySchedulerListener());
|
Aria.whit(this).addSchedulerListener(new MySchedulerListener());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,7 +28,6 @@ import android.widget.ProgressBar;
|
|||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
import butterknife.Bind;
|
import butterknife.Bind;
|
||||||
import com.arialyy.downloadutil.core.AMReceiver;
|
|
||||||
import com.arialyy.downloadutil.core.AMTarget;
|
import com.arialyy.downloadutil.core.AMTarget;
|
||||||
import com.arialyy.downloadutil.core.Aria;
|
import com.arialyy.downloadutil.core.Aria;
|
||||||
import com.arialyy.downloadutil.core.DownloadEntity;
|
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_RESUME = 0x05;
|
||||||
public static final int DOWNLOAD_COMPLETE = 0x06;
|
public static final int DOWNLOAD_COMPLETE = 0x06;
|
||||||
public static final int DOWNLOAD_RUNNING = 0x07;
|
public static final int DOWNLOAD_RUNNING = 0x07;
|
||||||
private ProgressBar mPb;
|
private static final String DOWNLOAD_URL =
|
||||||
private String mDownloadUrl =
|
"http://static.gaoshouyou.com/d/3a/93/573ae1db9493a801c24bf66128b11e39.apk";
|
||||||
"http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk";
|
@Bind(R.id.progressBar) ProgressBar mPb;
|
||||||
private Button mStart, mStop, mCancel;
|
@Bind(R.id.start) Button mStart;
|
||||||
private TextView mSize;
|
@Bind(R.id.stop) Button mStop;
|
||||||
|
@Bind(R.id.cancel) Button mCancel;
|
||||||
|
@Bind(R.id.size) TextView mSize;
|
||||||
@Bind(R.id.toolbar) Toolbar toolbar;
|
@Bind(R.id.toolbar) Toolbar toolbar;
|
||||||
private DownloadEntity mEntity;
|
private DownloadEntity mEntity;
|
||||||
private BroadcastReceiver mReceiver;
|
private BroadcastReceiver mReceiver;
|
||||||
@ -115,15 +116,11 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
|||||||
|
|
||||||
@Override protected void onResume() {
|
@Override protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
//IntentFilter filter = getModule(DownloadModule.class).getDownloadFilter();
|
|
||||||
//mReceiver = getModule(DownloadModule.class).createReceiver(mUpdateHandler);
|
|
||||||
//registerReceiver(mReceiver, filter);
|
|
||||||
Aria.whit(this).addSchedulerListener(new MySchedulerListener());
|
Aria.whit(this).addSchedulerListener(new MySchedulerListener());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override protected void onDestroy() {
|
@Override protected void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
//unregisterReceiver(mReceiver);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override protected int setLayoutId() {
|
@Override protected int setLayoutId() {
|
||||||
@ -138,15 +135,8 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void init() {
|
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" },
|
mEntity = DbEntity.findData(DownloadEntity.class, new String[] { "downloadUrl" },
|
||||||
new String[] { mDownloadUrl });
|
new String[] { DOWNLOAD_URL });
|
||||||
if (mEntity != null) {
|
if (mEntity != null) {
|
||||||
mPb.setProgress((int) ((mEntity.getCurrentProgress() * 100) / mEntity.getFileSize()));
|
mPb.setProgress((int) ((mEntity.getCurrentProgress() * 100) / mEntity.getFileSize()));
|
||||||
mSize.setText(CommonUtil.formatFileSize(mEntity.getFileSize()));
|
mSize.setText(CommonUtil.formatFileSize(mEntity.getFileSize()));
|
||||||
@ -159,7 +149,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
|||||||
} else {
|
} else {
|
||||||
mEntity = new DownloadEntity();
|
mEntity = new DownloadEntity();
|
||||||
mEntity.setFileName("test.apk");
|
mEntity.setFileName("test.apk");
|
||||||
mEntity.setDownloadUrl(mDownloadUrl);
|
mEntity.setDownloadUrl(DOWNLOAD_URL);
|
||||||
mEntity.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk");
|
mEntity.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -170,7 +160,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
|||||||
String text = ((TextView) view).getText().toString();
|
String text = ((TextView) view).getText().toString();
|
||||||
if (text.equals("重新开始?") || text.equals("开始")) {
|
if (text.equals("重新开始?") || text.equals("开始")) {
|
||||||
start();
|
start();
|
||||||
}else if (text.equals("恢复")){
|
} else if (text.equals("恢复")) {
|
||||||
resume();
|
resume();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -183,14 +173,11 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void resume(){
|
private void resume() {
|
||||||
Aria.whit(this).load(mEntity).resume();
|
Aria.whit(this).load(mEntity).resume();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void start() {
|
private void start() {
|
||||||
mEntity.setFileName("test.apk");
|
|
||||||
mEntity.setDownloadUrl(mDownloadUrl);
|
|
||||||
mEntity.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/test.apk");
|
|
||||||
Aria.whit(this).load(mEntity).start();
|
Aria.whit(this).load(mEntity).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -202,7 +189,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
|||||||
Aria.whit(this).load(mEntity).cancel();
|
Aria.whit(this).load(mEntity).cancel();
|
||||||
}
|
}
|
||||||
|
|
||||||
private class MySchedulerListener extends AMTarget.SimpleSchedulerListener{
|
private class MySchedulerListener extends AMTarget.SimpleSchedulerListener {
|
||||||
@Override public void onTaskStart(Task task) {
|
@Override public void onTaskStart(Task task) {
|
||||||
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize())
|
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, task.getDownloadEntity().getFileSize())
|
||||||
.sendToTarget();
|
.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.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by Lyy on 2016/9/27.
|
* 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 static final String TAG = "DownloadAdapter";
|
||||||
private DownloadManager mManager;
|
private DownloadManager mManager;
|
||||||
private CmdFactory mFactory;
|
private CmdFactory mFactory;
|
||||||
private Map<String, Integer> mPositions = new HashMap<>();
|
private Map<String, Integer> mPositions = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
public DownloadAdapter(Context context, List<DownloadEntity> data) {
|
public DownloadAdapter(Context context, List<DownloadEntity> data) {
|
||||||
super(context, data);
|
super(context, data);
|
||||||
@ -82,6 +83,9 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
|
|||||||
notifyDataSetChanged();
|
notifyDataSetChanged();
|
||||||
} else {
|
} else {
|
||||||
int position = indexItem(entity.getDownloadUrl());
|
int position = indexItem(entity.getDownloadUrl());
|
||||||
|
if (position == -1){
|
||||||
|
return;
|
||||||
|
}
|
||||||
mData.set(position, entity);
|
mData.set(position, entity);
|
||||||
notifyItemChanged(position);
|
notifyItemChanged(position);
|
||||||
}
|
}
|
||||||
@ -90,16 +94,18 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
|
|||||||
public synchronized void setProgress(DownloadEntity entity) {
|
public synchronized void setProgress(DownloadEntity entity) {
|
||||||
String url = entity.getDownloadUrl();
|
String url = entity.getDownloadUrl();
|
||||||
int position = indexItem(url);
|
int position = indexItem(url);
|
||||||
|
if (position == -1){
|
||||||
|
return;
|
||||||
|
}
|
||||||
mData.set(position, entity);
|
mData.set(position, entity);
|
||||||
notifyItemChanged(position);
|
notifyItemChanged(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized int indexItem(String url) {
|
private synchronized int indexItem(String url) {
|
||||||
Set set = mPositions.entrySet();
|
Set<String> keys = mPositions.keySet();
|
||||||
for (Object aSet : set) {
|
for (String key : keys){
|
||||||
Map.Entry entry = (Map.Entry) aSet;
|
if (key.equals(url)){
|
||||||
if (entry.getKey().equals(url)) {
|
return mPositions.get(key);
|
||||||
return (int) entry.getValue();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
@ -112,7 +118,7 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
|
|||||||
long speed = item.getSpeed();
|
long speed = item.getSpeed();
|
||||||
current = size == 0 ? 0 : (int) (progress * 100 / size);
|
current = size == 0 ? 0 : (int) (progress * 100 / size);
|
||||||
holder.progress.setProgress(current);
|
holder.progress.setProgress(current);
|
||||||
BtClickListener listener = new BtClickListener(position, item);
|
BtClickListener listener = new BtClickListener(item);
|
||||||
holder.bt.setOnClickListener(listener);
|
holder.bt.setOnClickListener(listener);
|
||||||
String str = "";
|
String str = "";
|
||||||
int color = android.R.color.holo_green_light;
|
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 class BtClickListener implements View.OnClickListener {
|
||||||
private DownloadEntity entity;
|
private DownloadEntity entity;
|
||||||
private int position;
|
|
||||||
|
|
||||||
BtClickListener(int position, DownloadEntity entity) {
|
BtClickListener(DownloadEntity entity) {
|
||||||
this.entity = entity;
|
this.entity = entity;
|
||||||
this.position = position;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void onClick(View v) {
|
@Override public void onClick(View v) {
|
||||||
@ -189,14 +193,10 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void start(DownloadEntity entity) {
|
private void start(DownloadEntity entity) {
|
||||||
//IDownloadCmd startCmd = mFactory.createCmd(entity, CmdFactory.TASK_START);
|
|
||||||
//mManager.setCmd(startCmd).exe();
|
|
||||||
Aria.whit(getContext()).load(entity).start();
|
Aria.whit(getContext()).load(entity).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void stop(DownloadEntity entity) {
|
private void stop(DownloadEntity entity) {
|
||||||
//IDownloadCmd stopCmd = mFactory.createCmd(entity, CmdFactory.TASK_STOP);
|
|
||||||
//mManager.setCmd(stopCmd).exe();
|
|
||||||
Aria.whit(getContext()).load(entity).stop();
|
Aria.whit(getContext()).load(entity).stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,6 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
package com.arialyy.simple.module;
|
package com.arialyy.simple.module;
|
||||||
|
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
@ -48,9 +47,16 @@ public class DownloadModule extends BaseModule {
|
|||||||
* 设置下载数据
|
* 设置下载数据
|
||||||
*/
|
*/
|
||||||
public List<DownloadEntity> getDownloadData() {
|
public List<DownloadEntity> getDownloadData() {
|
||||||
List<DownloadEntity> list = DownloadEntity.findAllData(DownloadEntity.class);
|
String[] urls = getContext().getResources().getStringArray(R.array.test_apk_download_url);
|
||||||
if (list == null || list.size() == 0) {
|
List<DownloadEntity> list = new ArrayList<>();
|
||||||
list = createNewDownload();
|
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;
|
return list;
|
||||||
}
|
}
|
||||||
@ -80,6 +86,17 @@ public class DownloadModule extends BaseModule {
|
|||||||
return list;
|
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);
|
String[] urls = getContext().getResources().getStringArray(R.array.test_apk_download_url);
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (String url : urls) {
|
for (String url : urls) {
|
||||||
String fileName = CommonUtil.keyToHashCode(url) + ".apk";
|
list.add(createDownloadEntity(url));
|
||||||
DownloadEntity entity = new DownloadEntity();
|
|
||||||
entity.setDownloadUrl(url);
|
|
||||||
entity.setDownloadPath(getDownloadPath(url));
|
|
||||||
//entity.setFileName(fileName);
|
|
||||||
entity.setFileName("taskName_________" + i);
|
|
||||||
entity.save();
|
|
||||||
list.add(entity);
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
@ -122,7 +132,7 @@ public class DownloadModule extends BaseModule {
|
|||||||
/**
|
/**
|
||||||
* 创建Receiver
|
* 创建Receiver
|
||||||
*/
|
*/
|
||||||
public BroadcastReceiver createReceiver(final Handler handler){
|
public BroadcastReceiver createReceiver(final Handler handler) {
|
||||||
|
|
||||||
return new BroadcastReceiver() {
|
return new BroadcastReceiver() {
|
||||||
long len = 0;
|
long len = 0;
|
||||||
@ -146,7 +156,7 @@ public class DownloadModule extends BaseModule {
|
|||||||
break;
|
break;
|
||||||
case DownloadManager.ACTION_RUNNING:
|
case DownloadManager.ACTION_RUNNING:
|
||||||
long current = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 0);
|
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();
|
handler.obtainMessage(SingleTaskActivity.DOWNLOAD_RUNNING, progress).sendToTarget();
|
||||||
break;
|
break;
|
||||||
case DownloadManager.ACTION_STOP:
|
case DownloadManager.ACTION_STOP:
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
>
|
>
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
|
android:fitsSystemWindows="true"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
|
@ -22,9 +22,7 @@ import android.app.Application;
|
|||||||
import android.app.Fragment;
|
import android.app.Fragment;
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.ContextWrapper;
|
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import com.bumptech.glide.Glide;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by lyy on 2016/12/1.
|
* 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 long speed = 0; //下载速度
|
||||||
|
@Ignore private int failNum = 0;
|
||||||
private String downloadUrl = ""; //下载路径
|
private String downloadUrl = ""; //下载路径
|
||||||
private String downloadPath = ""; //保存路径
|
private String downloadPath = ""; //保存路径
|
||||||
private String fileName = ""; //文件名
|
private String fileName = ""; //文件名
|
||||||
@ -83,7 +84,6 @@ public class DownloadEntity extends DbEntity implements Parcelable {
|
|||||||
private int state = STATE_WAIT;
|
private int state = STATE_WAIT;
|
||||||
private boolean isDownloadComplete = false; //是否下载完成
|
private boolean isDownloadComplete = false; //是否下载完成
|
||||||
private long currentProgress = 0; //当前下载进度
|
private long currentProgress = 0; //当前下载进度
|
||||||
private int failNum = 0;
|
|
||||||
private long completeTime; //完成时间
|
private long completeTime; //完成时间
|
||||||
|
|
||||||
public DownloadEntity() {
|
public DownloadEntity() {
|
||||||
|
@ -82,6 +82,7 @@ public class DownloadTaskQueue implements ITaskQueue {
|
|||||||
@Override public void startTask(Task task) {
|
@Override public void startTask(Task task) {
|
||||||
if (mExecutePool.putTask(task)) {
|
if (mExecutePool.putTask(task)) {
|
||||||
mCachePool.removeTask(task);
|
mCachePool.removeTask(task);
|
||||||
|
task.getDownloadEntity().setFailNum(0);
|
||||||
task.start();
|
task.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -186,6 +186,11 @@ public class DownloadSchedulers implements IDownloadSchedulers {
|
|||||||
if (entity.getFailNum() <= mFailNum) {
|
if (entity.getFailNum() <= mFailNum) {
|
||||||
Task task = mQueue.getTask(entity);
|
Task task = mQueue.getTask(entity);
|
||||||
mQueue.reTryStart(task);
|
mQueue.reTryStart(task);
|
||||||
|
try {
|
||||||
|
Thread.currentThread().sleep(4000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
startNextTask(entity);
|
startNextTask(entity);
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
package com.arialyy.downloadutil.core.scheduler;
|
package com.arialyy.downloadutil.core.scheduler;
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import com.arialyy.downloadutil.core.DownloadEntity;
|
import com.arialyy.downloadutil.core.DownloadEntity;
|
||||||
|
|
||||||
|
@ -218,7 +218,7 @@ public class Task {
|
|||||||
@Override public void onPre() {
|
@Override public void onPre() {
|
||||||
super.onPre();
|
super.onPre();
|
||||||
downloadEntity.setState(DownloadEntity.STATE_PRE);
|
downloadEntity.setState(DownloadEntity.STATE_PRE);
|
||||||
//sendIntent(DownloadManager.ACTION_PRE, -1);
|
sendIntent(DownloadManager.ACTION_PRE, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void onPostPre(long fileSize) {
|
@Override public void onPostPre(long fileSize) {
|
||||||
@ -226,21 +226,22 @@ public class Task {
|
|||||||
downloadEntity.setFileSize(fileSize);
|
downloadEntity.setFileSize(fileSize);
|
||||||
downloadEntity.setState(DownloadEntity.STATE_POST_PRE);
|
downloadEntity.setState(DownloadEntity.STATE_POST_PRE);
|
||||||
sendInState2Target(DownloadSchedulers.PRE);
|
sendInState2Target(DownloadSchedulers.PRE);
|
||||||
//sendIntent(DownloadManager.ACTION_POST_PRE, -1);
|
sendIntent(DownloadManager.ACTION_POST_PRE, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void onResume(long resumeLocation) {
|
@Override public void onResume(long resumeLocation) {
|
||||||
super.onResume(resumeLocation);
|
super.onResume(resumeLocation);
|
||||||
downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING);
|
downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING);
|
||||||
sendInState2Target(DownloadSchedulers.RESUME);
|
sendInState2Target(DownloadSchedulers.RESUME);
|
||||||
//sendIntent(DownloadManager.ACTION_RESUME, resumeLocation);
|
sendIntent(DownloadManager.ACTION_RESUME, resumeLocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void onStart(long startLocation) {
|
@Override public void onStart(long startLocation) {
|
||||||
super.onStart(startLocation);
|
super.onStart(startLocation);
|
||||||
downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING);
|
downloadEntity.setState(DownloadEntity.STATE_DOWNLOAD_ING);
|
||||||
|
downloadEntity.setFailNum(0);
|
||||||
sendInState2Target(DownloadSchedulers.START);
|
sendInState2Target(DownloadSchedulers.START);
|
||||||
//sendIntent(DownloadManager.ACTION_START, startLocation);
|
sendIntent(DownloadManager.ACTION_START, startLocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void onProgress(long currentLocation) {
|
@Override public void onProgress(long currentLocation) {
|
||||||
@ -259,7 +260,7 @@ public class Task {
|
|||||||
downloadEntity.setCurrentProgress(currentLocation);
|
downloadEntity.setCurrentProgress(currentLocation);
|
||||||
lastLen = currentLocation;
|
lastLen = currentLocation;
|
||||||
sendInState2Target(DownloadSchedulers.RUNNING);
|
sendInState2Target(DownloadSchedulers.RUNNING);
|
||||||
//context.sendBroadcast(sendIntent);
|
context.sendBroadcast(sendIntent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -268,14 +269,14 @@ public class Task {
|
|||||||
downloadEntity.setState(DownloadEntity.STATE_STOP);
|
downloadEntity.setState(DownloadEntity.STATE_STOP);
|
||||||
downloadEntity.setSpeed(0);
|
downloadEntity.setSpeed(0);
|
||||||
sendInState2Target(DownloadSchedulers.STOP);
|
sendInState2Target(DownloadSchedulers.STOP);
|
||||||
//sendIntent(DownloadManager.ACTION_STOP, stopLocation);
|
sendIntent(DownloadManager.ACTION_STOP, stopLocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void onCancel() {
|
@Override public void onCancel() {
|
||||||
super.onCancel();
|
super.onCancel();
|
||||||
downloadEntity.setState(DownloadEntity.STATE_CANCEL);
|
downloadEntity.setState(DownloadEntity.STATE_CANCEL);
|
||||||
sendInState2Target(DownloadSchedulers.CANCEL);
|
sendInState2Target(DownloadSchedulers.CANCEL);
|
||||||
//sendIntent(DownloadManager.ACTION_CANCEL, -1);
|
sendIntent(DownloadManager.ACTION_CANCEL, -1);
|
||||||
downloadEntity.deleteData();
|
downloadEntity.deleteData();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -285,15 +286,16 @@ public class Task {
|
|||||||
downloadEntity.setDownloadComplete(true);
|
downloadEntity.setDownloadComplete(true);
|
||||||
downloadEntity.setSpeed(0);
|
downloadEntity.setSpeed(0);
|
||||||
sendInState2Target(DownloadSchedulers.COMPLETE);
|
sendInState2Target(DownloadSchedulers.COMPLETE);
|
||||||
//sendIntent(DownloadManager.ACTION_COMPLETE, downloadEntity.getFileSize());
|
sendIntent(DownloadManager.ACTION_COMPLETE, downloadEntity.getFileSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void onFail() {
|
@Override public void onFail() {
|
||||||
super.onFail();
|
super.onFail();
|
||||||
|
downloadEntity.setFailNum(downloadEntity.getFailNum() + 1);
|
||||||
downloadEntity.setState(DownloadEntity.STATE_FAIL);
|
downloadEntity.setState(DownloadEntity.STATE_FAIL);
|
||||||
downloadEntity.setSpeed(0);
|
downloadEntity.setSpeed(0);
|
||||||
sendInState2Target(DownloadSchedulers.FAIL);
|
sendInState2Target(DownloadSchedulers.FAIL);
|
||||||
//sendIntent(DownloadManager.ACTION_FAIL, -1);
|
sendIntent(DownloadManager.ACTION_FAIL, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendIntent(String action, long location) {
|
private void sendIntent(String action, long location) {
|
||||||
@ -305,7 +307,7 @@ public class Task {
|
|||||||
if (location != -1) {
|
if (location != -1) {
|
||||||
intent.putExtra(DownloadManager.CURRENT_LOCATION, location);
|
intent.putExtra(DownloadManager.CURRENT_LOCATION, location);
|
||||||
}
|
}
|
||||||
context.sendBroadcast(intent);
|
//context.sendBroadcast(intent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user