Skip to content

Commit

Permalink
Improve and add speed
Browse files Browse the repository at this point in the history
  • Loading branch information
JessYanCoding committed Jun 17, 2017
1 parent 6af26ea commit e4a10c5
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.io.IOException;
import java.io.InputStream;

import me.jessyan.progressmanager.ProgressInfo;
import me.jessyan.progressmanager.body.ProgressInfo;
import me.jessyan.progressmanager.ProgressListener;
import me.jessyan.progressmanager.ProgressManager;
import okhttp3.MediaType;
Expand All @@ -32,7 +32,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
private static final String TAG = "MainActivity";
// github 服务器可能下载不稳定
public static final String IMAGE_URL = "https://raw.githubusercontent.com/JessYanCoding/MVPArmsTemplate/master/art/step.png";
public static final String DOWNLOAD_URL = "https://raw.githubusercontent.com/JessYanCoding/MVPArmsTemplate/master/art/MVPArms.gif";
public static final String DOWNLOAD_URL = "http://inthecheesefactory.com/uploads/source/nestedfragment/fragments.png";
public static final String UPLOAD_URL = "http://upload.qiniu.com/";

private ImageView mImageView;
Expand Down Expand Up @@ -94,10 +94,15 @@ private ProgressListener getGlideListener() {
return new ProgressListener() {
@Override
public void onProgress(ProgressInfo progressInfo) {
int progress = (int) ((100 * progressInfo.getCurrentbytes()) / progressInfo.getContentLength());
int progress = progressInfo.getPercent();
mGlideProgress.setProgress(progress);
mGlideProgressText.setText(progress + "%");
Log.d(TAG, progressInfo.getId() + "--glide--" + progress + " %");
Log.d(TAG, progressInfo.getId() + "--glide--" + progress + " % " + progressInfo.getEachBytes() + " " + progressInfo.getCurrentbytes() + " " + progressInfo.getContentLength());
Log.d(TAG, progressInfo.getSpeed()+" byte/s");
if (progressInfo.isFinish()) {
//说明已经加载完成
Log.d(TAG, "Glide -- finish");
}
}

@Override
Expand Down Expand Up @@ -134,10 +139,15 @@ public void onProgress(ProgressInfo progressInfo) {
}


int progress = (int) ((100 * mLastUploadingingInfo.getCurrentbytes()) / mLastUploadingingInfo.getContentLength());
int progress = mLastUploadingingInfo.getPercent();
mUploadProgress.setProgress(progress);
mUploadProgressText.setText(progress + "%");
Log.d(TAG, mLastUploadingingInfo.getId() + "--upload--" + progress + " % " + mLastUploadingingInfo.getCurrentbytes() + " " + mLastUploadingingInfo.getContentLength());
Log.d(TAG, mLastUploadingingInfo.getId() + "--upload--" + progress + " % " + mLastUploadingingInfo.getEachBytes() + " " + mLastUploadingingInfo.getCurrentbytes() + " " + mLastUploadingingInfo.getContentLength());
Log.d(TAG, mLastUploadingingInfo.getSpeed()+" byte/s");
if (mLastUploadingingInfo.isFinish()){
//说明已经上传完成
Log.d(TAG, "Upload -- finish");
}
}

@Override
Expand Down Expand Up @@ -172,11 +182,16 @@ public void onProgress(ProgressInfo progressInfo) {
} else if (progressInfo.getId() > mLastDownloadingInfo.getId()) {
mLastDownloadingInfo = progressInfo;
}
// 如果getCurrentbytes 等于 -1 说明二进制已经读取完,可能是成功下载完所有数据,也可能是遭遇了错误
int progress = (int) ((100 * mLastDownloadingInfo.getCurrentbytes()) / mLastDownloadingInfo.getContentLength());

int progress = mLastDownloadingInfo.getPercent();
mDownloadProgress.setProgress(progress);
mDownloadProgressText.setText(progress + "%");
Log.d(TAG, mLastDownloadingInfo.getId() + "--download--" + progress + " % " + mLastDownloadingInfo.getCurrentbytes() + " " + mLastDownloadingInfo.getContentLength());
Log.d(TAG, mLastDownloadingInfo.getId() + "--download--" + progress + " % " + mLastDownloadingInfo.getEachBytes() + " " + mLastDownloadingInfo.getCurrentbytes() + " " + mLastDownloadingInfo.getContentLength());
Log.d(TAG, mLastDownloadingInfo.getSpeed()+" byte/s");
if (mLastDownloadingInfo.isFinish()) {
//说明已经下载完成
Log.d(TAG, "Download -- finish");
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import android.widget.ProgressBar;
import android.widget.TextView;

import me.jessyan.progressmanager.ProgressInfo;
import me.jessyan.progressmanager.body.ProgressInfo;
import me.jessyan.progressmanager.ProgressListener;
import me.jessyan.progressmanager.ProgressManager;

Expand Down Expand Up @@ -105,7 +105,7 @@ public void onProgress(ProgressInfo progressInfo) {
}


int progress = (int) ((100 * mLastUploadingingInfo.getCurrentbytes()) / mLastUploadingingInfo.getContentLength());
int progress = mLastUploadingingInfo.getPercent();
mUploadProgress.setProgress(progress);
mUploadProgressText.setText(progress + "%");
Log.d(TAG, mLastUploadingingInfo.getId() + "--upload--" + progress + " %");
Expand Down Expand Up @@ -144,10 +144,14 @@ public void onProgress(ProgressInfo progressInfo) {
mLastDownloadingInfo = progressInfo;
}

int progress = (int) ((100 * mLastDownloadingInfo.getCurrentbytes()) / mLastDownloadingInfo.getContentLength());
int progress = mLastDownloadingInfo.getPercent();
mDownloadProgress.setProgress(progress);
mDownloadProgressText.setText(progress + "%");
Log.d(TAG, mLastDownloadingInfo.getId() + "--download--" + progress + " %");
if (mLastDownloadingInfo.isFinish()) {
//说明已经下载完成
Log.d(TAG, "Download -- finish");
}
}

@Override
Expand All @@ -168,7 +172,7 @@ private ProgressListener getGlideListener() {
return new ProgressListener() {
@Override
public void onProgress(ProgressInfo progressInfo) {
int progress = (int) ((100 * progressInfo.getCurrentbytes()) / progressInfo.getContentLength());
int progress = progressInfo.getPercent();
mGlideProgress.setProgress(progress);
mGlideProgressText.setText(progress + "%");
Log.d(TAG, progressInfo.getId() + "--glide--"+progress + " %");
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.jessyan.progressmanager;

import me.jessyan.progressmanager.body.ProgressInfo;

/**
* Created by jess on 02/06/2017 18:23
* Contact with jess.yan.effort@gmail.com
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public final class ProgressManager {
private static volatile ProgressManager mProgressManager;

public static final boolean DEPENDENCY_OKHTTP;
public static int REFRESH_TIME = 150; //回调刷新时间(单位ms),避免高频率调用
public static int REFRESH_TIME = 200; //回调刷新时间(单位ms),避免高频率调用

static {
boolean hasDependency;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package me.jessyan.progressmanager.body;

import android.os.Parcel;
import android.os.Parcelable;

/**
* Created by jess on 07/06/2017 12:09
* Contact with jess.yan.effort@gmail.com
*/

public class ProgressInfo implements Parcelable{
private long currentBytes; //当前已上传或下载的总长度
private long contentLength; //数据总长度
private long intervalTime; //本次调用距离上一次被调用所间隔的时间(毫秒)
private long eachBytes; //本次调用距离上一次被调用的间隔时间内上传或下载的byte长度
private long id; //如果同一个 Url 地址,上一次的上传或下载操作都还没结束,
//又开始了新的上传或下载操作(比如用户点击多次点击上传或下载同一个 Url 地址,当然你也可以在上层屏蔽掉用户的重复点击),
//此 id (请求开始时的时间)就变得尤为重要,用来区分正在执行的进度信息,因为是以请求开始时的时间作为 id ,所以值越大,说明该请求越新
private boolean finish; //进度是否完成


public ProgressInfo(long id) {
this.id = id;
}

void setCurrentbytes(long currentbytes) {
this.currentBytes = currentbytes;
}

void setContentLength(long contentLength) {
this.contentLength = contentLength;
}

void setIntervalTime(long intervalTime) {
this.intervalTime = intervalTime;
}

void setEachBytes(long eachBytes) {
this.eachBytes = eachBytes;
}

void setFinish(boolean finish) {
this.finish = finish;
}

public long getCurrentbytes() {
return currentBytes;
}

public long getContentLength() {
return contentLength;
}

public long getIntervalTime() {
return intervalTime;
}

public long getEachBytes() {
return eachBytes;
}

public long getId() {
return id;
}

public boolean isFinish() {
return finish;
}

/**
* 获取百分比,该计算舍去了小数点,如果你想得到更精确的值,请自行计算
*
* @return
*/
public int getPercent() {
if (getCurrentbytes() <= 0 || getContentLength() <= 0) return 0;
return (int) ((100 * getCurrentbytes()) / getContentLength());
}

/**
* 获取上传或下载网络速度,单位为byte/s,如果你想得到更精确的值,请自行计算
*
* @return
*/
public long getSpeed() {
if (getEachBytes() <= 0 || getIntervalTime() <= 0) return 0;
return getEachBytes() * 1000 / getIntervalTime();
}


@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(this.currentBytes);
dest.writeLong(this.contentLength);
dest.writeLong(this.intervalTime);
dest.writeLong(this.eachBytes);
dest.writeLong(this.id);
dest.writeByte(this.finish ? (byte) 1 : (byte) 0);
}

protected ProgressInfo(Parcel in) {
this.currentBytes = in.readLong();
this.contentLength = in.readLong();
this.intervalTime = in.readLong();
this.eachBytes = in.readLong();
this.id = in.readLong();
this.finish = in.readByte() != 0;
}

public static final Creator<ProgressInfo> CREATOR = new Creator<ProgressInfo>() {
@Override
public ProgressInfo createFromParcel(Parcel source) {
return new ProgressInfo(source);
}

@Override
public ProgressInfo[] newArray(int size) {
return new ProgressInfo[size];
}
};
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package me.jessyan.progressmanager.body;

import android.os.Handler;
import android.os.SystemClock;

import java.io.IOException;
import java.util.List;

import me.jessyan.progressmanager.ProgressInfo;
import me.jessyan.progressmanager.ProgressListener;
import okhttp3.MediaType;
import okhttp3.RequestBody;
Expand All @@ -31,6 +31,7 @@ public class ProgressRequestBody extends RequestBody {
protected final ProgressInfo mProgressInfo;
private BufferedSink mBufferedSink;


public ProgressRequestBody(Handler handler, RequestBody delegate, List<ProgressListener> listeners) {
this.mDelegate = delegate;
this.mListeners = listeners.toArray(new ProgressListener[listeners.size()]);
Expand Down Expand Up @@ -73,6 +74,7 @@ public void writeTo(BufferedSink sink) throws IOException {
protected final class CountingSink extends ForwardingSink {
private long totalBytesRead = 0L;
private long lastRefreshTime = 0L; //最后一次刷新的时间
private long tempSize = 0L;

public CountingSink(Sink delegate) {
super(delegate);
Expand All @@ -93,20 +95,30 @@ public void write(Buffer source, long byteCount) throws IOException {
mProgressInfo.setContentLength(contentLength());
}
totalBytesRead += byteCount;
tempSize += byteCount;
if (mListeners != null) {
long curTime = System.currentTimeMillis();
long curTime = SystemClock.elapsedRealtime();
if (curTime - lastRefreshTime >= REFRESH_TIME || totalBytesRead == mProgressInfo.getContentLength()) {
mProgressInfo.setCurrentbytes(totalBytesRead);
final long finalTempSize = tempSize;
final long finalTotalBytesRead = totalBytesRead;
final long finalIntervalTime = curTime - lastRefreshTime;
for (int i = 0; i < mListeners.length; i++) {
final int finalI = i;
final ProgressListener listener = mListeners[i];
mHandler.post(new Runnable() {
@Override
public void run() {
mListeners[finalI].onProgress(mProgressInfo);
// Runnable 里的代码是通过 Handler 执行在主线程的,外面代码可能执行在其他线程
// 所以我必须使用 final ,保证在 Runnable 执行前使用到的变量,在执行时不会被修改
mProgressInfo.setEachBytes(finalTempSize);
mProgressInfo.setCurrentbytes(finalTotalBytesRead);
mProgressInfo.setIntervalTime(finalIntervalTime);
mProgressInfo.setFinish(finalTotalBytesRead == mProgressInfo.getContentLength());
listener.onProgress(mProgressInfo);
}
});
}
lastRefreshTime = System.currentTimeMillis();
lastRefreshTime = curTime;
tempSize = 0;
}
}
}
Expand Down
Loading

0 comments on commit e4a10c5

Please sign in to comment.