• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package android.support.v17.leanback.widget;
2 
3 import android.content.Context;
4 import android.content.res.TypedArray;
5 import android.support.v17.leanback.R;
6 import android.util.AttributeSet;
7 import android.view.KeyEvent;
8 import android.view.View;
9 import android.view.ViewGroup;
10 import android.widget.RelativeLayout;
11 
12 /**
13  * Relative layout implementation that assign subactions list topMargin based on a percentage
14  * given by "guidedStepKeyline" theme attribute when the topMargin is set to a negative value.
15  */
16 class GuidedActionsRelativeLayout extends RelativeLayout {
17 
18     interface InterceptKeyEventListener {
onInterceptKeyEvent(KeyEvent event)19         public boolean onInterceptKeyEvent(KeyEvent event);
20     }
21 
22     private float mKeyLinePercent;
23     private boolean mInOverride = false;
24     private InterceptKeyEventListener mInterceptKeyEventListener;
25 
GuidedActionsRelativeLayout(Context context)26     public GuidedActionsRelativeLayout(Context context) {
27         this(context, null);
28     }
29 
GuidedActionsRelativeLayout(Context context, AttributeSet attrs)30     public GuidedActionsRelativeLayout(Context context, AttributeSet attrs) {
31         this(context, attrs, 0);
32     }
33 
GuidedActionsRelativeLayout(Context context, AttributeSet attrs, int defStyle)34     public GuidedActionsRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
35         super(context, attrs, defStyle);
36         mKeyLinePercent = GuidanceStylingRelativeLayout.getKeyLinePercent(context);
37     }
38 
init()39     private void init() {
40         TypedArray ta = getContext().getTheme().obtainStyledAttributes(
41                 R.styleable.LeanbackGuidedStepTheme);
42         mKeyLinePercent = ta.getFloat(R.styleable.LeanbackGuidedStepTheme_guidedStepKeyline,
43                 40);
44         ta.recycle();
45     }
46 
47     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)48     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
49         final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
50         if (heightSize > 0) {
51             View view = findViewById(R.id.guidedactions_sub_list);
52             if (view != null) {
53                 ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams)
54                         view.getLayoutParams();
55                 if (lp.topMargin < 0 && !mInOverride) {
56                     mInOverride = true;
57                 }
58                 if (mInOverride) {
59                     lp.topMargin = (int) (mKeyLinePercent * heightSize / 100);
60                 }
61             }
62         }
63         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
64     }
65 
66     @Override
onLayout(boolean changed, int l, int t, int r, int b)67     protected void onLayout(boolean changed, int l, int t, int r, int b) {
68         super.onLayout(changed, l, t, r, b);
69         mInOverride = false;
70     }
71 
setInterceptKeyEventListener(InterceptKeyEventListener l)72     public void setInterceptKeyEventListener(InterceptKeyEventListener l) {
73         mInterceptKeyEventListener = l;
74     }
75 
76     @Override
dispatchKeyEvent(KeyEvent event)77     public boolean dispatchKeyEvent(KeyEvent event) {
78         if (mInterceptKeyEventListener != null) {
79             if (mInterceptKeyEventListener.onInterceptKeyEvent(event)) {
80                 return true;
81             }
82         }
83         return super.dispatchKeyEvent(event);
84     }
85 }
86