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 */
14package android.support.v17.leanback.widget;
15
16import android.content.Context;
17import android.graphics.drawable.Drawable;
18import android.support.v17.leanback.R;
19import android.util.AttributeSet;
20import android.view.LayoutInflater;
21import android.view.View;
22import android.widget.FrameLayout;
23import android.widget.ImageView;
24import android.widget.TextView;
25
26import static android.support.v17.leanback.widget.TitleViewAdapter.BRANDING_VIEW_VISIBLE;
27import static android.support.v17.leanback.widget.TitleViewAdapter.SEARCH_VIEW_VISIBLE;
28import static android.support.v17.leanback.widget.TitleViewAdapter.FULL_VIEW_VISIBLE;
29
30/**
31 * Title view for a leanback fragment.
32 */
33public 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
93    public TitleView(Context context) {
94        this(context, null);
95    }
96
97    public TitleView(Context context, AttributeSet attrs) {
98        this(context, attrs, R.attr.browseTitleViewStyle);
99    }
100
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     */
118    public void setTitle(CharSequence titleText) {
119        mTextView.setText(titleText);
120        updateBadgeVisibility();
121    }
122
123    /**
124     * Returns the title text.
125     */
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     */
134    public void setBadgeDrawable(Drawable drawable) {
135        mBadgeView.setImageDrawable(drawable);
136        updateBadgeVisibility();
137    }
138
139    /**
140     * Returns the badge drawable.
141     */
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     */
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     */
158    public View getSearchAffordanceView() {
159        return mSearchOrbView;
160    }
161
162    /**
163     * Sets the {@link SearchOrbView.Colors} used to draw the search affordance.
164     */
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     */
172    public SearchOrbView.Colors getSearchAffordanceColors() {
173        return mSearchOrbView.getOrbColors();
174    }
175
176    /**
177     * Enables or disables any view animations.
178     */
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     */
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
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
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
222    public TitleViewAdapter getTitleViewAdapter() {
223        return mTitleViewAdapter;
224    }
225}
226