Skip to content

Commit 33dbda6

Browse files
committed
add download progress monitor
1 parent 2ecc902 commit 33dbda6

File tree

8 files changed

+63
-23
lines changed

8 files changed

+63
-23
lines changed

app/src/main/java/cn/hadcn/davinci_example/MainActivity.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.util.Log;
88
import android.view.View;
99
import android.widget.ImageView;
10+
import android.widget.TextView;
1011

1112
import com.bumptech.glide.Glide;
1213

@@ -75,7 +76,7 @@ public void onClick(View v) {
7576
JSONObject jsonObject = new JSONObject();
7677
try {
7778
JSONObject header = new JSONObject();
78-
header.put("tokenId", "6240e6e5-10d1-4d1f-83bf-39910870583c");
79+
header.put("tokenId", "0e5495fb-da46-4b28-95ea-e9f6aec1d69a");
7980
jsonObject.put("_header_", header);
8081
} catch (JSONException e) {
8182
e.printStackTrace();
@@ -96,12 +97,14 @@ public void onVinciUploadFailed(String reason) {
9697
DaVinci.with().addThreadPool("one", 1);
9798
DaVinci.with().tag("one").getImageLoader().load("http://y3.ifengimg.com/fashion_spider/dci_2012/02/20a78c36cc31225b1a7efa89f566f591.jpg").into(image3);
9899

99-
OutputStream out = null;
100+
OutputStream out;
100101
try {
101102
out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/download/" + "a.txt");
102103
} catch (FileNotFoundException e) {
103104
e.printStackTrace();
105+
return;
104106
}
107+
final TextView tv = (TextView)findViewById(R.id.test_text);
105108
DaVinci.with().getDownloader().body(jsonObject.toString()).download("http://ec2-52-192-96-229.ap-northeast-1.compute.amazonaws.com:12821/ecp/openapi/qs/file/download/p/2016/07/06/03/f5d28e3065244ab9952858f991838246.txt"
106109
, out, new OnVinciDownloadListener() {
107110
@Override
@@ -113,6 +116,13 @@ public void onVinciDownloadSuccess() {
113116
public void onVinciDownloadFailed(String reason) {
114117

115118
}
119+
120+
@Override
121+
public void onVinciDownloadProgress(int progress) {
122+
VinciLog.e("progress = " + progress);
123+
124+
tv.setText(String.valueOf(progress));
125+
}
116126
});
117127
}
118128

app/src/main/res/layout/activity_main.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,9 @@
5353
android:layout_width="wrap_content"
5454
android:layout_height="wrap_content" />
5555
</LinearLayout>
56-
56+
<TextView
57+
android:id="@+id/test_text"
58+
android:layout_width="wrap_content"
59+
android:layout_height="wrap_content"
60+
android:layout_centerInParent="true"/>
5761
</RelativeLayout>

davinci/src/main/java/cn/hadcn/davinci/image/base/ByteRequest.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package cn.hadcn.davinci.image.base;
1818

19+
import android.os.Handler;
20+
import android.os.Looper;
21+
1922
import java.io.UnsupportedEncodingException;
2023
import java.nio.ByteBuffer;
2124

@@ -34,32 +37,28 @@ public class ByteRequest extends Request<ByteBuffer> {
3437
protected static final String PROTOCOL_CHARSET = "utf-8";
3538

3639
private final Response.Listener<ByteBuffer> mListener;
40+
private Response.ProgressListener mProgressListener;
41+
42+
private Handler mHandler = new Handler(Looper.getMainLooper());
3743

3844
/** Decoding lock so that we don't decode more than one image at a time (to avoid OOM's) */
3945
private static final Object sDecodeLock = new Object();
4046

4147
private final String mRequestBody;
4248

43-
/**
44-
* Creates a new image request, decoding to a maximum specified width and
45-
* height. If both width and height are zero, the image will be decoded to
46-
* its natural size. If one of the two is nonzero, that dimension will be
47-
* clamped and the other one will be set to preserve the image's aspect
48-
* ratio. If both width and height are nonzero, the image will be decoded to
49-
* be fit in the rectangle of dimensions width x height while keeping its
50-
* aspect ratio.
51-
*
52-
* @param url URL of the image
53-
* @param listener Listener to receive the decoded bitmap
54-
* @param errorListener Error listener, or null to ignore errors
55-
*/
49+
5650
public ByteRequest(int method, String url, String requestBody, Response.Listener<ByteBuffer> listener, Response.ErrorListener errorListener) {
5751
super(method, url, errorListener);
5852

5953
mListener = listener;
6054
mRequestBody = requestBody;
6155
}
6256

57+
public ByteRequest(int method, String url, String requestBody, Response.Listener<ByteBuffer> listener, Response.ErrorListener errorListener, Response.ProgressListener progressListener) {
58+
this(method, url, requestBody, listener, errorListener);
59+
mProgressListener = progressListener;
60+
}
61+
6362
@Override
6463
public Priority getPriority() {
6564
return Priority.LOW;
@@ -99,4 +98,14 @@ protected Response<ByteBuffer> parseNetworkResponse(NetworkResponse response) {
9998
protected void deliverResponse(ByteBuffer response) {
10099
mListener.onResponse(response);
101100
}
101+
102+
@Override
103+
public void progressUpdate(final int progress) {
104+
mHandler.post(new Runnable() {
105+
@Override
106+
public void run() {
107+
mProgressListener.onProgressUpdate(progress);
108+
}
109+
});
110+
}
102111
}

davinci/src/main/java/cn/hadcn/davinci/other/OnVinciDownloadListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
public interface OnVinciDownloadListener {
88
void onVinciDownloadSuccess();
99
void onVinciDownloadFailed(String reason);
10+
void onVinciDownloadProgress(int progress);
1011
}

davinci/src/main/java/cn/hadcn/davinci/other/impl/VinciDownload.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ public void onResponse(ByteBuffer response) {
4747
public void onErrorResponse(VolleyError error) {
4848
listener.onVinciDownloadFailed("net failed");
4949
}
50+
}, new Response.ProgressListener() {
51+
@Override
52+
public void onProgressUpdate(int progress) {
53+
listener.onVinciDownloadProgress(progress);
54+
}
5055
});
5156
request.setRetryPolicy(new DefaultRetryPolicy(4 * DefaultRetryPolicy.DEFAULT_TIMEOUT_MS,
5257
DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
5358
mRequestQueue.add(request);
5459
}
55-
56-
public interface Listener {
57-
58-
}
5960
}

davinci/src/main/java/cn/hadcn/davinci/volley/Request.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,14 @@ protected VolleyError parseNetworkError(VolleyError volleyError) {
574574
*/
575575
abstract protected void deliverResponse(T response);
576576

577+
/**
578+
* publish progress
579+
* @param progress progress of http, how many bytes have already been downloaded
580+
*/
581+
public void progressUpdate(int progress) {
582+
583+
}
584+
577585
/**
578586
* Delivers error message to the ErrorListener that the Request was
579587
* initialized with.

davinci/src/main/java/cn/hadcn/davinci/volley/Response.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public interface ErrorListener {
3838
void onErrorResponse(VolleyError error);
3939
}
4040

41+
public interface ProgressListener{
42+
void onProgressUpdate(int progress);
43+
}
44+
4145
/** Returns a successful response containing the parsed result. */
4246
public static <T> Response<T> success(T result, Cache.Entry cacheEntry) {
4347
return new Response<T>(result, cacheEntry);

davinci/src/main/java/cn/hadcn/davinci/volley/toolbox/BasicNetwork.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public NetworkResponse performRequest(Request<?> request) throws VolleyError {
9191
Map<String, String> responseHeaders = Collections.emptyMap();
9292
try {
9393
// Gather headers.
94-
Map<String, String> headers = new HashMap<String, String>();
94+
Map<String, String> headers = new HashMap<>();
9595
addCacheHeaders(headers, request.getCacheEntry());
9696
httpResponse = mHttpStack.performRequest(request, headers);
9797
StatusLine statusLine = httpResponse.getStatusLine();
@@ -120,7 +120,7 @@ public NetworkResponse performRequest(Request<?> request) throws VolleyError {
120120

121121
// Some responses such as 204s do not have content. We must check.
122122
if (httpResponse.getEntity() != null) {
123-
responseContents = entityToBytes(httpResponse.getEntity());
123+
responseContents = entityToBytes(httpResponse.getEntity(), request);
124124
} else {
125125
// Add 0 byte response as a way of honestly representing a
126126
// no-content request.
@@ -234,7 +234,7 @@ protected void logError(String what, String url, long start) {
234234
}
235235

236236
/** Reads the contents of HttpEntity into a byte[]. */
237-
private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError {
237+
private byte[] entityToBytes(HttpEntity entity, Request request) throws IOException, ServerError {
238238
PoolingByteArrayOutputStream bytes =
239239
new PoolingByteArrayOutputStream(mPool, (int) entity.getContentLength());
240240
byte[] buffer = null;
@@ -244,9 +244,12 @@ private byte[] entityToBytes(HttpEntity entity) throws IOException, ServerError
244244
throw new ServerError();
245245
}
246246
buffer = mPool.getBuf(1024);
247+
int progress = 0;
247248
int count;
248249
while ((count = in.read(buffer)) != -1) {
249250
bytes.write(buffer, 0, count);
251+
progress += count;
252+
request.progressUpdate(progress);
250253
}
251254
return bytes.toByteArray();
252255
} finally {

0 commit comments

Comments
 (0)