pull/2/head
lyy 8 years ago
parent 432b8773b1
commit 312d6e85d5
  1. BIN
      .gradle/2.10/taskArtifacts/cache.properties.lock
  2. BIN
      .gradle/2.10/taskArtifacts/fileHashes.bin
  3. BIN
      .gradle/2.10/taskArtifacts/fileSnapshots.bin
  4. BIN
      .gradle/2.10/taskArtifacts/outputFileStates.bin
  5. BIN
      .gradle/2.10/taskArtifacts/taskArtifacts.bin
  6. 229
      .idea/codeStyleSettings.xml
  7. 2
      .idea/misc.xml
  8. 1
      .idea/modules.xml
  9. 6
      .idea/vcs.xml
  10. 5
      app/src/main/AndroidManifest.xml
  11. 102
      app/src/main/java/com/arialyy/simple/activity/MainActivity.java
  12. 94
      app/src/main/java/com/arialyy/simple/activity/MultiTaskActivity.java
  13. 18
      app/src/main/java/com/arialyy/simple/activity/SingleTaskActivity.java
  14. 135
      app/src/main/java/com/arialyy/simple/module/DownloadModule.java
  15. 38
      app/src/main/res/layout/activity_main.xml
  16. 21
      app/src/main/res/layout/activity_multi.xml
  17. 13
      app/src/main/res/layout/activity_single.xml
  18. 4
      app/src/main/res/layout/content_single.xml
  19. 10
      app/src/main/res/layout/layout_bar.xml
  20. 16
      app/src/main/res/values-v21/styles.xml
  21. 2
      app/src/main/res/values/styles.xml
  22. 0
      cache/build/mPro.properties
  23. 5
      downloadutil/src/main/java/com/arialyy/downloadutil/util/DownLoadUtil.java
  24. 2
      downloadutil/src/main/java/com/arialyy/downloadutil/util/Task.java

@ -0,0 +1,229 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectCodeStyleSettingsManager">
<option name="PER_PROJECT_SETTINGS">
<value>
<option name="CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND" value="99" />
<option name="NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND" value="99" />
<option name="PACKAGES_TO_USE_IMPORT_ON_DEMAND">
<value />
</option>
<option name="IMPORT_LAYOUT_TABLE">
<value>
<package name="android" withSubpackages="true" static="false" />
<emptyLine />
<package name="com" withSubpackages="true" static="false" />
<emptyLine />
<package name="junit" withSubpackages="true" static="false" />
<emptyLine />
<package name="net" withSubpackages="true" static="false" />
<emptyLine />
<package name="org" withSubpackages="true" static="false" />
<emptyLine />
<package name="java" withSubpackages="true" static="false" />
<emptyLine />
<package name="javax" withSubpackages="true" static="false" />
<emptyLine />
<package name="" withSubpackages="true" static="false" />
<emptyLine />
<package name="" withSubpackages="true" static="true" />
<emptyLine />
</value>
</option>
<option name="RIGHT_MARGIN" value="100" />
<AndroidXmlCodeStyleSettings>
<option name="USE_CUSTOM_SETTINGS" value="true" />
</AndroidXmlCodeStyleSettings>
<Objective-C-extensions>
<option name="GENERATE_INSTANCE_VARIABLES_FOR_PROPERTIES" value="ASK" />
<option name="RELEASE_STYLE" value="IVAR" />
<option name="TYPE_QUALIFIERS_PLACEMENT" value="BEFORE" />
<file>
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Import" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Macro" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Typedef" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Enum" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Constant" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Global" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Struct" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="FunctionPredecl" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Function" />
</file>
<class>
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Property" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Synthesize" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InitMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="StaticMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InstanceMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="DeallocMethod" />
</class>
<extensions>
<pair source="cpp" header="h" />
<pair source="c" header="h" />
</extensions>
</Objective-C-extensions>
<XML>
<option name="XML_LEGACY_SETTINGS_IMPORTED" value="true" />
</XML>
<codeStyleSettings language="XML">
<option name="FORCE_REARRANGE_MODE" value="1" />
<indentOptions>
<option name="CONTINUATION_INDENT_SIZE" value="4" />
</indentOptions>
<arrangement>
<rules>
<section>
<rule>
<match>
<AND>
<NAME>xmlns:android</NAME>
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>xmlns:.*</NAME>
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*:id</NAME>
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*:name</NAME>
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>name</NAME>
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>style</NAME>
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*</NAME>
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*:layout_width</NAME>
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*:layout_height</NAME>
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*:layout_.*</NAME>
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*:width</NAME>
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*:height</NAME>
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*</NAME>
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*</NAME>
<XML_NAMESPACE>.*</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
</rules>
</arrangement>
</codeStyleSettings>
</value>
</option>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="aria" />
</component>
</project>

@ -37,7 +37,7 @@
<ConfirmationsSetting value="0" id="Add" /> <ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" /> <ConfirmationsSetting value="0" id="Remove" />
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">

@ -3,6 +3,7 @@
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/DownloadPrj.iml" filepath="$PROJECT_DIR$/DownloadPrj.iml" /> <module fileurl="file://$PROJECT_DIR$/DownloadPrj.iml" filepath="$PROJECT_DIR$/DownloadPrj.iml" />
<module fileurl="file://$PROJECT_DIR$/DownloadUtil.iml" filepath="$PROJECT_DIR$/DownloadUtil.iml" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" /> <module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
<module fileurl="file://$PROJECT_DIR$/downloadutil/downloadutil.iml" filepath="$PROJECT_DIR$/downloadutil/downloadutil.iml" /> <module fileurl="file://$PROJECT_DIR$/downloadutil/downloadutil.iml" filepath="$PROJECT_DIR$/downloadutil/downloadutil.iml" />
</modules> </modules>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

@ -13,7 +13,7 @@
android:label="@string/app_name" android:label="@string/app_name"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<!--android:name=".activity.SimpleTestActivity"--> <!--android:name=".activity.SingleTaskActivity"-->
<activity <activity
android:name=".activity.MainActivity" android:name=".activity.MainActivity"
android:label="@string/app_name" android:label="@string/app_name"
@ -24,6 +24,9 @@
<category android:name="android.intent.category.LAUNCHER"/> <category android:name="android.intent.category.LAUNCHER"/>
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".activity.SingleTaskActivity"/>
<activity android:name=".activity.MultiTaskActivity"/>
</application> </application>
</manifest> </manifest>

@ -1,90 +1,38 @@
package com.arialyy.simple.activity; package com.arialyy.simple.activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.Toolbar;
import android.support.v7.widget.RecyclerView; import android.view.View;
import butterknife.Bind;
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.R;
import com.arialyy.simple.adapter.DownloadAdapter;
import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.ActivityMainBinding; import com.arialyy.simple.databinding.ActivityMainBinding;
import com.arialyy.simple.module.DownloadModule;
import butterknife.Bind;
/** /**
* Created by Lyy on 2016/9/27. * Created by Lyy on 2016/10/13.
*/ */
public class MainActivity extends BaseActivity<ActivityMainBinding> { public class MainActivity extends BaseActivity<ActivityMainBinding> {
@Bind(R.id.list) RecyclerView mList; @Bind(R.id.toolbar) Toolbar mBar;
DownloadAdapter mAdapter;
@Override protected int setLayoutId() {
@Override protected int setLayoutId() { return R.layout.activity_main;
return R.layout.activity_main; }
}
@Override protected void init(Bundle savedInstanceState) {
@Override protected void init(Bundle savedInstanceState) { super.init(savedInstanceState);
super.init(savedInstanceState); setSupportActionBar(mBar);
mAdapter = new DownloadAdapter(this, getModule(DownloadModule.class).getDownloadData()); mBar.setTitle("多线程多任务下载");
mList.setLayoutManager(new LinearLayoutManager(this)); }
mList.setAdapter(mAdapter);
} public void onClick(View view) {
switch (view.getId()) {
case R.id.single_task:
private BroadcastReceiver mReceiver = new BroadcastReceiver() { startActivity(new Intent(this, SingleTaskActivity.class));
long len = 0; break;
case R.id.multi_task:
@Override public void onReceive(Context context, Intent intent) { startActivity(new Intent(this, MultiTaskActivity.class));
String action = intent.getAction(); break;
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);
} }
}
} }

@ -0,0 +1,94 @@
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 android.support.v7.widget.Toolbar;
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;
import com.arialyy.simple.databinding.ActivityMultiBinding;
import com.arialyy.simple.module.DownloadModule;
import butterknife.Bind;
/**
* 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;
@Override protected int setLayoutId() {
return R.layout.activity_multi;
}
@Override protected void init(Bundle savedInstanceState) {
super.init(savedInstanceState);
setSupportActionBar(mBar);
mBar.setTitle("多任务下载");
mAdapter = new DownloadAdapter(this, getModule(DownloadModule.class).getDownloadData());
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);
}
}

@ -23,7 +23,7 @@ import com.arialyy.downloadutil.util.Util;
import com.arialyy.frame.util.show.L; import com.arialyy.frame.util.show.L;
import com.arialyy.simple.R; import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.base.BaseActivity;
import com.arialyy.simple.databinding.ActivitySimpleBinding; import com.arialyy.simple.databinding.ActivitySingleBinding;
import com.arialyy.simple.module.DownloadModule; import com.arialyy.simple.module.DownloadModule;
import java.util.ArrayList; import java.util.ArrayList;
@ -31,7 +31,7 @@ import java.util.List;
import butterknife.Bind; import butterknife.Bind;
public class SimpleTestActivity extends BaseActivity<ActivitySimpleBinding> { public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
private static final int DOWNLOAD_PRE = 0x01; private static final int DOWNLOAD_PRE = 0x01;
private static final int DOWNLOAD_STOP = 0x02; private static final int DOWNLOAD_STOP = 0x02;
private static final int DOWNLOAD_FAILE = 0x03; private static final int DOWNLOAD_FAILE = 0x03;
@ -56,28 +56,28 @@ public class SimpleTestActivity extends BaseActivity<ActivitySimpleBinding> {
setBtState(false); setBtState(false);
break; break;
case DOWNLOAD_FAILE: case DOWNLOAD_FAILE:
Toast.makeText(SimpleTestActivity.this, "下载失败", Toast.LENGTH_SHORT).show(); Toast.makeText(SingleTaskActivity.this, "下载失败", Toast.LENGTH_SHORT).show();
setBtState(true); setBtState(true);
break; break;
case DOWNLOAD_STOP: case DOWNLOAD_STOP:
Toast.makeText(SimpleTestActivity.this, "暂停下载", Toast.LENGTH_SHORT).show(); Toast.makeText(SingleTaskActivity.this, "暂停下载", Toast.LENGTH_SHORT).show();
mStart.setText("恢复"); mStart.setText("恢复");
setBtState(true); setBtState(true);
break; break;
case DOWNLOAD_CANCEL: case DOWNLOAD_CANCEL:
mPb.setProgress(0); mPb.setProgress(0);
Toast.makeText(SimpleTestActivity.this, "取消下载", Toast.LENGTH_SHORT).show(); Toast.makeText(SingleTaskActivity.this, "取消下载", Toast.LENGTH_SHORT).show();
mStart.setText("开始"); mStart.setText("开始");
setBtState(true); setBtState(true);
break; break;
case DOWNLOAD_RESUME: case DOWNLOAD_RESUME:
Toast.makeText(SimpleTestActivity.this, Toast.makeText(SingleTaskActivity.this,
"恢复下载,恢复位置 ==> " + Util.formatFileSize((Long) msg.obj), "恢复下载,恢复位置 ==> " + Util.formatFileSize((Long) msg.obj),
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
setBtState(false); setBtState(false);
break; break;
case DOWNLOAD_COMPLETE: case DOWNLOAD_COMPLETE:
Toast.makeText(SimpleTestActivity.this, "下载完成", Toast.LENGTH_SHORT).show(); Toast.makeText(SingleTaskActivity.this, "下载完成", Toast.LENGTH_SHORT).show();
mStart.setText("重新开始?"); mStart.setText("重新开始?");
mCancel.setEnabled(false); mCancel.setEnabled(false);
setBtState(true); setBtState(true);
@ -157,14 +157,14 @@ public class SimpleTestActivity extends BaseActivity<ActivitySimpleBinding> {
} }
@Override protected int setLayoutId() { @Override protected int setLayoutId() {
return R.layout.activity_simple; return R.layout.activity_single;
} }
@Override protected void init(Bundle savedInstanceState) { @Override protected void init(Bundle savedInstanceState) {
super.init(savedInstanceState); super.init(savedInstanceState);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
toolbar.setTitle("单任务下载");
init(); init();
} }
private void init() { private void init() {

@ -3,7 +3,6 @@ package com.arialyy.simple.module;
import android.content.Context; import android.content.Context;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.os.Environment; import android.os.Environment;
import com.arialyy.downloadutil.core.DownloadManager; import com.arialyy.downloadutil.core.DownloadManager;
import com.arialyy.downloadutil.entity.DownloadEntity; import com.arialyy.downloadutil.entity.DownloadEntity;
import com.arialyy.downloadutil.util.Util; import com.arialyy.downloadutil.util.Util;
@ -11,7 +10,6 @@ import com.arialyy.frame.util.AndroidUtils;
import com.arialyy.frame.util.StringUtil; import com.arialyy.frame.util.StringUtil;
import com.arialyy.simple.R; import com.arialyy.simple.R;
import com.arialyy.simple.base.BaseModule; import com.arialyy.simple.base.BaseModule;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -20,65 +18,94 @@ import java.util.List;
* Created by Lyy on 2016/9/27. * Created by Lyy on 2016/9/27.
*/ */
public class DownloadModule extends BaseModule { public class DownloadModule extends BaseModule {
public DownloadModule(Context context) { public DownloadModule(Context context) {
super(context); super(context);
} }
/** /**
* 设置下载数据 * 设置下载数据
* */
* @return public List<DownloadEntity> getDownloadData() {
*/ List<DownloadEntity> list = DownloadEntity.findAllData(DownloadEntity.class);
public List<DownloadEntity> getDownloadData() { List<DownloadEntity> newDownload = createNewDownload();
List<DownloadEntity> list = DownloadEntity.findAllData(DownloadEntity.class); if (list == null) {
if (list == null) { list = newDownload;
list = createNewDownload(); } else {
} list = filter(list, newDownload);
return list;
} }
return list;
}
private List<DownloadEntity> createNewDownload() { /**
List<DownloadEntity> list = new ArrayList<>(); * 过滤任务
String[] urls = getContext().getResources() *
.getStringArray(R.array.test_apk_download_url); * @param sqlEntity 数据库的下载实体
for (String url : urls) { * @param createdEntity 通过下载链接生成的下载实体
String fileName = Util.keyToHashCode(url) + ".apk"; */
DownloadEntity entity = new DownloadEntity(); private List<DownloadEntity> filter(List<DownloadEntity> sqlEntity,
entity.setDownloadUrl(url); List<DownloadEntity> createdEntity) {
entity.setDownloadPath(getDownloadPath(url)); List<DownloadEntity> list = new ArrayList<>();
entity.setFileName(fileName); list.addAll(sqlEntity);
list.add(entity); for (DownloadEntity cEntity : createdEntity) {
int count = 0;
for (DownloadEntity sEntity : sqlEntity) {
if (cEntity.getDownloadUrl().equals(sEntity.getDownloadUrl())) {
break;
} }
return list; count++;
if (count == sqlEntity.size()) {
list.add(cEntity);
}
}
} }
return list;
}
/** /**
* 下载广播过滤器 * 创建下载列表
* */
* @return private List<DownloadEntity> createNewDownload() {
*/ List<DownloadEntity> list = new ArrayList<>();
public IntentFilter getDownloadFilter() { String[] urls = getContext().getResources().getStringArray(R.array.test_apk_download_url);
IntentFilter filter = new IntentFilter(); for (String url : urls) {
filter.addDataScheme(getContext().getPackageName()); String fileName = Util.keyToHashCode(url) + ".apk";
filter.addAction(DownloadManager.ACTION_PRE); DownloadEntity entity = new DownloadEntity();
filter.addAction(DownloadManager.ACTION_RESUME); entity.setDownloadUrl(url);
filter.addAction(DownloadManager.ACTION_START); entity.setDownloadPath(getDownloadPath(url));
filter.addAction(DownloadManager.ACTION_RUNNING); entity.setFileName(fileName);
filter.addAction(DownloadManager.ACTION_STOP); list.add(entity);
filter.addAction(DownloadManager.ACTION_CANCEL);
filter.addAction(DownloadManager.ACTION_COMPLETE);
filter.addAction(DownloadManager.ACTION_FAIL);
return filter;
} }
return list;
}
private String getDownloadPath(String url) { /**
String path = Environment.getExternalStorageDirectory() * 下载广播过滤器
.getPath() + "/" + AndroidUtils.getAppName(getContext()) + "downloads/" + StringUtil */
.keyToHashKey(url) + ".apk"; public IntentFilter getDownloadFilter() {
File file = new File(path); IntentFilter filter = new IntentFilter();
if (!file.getParentFile().exists()) { filter.addDataScheme(getContext().getPackageName());
file.getParentFile().mkdirs(); filter.addAction(DownloadManager.ACTION_PRE);
} filter.addAction(DownloadManager.ACTION_RESUME);
return path; 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) {
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,18 +1,32 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<layout <layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"> >
<LinearLayout <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="@layout/layout_bar"/>
<Button
android:id="@+id/single_task"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:onClick="onClick"
> android:text="单任务下载"
style="?buttonBarButtonStyle"
/>
<android.support.v7.widget.RecyclerView <Button
android:id="@+id/list" android:id="@+id/multi_task"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
/> android:onClick="onClick"
android:text="多任务下载"
style="?buttonBarButtonStyle"
/>
</LinearLayout> </LinearLayout>
</layout> </layout>

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="@layout/layout_bar"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</layout>

@ -2,13 +2,12 @@
<layout xmlns:android="http://schemas.android.com/apk/res/android"> <layout xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.design.widget.CoordinatorLayout <android.support.design.widget.CoordinatorLayout
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" android:fitsSystemWindows="true"
tools:context="com.arialyy.simple.activity.SimpleTestActivity" tools:context="com.arialyy.simple.activity.SingleTaskActivity"
> >
<android.support.design.widget.AppBarLayout <android.support.design.widget.AppBarLayout
@ -17,17 +16,11 @@
android:theme="@style/AppTheme.AppBarOverlay" android:theme="@style/AppTheme.AppBarOverlay"
> >
<android.support.v7.widget.Toolbar <include layout="@layout/layout_bar"/>
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"
/>
</android.support.design.widget.AppBarLayout> </android.support.design.widget.AppBarLayout>
<include layout="@layout/content_simple"/> <include layout="@layout/content_single"/>
<android.support.design.widget.FloatingActionButton <android.support.design.widget.FloatingActionButton
android:id="@+id/fab" android:id="@+id/fab"

@ -5,8 +5,8 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.arialyy.simple.activity.SimpleTestActivity" tools:context="com.arialyy.simple.activity.SingleTaskActivity"
tools:showIn="@layout/activity_simple" tools:showIn="@layout/activity_single"
> >
<ProgressBar <ProgressBar

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
/>

@ -9,4 +9,20 @@
<item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item> <item name="colorAccent">@color/colorAccent</item>
</style> </style>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!--设置导航栏内容可用-->
<!--<item name="android:windowTranslucentNavigation">true</item>-->
<!--设置状态栏内容可以-->
<!--<item name="android:windowTranslucentStatus">true</item>-->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
</style>
</resources> </resources>

@ -6,6 +6,8 @@
<item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item> <item name="colorAccent">@color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style> </style>
<style name="AppTheme.NoActionBar"> <style name="AppTheme.NoActionBar">

@ -142,11 +142,11 @@ final class DownLoadUtil {
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET"); conn.setRequestMethod("GET");
conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("Charset", "UTF-8");
conn.setConnectTimeout(TIME_OUT * 4);
conn.setRequestProperty("User-Agent", conn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"); "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
conn.setRequestProperty("Accept", conn.setRequestProperty("Accept",
"image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
conn.setConnectTimeout(TIME_OUT * 4);
conn.connect(); conn.connect();
int len = conn.getContentLength(); int len = conn.getContentLength();
if (len < 0) { //网络被劫持时会出现这个问题 if (len < 0) { //网络被劫持时会出现这个问题
@ -290,11 +290,11 @@ final class DownLoadUtil {
"bytes=" + dEntity.startLocation + "-" + dEntity.endLocation); "bytes=" + dEntity.startLocation + "-" + dEntity.endLocation);
conn.setRequestMethod("GET"); conn.setRequestMethod("GET");
conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("Charset", "UTF-8");
conn.setConnectTimeout(TIME_OUT * 4);
conn.setRequestProperty("User-Agent", conn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"); "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
conn.setRequestProperty("Accept", conn.setRequestProperty("Accept",
"image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
conn.setConnectTimeout(TIME_OUT * 4);
conn.setReadTimeout(TIME_OUT * 24); //设置读取流的等待时间,必须设置该参数 conn.setReadTimeout(TIME_OUT * 24); //设置读取流的等待时间,必须设置该参数
InputStream is = conn.getInputStream(); InputStream is = conn.getInputStream();
//创建可设置位置的文件 //创建可设置位置的文件
@ -323,6 +323,7 @@ final class DownLoadUtil {
} }
file.close(); file.close();
is.close(); is.close();
if (isCancel) { if (isCancel) {
synchronized (DownLoadUtil.this) { synchronized (DownLoadUtil.this) {
mCancelNum++; mCancelNum++;

@ -1,5 +1,7 @@
package com.arialyy.downloadutil.util; package com.arialyy.downloadutil.util;
import android.Manifest;
import android.app.AppOpsManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;

Loading…
Cancel
Save