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 package com.android.tv.settings.connectivity.setup;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.util.AttributeSet;
22 import android.view.View;
23 import android.widget.FrameLayout;
24 import android.widget.LinearLayout;
25 import android.widget.TextView;
26 
27 import com.android.tv.settings.R;
28 
29 /**
30  * Provide a custom FrameLayout for the message page. The main purpose is that we need to
31  * dynamically adjust the position of status textView based on the count of lines.
32  */
33 public class MessagePageFrameLayout extends FrameLayout {
34     private float mTitleKeylinePercent;
35 
MessagePageFrameLayout(Context context)36     public MessagePageFrameLayout(Context context) {
37         this(context, null);
38     }
39 
MessagePageFrameLayout(Context context, AttributeSet attrs)40     public MessagePageFrameLayout(Context context, AttributeSet attrs) {
41         this(context, attrs, 0);
42     }
43 
MessagePageFrameLayout(Context context, AttributeSet attrs, int defStyle)44     public MessagePageFrameLayout(Context context, AttributeSet attrs, int defStyle) {
45         super(context, attrs, defStyle);
46         mTitleKeylinePercent = getKeyLinePercent(context);
47     }
48 
49 
getKeyLinePercent(Context context)50     private static float getKeyLinePercent(Context context) {
51         TypedArray ta = context.getTheme().obtainStyledAttributes(
52                 androidx.leanback.R.styleable.LeanbackGuidedStepTheme);
53         float percent = ta.getFloat(
54                 androidx.leanback.R.styleable.LeanbackGuidedStepTheme_guidedStepKeyline,
55                 40);
56         ta.recycle();
57         return percent;
58     }
59 
60     @Override
onLayout(boolean changed, int l, int t, int r, int b)61     protected void onLayout(boolean changed, int l, int t, int r, int b) {
62         super.onLayout(changed, l, t, r, b);
63         TextView mStatusView = getRootView().findViewById(R.id.status_text);
64         View mContentView = getRootView().findViewById(R.id.message_content);
65         int mTitleKeylinePixels = (int) (getMeasuredHeight() * mTitleKeylinePercent / 100);
66 
67         if (mStatusView != null) {
68             LinearLayout.LayoutParams lp =
69                     (LinearLayout.LayoutParams) mStatusView.getLayoutParams();
70 
71             // To make the baseline of the textView match the key line, we need to delete the
72             // existing textView top margin and the baseline offset for the text line.
73             int guidanceTextContainerTop = mTitleKeylinePixels - lp.topMargin;
74             // If there are more than 1 line, always make "second line base line" match
75             // the key line. Otherwise, use the "first line base line".
76             if (mStatusView.getLineCount() > 1) {
77                 guidanceTextContainerTop -= mStatusView.getLayout().getLineBaseline(1);
78             } else {
79                 guidanceTextContainerTop -= mStatusView.getLayout().getLineBaseline(0);
80             }
81 
82             int offset = guidanceTextContainerTop;
83             mContentView.offsetTopAndBottom(offset);
84         }
85     }
86 }
87