-
- All Implemented Interfaces:
-
com.mobwith.imgmodule.manager.LifecycleListener,com.mobwith.imgmodule.request.FutureTarget,com.mobwith.imgmodule.request.RequestListener,com.mobwith.imgmodule.request.target.Target,java.util.concurrent.Future
public class RequestFutureTarget<R> implements FutureTarget<R>, RequestListener<R>
A java.util.concurrent.Future implementation for ImageModule that can be used to load resources in a blocking manner on background threads.
Note - Unlike most targets, RequestFutureTargets can be used once and only once. Attempting to reuse a RequestFutureTarget will probably result in undesirable behavior or exceptions. Instead of reusing objects of this class, the pattern should be:
The cancel call will cancel pending operations and make sure that any resources used are recycled.{@code * FutureTargettarget = null; * RequestManager requestManager = ImageModule.with(context); * try { * target = requestManager * .downloadOnly() * .load(model) * .submit(); * File downloadedFile = target.get(); * // ... do something with the file (usually throws IOException) * } catch (ExecutionException | InterruptedException | IOException e) { * // ... bug reporting or recovery * } finally { * // make sure to cancel pending operations and free resources * if (target != null) { * target.cancel(true); // mayInterruptIfRunning * } * } * }
-
-
Field Summary
Fields Modifier and Type Field Description private Requestrequestprivate booleanisCancelled
-
Constructor Summary
Constructors Constructor Description RequestFutureTarget(int width, int height)Constructor for a RequestFutureTarget.
-
Method Summary
Modifier and Type Method Description synchronized RequestgetRequest()Retrieves the current request for this target, should not be called outside of ImageModule. synchronized voidsetRequest(@Nullable() Request request)Sets the current request for this target to retain, should not be called outside of ImageModule. synchronized booleanisCancelled()booleancancel(boolean mayInterruptIfRunning)synchronized booleanisDone()Rget()Rget(long time, @NonNull() TimeUnit timeUnit)voidgetSize(@NonNull() SizeReadyCallback cb)A callback that should never be invoked directly. voidremoveCallback(@NonNull() SizeReadyCallback cb)Removes the given callback from the pending set if it's still retained. voidonLoadCleared(@Nullable() Drawable placeholder)A callback that should never be invoked directly. voidonLoadStarted(@Nullable() Drawable placeholder)A callback that should never be invoked directly. synchronized voidonLoadFailed(@Nullable() Drawable errorDrawable)A callback that should never be invoked directly. synchronized voidonResourceReady(@NonNull() R resource, @Nullable() Transition<out Object> transition)A callback that should never be invoked directly. voidonStart()Callback for when onStart} or is called. voidonStop()Callback for when onStop} or } is called. voidonDestroy()Callback for when onDestroy} or is called. synchronized booleanonLoadFailed(@Nullable() ImageModuleException e, Object model, Target<R> target, boolean isFirstResource)Called when an exception occurs during a load, immediately before . synchronized booleanonResourceReady(R resource, Object model, Target<R> target, DataSource dataSource, boolean isFirstResource)Called when a load completes successfully, immediately before . -
Methods inherited from class java.util.concurrent.Future
cancel, get, isCancelled, isDone -
Methods inherited from class com.mobwith.imgmodule.request.target.Target
onResourceReady -
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Method Detail
-
getRequest
@Nullable() synchronized Request getRequest()
Retrieves the current request for this target, should not be called outside of ImageModule.
-
setRequest
synchronized void setRequest(@Nullable() Request request)
Sets the current request for this target to retain, should not be called outside of ImageModule.
-
isCancelled
synchronized boolean isCancelled()
-
cancel
boolean cancel(boolean mayInterruptIfRunning)
-
isDone
synchronized boolean isDone()
-
getSize
void getSize(@NonNull() SizeReadyCallback cb)
A callback that should never be invoked directly.
-
removeCallback
void removeCallback(@NonNull() SizeReadyCallback cb)
Removes the given callback from the pending set if it's still retained.
- Parameters:
cb- The callback to remove.
-
onLoadCleared
void onLoadCleared(@Nullable() Drawable placeholder)
A callback that should never be invoked directly.
-
onLoadStarted
void onLoadStarted(@Nullable() Drawable placeholder)
A callback that should never be invoked directly.
-
onLoadFailed
synchronized void onLoadFailed(@Nullable() Drawable errorDrawable)
A callback that should never be invoked directly.
-
onResourceReady
synchronized void onResourceReady(@NonNull() R resource, @Nullable() Transition<out Object> transition)
A callback that should never be invoked directly.
-
onLoadFailed
synchronized boolean onLoadFailed(@Nullable() ImageModuleException e, Object model, Target<R> target, boolean isFirstResource)
Called when an exception occurs during a load, immediately before . Will only be called if we currently want to display an imagefor the given model in the given target. It is recommended to create a single instance peractivity/fragment rather than instantiate a new object for each call to
{@code * ImageModule.with(fragment/activity).load()}to avoid object churn.It is not safe to reload this or a different model in this callback. If you need to do souse error instead.
Although you can't start an entirely new load, it is safe to change what is displayed in the Target at this point, as long as you return
{@code true}from the method to prevent onLoadFailed from being called.For threading guarantees, see the class comment.
For example:
{@code * public boolean onLoadFailed(Exception e, T model, Target target, boolean isFirstResource) { * target.setPlaceholder(R.drawable.a_specific_error_for_my_exception); * return true; // Prevent onLoadFailed from being called on the Target. * } * }- Parameters:
e- The maybe{@code null}exception containing information about why the request failed.model- The model we were trying to load when the exception occurred.target- The Target we were trying to load the image into.isFirstResource-{@code true}if this exception is for the first resource to load.
-
onResourceReady
synchronized boolean onResourceReady(R resource, Object model, Target<R> target, DataSource dataSource, boolean isFirstResource)
Called when a load completes successfully, immediately before .
For threading guarantees, see the class comment.
- Parameters:
resource- The resource that was loaded for the target.model- The specific model that was used to load the image.target- The target the model was loaded into.dataSource- The DataSource the resource was loaded from.isFirstResource-{@code true}if this is the first resource to in this load to be loadedinto the target.
-
-
-
-