11package org .neotech .library .retainabletasks .internal ;
22
3+ import android .os .Looper ;
34import android .support .annotation .MainThread ;
45import android .support .annotation .NonNull ;
56import android .util .Log ;
@@ -30,6 +31,9 @@ public final class BaseTaskManager extends TaskManager {
3031 }
3132
3233 public void attach (TaskManagerProvider taskManagerProvider ){
34+ if (TaskManager .isStrictDebugModeEnabled ()){
35+ assertMainThread ();
36+ }
3337 /*
3438 * If this loop would directly set new Callback listeners it could cause a task to deliver
3539 * its result. Meaning that the removeFinishedTask method can be called causing the 'tasks'
@@ -77,6 +81,9 @@ public void attach(TaskManagerProvider taskManagerProvider){
7781
7882 @ Override
7983 public Task <?, ?> attach (@ NonNull Task <?, ?> task , @ NonNull Task .Callback callback ) {
84+ if (TaskManager .isStrictDebugModeEnabled ()){
85+ assertMainThread ();
86+ }
8087 task .setCallback (new CallbackShadow (callback ));
8188 return task ;
8289 }
@@ -97,6 +104,9 @@ public void attachAll(@NonNull TaskAttachListener attachListener, @NonNull Strin
97104
98105 @ Override
99106 public Task <?, ?> detach (@ NonNull String tag ) {
107+ if (TaskManager .isStrictDebugModeEnabled ()){
108+ assertMainThread ();
109+ }
100110 final Task <?, ?> task = tasks .get (tag );
101111 if (task != null ){
102112 task .removeCallback ();
@@ -119,6 +129,9 @@ public <Progress, Result> void execute(@NonNull Task<Progress, Result> task, @No
119129
120130 @ Override
121131 public <Progress , Result > void execute (@ NonNull Task <Progress , Result > task , @ NonNull Task .Callback callback , @ NonNull Executor executor ) {
132+ if (TaskManager .isStrictDebugModeEnabled ()){
133+ assertMainThread ();
134+ }
122135 final Task currentTask = tasks .get (task .getTag ());
123136 if (currentTask != null && currentTask .isRunning ()){
124137 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
131144 @ Override
132145 @ MainThread
133146 public boolean isResultDelivered (@ NonNull String tag ) {
147+ if (TaskManager .isStrictDebugModeEnabled ()){
148+ assertMainThread ();
149+ }
134150 Task task = tasks .get (tag );
135151 return task != null && task .isResultDelivered ();
136152 }
137153
138154 @ Override
139155 @ MainThread
140156 public boolean isRunning (@ NonNull String tag ) {
157+ if (TaskManager .isStrictDebugModeEnabled ()){
158+ assertMainThread ();
159+ }
141160 Task task = tasks .get (tag );
142161 return task != null && task .isRunning ();
143162 }
144163
145164 @ Override
146165 @ MainThread
147166 public Task <?, ?> cancel (@ NonNull String tag ){
167+ if (TaskManager .isStrictDebugModeEnabled ()){
168+ assertMainThread ();
169+ }
148170 final Task <?, ?> task = tasks .remove (tag );
149171 if (task != null ){
150172 task .cancel (false );
151173 }
152174 return task ;
153175 }
154176
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+
155184 @ Override
156185 @ MainThread
157186 public void assertAllTasksDetached () throws IllegalStateException {
@@ -165,13 +194,24 @@ public void assertAllTasksDetached() throws IllegalStateException {
165194
166195 @ MainThread
167196 public void cancelAll (){
197+ if (TaskManager .isStrictDebugModeEnabled ()){
198+ assertMainThread ();
199+ }
168200 for (Map .Entry <String , Task <?, ?>> task : tasks .entrySet ()){
169201 task .getValue ().cancel (true );
170202 }
171203 }
172204
173205 @ MainThread
174206 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+ */
175215 for (Map .Entry <String , Task <?, ?>> task : tasks .entrySet ()){
176216 task .getValue ().removeCallback ();
177217 }
0 commit comments