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