15
15
*/
16
16
package com .squareup .picasso ;
17
17
18
- import android .content .Context ;
19
18
import android .graphics .Bitmap ;
20
- import android .graphics .BitmapFactory ;
21
19
import android .graphics .Matrix ;
22
20
import android .net .NetworkInfo ;
23
- import android .net .Uri ;
24
- import android .provider .MediaStore ;
25
21
import java .io .IOException ;
26
22
import java .io .PrintWriter ;
27
23
import java .io .StringWriter ;
28
24
import java .util .ArrayList ;
29
25
import java .util .List ;
30
26
import java .util .concurrent .Future ;
31
27
32
- import static android .content .ContentResolver .SCHEME_ANDROID_RESOURCE ;
33
- import static android .content .ContentResolver .SCHEME_CONTENT ;
34
- import static android .content .ContentResolver .SCHEME_FILE ;
35
- import static android .provider .ContactsContract .Contacts ;
36
- import static com .squareup .picasso .AssetBitmapHunter .ANDROID_ASSET ;
37
28
import static com .squareup .picasso .Picasso .LoadedFrom .MEMORY ;
38
29
import static com .squareup .picasso .Utils .OWNER_HUNTER ;
39
30
import static com .squareup .picasso .Utils .VERB_DECODED ;
44
35
import static com .squareup .picasso .Utils .getLogIdsForHunter ;
45
36
import static com .squareup .picasso .Utils .log ;
46
37
47
- abstract class BitmapHunter implements Runnable {
48
-
38
+ class BitmapHunter implements Runnable {
49
39
/**
50
40
* Global lock for bitmap decoding to ensure that we are only are decoding one at a time. Since
51
41
* this will only ever happen in background threads we help avoid excessive memory thrashing as
@@ -66,6 +56,7 @@ abstract class BitmapHunter implements Runnable {
66
56
final String key ;
67
57
final Request data ;
68
58
final boolean skipMemoryCache ;
59
+ final RequestHandler requestHandler ;
69
60
70
61
Action action ;
71
62
List <Action > actions ;
@@ -74,22 +65,22 @@ abstract class BitmapHunter implements Runnable {
74
65
Picasso .LoadedFrom loadedFrom ;
75
66
Exception exception ;
76
67
int exifRotation ; // Determined during decoding of original resource.
68
+ int retryCount ;
77
69
78
- BitmapHunter (Picasso picasso , Dispatcher dispatcher , Cache cache , Stats stats , Action action ) {
70
+ BitmapHunter (Picasso picasso , Dispatcher dispatcher , Cache cache , Stats stats , Action action ,
71
+ RequestHandler requestHandler ) {
79
72
this .picasso = picasso ;
80
73
this .dispatcher = dispatcher ;
81
74
this .cache = cache ;
82
75
this .stats = stats ;
83
76
this .key = action .getKey ();
84
77
this .data = action .getRequest ();
85
78
this .skipMemoryCache = action .skipCache ;
79
+ this .requestHandler = requestHandler ;
80
+ this .retryCount = requestHandler .getRetryCount ();
86
81
this .action = action ;
87
82
}
88
83
89
- protected void setExifRotation (int exifRotation ) {
90
- this .exifRotation = exifRotation ;
91
- }
92
-
93
84
@ Override public void run () {
94
85
try {
95
86
updateThreadName (data );
@@ -124,10 +115,8 @@ protected void setExifRotation(int exifRotation) {
124
115
}
125
116
}
126
117
127
- abstract Bitmap decode (Request data ) throws IOException ;
128
-
129
118
Bitmap hunt () throws IOException {
130
- Bitmap bitmap ;
119
+ Bitmap bitmap = null ;
131
120
132
121
if (!skipMemoryCache ) {
133
122
bitmap = cache .get (key );
@@ -141,7 +130,13 @@ Bitmap hunt() throws IOException {
141
130
}
142
131
}
143
132
144
- bitmap = decode (data );
133
+ data .loadFromLocalCacheOnly = (retryCount == 0 );
134
+ RequestHandler .Result result = requestHandler .load (data );
135
+ if (result != null ) {
136
+ bitmap = result .getBitmap ();
137
+ loadedFrom = result .getLoadedFrom ();
138
+ exifRotation = result .getExifOrientation ();
139
+ }
145
140
146
141
if (bitmap != null ) {
147
142
if (picasso .loggingEnabled ) {
@@ -227,11 +222,16 @@ boolean shouldSkipMemoryCache() {
227
222
}
228
223
229
224
boolean shouldRetry (boolean airplaneMode , NetworkInfo info ) {
230
- return false ;
225
+ boolean hasRetries = retryCount > 0 ;
226
+ if (!hasRetries ) {
227
+ return false ;
228
+ }
229
+ retryCount --;
230
+ return requestHandler .shouldRetry (airplaneMode , info );
231
231
}
232
232
233
233
boolean supportsReplay () {
234
- return false ;
234
+ return requestHandler . supportsReplay () ;
235
235
}
236
236
237
237
Bitmap getResult () {
@@ -276,70 +276,20 @@ static void updateThreadName(Request data) {
276
276
Thread .currentThread ().setName (builder .toString ());
277
277
}
278
278
279
- static BitmapHunter forRequest (Context context , Picasso picasso , Dispatcher dispatcher ,
280
- Cache cache , Stats stats , Action action , Downloader downloader ) {
281
- if (action .getRequest ().resourceId != 0 ) {
282
- return new ResourceBitmapHunter (context , picasso , dispatcher , cache , stats , action );
283
- }
284
- Uri uri = action .getRequest ().uri ;
285
- String scheme = uri .getScheme ();
286
- if (SCHEME_CONTENT .equals (scheme )) {
287
- if (Contacts .CONTENT_URI .getHost ().equals (uri .getHost ()) //
288
- && !uri .getPathSegments ().contains (Contacts .Photo .CONTENT_DIRECTORY )) {
289
- return new ContactsPhotoBitmapHunter (context , picasso , dispatcher , cache , stats , action );
290
- } else if (MediaStore .AUTHORITY .equals (uri .getAuthority ())) {
291
- return new MediaStoreBitmapHunter (context , picasso , dispatcher , cache , stats , action );
292
- } else {
293
- return new ContentStreamBitmapHunter (context , picasso , dispatcher , cache , stats , action );
294
- }
295
- } else if (SCHEME_FILE .equals (scheme )) {
296
- if (!uri .getPathSegments ().isEmpty () && ANDROID_ASSET .equals (uri .getPathSegments ().get (0 ))) {
297
- return new AssetBitmapHunter (context , picasso , dispatcher , cache , stats , action );
298
- }
299
- return new FileBitmapHunter (context , picasso , dispatcher , cache , stats , action );
300
- } else if (SCHEME_ANDROID_RESOURCE .equals (scheme )) {
301
- return new ResourceBitmapHunter (context , picasso , dispatcher , cache , stats , action );
302
- } else {
303
- return new NetworkBitmapHunter (picasso , dispatcher , cache , stats , action , downloader );
304
- }
305
- }
279
+ static BitmapHunter forRequest (Picasso picasso , Dispatcher dispatcher ,
280
+ Cache cache , Stats stats , Action action ) {
281
+ Request request = action .getRequest ();
306
282
307
- /**
308
- * Lazily create {@link android.graphics.BitmapFactory.Options} based in given
309
- * {@link com.squareup.picasso.Request}, only instantiating them if needed.
310
- */
311
- static BitmapFactory .Options createBitmapOptions (Request data ) {
312
- final boolean justBounds = data .hasSize ();
313
- final boolean hasConfig = data .config != null ;
314
- BitmapFactory .Options options = null ;
315
- if (justBounds || hasConfig ) {
316
- options = new BitmapFactory .Options ();
317
- options .inJustDecodeBounds = justBounds ;
318
- if (hasConfig ) {
319
- options .inPreferredConfig = data .config ;
283
+ List <RequestHandler > requestHandlers = picasso .getRequestHandlers ();
284
+ final int count = requestHandlers .size ();
285
+ for (int i = 0 ; i < count ; i ++) {
286
+ RequestHandler requestHandler = requestHandlers .get (i );
287
+ if (requestHandler .canHandleRequest (request )) {
288
+ return new BitmapHunter (picasso , dispatcher , cache , stats , action , requestHandler );
320
289
}
321
290
}
322
- return options ;
323
- }
324
291
325
- static boolean requiresInSampleSize (BitmapFactory .Options options ) {
326
- return options != null && options .inJustDecodeBounds ;
327
- }
328
-
329
- static void calculateInSampleSize (int reqWidth , int reqHeight , BitmapFactory .Options options ) {
330
- calculateInSampleSize (reqWidth , reqHeight , options .outWidth , options .outHeight , options );
331
- }
332
-
333
- static void calculateInSampleSize (int reqWidth , int reqHeight , int width , int height ,
334
- BitmapFactory .Options options ) {
335
- int sampleSize = 1 ;
336
- if (height > reqHeight || width > reqWidth ) {
337
- final int heightRatio = (int ) Math .floor ((float ) height / (float ) reqHeight );
338
- final int widthRatio = (int ) Math .floor ((float ) width / (float ) reqWidth );
339
- sampleSize = heightRatio < widthRatio ? heightRatio : widthRatio ;
340
- }
341
- options .inSampleSize = sampleSize ;
342
- options .inJustDecodeBounds = false ;
292
+ throw new IllegalStateException ("Unrecognized type of request: " + request );
343
293
}
344
294
345
295
static Bitmap applyCustomTransformations (List <Transformation > transformations , Bitmap result ) {
0 commit comments