From c1de3b84196dcccf9b9c57eb62de105d8e07513a Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Tue, 6 Dec 2016 18:16:31 +0800 Subject: [PATCH] fix bug --- .../arialyy/simple/activity/MainActivity.java | 33 ++++++++-------- .../simple/activity/MultiTaskActivity.java | 7 ---- .../simple/activity/SingleTaskActivity.java | 38 ++++++------------- .../simple/adapter/DownloadAdapter.java | 28 +++++++------- .../arialyy/simple/module/DownloadModule.java | 38 ++++++++++++------- app/src/main/res/layout/activity_main.xml | 1 + .../com/arialyy/downloadutil/core/Aria.java | 2 - .../downloadutil/core/DownloadEntity.java | 2 +- .../core/queue/DownloadTaskQueue.java | 1 + .../core/scheduler/DownloadSchedulers.java | 5 +++ .../core/scheduler/IDownloadSchedulers.java | 1 - .../arialyy/downloadutil/core/task/Task.java | 22 ++++++----- 12 files changed, 88 insertions(+), 90 deletions(-) diff --git a/app/src/main/java/com/arialyy/simple/activity/MainActivity.java b/app/src/main/java/com/arialyy/simple/activity/MainActivity.java index 6c22b7f4..f73c6eed 100644 --- a/app/src/main/java/com/arialyy/simple/activity/MainActivity.java +++ b/app/src/main/java/com/arialyy/simple/activity/MainActivity.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 { 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); + } } } diff --git a/app/src/main/java/com/arialyy/simple/activity/MultiTaskActivity.java b/app/src/main/java/com/arialyy/simple/activity/MultiTaskActivity.java index 8ce713eb..4fb51665 100644 --- a/app/src/main/java/com/arialyy/simple/activity/MultiTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/activity/MultiTaskActivity.java @@ -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 { @Override protected void onResume() { super.onResume(); - //registerReceiver(mReceiver, getModule(DownloadModule.class).getDownloadFilter()); Aria.whit(this).addSchedulerListener(new MySchedulerListener()); } diff --git a/app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java index 515e26d5..c8e52ede 100644 --- a/app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java @@ -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 { 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 { @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 { } 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 { } 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 { 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 { } } - 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 { 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 { } } } - } \ No newline at end of file diff --git a/app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java b/app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java index 15f26222..93aa7216 100644 --- a/app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java +++ b/app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java @@ -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 mPositions = new HashMap<>(); + private Map mPositions = new ConcurrentHashMap<>(); public DownloadAdapter(Context context, List data) { super(context, data); @@ -82,6 +83,9 @@ public class DownloadAdapter extends AbsRVAdapter 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 getDownloadData() { - List 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 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: diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index fb7c67ea..d3137e4e 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -3,6 +3,7 @@ >