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.Canvas;
18 import android.graphics.drawable.ColorDrawable;
19 import android.graphics.drawable.Drawable;
20 import android.support.annotation.ColorInt;
21 import android.support.v17.leanback.R;
22 import android.util.AttributeSet;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.LinearLayout;
27 
28 /**
29  * RowContainerView wraps header and user defined row view
30  */
31 final class RowContainerView extends LinearLayout {
32 
33     private ViewGroup mHeaderDock;
34     private Drawable mForeground;
35     private boolean mForegroundBoundsChanged = true;
36 
RowContainerView(Context context)37     public RowContainerView(Context context) {
38         this(context, null, 0);
39     }
40 
RowContainerView(Context context, AttributeSet attrs)41     public RowContainerView(Context context, AttributeSet attrs) {
42         this(context, attrs, 0);
43     }
44 
RowContainerView(Context context, AttributeSet attrs, int defStyle)45     public RowContainerView(Context context, AttributeSet attrs, int defStyle) {
46         super(context, attrs, defStyle);
47         setOrientation(VERTICAL);
48         LayoutInflater inflater = LayoutInflater.from(context);
49         inflater.inflate(R.layout.lb_row_container, this);
50 
51         mHeaderDock = (ViewGroup) findViewById(R.id.lb_row_container_header_dock);
52         setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
53     }
54 
addHeaderView(View headerView)55     public void addHeaderView(View headerView) {
56         if (mHeaderDock.indexOfChild(headerView) < 0) {
57             mHeaderDock.addView(headerView, 0);
58         }
59     }
60 
removeHeaderView(View headerView)61     public void removeHeaderView(View headerView) {
62         if (mHeaderDock.indexOfChild(headerView) >= 0) {
63             mHeaderDock.removeView(headerView);
64         }
65     }
66 
addRowView(View view)67     public void addRowView(View view) {
68         addView(view);
69     }
70 
showHeader(boolean show)71     public void showHeader(boolean show) {
72         mHeaderDock.setVisibility(show ? View.VISIBLE : View.GONE);
73     }
74 
setForeground(Drawable d)75     public void setForeground(Drawable d) {
76         mForeground = d;
77         setWillNotDraw(mForeground == null);
78         invalidate();
79     }
80 
setForegroundColor(@olorInt int color)81     public void setForegroundColor(@ColorInt int color) {
82         if (mForeground instanceof ColorDrawable) {
83             ((ColorDrawable) mForeground.mutate()).setColor(color);
84             invalidate();
85         } else {
86             setForeground(new ColorDrawable(color));
87         }
88     }
89 
getForeground()90     public Drawable getForeground() {
91         return mForeground;
92     }
93 
94     @Override
onSizeChanged(int w, int h, int oldw, int oldh)95     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
96         super.onSizeChanged(w, h, oldw, oldh);
97         mForegroundBoundsChanged = true;
98     }
99 
100     @Override
draw(Canvas canvas)101     public void draw(Canvas canvas) {
102         super.draw(canvas);
103         if (mForeground != null) {
104             if (mForegroundBoundsChanged) {
105                 mForegroundBoundsChanged = false;
106                 mForeground.setBounds(0, 0, getWidth(), getHeight());
107             }
108             mForeground.draw(canvas);
109         }
110     }
111 }
112