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