2
2
3
3
import android .util .Log ;
4
4
import androidx .annotation .NonNull ;
5
+ import androidx .annotation .Nullable ;
5
6
import com .bumptech .glide .load .DataSource ;
6
7
import com .bumptech .glide .load .Encoder ;
7
8
import com .bumptech .glide .load .Key ;
8
9
import com .bumptech .glide .load .data .DataFetcher ;
10
+ import com .bumptech .glide .load .data .DataFetcher .DataCallback ;
9
11
import com .bumptech .glide .load .model .ModelLoader ;
10
12
import com .bumptech .glide .load .model .ModelLoader .LoadData ;
11
13
import com .bumptech .glide .util .LogTime ;
14
+ import com .bumptech .glide .util .Synthetic ;
12
15
import java .util .Collections ;
13
16
14
17
/**
19
22
* <p>Depending on the disk cache strategy, source data may first be written to disk and then loaded
20
23
* from the cache file rather than returned directly.
21
24
*/
22
- class SourceGenerator
23
- implements DataFetcherGenerator ,
24
- DataFetcher .DataCallback <Object >,
25
- DataFetcherGenerator .FetcherReadyCallback {
25
+ class SourceGenerator implements DataFetcherGenerator , DataFetcherGenerator .FetcherReadyCallback {
26
26
private static final String TAG = "SourceGenerator" ;
27
27
28
28
private final DecodeHelper <?> helper ;
@@ -60,12 +60,40 @@ public boolean startNext() {
60
60
&& (helper .getDiskCacheStrategy ().isDataCacheable (loadData .fetcher .getDataSource ())
61
61
|| helper .hasLoadPath (loadData .fetcher .getDataClass ()))) {
62
62
started = true ;
63
- loadData . fetcher . loadData ( helper . getPriority (), this );
63
+ startNextLoad ( loadData );
64
64
}
65
65
}
66
66
return started ;
67
67
}
68
68
69
+ private void startNextLoad (final LoadData <?> toStart ) {
70
+ loadData .fetcher .loadData (
71
+ helper .getPriority (),
72
+ new DataCallback <Object >() {
73
+ @ Override
74
+ public void onDataReady (@ Nullable Object data ) {
75
+ if (isCurrentRequest (toStart )) {
76
+ onDataReadyInternal (toStart , data );
77
+ }
78
+ }
79
+
80
+ @ Override
81
+ public void onLoadFailed (@ NonNull Exception e ) {
82
+ if (isCurrentRequest (toStart )) {
83
+ onLoadFailedInternal (toStart , e );
84
+ }
85
+ }
86
+ });
87
+ }
88
+
89
+ // We want reference equality explicitly to make sure we ignore results from old requests.
90
+ @ SuppressWarnings ({"PMD.CompareObjectsWithEquals" , "WeakerAccess" })
91
+ @ Synthetic
92
+ boolean isCurrentRequest (LoadData <?> requestLoadData ) {
93
+ LoadData <?> currentLoadData = loadData ;
94
+ return currentLoadData != null && currentLoadData == requestLoadData ;
95
+ }
96
+
69
97
private boolean hasNextModelLoader () {
70
98
return loadDataListIndex < helper .getLoadData ().size ();
71
99
}
@@ -107,8 +135,9 @@ public void cancel() {
107
135
}
108
136
}
109
137
110
- @ Override
111
- public void onDataReady (Object data ) {
138
+ @ SuppressWarnings ("WeakerAccess" )
139
+ @ Synthetic
140
+ void onDataReadyInternal (LoadData <?> loadData , Object data ) {
112
141
DiskCacheStrategy diskCacheStrategy = helper .getDiskCacheStrategy ();
113
142
if (data != null && diskCacheStrategy .isDataCacheable (loadData .fetcher .getDataSource ())) {
114
143
dataToCache = data ;
@@ -125,8 +154,9 @@ public void onDataReady(Object data) {
125
154
}
126
155
}
127
156
128
- @ Override
129
- public void onLoadFailed (@ NonNull Exception e ) {
157
+ @ SuppressWarnings ("WeakerAccess" )
158
+ @ Synthetic
159
+ void onLoadFailedInternal (LoadData <?> loadData , @ NonNull Exception e ) {
130
160
cb .onDataFetcherFailed (originalKey , e , loadData .fetcher , loadData .fetcher .getDataSource ());
131
161
}
132
162
0 commit comments