1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package android.support.v17.leanback.widget;
15 
16 import android.content.Context;
17 import android.graphics.Rect;
18 import android.util.AttributeSet;
19 import android.view.View;
20 import android.widget.LinearLayout;
21 
22 class ControlBar extends LinearLayout {
23 
24     public interface OnChildFocusedListener {
onChildFocusedListener(View child, View focused)25         public void onChildFocusedListener(View child, View focused);
26     }
27 
28     private int mChildMarginFromCenter;
29     private OnChildFocusedListener mOnChildFocusedListener;
30 
ControlBar(Context context, AttributeSet attrs)31     public ControlBar(Context context, AttributeSet attrs) {
32         super(context, attrs);
33     }
34 
ControlBar(Context context, AttributeSet attrs, int defStyle)35     public ControlBar(Context context, AttributeSet attrs, int defStyle) {
36         super(context, attrs, defStyle);
37     }
38 
39     @Override
requestFocus(int direction, Rect previouslyFocusedRect)40     public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
41         if (getChildCount() > 0) {
42             if (getChildAt(getChildCount() / 2).requestFocus(direction, previouslyFocusedRect)) {
43                 return true;
44             }
45         }
46         return super.requestFocus(direction, previouslyFocusedRect);
47     }
48 
setOnChildFocusedListener(OnChildFocusedListener listener)49     public void setOnChildFocusedListener(OnChildFocusedListener listener) {
50         mOnChildFocusedListener = listener;
51     }
52 
setChildMarginFromCenter(int marginFromCenter)53     public void setChildMarginFromCenter(int marginFromCenter) {
54         mChildMarginFromCenter = marginFromCenter;
55     }
56 
57     @Override
requestChildFocus(View child, View focused)58     public void requestChildFocus (View child, View focused) {
59         super.requestChildFocus(child, focused);
60         if (mOnChildFocusedListener != null) {
61             mOnChildFocusedListener.onChildFocusedListener(child, focused);
62         }
63     }
64 
65     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)66     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
67         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
68         if (mChildMarginFromCenter <= 0) {
69             return;
70         }
71 
72         int totalExtraMargin = 0;
73         for (int i = 0; i < getChildCount() - 1; i++) {
74             View first = getChildAt(i);
75             View second = getChildAt(i+1);
76             int measuredWidth = first.getMeasuredWidth() + second.getMeasuredWidth();
77             int marginStart = mChildMarginFromCenter - measuredWidth / 2;
78             LayoutParams lp = (LayoutParams) second.getLayoutParams();
79             int extraMargin = marginStart - lp.getMarginStart();
80             lp.setMarginStart(marginStart);
81             second.setLayoutParams(lp);
82             totalExtraMargin += extraMargin;
83         }
84         setMeasuredDimension(getMeasuredWidth() + totalExtraMargin, getMeasuredHeight());
85     }
86 }
87