package com.alburivan.slickform.tooltip; /* * The MIT License (MIT) * <p/> * Copyright (c) 2016 Douglas Nassif Roma Junior * <p/> * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * <p/> * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * <p/> * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import android.content.res.Resources; import android.graphics.RectF; import android.os.Build; import android.support.annotation.StyleRes; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; import android.view.ViewTreeObserver; import android.widget.TextView; /** * SimpleTooltipUtils * Created by douglas on 09/05/16. */ @SuppressWarnings({"SameParameterValue", "unused"}) public final class SimpleTooltipUtils { private SimpleTooltipUtils() { } public static RectF calculeRectOnScreen(View view) { int[] location = new int[2]; view.getLocationOnScreen(location); return new RectF(location[0], location[1], location[0] + view.getMeasuredWidth(), location[1] + view.getMeasuredHeight()); } public static RectF calculeRectInWindow(View view) { int[] location = new int[2]; view.getLocationInWindow(location); return new RectF(location[0], location[1], location[0] + view.getMeasuredWidth(), location[1] + view.getMeasuredHeight()); } public static float dpFromPx(float px) { return px / Resources.getSystem().getDisplayMetrics().density; } public static float pxFromDp(float dp) { return dp * Resources.getSystem().getDisplayMetrics().density; } public static void setWidth(View view, float width) { ViewGroup.LayoutParams params = view.getLayoutParams(); if (params == null) { params = new ViewGroup.LayoutParams((int) width, view.getHeight()); } else { params.width = (int) width; } view.setLayoutParams(params); } public static int tooltipGravityToArrowDirection(int tooltipGravity) { switch (tooltipGravity) { case Gravity.START: return ArrowDrawable.RIGHT; case Gravity.END: return ArrowDrawable.LEFT; case Gravity.TOP: return ArrowDrawable.BOTTOM; case Gravity.BOTTOM: return ArrowDrawable.TOP; default: throw new IllegalArgumentException("Gravity must have be START, END, TOP or BOTTOM."); } } public static void setX(View view, int x) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { view.setX(x); } else { ViewGroup.MarginLayoutParams marginParams = getOrCreateMarginLayoutParams(view); marginParams.leftMargin = x - view.getLeft(); view.setLayoutParams(marginParams); } } public static void setY(View view, int y) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { view.setY(y); } else { ViewGroup.MarginLayoutParams marginParams = getOrCreateMarginLayoutParams(view); marginParams.topMargin = y - view.getTop(); view.setLayoutParams(marginParams); } } private static ViewGroup.MarginLayoutParams getOrCreateMarginLayoutParams(View view) { ViewGroup.LayoutParams lp = view.getLayoutParams(); if (lp != null) { if (lp instanceof ViewGroup.MarginLayoutParams) { return (ViewGroup.MarginLayoutParams) lp; } else { return new ViewGroup.MarginLayoutParams(lp); } } else { return new ViewGroup.MarginLayoutParams(view.getWidth(), view.getHeight()); } } public static void removeOnGlobalLayoutListener(View view, ViewTreeObserver.OnGlobalLayoutListener listener) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { view.getViewTreeObserver().removeOnGlobalLayoutListener(listener); } else { //noinspection deprecation view.getViewTreeObserver().removeGlobalOnLayoutListener(listener); } } public static void setTextAppearance(TextView tv, @StyleRes int textAppearanceRes) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { tv.setTextAppearance(textAppearanceRes); } else { //noinspection deprecation tv.setTextAppearance(tv.getContext(), textAppearanceRes); } } public static boolean isShown(View mContentLayout) { if (!mContentLayout.isShown()) return false; ViewParent parent = mContentLayout.getParent(); do { if (parent instanceof View && !((View) parent).isShown()) return false; else System.out.println(parent.getClass()); } while ((parent = parent.getParent()) != null); return true; } }