package net.simonvt.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.FrameLayout;

/**
 * FrameLayout which caches the hardware layer if available.
 * <p/>
 * If it's not posted twice the layer either wont be built on start, or it'll be built twice.
 */
public class BuildLayerFrameLayout extends FrameLayout {

    private boolean mChanged;

    private boolean mHardwareLayersEnabled = true;

    public BuildLayerFrameLayout(Context context) {
        super(context);
    }

    public BuildLayerFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BuildLayerFrameLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    void setHardwareLayersEnabled(boolean enabled) {
        mHardwareLayersEnabled = enabled;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        if (MenuDrawer.USE_TRANSLATIONS && mHardwareLayersEnabled) {
            post(new Runnable() {
                @Override
                public void run() {
                    mChanged = true;
                    invalidate();
                }
            });
        }
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);

        if (mChanged && MenuDrawer.USE_TRANSLATIONS) {
            post(new Runnable() {
                @Override
                public void run() {
                    final int layerType = getLayerType();
                    // If it's already a hardware layer, it'll be built anyway.
                    if (layerType != LAYER_TYPE_HARDWARE) {
                        setLayerType(LAYER_TYPE_HARDWARE, null);
                        buildLayer();
                        setLayerType(LAYER_TYPE_NONE, null);
                    }
                }
            });

            mChanged = false;
        }
    }
}

@@ -468,16 +468,48 @@
  true;%0A%0A
+    private boolean mAttached;%0A%0A
     publ
@@ -548,24 +548,24 @@
  context) %7B%0A
-
         supe
@@ -921,32 +921,287 @@
 enabled;%0A    %7D%0A%0A
+    @Override%0A    protected void onAttachedToWindow() %7B%0A        super.onAttachedToWindow();%0A        mAttached = true;%0A    %7D%0A%0A    @Override%0A    protected void onDetachedFromWindow() %7B%0A        super.onDetachedFromWindow();%0A        mAttached = false;%0A    %7D%0A%0A
     @Override%0A  
@@ -1845,32 +1845,73 @@
 ic void run() %7B%0A
+                    if (mAttached) %7B%0A    
                 
@@ -1972,16 +1972,20 @@
         
+   
+ 
 // If it
@@ -2052,24 +2052,28 @@
             
+    
 if (layerTyp
@@ -2116,32 +2116,36 @@
                 
+ 
+   
 setLayerType(LAY
@@ -2193,16 +2193,20 @@
         
+  
+  
 buildLay
@@ -2207,24 +2207,28 @@
 ildLayer();%0A
+    
             
@@ -2268,32 +2268,58 @@
 PE_NONE, null);%0A
+                        %7D%0A
                 

Check if view is attached before building hardware layer.