Class RequestFutureTarget<R>

  • Type Parameters:
    R - The type of the resource that will be loaded.
    All Implemented Interfaces:
    LifecycleListener, FutureTarget<R>, RequestListener<R>, Target<R>, java.util.concurrent.Future<R>

    public class RequestFutureTarget<R>
    extends java.lang.Object
    implements FutureTarget<R>, RequestListener<R>
    A Future implementation for Glide 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:

    
     FutureTarget<File> target = null;
     RequestManager requestManager = Glide.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
       }
     }
     
    The cancel(boolean) call will cancel pending operations and make sure that any resources used are recycled.
    • Constructor Summary

      Constructors 
      Constructor Description
      RequestFutureTarget​(int width, int height)
      Constructor for a RequestFutureTarget.
    • Constructor Detail

      • RequestFutureTarget

        public RequestFutureTarget​(int width,
                                   int height)
        Constructor for a RequestFutureTarget. Should not be used directly.
    • Method Detail

      • cancel

        public boolean cancel​(boolean mayInterruptIfRunning)
        Specified by:
        cancel in interface java.util.concurrent.Future<R>
      • isCancelled

        public boolean isCancelled()
        Specified by:
        isCancelled in interface java.util.concurrent.Future<R>
      • isDone

        public boolean isDone()
        Specified by:
        isDone in interface java.util.concurrent.Future<R>
      • get

        public R get()
              throws java.lang.InterruptedException,
                     java.util.concurrent.ExecutionException
        Specified by:
        get in interface java.util.concurrent.Future<R>
        Throws:
        java.lang.InterruptedException
        java.util.concurrent.ExecutionException
      • get

        public R get​(long time,
                     @NonNull
                     java.util.concurrent.TimeUnit timeUnit)
              throws java.lang.InterruptedException,
                     java.util.concurrent.ExecutionException,
                     java.util.concurrent.TimeoutException
        Specified by:
        get in interface java.util.concurrent.Future<R>
        Throws:
        java.lang.InterruptedException
        java.util.concurrent.ExecutionException
        java.util.concurrent.TimeoutException
      • getSize

        public void getSize​(@NonNull
                            SizeReadyCallback cb)
        A callback that should never be invoked directly.
        Specified by:
        getSize in interface Target<R>
        Parameters:
        cb - The callback that must be called when the size of the target has been determined
      • removeCallback

        public void removeCallback​(@NonNull
                                   SizeReadyCallback cb)
        Description copied from interface: Target
        Removes the given callback from the pending set if it's still retained.
        Specified by:
        removeCallback in interface Target<R>
        Parameters:
        cb - The callback to remove.
      • setRequest

        public void setRequest​(@Nullable
                               Request request)
        Description copied from interface: Target
        Sets the current request for this target to retain, should not be called outside of Glide.
        Specified by:
        setRequest in interface Target<R>
      • getRequest

        @Nullable
        public Request getRequest()
        Description copied from interface: Target
        Retrieves the current request for this target, should not be called outside of Glide.
        Specified by:
        getRequest in interface Target<R>
      • onLoadCleared

        public void onLoadCleared​(@Nullable
                                  android.graphics.drawable.Drawable placeholder)
        A callback that should never be invoked directly.
        Specified by:
        onLoadCleared in interface Target<R>
        Parameters:
        placeholder - The placeholder drawable to optionally show, or null.
      • onLoadStarted

        public void onLoadStarted​(@Nullable
                                  android.graphics.drawable.Drawable placeholder)
        A callback that should never be invoked directly.
        Specified by:
        onLoadStarted in interface Target<R>
        Parameters:
        placeholder - The placeholder drawable to optionally show, or null.
      • onLoadFailed

        public void onLoadFailed​(@Nullable
                                 android.graphics.drawable.Drawable errorDrawable)
        A callback that should never be invoked directly.
        Specified by:
        onLoadFailed in interface Target<R>
        Parameters:
        errorDrawable - The error drawable to optionally show, or null.
      • onResourceReady

        public void onResourceReady​(@NonNull
                                    R resource,
                                    @Nullable
                                    Transition<? super R> transition)
        A callback that should never be invoked directly.
        Specified by:
        onResourceReady in interface Target<R>
        Parameters:
        resource - the loaded resource.
      • onStart

        public void onStart()
        Description copied from interface: LifecycleListener
        Callback for when Fragment.onStart()} or Activity.onStart() is called.
        Specified by:
        onStart in interface LifecycleListener
      • onStop

        public void onStop()
        Description copied from interface: LifecycleListener
        Callback for when Fragment.onStop()} or Activity.onStop()} is called.
        Specified by:
        onStop in interface LifecycleListener
      • onDestroy

        public void onDestroy()
        Description copied from interface: LifecycleListener
        Callback for when Fragment.onDestroy()} or Activity.onDestroy() is called.
        Specified by:
        onDestroy in interface LifecycleListener
      • onLoadFailed

        public boolean onLoadFailed​(@Nullable
                                    GlideException e,
                                    java.lang.Object model,
                                    Target<R> target,
                                    boolean isFirstResource)
        Description copied from interface: RequestListener
        Called when an exception occurs during a load, immediately before Target.onLoadFailed(Drawable). Will only be called if we currently want to display an image for the given model in the given target. It is recommended to create a single instance per activity/fragment rather than instantiate a new object for each call to Glide.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 so use RequestBuilder.error(RequestBuilder) 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 true from the method to prevent Target.onLoadFailed(Drawable) from being called.

        For threading guarantees, see the class comment.

        For example:

        
         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.
         }
         
        Specified by:
        onLoadFailed in interface RequestListener<R>
        Parameters:
        e - The maybe 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 - true if this exception is for the first resource to load.
        Returns:
        true to prevent Target.onLoadFailed(Drawable) from being called on target, typically because the listener wants to update the target or the object the target wraps itself or false to allow Target.onLoadFailed(Drawable) to be called on target.
      • onResourceReady

        public boolean onResourceReady​(R resource,
                                       java.lang.Object model,
                                       Target<R> target,
                                       DataSource dataSource,
                                       boolean isFirstResource)
        Description copied from interface: RequestListener
        Called when a load completes successfully, immediately before Target.onResourceReady(Object, com.bumptech.glide.request.transition.Transition).

        For threading guarantees, see the class comment.

        Specified by:
        onResourceReady in interface RequestListener<R>
        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 - true if this is the first resource to in this load to be loaded into the target. For example when loading a thumbnail and a full-sized image, this will be true for the first image to load and false for the second.
        Returns:
        true to prevent Target.onResourceReady(Object, Transition) from being called on target, typically because the listener wants to update the target or the object the target wraps itself or false to allow Target.onResourceReady(Object, Transition) to be called on target.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object