Android Open Source - RZAndroidBaseUtils Async Drawable Utils






From Project

Back to project page RZAndroidBaseUtils.

License

The source code is released under:

MIT License

If you think the Android project RZAndroidBaseUtils listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.raizlabs.graphics.drawable.async;
/*w  w w  .jav a  2 s.com*/
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;

/**
 * Class of helper functions for {@link AsyncDrawable}s.
 * 
 * @author Dylan James
 */
public class AsyncDrawableUtils {
  /**
   * Attempts to cancel any work associated with a given {@link ImageView},
   * yielding if the existing work already assigned to the view contains the
   * same key as the given task.
   * @param task The task whose work is to later be associated with the view.
   * @param imageView The {@link ImageView} whose work to clear.
   * @return True if the work was empty or cancelled, false if the given tasks
   * key was already associated with the given view.
   */
  public static boolean cancelExistingWork(AsyncDrawableTask<?> task, ImageView imageView) {
    final AsyncDrawableTask<?> existingTask = getTask(imageView);
    return cancelExistingWork(existingTask, task);
  }
  
  /**
   * Attempts to cancel any work associated with a given {@link View},
   * yielding if the existing work already assigned to the view contains the
   * same key as the given task.
   * @param task The task whose work is to later be associated with the view.
   * @param view The {@link View} whose work to clear.
   * @return True if the work was empty or cancelled, false if the given tasks
   * key was already associated with the given view.
   */
  public static boolean cancelExistingWork(AsyncDrawableTask<?> task, View view) {
    final AsyncDrawableTask<?> existingTask = getTask(view);
    return cancelExistingWork(existingTask, task);
  }
  
  /**
   * Attempts to cancel the work of the existing task, but yields if the existing
   * task is already operating on the same key as the new task.
   * @param existingTask
   * @param newTask
   * @return True if the work was empty or cancelled, false if the given existing
   * tasks key matched the new tasks key.
   */
  private static boolean cancelExistingWork(AsyncDrawableTask<?> existingTask, AsyncDrawableTask<?> newTask) {
    if (existingTask != null) {
      if (newTask != null) {
        final Object key = newTask.getKey();
        if (key != null && key.equals(existingTask.getKey())) {
          // Both tasks have the same key
          if (!existingTask.isCancelled()) 
            return false;
        }
      }
      existingTask.cancel();
    }
    // No task associated or the task was cancelled
    return true;
  }
  
  /**
   * Retrieves the task associated with the given {@link ImageView}.
   * @param imageView The view to get the task from.
   * @return The task associated with the view or null if none exists.
   */
  public static AsyncDrawableTask<?> getTask(ImageView imageView) {
    if (imageView != null) {
      final Drawable drawable = imageView.getDrawable();
      if (drawable instanceof AsyncDrawable<?>) {
        return ((AsyncDrawable<?>) drawable).getTask();
      }
    }
    return null;
  }
  
  /**
   * Retrieves the task associated with the given {@link View}.
   * @param view The view to get the task from.
   * @return The task associated with the view or null if none exists.
   */
  public static AsyncDrawableTask<?> getTask(View view) {
    if (view != null) {
      final Drawable drawable = view.getBackground();
      if (drawable instanceof AsyncDrawable<?>) {
        return ((AsyncDrawable<?>) drawable).getTask();
      }
    }
    return null;
  }
}




Java Source Code List

com.raizlabs.baseutils.CompatibilityUtils.java
com.raizlabs.baseutils.IOUtils.java
com.raizlabs.baseutils.Logger.java
com.raizlabs.baseutils.Math.java
com.raizlabs.baseutils.StringUtils.java
com.raizlabs.baseutils.ThreadingUtils.java
com.raizlabs.baseutils.Wrapper.java
com.raizlabs.baseutils.examples.MainActivity.java
com.raizlabs.baseutils.examples.asyncdrawable.AsyncDrawableExampleActivity.java
com.raizlabs.baseutils.examples.asyncdrawable.AsyncDrawableListExampleActivity.java
com.raizlabs.baseutils.examples.simplegenericadapter.SimpleGenericAdapterExampleActivity.java
com.raizlabs.baseutils.examples.viewgroupadapter.ViewGroupAdapterExampleActivity.java
com.raizlabs.baseutils.examples.viewholderstrategy.SimpleViewHolderStrategyExampleActivity.java
com.raizlabs.collections.ListUtils.java
com.raizlabs.collections.MappableSet.java
com.raizlabs.collections.TransactionalHashSet.java
com.raizlabs.concurrent.BasePrioritizedRunnable.java
com.raizlabs.concurrent.ConcurrencyUtils.java
com.raizlabs.concurrent.PrioritizedRunnable.java
com.raizlabs.concurrent.Prioritized.java
com.raizlabs.content.sharing.SharingUtils.java
com.raizlabs.database.CursorIterable.java
com.raizlabs.database.CursorIterator.java
com.raizlabs.events.EventListener.java
com.raizlabs.events.Event.java
com.raizlabs.events.ProgressListener.java
com.raizlabs.events.SimpleEventListener.java
com.raizlabs.functions.Delegate.java
com.raizlabs.functions.Predicate.java
com.raizlabs.functions.Provider.java
com.raizlabs.graphics.ImageFactory.java
com.raizlabs.graphics.drawable.async.AsyncDrawableTask.java
com.raizlabs.graphics.drawable.async.AsyncDrawableUtils.java
com.raizlabs.graphics.drawable.async.AsyncDrawableWrapper.java
com.raizlabs.graphics.drawable.async.AsyncDrawable.java
com.raizlabs.graphics.drawable.async.BaseAsyncDrawableTask.java
com.raizlabs.imagecaching.ImageCache.java
com.raizlabs.imagecaching.PrefixedImageCacheAdapter.java
com.raizlabs.imagecaching.StubImageCache.java
com.raizlabs.json.JSONArrayParserDelegate.java
com.raizlabs.json.JSONHelper.java
com.raizlabs.synchronization.OneShotLock.java
com.raizlabs.tasks.RZAsyncTaskEvent.java
com.raizlabs.tasks.RZAsyncTaskListener.java
com.raizlabs.tasks.RZAsyncTask.java
com.raizlabs.util.observable.ObservableData.java
com.raizlabs.util.observable.ObservableListAdapter.java
com.raizlabs.util.observable.ObservableList.java
com.raizlabs.view.ViewCompatibility.java
com.raizlabs.view.animation.AnimationListenerWrapper.java
com.raizlabs.view.animation.RelativeLayoutParamsAnimation.java
com.raizlabs.view.animation.ResizeAnimation.java
com.raizlabs.widget.EvenLinearLayout.java
com.raizlabs.widget.ImageMixView.java
com.raizlabs.widget.SlideRevealLayout.java
com.raizlabs.widget.ViewUtils.java
com.raizlabs.widget.adapters.ListBasedAdapter.java
com.raizlabs.widget.adapters.SimpleGenericAdapter.java
com.raizlabs.widget.adapters.ViewGroupAdapter.java
com.raizlabs.widget.adapters.ViewHolderStrategyAdapter.java
com.raizlabs.widget.utils.SimpleViewHolderStrategy.java
com.raizlabs.widget.utils.ViewHolderStrategyConverter.java
com.raizlabs.widget.utils.ViewHolderStrategyUtils.java
com.raizlabs.widget.utils.ViewHolderStrategy.java