1b86a6c2c71514e0cb693597a955e224811b90f96Dake Gu// CHECKSTYLE:OFF Generated code
2b86a6c2c71514e0cb693597a955e224811b90f96Dake Gu/* This file is auto-generated from HeadersSupportFragment.java.  DO NOT MODIFY. */
3b86a6c2c71514e0cb693597a955e224811b90f96Dake Gu
455c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout/*
555c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout * Copyright (C) 2014 The Android Open Source Project
655c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout *
755c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
855c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout * in compliance with the License. You may obtain a copy of the License at
955c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout *
1055c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout * http://www.apache.org/licenses/LICENSE-2.0
1155c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout *
1255c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout * Unless required by applicable law or agreed to in writing, software distributed under the License
1355c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
1455c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout * or implied. See the License for the specific language governing permissions and limitations under
1555c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout * the License.
1655c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout */
1755c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout
18ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikaspackage androidx.leanback.app;
1955c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout
203659dc62f9e55b1043edb4105c311c8ef997f2aeDake Guimport android.content.Context;
217a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Guimport android.graphics.Color;
229a49e322d10c1a67860d812352a5ea710843b75eDake Guimport android.graphics.drawable.ColorDrawable;
237a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Guimport android.graphics.drawable.Drawable;
247a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Guimport android.graphics.drawable.GradientDrawable;
253c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Guimport android.os.Bundle;
268619e0ef7062b6a714f22af993e4b440fae7ef08Aurimas Liutikasimport android.view.View;
278619e0ef7062b6a714f22af993e4b440fae7ef08Aurimas Liutikasimport android.view.View.OnLayoutChangeListener;
288619e0ef7062b6a714f22af993e4b440fae7ef08Aurimas Liutikasimport android.view.ViewGroup;
298619e0ef7062b6a714f22af993e4b440fae7ef08Aurimas Liutikasimport android.widget.FrameLayout;
308619e0ef7062b6a714f22af993e4b440fae7ef08Aurimas Liutikas
31ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.annotation.NonNull;
32ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.annotation.Nullable;
33ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.leanback.R;
34ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.leanback.widget.ClassPresenterSelector;
35ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.leanback.widget.DividerPresenter;
36ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.leanback.widget.DividerRow;
37ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.leanback.widget.FocusHighlightHelper;
38ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.leanback.widget.HorizontalGridView;
39ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.leanback.widget.ItemBridgeAdapter;
40ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.leanback.widget.PresenterSelector;
41ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.leanback.widget.Row;
42ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.leanback.widget.RowHeaderPresenter;
43ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.leanback.widget.SectionRow;
44ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.leanback.widget.VerticalGridView;
45ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport androidx.recyclerview.widget.RecyclerView;
4655c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout
4755c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout/**
4850cf9ada93e50e906f20f5edf595234ada196d45Dake Gu * An fragment containing a list of row headers. Implementation must support three types of rows:
4950cf9ada93e50e906f20f5edf595234ada196d45Dake Gu * <ul>
5050cf9ada93e50e906f20f5edf595234ada196d45Dake Gu *     <li>{@link DividerRow} rendered by {@link DividerPresenter}.</li>
5150cf9ada93e50e906f20f5edf595234ada196d45Dake Gu *     <li>{@link Row} rendered by {@link RowHeaderPresenter}.</li>
5250cf9ada93e50e906f20f5edf595234ada196d45Dake Gu *     <li>{@link SectionRow} rendered by {@link RowHeaderPresenter}.</li>
5350cf9ada93e50e906f20f5edf595234ada196d45Dake Gu * </ul>
5450cf9ada93e50e906f20f5edf595234ada196d45Dake Gu * Use {@link #setPresenterSelector(PresenterSelector)} in subclass constructor to customize
5550cf9ada93e50e906f20f5edf595234ada196d45Dake Gu * Presenters. App may override {@link BrowseFragment#onCreateHeadersFragment()}.
569870cf8fc2cbe52e761b850f58d5ac26db26a4c8Dake Gu * @deprecated use {@link HeadersSupportFragment}
5755c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout */
589870cf8fc2cbe52e761b850f58d5ac26db26a4c8Dake Gu@Deprecated
59a8a3b898da49324e83ea32c3f08776a481312166Tim Kilbournpublic class HeadersFragment extends BaseRowFragment {
603c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu
61729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu    /**
62729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu     * Interface definition for a callback to be invoked when a header item is clicked.
639870cf8fc2cbe52e761b850f58d5ac26db26a4c8Dake Gu     * @deprecated use {@link HeadersSupportFragment}
64729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu     */
659870cf8fc2cbe52e761b850f58d5ac26db26a4c8Dake Gu    @Deprecated
66729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu    public interface OnHeaderClickedListener {
67729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu        /**
68729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu         * Called when a header item has been clicked.
69729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu         *
70729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu         * @param viewHolder Row ViewHolder object corresponding to the selected Header.
71729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu         * @param row Row object corresponding to the selected Header.
72729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu         */
73729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu        void onHeaderClicked(RowHeaderPresenter.ViewHolder viewHolder, Row row);
743c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu    }
753c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu
76729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu    /**
77729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu     * Interface definition for a callback to be invoked when a header item is selected.
789870cf8fc2cbe52e761b850f58d5ac26db26a4c8Dake Gu     * @deprecated use {@link HeadersSupportFragment}
79729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu     */
809870cf8fc2cbe52e761b850f58d5ac26db26a4c8Dake Gu    @Deprecated
81729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu    public interface OnHeaderViewSelectedListener {
82729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu        /**
83729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu         * Called when a header item has been selected.
84729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu         *
85729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu         * @param viewHolder Row ViewHolder object corresponding to the selected Header.
86729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu         * @param row Row object corresponding to the selected Header.
87729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu         */
888df88a1ead9ea62456fc3bbda41657ccf61d5721Dake Gu        void onHeaderSelected(RowHeaderPresenter.ViewHolder viewHolder, Row row);
898df88a1ead9ea62456fc3bbda41657ccf61d5721Dake Gu    }
908df88a1ead9ea62456fc3bbda41657ccf61d5721Dake Gu
918df88a1ead9ea62456fc3bbda41657ccf61d5721Dake Gu    private OnHeaderViewSelectedListener mOnHeaderViewSelectedListener;
9299ec8b0cb375f7e5577ea3ec9f09e6ff7a95de0dAurimas Liutikas    OnHeaderClickedListener mOnHeaderClickedListener;
93d559710c8763acb37fd444735da7ee770b84df07Craig Stout    private boolean mHeadersEnabled = true;
94d559710c8763acb37fd444735da7ee770b84df07Craig Stout    private boolean mHeadersGone = false;
954cf79b1c4d38a190317961891f9fd052836710fdCraig Stout    private int mBackgroundColor;
964cf79b1c4d38a190317961891f9fd052836710fdCraig Stout    private boolean mBackgroundColorSet;
973c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu
988ff4c54cdaf5e8164fba7eac999c1ceafc462fc8Dake Gu    private static final PresenterSelector sHeaderPresenter = new ClassPresenterSelector()
998ff4c54cdaf5e8164fba7eac999c1ceafc462fc8Dake Gu            .addClassPresenter(DividerRow.class, new DividerPresenter())
1008ff4c54cdaf5e8164fba7eac999c1ceafc462fc8Dake Gu            .addClassPresenter(SectionRow.class,
1018ff4c54cdaf5e8164fba7eac999c1ceafc462fc8Dake Gu                    new RowHeaderPresenter(R.layout.lb_section_header, false))
1028ff4c54cdaf5e8164fba7eac999c1ceafc462fc8Dake Gu            .addClassPresenter(Row.class, new RowHeaderPresenter(R.layout.lb_header));
10355c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout
104a8a3b898da49324e83ea32c3f08776a481312166Tim Kilbourn    public HeadersFragment() {
105bd80644186ff8622c565f067dc3a4136a4dc0586Dake Gu        setPresenterSelector(sHeaderPresenter);
106c807587e03e775abb23f62894ad59bfb91cfbd97Dake Gu        FocusHighlightHelper.setupHeaderItemFocusHighlight(getBridgeAdapter());
10755c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout    }
10855c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout
1093c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu    public void setOnHeaderClickedListener(OnHeaderClickedListener listener) {
1103c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu        mOnHeaderClickedListener = listener;
11155c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout    }
11255c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout
1138df88a1ead9ea62456fc3bbda41657ccf61d5721Dake Gu    public void setOnHeaderViewSelectedListener(OnHeaderViewSelectedListener listener) {
1148df88a1ead9ea62456fc3bbda41657ccf61d5721Dake Gu        mOnHeaderViewSelectedListener = listener;
11555c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout    }
11655c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout
11755c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout    @Override
11842752c860a26deacca04ea9ebeb00ddb4d8ce2fcDake Gu    VerticalGridView findGridViewFromRoot(View view) {
1197a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu        return (VerticalGridView) view.findViewById(R.id.browse_headers);
1207a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu    }
1217a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu
1227a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu    @Override
1230246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu    void onRowSelected(RecyclerView parent, RecyclerView.ViewHolder viewHolder,
1240246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu            int position, int subposition) {
1258df88a1ead9ea62456fc3bbda41657ccf61d5721Dake Gu        if (mOnHeaderViewSelectedListener != null) {
1260246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu            if (viewHolder != null && position >= 0) {
1270246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu                ItemBridgeAdapter.ViewHolder vh = (ItemBridgeAdapter.ViewHolder) viewHolder;
1288df88a1ead9ea62456fc3bbda41657ccf61d5721Dake Gu                mOnHeaderViewSelectedListener.onHeaderSelected(
129729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu                        (RowHeaderPresenter.ViewHolder) vh.getViewHolder(), (Row) vh.getItem());
130d736653bfee7fc83fc68d57d0e0bee5fa807287fDake Gu            } else {
1318df88a1ead9ea62456fc3bbda41657ccf61d5721Dake Gu                mOnHeaderViewSelectedListener.onHeaderSelected(null, null);
1322f5fbc79d656fc4966119141aaef48161f08376fAndrew Wilson            }
13355c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout        }
13455c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout    }
13555c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout
1363c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu    private final ItemBridgeAdapter.AdapterListener mAdapterListener =
1373c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu            new ItemBridgeAdapter.AdapterListener() {
1383c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu        @Override
139729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu        public void onCreate(final ItemBridgeAdapter.ViewHolder viewHolder) {
1403c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu            View headerView = viewHolder.getViewHolder().view;
1413c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu            headerView.setOnClickListener(new View.OnClickListener() {
1423c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu                @Override
1433c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu                public void onClick(View v) {
1443c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu                    if (mOnHeaderClickedListener != null) {
145729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu                        mOnHeaderClickedListener.onHeaderClicked(
146729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu                                (RowHeaderPresenter.ViewHolder) viewHolder.getViewHolder(),
147729cbf4cd57c87bcd569db5974c8cbd51a942581Dake Gu                                (Row) viewHolder.getItem());
1483c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu                    }
1493c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu                }
1503c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu            });
151e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout            if (mWrapper != null) {
152e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout                viewHolder.itemView.addOnLayoutChangeListener(sLayoutChangeListener);
153e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout            } else {
154e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout                headerView.addOnLayoutChangeListener(sLayoutChangeListener);
155e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout            }
1563c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu        }
1573c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu
1583c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu    };
1593c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu
16099ec8b0cb375f7e5577ea3ec9f09e6ff7a95de0dAurimas Liutikas    static OnLayoutChangeListener sLayoutChangeListener = new OnLayoutChangeListener() {
1612f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu        @Override
1622f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu        public void onLayoutChange(View v, int left, int top, int right, int bottom,
1632f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu            int oldLeft, int oldTop, int oldRight, int oldBottom) {
1648e3566285de4ac771d6188f62fe947e23d371a3dKris Giesing            v.setPivotX(v.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL ? v.getWidth() : 0);
1652f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu            v.setPivotY(v.getMeasuredHeight() / 2);
1662f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu        }
1672f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu    };
1682f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu
1692f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu    @Override
17042752c860a26deacca04ea9ebeb00ddb4d8ce2fcDake Gu    int getLayoutResourceId() {
1712f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu        return R.layout.lb_headers_fragment;
1722f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu    }
1732f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu
1742f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu    @Override
175c0929ef05e205db275ef72ac14e8f73cf9436676Dake Gu    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
1762f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu        super.onViewCreated(view, savedInstanceState);
1774cf79b1c4d38a190317961891f9fd052836710fdCraig Stout        final VerticalGridView listView = getVerticalGridView();
1784cf79b1c4d38a190317961891f9fd052836710fdCraig Stout        if (listView == null) {
1794cf79b1c4d38a190317961891f9fd052836710fdCraig Stout            return;
1803c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu        }
1817f43e18536eae40705d5c63830e9edb283f196adDake Gu        if (mBackgroundColorSet) {
1829a49e322d10c1a67860d812352a5ea710843b75eDake Gu            listView.setBackgroundColor(mBackgroundColor);
1837f43e18536eae40705d5c63830e9edb283f196adDake Gu            updateFadingEdgeToBrandColor(mBackgroundColor);
1849a49e322d10c1a67860d812352a5ea710843b75eDake Gu        } else {
1859a49e322d10c1a67860d812352a5ea710843b75eDake Gu            Drawable d = listView.getBackground();
1869a49e322d10c1a67860d812352a5ea710843b75eDake Gu            if (d instanceof ColorDrawable) {
1879a49e322d10c1a67860d812352a5ea710843b75eDake Gu                updateFadingEdgeToBrandColor(((ColorDrawable) d).getColor());
1889a49e322d10c1a67860d812352a5ea710843b75eDake Gu            }
1897f43e18536eae40705d5c63830e9edb283f196adDake Gu        }
190d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu        updateListViewVisibility();
1913c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu    }
1923c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu
193d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu    private void updateListViewVisibility() {
1943c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu        final VerticalGridView listView = getVerticalGridView();
1952f97594742886d045ca1ce409ebc6e6e780452f6Dake Gu        if (listView != null) {
1969652881bb6af5a2b76f89d683bd420336f4bf463Dake Gu            getView().setVisibility(mHeadersGone ? View.GONE : View.VISIBLE);
197d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu            if (!mHeadersGone) {
198d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu                if (mHeadersEnabled) {
199d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu                    listView.setChildrenVisibility(View.VISIBLE);
200d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu                } else {
201d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu                    listView.setChildrenVisibility(View.INVISIBLE);
202d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu                }
203d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu            }
204d559710c8763acb37fd444735da7ee770b84df07Craig Stout        }
205d559710c8763acb37fd444735da7ee770b84df07Craig Stout    }
206d559710c8763acb37fd444735da7ee770b84df07Craig Stout
207d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu    void setHeadersEnabled(boolean enabled) {
208d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu        mHeadersEnabled = enabled;
209d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu        updateListViewVisibility();
210d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu    }
211d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu
212d559710c8763acb37fd444735da7ee770b84df07Craig Stout    void setHeadersGone(boolean gone) {
213d559710c8763acb37fd444735da7ee770b84df07Craig Stout        mHeadersGone = gone;
214d7618ab69ef591dd5342b9481c4954bfc7e9110cDake Gu        updateListViewVisibility();
2153c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu    }
2163c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu
2173659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu    static class NoOverlappingFrameLayout extends FrameLayout {
2183659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu
2193659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu        public NoOverlappingFrameLayout(Context context) {
2203659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu            super(context);
2213659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu        }
2223659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu
2233659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu        /**
2243659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu         * Avoid creating hardware layer for header dock.
2253659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu         */
2263659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu        @Override
2273659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu        public boolean hasOverlappingRendering() {
2283659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu            return false;
2293659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu        }
2303659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu    }
2313659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu
232e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout    // Wrapper needed because of conflict between RecyclerView's use of alpha
233c9a859537b0871f84afeeb706a5b425fe3f2b4ddAurimas Liutikas    // for ADD animations, and RowHeaderPresenter's use of alpha for selected level.
23499ec8b0cb375f7e5577ea3ec9f09e6ff7a95de0dAurimas Liutikas    final ItemBridgeAdapter.Wrapper mWrapper = new ItemBridgeAdapter.Wrapper() {
235e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout        @Override
236e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout        public void wrap(View wrapper, View wrapped) {
237e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout            ((FrameLayout) wrapper).addView(wrapped);
238e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout        }
239e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout
240e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout        @Override
241e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout        public View createWrapper(View root) {
2423659dc62f9e55b1043edb4105c311c8ef997f2aeDake Gu            return new NoOverlappingFrameLayout(root.getContext());
243e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout        }
244e51474af2c4b5192a75fd283cb737d02fccc58a4Craig Stout    };
2453c23ada8bc25a05bbaa8c479a9df72e8172c4349Dake Gu    @Override
24642752c860a26deacca04ea9ebeb00ddb4d8ce2fcDake Gu    void updateAdapter() {
24755c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout        super.updateAdapter();
24855c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout        ItemBridgeAdapter adapter = getBridgeAdapter();
24950cf9ada93e50e906f20f5edf595234ada196d45Dake Gu        adapter.setAdapterListener(mAdapterListener);
25050cf9ada93e50e906f20f5edf595234ada196d45Dake Gu        adapter.setWrapper(mWrapper);
25155c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout    }
2524cf79b1c4d38a190317961891f9fd052836710fdCraig Stout
2534cf79b1c4d38a190317961891f9fd052836710fdCraig Stout    void setBackgroundColor(int color) {
2544cf79b1c4d38a190317961891f9fd052836710fdCraig Stout        mBackgroundColor = color;
2554cf79b1c4d38a190317961891f9fd052836710fdCraig Stout        mBackgroundColorSet = true;
2564cf79b1c4d38a190317961891f9fd052836710fdCraig Stout
2579a49e322d10c1a67860d812352a5ea710843b75eDake Gu        if (getVerticalGridView() != null) {
2589a49e322d10c1a67860d812352a5ea710843b75eDake Gu            getVerticalGridView().setBackgroundColor(mBackgroundColor);
259b74149016854eb172c0ef121ccfef749c6ef7eb1Dake Gu            updateFadingEdgeToBrandColor(mBackgroundColor);
2607a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu        }
2617a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu    }
2627a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu
263b74149016854eb172c0ef121ccfef749c6ef7eb1Dake Gu    private void updateFadingEdgeToBrandColor(int backgroundColor) {
2647a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu        View fadingView = getView().findViewById(R.id.fade_out_edge);
2657a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu        Drawable background = fadingView.getBackground();
2667a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu        if (background instanceof GradientDrawable) {
2677a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu            background.mutate();
2687a208cd35be79a69b1f4f36724b85ab96eb23e75Dake Gu            ((GradientDrawable) background).setColors(
269b74149016854eb172c0ef121ccfef749c6ef7eb1Dake Gu                    new int[] {Color.TRANSPARENT, backgroundColor});
2704cf79b1c4d38a190317961891f9fd052836710fdCraig Stout        }
2714cf79b1c4d38a190317961891f9fd052836710fdCraig Stout    }
2724cf79b1c4d38a190317961891f9fd052836710fdCraig Stout
27349e6a6cd81f3a1117b306323064c53230a7f01adDake Gu    @Override
2742f5ebf3f6f7bb6a24856f389e369b247118ba119susnata    public void onTransitionStart() {
27549e6a6cd81f3a1117b306323064c53230a7f01adDake Gu        super.onTransitionStart();
27649e6a6cd81f3a1117b306323064c53230a7f01adDake Gu        if (!mHeadersEnabled) {
27749e6a6cd81f3a1117b306323064c53230a7f01adDake Gu            // When enabling headers fragment,  the RowHeaderView gets a focus but
278c9a859537b0871f84afeeb706a5b425fe3f2b4ddAurimas Liutikas            // isShown() is still false because its parent is INVISIBLE, accessibility
27949e6a6cd81f3a1117b306323064c53230a7f01adDake Gu            // event is not sent.
28049e6a6cd81f3a1117b306323064c53230a7f01adDake Gu            // Workaround is: prevent focus to a child view during transition and put
28149e6a6cd81f3a1117b306323064c53230a7f01adDake Gu            // focus on it after transition is done.
28249e6a6cd81f3a1117b306323064c53230a7f01adDake Gu            final VerticalGridView listView = getVerticalGridView();
28349e6a6cd81f3a1117b306323064c53230a7f01adDake Gu            if (listView != null) {
28449e6a6cd81f3a1117b306323064c53230a7f01adDake Gu                listView.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
28549e6a6cd81f3a1117b306323064c53230a7f01adDake Gu                if (listView.hasFocus()) {
28649e6a6cd81f3a1117b306323064c53230a7f01adDake Gu                    listView.requestFocus();
28749e6a6cd81f3a1117b306323064c53230a7f01adDake Gu                }
28849e6a6cd81f3a1117b306323064c53230a7f01adDake Gu            }
28949e6a6cd81f3a1117b306323064c53230a7f01adDake Gu        }
29049e6a6cd81f3a1117b306323064c53230a7f01adDake Gu    }
29149e6a6cd81f3a1117b306323064c53230a7f01adDake Gu
29249e6a6cd81f3a1117b306323064c53230a7f01adDake Gu    @Override
2932f5ebf3f6f7bb6a24856f389e369b247118ba119susnata    public void onTransitionEnd() {
29449e6a6cd81f3a1117b306323064c53230a7f01adDake Gu        if (mHeadersEnabled) {
29549e6a6cd81f3a1117b306323064c53230a7f01adDake Gu            final VerticalGridView listView = getVerticalGridView();
29649e6a6cd81f3a1117b306323064c53230a7f01adDake Gu            if (listView != null) {
29749e6a6cd81f3a1117b306323064c53230a7f01adDake Gu                listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
29849e6a6cd81f3a1117b306323064c53230a7f01adDake Gu                if (listView.hasFocus()) {
29949e6a6cd81f3a1117b306323064c53230a7f01adDake Gu                    listView.requestFocus();
30049e6a6cd81f3a1117b306323064c53230a7f01adDake Gu                }
30149e6a6cd81f3a1117b306323064c53230a7f01adDake Gu            }
30249e6a6cd81f3a1117b306323064c53230a7f01adDake Gu        }
30349e6a6cd81f3a1117b306323064c53230a7f01adDake Gu        super.onTransitionEnd();
30449e6a6cd81f3a1117b306323064c53230a7f01adDake Gu    }
3052f5ebf3f6f7bb6a24856f389e369b247118ba119susnata
3062f5ebf3f6f7bb6a24856f389e369b247118ba119susnata    public boolean isScrolling() {
3072f5ebf3f6f7bb6a24856f389e369b247118ba119susnata        return getVerticalGridView().getScrollState()
3082f5ebf3f6f7bb6a24856f389e369b247118ba119susnata                != HorizontalGridView.SCROLL_STATE_IDLE;
3092f5ebf3f6f7bb6a24856f389e369b247118ba119susnata    }
31055c9ee4b612ffc7b4632b1e4b7b7ab4900cd47c7Craig Stout}
311