Skip to content

Commit fe06f62

Browse files
committed
Improve ProgressManager
1 parent 244f447 commit fe06f62

File tree

3 files changed

+111
-31
lines changed

3 files changed

+111
-31
lines changed

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

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* Copyright 2017 JessYan
3-
*
3+
* <p>
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -42,6 +42,7 @@
4242
import okhttp3.Request;
4343
import okhttp3.RequestBody;
4444
import okhttp3.Response;
45+
4546
/**
4647
* ================================================
4748
* Created by JessYan on 08/06/2017 12:59
@@ -73,6 +74,9 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
7374
private ProgressInfo mLastDownloadingInfo;
7475
private ProgressInfo mLastUploadingingInfo;
7576
private Handler mHandler;
77+
private String mNewImageUrl;
78+
private String mNewDownloadUrl;
79+
private String mNewUploadUrl;
7680

7781

7882
@Override
@@ -103,15 +107,15 @@ private void initView() {
103107

104108
private void initListener() {
105109
//Glide 加载监听
106-
ProgressManager.getInstance().addResponseListener(IMAGE_URL, getGlideListener());
110+
mNewImageUrl = ProgressManager.getInstance().addDiffResponseListenerOnSameUrl(IMAGE_URL, getGlideListener());
107111

108112

109113
//Okhttp/Retofit 下载监听
110-
ProgressManager.getInstance().addResponseListener(DOWNLOAD_URL, getDownloadListener());
114+
mNewDownloadUrl = ProgressManager.getInstance().addDiffResponseListenerOnSameUrl(DOWNLOAD_URL, getDownloadListener());
111115

112116

113117
//Okhttp/Retofit 上传监听
114-
ProgressManager.getInstance().addRequestListener(UPLOAD_URL, getUploadListener());
118+
mNewUploadUrl = ProgressManager.getInstance().addDiffRequestListenerOnSameUrl(UPLOAD_URL, getUploadListener());
115119
}
116120

117121

@@ -259,7 +263,7 @@ public void run() {
259263
writeToFile(getAssets().open("a.java"), file);
260264

261265
Request request = new Request.Builder()
262-
.url(UPLOAD_URL)
266+
.url(mNewUploadUrl)
263267
.post(RequestBody.create(MediaType.parse("multipart/form-data"), file))
264268
.build();
265269

@@ -268,7 +272,7 @@ public void run() {
268272
} catch (IOException e) {
269273
e.printStackTrace();
270274
//当外部发生错误时,使用此方法可以通知所有监听器的 onError 方法
271-
ProgressManager.getInstance().notifyOnErorr(UPLOAD_URL, e);
275+
ProgressManager.getInstance().notifyOnErorr(mNewUploadUrl, e);
272276
}
273277
}
274278
}).start();
@@ -283,7 +287,7 @@ private void downloadStart() {
283287
public void run() {
284288
try {
285289
Request request = new Request.Builder()
286-
.url(DOWNLOAD_URL)
290+
.url(mNewDownloadUrl)
287291
.build();
288292

289293
Response response = mOkHttpClient.newCall(request).execute();
@@ -307,7 +311,7 @@ public void run() {
307311
} catch (IOException e) {
308312
e.printStackTrace();
309313
//当外部发生错误时,使用此方法可以通知所有监听器的 onError 方法
310-
ProgressManager.getInstance().notifyOnErorr(DOWNLOAD_URL, e);
314+
ProgressManager.getInstance().notifyOnErorr(mNewDownloadUrl, e);
311315
}
312316
}
313317
}).start();
@@ -319,13 +323,20 @@ public void run() {
319323
*/
320324
private void glideStart() {
321325
GlideApp.with(this)
322-
.load(IMAGE_URL)
326+
.load(mNewImageUrl)
323327
.centerCrop()
324328
.placeholder(R.color.colorPrimary)
325329
.diskCacheStrategy(DiskCacheStrategy.NONE)
326330
.into(mImageView);
327331
}
328332

333+
@Override
334+
protected void onDestroy() {
335+
super.onDestroy();
336+
mNewImageUrl = null;// TODO: 10/10/2017 释放资源
337+
mNewDownloadUrl = null;
338+
mNewUploadUrl = null;
339+
}
329340

330341
public static File writeToFile(InputStream in, File file) throws IOException {
331342
FileOutputStream out = new FileOutputStream(file);

app/src/test/java/me/jessyan/progressmanager/demo/ExampleUnitTest.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
import java.util.WeakHashMap;
88
import java.util.concurrent.TimeUnit;
99

10+
import me.jessyan.progressmanager.ProgressListener;
11+
import me.jessyan.progressmanager.ProgressManager;
12+
import me.jessyan.progressmanager.body.ProgressInfo;
13+
import okhttp3.HttpUrl;
14+
import okhttp3.Request;
15+
1016
/**
1117
* Example local unit test, which will execute on the development machine (host).
1218
*
@@ -16,7 +22,35 @@ public class ExampleUnitTest {
1622

1723
@Test
1824
public void test(){
19-
testWeakHashMapAPIs();
25+
// testWeakHashMapAPIs();
26+
testString();
27+
}
28+
29+
private void testRequest(){
30+
String newUrl = ProgressManager.getInstance().addDiffResponseListenerOnSameUrl("http://www.baidu.com/user", new ProgressListener() {
31+
@Override
32+
public void onProgress(ProgressInfo progressInfo) {
33+
34+
}
35+
36+
@Override
37+
public void onError(long id, Exception e) {
38+
39+
}
40+
});
41+
Request request = new Request
42+
.Builder()
43+
.url(newUrl)
44+
.build();
45+
ProgressManager.getInstance().wrapRequestBody(request);
46+
}
47+
48+
private void testString(){
49+
String s = "http://www.baidu.com/user$JessYan$456";
50+
String substring = s.substring(0, s.indexOf("$JessYan$"));
51+
System.out.println(HttpUrl.parse("http://www.baidu.com/abc/d").toString());
52+
System.out.println(substring);
53+
System.out.println(s);
2054
}
2155

2256
private void testWeakHashMapAPIs() {

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

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public final class ProgressManager {
7171
public static final String OKHTTP_PACKAGE_NAME = "okhttp3.OkHttpClient";
7272
public static final boolean DEPENDENCY_OKHTTP;
7373
public static final int DEFAULT_REFRESH_TIME = 150;
74-
public static final String IDENTIFICATION_NUMBER = "$JessYan$";
74+
public static final String IDENTIFICATION_NUMBER = "?JessYan=";
75+
public static final String IDENTIFICATION_HEADER = "JessYan";
7576

7677

7778
static {
@@ -191,10 +192,14 @@ public OkHttpClient.Builder with(OkHttpClient.Builder builder) {
191192
* @return
192193
*/
193194
public Request wrapRequestBody(Request request) {
194-
if (request == null || request.body() == null)
195+
if (request == null)
195196
return request;
196197

197198
String key = request.url().toString();
199+
request = pruneIdentification(key, request);
200+
201+
if (request.body() == null)
202+
return request;
198203
if (mRequestListeners.containsKey(key)) {
199204
List<ProgressListener> listeners = mRequestListeners.get(key);
200205
return request.newBuilder()
@@ -204,6 +209,16 @@ public Request wrapRequestBody(Request request) {
204209
return request;
205210
}
206211

212+
private Request pruneIdentification(String url, Request request) {
213+
boolean needPrune = url.contains(IDENTIFICATION_NUMBER);
214+
if (!needPrune)
215+
return request;
216+
return request.newBuilder()
217+
.url(url.substring(0, url.indexOf(IDENTIFICATION_NUMBER)))
218+
.header(IDENTIFICATION_HEADER, url)
219+
.build();
220+
}
221+
207222
/**
208223
* 将 {@link Response} 传入,配置一些本框架需要的参数,常用于自定义 {@link Interceptor}
209224
* 如已使用 {@link ProgressManager#with(OkHttpClient.Builder)},就不会用到此方法
@@ -212,16 +227,23 @@ public Request wrapRequestBody(Request request) {
212227
* @return
213228
*/
214229
public Response wrapResponseBody(Response response) {
215-
if (response == null || response.body() == null)
230+
if (response == null)
216231
return response;
217232

233+
String key = response.request().url().toString();
234+
if (!TextUtils.isEmpty(response.request().header(IDENTIFICATION_HEADER))) {
235+
key = response.request().header(IDENTIFICATION_HEADER);
236+
}
237+
218238
if (haveRedirect(response)) {
219-
resolveRedirect(mRequestListeners, response);
220-
resolveRedirect(mResponseListeners, response);
239+
resolveRedirect(mRequestListeners, response, key);
240+
resolveRedirect(mResponseListeners, response, key);
221241
return response;
222242
}
223243

224-
String key = response.request().url().toString();
244+
if (response.body() == null)
245+
return response;
246+
225247
if (mResponseListeners.containsKey(key)) {
226248
List<ProgressListener> listeners = mResponseListeners.get(key);
227249
return response.newBuilder()
@@ -248,7 +270,7 @@ public Response wrapResponseBody(Response response) {
248270
* @return 加入了时间戳的新的 {@code url}
249271
*/
250272
public String addDiffResponseListenerOnSameUrl(String originUrl, ProgressListener listener) {
251-
return addDiffResponseListenerOnSameUrl(originUrl, String.valueOf(SystemClock.elapsedRealtime()), listener);
273+
return addDiffResponseListenerOnSameUrl(originUrl, String.valueOf(SystemClock.elapsedRealtime() + listener.hashCode()), listener);
252274
}
253275

254276
/**
@@ -264,6 +286,7 @@ public String addDiffResponseListenerOnSameUrl(String originUrl, ProgressListene
264286
* <p>
265287
* Example usage:
266288
* <pre> {@code
289+
*
267290
* String newUrl = ProgressManager.getInstance().addDiffResponseListenerOnSameUrl(DOWNLOAD_URL, "id", getDownloadListener());
268291
* new Thread(new Runnable() {
269292
* @Override
@@ -276,11 +299,12 @@ public String addDiffResponseListenerOnSameUrl(String originUrl, ProgressListene
276299
* Response response = mOkHttpClient.newCall(request).execute();
277300
* } catch (IOException e) {
278301
* e.printStackTrace();
279-
* //当外部发生错误时,使用此方法可以通知所有监听器的 onError 方法
280-
* ProgressManager.getInstance().notifyOnErorr(DOWNLOAD_URL, e);
302+
* //当外部发生错误时,使用此方法可以通知所有监听器的 onError 方法,这里也要使用 newUrl
303+
* ProgressManager.getInstance().notifyOnErorr(newUrl, e);
281304
* }
282305
* }
283306
* }).start();
307+
*
284308
* } </pre>
285309
*
286310
* @param originUrl {@code originUrl} 作为基础并结合 {@code key} 用于生成新的 {@code url} 作为标识符
@@ -311,7 +335,7 @@ public String addDiffResponseListenerOnSameUrl(String originUrl, String key, Pro
311335
* @return 加入了时间戳的新的 {@code url}
312336
*/
313337
public String addDiffRequestListenerOnSameUrl(String originUrl, ProgressListener listener) {
314-
return addDiffRequestListenerOnSameUrl(originUrl, String.valueOf(SystemClock.elapsedRealtime()), listener);
338+
return addDiffRequestListenerOnSameUrl(originUrl, String.valueOf(SystemClock.elapsedRealtime() + listener.hashCode()), listener);
315339
}
316340

317341

@@ -328,6 +352,7 @@ public String addDiffRequestListenerOnSameUrl(String originUrl, ProgressListener
328352
* <p>
329353
* Example usage:
330354
* <pre> {@code
355+
*
331356
* String newUrl = ProgressManager.getInstance().addDiffRequestListenerOnSameUrl(UPLOAD_URL, "id", getUploadListener());
332357
* new Thread(new Runnable() {
333358
* @Override
@@ -343,11 +368,12 @@ public String addDiffRequestListenerOnSameUrl(String originUrl, ProgressListener
343368
* Response response = mOkHttpClient.newCall(request).execute();
344369
* } catch (IOException e) {
345370
* e.printStackTrace();
346-
* //当外部发生错误时,使用此方法可以通知所有监听器的 onError 方法
347-
* ProgressManager.getInstance().notifyOnErorr(DOWNLOAD_URL, e);
371+
* //当外部发生错误时,使用此方法可以通知所有监听器的 onError 方法,这里也要使用 newUrl
372+
* ProgressManager.getInstance().notifyOnErorr(newUrl, e);
348373
* }
349374
* }
350375
* }).start();
376+
*
351377
* } </pre>
352378
*
353379
* @param originUrl {@code originUrl} 作为基础并结合 {@code key} 用于生成新的 {@code url} 作为标识符
@@ -368,7 +394,7 @@ public String addDiffRequestListenerOnSameUrl(String originUrl, String key, Prog
368394
* @return
369395
*/
370396
private boolean haveRedirect(Response response) {
371-
String status = response.header("Status");
397+
String status = String.valueOf(response.code());
372398
if (TextUtils.isEmpty(status))
373399
return false;
374400
if (status.contains("301") || status.contains("302") || status.contains("303") || status.contains("307")) {
@@ -382,14 +408,23 @@ private boolean haveRedirect(Response response) {
382408
*
383409
* @param map {@link #mRequestListeners} 或者 {@link #mResponseListeners}
384410
* @param response 原始的 {@link Response}
411+
* @param url {@code url} 地址
385412
*/
386-
private void resolveRedirect(Map<String, List<ProgressListener>> map, Response response) {
387-
String url = response.request().url().toString();
413+
private void resolveRedirect(Map<String, List<ProgressListener>> map, Response response, String url) {
388414
List<ProgressListener> progressListeners = map.get(url); //查看此重定向 url ,是否已经注册过监听器
389-
if (progressListeners != null) {
415+
if (progressListeners != null && progressListeners.size() > 0) {
390416
String location = response.header("Location");// 重定向地址
391-
if (!TextUtils.isEmpty(location) && !map.containsKey(location)) {
392-
map.put(location, progressListeners); //将需要重定向地址的监听器,提供给重定向地址,保证重定向后也可以监听进度
417+
if (!TextUtils.isEmpty(location)) {
418+
if (!map.containsKey(location)) {
419+
map.put(location, progressListeners); //将需要重定向地址的监听器,提供给重定向地址,保证重定向后也可以监听进度
420+
} else {
421+
List<ProgressListener> locationListener = map.get(location);
422+
for (ProgressListener listener : progressListeners) {
423+
if (!locationListener.contains(listener)) {
424+
locationListener.add(listener);
425+
}
426+
}
427+
}
393428
}
394429
}
395430
}

0 commit comments

Comments
 (0)