1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 package com.android.tv.settings.connectivity.setup;
19 
20 import android.content.Context;
21 import android.content.res.TypedArray;
22 import android.util.AttributeSet;
23 import android.widget.ImageView;
24 import android.widget.RelativeLayout;
25 import android.widget.TextView;
26 
27 import com.android.tv.settings.R;
28 
29 /**
30  * Relative layout implementation that lays out child views based on provided keyline percent(
31  * distance of TitleView baseline from the top).
32  */
33 public class GuidanceRelativeLayout extends RelativeLayout {
34     private float mTitleKeylinePercent;
35 
GuidanceRelativeLayout(Context context)36     public GuidanceRelativeLayout(Context context) {
37         this(context, null);
38     }
39 
GuidanceRelativeLayout(Context context, AttributeSet attrs)40     public GuidanceRelativeLayout(Context context, AttributeSet attrs) {
41         this(context, attrs, 0);
42     }
43 
GuidanceRelativeLayout(Context context, AttributeSet attrs, int defStyle)44     public GuidanceRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
45         super(context, attrs, defStyle);
46         mTitleKeylinePercent = getKeyLinePercent(context);
47     }
48 
getKeyLinePercent(Context context)49     private static float getKeyLinePercent(Context context) {
50         TypedArray ta = context.getTheme().obtainStyledAttributes(
51                 R.styleable.LeanbackGuidedStepTheme);
52         float percent = ta.getFloat(R.styleable.LeanbackGuidedStepTheme_guidedStepKeyline, 40);
53         ta.recycle();
54         return percent;
55     }
56 
57     @Override
onLayout(boolean changed, int l, int t, int r, int b)58     protected void onLayout(boolean changed, int l, int t, int r, int b) {
59         super.onLayout(changed, l, t, r, b);
60 
61         TextView titleView = getRootView().findViewById(R.id.guidance_title);
62         TextView descriptionView = getRootView().findViewById(R.id.guidance_description);
63         ImageView iconView = getRootView().findViewById(R.id.guidance_icon);
64 
65         int mTitleKeylinePixels = (int) (getMeasuredHeight() * mTitleKeylinePercent / 100);
66         if (titleView != null && titleView.getParent() == this) {
67             LayoutParams lp = (LayoutParams) titleView.getLayoutParams();
68 
69             // To make the mid position of a text line match the key line, we need to delete the
70             // existing textview top margin, the icon height and the baseline offset for the text
71             // line.
72             int guidanceTextContainerTop = mTitleKeylinePixels - lp.topMargin;
73 
74             // If there are more than 1 line, always make "second line baseline" match
75             // the key line. Otherwise, use the "first line baseline".
76             if (titleView.getLineCount() > 1) {
77                 guidanceTextContainerTop -= titleView.getLayout().getLineBaseline(1);
78             } else {
79                 guidanceTextContainerTop -= titleView.getLayout().getLineBaseline(0);
80             }
81 
82             int offset = guidanceTextContainerTop;
83             titleView.offsetTopAndBottom(offset);
84 
85             if (descriptionView != null && descriptionView.getParent() == this) {
86                 descriptionView.offsetTopAndBottom(offset);
87             }
88             if (iconView != null && iconView.getParent() == this) {
89                 iconView.offsetTopAndBottom(offset);
90             }
91         }
92     }
93 }
94