From 42c374e2752d8e6a500e2c17f28d1d8d32547f39 Mon Sep 17 00:00:00 2001 From: AriaLyy <511455842@qq.com> Date: Wed, 1 Mar 2017 17:17:44 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BE=8B=E5=AD=90=E7=BC=96=E5=86=99?= =?UTF-8?q?=EF=BC=8Cbug=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/arialyy/aria/core/Aria.java | 3 +- .../com/arialyy/aria/core/AriaManager.java | 2 +- .../aria/core/queue/pool/ExecutePool.java | 8 +- .../aria/core/upload/UploadReceiver.java | 8 ++ .../arialyy/aria/core/upload/UploadTask.java | 6 +- .../arialyy/aria/core/upload/UploadUtil.java | 28 +++-- app/src/main/AndroidManifest.xml | 13 +- .../java/com/arialyy/simple/MainActivity.java | 105 +++------------- .../simple/download/DownloadActivity.java | 111 +++++++++++++++++ .../DownloadDialog.java | 2 +- .../{module => download}/DownloadModule.java | 5 +- .../DownloadPopupWindow.java | 2 +- .../SimpleNotification.java | 2 +- .../SingleTaskActivity.java | 2 +- .../fragment_download}/DownloadFragment.java | 2 +- .../fragment_download}/FragmentActivity.java | 2 +- .../multi_download}/DownloadAdapter.java | 2 +- .../multi_download}/DownloadNumDialog.java | 2 +- .../multi_download}/FileListAdapter.java | 2 +- .../multi_download}/FileListEntity.java | 2 +- .../MultiDownloadActivity.java} | 15 ++- .../multi_download}/MultiTaskActivity.java | 6 +- .../main/java/com/arialyy/simple/upload.java | 71 ----------- .../arialyy/simple/upload/UploadActivity.java | 115 ++++++++++++++++++ .../res/layout/activity_download_mean.xml | 69 +++++++++++ app/src/main/res/layout/activity_fragment.xml | 2 +- app/src/main/res/layout/activity_main.xml | 51 +------- ...wnload.xml => activity_multi_download.xml} | 3 + app/src/main/res/layout/activity_single.xml | 2 +- app/src/main/res/layout/activity_upload.xml | 31 ----- .../main/res/layout/activity_upload_mean.xml | 97 +++++++++++++++ app/src/main/res/layout/content_single.xml | 2 +- 32 files changed, 483 insertions(+), 290 deletions(-) create mode 100644 app/src/main/java/com/arialyy/simple/download/DownloadActivity.java rename app/src/main/java/com/arialyy/simple/{dialog_task => download}/DownloadDialog.java (98%) rename app/src/main/java/com/arialyy/simple/{module => download}/DownloadModule.java (98%) rename app/src/main/java/com/arialyy/simple/{pop_task => download}/DownloadPopupWindow.java (99%) rename app/src/main/java/com/arialyy/simple/{notification => download}/SimpleNotification.java (98%) rename app/src/main/java/com/arialyy/simple/{single_task => download}/SingleTaskActivity.java (99%) rename app/src/main/java/com/arialyy/simple/{fragment_task => download/fragment_download}/DownloadFragment.java (98%) rename app/src/main/java/com/arialyy/simple/{fragment_task => download/fragment_download}/FragmentActivity.java (86%) rename app/src/main/java/com/arialyy/simple/{multi_task => download/multi_download}/DownloadAdapter.java (99%) rename app/src/main/java/com/arialyy/simple/{multi_task => download/multi_download}/DownloadNumDialog.java (97%) rename app/src/main/java/com/arialyy/simple/{multi_task => download/multi_download}/FileListAdapter.java (98%) rename app/src/main/java/com/arialyy/simple/{multi_task => download/multi_download}/FileListEntity.java (70%) rename app/src/main/java/com/arialyy/simple/{multi_task/DownloadActivity.java => download/multi_download/MultiDownloadActivity.java} (83%) rename app/src/main/java/com/arialyy/simple/{multi_task => download/multi_download}/MultiTaskActivity.java (95%) delete mode 100644 app/src/main/java/com/arialyy/simple/upload.java create mode 100644 app/src/main/java/com/arialyy/simple/upload/UploadActivity.java create mode 100644 app/src/main/res/layout/activity_download_mean.xml rename app/src/main/res/layout/{activity_download.xml => activity_multi_download.xml} (86%) delete mode 100644 app/src/main/res/layout/activity_upload.xml create mode 100644 app/src/main/res/layout/activity_upload_mean.xml 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 99ab3d33..dbb99ffa 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/Aria.java +++ b/Aria/src/main/java/com/arialyy/aria/core/Aria.java @@ -51,8 +51,7 @@ import com.arialyy.aria.core.upload.UploadTask; * Aria.upload(this) * .load(filePath) //文件路径,必填 * .setUploadUrl(uploadUrl) //上传路径,必填 - * .setFileName(fileName) //文件名 - * .setAttachment(fileKey) //服务器读取文件的key + * .setAttachment(fileKey) //服务器读取文件的key,必填 * .start(); * * 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 db0e4cd4..2bfd5868 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java +++ b/Aria/src/main/java/com/arialyy/aria/core/AriaManager.java @@ -124,7 +124,7 @@ import java.util.Map; UploadReceiver upload(Object obj) { IReceiver receiver = mReceivers.get(getKey(false, obj)); if (receiver == null) { - receiver = putReceiver(true, obj); + receiver = putReceiver(false, obj); } return (receiver instanceof UploadReceiver) ? (UploadReceiver) receiver : null; } diff --git a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java index 6b9b573d..9cfed938 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java +++ b/Aria/src/main/java/com/arialyy/aria/core/queue/pool/ExecutePool.java @@ -47,12 +47,12 @@ public class ExecutePool implements IPool { @Override public boolean putTask(TASK task) { synchronized (LOCK) { if (task == null) { - Log.e(TAG, "下载任务不能为空!!"); + Log.e(TAG, "任务不能为空!!"); return false; } String url = task.getKey(); if (mExecuteQueue.contains(task)) { - Log.e(TAG, "队列中已经包含了该任务,任务下载链接【" + url + "】"); + Log.e(TAG, "队列中已经包含了该任务,任务key【" + url + "】"); return false; } else { if (mExecuteQueue.size() >= mSize) { @@ -144,7 +144,7 @@ public class ExecutePool implements IPool { @Override public TASK getTask(String downloadUrl) { synchronized (LOCK) { if (TextUtils.isEmpty(downloadUrl)) { - Log.e(TAG, "请传入有效的下载链接"); + Log.e(TAG, "请传入有效的任务key"); return null; } String key = CommonUtil.keyToHashKey(downloadUrl); @@ -168,7 +168,7 @@ public class ExecutePool implements IPool { @Override public boolean removeTask(String downloadUrl) { synchronized (LOCK) { if (TextUtils.isEmpty(downloadUrl)) { - Log.e(TAG, "请传入有效的下载链接"); + Log.e(TAG, "请传入有效的任务key"); return false; } String key = CommonUtil.keyToHashKey(downloadUrl); diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java index 4ebee3c7..80b70cfa 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadReceiver.java @@ -16,6 +16,7 @@ package com.arialyy.aria.core.upload; import android.support.annotation.NonNull; +import android.util.Log; import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.command.AbsCmd; import com.arialyy.aria.core.command.CmdFactory; @@ -29,6 +30,8 @@ import com.arialyy.aria.util.CommonUtil; import java.util.ArrayList; import java.util.List; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Created by Aria.Lao on 2017/2/6. @@ -50,6 +53,11 @@ public class UploadReceiver implements IReceiver { if (entity == null) { entity = new UploadEntity(); } + String regex = "[/|\\\\|//]"; + Pattern p = Pattern.compile(regex); + String[] strs = p.split(filePath); + String fileName = strs[strs.length - 1]; + entity.setFileName(fileName); entity.setFilePath(filePath); return new UploadTarget(entity, targetName); } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java index 5b79b1ed..9c084a2a 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java @@ -41,11 +41,11 @@ public class UploadTask implements ITask { } @Override public String getKey() { - return null; + return mUploadEntity.getFilePath(); } @Override public boolean isRunning() { - return false; + return mUtil.isRunning(); } public UploadEntity getUploadEntity() { @@ -197,7 +197,7 @@ public class UploadTask implements ITask { */ private void sendInState2Target(int state) { if (outHandler.get() != null) { - outHandler.get().obtainMessage(state, task).sendToTarget(); + outHandler.get().obtainMessage(state, task.get()).sendToTarget(); } } diff --git a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadUtil.java b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadUtil.java index 12e5a702..916ed0f3 100644 --- a/Aria/src/main/java/com/arialyy/aria/core/upload/UploadUtil.java +++ b/Aria/src/main/java/com/arialyy/aria/core/upload/UploadUtil.java @@ -1,3 +1,18 @@ +/* + * 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.upload; import android.util.Log; @@ -21,7 +36,7 @@ import java.util.UUID; * Created by Aria.Lao on 2017/2/9. * 上传工具 */ -public class UploadUtil implements Runnable { +final class UploadUtil implements Runnable { private static final String TAG = "UploadUtil"; private final String BOUNDARY = UUID.randomUUID().toString(); // 边界标识 随机生成 private final String PREFIX = "--", LINE_END = "\r\n"; @@ -35,7 +50,7 @@ public class UploadUtil implements Runnable { private boolean isCancel = false; private boolean isRunning = false; - public UploadUtil(UploadTaskEntity taskEntity, IUploadListener listener) { + UploadUtil(UploadTaskEntity taskEntity, IUploadListener listener) { mTaskEntity = taskEntity; CheckUtil.checkUploadEntity(taskEntity.uploadEntity); mUploadEntity = taskEntity.uploadEntity; @@ -51,7 +66,7 @@ public class UploadUtil implements Runnable { new Thread(this).start(); } - public void cancel(){ + public void cancel() { isCancel = true; isRunning = false; } @@ -96,16 +111,13 @@ public class UploadUtil implements Runnable { mListener.onStart(uploadFile.length()); addFilePart(mTaskEntity.attachment, uploadFile); Log.d(TAG, finish() + ""); - } catch (MalformedURLException e) { - e.printStackTrace(); - fail(); } catch (IOException e) { e.printStackTrace(); fail(); } } - public boolean isRunning() { + boolean isRunning() { return isRunning; } @@ -200,8 +212,6 @@ public class UploadUtil implements Runnable { } reader.close(); mHttpConn.disconnect(); - } else { - throw new IOException("Server returned non-OK status: " + status); } mWriter.flush(); diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5aacf3df..edc9d707 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -13,10 +13,8 @@ android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme.NoActionBar"> - - @@ -25,10 +23,11 @@ - - - - + + + + + diff --git a/app/src/main/java/com/arialyy/simple/MainActivity.java b/app/src/main/java/com/arialyy/simple/MainActivity.java index 0df80bbc..d56a452b 100644 --- a/app/src/main/java/com/arialyy/simple/MainActivity.java +++ b/app/src/main/java/com/arialyy/simple/MainActivity.java @@ -1,114 +1,37 @@ -/* - * 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.simple; -import android.Manifest; import android.content.Intent; -import android.os.Build; import android.os.Bundle; import android.support.v7.widget.Toolbar; -import android.view.Gravity; import android.view.View; -import android.widget.Button; import butterknife.Bind; -import com.arialyy.frame.permission.OnPermissionCallback; -import com.arialyy.frame.permission.PermissionManager; -import com.arialyy.frame.util.show.T; +import butterknife.OnClick; import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.databinding.ActivityMainBinding; -import com.arialyy.simple.dialog_task.DownloadDialog; -import com.arialyy.simple.fragment_task.FragmentActivity; -import com.arialyy.simple.multi_task.MultiTaskActivity; -import com.arialyy.simple.notification.SimpleNotification; -import com.arialyy.simple.pop_task.DownloadPopupWindow; -import com.arialyy.simple.single_task.SingleTaskActivity; +import com.arialyy.simple.download.DownloadActivity; +import com.arialyy.simple.upload.UploadActivity; /** - * Created by Lyy on 2016/10/13. + * Created by Aria.Lao on 2017/3/1. */ public class MainActivity extends BaseActivity { - @Bind(R.id.toolbar) Toolbar mBar; - @Bind(R.id.single_task) Button mSigleBt; - @Bind(R.id.multi_task) Button mMultiBt; - @Bind(R.id.dialog_task) Button mDialogBt; - @Bind(R.id.pop_task) Button mPopBt; - @Override protected int setLayoutId() { - return R.layout.activity_main; - } + @Bind(R.id.toolbar) Toolbar mBar; @Override protected void init(Bundle savedInstanceState) { super.init(savedInstanceState); - setSupportActionBar(mBar); - mBar.setTitle("多线程多任务下载"); - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { - setEnable(true); - } else { //6.0处理 - boolean hasPermission = PermissionManager.getInstance() - .checkPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); - if (hasPermission) { - setEnable(true); - } else { - setEnable(false); - PermissionManager.getInstance().requestPermission(this, new OnPermissionCallback() { - @Override public void onSuccess(String... permissions) { - setEnable(true); - } + mBar.setTitle("Aria Demo"); + } - @Override public void onFail(String... permissions) { - T.showShort(MainActivity.this, "没有文件读写权限"); - setEnable(false); - } - }, Manifest.permission.WRITE_EXTERNAL_STORAGE); - } - } + @Override protected int setLayoutId() { + return R.layout.activity_main; } - private void setEnable(boolean enable) { - mSigleBt.setEnabled(enable); - mMultiBt.setEnabled(enable); - mDialogBt.setEnabled(enable); - mPopBt.setEnabled(enable); + @OnClick(R.id.download) public void downloadDemo() { + startActivity(new Intent(this, DownloadActivity.class)); } - public void onClick(View view) { - switch (view.getId()) { - case R.id.single_task: - startActivity(new Intent(this, SingleTaskActivity.class)); - break; - case R.id.multi_task: - startActivity(new Intent(this, MultiTaskActivity.class)); - break; - case R.id.dialog_task: - DownloadDialog dialog = new DownloadDialog(this); - dialog.show(); - break; - case R.id.pop_task: - DownloadPopupWindow pop = new DownloadPopupWindow(this); - //pop.showAsDropDown(mRootView); - pop.showAtLocation(mRootView, Gravity.CENTER_VERTICAL, 0, 0); - break; - case R.id.fragment_task: - startActivity(new Intent(this, FragmentActivity.class)); - break; - case R.id.notification: - SimpleNotification notification = new SimpleNotification(this); - notification.start(); - break; - } + @OnClick(R.id.upload) public void uploadDemo() { + startActivity(new Intent(this, UploadActivity.class)); } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/arialyy/simple/download/DownloadActivity.java b/app/src/main/java/com/arialyy/simple/download/DownloadActivity.java new file mode 100644 index 00000000..eec0ce78 --- /dev/null +++ b/app/src/main/java/com/arialyy/simple/download/DownloadActivity.java @@ -0,0 +1,111 @@ +/* + * 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.simple.download; + +import android.Manifest; +import android.content.Intent; +import android.os.Build; +import android.os.Bundle; +import android.support.v7.widget.Toolbar; +import android.view.Gravity; +import android.view.View; +import android.widget.Button; +import butterknife.Bind; +import com.arialyy.frame.permission.OnPermissionCallback; +import com.arialyy.frame.permission.PermissionManager; +import com.arialyy.frame.util.show.T; +import com.arialyy.simple.R; +import com.arialyy.simple.base.BaseActivity; +import com.arialyy.simple.databinding.ActivityDownloadMeanBinding; +import com.arialyy.simple.download.fragment_download.FragmentActivity; +import com.arialyy.simple.download.multi_download.MultiTaskActivity; + +/** + * Created by Lyy on 2016/10/13. + */ +public class DownloadActivity extends BaseActivity { + @Bind(R.id.toolbar) Toolbar mBar; + @Bind(R.id.single_task) Button mSigleBt; + @Bind(R.id.multi_task) Button mMultiBt; + @Bind(R.id.dialog_task) Button mDialogBt; + @Bind(R.id.pop_task) Button mPopBt; + + @Override protected int setLayoutId() { + return R.layout.activity_download_mean; + } + + @Override protected void init(Bundle savedInstanceState) { + super.init(savedInstanceState); + setSupportActionBar(mBar); + mBar.setTitle("多线程多任务下载"); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { + setEnable(true); + } else { //6.0处理 + boolean hasPermission = PermissionManager.getInstance() + .checkPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); + if (hasPermission) { + setEnable(true); + } else { + setEnable(false); + PermissionManager.getInstance().requestPermission(this, new OnPermissionCallback() { + @Override public void onSuccess(String... permissions) { + setEnable(true); + } + + @Override public void onFail(String... permissions) { + T.showShort(DownloadActivity.this, "没有文件读写权限"); + setEnable(false); + } + }, Manifest.permission.WRITE_EXTERNAL_STORAGE); + } + } + } + + private void setEnable(boolean enable) { + mSigleBt.setEnabled(enable); + mMultiBt.setEnabled(enable); + mDialogBt.setEnabled(enable); + mPopBt.setEnabled(enable); + } + + public void onClick(View view) { + switch (view.getId()) { + case R.id.single_task: + startActivity(new Intent(this, SingleTaskActivity.class)); + break; + case R.id.multi_task: + startActivity(new Intent(this, MultiTaskActivity.class)); + break; + case R.id.dialog_task: + DownloadDialog dialog = new DownloadDialog(this); + dialog.show(); + break; + case R.id.pop_task: + DownloadPopupWindow pop = new DownloadPopupWindow(this); + //pop.showAsDropDown(mRootView); + pop.showAtLocation(mRootView, Gravity.CENTER_VERTICAL, 0, 0); + break; + case R.id.fragment_task: + startActivity(new Intent(this, FragmentActivity.class)); + break; + case R.id.notification: + SimpleNotification notification = new SimpleNotification(this); + notification.start(); + break; + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/arialyy/simple/dialog_task/DownloadDialog.java b/app/src/main/java/com/arialyy/simple/download/DownloadDialog.java similarity index 98% rename from app/src/main/java/com/arialyy/simple/dialog_task/DownloadDialog.java rename to app/src/main/java/com/arialyy/simple/download/DownloadDialog.java index f1092a96..62a7e593 100644 --- a/app/src/main/java/com/arialyy/simple/dialog_task/DownloadDialog.java +++ b/app/src/main/java/com/arialyy/simple/download/DownloadDialog.java @@ -1,4 +1,4 @@ -package com.arialyy.simple.dialog_task; +package com.arialyy.simple.download; import android.content.Context; import android.os.Environment; diff --git a/app/src/main/java/com/arialyy/simple/module/DownloadModule.java b/app/src/main/java/com/arialyy/simple/download/DownloadModule.java similarity index 98% rename from app/src/main/java/com/arialyy/simple/module/DownloadModule.java rename to app/src/main/java/com/arialyy/simple/download/DownloadModule.java index 4a084193..a4303708 100644 --- a/app/src/main/java/com/arialyy/simple/module/DownloadModule.java +++ b/app/src/main/java/com/arialyy/simple/download/DownloadModule.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.arialyy.simple.module; +package com.arialyy.simple.download; import android.content.BroadcastReceiver; import android.content.Context; @@ -29,8 +29,7 @@ import com.arialyy.frame.util.AndroidUtils; import com.arialyy.frame.util.StringUtil; import com.arialyy.frame.util.show.L; import com.arialyy.simple.R; -import com.arialyy.simple.multi_task.FileListEntity; -import com.arialyy.simple.single_task.SingleTaskActivity; +import com.arialyy.simple.download.multi_download.FileListEntity; import com.arialyy.simple.base.BaseModule; import java.io.File; import java.util.ArrayList; diff --git a/app/src/main/java/com/arialyy/simple/pop_task/DownloadPopupWindow.java b/app/src/main/java/com/arialyy/simple/download/DownloadPopupWindow.java similarity index 99% rename from app/src/main/java/com/arialyy/simple/pop_task/DownloadPopupWindow.java rename to app/src/main/java/com/arialyy/simple/download/DownloadPopupWindow.java index 176ba3e0..c5631a4f 100644 --- a/app/src/main/java/com/arialyy/simple/pop_task/DownloadPopupWindow.java +++ b/app/src/main/java/com/arialyy/simple/download/DownloadPopupWindow.java @@ -1,4 +1,4 @@ -package com.arialyy.simple.pop_task; +package com.arialyy.simple.download; import android.content.Context; import android.graphics.Color; diff --git a/app/src/main/java/com/arialyy/simple/notification/SimpleNotification.java b/app/src/main/java/com/arialyy/simple/download/SimpleNotification.java similarity index 98% rename from app/src/main/java/com/arialyy/simple/notification/SimpleNotification.java rename to app/src/main/java/com/arialyy/simple/download/SimpleNotification.java index e16d1d87..e4e6c605 100644 --- a/app/src/main/java/com/arialyy/simple/notification/SimpleNotification.java +++ b/app/src/main/java/com/arialyy/simple/download/SimpleNotification.java @@ -1,4 +1,4 @@ -package com.arialyy.simple.notification; +package com.arialyy.simple.download; import android.app.NotificationManager; import android.content.Context; diff --git a/app/src/main/java/com/arialyy/simple/single_task/SingleTaskActivity.java b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java similarity index 99% rename from app/src/main/java/com/arialyy/simple/single_task/SingleTaskActivity.java rename to app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java index a398b82b..12941c7f 100644 --- a/app/src/main/java/com/arialyy/simple/single_task/SingleTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.arialyy.simple.single_task; +package com.arialyy.simple.download; import android.content.BroadcastReceiver; import android.content.Context; diff --git a/app/src/main/java/com/arialyy/simple/fragment_task/DownloadFragment.java b/app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java similarity index 98% rename from app/src/main/java/com/arialyy/simple/fragment_task/DownloadFragment.java rename to app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java index edd7155a..d9599818 100644 --- a/app/src/main/java/com/arialyy/simple/fragment_task/DownloadFragment.java +++ b/app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java @@ -1,4 +1,4 @@ -package com.arialyy.simple.fragment_task; +package com.arialyy.simple.download.fragment_download; import android.os.Bundle; import android.os.Environment; diff --git a/app/src/main/java/com/arialyy/simple/fragment_task/FragmentActivity.java b/app/src/main/java/com/arialyy/simple/download/fragment_download/FragmentActivity.java similarity index 86% rename from app/src/main/java/com/arialyy/simple/fragment_task/FragmentActivity.java rename to app/src/main/java/com/arialyy/simple/download/fragment_download/FragmentActivity.java index ff7a30cd..385c5769 100644 --- a/app/src/main/java/com/arialyy/simple/fragment_task/FragmentActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/fragment_download/FragmentActivity.java @@ -1,4 +1,4 @@ -package com.arialyy.simple.fragment_task; +package com.arialyy.simple.download.fragment_download; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; diff --git a/app/src/main/java/com/arialyy/simple/multi_task/DownloadAdapter.java b/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java similarity index 99% rename from app/src/main/java/com/arialyy/simple/multi_task/DownloadAdapter.java rename to app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java index 17a7d3be..50326e33 100644 --- a/app/src/main/java/com/arialyy/simple/multi_task/DownloadAdapter.java +++ b/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.arialyy.simple.multi_task; +package com.arialyy.simple.download.multi_download; import android.content.Context; import android.content.res.Resources; diff --git a/app/src/main/java/com/arialyy/simple/multi_task/DownloadNumDialog.java b/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadNumDialog.java similarity index 97% rename from app/src/main/java/com/arialyy/simple/multi_task/DownloadNumDialog.java rename to app/src/main/java/com/arialyy/simple/download/multi_download/DownloadNumDialog.java index 9f666f3e..c6170809 100644 --- a/app/src/main/java/com/arialyy/simple/multi_task/DownloadNumDialog.java +++ b/app/src/main/java/com/arialyy/simple/download/multi_download/DownloadNumDialog.java @@ -15,7 +15,7 @@ */ -package com.arialyy.simple.multi_task; +package com.arialyy.simple.download.multi_download; import android.annotation.SuppressLint; import android.os.Bundle; diff --git a/app/src/main/java/com/arialyy/simple/multi_task/FileListAdapter.java b/app/src/main/java/com/arialyy/simple/download/multi_download/FileListAdapter.java similarity index 98% rename from app/src/main/java/com/arialyy/simple/multi_task/FileListAdapter.java rename to app/src/main/java/com/arialyy/simple/download/multi_download/FileListAdapter.java index 65ebccf7..f2349390 100644 --- a/app/src/main/java/com/arialyy/simple/multi_task/FileListAdapter.java +++ b/app/src/main/java/com/arialyy/simple/download/multi_download/FileListAdapter.java @@ -1,4 +1,4 @@ -package com.arialyy.simple.multi_task; +package com.arialyy.simple.download.multi_download; import android.content.Context; import android.view.View; diff --git a/app/src/main/java/com/arialyy/simple/multi_task/FileListEntity.java b/app/src/main/java/com/arialyy/simple/download/multi_download/FileListEntity.java similarity index 70% rename from app/src/main/java/com/arialyy/simple/multi_task/FileListEntity.java rename to app/src/main/java/com/arialyy/simple/download/multi_download/FileListEntity.java index 15d584a8..0f356b11 100644 --- a/app/src/main/java/com/arialyy/simple/multi_task/FileListEntity.java +++ b/app/src/main/java/com/arialyy/simple/download/multi_download/FileListEntity.java @@ -1,4 +1,4 @@ -package com.arialyy.simple.multi_task; +package com.arialyy.simple.download.multi_download; /** * Created by AriaL on 2017/1/6. diff --git a/app/src/main/java/com/arialyy/simple/multi_task/DownloadActivity.java b/app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java similarity index 83% rename from app/src/main/java/com/arialyy/simple/multi_task/DownloadActivity.java rename to app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java index 61d4836a..304f685a 100644 --- a/app/src/main/java/com/arialyy/simple/multi_task/DownloadActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/multi_download/MultiDownloadActivity.java @@ -1,26 +1,28 @@ -package com.arialyy.simple.multi_task; +package com.arialyy.simple.download.multi_download; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.Toolbar; import butterknife.Bind; import com.arialyy.aria.core.Aria; 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.ActivityDownloadBinding; +import com.arialyy.simple.databinding.ActivityMultiDownloadBinding; /** * Created by AriaL on 2017/1/6. */ -public class DownloadActivity extends BaseActivity { - @Bind(R.id.list) RecyclerView mList; - private DownloadAdapter mAdapter; +public class MultiDownloadActivity extends BaseActivity { + @Bind(R.id.list) RecyclerView mList; + @Bind(R.id.toolbar) Toolbar mBar; + private DownloadAdapter mAdapter; @Override protected int setLayoutId() { - return R.layout.activity_download; + return R.layout.activity_multi_download; } @Override protected void init(Bundle savedInstanceState) { @@ -28,6 +30,7 @@ public class DownloadActivity extends BaseActivity { mAdapter = new DownloadAdapter(this, Aria.download(this).getTaskList()); mList.setLayoutManager(new LinearLayoutManager(this)); mList.setAdapter(mAdapter); + mBar.setTitle("多任务下载"); } @Override protected void dataCallback(int result, Object data) { diff --git a/app/src/main/java/com/arialyy/simple/multi_task/MultiTaskActivity.java b/app/src/main/java/com/arialyy/simple/download/multi_download/MultiTaskActivity.java similarity index 95% rename from app/src/main/java/com/arialyy/simple/multi_task/MultiTaskActivity.java rename to app/src/main/java/com/arialyy/simple/download/multi_download/MultiTaskActivity.java index e8cb4af5..1817754a 100644 --- a/app/src/main/java/com/arialyy/simple/multi_task/MultiTaskActivity.java +++ b/app/src/main/java/com/arialyy/simple/download/multi_download/MultiTaskActivity.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.arialyy.simple.multi_task; +package com.arialyy.simple.download.multi_download; import android.content.Intent; import android.os.Bundle; @@ -28,7 +28,7 @@ import com.arialyy.aria.core.download.DownloadTask; import com.arialyy.simple.R; import com.arialyy.simple.base.BaseActivity; import com.arialyy.simple.databinding.ActivityMultiBinding; -import com.arialyy.simple.module.DownloadModule; +import com.arialyy.simple.download.DownloadModule; import java.util.ArrayList; import java.util.List; @@ -65,7 +65,7 @@ public class MultiTaskActivity extends BaseActivity { Aria.download(this).stopAllTask(); break; case R.id.turn: - startActivity(new Intent(this, DownloadActivity.class)); + startActivity(new Intent(this, MultiDownloadActivity.class)); break; } } diff --git a/app/src/main/java/com/arialyy/simple/upload.java b/app/src/main/java/com/arialyy/simple/upload.java deleted file mode 100644 index bdf1a22d..00000000 --- a/app/src/main/java/com/arialyy/simple/upload.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.arialyy.simple; - -import android.view.View; -import android.widget.ProgressBar; -import butterknife.Bind; -import com.arialyy.aria.core.upload.IUploadListener; -import com.arialyy.aria.core.upload.UploadEntity; -import com.arialyy.aria.core.upload.UploadTaskEntity; -import com.arialyy.aria.core.upload.UploadUtil; -import com.arialyy.simple.base.BaseActivity; -import com.arialyy.simple.databinding.ActivityUploadBinding; -import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; - -/** - * Created by Aria.Lao on 2017/2/9. - */ - -public class upload extends BaseActivity { - - @Bind(R.id.pb) HorizontalProgressBarWithNumber mPb; - - @Override protected int setLayoutId() { - return R.layout.activity_upload; - } - - public void onClick(View view) { - UploadEntity entity = new UploadEntity(); - entity.setFilePath("/sdcard/Download/test.zip"); - entity.setFileName("test.pdf"); - UploadTaskEntity taskEntity = new UploadTaskEntity(entity); - taskEntity.uploadUrl = "http://172.18.104.189:8080/upload/sign_file"; - taskEntity.attachment = "file"; - UploadUtil util = new UploadUtil(taskEntity, new IUploadListener() { - long fileSize = 0; - - @Override public void onPre() { - - } - - @Override public void onStart(long fileSize) { - this.fileSize = fileSize; - } - - @Override public void onResume(long resumeLocation) { - - } - - @Override public void onStop(long stopLocation) { - - } - - @Override public void onProgress(long currentLocation) { - int p = (int) (currentLocation * 100 / fileSize); - mPb.setProgress(p); - } - - @Override public void onCancel() { - - } - - @Override public void onComplete() { - - } - - @Override public void onFail() { - - } - }); - util.start(); - } -} diff --git a/app/src/main/java/com/arialyy/simple/upload/UploadActivity.java b/app/src/main/java/com/arialyy/simple/upload/UploadActivity.java new file mode 100644 index 00000000..82010b09 --- /dev/null +++ b/app/src/main/java/com/arialyy/simple/upload/UploadActivity.java @@ -0,0 +1,115 @@ +package com.arialyy.simple.upload; + +import android.os.Handler; +import android.os.Message; +import butterknife.Bind; +import butterknife.OnClick; +import com.arialyy.aria.core.Aria; +import com.arialyy.aria.core.upload.UploadTask; +import com.arialyy.frame.util.FileUtil; +import com.arialyy.frame.util.show.L; +import com.arialyy.frame.util.show.T; +import com.arialyy.simple.R; +import com.arialyy.simple.base.BaseActivity; +import com.arialyy.simple.databinding.ActivityUploadMeanBinding; +import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; +import java.lang.ref.WeakReference; + +/** + * Created by Aria.Lao on 2017/2/9. + */ + +public class UploadActivity extends BaseActivity { + @Bind(R.id.pb) HorizontalProgressBarWithNumber mPb; + private static final int START = 0; + private static final int STOP = 1; + private static final int CANCEL = 2; + private static final int RUNNING = 3; + private static final int COMPLETE = 4; + + private static final String FILE_PATH = "/sdcard/Download/test.zip"; + + private Handler mHandler = new Handler() { + @Override public void handleMessage(Message msg) { + super.handleMessage(msg); + UploadTask task = (UploadTask) msg.obj; + switch (msg.what) { + case START: + getBinding().setFileSize(FileUtil.formatFileSize(task.getFileSize())); + break; + case STOP: + mPb.setProgress(0); + break; + case CANCEL: + mPb.setProgress(0); + break; + case RUNNING: + int p = (int) (task.getCurrentProgress() * 100 / task.getFileSize()); + L.d(TAG, "p ==> " + p); + mPb.setProgress(p); + break; + case COMPLETE: + T.showShort(UploadActivity.this, "上传完成"); + break; + } + } + }; + + @Override protected int setLayoutId() { + return R.layout.activity_upload_mean; + } + + @OnClick(R.id.upload) void upload() { + Aria.upload(this) + .load(FILE_PATH) + .setUploadUrl("http://172.18.104.50:8080/UploadActivity/sign_file") + .setAttachment("file") + .start(); + } + + @OnClick(R.id.stop) void stop() { + Aria.upload(this).load(FILE_PATH).stop(); + } + + @OnClick(R.id.remove) void remove() { + Aria.upload(this).load(FILE_PATH).cancel(); + } + + @Override protected void onResume() { + super.onResume(); + Aria.upload(this).addSchedulerListener(new UploadListener(mHandler)); + } + + static class UploadListener extends Aria.UploadSchedulerListener { + WeakReference handler; + + UploadListener(Handler handler) { + this.handler = new WeakReference<>(handler); + } + + @Override public void onTaskStart(UploadTask task) { + super.onTaskStart(task); + handler.get().obtainMessage(START, task).sendToTarget(); + } + + @Override public void onTaskStop(UploadTask task) { + super.onTaskStop(task); + handler.get().obtainMessage(STOP, task).sendToTarget(); + } + + @Override public void onTaskCancel(UploadTask task) { + super.onTaskCancel(task); + handler.get().obtainMessage(CANCEL, task).sendToTarget(); + } + + @Override public void onTaskRunning(UploadTask task) { + super.onTaskRunning(task); + handler.get().obtainMessage(RUNNING, task).sendToTarget(); + } + + @Override public void onTaskComplete(UploadTask task) { + super.onTaskComplete(task); + handler.get().obtainMessage(COMPLETE, task).sendToTarget(); + } + } +} diff --git a/app/src/main/res/layout/activity_download_mean.xml b/app/src/main/res/layout/activity_download_mean.xml new file mode 100644 index 00000000..08ccd012 --- /dev/null +++ b/app/src/main/res/layout/activity_download_mean.xml @@ -0,0 +1,69 @@ + + + + + + + +