diff --git a/.gradle/2.10/taskArtifacts/cache.properties.lock b/.gradle/2.10/taskArtifacts/cache.properties.lock index 7319ef38..6a58584b 100644 Binary files a/.gradle/2.10/taskArtifacts/cache.properties.lock and b/.gradle/2.10/taskArtifacts/cache.properties.lock differ diff --git a/.gradle/2.10/taskArtifacts/fileHashes.bin b/.gradle/2.10/taskArtifacts/fileHashes.bin index 847b73d1..4a2288e6 100644 Binary files a/.gradle/2.10/taskArtifacts/fileHashes.bin and b/.gradle/2.10/taskArtifacts/fileHashes.bin differ diff --git a/.gradle/2.10/taskArtifacts/fileSnapshots.bin b/.gradle/2.10/taskArtifacts/fileSnapshots.bin index 353e011c..11e5544c 100644 Binary files a/.gradle/2.10/taskArtifacts/fileSnapshots.bin and b/.gradle/2.10/taskArtifacts/fileSnapshots.bin differ diff --git a/.gradle/2.10/taskArtifacts/outputFileStates.bin b/.gradle/2.10/taskArtifacts/outputFileStates.bin index 40b3d78e..7e677d7b 100644 Binary files a/.gradle/2.10/taskArtifacts/outputFileStates.bin and b/.gradle/2.10/taskArtifacts/outputFileStates.bin differ diff --git a/.gradle/2.10/taskArtifacts/taskArtifacts.bin b/.gradle/2.10/taskArtifacts/taskArtifacts.bin index 7bb5790b..9895e539 100644 Binary files a/.gradle/2.10/taskArtifacts/taskArtifacts.bin and b/.gradle/2.10/taskArtifacts/taskArtifacts.bin differ diff --git a/.idea/codeStyleSettings.xml b/.idea/codeStyleSettings.xml new file mode 100644 index 00000000..7c259691 --- /dev/null +++ b/.idea/codeStyleSettings.xml @@ -0,0 +1,229 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index fbb68289..5d199810 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -37,7 +37,7 @@ - + diff --git a/.idea/modules.xml b/.idea/modules.xml index 8700295b..0c35bf99 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -3,6 +3,7 @@ + diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2f14585f..8633c76b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -13,7 +13,7 @@ android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> - + + + + 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 ee122586..728b6215 100644 --- a/app/src/main/java/com/arialyy/simple/activity/MainActivity.java +++ b/app/src/main/java/com/arialyy/simple/activity/MainActivity.java @@ -1,90 +1,38 @@ 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 com.arialyy.downloadutil.core.DownloadManager; -import com.arialyy.downloadutil.entity.DownloadEntity; -import com.arialyy.frame.util.show.L; +import android.support.v7.widget.Toolbar; +import android.view.View; +import butterknife.Bind; import com.arialyy.simple.R; -import com.arialyy.simple.adapter.DownloadAdapter; import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.databinding.ActivityMainBinding; -import com.arialyy.simple.module.DownloadModule; - -import butterknife.Bind; /** - * Created by Lyy on 2016/9/27. + * Created by Lyy on 2016/10/13. */ public class MainActivity extends BaseActivity { - @Bind(R.id.list) RecyclerView mList; - DownloadAdapter mAdapter; - - @Override protected int setLayoutId() { - return R.layout.activity_main; - } - - @Override protected void init(Bundle savedInstanceState) { - super.init(savedInstanceState); - mAdapter = new DownloadAdapter(this, getModule(DownloadModule.class).getDownloadData()); - mList.setLayoutManager(new LinearLayoutManager(this)); - mList.setAdapter(mAdapter); - } - - - private BroadcastReceiver mReceiver = new BroadcastReceiver() { - long len = 0; - - @Override public void onReceive(Context context, Intent intent) { - String action = intent.getAction(); - DownloadEntity entity = intent.getParcelableExtra(DownloadManager.ENTITY); - switch (action) { - case DownloadManager.ACTION_PRE: - len = entity.getFileSize(); - L.d(TAG, "download pre"); - break; - case DownloadManager.ACTION_START: - L.d(TAG, "download start"); - break; - case DownloadManager.ACTION_RESUME: - L.d(TAG, "download resume"); - long location = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 1); - mAdapter.updateState(entity); - break; - case DownloadManager.ACTION_RUNNING: - long current = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 0); - mAdapter.setProgress(entity.getDownloadUrl(), current); - break; - case DownloadManager.ACTION_STOP: - L.d(TAG, "download stop"); - mAdapter.updateState(entity); - break; - case DownloadManager.ACTION_COMPLETE: - L.d(TAG, "download complete"); - mAdapter.updateState(entity); - break; - case DownloadManager.ACTION_CANCEL: - L.d(TAG, "download cancel"); - break; - case DownloadManager.ACTION_FAIL: - L.d(TAG, "download fail"); - break; - } - } - }; - - @Override protected void onResume() { - super.onResume(); - registerReceiver(mReceiver, getModule(DownloadModule.class).getDownloadFilter()); - } - - @Override protected void onDestroy() { - super.onDestroy(); - unregisterReceiver(mReceiver); + @Bind(R.id.toolbar) Toolbar mBar; + + @Override protected int setLayoutId() { + return R.layout.activity_main; + } + + @Override protected void init(Bundle savedInstanceState) { + super.init(savedInstanceState); + setSupportActionBar(mBar); + mBar.setTitle("多线程多任务下载"); + } + + public void onClick(View view) { + switch (view.getId()) { + case R.id.single_task: + startActivity(new Intent(this, SingleTaskActivity.class)); + break; + case R.id.multi_task: + startActivity(new Intent(this, MultiTaskActivity.class)); + break; } + } } diff --git a/app/src/main/java/com/arialyy/simple/activity/MultiTaskActivity.java b/app/src/main/java/com/arialyy/simple/activity/MultiTaskActivity.java new file mode 100644 index 00000000..4e23b3dd --- /dev/null +++ b/app/src/main/java/com/arialyy/simple/activity/MultiTaskActivity.java @@ -0,0 +1,94 @@ +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 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; +import com.arialyy.simple.base.BaseActivity; +import com.arialyy.simple.databinding.ActivityMultiBinding; +import com.arialyy.simple.module.DownloadModule; + +import butterknife.Bind; + +/** + * Created by Lyy on 2016/9/27. + */ +public class MultiTaskActivity extends BaseActivity { + @Bind(R.id.list) RecyclerView mList; + @Bind(R.id.toolbar) Toolbar mBar; + DownloadAdapter mAdapter; + + @Override protected int setLayoutId() { + return R.layout.activity_multi; + } + + @Override protected void init(Bundle savedInstanceState) { + super.init(savedInstanceState); + setSupportActionBar(mBar); + mBar.setTitle("多任务下载"); + mAdapter = new DownloadAdapter(this, getModule(DownloadModule.class).getDownloadData()); + mList.setLayoutManager(new LinearLayoutManager(this)); + mList.setAdapter(mAdapter); + } + + + private BroadcastReceiver mReceiver = new BroadcastReceiver() { + long len = 0; + + @Override public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + DownloadEntity entity = intent.getParcelableExtra(DownloadManager.ENTITY); + switch (action) { + case DownloadManager.ACTION_PRE: + len = entity.getFileSize(); + L.d(TAG, "download pre"); + break; + case DownloadManager.ACTION_START: + L.d(TAG, "download start"); + break; + case DownloadManager.ACTION_RESUME: + L.d(TAG, "download resume"); + long location = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 1); + mAdapter.updateState(entity); + break; + case DownloadManager.ACTION_RUNNING: + long current = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 0); + mAdapter.setProgress(entity.getDownloadUrl(), current); + break; + case DownloadManager.ACTION_STOP: + L.d(TAG, "download stop"); + mAdapter.updateState(entity); + break; + case DownloadManager.ACTION_COMPLETE: + L.d(TAG, "download complete"); + mAdapter.updateState(entity); + break; + case DownloadManager.ACTION_CANCEL: + L.d(TAG, "download cancel"); + break; + case DownloadManager.ACTION_FAIL: + L.d(TAG, "download fail"); + break; + } + } + }; + + @Override protected void onResume() { + super.onResume(); + registerReceiver(mReceiver, getModule(DownloadModule.class).getDownloadFilter()); + } + + @Override protected void onDestroy() { + super.onDestroy(); + unregisterReceiver(mReceiver); + } +} diff --git a/app/src/main/java/com/arialyy/simple/activity/SimpleTestActivity.java b/app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java similarity index 94% rename from app/src/main/java/com/arialyy/simple/activity/SimpleTestActivity.java rename to app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java index 5844b0c2..8cfe5736 100644 --- a/app/src/main/java/com/arialyy/simple/activity/SimpleTestActivity.java +++ b/app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java @@ -23,7 +23,7 @@ import com.arialyy.downloadutil.util.Util; import com.arialyy.frame.util.show.L; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; -import com.arialyy.simple.databinding.ActivitySimpleBinding; +import com.arialyy.simple.databinding.ActivitySingleBinding; import com.arialyy.simple.module.DownloadModule; import java.util.ArrayList; @@ -31,7 +31,7 @@ import java.util.List; import butterknife.Bind; -public class SimpleTestActivity extends BaseActivity { +public class SingleTaskActivity extends BaseActivity { private static final int DOWNLOAD_PRE = 0x01; private static final int DOWNLOAD_STOP = 0x02; private static final int DOWNLOAD_FAILE = 0x03; @@ -56,28 +56,28 @@ public class SimpleTestActivity extends BaseActivity { setBtState(false); break; case DOWNLOAD_FAILE: - Toast.makeText(SimpleTestActivity.this, "下载失败", Toast.LENGTH_SHORT).show(); + Toast.makeText(SingleTaskActivity.this, "下载失败", Toast.LENGTH_SHORT).show(); setBtState(true); break; case DOWNLOAD_STOP: - Toast.makeText(SimpleTestActivity.this, "暂停下载", Toast.LENGTH_SHORT).show(); + Toast.makeText(SingleTaskActivity.this, "暂停下载", Toast.LENGTH_SHORT).show(); mStart.setText("恢复"); setBtState(true); break; case DOWNLOAD_CANCEL: mPb.setProgress(0); - Toast.makeText(SimpleTestActivity.this, "取消下载", Toast.LENGTH_SHORT).show(); + Toast.makeText(SingleTaskActivity.this, "取消下载", Toast.LENGTH_SHORT).show(); mStart.setText("开始"); setBtState(true); break; case DOWNLOAD_RESUME: - Toast.makeText(SimpleTestActivity.this, + Toast.makeText(SingleTaskActivity.this, "恢复下载,恢复位置 ==> " + Util.formatFileSize((Long) msg.obj), Toast.LENGTH_SHORT).show(); setBtState(false); break; case DOWNLOAD_COMPLETE: - Toast.makeText(SimpleTestActivity.this, "下载完成", Toast.LENGTH_SHORT).show(); + Toast.makeText(SingleTaskActivity.this, "下载完成", Toast.LENGTH_SHORT).show(); mStart.setText("重新开始?"); mCancel.setEnabled(false); setBtState(true); @@ -157,14 +157,14 @@ public class SimpleTestActivity extends BaseActivity { } @Override protected int setLayoutId() { - return R.layout.activity_simple; + return R.layout.activity_single; } @Override protected void init(Bundle savedInstanceState) { super.init(savedInstanceState); setSupportActionBar(toolbar); + toolbar.setTitle("单任务下载"); init(); - } private void init() { diff --git a/app/src/main/java/com/arialyy/simple/module/DownloadModule.java b/app/src/main/java/com/arialyy/simple/module/DownloadModule.java index 26c37711..77a9386c 100644 --- a/app/src/main/java/com/arialyy/simple/module/DownloadModule.java +++ b/app/src/main/java/com/arialyy/simple/module/DownloadModule.java @@ -3,7 +3,6 @@ package com.arialyy.simple.module; 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.util.Util; @@ -11,7 +10,6 @@ import com.arialyy.frame.util.AndroidUtils; import com.arialyy.frame.util.StringUtil; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseModule; - import java.io.File; import java.util.ArrayList; import java.util.List; @@ -20,65 +18,94 @@ import java.util.List; * Created by Lyy on 2016/9/27. */ public class DownloadModule extends BaseModule { - public DownloadModule(Context context) { - super(context); - } + public DownloadModule(Context context) { + super(context); + } - /** - * 设置下载数据 - * - * @return - */ - public List getDownloadData() { - List list = DownloadEntity.findAllData(DownloadEntity.class); - if (list == null) { - list = createNewDownload(); - } - return list; + /** + * 设置下载数据 + */ + public List getDownloadData() { + List list = DownloadEntity.findAllData(DownloadEntity.class); + List newDownload = createNewDownload(); + if (list == null) { + list = newDownload; + } else { + list = filter(list, newDownload); } + return list; + } - private List createNewDownload() { - List list = new ArrayList<>(); - String[] urls = getContext().getResources() - .getStringArray(R.array.test_apk_download_url); - for (String url : urls) { - String fileName = Util.keyToHashCode(url) + ".apk"; - DownloadEntity entity = new DownloadEntity(); - entity.setDownloadUrl(url); - entity.setDownloadPath(getDownloadPath(url)); - entity.setFileName(fileName); - list.add(entity); + /** + * 过滤任务 + * + * @param sqlEntity 数据库的下载实体 + * @param createdEntity 通过下载链接生成的下载实体 + */ + private List filter(List sqlEntity, + List createdEntity) { + List list = new ArrayList<>(); + list.addAll(sqlEntity); + for (DownloadEntity cEntity : createdEntity) { + int count = 0; + for (DownloadEntity sEntity : sqlEntity) { + if (cEntity.getDownloadUrl().equals(sEntity.getDownloadUrl())) { + break; } - return list; + count++; + if (count == sqlEntity.size()) { + list.add(cEntity); + } + } } + return list; + } - /** - * 下载广播过滤器 - * - * @return - */ - public IntentFilter getDownloadFilter() { - IntentFilter filter = new IntentFilter(); - filter.addDataScheme(getContext().getPackageName()); - filter.addAction(DownloadManager.ACTION_PRE); - filter.addAction(DownloadManager.ACTION_RESUME); - filter.addAction(DownloadManager.ACTION_START); - filter.addAction(DownloadManager.ACTION_RUNNING); - filter.addAction(DownloadManager.ACTION_STOP); - filter.addAction(DownloadManager.ACTION_CANCEL); - filter.addAction(DownloadManager.ACTION_COMPLETE); - filter.addAction(DownloadManager.ACTION_FAIL); - return filter; + /** + * 创建下载列表 + */ + private List createNewDownload() { + List list = new ArrayList<>(); + String[] urls = getContext().getResources().getStringArray(R.array.test_apk_download_url); + for (String url : urls) { + String fileName = Util.keyToHashCode(url) + ".apk"; + DownloadEntity entity = new DownloadEntity(); + entity.setDownloadUrl(url); + entity.setDownloadPath(getDownloadPath(url)); + entity.setFileName(fileName); + list.add(entity); } + return list; + } - private String getDownloadPath(String url) { - String path = Environment.getExternalStorageDirectory() - .getPath() + "/" + AndroidUtils.getAppName(getContext()) + "downloads/" + StringUtil - .keyToHashKey(url) + ".apk"; - File file = new File(path); - if (!file.getParentFile().exists()) { - file.getParentFile().mkdirs(); - } - return path; + /** + * 下载广播过滤器 + */ + public IntentFilter getDownloadFilter() { + IntentFilter filter = new IntentFilter(); + filter.addDataScheme(getContext().getPackageName()); + filter.addAction(DownloadManager.ACTION_PRE); + filter.addAction(DownloadManager.ACTION_RESUME); + filter.addAction(DownloadManager.ACTION_START); + filter.addAction(DownloadManager.ACTION_RUNNING); + filter.addAction(DownloadManager.ACTION_STOP); + filter.addAction(DownloadManager.ACTION_CANCEL); + filter.addAction(DownloadManager.ACTION_COMPLETE); + filter.addAction(DownloadManager.ACTION_FAIL); + return filter; + } + + /** + * 设置下载队列 + */ + private String getDownloadPath(String url) { + String path = + Environment.getExternalStorageDirectory().getPath() + "/" + AndroidUtils.getAppName( + getContext()) + "downloads/" + StringUtil.keyToHashKey(url) + ".apk"; + File file = new File(path); + if (!file.getParentFile().exists()) { + file.getParentFile().mkdirs(); } + return path; + } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 245771f5..fb7c67ea 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,18 +1,32 @@ - + - + + + +