parent
9aa7df035a
commit
5ee2cba331
@ -0,0 +1,53 @@ |
|||||||
|
/* |
||||||
|
* 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.aria.core.command; |
||||||
|
|
||||||
|
import android.text.TextUtils; |
||||||
|
import com.arialyy.aria.core.inf.ITask; |
||||||
|
import com.arialyy.aria.core.inf.ITaskEntity; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by lyy on 2017/6/2. |
||||||
|
* 最高优先级命令,最高优先级命令有以下属性 |
||||||
|
* 1、在下载队列中,有且只有一个最高优先级任务 |
||||||
|
* 2、最高优先级任务会一直存在,直到用户手动暂停或任务完成 |
||||||
|
* 3、任务调度器不会暂停最高优先级任务 |
||||||
|
* 4、用户手动暂停或任务完成后,第二次重新执行该任务,该命令将失效 |
||||||
|
* 5、如果下载队列中已经满了,则会停止队尾的任务 |
||||||
|
* 6、把任务设置为最高优先级任务后,将自动执行任务,不需要重新调用start()启动任务 |
||||||
|
*/ |
||||||
|
final class HighestPriorityCmd<T extends ITaskEntity> extends AbsCmd<T> { |
||||||
|
/** |
||||||
|
* @param targetName 产生任务的对象名 |
||||||
|
*/ |
||||||
|
HighestPriorityCmd(String targetName, T entity) { |
||||||
|
super(targetName, entity); |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void executeCmd() { |
||||||
|
if (!canExeCmd) return; |
||||||
|
ITask task = mQueue.getTask(mEntity.getEntity()); |
||||||
|
if (task == null) { |
||||||
|
task = mQueue.createTask(mTargetName, mEntity); |
||||||
|
} |
||||||
|
if (task != null) { |
||||||
|
if (!TextUtils.isEmpty(mTargetName)) { |
||||||
|
task.setTargetName(mTargetName); |
||||||
|
} |
||||||
|
task.setHighestPriority(true); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,169 @@ |
|||||||
|
package com.arialyy.simple.download; |
||||||
|
|
||||||
|
import android.os.Bundle; |
||||||
|
import android.os.Environment; |
||||||
|
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 android.widget.TextView; |
||||||
|
import butterknife.Bind; |
||||||
|
import com.arialyy.aria.core.Aria; |
||||||
|
import com.arialyy.aria.core.download.DownloadTarget; |
||||||
|
import com.arialyy.aria.core.download.DownloadTask; |
||||||
|
import com.arialyy.frame.util.show.L; |
||||||
|
import com.arialyy.simple.R; |
||||||
|
import com.arialyy.simple.base.BaseActivity; |
||||||
|
import com.arialyy.simple.databinding.ActivityHighestPriorityBinding; |
||||||
|
import com.arialyy.simple.download.multi_download.DownloadAdapter; |
||||||
|
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by lyy on 2017/6/2. |
||||||
|
* 最高优先级任务Demo |
||||||
|
*/ |
||||||
|
public class HighestPriorityActivity extends BaseActivity<ActivityHighestPriorityBinding> { |
||||||
|
@Bind(R.id.progressBar) HorizontalProgressBarWithNumber 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; |
||||||
|
@Bind(R.id.speed) TextView mSpeed; |
||||||
|
@Bind(R.id.list) RecyclerView mList; |
||||||
|
|
||||||
|
private String mTaskName = "狂野飙车8"; |
||||||
|
private static final String DOWNLOAD_URL = |
||||||
|
"http://static.gaoshouyou.com/d/82/ff/df82ed0af4ff4c1746cb191cf765aa8f.apk"; |
||||||
|
private DownloadAdapter mAdapter; |
||||||
|
|
||||||
|
@Override protected int setLayoutId() { |
||||||
|
return R.layout.activity_highest_priority; |
||||||
|
} |
||||||
|
|
||||||
|
@Override protected void init(Bundle savedInstanceState) { |
||||||
|
super.init(savedInstanceState); |
||||||
|
setSupportActionBar(toolbar); |
||||||
|
toolbar.setTitle("最高优先级任务演示"); |
||||||
|
getBinding().setTaskName("任务名:" + mTaskName + " (该任务是最高优先级任务)"); |
||||||
|
initWidget(); |
||||||
|
} |
||||||
|
|
||||||
|
private void initWidget() { |
||||||
|
if (Aria.download(this).taskExists(DOWNLOAD_URL)) { |
||||||
|
DownloadTarget target = Aria.download(this).load(DOWNLOAD_URL); |
||||||
|
int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize()); |
||||||
|
mPb.setProgress(p); |
||||||
|
} |
||||||
|
mAdapter = new DownloadAdapter(this, getModule(DownloadModule.class).getDownloadTaskList()); |
||||||
|
mList.setLayoutManager(new LinearLayoutManager(this)); |
||||||
|
mList.setAdapter(mAdapter); |
||||||
|
} |
||||||
|
|
||||||
|
@Override protected void onResume() { |
||||||
|
super.onResume(); |
||||||
|
Aria.download(this).addSchedulerListener(new MySchedulerListener()); |
||||||
|
} |
||||||
|
|
||||||
|
public void onClick(View view) { |
||||||
|
switch (view.getId()) { |
||||||
|
case R.id.start: |
||||||
|
String text = ((TextView) view).getText().toString(); |
||||||
|
if (text.equals("重新开始?") || text.equals("开始")) { |
||||||
|
Aria.download(this) |
||||||
|
.load(DOWNLOAD_URL) |
||||||
|
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() |
||||||
|
+ "/Download/" |
||||||
|
+ mTaskName |
||||||
|
+ ".apk") |
||||||
|
.setHighestPriority(); |
||||||
|
} else if (text.equals("恢复")) { |
||||||
|
Aria.download(this).load(DOWNLOAD_URL).resume(); |
||||||
|
} |
||||||
|
break; |
||||||
|
case R.id.stop: |
||||||
|
Aria.download(this).load(DOWNLOAD_URL).pause(); |
||||||
|
break; |
||||||
|
case R.id.cancel: |
||||||
|
Aria.download(this).load(DOWNLOAD_URL).cancel(); |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 设置start 和 stop 按钮状态 |
||||||
|
*/ |
||||||
|
private void setBtState(boolean state) { |
||||||
|
mStart.setEnabled(state); |
||||||
|
mStop.setEnabled(!state); |
||||||
|
} |
||||||
|
|
||||||
|
private class MySchedulerListener extends Aria.DownloadSchedulerListener { |
||||||
|
|
||||||
|
@Override public void onTaskPre(DownloadTask task) { |
||||||
|
super.onTaskPre(task); |
||||||
|
if (task.getKey().equals(DOWNLOAD_URL)) { |
||||||
|
mSize.setText(task.getConvertFileSize()); |
||||||
|
} else { |
||||||
|
mAdapter.updateState(task.getDownloadEntity()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onTaskStart(DownloadTask task) { |
||||||
|
if (task.getKey().equals(DOWNLOAD_URL)) { |
||||||
|
setBtState(false); |
||||||
|
} else { |
||||||
|
mAdapter.updateState(task.getDownloadEntity()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onTaskResume(DownloadTask task) { |
||||||
|
if (task.getKey().equals(DOWNLOAD_URL)) { |
||||||
|
setBtState(false); |
||||||
|
} else { |
||||||
|
mAdapter.updateState(task.getDownloadEntity()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onTaskStop(DownloadTask task) { |
||||||
|
if (task.getKey().equals(DOWNLOAD_URL)) { |
||||||
|
setBtState(true); |
||||||
|
} else { |
||||||
|
mAdapter.updateState(task.getDownloadEntity()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onTaskCancel(DownloadTask task) { |
||||||
|
if (task.getKey().equals(DOWNLOAD_URL)) { |
||||||
|
setBtState(true); |
||||||
|
} else { |
||||||
|
mAdapter.updateState(task.getDownloadEntity()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onTaskFail(DownloadTask task) { |
||||||
|
if (task.getKey().equals(DOWNLOAD_URL)) { |
||||||
|
setBtState(true); |
||||||
|
} else { |
||||||
|
L.d(TAG, "download fail【" + task.getKey() + "】"); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onTaskComplete(DownloadTask task) { |
||||||
|
if (task.getKey().equals(DOWNLOAD_URL)) { |
||||||
|
setBtState(true); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override public void onTaskRunning(DownloadTask task) { |
||||||
|
if (task.getKey().equals(DOWNLOAD_URL)) { |
||||||
|
setBtState(true); |
||||||
|
mPb.setProgress(task.getPercent()); |
||||||
|
mSpeed.setText(task.getConvertSpeed()); |
||||||
|
} else { |
||||||
|
mAdapter.setProgress(task.getDownloadEntity()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,47 @@ |
|||||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||||
|
<layout xmlns:android="http://schemas.android.com/apk/res/android"> |
||||||
|
<data> |
||||||
|
<variable |
||||||
|
name="taskName" |
||||||
|
type="String" |
||||||
|
/> |
||||||
|
</data> |
||||||
|
|
||||||
|
<RelativeLayout |
||||||
|
android:layout_width="match_parent" |
||||||
|
android:layout_height="match_parent" |
||||||
|
android:orientation="vertical" |
||||||
|
> |
||||||
|
|
||||||
|
<include layout="@layout/layout_bar"/> |
||||||
|
|
||||||
|
<TextView |
||||||
|
android:id="@+id/name" |
||||||
|
android:layout_width="wrap_content" |
||||||
|
android:layout_height="wrap_content" |
||||||
|
android:layout_below="@+id/toolbar" |
||||||
|
android:layout_marginLeft="16dp" |
||||||
|
android:layout_marginTop="16dp" |
||||||
|
android:text="@{taskName}" |
||||||
|
android:textColor="@android:color/black" |
||||||
|
android:textSize="16sp" |
||||||
|
/> |
||||||
|
|
||||||
|
<include |
||||||
|
layout="@layout/content_single" |
||||||
|
android:id="@+id/task" |
||||||
|
android:layout_width="match_parent" |
||||||
|
android:layout_height="wrap_content" |
||||||
|
android:layout_below="@+id/name" |
||||||
|
/> |
||||||
|
|
||||||
|
<android.support.v7.widget.RecyclerView |
||||||
|
android:id="@+id/list" |
||||||
|
android:layout_width="match_parent" |
||||||
|
android:layout_height="wrap_content" |
||||||
|
android:layout_below="@+id/task" |
||||||
|
android:layout_margin="16dp" |
||||||
|
/> |
||||||
|
|
||||||
|
</RelativeLayout> |
||||||
|
</layout> |
Loading…
Reference in new issue