速度单位转换

pull/330/head
AriaLyy 8 years ago
parent aac060b9bb
commit c402dde329
  1. 2
      .idea/modules.xml
  2. 13
      Aria/src/main/java/com/arialyy/aria/core/ConfigHelper.java
  3. 15
      Aria/src/main/java/com/arialyy/aria/core/Configuration.java
  4. 27
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java
  5. 72
      Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java
  6. 8
      Aria/src/main/java/com/arialyy/aria/core/inf/ITask.java
  7. 11
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java
  8. 104
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadTask.java
  9. 2
      Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java
  10. 2
      app/src/main/assets/aria_config.xml
  11. 6
      app/src/main/java/com/arialyy/simple/download/DownloadDialog.java
  12. 2
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java
  13. 2
      app/src/main/java/com/arialyy/simple/download/fragment_download/DownloadFragment.java
  14. 3
      app/src/main/java/com/arialyy/simple/download/multi_download/DownloadAdapter.java
  15. 4
      gradle.properties

@ -2,8 +2,8 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Aria/Aria.iml" filepath="$PROJECT_DIR$/Aria/Aria.iml" />
<module fileurl="file://$PROJECT_DIR$/Aria.iml" filepath="$PROJECT_DIR$/Aria.iml" />
<module fileurl="file://$PROJECT_DIR$/Aria/Aria.iml" filepath="$PROJECT_DIR$/Aria/Aria.iml" />
<module fileurl="file://$PROJECT_DIR$/Aria/Aria-Aria.iml" filepath="$PROJECT_DIR$/Aria/Aria-Aria.iml" />
<module fileurl="file://$PROJECT_DIR$/AriaPrj.iml" filepath="$PROJECT_DIR$/AriaPrj.iml" />
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />

@ -80,7 +80,20 @@ public class ConfigHelper extends DefaultHandler {
String caPath = attributes.getValue("path");
loadCA(caName, caPath);
break;
case "convertSpeed":
loadConvertSpeed(value);
break;
}
}
}
private void loadConvertSpeed(String value) {
boolean open = Boolean.parseBoolean(value);
if (isDownloadConfig) {
mDownloadConfig.isConvertSpeed = open;
}
if (isUploadConfig) {
mUploadConfig.isConvertSpeed = open;
}
}

@ -63,6 +63,11 @@ class Configuration {
*/
int connectTimeOut = 5000;
/**
* 是否需要转换速度单位转换完成后为1b/s1k/s1m/s1g/s1t/s如果不需要将返回byte长度
*/
boolean isConvertSpeed = false;
public boolean isOpenBreadCast() {
return isOpenBreadCast;
}
@ -105,6 +110,16 @@ class Configuration {
return this;
}
public boolean isConvertSpeed() {
return isConvertSpeed;
}
public BaseConfig setConvertSpeed(boolean convertSpeed) {
isConvertSpeed = convertSpeed;
saveKey("isConvertSpeed", isConvertSpeed + "");
return this;
}
public int getConnectTimeOut() {
return connectTimeOut;
}

@ -31,6 +31,7 @@ import com.arialyy.aria.orm.DbEntity;
*/
public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
@Ignore private long speed = 0; //下载速度
@Ignore private String convertSpeed = "0b/s";
@Ignore private int failNum = 0;
private String downloadUrl = ""; //下载路径
private String downloadPath = ""; //保存路径
@ -43,19 +44,10 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
private long completeTime; //完成时间
private boolean isRedirect = false;
private String redirectUrl = ""; //重定向链接
private int threadNum; //下载线程数
public DownloadEntity() {
}
public int getThreadNum() {
return threadNum;
}
public void setThreadNum(int threadNum) {
this.threadNum = threadNum;
}
public String getStr() {
return str;
}
@ -131,6 +123,14 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
isDownloadComplete = downloadComplete;
}
public String getConvertSpeed() {
return convertSpeed;
}
public void setConvertSpeed(String convertSpeed) {
this.convertSpeed = convertSpeed;
}
public long getSpeed() {
return speed;
}
@ -171,6 +171,9 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
return "DownloadEntity{"
+ "speed="
+ speed
+ ", convertSpeed='"
+ convertSpeed
+ '\''
+ ", failNum="
+ failNum
+ ", downloadUrl='"
@ -200,8 +203,6 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
+ ", redirectUrl='"
+ redirectUrl
+ '\''
+ ", threadNum="
+ threadNum
+ '}';
}
@ -211,6 +212,7 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
@Override public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(this.speed);
dest.writeString(this.convertSpeed);
dest.writeInt(this.failNum);
dest.writeString(this.downloadUrl);
dest.writeString(this.downloadPath);
@ -223,11 +225,11 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
dest.writeLong(this.completeTime);
dest.writeByte(this.isRedirect ? (byte) 1 : (byte) 0);
dest.writeString(this.redirectUrl);
dest.writeInt(this.threadNum);
}
protected DownloadEntity(Parcel in) {
this.speed = in.readLong();
this.convertSpeed = in.readString();
this.failNum = in.readInt();
this.downloadUrl = in.readString();
this.downloadPath = in.readString();
@ -240,7 +242,6 @@ public class DownloadEntity extends DbEntity implements Parcelable, IEntity {
this.completeTime = in.readLong();
this.isRedirect = in.readByte() != 0;
this.redirectUrl = in.readString();
this.threadNum = in.readInt();
}
@Ignore public static final Creator<DownloadEntity> CREATOR = new Creator<DownloadEntity>() {

@ -40,32 +40,53 @@ public class DownloadTask implements ITask {
*/
private String mTargetName;
private DownloadEntity mEntity;
private DownloadTaskEntity mTaskEntity;
private IDownloadListener mListener;
private Handler mOutHandler;
private IDownloadUtil mUtil;
private Context mContext;
private DownloadTask(DownloadTaskEntity taskEntity, Handler outHandler) {
mTaskEntity = taskEntity;
mEntity = taskEntity.downloadEntity;
mOutHandler = outHandler;
mContext = AriaManager.APP;
init();
}
private void init() {
mListener = new DListener(mContext, this, mOutHandler);
mUtil = new DownloadUtil(mContext, mTaskEntity, mListener);
mUtil = new DownloadUtil(mContext, taskEntity, mListener);
}
/**
* 获取下载速度
* @return 返回原始byte速度需要你在配置文件中配置
* <pre>
* <download>
* ...
* <convertSpeed value="false"/>
* </download>
*
* 或在代码中设置
* Aria.get(this).getDownloadConfig().setConvertSpeed(false);
* </pre>
* 才能生效
*/
@Override public long getSpeed() {
return mEntity.getSpeed();
}
/**
* @return 返回转换单位后的速度需要你在配置文件中配置转换完成后为1b/s1k/s1m/s1g/s1t/s
* <pre>
* <download>
* ...
* <convertSpeed value="true"/>
* </download>
*
* 或在代码中设置
* Aria.get(this).getDownloadConfig().setConvertSpeed(true);
* </pre>
* 才能生效
*/
@Override public String getConvertSpeed() {
return mEntity.getConvertSpeed();
}
/**
* 获取文件大小
*/
@ -182,7 +203,6 @@ public class DownloadTask implements ITask {
public static class Builder {
DownloadTaskEntity taskEntity;
Handler outHandler;
int threadNum = 3;
String targetName;
public Builder(String targetName, DownloadTaskEntity taskEntity) {
@ -201,14 +221,6 @@ public class DownloadTask implements ITask {
return this;
}
/**
* 设置线程数
*/
public Builder setThreadNum(int threadNum) {
this.threadNum = threadNum;
return this;
}
public DownloadTask build() {
DownloadTask task = new DownloadTask(taskEntity, outHandler);
task.setTargetName(targetName);
@ -225,7 +237,6 @@ public class DownloadTask implements ITask {
WeakReference<DownloadTask> wTask;
Context context;
Intent sendIntent;
long INTERVAL = 1024 * 10; //10k大小的间隔
long lastLen = 0; //上一次发送长度
long lastTime = 0;
long INTERVAL_TIME = 1000; //1m更新周期
@ -233,6 +244,7 @@ public class DownloadTask implements ITask {
DownloadEntity downloadEntity;
DownloadTask task;
boolean isOpenBroadCast = false;
boolean isConvertSpeed = false;
DListener(Context context, DownloadTask task, Handler outHandler) {
this.context = context;
@ -242,7 +254,9 @@ public class DownloadTask implements ITask {
this.downloadEntity = this.task.getDownloadEntity();
sendIntent = CommonUtil.createIntent(context.getPackageName(), Aria.ACTION_RUNNING);
sendIntent.putExtra(Aria.ENTITY, downloadEntity);
isOpenBroadCast = AriaManager.getInstance(context).getDownloadConfig().isOpenBreadCast();
final AriaManager manager = AriaManager.getInstance(context);
isOpenBroadCast = manager.getDownloadConfig().isOpenBreadCast();
isConvertSpeed = manager.getDownloadConfig().isConvertSpeed();
}
@Override public void supportBreakpoint(boolean support) {
@ -289,11 +303,10 @@ public class DownloadTask implements ITask {
sendIntent.putExtra(Aria.CURRENT_SPEED, speed);
lastTime = System.currentTimeMillis();
if (isFirst) {
downloadEntity.setSpeed(0);
speed = 0;
isFirst = false;
} else {
downloadEntity.setSpeed(speed);
}
handleSpeed(speed);
downloadEntity.setCurrentProgress(currentLocation);
lastLen = currentLocation;
sendInState2Target(DownloadSchedulers.RUNNING);
@ -304,7 +317,7 @@ public class DownloadTask implements ITask {
@Override public void onStop(long stopLocation) {
super.onStop(stopLocation);
downloadEntity.setState(DownloadEntity.STATE_STOP);
downloadEntity.setSpeed(0);
handleSpeed(0);
sendInState2Target(DownloadSchedulers.STOP);
sendIntent(Aria.ACTION_STOP, stopLocation);
}
@ -312,6 +325,7 @@ public class DownloadTask implements ITask {
@Override public void onCancel() {
super.onCancel();
downloadEntity.setState(DownloadEntity.STATE_CANCEL);
handleSpeed(0);
sendInState2Target(DownloadSchedulers.CANCEL);
sendIntent(Aria.ACTION_CANCEL, -1);
downloadEntity.deleteData();
@ -321,7 +335,7 @@ public class DownloadTask implements ITask {
super.onComplete();
downloadEntity.setState(DownloadEntity.STATE_COMPLETE);
downloadEntity.setDownloadComplete(true);
downloadEntity.setSpeed(0);
handleSpeed(0);
sendInState2Target(DownloadSchedulers.COMPLETE);
sendIntent(Aria.ACTION_COMPLETE, downloadEntity.getFileSize());
}
@ -330,11 +344,19 @@ public class DownloadTask implements ITask {
super.onFail();
downloadEntity.setFailNum(downloadEntity.getFailNum() + 1);
downloadEntity.setState(DownloadEntity.STATE_FAIL);
downloadEntity.setSpeed(0);
handleSpeed(0);
sendInState2Target(DownloadSchedulers.FAIL);
sendIntent(Aria.ACTION_FAIL, -1);
}
private void handleSpeed(long speed) {
if (isConvertSpeed) {
downloadEntity.setConvertSpeed(CommonUtil.formatFileSize(speed) + "/s");
} else {
downloadEntity.setSpeed(speed);
}
}
/**
* 将任务状态发送给下载器
*

@ -44,8 +44,16 @@ public interface ITask {
public void cancel();
/**
* 原始byte速度
*/
public long getSpeed();
/**
* 转换单位后的速度
*/
public String getConvertSpeed();
public long getFileSize();
public long getCurrentProgress();

@ -34,8 +34,17 @@ public class UploadEntity extends DbEntity implements IEntity, Parcelable {
private long currentProgress = 0;
private boolean isComplete = false;
@Ignore private long speed = 0; //下载速度
@Ignore private String convertSpeed = "0/s";
@Ignore private int failNum = 0;
public String getConvertSpeed() {
return convertSpeed;
}
public void setConvertSpeed(String convertSpeed) {
this.convertSpeed = convertSpeed;
}
public boolean isComplete() {
return isComplete;
}
@ -115,6 +124,7 @@ public class UploadEntity extends DbEntity implements IEntity, Parcelable {
dest.writeLong(this.currentProgress);
dest.writeByte(this.isComplete ? (byte) 1 : (byte) 0);
dest.writeLong(this.speed);
dest.writeString(this.convertSpeed);
dest.writeInt(this.failNum);
}
@ -126,6 +136,7 @@ public class UploadEntity extends DbEntity implements IEntity, Parcelable {
this.currentProgress = in.readLong();
this.isComplete = in.readByte() != 0;
this.speed = in.readLong();
this.convertSpeed = in.readString();
this.failNum = in.readInt();
}

@ -36,19 +36,17 @@ import java.lang.ref.WeakReference;
public class UploadTask implements ITask {
private static final String TAG = "UploadTask";
private Handler mOutHandler;
private UploadTaskEntity mTaskEntity;
private UploadEntity mUploadEntity;
private String mTargetName;
private UploadUtil mUtil;
private UListener mListener;
UploadTask(UploadTaskEntity taskEntity, Handler outHandler) {
mTaskEntity = taskEntity;
private UploadTask(UploadTaskEntity taskEntity, Handler outHandler) {
mOutHandler = outHandler;
mUploadEntity = mTaskEntity.uploadEntity;
mUploadEntity = taskEntity.uploadEntity;
mListener = new UListener(mOutHandler, this);
mUtil = new UploadUtil(mTaskEntity, mListener);
mUtil = new UploadUtil(taskEntity, mListener);
}
@Override public void setTargetName(String targetName) {
@ -110,10 +108,41 @@ public class UploadTask implements ITask {
return mTargetName;
}
/**
* @return 返回原始byte速度需要你在配置文件中配置
* <pre>
* <upload>
* ...
* <convertSpeed value="false"/>
* </upload>
*
* 或在代码中设置
* Aria.get(this).getUploadConfig().setConvertSpeed(false);
* </pre>
* 才能生效
*/
@Override public long getSpeed() {
return mUploadEntity.getSpeed();
}
/**
* @return 返回转换单位后的速度需要你在配置文件中配置转换完成后为1b/s1k/s1m/s1g/s1t/s
* <pre>
* <upload>
* ...
* <convertSpeed value="true"/>
* </upload>
*
* 或在代码中设置
* Aria.get(this).getUploadConfig().setConvertSpeed(true);
* </pre>
*
* 才能生效
*/
@Override public String getConvertSpeed() {
return mUploadEntity.getConvertSpeed();
}
@Override public long getFileSize() {
return mUploadEntity.getFileSize();
}
@ -129,43 +158,46 @@ public class UploadTask implements ITask {
long lastTime = 0;
long INTERVAL_TIME = 1000; //1m更新周期
boolean isFirst = true;
UploadEntity entity;
UploadEntity uploadEntity;
Intent sendIntent;
boolean isOpenBroadCast = false;
boolean isConvertSpeed = false;
Context context;
UListener(Handler outHandle, UploadTask task) {
this.outHandler = new WeakReference<>(outHandle);
this.task = new WeakReference<>(task);
entity = this.task.get().getUploadEntity();
uploadEntity = this.task.get().getUploadEntity();
sendIntent = CommonUtil.createIntent(AriaManager.APP.getPackageName(), Aria.ACTION_RUNNING);
sendIntent.putExtra(Aria.ENTITY, entity);
sendIntent.putExtra(Aria.ENTITY, uploadEntity);
context = AriaManager.APP;
isOpenBroadCast = AriaManager.getInstance(context).getUploadConfig().isOpenBreadCast();
final AriaManager manager = AriaManager.getInstance(context);
isOpenBroadCast = manager.getUploadConfig().isOpenBreadCast();
isConvertSpeed = manager.getUploadConfig().isConvertSpeed();
}
@Override public void onPre() {
entity.setState(IEntity.STATE_PRE);
uploadEntity.setState(IEntity.STATE_PRE);
sendIntent(Aria.ACTION_PRE, -1);
sendInState2Target(ISchedulers.PRE);
}
@Override public void onStart(long fileSize) {
entity.setFileSize(fileSize);
entity.setState(IEntity.STATE_RUNNING);
uploadEntity.setFileSize(fileSize);
uploadEntity.setState(IEntity.STATE_RUNNING);
sendIntent(Aria.ACTION_PRE, -1);
sendInState2Target(ISchedulers.START);
}
@Override public void onResume(long resumeLocation) {
entity.setState(DownloadEntity.STATE_RUNNING);
uploadEntity.setState(DownloadEntity.STATE_RUNNING);
sendInState2Target(DownloadSchedulers.RESUME);
sendIntent(Aria.ACTION_RESUME, resumeLocation);
}
@Override public void onStop(long stopLocation) {
entity.setState(DownloadEntity.STATE_STOP);
entity.setSpeed(0);
uploadEntity.setState(DownloadEntity.STATE_STOP);
handleSpeed(0);
sendInState2Target(DownloadSchedulers.STOP);
sendIntent(Aria.ACTION_STOP, stopLocation);
}
@ -177,12 +209,11 @@ public class UploadTask implements ITask {
sendIntent.putExtra(Aria.CURRENT_SPEED, speed);
lastTime = System.currentTimeMillis();
if (isFirst) {
entity.setSpeed(0);
speed = 0;
isFirst = false;
} else {
entity.setSpeed(speed);
}
entity.setCurrentProgress(currentLocation);
handleSpeed(speed);
uploadEntity.setCurrentProgress(currentLocation);
lastLen = currentLocation;
sendInState2Target(DownloadSchedulers.RUNNING);
AriaManager.APP.sendBroadcast(sendIntent);
@ -190,28 +221,37 @@ public class UploadTask implements ITask {
}
@Override public void onCancel() {
entity.setState(DownloadEntity.STATE_CANCEL);
uploadEntity.setState(DownloadEntity.STATE_CANCEL);
handleSpeed(0);
sendInState2Target(DownloadSchedulers.CANCEL);
sendIntent(Aria.ACTION_CANCEL, -1);
entity.deleteData();
uploadEntity.deleteData();
}
@Override public void onComplete() {
entity.setState(DownloadEntity.STATE_COMPLETE);
entity.setComplete(true);
entity.setSpeed(0);
uploadEntity.setState(DownloadEntity.STATE_COMPLETE);
uploadEntity.setComplete(true);
handleSpeed(0);
sendInState2Target(DownloadSchedulers.COMPLETE);
sendIntent(Aria.ACTION_COMPLETE, entity.getFileSize());
sendIntent(Aria.ACTION_COMPLETE, uploadEntity.getFileSize());
}
@Override public void onFail() {
entity.setFailNum(entity.getFailNum() + 1);
entity.setState(DownloadEntity.STATE_FAIL);
entity.setSpeed(0);
uploadEntity.setFailNum(uploadEntity.getFailNum() + 1);
uploadEntity.setState(DownloadEntity.STATE_FAIL);
handleSpeed(0);
sendInState2Target(DownloadSchedulers.FAIL);
sendIntent(Aria.ACTION_FAIL, -1);
}
private void handleSpeed(long speed) {
if (isConvertSpeed) {
uploadEntity.setConvertSpeed(CommonUtil.formatFileSize(speed) + "/s");
} else {
uploadEntity.setSpeed(speed);
}
}
/**
* 将任务状态发送给下载器
*
@ -224,12 +264,12 @@ public class UploadTask implements ITask {
}
private void sendIntent(String action, long location) {
entity.setComplete(action.equals(Aria.ACTION_COMPLETE));
entity.setCurrentProgress(location);
entity.update();
uploadEntity.setComplete(action.equals(Aria.ACTION_COMPLETE));
uploadEntity.setCurrentProgress(location);
uploadEntity.update();
if (!isOpenBroadCast) return;
Intent intent = CommonUtil.createIntent(context.getPackageName(), action);
intent.putExtra(Aria.ENTITY, entity);
intent.putExtra(Aria.ENTITY, uploadEntity);
if (location != -1) {
intent.putExtra(Aria.CURRENT_LOCATION, location);
}

@ -28,7 +28,7 @@ import java.util.Map;
public class DBConfig {
static Map<String, Class> mapping = new HashMap<>();
static String DB_NAME;
static int VERSION = 3;
static int VERSION = 2;
static {
if (TextUtils.isEmpty(DB_NAME)) {

@ -32,7 +32,7 @@
<ca name="" path=""/>
<!--是否需要转换速度单位,转换完成后为:1b/s、1k/s、1m/s、1g/s、1t/s,如果不需要将返回byte长度-->
<cnvertSpeed value="false"/>
<convertSpeed value="true"/>
</download>

@ -110,14 +110,14 @@ public class DownloadDialog extends AbsDialog {
@Override public void onTaskStop(DownloadTask task) {
super.onTaskStop(task);
setBtState(true);
mSpeed.setText("0.0kb/s");
mSpeed.setText(task.getConvertSpeed());
}
@Override public void onTaskCancel(DownloadTask task) {
super.onTaskCancel(task);
setBtState(true);
mPb.setProgress(0);
mSpeed.setText("0.0kb/s");
mSpeed.setText(task.getConvertSpeed());
}
@Override public void onTaskRunning(DownloadTask task) {
@ -129,7 +129,7 @@ public class DownloadDialog extends AbsDialog {
} else {
mPb.setProgress((int) ((current * 100) / len));
}
mSpeed.setText(CommonUtil.formatFileSize(task.getSpeed()) + "/s");
mSpeed.setText(task.getConvertSpeed());
}
}
}

@ -91,7 +91,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
} else {
mPb.setProgress((int) ((current * 100) / len));
}
mSpeed.setText(CommonUtil.formatFileSize(task.getSpeed()) + "/s");
mSpeed.setText(task.getConvertSpeed());
break;
case DOWNLOAD_PRE:
mSize.setText(CommonUtil.formatFileSize((Long) msg.obj));

@ -134,7 +134,7 @@ public class DownloadFragment extends AbsFragment<FragmentDownloadBinding> {
} else {
mPb.setProgress((int) ((current * 100) / len));
}
mSpeed.setText(CommonUtil.formatFileSize(task.getSpeed()) + "/s");
mSpeed.setText(task.getConvertSpeed());
}
}
}

@ -103,7 +103,6 @@ final class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapter
long size = item.getFileSize();
int current = 0;
long progress = item.getCurrentProgress();
long speed = item.getSpeed();
current = size == 0 ? 0 : (int) (progress * 100 / size);
holder.progress.setProgress(current);
BtClickListener listener = new BtClickListener(item);
@ -136,7 +135,7 @@ final class DownloadAdapter extends AbsRVAdapter<DownloadEntity, DownloadAdapter
}
holder.bt.setText(str);
holder.bt.setTextColor(getColor(color));
holder.speed.setText(CommonUtil.formatFileSize(speed) + "/s");
holder.speed.setText(item.getConvertSpeed());
holder.fileSize.setText(covertCurrentSize(progress) + "/" + CommonUtil.formatFileSize(size));
holder.cancel.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {

@ -13,7 +13,3 @@
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
#Wed Dec 07 20:19:22 CST 2016
systemProp.http.proxyPassword=7RbgsDfOoBn
systemProp.http.proxyHost=hilton.h.xduotai.com
systemProp.http.proxyUser=duotai
systemProp.http.proxyPort=10969

Loading…
Cancel
Save