package com.lucasurbas.listitemview.util;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import com.lucasurbas.listitemview.R;

/**
 * Static methods to help with views and dimensions.
 *
 * @author Lucas Urbas
 */
public class ViewUtils {

    ViewUtils() {
    }

    /**
     * Returns whether or not the view has been laid out
     **/
    public static boolean isLaidOut(View view) {
        if (Build.VERSION.SDK_INT >= 19) {
            return view.isLaidOut();
        }

        return view.getWidth() > 0 && view.getHeight() > 0;
    }

    /**
     * Executes the given {@link Runnable} when the view is laid out
     **/
    public static void onLaidOut(final View view, final Runnable runnable) {
        if (isLaidOut(view)) {
            runnable.run();
            return;
        }

        final ViewTreeObserver observer = view.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                final ViewTreeObserver trueObserver;

                if (observer.isAlive()) {
                    trueObserver = observer;
                } else {
                    trueObserver = view.getViewTreeObserver();
                }

                removeGlobalLayoutObserver(trueObserver, this);
                runnable.run();
            }
        });
    }

    @ColorInt
    public static int getDefaultColor(Context context) {
        // Get the secondary text color of the theme
        TypedValue typedValue = new TypedValue();
        Resources.Theme theme = context.getTheme();
        theme.resolveAttribute(android.R.attr.textColorSecondary, typedValue, true);
        TypedArray a = context.obtainStyledAttributes(typedValue.data, new int[] {
                android.R.attr.textColorSecondary
        });
        int colorFallback = ContextCompat.getColor(context, R.color.liv_gray_active_icon);
        int color = colorFallback;
        try {
            color = a.getColor(0, colorFallback);
        } finally {
            a.recycle();
        }
        return color;
    }

    public static void setIconColor(ImageView iconHolder, int color) {
        Drawable wrappedDrawable = DrawableCompat.wrap(iconHolder.getDrawable());
        DrawableCompat.setTint(wrappedDrawable, color);
        iconHolder.setImageDrawable(wrappedDrawable);
        iconHolder.invalidate();
    }

    public static float dpToPixel(float dp) {
        DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
        return (dp * metrics.density);
    }

    public static float spToPixel(float sp) {
        DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, metrics);
    }

    public static void removeGlobalLayoutObserver(final ViewTreeObserver observer,
            ViewTreeObserver.OnGlobalLayoutListener layoutListener) {
        if (Build.VERSION.SDK_INT < 16) {
            observer.removeGlobalOnLayoutListener(layoutListener);
        } else {
            observer.removeOnGlobalLayoutListener(layoutListener);
        }
    }
}

@@ -1841,24 +1841,90 @@
  %7D);%0A    %7D%0A%0A
+%0A    /**%0A     * Get the secondary text color of the theme%0A     */%0A
     @ColorIn
@@ -1994,237 +1994,47 @@
     
-// Get the secondary text color of the theme%0A        TypedValue typedValue = new TypedValue();%0A        Resources.Theme theme = context.getTheme();%0A        theme.resolveAttribute(android.R.attr.textColorSecondary, typedValue, true
+TypedValue typedValue = new TypedValue(
 );%0A 
@@ -2112,35 +2112,17 @@
 ew int%5B%5D
- %7B%0A                
+%7B
 android.
@@ -2146,16 +2146,19 @@
 econdary
+%7D);
 %0A       
@@ -2154,27 +2154,29 @@
 %7D);%0A        
-%7D);
+try %7B
 %0A        int
@@ -2176,27 +2176,32 @@
     
-int colorFallback =
+    return a.getColor(0,
  Con
@@ -2262,106 +2262,8 @@
 con)
-;%0A        int color = colorFallback;%0A        try %7B%0A            color = a.getColor(0, colorFallback
 );%0A 
@@ -2320,30 +2320,8 @@
   %7D%0A
-        return color;%0A
     

Fix ResourceType: Invalid package identifier when getting bag for resource number 0x00000282 warnings