parent
3496022c60
commit
2ad2f31594
@ -1,21 +1,66 @@ |
||||
//package com.arialyy.simple.module;
|
||||
//
|
||||
//import android.content.Context;
|
||||
//import com.arialyy.downloadutil.entity.DownloadEntity;
|
||||
//import com.arialyy.simple.base.BaseModule;
|
||||
//import java.util.ArrayList;
|
||||
//import java.util.List;
|
||||
//
|
||||
///**
|
||||
// * Created by Lyy on 2016/9/27.
|
||||
// */
|
||||
//public class DownloadModule extends BaseModule{
|
||||
// public DownloadModule(Context context) {
|
||||
// super(context);
|
||||
// }
|
||||
//
|
||||
// public List<DownloadEntity> getDownloadData(){
|
||||
// List<DownloadEntity> list = new ArrayList<>();
|
||||
// DownloadEntity entity
|
||||
// }
|
||||
//}
|
||||
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.frame.util.AndroidUtils; |
||||
import com.arialyy.frame.util.StringUtil; |
||||
import com.arialyy.simple.R; |
||||
import com.arialyy.simple.base.BaseModule; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* Created by Lyy on 2016/9/27. |
||||
*/ |
||||
public class DownloadModule extends BaseModule { |
||||
public DownloadModule(Context context) { |
||||
super(context); |
||||
} |
||||
|
||||
/** |
||||
* 设置下载数据 |
||||
* @return |
||||
*/ |
||||
public List<DownloadEntity> getDownloadData() { |
||||
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"; |
||||
DownloadEntity entity = new DownloadEntity(); |
||||
entity.setDownloadUrl(url); |
||||
entity.setDownloadPath(getDownloadPath(url)); |
||||
entity.setFileName(fileName); |
||||
list.add(entity); |
||||
} |
||||
return list; |
||||
} |
||||
|
||||
/** |
||||
* 下载广播过滤器 |
||||
* @return |
||||
*/ |
||||
public IntentFilter getDownloadFilter(){ |
||||
IntentFilter filter = new IntentFilter(); |
||||
filter.addCategory(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) { |
||||
return Environment.getExternalStorageDirectory().getPath() + "/" + AndroidUtils.getAppName( |
||||
getContext()) + "downloads/" + StringUtil.keyToHashKey(url) + ".apk"; |
||||
} |
||||
} |
||||
|
@ -1,16 +1,16 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<resources> |
||||
<declare-styleable name="HorizontalProgressBarWithNumber"> |
||||
<attr name="progress_unreached_color" format="color" /> |
||||
<attr name="progress_reached_color" format="color" /> |
||||
<attr name="progress_reached_bar_height" format="dimension" /> |
||||
<attr name="progress_unreached_bar_height" format="dimension" /> |
||||
<attr name="progress_text_size" format="dimension" /> |
||||
<attr name="progress_text_color" format="color" /> |
||||
<attr name="progress_text_offset" format="dimension" /> |
||||
<attr name="progress_unreached_color" format="color"/> |
||||
<attr name="progress_reached_color" format="color"/> |
||||
<attr name="progress_reached_bar_height" format="dimension"/> |
||||
<attr name="progress_unreached_bar_height" format="dimension"/> |
||||
<attr name="progress_text_size" format="dimension"/> |
||||
<attr name="progress_text_color" format="color"/> |
||||
<attr name="progress_text_offset" format="dimension"/> |
||||
<attr name="progress_text_visibility" format="enum"> |
||||
<enum name="visible" value="0" /> |
||||
<enum name="invisible" value="1" /> |
||||
<enum name="visible" value="0"/> |
||||
<enum name="invisible" value="1"/> |
||||
</attr> |
||||
</declare-styleable> |
||||
</resources> |
@ -1,4 +1,18 @@ |
||||
<resources> |
||||
<string name="app_name">DownloadDemo</string> |
||||
<string name="action_settings">Settings</string> |
||||
|
||||
<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/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>--> |
||||
<!--<item>http://static.gaoshouyou.com/d/22/94/822260b849944492caadd2983f9bb624.apk</item>--> |
||||
<!--<item>http://static.gaoshouyou.com/d/e6/f5/4de6329f9cf5dc3a1d1e6bbcca0d003c.apk</item>--> |
||||
<!--<item>http://static.gaoshouyou.com/d/6e/e5/ff6ecaaf45e532e6d07747af82357472.apk</item>--> |
||||
<!--<item>http://static.gaoshouyou.com/d/36/69/2d3699acfa69e9632262442c46516ad8.apk</item>--> |
||||
</string-array> |
||||
</resources> |
||||
|
@ -0,0 +1,63 @@ |
||||
package com.arialyy.downloadutil.help; |
||||
|
||||
import android.os.Environment; |
||||
|
||||
import java.security.MessageDigest; |
||||
import java.security.NoSuchAlgorithmException; |
||||
|
||||
/** |
||||
* Created by lyy on 2016/9/27. |
||||
* 下载路径帮助类 |
||||
*/ |
||||
public class PathHelp { |
||||
|
||||
/** |
||||
* 下载链接转换保存路径 |
||||
* |
||||
* @param downloadUrl 下载链接 |
||||
* @return 保存路径 |
||||
*/ |
||||
public static String urlconvertPath(String downloadUrl) { |
||||
return Environment.getDownloadCacheDirectory().getPath() + "/" + StringToHashKey( |
||||
downloadUrl); |
||||
} |
||||
|
||||
/** |
||||
* 字符串转换为hash码 |
||||
* |
||||
* @param str |
||||
* @return |
||||
*/ |
||||
public static String StringToHashKey(String str) { |
||||
String cacheKey; |
||||
try { |
||||
final MessageDigest mDigest = MessageDigest.getInstance("MD5"); |
||||
mDigest.update(str.getBytes()); |
||||
cacheKey = bytesToHexString(mDigest.digest()); |
||||
} catch (NoSuchAlgorithmException e) { |
||||
cacheKey = String.valueOf(str.hashCode()); |
||||
} |
||||
return cacheKey; |
||||
} |
||||
|
||||
/** |
||||
* 将普通字符串转换为16位进制字符串 |
||||
* |
||||
* @param src |
||||
* @return |
||||
*/ |
||||
public static String bytesToHexString(byte[] src) { |
||||
StringBuilder stringBuilder = new StringBuilder("0x"); |
||||
if (src == null || src.length <= 0) { |
||||
return null; |
||||
} |
||||
char[] buffer = new char[2]; |
||||
for (byte aSrc : src) { |
||||
buffer[0] = Character.forDigit((aSrc >>> 4) & 0x0F, 16); |
||||
buffer[1] = Character.forDigit(aSrc & 0x0F, 16); |
||||
stringBuilder.append(buffer); |
||||
} |
||||
return stringBuilder.toString(); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue