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 static androidx.leanback.widget.TitleViewAdapter.BRANDING_VIEW_VISIBLE;
17 import static androidx.leanback.widget.TitleViewAdapter.FULL_VIEW_VISIBLE;
18 import static androidx.leanback.widget.TitleViewAdapter.SEARCH_VIEW_VISIBLE;
19 
20 import android.content.Context;
21 import android.graphics.drawable.Drawable;
22 import android.util.AttributeSet;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.widget.FrameLayout;
26 import android.widget.ImageView;
27 import android.widget.TextView;
28 
29 import androidx.leanback.R;
30 
31 /**
32  * Title view for a leanback fragment.
33  */
34 public class TitleView extends FrameLayout implements TitleViewAdapter.Provider {
35 
36     private ImageView mBadgeView;
37     private TextView mTextView;
38     private SearchOrbView mSearchOrbView;
39     private int flags = FULL_VIEW_VISIBLE;
40     private boolean mHasSearchListener = false;
41 
42     private final TitleViewAdapter mTitleViewAdapter = new TitleViewAdapter() {
43         @Override
44         public View getSearchAffordanceView() {
45             return TitleView.this.getSearchAffordanceView();
46         }
47 
48         @Override
49         public void setOnSearchClickedListener(View.OnClickListener listener) {
50             TitleView.this.setOnSearchClickedListener(listener);
51         }
52 
53         @Override
54         public void setAnimationEnabled(boolean enable) {
55             TitleView.this.enableAnimation(enable);
56         }
57 
58         @Override
59         public Drawable getBadgeDrawable() {
60             return TitleView.this.getBadgeDrawable();
61         }
62 
63         @Override
64         public SearchOrbView.Colors getSearchAffordanceColors() {
65             return TitleView.this.getSearchAffordanceColors();
66         }
67 
68         @Override
69         public CharSequence getTitle() {
70             return TitleView.this.getTitle();
71         }
72 
73         @Override
74         public void setBadgeDrawable(Drawable drawable) {
75             TitleView.this.setBadgeDrawable(drawable);
76         }
77 
78         @Override
79         public void setSearchAffordanceColors(SearchOrbView.Colors colors) {
80             TitleView.this.setSearchAffordanceColors(colors);
81         }
82 
83         @Override
84         public void setTitle(CharSequence titleText) {
85             TitleView.this.setTitle(titleText);
86         }
87 
88         @Override
89         public void updateComponentsVisibility(int flags) {
90             TitleView.this.updateComponentsVisibility(flags);
91         }
92     };
93 
TitleView(Context context)94     public TitleView(Context context) {
95         this(context, null);
96     }
97 
TitleView(Context context, AttributeSet attrs)98     public TitleView(Context context, AttributeSet attrs) {
99         this(context, attrs, R.attr.browseTitleViewStyle);
100     }
101 
TitleView(Context context, AttributeSet attrs, int defStyleAttr)102     public TitleView(Context context, AttributeSet attrs, int defStyleAttr) {
103         super(context, attrs, defStyleAttr);
104 
105         LayoutInflater inflater = LayoutInflater.from(context);
106         View rootView = inflater.inflate(R.layout.lb_title_view, this);
107 
108         mBadgeView = (ImageView) rootView.findViewById(R.id.title_badge);
109         mTextView = (TextView) rootView.findViewById(R.id.title_text);
110         mSearchOrbView = (SearchOrbView) rootView.findViewById(R.id.title_orb);
111 
112         setClipToPadding(false);
113         setClipChildren(false);
114     }
115 
116     /**
117      * Sets the title text.
118      */
setTitle(CharSequence titleText)119     public void setTitle(CharSequence titleText) {
120         mTextView.setText(titleText);
121         updateBadgeVisibility();
122     }
123 
124     /**
125      * Returns the title text.
126      */
getTitle()127     public CharSequence getTitle() {
128         return mTextView.getText();
129     }
130 
131     /**
132      * Sets the badge drawable.
133      * If non-null, the drawable is displayed instead of the title text.
134      */
setBadgeDrawable(Drawable drawable)135     public void setBadgeDrawable(Drawable drawable) {
136         mBadgeView.setImageDrawable(drawable);
137         updateBadgeVisibility();
138     }
139 
140     /**
141      * Returns the badge drawable.
142      */
getBadgeDrawable()143     public Drawable getBadgeDrawable() {
144         return mBadgeView.getDrawable();
145     }
146 
147     /**
148      * Sets the listener to be called when the search affordance is clicked.
149      */
setOnSearchClickedListener(View.OnClickListener listener)150     public void setOnSearchClickedListener(View.OnClickListener listener) {
151         mHasSearchListener = listener != null;
152         mSearchOrbView.setOnOrbClickedListener(listener);
153         updateSearchOrbViewVisiblity();
154     }
155 
156     /**
157      *  Returns the view for the search affordance.
158      */
getSearchAffordanceView()159     public View getSearchAffordanceView() {
160         return mSearchOrbView;
161     }
162 
163     /**
164      * Sets the {@link SearchOrbView.Colors} used to draw the search affordance.
165      */
setSearchAffordanceColors(SearchOrbView.Colors colors)166     public void setSearchAffordanceColors(SearchOrbView.Colors colors) {
167         mSearchOrbView.setOrbColors(colors);
168     }
169 
170     /**
171      * Returns the {@link SearchOrbView.Colors} used to draw the search affordance.
172      */
getSearchAffordanceColors()173     public SearchOrbView.Colors getSearchAffordanceColors() {
174         return mSearchOrbView.getOrbColors();
175     }
176 
177     /**
178      * Enables or disables any view animations.
179      */
enableAnimation(boolean enable)180     public void enableAnimation(boolean enable) {
181         mSearchOrbView.enableOrbColorAnimation(enable && mSearchOrbView.hasFocus());
182     }
183 
184     /**
185      * Based on the flag, it updates the visibility of the individual components -
186      * BadgeView, TextView and SearchView.
187      *
188      * @param flags integer representing the visibility of TitleView components.
189      * @see TitleViewAdapter#SEARCH_VIEW_VISIBLE
190      * @see TitleViewAdapter#BRANDING_VIEW_VISIBLE
191      * @see TitleViewAdapter#FULL_VIEW_VISIBLE
192      */
updateComponentsVisibility(int flags)193     public void updateComponentsVisibility(int flags) {
194         this.flags = flags;
195 
196         if ((flags & BRANDING_VIEW_VISIBLE) == BRANDING_VIEW_VISIBLE) {
197             updateBadgeVisibility();
198         } else {
199             mBadgeView.setVisibility(View.GONE);
200             mTextView.setVisibility(View.GONE);
201         }
202         updateSearchOrbViewVisiblity();
203     }
204 
updateSearchOrbViewVisiblity()205     private void updateSearchOrbViewVisiblity() {
206         int visibility = mHasSearchListener && (flags & SEARCH_VIEW_VISIBLE) == SEARCH_VIEW_VISIBLE
207                 ? View.VISIBLE : View.INVISIBLE;
208         mSearchOrbView.setVisibility(visibility);
209     }
210 
updateBadgeVisibility()211     private void updateBadgeVisibility() {
212         Drawable drawable = mBadgeView.getDrawable();
213         if (drawable != null) {
214             mBadgeView.setVisibility(View.VISIBLE);
215             mTextView.setVisibility(View.GONE);
216         } else {
217             mBadgeView.setVisibility(View.GONE);
218             mTextView.setVisibility(View.VISIBLE);
219         }
220     }
221 
222     @Override
getTitleViewAdapter()223     public TitleViewAdapter getTitleViewAdapter() {
224         return mTitleViewAdapter;
225     }
226 }
227