parent
ec7cfd3549
commit
6ac3648bba
@ -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<ActivityMultiBinding> { |
||||
@Bind(R.id.list) RecyclerView mList; |
||||
@Bind(R.id.toolbar) Toolbar mBar; |
||||
DownloadAdapter mAdapter; |
||||
List<DownloadEntity> 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"); |
||||
} |
||||
} |
||||
} |
@ -1,4 +1,4 @@ |
||||
package com.arialyy.simple.dialog; |
||||
package com.arialyy.simple.dialog_task; |
||||
|
||||
import android.content.Context; |
||||
import android.os.Environment; |
@ -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; |
@ -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<ActivityDownloadBinding> { |
||||
@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"); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,100 @@ |
||||
package com.arialyy.simple.multi_task; |
||||
|
||||
import android.content.Context; |
||||
import android.util.Log; |
||||
import android.view.View; |
||||
import android.widget.Button; |
||||
import android.widget.TextView; |
||||
import android.widget.Toast; |
||||
import butterknife.Bind; |
||||
import com.arialyy.absadapter.common.AbsHolder; |
||||
import com.arialyy.absadapter.recycler_view.AbsRVAdapter; |
||||
import com.arialyy.aria.core.Aria; |
||||
import com.arialyy.simple.R; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
import java.util.concurrent.ConcurrentHashMap; |
||||
|
||||
/** |
||||
* Created by AriaL on 2017/1/6. |
||||
*/ |
||||
final class FileListAdapter extends AbsRVAdapter<FileListEntity, FileListAdapter.FileListHolder> { |
||||
|
||||
//SparseBooleanArray mBtStates = new SparseBooleanArray();
|
||||
Map<String, Boolean> mBtStates = new ConcurrentHashMap<>(); |
||||
private Map<String, Integer> mPositions = new ConcurrentHashMap<>(); |
||||
|
||||
public FileListAdapter(Context context, List<FileListEntity> 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<String> 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<String> 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); |
||||
} |
||||
} |
||||
} |
@ -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; |
||||
} |
@ -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<ActivityMultiBinding> { |
||||
@Bind(R.id.list) RecyclerView mList; |
||||
@Bind(R.id.toolbar) Toolbar mBar; |
||||
private FileListAdapter mAdapter; |
||||
List<FileListEntity> 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); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,19 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"> |
||||
|
||||
<RelativeLayout |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:id="@+id/activity_main" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
tools:context="com.example.arial.test.MainActivity" |
||||
> |
||||
|
||||
<android.support.v7.widget.RecyclerView |
||||
android:id="@+id/list" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
/> |
||||
|
||||
</RelativeLayout> |
||||
</layout> |
@ -0,0 +1,40 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:layout_margin="16dp" |
||||
android:orientation="vertical" |
||||
> |
||||
|
||||
<TextView |
||||
android:id="@+id/name" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:text="name" |
||||
/> |
||||
|
||||
<TextView |
||||
android:id="@+id/download_url" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_below="@+id/name" |
||||
android:layout_marginTop="10dp" |
||||
/> |
||||
|
||||
<TextView |
||||
android:id="@+id/download_path" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_below="@+id/download_url" |
||||
android:layout_marginTop="10dp" |
||||
/> |
||||
|
||||
<Button |
||||
android:id="@+id/bt" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_below="@+id/download_path" |
||||
android:text="点击下载" |
||||
/> |
||||
|
||||
</RelativeLayout> |
Before Width: | Height: | Size: 1.1 MiB |
After Width: | Height: | Size: 3.4 MiB |
Loading…
Reference in new issue