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