• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.bumptech.glide.manager;
2 
3 import com.bumptech.glide.request.Request;
4 
5 import java.util.Collections;
6 import java.util.Set;
7 import java.util.WeakHashMap;
8 
9 /**
10  * A class for tracking, canceling, and restarting in progress, completed, and failed requests.
11  */
12 public class RequestTracker {
13     // Most requests will be for views and will therefore be held strongly (and safely) by the view via the tag.
14     // However, a user can always pass in a different type of target which may end up not being strongly referenced even
15     // though the user still would like the request to finish. Weak references are therefore only really functional in
16     // this context for view targets. Despite the side affects, WeakReferences are still essentially required. A user
17     // can always make repeated requests into targets other than views, or use an activity manager in a fragment pager
18     // where holding strong references would steadily leak bitmaps and/or views.
19     private final Set<Request> requests = Collections.newSetFromMap(new WeakHashMap<Request, Boolean>());
20     private boolean isPaused;
21 
22     /**
23      * Starts tracking the given request.
24      */
runRequest(Request request)25     public void runRequest(Request request) {
26         requests.add(request);
27         if (!isPaused) {
28             request.begin();
29         }
30     }
31 
32     // Exposed for testing.
addRequest(Request request)33     void addRequest(Request request) {
34         requests.add(request);
35     }
36 
37     /**
38      * Stops tracking the given request.
39      */
removeRequest(Request request)40     public void removeRequest(Request request) {
41         requests.remove(request);
42     }
43 
44     /**
45      * Returns {@code true} if requests are currently paused, and {@code false} otherwise.
46      */
isPaused()47     public boolean isPaused() {
48         return isPaused;
49     }
50 
51     /**
52      * Stops any in progress requests.
53      */
pauseRequests()54     public void pauseRequests() {
55         isPaused = true;
56         for (Request request : requests) {
57             if (request.isRunning()) {
58                 request.pause();
59             }
60         }
61     }
62 
63     /**
64      * Starts any not yet completed or failed requests.
65      */
resumeRequests()66     public void resumeRequests() {
67         isPaused = false;
68         for (Request request : requests) {
69             if (!request.isComplete() && !request.isCancelled() && !request.isRunning()) {
70                 request.begin();
71             }
72         }
73     }
74 
75     /**
76      * Cancels all requests and clears their resources.
77      */
clearRequests()78     public void clearRequests() {
79         for (Request request : requests) {
80             request.clear();
81         }
82     }
83 
84     /**
85      * Restarts failed requests and cancels and restarts in progress requests.
86      */
restartRequests()87     public void restartRequests() {
88         for (Request request : requests) {
89             if (!request.isComplete() && !request.isCancelled()) {
90                 // Ensure the request will be restarted in onResume.
91                 request.pause();
92                 if (!isPaused) {
93                     request.begin();
94                 }
95             }
96         }
97     }
98 }
99