diff --git a/Aria/src/main/java/com/arialyy/aria/core/AMReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/AMReceiver.java index b52101e7..f6d3e804 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AMReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AMReceiver.java @@ -27,16 +27,14 @@ import com.arialyy.aria.util.CheckUtil; * AM 接收器 */ public class AMReceiver { - String targetName; + String targetName; OnSchedulerListener listener; - DownloadEntity entity; - Object obj; + Object obj; /** * {@link #load(String)},请使用该方法 */ @Deprecated public AMTarget load(DownloadEntity entity) { - this.entity = entity; return new AMTarget(entity, targetName); } @@ -45,9 +43,8 @@ public class AMReceiver { */ public AMTarget load(@NonNull String downloadUrl) { CheckUtil.checkDownloadUrl(downloadUrl); - if (entity == null) { - entity = DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl); - } + DownloadEntity entity = + DownloadEntity.findData(DownloadEntity.class, "downloadUrl=?", downloadUrl); if (entity == null) { entity = new DownloadEntity(); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/task/Task.java b/Aria/src/main/java/com/arialyy/aria/core/task/Task.java index fdccc9d2..c66b5f67 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/task/Task.java +++ b/Aria/src/main/java/com/arialyy/aria/core/task/Task.java @@ -67,17 +67,24 @@ public class Task { /** * 获取文件大小 */ - public long getFileSize(){ + public long getFileSize() { return mEntity.getFileSize(); } /** * 获取当前下载进度 */ - public long getCurrentProgress(){ + public long getCurrentProgress() { return mEntity.getCurrentProgress(); } + /** + * 获取当前下载任务的下载地址 + */ + public String getDownloadUrl() { + return mEntity.getDownloadUrl(); + } + /** * 开始下载 */ @@ -202,6 +209,7 @@ public class Task { public Task build() { Task task = new Task(context, downloadEntity, outHandler); task.setTargetName(targetName); + //Log.w(TAG, "downloadEntity hashcode ==> " + downloadEntity.hashCode()); downloadEntity.save(); return task; } diff --git a/README.md b/README.md index 36052019..97ca626b 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Aria,致力于让下载傻瓜化
## 下载 [![Download](https://api.bintray.com/packages/arialyy/maven/Aria/images/download.svg)](https://bintray.com/arialyy/maven/Aria/_latestVersion)
```java -compile 'com.arialyy.aria:Aria:2.3.6' +compile 'com.arialyy.aria:Aria:2.3.8' ``` @@ -30,7 +30,13 @@ compile 'com.arialyy.aria:Aria:2.3.6' *** ## 使用 -### 一、只需要以下参数,你便能很简单的使用Aria下载文件了 +### 一、添加权限 +```xml + + + +``` +### 二、只需要以下参数,你便能很简单的使用Aria下载文件了 ```java Aria.whit(this) .load(DOWNLOAD_URL) //下载地址,必填 @@ -38,14 +44,14 @@ compile 'com.arialyy.aria:Aria:2.3.6' .setDownloadName("test.apk") //文件名,必填 .start(); ``` -### 二、为了能接收到Aria传递的数据,你需要把你的Activity或fragment注册到Aria管理器中,注册的方式很简单,在onResume +### 三、为了能接收到Aria传递的数据,你需要把你的Activity或fragment注册到Aria管理器中,注册的方式很简单,在onResume ```java @Override protected void onResume() { super.onResume(); Aria.whit(this).addSchedulerListener(new MySchedulerListener()); } ``` -### 三、通过下载链接,你还能使用Aria执行很多操作,如: +### 四、通过下载链接,你还能使用Aria执行很多操作,如: - 添加任务(不进行下载) ```java @@ -85,7 +91,7 @@ compile 'com.arialyy.aria:Aria:2.3.6' Aria.whit(this).load(DOWNLOAD_URL).getFileSize(); ``` -### 四、关于Aria,你还需要知道的一些东西 +### 五、关于Aria,你还需要知道的一些东西 - 设置下载任务数,Aria默认下载任务为**2** ```java @@ -114,6 +120,7 @@ compile 'com.arialyy.aria:Aria:2.3.6' *** ## 开发日志 + + v_2.3.8 修复数据错乱的bug、添加fragment支持 + v_2.3.6 添加dialog、popupWindow支持 + v_2.3.3 - 添加断点支持 diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 99eed35e..681656f0 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -15,7 +15,7 @@ android:theme="@style/AppTheme.NoActionBar"> @@ -24,9 +24,10 @@ - - - + + + + diff --git a/app/src/main/java/com/arialyy/simple/activity/MainActivity.java b/app/src/main/java/com/arialyy/simple/MainActivity.java similarity index 91% rename from app/src/main/java/com/arialyy/simple/activity/MainActivity.java rename to app/src/main/java/com/arialyy/simple/MainActivity.java index 4a53c3cd..33a10ee8 100644 --- a/app/src/main/java/com/arialyy/simple/activity/MainActivity.java +++ b/app/src/main/java/com/arialyy/simple/MainActivity.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.arialyy.simple.activity; +package com.arialyy.simple; import android.Manifest; import android.content.Intent; @@ -28,11 +28,13 @@ import butterknife.Bind; import com.arialyy.frame.permission.OnPermissionCallback; import com.arialyy.frame.permission.PermissionManager; import com.arialyy.frame.util.show.T; -import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.databinding.ActivityMainBinding; -import com.arialyy.simple.dialog.DownloadDialog; -import com.arialyy.simple.pop.DownloadPopupWindow; +import com.arialyy.simple.dialog_task.DownloadDialog; +import com.arialyy.simple.fragment_task.FragmentActivity; +import com.arialyy.simple.multi_task.MultiTaskActivity; +import com.arialyy.simple.pop_task.DownloadPopupWindow; +import com.arialyy.simple.single_task.SingleTaskActivity; /** * Created by Lyy on 2016/10/13. diff --git a/app/src/main/java/com/arialyy/simple/activity/MultiTaskActivity.java b/app/src/main/java/com/arialyy/simple/activity/MultiTaskActivity.java deleted file mode 100644 index 6feba7a3..00000000 --- a/app/src/main/java/com/arialyy/simple/activity/MultiTaskActivity.java +++ /dev/null @@ -1,187 +0,0 @@ -/* - * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.arialyy.simple.activity; - -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 butterknife.Bind; -import com.arialyy.aria.core.AMTarget; -import com.arialyy.aria.core.Aria; -import com.arialyy.aria.core.DownloadEntity; -import com.arialyy.aria.core.task.Task; -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.dialog.DownloadNumDialog; -import com.arialyy.simple.module.DownloadModule; -import java.util.ArrayList; -import java.util.List; - -/** - * 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; - List mData = new ArrayList<>(); - - @Override protected int setLayoutId() { - return R.layout.activity_multi; - } - - @Override protected void init(Bundle savedInstanceState) { - super.init(savedInstanceState); - setSupportActionBar(mBar); - mBar.setTitle("多任务下载"); - mData.addAll(getModule(DownloadModule.class).getDownloadData()); - mAdapter = new DownloadAdapter(this, mData); - 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: - // L.d(TAG, "download pre"); - // mAdapter.updateState(entity); - // break; - // case DownloadManager.ACTION_POST_PRE: - // len = entity.getFileSize(); - // L.d(TAG, "download post 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); - // 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"); - // 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"); - // mAdapter.updateState(entity); - // break; - // case DownloadManager.ACTION_FAIL: - // L.d(TAG, "download fail"); - // break; - // } - // } - //}; - - public void onClick(View view) { - switch (view.getId()) { - case R.id.num: - DownloadNumDialog dialog = new DownloadNumDialog(this); - dialog.show(getSupportFragmentManager(), "download_num"); - break; - case R.id.stop_all: - Aria.get(this).stopAllTask(); - break; - case R.id.add_task: - mData.add(getModule(DownloadModule.class).createRandomDownloadEntity()); - mAdapter.notifyDataSetChanged(); - break; - } - } - - @Override protected void onResume() { - super.onResume(); - Aria.whit(this).addSchedulerListener(new MySchedulerListener()); - } - - @Override protected void onDestroy() { - super.onDestroy(); - //unregisterReceiver(mReceiver); - } - - @Override protected void dataCallback(int result, Object data) { - super.dataCallback(result, data); - if (result == DownloadNumDialog.RESULT_CODE) { - mAdapter.setDownloadNum(Integer.parseInt(data + "")); - } - } - - private class MySchedulerListener extends Aria.SimpleSchedulerListener { - @Override public void onTaskPre(Task task) { - super.onTaskPre(task); - L.d(TAG, "download pre"); - mAdapter.updateState(task.getDownloadEntity()); - } - - @Override public void onTaskStart(Task task) { - super.onTaskStart(task); - L.d(TAG, "download start"); - mAdapter.updateState(task.getDownloadEntity()); - } - - @Override public void onTaskResume(Task task) { - super.onTaskResume(task); - L.d(TAG, "download resume"); - mAdapter.updateState(task.getDownloadEntity()); - } - - @Override public void onTaskRunning(Task task) { - super.onTaskRunning(task); - mAdapter.setProgress(task.getDownloadEntity()); - } - - @Override public void onTaskStop(Task task) { - super.onTaskStop(task); - mAdapter.updateState(task.getDownloadEntity()); - } - - @Override public void onTaskCancel(Task task) { - super.onTaskCancel(task); - mAdapter.updateState(task.getDownloadEntity()); - } - - @Override public void onTaskComplete(Task task) { - super.onTaskComplete(task); - mAdapter.updateState(task.getDownloadEntity()); - } - - @Override public void onTaskFail(Task task) { - super.onTaskFail(task); - L.d(TAG, "download fail"); - } - } -} \ No newline at end of file diff --git a/app/src/main/java/com/arialyy/simple/dialog/DownloadDialog.java b/app/src/main/java/com/arialyy/simple/dialog_task/DownloadDialog.java similarity index 98% rename from app/src/main/java/com/arialyy/simple/dialog/DownloadDialog.java rename to app/src/main/java/com/arialyy/simple/dialog_task/DownloadDialog.java index f8e723fd..8f6db116 100644 --- a/app/src/main/java/com/arialyy/simple/dialog/DownloadDialog.java +++ b/app/src/main/java/com/arialyy/simple/dialog_task/DownloadDialog.java @@ -1,4 +1,4 @@ -package com.arialyy.simple.dialog; +package com.arialyy.simple.dialog_task; import android.content.Context; import android.os.Environment; diff --git a/app/src/main/java/com/arialyy/simple/fragment/DownloadFragment.java b/app/src/main/java/com/arialyy/simple/fragment_task/DownloadFragment.java similarity index 97% rename from app/src/main/java/com/arialyy/simple/fragment/DownloadFragment.java rename to app/src/main/java/com/arialyy/simple/fragment_task/DownloadFragment.java index 0a9f3b4e..19502277 100644 --- a/app/src/main/java/com/arialyy/simple/fragment/DownloadFragment.java +++ b/app/src/main/java/com/arialyy/simple/fragment_task/DownloadFragment.java @@ -1,4 +1,4 @@ -package com.arialyy.simple.fragment; +package com.arialyy.simple.fragment_task; import android.os.Bundle; import android.os.Environment; @@ -15,7 +15,6 @@ import com.arialyy.aria.util.CommonUtil; import com.arialyy.frame.core.AbsFragment; import com.arialyy.simple.R; import com.arialyy.simple.databinding.FragmentDownloadBinding; -import com.arialyy.simple.dialog.DownloadDialog; import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; /** diff --git a/app/src/main/java/com/arialyy/simple/activity/FragmentActivity.java b/app/src/main/java/com/arialyy/simple/fragment_task/FragmentActivity.java similarity index 89% rename from app/src/main/java/com/arialyy/simple/activity/FragmentActivity.java rename to app/src/main/java/com/arialyy/simple/fragment_task/FragmentActivity.java index 2d8a4f07..ff7a30cd 100644 --- a/app/src/main/java/com/arialyy/simple/activity/FragmentActivity.java +++ b/app/src/main/java/com/arialyy/simple/fragment_task/FragmentActivity.java @@ -1,4 +1,4 @@ -package com.arialyy.simple.activity; +package com.arialyy.simple.fragment_task; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; 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 7d383a1c..b9ac5582 100644 --- a/app/src/main/java/com/arialyy/simple/module/DownloadModule.java +++ b/app/src/main/java/com/arialyy/simple/module/DownloadModule.java @@ -24,13 +24,13 @@ import android.os.Environment; import android.os.Handler; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.DownloadEntity; -import com.arialyy.aria.core.DownloadManager; import com.arialyy.aria.util.CommonUtil; import com.arialyy.frame.util.AndroidUtils; import com.arialyy.frame.util.StringUtil; import com.arialyy.frame.util.show.L; import com.arialyy.simple.R; -import com.arialyy.simple.activity.SingleTaskActivity; +import com.arialyy.simple.multi_task.FileListEntity; +import com.arialyy.simple.single_task.SingleTaskActivity; import com.arialyy.simple.base.BaseModule; import java.io.File; import java.util.ArrayList; @@ -45,9 +45,9 @@ public class DownloadModule extends BaseModule { public DownloadModule(Context context) { super(context); - mTestDownloadUrl.add("static.gaoshouyou.com/d/e6/f5/4de6329f9cf5dc3a1d1e6bbcca0d003c.apk"); - mTestDownloadUrl.add("static.gaoshouyou.com/d/6e/e5/ff6ecaaf45e532e6d07747af82357472.apk"); - mTestDownloadUrl.add("static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk"); + mTestDownloadUrl.add("http://static.gaoshouyou.com/d/e6/f5/4de6329f9cf5dc3a1d1e6bbcca0d003c.apk"); + mTestDownloadUrl.add("http://static.gaoshouyou.com/d/6e/e5/ff6ecaaf45e532e6d07747af82357472.apk"); + mTestDownloadUrl.add("http://static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk"); } public String getRadomUrl() { @@ -60,6 +60,25 @@ public class DownloadModule extends BaseModule { return createDownloadEntity(getRadomUrl()); } + /** + * 创建下载地址 + */ + public List createFileList() { + String[] names = getContext().getResources().getStringArray(R.array.file_nams); + String[] downloadUrl = getContext().getResources().getStringArray(R.array.download_url); + List list = new ArrayList<>(); + int i = 0; + for (String name : names) { + FileListEntity entity = new FileListEntity(); + entity.name = name; + entity.downloadUrl = downloadUrl[i]; + entity.downloadPath = Environment.getExternalStorageDirectory() + "/Download/" + name; + list.add(entity); + i++; + } + return list; + } + /** * 设置下载数据 */ diff --git a/app/src/main/java/com/arialyy/simple/multi_task/DownloadActivity.java b/app/src/main/java/com/arialyy/simple/multi_task/DownloadActivity.java new file mode 100644 index 00000000..7e6652e4 --- /dev/null +++ b/app/src/main/java/com/arialyy/simple/multi_task/DownloadActivity.java @@ -0,0 +1,86 @@ +package com.arialyy.simple.multi_task; + +import android.os.Bundle; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import butterknife.Bind; +import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.task.Task; +import com.arialyy.frame.util.show.L; +import com.arialyy.simple.R; +import com.arialyy.simple.base.BaseActivity; +import com.arialyy.simple.databinding.ActivityDownloadBinding; + +/** + * Created by AriaL on 2017/1/6. + */ + +public class DownloadActivity extends BaseActivity { + @Bind(R.id.list) RecyclerView mList; + private DownloadAdapter mAdapter; + + @Override protected int setLayoutId() { + return R.layout.activity_download; + } + + @Override protected void init(Bundle savedInstanceState) { + super.init(savedInstanceState); + mAdapter = new DownloadAdapter(this, Aria.get(this).getDownloadList()); + mList.setLayoutManager(new LinearLayoutManager(this)); + mList.setAdapter(mAdapter); + } + + @Override protected void dataCallback(int result, Object data) { + + } + + @Override protected void onResume() { + super.onResume(); + Aria.whit(this).addSchedulerListener(new MySchedulerListener()); + } + + private class MySchedulerListener extends Aria.SimpleSchedulerListener { + @Override public void onTaskPre(Task task) { + super.onTaskPre(task); + L.d(TAG, "download pre"); + mAdapter.updateState(task.getDownloadEntity()); + } + + @Override public void onTaskStart(Task task) { + super.onTaskStart(task); + L.d(TAG, "download start"); + mAdapter.updateState(task.getDownloadEntity()); + } + + @Override public void onTaskResume(Task task) { + super.onTaskResume(task); + L.d(TAG, "download resume"); + mAdapter.updateState(task.getDownloadEntity()); + } + + @Override public void onTaskRunning(Task task) { + super.onTaskRunning(task); + mAdapter.setProgress(task.getDownloadEntity()); + } + + @Override public void onTaskStop(Task task) { + super.onTaskStop(task); + mAdapter.updateState(task.getDownloadEntity()); + } + + @Override public void onTaskCancel(Task task) { + super.onTaskCancel(task); + mAdapter.updateState(task.getDownloadEntity()); + } + + @Override public void onTaskComplete(Task task) { + super.onTaskComplete(task); + mAdapter.updateState(task.getDownloadEntity()); + } + + @Override public void onTaskFail(Task task) { + super.onTaskFail(task); + L.d(TAG, "download fail"); + } + } +} diff --git a/app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java b/app/src/main/java/com/arialyy/simple/multi_task/DownloadAdapter.java similarity index 78% rename from app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java rename to app/src/main/java/com/arialyy/simple/multi_task/DownloadAdapter.java index 02b26c9a..4ee18fde 100644 --- a/app/src/main/java/com/arialyy/simple/adapter/DownloadAdapter.java +++ b/app/src/main/java/com/arialyy/simple/multi_task/DownloadAdapter.java @@ -14,12 +14,10 @@ * limitations under the License. */ - -package com.arialyy.simple.adapter; +package com.arialyy.simple.multi_task; import android.content.Context; import android.content.res.Resources; -import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.TextView; @@ -40,16 +38,15 @@ import java.util.concurrent.ConcurrentHashMap; * Created by Lyy on 2016/9/27. * 下载列表适配器 */ -public class DownloadAdapter extends AbsRVAdapter { - private static final String TAG = "DownloadAdapter"; - private Map mPositions = new ConcurrentHashMap<>(); +final class DownloadAdapter extends AbsRVAdapter { + private static final String TAG = "DownloadAdapter"; + private Map mPositions = new ConcurrentHashMap<>(); - public DownloadAdapter(Context context, List data) { + DownloadAdapter(Context context, List data) { super(context, data); - int i = 0; + int i = 0; for (DownloadEntity entity : data) { mPositions.put(entity.getDownloadUrl(), i); - Aria.whit(getContext()).load(entity).add(); i++; } } @@ -73,7 +70,7 @@ public class DownloadAdapter extends AbsRVAdapter keys = mPositions.keySet(); - for (String key : keys){ - if (key.equals(url)){ + for (String key : keys) { + if (key.equals(url)) { return mPositions.get(key); } } @@ -103,16 +100,19 @@ public class DownloadAdapter extends AbsRVAdapter { + + //SparseBooleanArray mBtStates = new SparseBooleanArray(); + Map mBtStates = new ConcurrentHashMap<>(); + private Map mPositions = new ConcurrentHashMap<>(); + + public FileListAdapter(Context context, List data) { + super(context, data); + for (int i = 0, len = data.size(); i < len; i++) { + mBtStates.put(data.get(i).downloadUrl, true); + mPositions.put(data.get(i).downloadUrl, i); + } + } + + @Override protected FileListHolder getViewHolder(View convertView, int viewType) { + return new FileListHolder(convertView); + } + + @Override protected int setLayoutId(int type) { + return R.layout.item_file_list; + } + + @Override + protected void bindData(FileListHolder holder, int position, final FileListEntity item) { + holder.name.setText("文件名:" + item.name); + holder.url.setText("下载地址:" + item.downloadUrl); + holder.path.setText("保存路径:" + item.downloadPath); + if (mBtStates.get(item.downloadUrl)) { + holder.bt.setEnabled(true); + holder.bt.setOnClickListener(new View.OnClickListener() { + @Override public void onClick(View v) { + Toast.makeText(getContext(), "开始下载:" + item.name, Toast.LENGTH_SHORT).show(); + Aria.whit(getContext()) + .load(item.downloadUrl) + .setDownloadName(item.name) + .setDownloadPath(item.downloadPath) + .start(); + } + }); + } else { + holder.bt.setEnabled(false); + } + } + + public void updateBtState(String downloadUrl, boolean able) { + Set keys = mBtStates.keySet(); + for (String key : keys) { + if (key.equals(downloadUrl)) { + Log.d(TAG, "able ==> " + able); + mBtStates.put(downloadUrl, able); + notifyItemChanged(indexItem(downloadUrl)); + return; + } + } + } + + private synchronized int indexItem(String url) { + Set keys = mPositions.keySet(); + for (String key : keys) { + if (key.equals(url)) { + int index = mPositions.get(key); + Log.d(TAG, "index ==> " + index); + return index; + } + } + return -1; + } + + class FileListHolder extends AbsHolder { + @Bind(R.id.name) TextView name; + @Bind(R.id.download_url) TextView url; + @Bind(R.id.download_path) TextView path; + @Bind(R.id.bt) Button bt; + + FileListHolder(View itemView) { + super(itemView); + } + } +} diff --git a/app/src/main/java/com/arialyy/simple/multi_task/FileListEntity.java b/app/src/main/java/com/arialyy/simple/multi_task/FileListEntity.java new file mode 100644 index 00000000..15d584a8 --- /dev/null +++ b/app/src/main/java/com/arialyy/simple/multi_task/FileListEntity.java @@ -0,0 +1,9 @@ +package com.arialyy.simple.multi_task; + +/** + * Created by AriaL on 2017/1/6. + */ + +public class FileListEntity { + public String name, downloadUrl, downloadPath; +} diff --git a/app/src/main/java/com/arialyy/simple/multi_task/MultiTaskActivity.java b/app/src/main/java/com/arialyy/simple/multi_task/MultiTaskActivity.java new file mode 100644 index 00000000..c7546b24 --- /dev/null +++ b/app/src/main/java/com/arialyy/simple/multi_task/MultiTaskActivity.java @@ -0,0 +1,118 @@ +/* + * Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.arialyy.simple.multi_task; + +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 butterknife.Bind; +import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.DownloadEntity; +import com.arialyy.aria.core.task.Task; +import com.arialyy.frame.util.show.L; +import com.arialyy.simple.R; +import com.arialyy.simple.base.BaseActivity; +import com.arialyy.simple.databinding.ActivityMultiBinding; +import com.arialyy.simple.module.DownloadModule; +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Lyy on 2016/9/27. + */ +public class MultiTaskActivity extends BaseActivity { + @Bind(R.id.list) RecyclerView mList; + @Bind(R.id.toolbar) Toolbar mBar; + private FileListAdapter mAdapter; + List mData = new ArrayList<>(); + + @Override protected int setLayoutId() { + return R.layout.activity_multi; + } + + @Override protected void init(Bundle savedInstanceState) { + super.init(savedInstanceState); + setSupportActionBar(mBar); + mBar.setTitle("多任务下载"); + mData.addAll(getModule(DownloadModule.class).createFileList()); + mAdapter = new FileListAdapter(this, mData); + mList.setLayoutManager(new LinearLayoutManager(this)); + mList.setAdapter(mAdapter); + } + + public void onClick(View view) { + switch (view.getId()) { + case R.id.num: + DownloadNumDialog dialog = new DownloadNumDialog(this); + dialog.show(getSupportFragmentManager(), "download_num"); + break; + case R.id.stop_all: + Aria.get(this).stopAllTask(); + break; + case R.id.turn: + startActivity(new Intent(this, DownloadActivity.class)); + break; + } + } + + @Override protected void onResume() { + super.onResume(); + Aria.whit(this).addSchedulerListener(new DownloadListener()); + } + + @Override protected void onDestroy() { + super.onDestroy(); + //unregisterReceiver(mReceiver); + } + + @Override protected void dataCallback(int result, Object data) { + super.dataCallback(result, data); + if (result == DownloadNumDialog.RESULT_CODE) { + Aria.get(this).setMaxDownloadNum(Integer.parseInt(data + "")); + } + } + + private class DownloadListener extends Aria.SimpleSchedulerListener { + + @Override public void onTaskStart(Task task) { + super.onTaskStart(task); + mAdapter.updateBtState(task.getDownloadUrl(), false); + } + + @Override public void onTaskRunning(Task task) { + super.onTaskRunning(task); + } + + @Override public void onTaskResume(Task task) { + super.onTaskResume(task); + mAdapter.updateBtState(task.getDownloadUrl(), false); + } + + @Override public void onTaskStop(Task task) { + super.onTaskStop(task); + mAdapter.updateBtState(task.getDownloadUrl(), true); + } + + @Override public void onTaskComplete(Task task) { + super.onTaskComplete(task); + mAdapter.updateBtState(task.getDownloadUrl(), true); + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/arialyy/simple/pop/DownloadPopupWindow.java b/app/src/main/java/com/arialyy/simple/pop_task/DownloadPopupWindow.java similarity index 96% rename from app/src/main/java/com/arialyy/simple/pop/DownloadPopupWindow.java rename to app/src/main/java/com/arialyy/simple/pop_task/DownloadPopupWindow.java index 20692903..444d6767 100644 --- a/app/src/main/java/com/arialyy/simple/pop/DownloadPopupWindow.java +++ b/app/src/main/java/com/arialyy/simple/pop_task/DownloadPopupWindow.java @@ -1,8 +1,7 @@ -package com.arialyy.simple.pop; +package com.arialyy.simple.pop_task; import android.content.Context; import android.graphics.Color; -import android.graphics.drawable.ClipDrawable; import android.graphics.drawable.ColorDrawable; import android.os.Environment; import android.view.View; @@ -15,7 +14,6 @@ import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.DownloadEntity; import com.arialyy.aria.core.task.Task; import com.arialyy.aria.util.CommonUtil; -import com.arialyy.frame.core.AbsDialog; import com.arialyy.frame.core.AbsPopupWindow; import com.arialyy.simple.R; import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; diff --git a/app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/single_task/SingleTaskActivity.java similarity index 97% rename from app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java rename to app/src/main/java/com/arialyy/simple/single_task/SingleTaskActivity.java index 2446f2db..b66ae0b4 100644 --- a/app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/single_task/SingleTaskActivity.java @@ -14,13 +14,11 @@ * limitations under the License. */ -package com.arialyy.simple.activity; +package com.arialyy.simple.single_task; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Environment; import android.os.Handler; @@ -35,15 +33,12 @@ import butterknife.Bind; import com.arialyy.aria.core.AMTarget; import com.arialyy.aria.core.Aria; import com.arialyy.aria.core.DownloadEntity; -import com.arialyy.aria.core.DownloadManager; import com.arialyy.aria.core.task.Task; -import com.arialyy.aria.orm.DbEntity; import com.arialyy.aria.util.CommonUtil; import com.arialyy.frame.util.show.L; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.databinding.ActivitySingleBinding; -import com.arialyy.simple.module.DownloadModule; import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; public class SingleTaskActivity extends BaseActivity { diff --git a/app/src/main/res/layout/activity_download.xml b/app/src/main/res/layout/activity_download.xml new file mode 100644 index 00000000..8fa600fe --- /dev/null +++ b/app/src/main/res/layout/activity_download.xml @@ -0,0 +1,19 @@ + + + + + + + + + diff --git a/app/src/main/res/layout/activity_fragment.xml b/app/src/main/res/layout/activity_fragment.xml index 03bb007d..9359917a 100644 --- a/app/src/main/res/layout/activity_fragment.xml +++ b/app/src/main/res/layout/activity_fragment.xml @@ -8,7 +8,7 @@ > diff --git a/app/src/main/res/layout/activity_multi.xml b/app/src/main/res/layout/activity_multi.xml index ceaedec6..9cdb5761 100644 --- a/app/src/main/res/layout/activity_multi.xml +++ b/app/src/main/res/layout/activity_multi.xml @@ -15,8 +15,8 @@ android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" - android:layout_below="@+id/toolbar" android:layout_above="@+id/num" + android:layout_below="@+id/toolbar" />