1package android.support.v17.leanback.widget;
2
3import android.content.Context;
4import android.content.res.TypedArray;
5import android.graphics.Paint;
6import android.graphics.drawable.Drawable;
7import android.support.v17.leanback.R;
8import android.util.AttributeSet;
9import android.widget.ImageView;
10import android.widget.RelativeLayout;
11import android.widget.TextView;
12
13/**
14 * Relative layout implementation that lays out child views based on provided keyline percent(
15 * distance of TitleView baseline from the top).
16 *
17 * Repositioning child views in PreDraw callback in {@link GuidanceStylist} was interfering with
18 * fragment transition. To avoid that, we do that in the onLayout pass.
19 *
20 * @hide
21 */
22class GuidanceStylingRelativeLayout extends RelativeLayout {
23    private float mTitleKeylinePercent;
24
25    public GuidanceStylingRelativeLayout(Context context) {
26        this(context, null);
27    }
28
29    public GuidanceStylingRelativeLayout(Context context, AttributeSet attrs) {
30        this(context, attrs, 0);
31    }
32
33    public GuidanceStylingRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
34        super(context, attrs, defStyle);
35        init();
36    }
37
38    private void init() {
39        TypedArray ta = getContext().getTheme().obtainStyledAttributes(
40                R.styleable.LeanbackGuidedStepTheme);
41        mTitleKeylinePercent = ta.getFloat(R.styleable.LeanbackGuidedStepTheme_guidedStepKeyline,
42                40);
43        ta.recycle();
44    }
45
46    @Override
47    protected void onLayout(boolean changed, int l, int t, int r, int b) {
48        super.onLayout(changed, l, t, r, b);
49
50        TextView mTitleView = (TextView) getRootView().findViewById(R.id.guidance_title);
51        TextView mBreadcrumbView = (TextView) getRootView().findViewById(R.id.guidance_breadcrumb);
52        TextView mDescriptionView = (TextView) getRootView().findViewById(
53                R.id.guidance_description);
54        ImageView mIconView = (ImageView) getRootView().findViewById(R.id.guidance_icon);
55        int mTitleKeylinePixels = (int) (getMeasuredHeight() * mTitleKeylinePercent / 100);
56
57        if (mTitleView != null && mTitleView.getParent() == this) {
58            Paint textPaint = mTitleView.getPaint();
59            int titleViewTextHeight = -textPaint.getFontMetricsInt().top;
60            int mBreadcrumbViewHeight = mBreadcrumbView.getMeasuredHeight();
61            int guidanceTextContainerTop = mTitleKeylinePixels
62                    - titleViewTextHeight - mBreadcrumbViewHeight - mTitleView.getPaddingTop();
63            int offset = guidanceTextContainerTop - mBreadcrumbView.getTop();
64
65            if (mBreadcrumbView != null && mBreadcrumbView.getParent() == this) {
66                mBreadcrumbView.offsetTopAndBottom(offset);
67            }
68
69            mTitleView.offsetTopAndBottom(offset);
70
71            if (mDescriptionView != null && mDescriptionView.getParent() == this) {
72                mDescriptionView.offsetTopAndBottom(offset);
73            }
74        }
75
76        if (mIconView != null && mIconView.getParent() == this) {
77            Drawable drawable = mIconView.getDrawable();
78            if (drawable != null) {
79                mIconView.offsetTopAndBottom(
80                        mTitleKeylinePixels - mIconView.getMeasuredHeight() / 2);
81            }
82        }
83    }
84}
85