package com.dailystudio.app.ui.utils;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;

import com.dailystudio.development.Logger;

import java.lang.reflect.Method;

public class ViewHelper {
	
	private final static int SCREENLAYOUT_SIZE_XLARGE = 4;

	private static int[] sTempPos = new int[2];

	public synchronized static Rect getBoundsOnScreen(View view) {
		if (view == null) {
			return new Rect(0, 0, 0, 0);
		}

		view.getLocationOnScreen(sTempPos);

		Rect rect = new Rect();
		rect.set(view.getLeft(), view.getTop(),
				view.getRight(), view.getBottom());
		rect.offset(sTempPos[0] - view.getLeft(), sTempPos[1] - view.getTop());

		return rect;
	}

	public static void disableHardwareAccelerated(View view) {
		if (view == null) {
			return;
		}
		
		if (Build.VERSION.SDK_INT >= 11) {
			try {
				Method method = View.class.getMethod("setLayerType", 
						int.class,
						Paint.class);
				method.invoke(view, 1, new Paint()); 
			} catch (Exception e) {
				Logger.warn("setLayerType() failure: %s", e.toString());
			}
		}
	}
	
	public static boolean isLargeScreen(Context context) {
		if (context == null) {
			return false;
		}
		
		final Resources res = context.getResources();
		if (res == null) {
			return false;
		}
		
		final Configuration config = res.getConfiguration();
		if (config == null) {
			return false;
		}
		
        final int screenSize = 
        	(config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK);
        
        return ((screenSize == Configuration.SCREENLAYOUT_SIZE_LARGE)
        		|| (screenSize == SCREENLAYOUT_SIZE_XLARGE));
	}

}

@@ -249,17 +249,88 @@
 oid.
-view.View
+util.DisplayMetrics;%0Aimport android.view.View;%0Aimport android.view.WindowManager
 ;%0A%0Ai
@@ -1829,10 +1829,721 @@
 ));%0A%09%7D%0A%0A
+%09public static int dpToPixel(Context context, int dp) %7B%0A%09%09if (context == null) %7B%0A%09%09%09return dp;%0A%09%09%7D%0A%0A%09%09final Resources res = context.getResources();%0A%09%09if (res == null) %7B%0A%09%09%09return dp;%0A%09%09%7D%0A%0A%09%09DisplayMetrics displayMetrics = res.getDisplayMetrics();%0A%09%09if (displayMetrics == null) %7B%0A%09%09%09return dp;%0A%09%09%7D%0A%0A%09%09return (int)((dp * displayMetrics.density) + 0.5);%0A%09%7D%0A%0A%09public static int pixelToDp(Context context, int px) %7B%0A%09%09if (context == null) %7B%0A%09%09%09return px;%0A%09%09%7D%0A%0A%09%09final Resources res = context.getResources();%0A%09%09if (res == null) %7B%0A%09%09%09return px;%0A%09%09%7D%0A%0A%09%09DisplayMetrics displayMetrics = res.getDisplayMetrics();%0A%09%09if (displayMetrics == null) %7B%0A%09%09%09return px;%0A%09%09%7D%0A%0A%09%09return (int) ((px / displayMetrics.density) + 0.5);%0A%09%7D%0A%0A
 %7D%0A

add dp and px converter in ViewHelper;