完善下载
This commit is contained in:
@ -5,6 +5,7 @@
|
||||
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
|
||||
|
||||
<application
|
||||
android:name=".base.BaseApplication"
|
||||
android:allowBackup="true"
|
||||
@ -12,8 +13,9 @@
|
||||
android:label="@string/app_name"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme">
|
||||
<!--android:name=".activity.SimpleTestActivity"-->
|
||||
<activity
|
||||
android:name=".activity.SimpleTestActivity"
|
||||
android:name=".activity.MainActivity"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/AppTheme.NoActionBar">
|
||||
<intent-filter>
|
||||
|
@ -1,9 +1,15 @@
|
||||
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 com.arialyy.simple.R;
|
||||
import com.arialyy.simple.adapter.DownloadAdapter;
|
||||
import com.arialyy.simple.base.BaseActivity;
|
||||
@ -29,4 +35,56 @@ public class MainActivity extends BaseActivity<ActivityMainBinding> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -103,7 +103,7 @@ public class SimpleTestActivity extends BaseActivity<ActivitySimpleBinding> {
|
||||
String action = intent.getAction();
|
||||
switch (action) {
|
||||
case DownloadManager.ACTION_PRE:
|
||||
DownloadEntity entity = intent.getParcelableExtra(DownloadManager.ACTION_PRE);
|
||||
DownloadEntity entity = intent.getParcelableExtra(DownloadManager.ENTITY);
|
||||
len = entity.getFileSize();
|
||||
L.d(TAG, "download pre");
|
||||
mUpdateHandler.obtainMessage(DOWNLOAD_PRE, len).sendToTarget();
|
||||
@ -117,7 +117,7 @@ public class SimpleTestActivity extends BaseActivity<ActivitySimpleBinding> {
|
||||
mUpdateHandler.obtainMessage(DOWNLOAD_RESUME, location).sendToTarget();
|
||||
break;
|
||||
case DownloadManager.ACTION_RUNNING:
|
||||
long current = intent.getLongExtra(DownloadManager.ACTION_RUNNING, 0);
|
||||
long current = intent.getLongExtra(DownloadManager.CURRENT_LOCATION, 0);
|
||||
if (len == 0) {
|
||||
mPb.setProgress(0);
|
||||
} else {
|
||||
|
@ -1,16 +1,21 @@
|
||||
package com.arialyy.simple.adapter;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.SparseArray;
|
||||
import android.util.SparseIntArray;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
|
||||
import com.arialyy.absadapter.common.AbsHolder;
|
||||
import com.arialyy.absadapter.recycler_view.AbsRVAdapter;
|
||||
import com.arialyy.downloadutil.core.DownloadManager;
|
||||
import com.arialyy.downloadutil.core.command.CommandFactory;
|
||||
import com.arialyy.downloadutil.core.command.IDownloadCommand;
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
import com.arialyy.downloadutil.util.Util;
|
||||
import com.arialyy.simple.R;
|
||||
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -22,18 +27,22 @@ import butterknife.Bind;
|
||||
* 下载列表适配器
|
||||
*/
|
||||
public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapter.MyHolder> {
|
||||
private static final String TAG = "DownloadAdapter";
|
||||
private Map<String, Long> mProgress = new HashMap<>();
|
||||
private SparseArray<String> mPositions = new SparseArray<>();
|
||||
private static final String TAG = "DownloadAdapter";
|
||||
private DownloadManager mManager;
|
||||
private CommandFactory mFactory;
|
||||
private Map<String, Long> mProgress = new HashMap<>();
|
||||
private SparseIntArray mPositions = new SparseIntArray();
|
||||
|
||||
public DownloadAdapter(Context context, List<DownloadEntity> data) {
|
||||
super(context, data);
|
||||
int i = 0;
|
||||
for (DownloadEntity entity : data) {
|
||||
mProgress.put(entity.getDownloadUrl(), entity.getCurrentProgress());
|
||||
mPositions.append(i, entity.getDownloadUrl());
|
||||
mPositions.append(i, Util.keyToHashCode(entity.getDownloadUrl()));
|
||||
i++;
|
||||
}
|
||||
mFactory = CommandFactory.getInstance();
|
||||
mManager = DownloadManager.getInstance();
|
||||
}
|
||||
|
||||
@Override protected MyHolder getViewHolder(View convertView, int viewType) {
|
||||
@ -44,13 +53,17 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
|
||||
return R.layout.item_download;
|
||||
}
|
||||
|
||||
public synchronized void updateState(DownloadEntity entity) {
|
||||
notifyItemChanged(indexItem(entity.getDownloadUrl()));
|
||||
}
|
||||
|
||||
public synchronized void setProgress(String url, long currentPosition) {
|
||||
mProgress.put(url, currentPosition);
|
||||
notifyItemChanged(indexItem(url));
|
||||
}
|
||||
|
||||
private int indexItem(String url) {
|
||||
return mPositions.indexOfValue(url);
|
||||
return mPositions.indexOfValue(Util.keyToHashCode(url));
|
||||
}
|
||||
|
||||
@Override protected void bindData(MyHolder holder, int position, DownloadEntity item) {
|
||||
@ -62,13 +75,80 @@ public class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapte
|
||||
}
|
||||
current = (int) (mProgress.get(item.getDownloadUrl()) * 100 / item.getFileSize());
|
||||
holder.progress.setProgress(current);
|
||||
BtClickListener listener = (BtClickListener) holder.bt.getTag(holder.bt.getId());
|
||||
if (listener == null) {
|
||||
listener = new BtClickListener(item);
|
||||
holder.bt.setTag(holder.bt.getId(), listener);
|
||||
}
|
||||
holder.bt.setOnClickListener(listener);
|
||||
String str = "";
|
||||
switch (item.getState()) {
|
||||
case DownloadEntity.STATE_WAIT:
|
||||
case DownloadEntity.STATE_OTHER:
|
||||
case DownloadEntity.STATE_FAIL:
|
||||
str = "开始";
|
||||
break;
|
||||
case DownloadEntity.STATE_STOP:
|
||||
str = "恢复";
|
||||
break;
|
||||
case DownloadEntity.STATE_DOWNLOAD_ING:
|
||||
str = "暂停";
|
||||
break;
|
||||
}
|
||||
holder.bt.setText(str);
|
||||
}
|
||||
|
||||
private class BtClickListener implements View.OnClickListener {
|
||||
private DownloadEntity entity;
|
||||
|
||||
BtClickListener(DownloadEntity entity) {
|
||||
this.entity = entity;
|
||||
}
|
||||
|
||||
@Override public void onClick(View v) {
|
||||
switch (entity.getState()) {
|
||||
case DownloadEntity.STATE_WAIT:
|
||||
case DownloadEntity.STATE_OTHER:
|
||||
case DownloadEntity.STATE_FAIL:
|
||||
case DownloadEntity.STATE_STOP:
|
||||
start(entity);
|
||||
break;
|
||||
case DownloadEntity.STATE_DOWNLOAD_ING:
|
||||
stop(entity);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void start(DownloadEntity entity) {
|
||||
|
||||
List<IDownloadCommand> commands = new ArrayList<>();
|
||||
IDownloadCommand addCommand = mFactory.createCommand(getContext(), entity,
|
||||
CommandFactory.TASK_CREATE);
|
||||
IDownloadCommand startCommand = mFactory.createCommand(getContext(), entity,
|
||||
CommandFactory.TASK_START);
|
||||
commands.add(addCommand);
|
||||
commands.add(startCommand);
|
||||
mManager.setCommands(commands).exe();
|
||||
}
|
||||
|
||||
private void stop(DownloadEntity entity) {
|
||||
IDownloadCommand stopCommand = mFactory.createCommand(getContext(), entity,
|
||||
CommandFactory.TASK_STOP);
|
||||
mManager.setCommand(stopCommand).exe();
|
||||
}
|
||||
|
||||
private void cancel(DownloadEntity entity) {
|
||||
IDownloadCommand cancelCommand = mFactory.createCommand(getContext(), entity,
|
||||
CommandFactory.TASK_CANCEL);
|
||||
mManager.setCommand(cancelCommand).exe();
|
||||
}
|
||||
}
|
||||
|
||||
class MyHolder extends AbsHolder {
|
||||
@Bind(R.id.progressBar) HorizontalProgressBarWithNumber progress;
|
||||
@Bind(R.id.bt) Button bt;
|
||||
|
||||
public MyHolder(View itemView) {
|
||||
MyHolder(View itemView) {
|
||||
super(itemView);
|
||||
}
|
||||
}
|
||||
|
@ -6,11 +6,13 @@ import android.os.Environment;
|
||||
|
||||
import com.arialyy.downloadutil.core.DownloadManager;
|
||||
import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
import com.arialyy.downloadutil.util.Util;
|
||||
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;
|
||||
|
||||
@ -24,14 +26,23 @@ public class DownloadModule extends BaseModule {
|
||||
|
||||
/**
|
||||
* 设置下载数据
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public List<DownloadEntity> getDownloadData() {
|
||||
List<DownloadEntity> list = DownloadEntity.findAllData(DownloadEntity.class);
|
||||
if (list == null) {
|
||||
list = createNewDownload();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private List<DownloadEntity> createNewDownload() {
|
||||
List<DownloadEntity> list = new ArrayList<>();
|
||||
String[] urls = getContext().getResources()
|
||||
.getStringArray(R.array.test_apk_download_url);
|
||||
for (String url : urls) {
|
||||
String fileName = StringUtil.keyToHashKey(url) + ".apk";
|
||||
String fileName = Util.keyToHashCode(url) + ".apk";
|
||||
DownloadEntity entity = new DownloadEntity();
|
||||
entity.setDownloadUrl(url);
|
||||
entity.setDownloadPath(getDownloadPath(url));
|
||||
@ -43,9 +54,10 @@ public class DownloadModule extends BaseModule {
|
||||
|
||||
/**
|
||||
* 下载广播过滤器
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public IntentFilter getDownloadFilter(){
|
||||
public IntentFilter getDownloadFilter() {
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addDataScheme(getContext().getPackageName());
|
||||
filter.addAction(DownloadManager.ACTION_PRE);
|
||||
@ -60,7 +72,13 @@ public class DownloadModule extends BaseModule {
|
||||
}
|
||||
|
||||
private String getDownloadPath(String url) {
|
||||
return Environment.getExternalStorageDirectory().getPath() + "/" + AndroidUtils.getAppName(
|
||||
getContext()) + "downloads/" + StringUtil.keyToHashKey(url) + ".apk";
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<layout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
|
@ -5,5 +5,8 @@
|
||||
<item name="windowNoTitle">true</item>
|
||||
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
|
||||
<item name="android:statusBarColor">@android:color/transparent</item>
|
||||
<item name="colorPrimary">@color/colorPrimary</item>
|
||||
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
|
||||
<item name="colorAccent">@color/colorAccent</item>
|
||||
</style>
|
||||
</resources>
|
||||
|
@ -4,9 +4,9 @@
|
||||
|
||||
<string-array name="test_apk_download_url">
|
||||
<item>http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk</item> <!--300M的文件-->
|
||||
<item>http://static.gaoshouyou.com/d/21/e8/61218d78d0e8b79df68dbc18dd484c97.apk</item>
|
||||
<item>http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk</item>
|
||||
<item>http://static.gaoshouyou.com/d/d4/4f/d6d48db3794fb9ecf47e83c346570881.apk</item>
|
||||
<!--<item>http://static.gaoshouyou.com/d/21/e8/61218d78d0e8b79df68dbc18dd484c97.apk</item>-->
|
||||
<!--<item>http://static.gaoshouyou.com/d/12/0d/7f120f50c80d2e7b8c4ba24ece4f9cdd.apk</item>-->
|
||||
<!--<item>http://static.gaoshouyou.com/d/d4/4f/d6d48db3794fb9ecf47e83c346570881.apk</item>-->
|
||||
<!--<item>http://static.gaoshouyou.com/d/18/cf/ba18113bc6cf56c1c5863e761e717003.apk</item>-->
|
||||
<!--<item>http://static.gaoshouyou.com/d/60/17/2460921367173ea7145f11194a6f2587.apk</item>-->
|
||||
<!--<item>http://static.gaoshouyou.com/d/32/e0/1a32123ecbe0ee010d35479df248f90f.apk</item>-->
|
||||
|
@ -11,6 +11,9 @@
|
||||
<style name="AppTheme.NoActionBar">
|
||||
<item name="windowActionBar">false</item>
|
||||
<item name="windowNoTitle">true</item>
|
||||
<item name="colorPrimary">@color/colorPrimary</item>
|
||||
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
|
||||
<item name="colorAccent">@color/colorAccent</item>
|
||||
</style>
|
||||
|
||||
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
|
||||
|
Reference in New Issue
Block a user