package com.stanfy.images.decorator;

import android.graphics.Bitmap;
import android.graphics.Canvas;

/**
 * Chain of decorators.
 * @author Roman Mazur (Stanfy - http://www.stanfy.com)
 */
public class ChainDecorator extends ImageDecoratorAdapter {

  /** Internal decorators. */
  private final ImageDecorator[] chain;

  public ChainDecorator(final ImageDecorator... decorators) {
    this.chain = decorators;
  }

  @Override
  public Bitmap processBitmap(final Bitmap bitmap, final Canvas bitmapVanvas) {
    Bitmap result = bitmap;
    final ImageDecorator[] chain = this.chain;
    for (int i = 0; i < chain.length; i++) {
      final Bitmap next = chain[i].decorateBitmap(result, bitmapVanvas);
      if (next != result) { result.recycle(); }
      result = next;
    }
    return result;
  }

  @Override
  public void setup(final int width, final int height, final int[] state, final int level) {
    final ImageDecorator[] chain = this.chain;
    for (int i = 0; i < chain.length; i++) {
      chain[i].setup(width, height, state, level);
    }
  }

  public ImageDecorator[] getChain() {
    return chain;
  }

}

@@ -316,16 +316,75 @@
 chain;%0A%0A
+  /** Stateful flag. */%0A  private final boolean stateful;%0A%0A
   public
@@ -466,16 +466,191 @@
 rators;%0A
+    for (final ImageDecorator d : decorators) %7B%0A      if (d.dependsOnDrawableState()) %7B%0A        this.stateful = true;%0A        return;%0A      %7D%0A    %7D%0A    this.stateful = false;%0A
   %7D%0A%0A  @
@@ -1026,24 +1026,100 @@
 esult;%0A  %7D%0A%0A
+  @Override%0A  public boolean dependsOnDrawableState() %7B return stateful; %7D%0A%0A
   @Override%0A

Fix state in chain decorator