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