/*
 * Copyright (C) 2014 Andrey Kulikov (andkulikov@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.transitionseverywhere.utils;

import android.animation.LayoutTransition;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.view.ViewGroup;

import com.transitionseverywhere.R;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

@TargetApi(VERSION_CODES.ICE_CREAM_SANDWICH)
public class ViewGroupUtils {

    @TargetApi(VERSION_CODES.JELLY_BEAN)
    static class BaseViewGroupUtils {

        private static Field sFieldLayoutSuppressed;
        private static LayoutTransition sEmptyLayoutTransition;
        private static final Method METHOD_LAYOUT_TRANSITION_CANCEL =
                ReflectionUtils.getPrivateMethod(LayoutTransition.class, "cancel");

        public void suppressLayout(final ViewGroup group, boolean suppress) {
            if (sEmptyLayoutTransition == null) {
                sEmptyLayoutTransition = new LayoutTransition() {
                    @Override
                    public boolean isChangingLayout() {
                        return true;
                    }
                };
                sEmptyLayoutTransition.setAnimator(LayoutTransition.APPEARING, null);
                sEmptyLayoutTransition.setAnimator(LayoutTransition.CHANGE_APPEARING, null);
                sEmptyLayoutTransition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING, null);
                sEmptyLayoutTransition.setAnimator(LayoutTransition.DISAPPEARING, null);
                sEmptyLayoutTransition.setAnimator(LayoutTransition.CHANGING, null);
            }
            if (suppress) {
                cancelLayoutTransition(group);
                LayoutTransition layoutTransition = group.getLayoutTransition();
                if (layoutTransition != null && layoutTransition != sEmptyLayoutTransition) {
                    group.setTag(R.id.group_layouttransition_backup, group.getLayoutTransition());
                }
                group.setLayoutTransition(sEmptyLayoutTransition);
            } else {
                group.setLayoutTransition(null);
                if (sFieldLayoutSuppressed == null) {
                    sFieldLayoutSuppressed = ReflectionUtils.getPrivateField(ViewGroup.class,
                            "mLayoutSuppressed");
                }
                Boolean suppressed = (Boolean) ReflectionUtils.getFieldValue(group,
                        Boolean.FALSE, sFieldLayoutSuppressed);
                if (!Boolean.FALSE.equals(suppressed)) {
                    ReflectionUtils.setFieldValue(group, sFieldLayoutSuppressed, false);
                    group.requestLayout();
                }
                final LayoutTransition layoutTransition = (LayoutTransition)
                        group.getTag(R.id.group_layouttransition_backup);
                if (layoutTransition != null) {
                    group.setTag(R.id.group_layouttransition_backup, null);
                    group.post(new Runnable() {
                        @Override
                        public void run() {
                            group.setLayoutTransition(layoutTransition);
                        }
                    });
                }
            }
        }

        public boolean cancelLayoutTransition(ViewGroup group) {
            if (group != null) {
                final LayoutTransition layoutTransition = group.getLayoutTransition();
                if (layoutTransition != null && layoutTransition.isRunning() &&
                        METHOD_LAYOUT_TRANSITION_CANCEL != null) {
                    ReflectionUtils.invoke(group.getLayoutTransition(), null, METHOD_LAYOUT_TRANSITION_CANCEL);
                    return true;
                }
            }
            return false;
        }
    }

    @TargetApi(VERSION_CODES.JELLY_BEAN_MR2)
    static class JellyBeanMr2ViewGroupUtils extends BaseViewGroupUtils {

        private static final Method METHOD_suppressLayout = ReflectionUtils.getMethod(ViewGroup.class,
                "suppressLayout", boolean.class);

        @Override
        public void suppressLayout(ViewGroup group, boolean suppress) {
            ReflectionUtils.invoke(group, null, METHOD_suppressLayout, suppress);
        }
    }

    private static final BaseViewGroupUtils IMPL;

    static {
        if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR2) {
            IMPL = new JellyBeanMr2ViewGroupUtils();
        } else {
            IMPL = new BaseViewGroupUtils();
        }
    }

    public static void suppressLayout(ViewGroup group, boolean suppress) {
        if (group != null) {
            IMPL.suppressLayout(group, suppress);
        }
    }

    /**
     * @return is cancel performed
     */
    public static boolean cancelLayoutTransition(ViewGroup group) {
        return IMPL.cancelLayoutTransition(group);
    }
}

@@ -1088,32 +1088,98 @@
 ewGroupUtils %7B%0A%0A
+        private static final int LAYOUT_TRANSITION_CHANGING = 4;%0A%0A
         private 
@@ -1306,104 +1306,22 @@
 tic 
-final 
 Method 
-METHOD_LAYOUT_TRANSITION_CANCEL =%0A                ReflectionUtils.getPrivate
+s
 Method
-(
 Layo
@@ -1336,25 +1336,14 @@
 tion
-.class, %22c
+C
 ancel
-%22)
 ;%0A%0A 
@@ -2115,32 +2115,33 @@
 imator(L
-ayoutTransition.
+AYOUT_TRANSITION_
 CHANGING
@@ -3543,228 +3543,44 @@
 oup.
-post(new Runnable() %7B%0A                        @Override%0A                        public void run() %7B%0A                            group.setLayoutTransition(layoutTransition);%0A                        %7D%0A                    %7D
+setLayoutTransition(layoutTransition
 );%0A 
@@ -3882,19 +3882,19 @@
 unning()
- &&
+) %7B
 %0A       
@@ -3910,54 +3910,198 @@
     
-    METHOD_LAYOUT_TRANSITION_CANCEL != null) %7B
+if (sMethodLayoutTransitionCancel == null) %7B%0A                        sMethodLayoutTransitionCancel = ReflectionUtils.getPrivateMethod(LayoutTransition.class, %22cancel%22);%0A                    %7D
 %0A   
@@ -4179,39 +4179,37 @@
 ll, 
-METHOD_LAYOUT_TRANSITION_CANCEL
+sMethodLayoutTransitionCancel
 );%0A 
@@ -4461,29 +4461,23 @@
 tic 
-final 
 Method 
-METHOD_s
+sMethodS
 uppr
@@ -4489,194 +4489,280 @@
 yout
- = ReflectionUtils.getMethod(ViewGroup.class,%0A                %22suppressLayout%22, boolean.class);%0A%0A        @Override%0A        public void suppressLayout(ViewGroup group, boolean suppress) %7B
+;%0A%0A        @Override%0A        public void suppressLayout(ViewGroup group, boolean suppress) %7B%0A            if (sMethodSuppressLayout == null) %7B%0A                sMethodSuppressLayout = ReflectionUtils.getMethod(ViewGroup.class, %22suppressLayout%22, boolean.class);%0A            %7D
 %0A   
@@ -4810,16 +4810,16 @@
 ll, 
-METHOD_s
+sMethodS
 uppr

reformat in ViewGroupUtils