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