1/* This file is auto-generated from BrandedFragment.java.  DO NOT MODIFY. */
2
3/*
4 * Copyright (C) 2014 The Android Open Source Project
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7 * in compliance with the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software distributed under the License
12 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing permissions and limitations under
14 * the License.
15 */
16package android.support.v17.leanback.app;
17
18import android.support.v4.app.Fragment;
19import android.graphics.drawable.Drawable;
20import android.os.Bundle;
21import android.support.v17.leanback.R;
22import android.support.v17.leanback.widget.SearchOrbView;
23import android.support.v17.leanback.widget.TitleHelper;
24import android.support.v17.leanback.widget.TitleViewAdapter;
25import android.util.TypedValue;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.view.ViewGroup;
29
30/**
31 * Fragment class for managing search and branding using a view that implements
32 * {@link TitleViewAdapter.Provider}.
33 */
34public class BrandedSupportFragment extends Fragment {
35
36    // BUNDLE attribute for title is showing
37    private static final String TITLE_SHOW = "titleShow";
38
39    private boolean mShowingTitle = true;
40    private CharSequence mTitle;
41    private Drawable mBadgeDrawable;
42    private View mTitleView;
43    private TitleViewAdapter mTitleViewAdapter;
44    private SearchOrbView.Colors mSearchAffordanceColors;
45    private boolean mSearchAffordanceColorSet;
46    private View.OnClickListener mExternalOnSearchClickedListener;
47    private TitleHelper mTitleHelper;
48
49    /**
50     * Called by {@link #installTitleView(LayoutInflater, ViewGroup, Bundle)} to inflate
51     * title view.  Default implementation uses layout file lb_browse_title.
52     * Subclass may override and use its own layout, the layout must have a descendant with id
53     * browse_title_group that implements {@link TitleViewAdapter.Provider}. Subclass may return
54     * null if no title is needed.
55     *
56     * @param inflater           The LayoutInflater object that can be used to inflate
57     *                           any views in the fragment,
58     * @param parent             Parent of title view.
59     * @param savedInstanceState If non-null, this fragment is being re-constructed
60     *                           from a previous saved state as given here.
61     * @return Title view which must have a descendant with id browse_title_group that implements
62     *         {@link TitleViewAdapter.Provider}, or null for no title view.
63     */
64    public View onInflateTitleView(LayoutInflater inflater, ViewGroup parent,
65                                Bundle savedInstanceState) {
66        TypedValue typedValue = new TypedValue();
67        boolean found = parent.getContext().getTheme().resolveAttribute(
68                R.attr.browseTitleViewLayout, typedValue, true);
69        return inflater.inflate(found ? typedValue.resourceId : R.layout.lb_browse_title,
70                parent, false);
71    }
72
73    /**
74     * Inflate title view and add to parent.  This method should be called in
75     * {@link Fragment#onCreateView(LayoutInflater, ViewGroup, Bundle)}.
76     * @param inflater The LayoutInflater object that can be used to inflate
77     * any views in the fragment,
78     * @param parent Parent of title view.
79     * @param savedInstanceState If non-null, this fragment is being re-constructed
80     * from a previous saved state as given here.
81     */
82    public void installTitleView(LayoutInflater inflater, ViewGroup parent,
83                            Bundle savedInstanceState) {
84        View titleLayoutRoot = onInflateTitleView(inflater, parent, savedInstanceState);
85        if (titleLayoutRoot != null) {
86            parent.addView(titleLayoutRoot);
87            setTitleView(titleLayoutRoot.findViewById(R.id.browse_title_group));
88        } else {
89            setTitleView(null);
90        }
91    }
92
93    /**
94     * Sets the view that implemented {@link TitleViewAdapter}.
95     * @param titleView The view that implemented {@link TitleViewAdapter.Provider}.
96     */
97    public void setTitleView(View titleView) {
98        mTitleView = titleView;
99        if (mTitleView == null) {
100            mTitleViewAdapter = null;
101            mTitleHelper = null;
102        } else {
103            mTitleViewAdapter = ((TitleViewAdapter.Provider) mTitleView).getTitleViewAdapter();
104            mTitleViewAdapter.setTitle(mTitle);
105            mTitleViewAdapter.setBadgeDrawable(mBadgeDrawable);
106            if (mSearchAffordanceColorSet) {
107                mTitleViewAdapter.setSearchAffordanceColors(mSearchAffordanceColors);
108            }
109            if (mExternalOnSearchClickedListener != null) {
110                setOnSearchClickedListener(mExternalOnSearchClickedListener);
111            }
112            if (getView() instanceof ViewGroup) {
113                mTitleHelper = new TitleHelper((ViewGroup) getView(), mTitleView);
114            }
115        }
116    }
117
118    /**
119     * Returns the view that implements {@link TitleViewAdapter.Provider}.
120     * @return The view that implements {@link TitleViewAdapter.Provider}.
121     */
122    public View getTitleView() {
123        return mTitleView;
124    }
125
126    /**
127     * Returns the {@link TitleViewAdapter} implemented by title view.
128     * @return The {@link TitleViewAdapter} implemented by title view.
129     */
130    public TitleViewAdapter getTitleViewAdapter() {
131        return mTitleViewAdapter;
132    }
133
134    /**
135     * Returns the {@link TitleHelper}.
136     */
137    TitleHelper getTitleHelper() {
138        return mTitleHelper;
139    }
140
141    @Override
142    public void onSaveInstanceState(Bundle outState) {
143        super.onSaveInstanceState(outState);
144        outState.putBoolean(TITLE_SHOW, mShowingTitle);
145    }
146
147    @Override
148    public void onViewCreated(View view, Bundle savedInstanceState) {
149        super.onViewCreated(view, savedInstanceState);
150        if (savedInstanceState != null) {
151            mShowingTitle = savedInstanceState.getBoolean(TITLE_SHOW);
152        }
153        if (mTitleView != null && view instanceof ViewGroup) {
154            mTitleHelper = new TitleHelper((ViewGroup) view, mTitleView);
155        }
156    }
157
158    @Override
159    public void onDestroyView() {
160        super.onDestroyView();
161        mTitleHelper = null;
162    }
163
164    /**
165     * Shows or hides the title view.
166     * @param show True to show title view, false to hide title view.
167     */
168    public void showTitle(boolean show) {
169        // TODO: handle interruptions?
170        if (show == mShowingTitle) {
171            return;
172        }
173        mShowingTitle = show;
174        if (mTitleHelper != null) {
175            mTitleHelper.showTitle(show);
176        }
177    }
178
179    /**
180     * Changes title view's components visibility and shows title.
181     * @param flags Flags representing the visibility of components inside title view.
182     * @see TitleViewAdapter#SEARCH_VIEW_VISIBLE
183     * @see TitleViewAdapter#BRANDING_VIEW_VISIBLE
184     * @see TitleViewAdapter#FULL_VIEW_VISIBLE
185     * @see TitleViewAdapter#updateComponentsVisibility(int)
186     */
187    public void showTitle(int flags) {
188        if (mTitleViewAdapter != null) {
189            mTitleViewAdapter.updateComponentsVisibility(flags);
190        }
191        showTitle(true);
192    }
193
194    /**
195     * Sets the drawable displayed in the fragment title.
196     *
197     * @param drawable The Drawable to display in the fragment title.
198     */
199    public void setBadgeDrawable(Drawable drawable) {
200        if (mBadgeDrawable != drawable) {
201            mBadgeDrawable = drawable;
202            if (mTitleViewAdapter != null) {
203                mTitleViewAdapter.setBadgeDrawable(drawable);
204            }
205        }
206    }
207
208    /**
209     * Returns the badge drawable used in the fragment title.
210     * @return The badge drawable used in the fragment title.
211     */
212    public Drawable getBadgeDrawable() {
213        return mBadgeDrawable;
214    }
215
216    /**
217     * Sets title text for the fragment.
218     *
219     * @param title The title text of the fragment.
220     */
221    public void setTitle(CharSequence title) {
222        mTitle = title;
223        if (mTitleViewAdapter != null) {
224            mTitleViewAdapter.setTitle(title);
225        }
226    }
227
228    /**
229     * Returns the title text for the fragment.
230     * @return Title text for the fragment.
231     */
232    public CharSequence getTitle() {
233        return mTitle;
234    }
235
236    /**
237     * Sets a click listener for the search affordance.
238     *
239     * <p>The presence of a listener will change the visibility of the search
240     * affordance in the fragment title. When set to non-null, the title will
241     * contain an element that a user may click to begin a search.
242     *
243     * <p>The listener's {@link View.OnClickListener#onClick onClick} method
244     * will be invoked when the user clicks on the search element.
245     *
246     * @param listener The listener to call when the search element is clicked.
247     */
248    public void setOnSearchClickedListener(View.OnClickListener listener) {
249        mExternalOnSearchClickedListener = listener;
250        if (mTitleViewAdapter != null) {
251            mTitleViewAdapter.setOnSearchClickedListener(listener);
252        }
253    }
254
255    /**
256     * Sets the {@link android.support.v17.leanback.widget.SearchOrbView.Colors} used to draw the
257     * search affordance.
258     *
259     * @param colors Colors used to draw search affordance.
260     */
261    public void setSearchAffordanceColors(SearchOrbView.Colors colors) {
262        mSearchAffordanceColors = colors;
263        mSearchAffordanceColorSet = true;
264        if (mTitleViewAdapter != null) {
265            mTitleViewAdapter.setSearchAffordanceColors(mSearchAffordanceColors);
266        }
267    }
268
269    /**
270     * Returns the {@link android.support.v17.leanback.widget.SearchOrbView.Colors}
271     * used to draw the search affordance.
272     */
273    public SearchOrbView.Colors getSearchAffordanceColors() {
274        if (mSearchAffordanceColorSet) {
275            return mSearchAffordanceColors;
276        }
277        if (mTitleViewAdapter == null) {
278            throw new IllegalStateException("Fragment views not yet created");
279        }
280        return mTitleViewAdapter.getSearchAffordanceColors();
281    }
282
283    /**
284     * Sets the color used to draw the search affordance.
285     * A default brighter color will be set by the framework.
286     *
287     * @param color The color to use for the search affordance.
288     */
289    public void setSearchAffordanceColor(int color) {
290        setSearchAffordanceColors(new SearchOrbView.Colors(color));
291    }
292
293    /**
294     * Returns the color used to draw the search affordance.
295     */
296    public int getSearchAffordanceColor() {
297        return getSearchAffordanceColors().color;
298    }
299
300    @Override
301    public void onStart() {
302        super.onStart();
303        if (mTitleViewAdapter != null) {
304            showTitle(mShowingTitle);
305            mTitleViewAdapter.setAnimationEnabled(true);
306        }
307    }
308
309    @Override
310    public void onPause() {
311        if (mTitleViewAdapter != null) {
312            mTitleViewAdapter.setAnimationEnabled(false);
313        }
314        super.onPause();
315    }
316
317    @Override
318    public void onResume() {
319        super.onResume();
320        if (mTitleViewAdapter != null) {
321            mTitleViewAdapter.setAnimationEnabled(true);
322        }
323    }
324
325    /**
326     * Returns true/false to indicate the visibility of TitleView.
327     *
328     * @return boolean to indicate whether or not it's showing the title.
329     */
330    public final boolean isShowingTitle() {
331        return mShowingTitle;
332    }
333
334}
335