package com.fxy.baselibrary.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;


/**
 * 屏幕密度、大小等相关函数类
 * 
 * @author WuRS
 * 
 */
public class DisplayUtil {

	/**
	 * 获取状态栏高度
	 * 
	 * @return
	 */
	public static int getStatusHeight(Activity activity) {
		int statusHeight = 0;
		Rect localRect = new Rect();
		activity.getWindow().getDecorView()
				.getWindowVisibleDisplayFrame(localRect);
		statusHeight = localRect.top;
		if (0 == statusHeight) {
			Class<?> localClass;
			try {
				localClass = Class.forName("com.android.internal.R$dimen");
				Object localObject = localClass.newInstance();
				int i5 = Integer.parseInt(localClass
						.getField("status_bar_height").get(localObject)
						.toString());
				statusHeight = activity.getResources()
						.getDimensionPixelSize(i5);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			}
		}
		return statusHeight;
	}

	/**
	 * 获取屏幕大小
	 * 
	 * @return
	 */
	public static Point getDisplayMetrics(Context mContext) {
		DisplayMetrics dm = new DisplayMetrics();
		dm = mContext.getResources().getDisplayMetrics();
		int screenWidth = dm.widthPixels;
		int screenHeight = dm.heightPixels;
		return new Point(screenWidth, screenHeight);
	}

	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dip2px(Context mContext, int dpValue) {
		final float scale = mContext.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	/**
	 * 测量view的宽高
	 * 
	 * @param view
	 */
	public static void measureView(View view) {
		LayoutParams params = view.getLayoutParams();
		if (params == null) {
			params = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT);
		}
		// 获得精确的宽度、高度
		int widthMeasureSpec = MeasureSpec.makeMeasureSpec(params.width,
				MeasureSpec.EXACTLY);
		int heightMeasureSpec = MeasureSpec.makeMeasureSpec(params.height,
				MeasureSpec.EXACTLY);
		view.measure(widthMeasureSpec, heightMeasureSpec);
	}

	/**
	 * 计算ListView的高度
	 * 
	 * @param lv
	 */
	public static void measureLVHeight(ListView lv) {
		ListAdapter adapter = lv.getAdapter();
		if (adapter != null) {
			int totalHeight = 0;
			for (int i = 0; i < adapter.getCount(); i++) {
				View view = adapter.getView(i, null, null);
				view.measure(0, 0);
				totalHeight += view.getMeasuredHeight();
			}
			LayoutParams params = (LayoutParams) lv
					.getLayoutParams();
			params.height = totalHeight
					+ (lv.getDividerHeight() * (adapter.getCount() - 1));
			lv.setLayoutParams(params);
		}
	}

	/**
	 * 计算GridView的高度
	 * 
	 * @param gv
	 */
	@SuppressLint("NewApi")
	public static void measureGridViewHeight(GridView gv) {
		ListAdapter adapter = gv.getAdapter();
		if (adapter != null) {
			int totalHeight = 0;
			for (int i = 0; i < adapter.getCount(); i++) {
				View view = adapter.getView(i, null, null);
				view.measure(0, 0);
				totalHeight += view.getMeasuredHeight();
			}
			LayoutParams params = (LayoutParams) gv
					.getLayoutParams();
			if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
				params.height = totalHeight
						+ (gv.getVerticalSpacing() * (adapter.getCount() - 1));
			} else {
				params.height = totalHeight;
			}
			gv.setLayoutParams(params);
		}
	}

	/**
	 * 计算ListView的高度，父控件为LinearLayout
	 * 
	 * @param lv
	 */
	public static void measureLinearLayoutLVHeight(ListView lv) {
		ListAdapter adapter = lv.getAdapter();
		if (adapter != null) {
			int totalHeight = 0;
			for (int i = 0; i < adapter.getCount(); i++) {
				View view = adapter.getView(i, null, null);
				view.measure(0, 0);
				totalHeight += view.getMeasuredHeight();
			}
			LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) lv
					.getLayoutParams();
			params.height = totalHeight
					+ (lv.getDividerHeight() * (adapter.getCount() - 1));
			lv.setLayoutParams(params);
		}
	}

	/**
	 * 计算ListView的高度，父控件为FrameLayout
	 * 
	 * @param lv
	 */
	public static void measureFrameLayoutLVHeight(ListView lv) {
		ListAdapter adapter = lv.getAdapter();
		if (adapter != null) {
			int totalHeight = 0;
			for (int i = 0; i < adapter.getCount(); i++) {
				View view = adapter.getView(i, null, null);
				view.measure(0, 0);
				totalHeight += view.getMeasuredHeight();
			}
			FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) lv
					.getLayoutParams();
			params.height = totalHeight
					+ (lv.getDividerHeight() * (adapter.getCount() - 1));
			lv.setLayoutParams(params);
		}
	}

}
