ThumbnailRequestCoordinator.java revision f2285a83d5ac3e79383278b91aa21b0a87603cd1
1package com.bumptech.glide.request;
2
3/**
4 * A coordinator that coordinates two individual {@link Request}s that load a small thumbnail version of an image and
5 * the full size version of the image at the same time.
6 */
7public class ThumbnailRequestCoordinator implements RequestCoordinator, Request {
8    private Request full;
9    private Request thumb;
10    private RequestCoordinator coordinator;
11
12    public ThumbnailRequestCoordinator() {
13        this(null);
14    }
15
16    public ThumbnailRequestCoordinator(RequestCoordinator coordinator) {
17        this.coordinator = coordinator;
18    }
19
20    public void setRequests(Request full, Request thumb) {
21        this.full = full;
22        this.thumb = thumb;
23    }
24
25    /**
26     *
27     * Returns true if the request is either the request loading the fullsize image or if the request loading the
28     * full size image has not yet completed.
29     *
30     * @param request {@inheritDoc}
31     */
32    @Override
33    public boolean canSetImage(Request request) {
34        return parentCanSetImage() && (request == full || !full.isResourceSet());
35    }
36
37    private boolean parentCanSetImage() {
38        return coordinator == null || coordinator.canSetImage(this);
39    }
40
41    /**
42     * Returns true if the request is the request loading the fullsize image and if neither the full nor the thumbnail
43     * image have completed sucessfully.
44     *
45     * @param request {@inheritDoc}.
46     */
47    @Override
48    public boolean canNotifyStatusChanged(Request request) {
49        return parentCanNotifyStatusChanged() && (request == full && !isAnyResourceSet());
50    }
51
52    private boolean parentCanNotifyStatusChanged() {
53        return coordinator == null || coordinator.canNotifyStatusChanged(this);
54    }
55
56    @Override
57    public boolean isAnyResourceSet() {
58        return parentIsAnyResourceSet() || isResourceSet();
59    }
60
61    private boolean parentIsAnyResourceSet() {
62        return coordinator != null && coordinator.isAnyResourceSet();
63    }
64
65    /**
66     * Starts first the thumb request and then the full request.
67     */
68    @Override
69    public void begin() {
70        if (!thumb.isRunning()) {
71            thumb.begin();
72        }
73        if (!full.isRunning()) {
74            full.begin();
75        }
76    }
77
78    @Override
79    public void pause() {
80        full.pause();
81        thumb.pause();
82    }
83
84    /**
85     * {@inheritDoc}
86     */
87    @Override
88    public void clear() {
89        thumb.clear();
90        full.clear();
91    }
92
93    @Override
94    public boolean isPaused() {
95        return full.isPaused();
96    }
97
98    /**
99     * Returns true if the full request is still running.
100     */
101    @Override
102    public boolean isRunning() {
103        return full.isRunning();
104    }
105
106    /**
107     * Returns true if the full request is complete.
108     */
109    @Override
110    public boolean isComplete() {
111        return full.isComplete() || thumb.isComplete();
112    }
113
114    @Override
115    public boolean isResourceSet() {
116        return full.isResourceSet() || thumb.isResourceSet();
117    }
118
119    @Override
120    public boolean isCancelled() {
121        return full.isCancelled();
122    }
123
124    /**
125     * Returns true if the full request has failed.
126     */
127    @Override
128    public boolean isFailed() {
129        return full.isFailed();
130    }
131
132    /**
133     * {@inheritDoc}.
134     */
135    @Override
136    public void recycle() {
137        full.recycle();
138        thumb.recycle();
139    }
140}
141