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.TitleView;
25import android.view.View;
26import android.view.ViewGroup;
27
28/**
29 * Fragment support for managing branding on a
30 * {@link android.support.v17.leanback.widget.TitleView}.
31 * @hide
32 */
33class BrandedSupportFragment extends Fragment {
34
35    // BUNDLE attribute for title is showing
36    private static final String TITLE_SHOW = "titleShow";
37
38    private boolean mShowingTitle = true;
39    private String mTitle;
40    private Drawable mBadgeDrawable;
41    private TitleView mTitleView;
42    private SearchOrbView.Colors mSearchAffordanceColors;
43    private boolean mSearchAffordanceColorSet;
44    private View.OnClickListener mExternalOnSearchClickedListener;
45    private TitleHelper mTitleHelper;
46
47    /**
48     * Sets the {@link TitleView}.
49     */
50    void setTitleView(TitleView titleView) {
51        mTitleView = titleView;
52        if (mTitleView == null) {
53            mTitleHelper = null;
54        } else {
55            mTitleView.setTitle(mTitle);
56            mTitleView.setBadgeDrawable(mBadgeDrawable);
57            if (mSearchAffordanceColorSet) {
58                mTitleView.setSearchAffordanceColors(mSearchAffordanceColors);
59            }
60            if (mExternalOnSearchClickedListener != null) {
61                mTitleView.setOnSearchClickedListener(mExternalOnSearchClickedListener);
62            }
63            if (getView() instanceof ViewGroup) {
64                mTitleHelper = new TitleHelper((ViewGroup) getView(), mTitleView);
65            }
66        }
67    }
68
69    /**
70     * Returns the {@link TitleView}.
71     */
72    TitleView getTitleView() {
73        return mTitleView;
74    }
75
76    /**
77     * Returns the {@link TitleHelper}.
78     */
79    TitleHelper getTitleHelper() {
80        return mTitleHelper;
81    }
82
83    @Override
84    public void onSaveInstanceState(Bundle outState) {
85        super.onSaveInstanceState(outState);
86        outState.putBoolean(TITLE_SHOW, mShowingTitle);
87    }
88
89    @Override
90    public void onViewCreated(View view, Bundle savedInstanceState) {
91        super.onViewCreated(view, savedInstanceState);
92        if (savedInstanceState != null) {
93            mShowingTitle = savedInstanceState.getBoolean(TITLE_SHOW);
94        }
95        if (mTitleView != null && view instanceof ViewGroup) {
96            mTitleHelper = new TitleHelper((ViewGroup) view, mTitleView);
97        }
98    }
99
100    @Override
101    public void onDestroyView() {
102        super.onDestroyView();
103        mTitleHelper = null;
104    }
105
106    /**
107     * Shows or hides the {@link android.support.v17.leanback.widget.TitleView}.
108     */
109    void showTitle(boolean show) {
110        // TODO: handle interruptions?
111        if (show == mShowingTitle) {
112            return;
113        }
114        mShowingTitle = show;
115        if (mTitleHelper != null) {
116            mTitleHelper.showTitle(show);
117        }
118    }
119
120    /**
121     * Sets the drawable displayed in the browse fragment title.
122     *
123     * @param drawable The Drawable to display in the browse fragment title.
124     */
125    public void setBadgeDrawable(Drawable drawable) {
126        if (mBadgeDrawable != drawable) {
127            mBadgeDrawable = drawable;
128            if (mTitleView != null) {
129                mTitleView.setBadgeDrawable(drawable);
130            }
131        }
132    }
133
134    /**
135     * Returns the badge drawable used in the fragment title.
136     */
137    public Drawable getBadgeDrawable() {
138        return mBadgeDrawable;
139    }
140
141    /**
142     * Sets a title for the browse fragment.
143     *
144     * @param title The title of the browse fragment.
145     */
146    public void setTitle(String title) {
147        mTitle = title;
148        if (mTitleView != null) {
149            mTitleView.setTitle(title);
150        }
151    }
152
153    /**
154     * Returns the title for the browse fragment.
155     */
156    public String getTitle() {
157        return mTitle;
158    }
159
160    /**
161     * Sets a click listener for the search affordance.
162     *
163     * <p>The presence of a listener will change the visibility of the search
164     * affordance in the fragment title. When set to non-null, the title will
165     * contain an element that a user may click to begin a search.
166     *
167     * <p>The listener's {@link View.OnClickListener#onClick onClick} method
168     * will be invoked when the user clicks on the search element.
169     *
170     * @param listener The listener to call when the search element is clicked.
171     */
172    public void setOnSearchClickedListener(View.OnClickListener listener) {
173        mExternalOnSearchClickedListener = listener;
174        if (mTitleView != null) {
175            mTitleView.setOnSearchClickedListener(listener);
176        }
177    }
178
179    /**
180     * Sets the {@link android.support.v17.leanback.widget.SearchOrbView.Colors} used to draw the search affordance.
181     */
182    public void setSearchAffordanceColors(SearchOrbView.Colors colors) {
183        mSearchAffordanceColors = colors;
184        mSearchAffordanceColorSet = true;
185        if (mTitleView != null) {
186            mTitleView.setSearchAffordanceColors(mSearchAffordanceColors);
187        }
188    }
189
190    /**
191     * Returns the {@link android.support.v17.leanback.widget.SearchOrbView.Colors}
192     * used to draw the search affordance.
193     */
194    public SearchOrbView.Colors getSearchAffordanceColors() {
195        if (mSearchAffordanceColorSet) {
196            return mSearchAffordanceColors;
197        }
198        if (mTitleView == null) {
199            throw new IllegalStateException("Fragment views not yet created");
200        }
201        return mTitleView.getSearchAffordanceColors();
202    }
203
204    /**
205     * Sets the color used to draw the search affordance.
206     * A default brighter color will be set by the framework.
207     *
208     * @param color The color to use for the search affordance.
209     */
210    public void setSearchAffordanceColor(int color) {
211        setSearchAffordanceColors(new SearchOrbView.Colors(color));
212    }
213
214    /**
215     * Returns the color used to draw the search affordance.
216     */
217    public int getSearchAffordanceColor() {
218        return getSearchAffordanceColors().color;
219    }
220
221    @Override
222    public void onStart() {
223        super.onStart();
224        if (mTitleView != null) {
225            mTitleView.setVisibility(mShowingTitle ? View.VISIBLE : View.INVISIBLE);
226        }
227    }
228
229    @Override
230    public void onPause() {
231        if (mTitleView != null) {
232            mTitleView.enableAnimation(false);
233        }
234        super.onPause();
235    }
236
237    @Override
238    public void onResume() {
239        super.onResume();
240        if (mTitleView != null) {
241            mTitleView.enableAnimation(true);
242        }
243    }
244}
245