1
1
package org .neotech .library .retainabletasks .internal ;
2
2
3
+ import android .os .Looper ;
3
4
import android .support .annotation .MainThread ;
4
5
import android .support .annotation .NonNull ;
5
6
import android .util .Log ;
@@ -30,6 +31,9 @@ public final class BaseTaskManager extends TaskManager {
30
31
}
31
32
32
33
public void attach (TaskManagerProvider taskManagerProvider ){
34
+ if (TaskManager .isStrictDebugModeEnabled ()){
35
+ assertMainThread ();
36
+ }
33
37
/*
34
38
* If this loop would directly set new Callback listeners it could cause a task to deliver
35
39
* its result. Meaning that the removeFinishedTask method can be called causing the 'tasks'
@@ -77,6 +81,9 @@ public void attach(TaskManagerProvider taskManagerProvider){
77
81
78
82
@ Override
79
83
public Task <?, ?> attach (@ NonNull Task <?, ?> task , @ NonNull Task .Callback callback ) {
84
+ if (TaskManager .isStrictDebugModeEnabled ()){
85
+ assertMainThread ();
86
+ }
80
87
task .setCallback (new CallbackShadow (callback ));
81
88
return task ;
82
89
}
@@ -97,6 +104,9 @@ public void attachAll(@NonNull TaskAttachListener attachListener, @NonNull Strin
97
104
98
105
@ Override
99
106
public Task <?, ?> detach (@ NonNull String tag ) {
107
+ if (TaskManager .isStrictDebugModeEnabled ()){
108
+ assertMainThread ();
109
+ }
100
110
final Task <?, ?> task = tasks .get (tag );
101
111
if (task != null ){
102
112
task .removeCallback ();
@@ -119,6 +129,9 @@ public <Progress, Result> void execute(@NonNull Task<Progress, Result> task, @No
119
129
120
130
@ Override
121
131
public <Progress , Result > void execute (@ NonNull Task <Progress , Result > task , @ NonNull Task .Callback callback , @ NonNull Executor executor ) {
132
+ if (TaskManager .isStrictDebugModeEnabled ()){
133
+ assertMainThread ();
134
+ }
122
135
final Task currentTask = tasks .get (task .getTag ());
123
136
if (currentTask != null && currentTask .isRunning ()){
124
137
throw new IllegalStateException ("Task with an equal tag: '" + task .getTag () + "' has already been added and is currently running or finishing." );
@@ -131,27 +144,43 @@ public <Progress, Result> void execute(@NonNull Task<Progress, Result> task, @No
131
144
@ Override
132
145
@ MainThread
133
146
public boolean isResultDelivered (@ NonNull String tag ) {
147
+ if (TaskManager .isStrictDebugModeEnabled ()){
148
+ assertMainThread ();
149
+ }
134
150
Task task = tasks .get (tag );
135
151
return task != null && task .isResultDelivered ();
136
152
}
137
153
138
154
@ Override
139
155
@ MainThread
140
156
public boolean isRunning (@ NonNull String tag ) {
157
+ if (TaskManager .isStrictDebugModeEnabled ()){
158
+ assertMainThread ();
159
+ }
141
160
Task task = tasks .get (tag );
142
161
return task != null && task .isRunning ();
143
162
}
144
163
145
164
@ Override
146
165
@ MainThread
147
166
public Task <?, ?> cancel (@ NonNull String tag ){
167
+ if (TaskManager .isStrictDebugModeEnabled ()){
168
+ assertMainThread ();
169
+ }
148
170
final Task <?, ?> task = tasks .remove (tag );
149
171
if (task != null ){
150
172
task .cancel (false );
151
173
}
152
174
return task ;
153
175
}
154
176
177
+
178
+ public void assertMainThread () throws IllegalStateException {
179
+ if (Looper .getMainLooper () != Looper .myLooper ()){
180
+ throw new IllegalStateException ("Method not called on the UI-thread!" );
181
+ }
182
+ }
183
+
155
184
@ Override
156
185
@ MainThread
157
186
public void assertAllTasksDetached () throws IllegalStateException {
@@ -165,13 +194,24 @@ public void assertAllTasksDetached() throws IllegalStateException {
165
194
166
195
@ MainThread
167
196
public void cancelAll (){
197
+ if (TaskManager .isStrictDebugModeEnabled ()){
198
+ assertMainThread ();
199
+ }
168
200
for (Map .Entry <String , Task <?, ?>> task : tasks .entrySet ()){
169
201
task .getValue ().cancel (true );
170
202
}
171
203
}
172
204
173
205
@ MainThread
174
206
public void detach (){
207
+ if (TaskManager .isStrictDebugModeEnabled ()){
208
+ assertMainThread ();
209
+ }
210
+ /**
211
+ * The problem described in the attach(TaskManagerProvider) doesn't apply to this method
212
+ * as all TaskManager methods should be executed on the UI-thread, meaning that no other
213
+ * method can be called while inside this method.
214
+ */
175
215
for (Map .Entry <String , Task <?, ?>> task : tasks .entrySet ()){
176
216
task .getValue ().removeCallback ();
177
217
}
0 commit comments