From 618db821ed7856ec4d493977a6eb2796b42dc412 Mon Sep 17 00:00:00 2001 From: laoyuyu <511455842@qq.com> Date: Fri, 13 Apr 2018 20:24:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=BB=E6=84=8F=E7=B1=BB=E4=BD=BF=E7=94=A8ar?= =?UTF-8?q?ia?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/arialyy/aria/core/Aria.java | 94 ++++++++++--------- .../com/arialyy/aria/core/AriaManager.java | 58 +++++------- .../aria/core/download/BaseNormalTarget.java | 13 ++- .../com/arialyy/aria/core/inf/AbsTarget.java | 31 +++--- .../arialyy/aria/core/inf/AbsTaskEntity.java | 7 -- .../arialyy/aria/core/manager/DTEFactory.java | 2 + .../com/arialyy/aria/orm/AbsDelegate.java | 3 +- app/src/main/AndroidManifest.xml | 3 +- .../simple/download/DownloadDialog.java | 10 +- .../download/DownloadDialogFragment.java | 12 +-- .../simple/download/DownloadPopupWindow.java | 14 +-- .../fragment_download/DownloadFragment.java | 14 +-- .../download/group/ChildHandleDialog.java | 8 +- .../arialyy/simple/test/AnyRunActivity.java | 44 +++++++++ .../arialyy/simple/test/AnyRunnModule.java | 86 +++++++++++++++++ 15 files changed, 265 insertions(+), 134 deletions(-) create mode 100644 app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java create mode 100644 app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java diff --git a/Aria/src/main/java/com/arialyy/aria/core/Aria.java b/Aria/src/main/java/com/arialyy/aria/core/Aria.java index d6d681b7..f4d2b65e 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/Aria.java +++ b/Aria/src/main/java/com/arialyy/aria/core/Aria.java @@ -17,15 +17,8 @@ package com.arialyy.aria.core; import android.annotation.TargetApi; -import android.app.Activity; -import android.app.Application; -import android.app.Dialog; -import android.app.DialogFragment; -import android.app.Fragment; -import android.app.Service; import android.content.Context; import android.os.Build; -import android.widget.PopupWindow; import com.arialyy.aria.core.download.DownloadReceiver; import com.arialyy.aria.core.upload.UploadReceiver; @@ -60,52 +53,69 @@ import com.arialyy.aria.core.upload.UploadReceiver; /** * 初始化下载 * - * @param obj 支持类型有【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】 + * @param context 支持类型有【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】 */ - public static DownloadReceiver download(Object obj) { - return get(obj).download(obj); + public static DownloadReceiver download(Context context) { + return get(context).download(context); } /** * 初始化上传 * - * @param obj 支持类型有【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】 + * @param context 支持类型有【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】 */ - public static UploadReceiver upload(Object obj) { - return get(obj).upload(obj); + public static UploadReceiver upload(Context context) { + return get(context).upload(context); } /** - * 处理通用事件 + * 在任意对象中初始化下载 + * + * @param obj 任意对象 + */ + public static DownloadReceiver download(Context context, Object obj) { + return get(context).download(obj); + } + + /** + * 在任意对象中初始化上传 * - * @param obj 支持类型有【Activity、Service、Application、DialogFragment、Fragment、PopupWindow、Dialog】 + * @param obj 任意对象 + */ + public static UploadReceiver upload(Context context, Object obj) { + return get(context).upload(obj); + } + + /** + * 处理通用事件 */ - public static AriaManager get(Object obj) { - if (obj instanceof Activity || obj instanceof Service || obj instanceof Application) { - return AriaManager.getInstance((Context) obj); - } else if (obj instanceof DialogFragment) { - DialogFragment dialog = (DialogFragment) obj; - return AriaManager.getInstance( - Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? dialog.getContext() - : dialog.getActivity()); - } else if (obj instanceof android.support.v4.app.Fragment) { - android.support.v4.app.Fragment fragment = (android.support.v4.app.Fragment) obj; - return AriaManager.getInstance( - Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? fragment.getContext() - : fragment.getActivity()); - } else if (obj instanceof Fragment) { - Fragment fragment = (Fragment) obj; - return AriaManager.getInstance( - Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? fragment.getContext() - : fragment.getActivity()); - } else if (obj instanceof PopupWindow) { - PopupWindow popupWindow = (PopupWindow) obj; - return AriaManager.getInstance(popupWindow.getContentView().getContext()); - } else if (obj instanceof Dialog) { - Dialog dialog = (Dialog) obj; - return AriaManager.getInstance(dialog.getContext()); - } else { - throw new IllegalArgumentException("不支持的类型"); - } + public static AriaManager get(Context context) { + return AriaManager.getInstance(context); + //if (obj instanceof Activity || obj instanceof Service || obj instanceof Application) { + // return AriaManager.getInstance((Context) obj); + //} else if (obj instanceof DialogFragment) { + // DialogFragment dialog = (DialogFragment) obj; + // return AriaManager.getInstance( + // Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? dialog.getContext() + // : dialog.getActivity()); + //} else if (obj instanceof android.support.v4.app.Fragment) { + // android.support.v4.app.Fragment fragment = (android.support.v4.app.Fragment) obj; + // return AriaManager.getInstance( + // Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? fragment.getContext() + // : fragment.getActivity()); + //} else if (obj instanceof Fragment) { + // Fragment fragment = (Fragment) obj; + // return AriaManager.getInstance( + // Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? fragment.getContext() + // : fragment.getActivity()); + //} else if (obj instanceof PopupWindow) { + // PopupWindow popupWindow = (PopupWindow) obj; + // return AriaManager.getInstance(popupWindow.getContentView().getContext()); + //} else if (obj instanceof Dialog) { + // Dialog dialog = (Dialog) obj; + // return AriaManager.getInstance(dialog.getContext()); + //} else { + // throw new IllegalArgumentException("不支持的类型"); + //} } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java index c76c4cc0..645b458b 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java @@ -20,13 +20,11 @@ import android.annotation.TargetApi; import android.app.Activity; import android.app.Application; import android.app.Dialog; -import android.app.Service; import android.content.Context; import android.os.Build; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; -import android.text.TextUtils; import android.widget.PopupWindow; import com.arialyy.aria.core.command.ICmd; import com.arialyy.aria.core.common.QueueMod; @@ -258,6 +256,7 @@ import org.xml.sax.SAXException; final String key = getKey(isDownload, obj); IReceiver receiver = mReceivers.get(key); boolean needRmReceiver = false; + // 监控Dialog、fragment、popupWindow的生命周期 final WidgetLiftManager widgetLiftManager = new WidgetLiftManager(); if (obj instanceof Dialog) { needRmReceiver = widgetLiftManager.handleDialogLift((Dialog) obj); @@ -323,46 +322,33 @@ import org.xml.sax.SAXException; * 根据功能类型和控件类型获取对应的key */ private String getKey(boolean isDownload, Object obj) { - // TODO: 2018/4/12 不做限制了,让所有类都可以使用 String clsName = obj.getClass().getName(); - String key = ""; - if (!(obj instanceof Activity)) { - if (obj instanceof DialogFragment) { - key = clsName + "_" + ((DialogFragment) obj).getActivity().getClass().getName(); - } else if (obj instanceof android.app.DialogFragment) { - key = clsName + "_" + ((android.app.DialogFragment) obj).getActivity().getClass().getName(); - } else if (obj instanceof android.support.v4.app.Fragment) { - key = clsName + "_" + ((Fragment) obj).getActivity().getClass().getName(); - } else if (obj instanceof android.app.Fragment) { - key = clsName + "_" + ((android.app.Fragment) obj).getActivity().getClass().getName(); - } else if (obj instanceof Dialog) { - Activity activity = ((Dialog) obj).getOwnerActivity(); - if (activity != null) { - key = clsName + "_" + activity.getClass().getName(); - } else { - key = clsName; - } - } else if (obj instanceof PopupWindow) { - Context context = ((PopupWindow) obj).getContentView().getContext(); - if (context instanceof Activity) { - key = clsName + "_" + context.getClass().getName(); - } else { - key = clsName; - } - } else if (obj instanceof Service) { + String key; + if (obj instanceof DialogFragment) { + key = clsName + "_" + ((DialogFragment) obj).getActivity().getClass().getName(); + } else if (obj instanceof android.app.DialogFragment) { + key = clsName + "_" + ((android.app.DialogFragment) obj).getActivity().getClass().getName(); + } else if (obj instanceof android.support.v4.app.Fragment) { + key = clsName + "_" + ((Fragment) obj).getActivity().getClass().getName(); + } else if (obj instanceof android.app.Fragment) { + key = clsName + "_" + ((android.app.Fragment) obj).getActivity().getClass().getName(); + } else if (obj instanceof Dialog) { + Activity activity = ((Dialog) obj).getOwnerActivity(); + if (activity != null) { + key = clsName + "_" + activity.getClass().getName(); + } else { key = clsName; - } else if (obj instanceof Application) { + } + } else if (obj instanceof PopupWindow) { + Context context = ((PopupWindow) obj).getContentView().getContext(); + if (context instanceof Activity) { + key = clsName + "_" + context.getClass().getName(); + } else { key = clsName; } - } - if (obj instanceof Activity || obj instanceof Service) { - key = clsName; - } else if (obj instanceof Application) { + } else { key = clsName; } - if (TextUtils.isEmpty(key)) { - throw new IllegalArgumentException("未知类型"); - } key += (isDownload ? DOWNLOAD : UPLOAD) + obj.hashCode(); return key; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/download/BaseNormalTarget.java b/Aria/src/main/java/com/arialyy/aria/core/download/BaseNormalTarget.java index f5e8e6b0..70074b82 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/download/BaseNormalTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/download/BaseNormalTarget.java @@ -137,6 +137,8 @@ abstract class BaseNormalTarget filePath += mEntity.getFileName(); } } + mEntity.setFileName(file.getName()); + //设置文件保存路径,如果新文件路径和就文件路径不同,则修改路径 if (!filePath.equals(mEntity.getDownloadPath())) { if (!mTaskEntity.refreshInfo && DbEntity.checkDataExist(DownloadEntity.class, @@ -146,11 +148,12 @@ abstract class BaseNormalTarget } File oldFile = new File(mEntity.getDownloadPath()); File newFile = new File(filePath); - if (TextUtils.isEmpty(mEntity.getDownloadPath()) || oldFile.renameTo(newFile)) { - mEntity.setDownloadPath(filePath); - mEntity.setFileName(newFile.getName()); - mTaskEntity.key = filePath; - //mTaskEntity.update(); + mEntity.setDownloadPath(filePath); + mEntity.setFileName(newFile.getName()); + mTaskEntity.key = filePath; + //mTaskEntity.update(); + if (oldFile.exists()) { + oldFile.renameTo(newFile); CommonUtil.renameDownloadConfig(oldFile.getName(), newFile.getName()); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java index f67a7727..11f35e88 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java +++ b/Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java @@ -45,14 +45,12 @@ public abstract class AbsTarget extends DbEntity { } public abstract String getKey(); - - @Override public void update() { - if (getEntity() != null) { - getEntity().update(); - } - super.update(); - } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/manager/DTEFactory.java b/Aria/src/main/java/com/arialyy/aria/core/manager/DTEFactory.java index b958f11e..29f5299a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/manager/DTEFactory.java +++ b/Aria/src/main/java/com/arialyy/aria/core/manager/DTEFactory.java @@ -23,6 +23,7 @@ import com.arialyy.aria.core.inf.IEntity; import com.arialyy.aria.orm.DbEntity; import java.io.File; import java.util.List; +import java.util.UUID; /** * Created by Aria.Lao on 2017/11/1. @@ -89,6 +90,7 @@ class DTEFactory implements INormalTEFactory entity.setUrl(downloadUrl); entity.setGroupChild(false); entity.setGroupName(null); + entity.setDownloadPath(UUID.randomUUID().toString().replace("-", "")); //设置临时路径 } File file = new File(entity.getDownloadPath()); if (!file.exists()) { diff --git a/Aria/src/main/java/com/arialyy/aria/orm/AbsDelegate.java b/Aria/src/main/java/com/arialyy/aria/orm/AbsDelegate.java index e3a1ae64..fa5dd96d 100644 --- a/Aria/src/main/java/com/arialyy/aria/orm/AbsDelegate.java +++ b/Aria/src/main/java/com/arialyy/aria/orm/AbsDelegate.java @@ -18,6 +18,7 @@ package com.arialyy.aria.orm; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.v4.util.LruCache; +import android.text.TextUtils; import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.CommonUtil; import java.lang.reflect.Field; @@ -128,7 +129,7 @@ abstract class AbsDelegate { * @return 转换后的内容 */ String convertValue(String value) { - if (value.contains("'")) { + if (!TextUtils.isEmpty(value) && value.contains("'")) { return URLEncoder.encode(value); } return value; diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 18cc4fd4..e7f371fb 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -16,8 +16,9 @@ android:theme="@style/AppTheme.NoActionBar"> + diff --git a/app/src/main/java/com/arialyy/simple/download/DownloadDialog.java b/app/src/main/java/com/arialyy/simple/download/DownloadDialog.java index 234fb81e..68d656e4 100644 --- a/app/src/main/java/com/arialyy/simple/download/DownloadDialog.java +++ b/app/src/main/java/com/arialyy/simple/download/DownloadDialog.java @@ -57,8 +57,8 @@ public class DownloadDialog extends AbsDialog { } private void init() { - Aria.download(this).register(); - DownloadEntity entity = Aria.download(this).getDownloadEntity(DOWNLOAD_URL); + Aria.download(getContext()).register(); + DownloadEntity entity = Aria.download(getContext()).getDownloadEntity(DOWNLOAD_URL); if (entity != null) { mSize.setText(CommonUtil.formatFileSize(entity.getFileSize())); int p = (int) (entity.getCurrentProgress() * 100 / entity.getFileSize()); @@ -73,16 +73,16 @@ public class DownloadDialog extends AbsDialog { @OnClick({ R.id.start, R.id.stop, R.id.cancel }) public void onClick(View view) { switch (view.getId()) { case R.id.start: - Aria.download(this) + Aria.download(getContext()) .load(DOWNLOAD_URL) .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/飞机大战.apk") .start(); break; case R.id.stop: - Aria.download(this).load(DOWNLOAD_URL).pause(); + Aria.download(getContext()).load(DOWNLOAD_URL).pause(); break; case R.id.cancel: - Aria.download(this).load(DOWNLOAD_URL).cancel(); + Aria.download(getContext()).load(DOWNLOAD_URL).cancel(); break; } } diff --git a/app/src/main/java/com/arialyy/simple/download/DownloadDialogFragment.java b/app/src/main/java/com/arialyy/simple/download/DownloadDialogFragment.java index 77d59ce1..ec7a0153 100644 --- a/app/src/main/java/com/arialyy/simple/download/DownloadDialogFragment.java +++ b/app/src/main/java/com/arialyy/simple/download/DownloadDialogFragment.java @@ -31,8 +31,8 @@ import com.arialyy.simple.databinding.DialogFragmentDownloadBinding; @Override protected void init(Bundle savedInstanceState) { super.init(savedInstanceState); - Aria.download(this).register(); - DownloadEntity entity = Aria.download(this).getDownloadEntity(DOWNLOAD_URL); + Aria.download(getContext()).register(); + DownloadEntity entity = Aria.download(getContext()).getDownloadEntity(DOWNLOAD_URL); if (entity != null) { getBinding().setFileSize(CommonUtil.formatFileSize(entity.getFileSize())); getBinding().setProgress((int) (entity.getCurrentProgress() * 100 / entity.getFileSize())); @@ -45,7 +45,7 @@ import com.arialyy.simple.databinding.DialogFragmentDownloadBinding; @Override public void onDestroy() { super.onDestroy(); - Aria.download(this).unRegister(); + Aria.download(getContext()).unRegister(); } @Download.onPre(DOWNLOAD_URL) protected void onPre(DownloadTask task) { @@ -96,16 +96,16 @@ import com.arialyy.simple.databinding.DialogFragmentDownloadBinding; @OnClick({ R.id.start, R.id.stop, R.id.cancel }) public void onClick(View view) { switch (view.getId()) { case R.id.start: - Aria.download(this) + Aria.download(getContext()) .load(DOWNLOAD_URL) .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/放置江湖.apk") .start(); break; case R.id.stop: - Aria.download(this).load(DOWNLOAD_URL).stop(); + Aria.download(getContext()).load(DOWNLOAD_URL).stop(); break; case R.id.cancel: - Aria.download(this).load(DOWNLOAD_URL).cancel(); + Aria.download(getContext()).load(DOWNLOAD_URL).cancel(); break; } } diff --git a/app/src/main/java/com/arialyy/simple/download/DownloadPopupWindow.java b/app/src/main/java/com/arialyy/simple/download/DownloadPopupWindow.java index a8c7188d..f6fae8ac 100644 --- a/app/src/main/java/com/arialyy/simple/download/DownloadPopupWindow.java +++ b/app/src/main/java/com/arialyy/simple/download/DownloadPopupWindow.java @@ -59,13 +59,13 @@ public class DownloadPopupWindow extends AbsPopupWindow { } private void initWidget() { - if (Aria.download(this).taskExists(DOWNLOAD_URL)) { - DownloadTarget target = Aria.download(this).load(DOWNLOAD_URL); + if (Aria.download(getContext()).taskExists(DOWNLOAD_URL)) { + DownloadTarget target = Aria.download(getContext()).load(DOWNLOAD_URL); int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize()); mPb.setProgress(p); } - Aria.download(this).register(); - DownloadEntity entity = Aria.download(this).getDownloadEntity(DOWNLOAD_URL); + Aria.download(getContext()).register(); + DownloadEntity entity = Aria.download(getContext()).getDownloadEntity(DOWNLOAD_URL); if (entity != null) { mSize.setText(CommonUtil.formatFileSize(entity.getFileSize())); int state = entity.getState(); @@ -78,16 +78,16 @@ public class DownloadPopupWindow extends AbsPopupWindow { @OnClick({ R.id.start, R.id.stop, R.id.cancel }) public void onClick(View view) { switch (view.getId()) { case R.id.start: - Aria.download(this) + Aria.download(getContext()) .load(DOWNLOAD_URL) .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/消消乐.apk") .start(); break; case R.id.stop: - Aria.download(this).load(DOWNLOAD_URL).pause(); + Aria.download(getContext()).load(DOWNLOAD_URL).pause(); break; case R.id.cancel: - Aria.download(this).load(DOWNLOAD_URL).cancel(); + Aria.download(getContext()).load(DOWNLOAD_URL).cancel(); break; } } diff --git a/app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java b/app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java index 5fd45147..068ac47f 100644 --- a/app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java +++ b/app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java @@ -45,11 +45,11 @@ public class DownloadFragment extends AbsFragment { private static final String DOWNLOAD_URL = "https://res5.d.cn/2137e42d610b3488d9420c6421529386eee5bdbfd9be1fafe0a05d6dabaec8c156ddbd00581055bbaeac03904fb63310e80010680235d16bd4c040b50096a0c20dd1c4b0854529a1.apk"; @Override protected void init(Bundle savedInstanceState) { - if (Aria.download(this).taskExists(DOWNLOAD_URL)) { - DownloadTarget target = Aria.download(this).load(DOWNLOAD_URL); + if (Aria.download(getContext()).taskExists(DOWNLOAD_URL)) { + DownloadTarget target = Aria.download(getContext()).load(DOWNLOAD_URL); getBinding().setProgress(target.getPercent()); } - DownloadEntity entity = Aria.download(this).getDownloadEntity(DOWNLOAD_URL); + DownloadEntity entity = Aria.download(getContext()).getDownloadEntity(DOWNLOAD_URL); if (entity != null) { getBinding().setFileSize(CommonUtil.formatFileSize(entity.getFileSize())); int state = entity.getState(); @@ -57,22 +57,22 @@ public class DownloadFragment extends AbsFragment { } else { setBtState(true); } - Aria.download(this).register(); + Aria.download(getContext()).register(); } @OnClick({ R.id.start, R.id.stop, R.id.cancel }) public void onClick(View view) { switch (view.getId()) { case R.id.start: - Aria.download(this) + Aria.download(getContext()) .load(DOWNLOAD_URL) .setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/王者军团.apk") .start(); break; case R.id.stop: - Aria.download(this).load(DOWNLOAD_URL).pause(); + Aria.download(getContext()).load(DOWNLOAD_URL).pause(); break; case R.id.cancel: - Aria.download(this).load(DOWNLOAD_URL).cancel(); + Aria.download(getContext()).load(DOWNLOAD_URL).cancel(); break; } } diff --git a/app/src/main/java/com/arialyy/simple/download/group/ChildHandleDialog.java b/app/src/main/java/com/arialyy/simple/download/group/ChildHandleDialog.java index 0e16664d..c7bc958b 100644 --- a/app/src/main/java/com/arialyy/simple/download/group/ChildHandleDialog.java +++ b/app/src/main/java/com/arialyy/simple/download/group/ChildHandleDialog.java @@ -62,13 +62,13 @@ import java.util.List; @Override protected void init(Bundle savedInstanceState) { super.init(savedInstanceState); - Aria.download(this).register(); + Aria.download(getContext()).register(); initWidget(); } @Override public void onDestroy() { super.onDestroy(); - Aria.download(this).unRegister(); + Aria.download(getContext()).unRegister(); } private void initWidget() { @@ -134,10 +134,10 @@ import java.util.List; @OnClick({ R.id.start, R.id.stop, R.id.cancel }) void onClick(View view) { switch (view.getId()) { case R.id.start: - Aria.download(this).load(mUrls).getSubTaskManager().startSubTask(mChildEntity.getUrl()); + Aria.download(getContext()).load(mUrls).getSubTaskManager().startSubTask(mChildEntity.getUrl()); break; case R.id.stop: - Aria.download(this).load(mUrls).getSubTaskManager().stopSubTask(mChildEntity.getUrl()); + Aria.download(getContext()).load(mUrls).getSubTaskManager().stopSubTask(mChildEntity.getUrl()); break; //case R.id.cancel: // Aria.download(this).load(mUrls).getSubTaskManager().cancelSubTask(mChildEntity.getUrl()); diff --git a/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java b/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java new file mode 100644 index 00000000..c15d99be --- /dev/null +++ b/app/src/main/java/com/arialyy/simple/test/AnyRunActivity.java @@ -0,0 +1,44 @@ +package com.arialyy.simple.test; + +import android.os.Bundle; +import android.view.View; +import com.arialyy.simple.R; +import com.arialyy.simple.base.BaseActivity; +import com.arialyy.simple.databinding.ActivityTestBinding; + +/** + * Created by laoyuyu on 2018/4/13. + */ + +public class AnyRunActivity extends BaseActivity { + AnyRunnModule module; + + @Override protected int setLayoutId() { + return R.layout.activity_test; + } + + @Override protected void init(Bundle savedInstanceState) { + super.init(savedInstanceState); + mBar.setVisibility(View.GONE); + module = new AnyRunnModule(this); + } + + public void onClick(View view) { + switch (view.getId()) { + case R.id.start: + module.start(); + break; + case R.id.stop: + module.stop(); + break; + case R.id.cancel: + module.cancel(); + break; + } + } + + @Override protected void onDestroy() { + super.onDestroy(); + module.unRegist(); + } +} diff --git a/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java b/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java new file mode 100644 index 00000000..4817e0f0 --- /dev/null +++ b/app/src/main/java/com/arialyy/simple/test/AnyRunnModule.java @@ -0,0 +1,86 @@ +package com.arialyy.simple.test; + +import android.content.Context; +import android.os.Environment; +import android.util.Log; +import com.arialyy.annotations.Download; +import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.common.RequestEnum; +import com.arialyy.aria.core.download.DownloadTask; +import com.arialyy.aria.util.CommonUtil; +import com.arialyy.frame.util.show.L; +import java.io.File; + +/** + * Created by laoyuyu on 2018/4/13. + */ + +public class AnyRunnModule { + String TAG = "TestActivity"; + String URL = "http://58.210.9.131/tpk/sipgt//TDLYZTGH.tpk"; //chunked 下载 + private Context mContext; + + public AnyRunnModule(Context context) { + Aria.download(context, this).register(); + mContext = context; + } + + @Download.onWait void onWait(DownloadTask task) { + Log.d(TAG, "wait ==> " + task.getDownloadEntity().getFileName()); + } + + @Download.onPre protected void onPre(DownloadTask task) { + Log.d(TAG, "onPre"); + } + + @Download.onTaskStart void taskStart(DownloadTask task) { + Log.d(TAG, "onStart"); + } + + @Download.onTaskRunning protected void running(DownloadTask task) { + Log.d(TAG, "running"); + } + + @Download.onTaskResume void taskResume(DownloadTask task) { + Log.d(TAG, "resume"); + } + + @Download.onTaskStop void taskStop(DownloadTask task) { + Log.d(TAG, "stop"); + } + + @Download.onTaskCancel void taskCancel(DownloadTask task) { + Log.d(TAG, "cancel"); + } + + @Download.onTaskFail void taskFail(DownloadTask task) { + Log.d(TAG, "fail"); + } + + @Download.onTaskComplete void taskComplete(DownloadTask task) { + L.d(TAG, "path ==> " + task.getDownloadEntity().getDownloadPath()); + L.d(TAG, "md5Code ==> " + CommonUtil.getFileMD5(new File(task.getDownloadPath()))); + } + + void start() { + Aria.download(mContext, this) + .load(URL) + .addHeader("Accept-Encoding", "gzip, deflate") + .setRequestMode(RequestEnum.GET) + .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/ggsg1.apk") + .resetState() + .start(); + } + + void stop() { + Aria.download(mContext, this).load(URL).stop(); + } + + void cancel() { + Aria.download(mContext, this).load(URL).cancel(); + } + + void unRegist() { + Aria.download(mContext, this).unRegister(); + } +}