|
16 | 16 | import android.support.annotation.Nullable;
|
17 | 17 | import android.support.annotation.RawRes;
|
18 | 18 | import android.view.View;
|
| 19 | +import com.bumptech.glide.load.DataSource; |
19 | 20 | import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
| 21 | +import com.bumptech.glide.load.engine.GlideException; |
20 | 22 | import com.bumptech.glide.load.resource.gif.GifDrawable;
|
21 | 23 | import com.bumptech.glide.manager.ConnectivityMonitor;
|
22 | 24 | import com.bumptech.glide.manager.ConnectivityMonitorFactory;
|
|
26 | 28 | import com.bumptech.glide.manager.RequestTracker;
|
27 | 29 | import com.bumptech.glide.manager.TargetTracker;
|
28 | 30 | import com.bumptech.glide.request.Request;
|
| 31 | +import com.bumptech.glide.request.RequestListener; |
29 | 32 | import com.bumptech.glide.request.RequestOptions;
|
30 | 33 | import com.bumptech.glide.request.target.Target;
|
31 | 34 | import com.bumptech.glide.request.target.ViewTarget;
|
|
34 | 37 | import com.bumptech.glide.util.Util;
|
35 | 38 | import java.io.File;
|
36 | 39 | import java.net.URL;
|
| 40 | +import java.util.List; |
| 41 | +import java.util.concurrent.CopyOnWriteArrayList; |
37 | 42 |
|
38 | 43 | /**
|
39 | 44 | * A class for managing and starting requests for Glide. Can use activity, fragment and connectivity
|
@@ -69,6 +74,10 @@ public void run() {
|
69 | 74 | };
|
70 | 75 | private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
71 | 76 | private final ConnectivityMonitor connectivityMonitor;
|
| 77 | + // Adding default listeners should be much less common than starting new requests. We want |
| 78 | + // some way of making sure that requests don't mutate our listeners without creating a new copy of |
| 79 | + // the list each time a request is started. |
| 80 | + private final CopyOnWriteArrayList<RequestListener<Object>> defaultRequestListeners; |
72 | 81 |
|
73 | 82 | private RequestOptions requestOptions;
|
74 | 83 |
|
@@ -115,6 +124,8 @@ public RequestManager(
|
115 | 124 | }
|
116 | 125 | lifecycle.addListener(connectivityMonitor);
|
117 | 126 |
|
| 127 | + defaultRequestListeners = |
| 128 | + new CopyOnWriteArrayList<>(glide.getGlideContext().getDefaultRequestListeners()); |
118 | 129 | setRequestOptions(glide.getGlideContext().getDefaultRequestOptions());
|
119 | 130 |
|
120 | 131 | glide.registerRequestManager(this);
|
@@ -174,6 +185,29 @@ public RequestManager setDefaultRequestOptions(@NonNull RequestOptions requestOp
|
174 | 185 | return this;
|
175 | 186 | }
|
176 | 187 |
|
| 188 | + /** |
| 189 | + * Adds a default {@link RequestListener} that will be added to every request started with this |
| 190 | + * {@link RequestManager}. |
| 191 | + * |
| 192 | + * <p>Multiple {@link RequestListener}s can be added here, in {@link RequestManager} scopes or |
| 193 | + * to individual {@link RequestBuilder}s. {@link RequestListener}s are called in the order they're |
| 194 | + * added. Even if an earlier {@link RequestListener} returns {@code true} from |
| 195 | + * {@link RequestListener#onLoadFailed(GlideException, Object, Target, boolean)} or |
| 196 | + * {@link RequestListener#onResourceReady(Object, Object, Target, DataSource, boolean)}, it will |
| 197 | + * not prevent subsequent {@link RequestListener}s from being called. |
| 198 | + * |
| 199 | + * <p>Because Glide requests can be started for any number of individual resource types, any |
| 200 | + * listener added here has to accept any generic resource type in |
| 201 | + * {@link RequestListener#onResourceReady(Object, Object, Target, DataSource, boolean)}. If you |
| 202 | + * must base the behavior of the listener on the resource type, you will need to use |
| 203 | + * {@code instanceof} to do so. It's not safe to cast resource types without first checking |
| 204 | + * with {@code instanceof}. |
| 205 | + */ |
| 206 | + public RequestManager addDefaultRequestListener(RequestListener<Object> requestListener) { |
| 207 | + defaultRequestListeners.add(requestListener); |
| 208 | + return this; |
| 209 | + } |
| 210 | + |
177 | 211 | /**
|
178 | 212 | * Returns true if loads for this {@link RequestManager} are currently paused.
|
179 | 213 | *
|
@@ -614,6 +648,10 @@ void track(@NonNull Target<?> target, @NonNull Request request) {
|
614 | 648 | requestTracker.runRequest(request);
|
615 | 649 | }
|
616 | 650 |
|
| 651 | + List<RequestListener<Object>> getDefaultRequestListeners() { |
| 652 | + return defaultRequestListeners; |
| 653 | + } |
| 654 | + |
617 | 655 | RequestOptions getDefaultRequestOptions() {
|
618 | 656 | return requestOptions;
|
619 | 657 | }
|
|
0 commit comments