Skip to content

Commit e4a10c5

Browse files
committed
Improve and add speed
1 parent 6af26ea commit e4a10c5

8 files changed

Lines changed: 198 additions & 65 deletions

File tree

app/src/main/java/me/jessyan/progressmanager/demo/MainActivity.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.InputStream;
2121

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

3838
private ImageView mImageView;
@@ -94,10 +94,15 @@ private ProgressListener getGlideListener() {
9494
return new ProgressListener() {
9595
@Override
9696
public void onProgress(ProgressInfo progressInfo) {
97-
int progress = (int) ((100 * progressInfo.getCurrentbytes()) / progressInfo.getContentLength());
97+
int progress = progressInfo.getPercent();
9898
mGlideProgress.setProgress(progress);
9999
mGlideProgressText.setText(progress + "%");
100-
Log.d(TAG, progressInfo.getId() + "--glide--" + progress + " %");
100+
Log.d(TAG, progressInfo.getId() + "--glide--" + progress + " % " + progressInfo.getEachBytes() + " " + progressInfo.getCurrentbytes() + " " + progressInfo.getContentLength());
101+
Log.d(TAG, progressInfo.getSpeed()+" byte/s");
102+
if (progressInfo.isFinish()) {
103+
//说明已经加载完成
104+
Log.d(TAG, "Glide -- finish");
105+
}
101106
}
102107

103108
@Override
@@ -134,10 +139,15 @@ public void onProgress(ProgressInfo progressInfo) {
134139
}
135140

136141

137-
int progress = (int) ((100 * mLastUploadingingInfo.getCurrentbytes()) / mLastUploadingingInfo.getContentLength());
142+
int progress = mLastUploadingingInfo.getPercent();
138143
mUploadProgress.setProgress(progress);
139144
mUploadProgressText.setText(progress + "%");
140-
Log.d(TAG, mLastUploadingingInfo.getId() + "--upload--" + progress + " % " + mLastUploadingingInfo.getCurrentbytes() + " " + mLastUploadingingInfo.getContentLength());
145+
Log.d(TAG, mLastUploadingingInfo.getId() + "--upload--" + progress + " % " + mLastUploadingingInfo.getEachBytes() + " " + mLastUploadingingInfo.getCurrentbytes() + " " + mLastUploadingingInfo.getContentLength());
146+
Log.d(TAG, mLastUploadingingInfo.getSpeed()+" byte/s");
147+
if (mLastUploadingingInfo.isFinish()){
148+
//说明已经上传完成
149+
Log.d(TAG, "Upload -- finish");
150+
}
141151
}
142152

143153
@Override
@@ -172,11 +182,16 @@ public void onProgress(ProgressInfo progressInfo) {
172182
} else if (progressInfo.getId() > mLastDownloadingInfo.getId()) {
173183
mLastDownloadingInfo = progressInfo;
174184
}
175-
// 如果getCurrentbytes 等于 -1 说明二进制已经读取完,可能是成功下载完所有数据,也可能是遭遇了错误
176-
int progress = (int) ((100 * mLastDownloadingInfo.getCurrentbytes()) / mLastDownloadingInfo.getContentLength());
185+
186+
int progress = mLastDownloadingInfo.getPercent();
177187
mDownloadProgress.setProgress(progress);
178188
mDownloadProgressText.setText(progress + "%");
179-
Log.d(TAG, mLastDownloadingInfo.getId() + "--download--" + progress + " % " + mLastDownloadingInfo.getCurrentbytes() + " " + mLastDownloadingInfo.getContentLength());
189+
Log.d(TAG, mLastDownloadingInfo.getId() + "--download--" + progress + " % " + mLastDownloadingInfo.getEachBytes() + " " + mLastDownloadingInfo.getCurrentbytes() + " " + mLastDownloadingInfo.getContentLength());
190+
Log.d(TAG, mLastDownloadingInfo.getSpeed()+" byte/s");
191+
if (mLastDownloadingInfo.isFinish()) {
192+
//说明已经下载完成
193+
Log.d(TAG, "Download -- finish");
194+
}
180195
}
181196

182197
@Override

app/src/main/java/me/jessyan/progressmanager/demo/MainFragment.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import android.widget.ProgressBar;
1313
import android.widget.TextView;
1414

15-
import me.jessyan.progressmanager.ProgressInfo;
15+
import me.jessyan.progressmanager.body.ProgressInfo;
1616
import me.jessyan.progressmanager.ProgressListener;
1717
import me.jessyan.progressmanager.ProgressManager;
1818

@@ -105,7 +105,7 @@ public void onProgress(ProgressInfo progressInfo) {
105105
}
106106

107107

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

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

153157
@Override
@@ -168,7 +172,7 @@ private ProgressListener getGlideListener() {
168172
return new ProgressListener() {
169173
@Override
170174
public void onProgress(ProgressInfo progressInfo) {
171-
int progress = (int) ((100 * progressInfo.getCurrentbytes()) / progressInfo.getContentLength());
175+
int progress = progressInfo.getPercent();
172176
mGlideProgress.setProgress(progress);
173177
mGlideProgressText.setText(progress + "%");
174178
Log.d(TAG, progressInfo.getId() + "--glide--"+progress + " %");

progress/src/main/java/me/jessyan/progressmanager/ProgressInfo.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

progress/src/main/java/me/jessyan/progressmanager/ProgressListener.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package me.jessyan.progressmanager;
22

3+
import me.jessyan.progressmanager.body.ProgressInfo;
4+
35
/**
46
* Created by jess on 02/06/2017 18:23
57
* Contact with jess.yan.effort@gmail.com

progress/src/main/java/me/jessyan/progressmanager/ProgressManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public final class ProgressManager {
3636
private static volatile ProgressManager mProgressManager;
3737

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

4141
static {
4242
boolean hasDependency;
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package me.jessyan.progressmanager.body;
2+
3+
import android.os.Parcel;
4+
import android.os.Parcelable;
5+
6+
/**
7+
* Created by jess on 07/06/2017 12:09
8+
* Contact with jess.yan.effort@gmail.com
9+
*/
10+
11+
public class ProgressInfo implements Parcelable{
12+
private long currentBytes; //当前已上传或下载的总长度
13+
private long contentLength; //数据总长度
14+
private long intervalTime; //本次调用距离上一次被调用所间隔的时间(毫秒)
15+
private long eachBytes; //本次调用距离上一次被调用的间隔时间内上传或下载的byte长度
16+
private long id; //如果同一个 Url 地址,上一次的上传或下载操作都还没结束,
17+
//又开始了新的上传或下载操作(比如用户点击多次点击上传或下载同一个 Url 地址,当然你也可以在上层屏蔽掉用户的重复点击),
18+
//此 id (请求开始时的时间)就变得尤为重要,用来区分正在执行的进度信息,因为是以请求开始时的时间作为 id ,所以值越大,说明该请求越新
19+
private boolean finish; //进度是否完成
20+
21+
22+
public ProgressInfo(long id) {
23+
this.id = id;
24+
}
25+
26+
void setCurrentbytes(long currentbytes) {
27+
this.currentBytes = currentbytes;
28+
}
29+
30+
void setContentLength(long contentLength) {
31+
this.contentLength = contentLength;
32+
}
33+
34+
void setIntervalTime(long intervalTime) {
35+
this.intervalTime = intervalTime;
36+
}
37+
38+
void setEachBytes(long eachBytes) {
39+
this.eachBytes = eachBytes;
40+
}
41+
42+
void setFinish(boolean finish) {
43+
this.finish = finish;
44+
}
45+
46+
public long getCurrentbytes() {
47+
return currentBytes;
48+
}
49+
50+
public long getContentLength() {
51+
return contentLength;
52+
}
53+
54+
public long getIntervalTime() {
55+
return intervalTime;
56+
}
57+
58+
public long getEachBytes() {
59+
return eachBytes;
60+
}
61+
62+
public long getId() {
63+
return id;
64+
}
65+
66+
public boolean isFinish() {
67+
return finish;
68+
}
69+
70+
/**
71+
* 获取百分比,该计算舍去了小数点,如果你想得到更精确的值,请自行计算
72+
*
73+
* @return
74+
*/
75+
public int getPercent() {
76+
if (getCurrentbytes() <= 0 || getContentLength() <= 0) return 0;
77+
return (int) ((100 * getCurrentbytes()) / getContentLength());
78+
}
79+
80+
/**
81+
* 获取上传或下载网络速度,单位为byte/s,如果你想得到更精确的值,请自行计算
82+
*
83+
* @return
84+
*/
85+
public long getSpeed() {
86+
if (getEachBytes() <= 0 || getIntervalTime() <= 0) return 0;
87+
return getEachBytes() * 1000 / getIntervalTime();
88+
}
89+
90+
91+
@Override
92+
public int describeContents() {
93+
return 0;
94+
}
95+
96+
@Override
97+
public void writeToParcel(Parcel dest, int flags) {
98+
dest.writeLong(this.currentBytes);
99+
dest.writeLong(this.contentLength);
100+
dest.writeLong(this.intervalTime);
101+
dest.writeLong(this.eachBytes);
102+
dest.writeLong(this.id);
103+
dest.writeByte(this.finish ? (byte) 1 : (byte) 0);
104+
}
105+
106+
protected ProgressInfo(Parcel in) {
107+
this.currentBytes = in.readLong();
108+
this.contentLength = in.readLong();
109+
this.intervalTime = in.readLong();
110+
this.eachBytes = in.readLong();
111+
this.id = in.readLong();
112+
this.finish = in.readByte() != 0;
113+
}
114+
115+
public static final Creator<ProgressInfo> CREATOR = new Creator<ProgressInfo>() {
116+
@Override
117+
public ProgressInfo createFromParcel(Parcel source) {
118+
return new ProgressInfo(source);
119+
}
120+
121+
@Override
122+
public ProgressInfo[] newArray(int size) {
123+
return new ProgressInfo[size];
124+
}
125+
};
126+
}

progress/src/main/java/me/jessyan/progressmanager/body/ProgressRequestBody.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package me.jessyan.progressmanager.body;
22

33
import android.os.Handler;
4+
import android.os.SystemClock;
45

56
import java.io.IOException;
67
import java.util.List;
78

8-
import me.jessyan.progressmanager.ProgressInfo;
99
import me.jessyan.progressmanager.ProgressListener;
1010
import okhttp3.MediaType;
1111
import okhttp3.RequestBody;
@@ -31,6 +31,7 @@ public class ProgressRequestBody extends RequestBody {
3131
protected final ProgressInfo mProgressInfo;
3232
private BufferedSink mBufferedSink;
3333

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

7779
public CountingSink(Sink delegate) {
7880
super(delegate);
@@ -93,20 +95,30 @@ public void write(Buffer source, long byteCount) throws IOException {
9395
mProgressInfo.setContentLength(contentLength());
9496
}
9597
totalBytesRead += byteCount;
98+
tempSize += byteCount;
9699
if (mListeners != null) {
97-
long curTime = System.currentTimeMillis();
100+
long curTime = SystemClock.elapsedRealtime();
98101
if (curTime - lastRefreshTime >= REFRESH_TIME || totalBytesRead == mProgressInfo.getContentLength()) {
99-
mProgressInfo.setCurrentbytes(totalBytesRead);
102+
final long finalTempSize = tempSize;
103+
final long finalTotalBytesRead = totalBytesRead;
104+
final long finalIntervalTime = curTime - lastRefreshTime;
100105
for (int i = 0; i < mListeners.length; i++) {
101-
final int finalI = i;
106+
final ProgressListener listener = mListeners[i];
102107
mHandler.post(new Runnable() {
103108
@Override
104109
public void run() {
105-
mListeners[finalI].onProgress(mProgressInfo);
110+
// Runnable 里的代码是通过 Handler 执行在主线程的,外面代码可能执行在其他线程
111+
// 所以我必须使用 final ,保证在 Runnable 执行前使用到的变量,在执行时不会被修改
112+
mProgressInfo.setEachBytes(finalTempSize);
113+
mProgressInfo.setCurrentbytes(finalTotalBytesRead);
114+
mProgressInfo.setIntervalTime(finalIntervalTime);
115+
mProgressInfo.setFinish(finalTotalBytesRead == mProgressInfo.getContentLength());
116+
listener.onProgress(mProgressInfo);
106117
}
107118
});
108119
}
109-
lastRefreshTime = System.currentTimeMillis();
120+
lastRefreshTime = curTime;
121+
tempSize = 0;
110122
}
111123
}
112124
}

0 commit comments

Comments
 (0)