PlaybackControlsPresenter.java revision 7ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7
17ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout/*
27ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout * Copyright (C) 2014 The Android Open Source Project
37ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout *
47ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
57ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout * in compliance with the License. You may obtain a copy of the License at
67ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout *
77ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout * http://www.apache.org/licenses/LICENSE-2.0
87ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout *
97ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout * Unless required by applicable law or agreed to in writing, software distributed under the License
107ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
117ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout * or implied. See the License for the specific language governing permissions and limitations under
127ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout * the License.
137ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout */
147ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stoutpackage android.support.v17.leanback.widget;
157ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout
167ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stoutimport android.support.v17.leanback.R;
177ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stoutimport android.view.LayoutInflater;
187ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stoutimport android.view.View;
197ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stoutimport android.view.ViewGroup;
207ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stoutimport android.widget.FrameLayout;
217ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout
227ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout/**
237ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout * A presenter for a control bar that supports "more actions",
247ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout * and toggling the set of controls between primary and secondary
257ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout * sets of {@link Actions}.
267ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout */
277ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stoutclass PlaybackControlsPresenter extends ControlBarPresenter {
287ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout
297ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    /**
307ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout     * The data type expected by this presenter.
317ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout     */
327ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    static class BoundData extends ControlBarPresenter.BoundData {
337ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        /**
347ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout         * The adapter containing secondary actions.
357ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout         */
367ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        ObjectAdapter secondaryActionsAdapter;
377ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    }
387ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout
397ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    class ViewHolder extends ControlBarPresenter.ViewHolder {
407ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        ObjectAdapter mMoreActionsAdapter;
417ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        ObjectAdapter.DataObserver mMoreActionsObserver;
427ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        FrameLayout mMoreActionsDock;
437ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        Presenter.ViewHolder mMoreActionsViewHolder;
447ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        boolean mMoreActionsShowing;
457ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout
467ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        ViewHolder(View rootView) {
477ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout            super(rootView);
487ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout            mMoreActionsDock = (FrameLayout) rootView.findViewById(R.id.more_actions_dock);
497ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout            mMoreActionsObserver = new ObjectAdapter.DataObserver() {
507ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                @Override
517ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                public void onChanged() {
527ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                    if (mMoreActionsShowing) {
537ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                        showControls(mMoreActionsAdapter, mPresenter);
547ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                    }
557ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                }
567ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                @Override
577ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                public void onItemRangeChanged(int positionStart, int itemCount) {
587ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                    if (mMoreActionsShowing) {
597ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                        for (int i = 0; i < itemCount; i++) {
607ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                            bindControlToAction(positionStart + i,
617ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                                    mMoreActionsAdapter, mPresenter);
627ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                        }
637ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                    }
647ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                }
657ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout            };
667ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        }
677ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout
687ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        void showMoreActions() {
697ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout            if (mMoreActionsViewHolder == null) {
707ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                Action action = new PlaybackControlsRow.MoreActions(mMoreActionsDock.getContext());
717ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                mMoreActionsViewHolder = mPresenter.onCreateViewHolder(mMoreActionsDock);
727ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                mPresenter.onBindViewHolder(mMoreActionsViewHolder, action);
737ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                mPresenter.setOnClickListener(mMoreActionsViewHolder, new View.OnClickListener() {
747ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                    @Override
757ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                    public void onClick(View v) {
767ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                        toggleMoreActions();
777ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                    }
787ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout                });
797ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout            }
807ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout            mMoreActionsDock.addView(mMoreActionsViewHolder.view);
817ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        }
827ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout
837ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        void toggleMoreActions() {
847ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout            mMoreActionsShowing = !mMoreActionsShowing;
857ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout            showControls(getAdapter(), mPresenter);
867ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        }
877ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout
887ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        @Override
897ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        ObjectAdapter getAdapter() {
907ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout            return mMoreActionsShowing ? mMoreActionsAdapter : mAdapter;
917ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        }
927ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    }
937ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout
947ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    private boolean mMoreActionsEnabled = true;
957ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout
967ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    /**
977ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout     * Constructor for a PlaybackControlsRowPresenter.
987ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout     *
997ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout     * @param layoutResourceId The resource id of the layout for this presenter.
1007ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout     */
1017ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    public PlaybackControlsPresenter(int layoutResourceId) {
1027ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        super(layoutResourceId);
1037ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    }
1047ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout
1057ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    /**
1067ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout     * Enables the display of secondary actions.
1077ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout     * A "more actions" button will be displayed.  When "more actions" is selected,
1087ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout     * the primary actions are replaced with the secondary actions.
1097ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout     */
1107ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    public void enableSecondaryActions(boolean enable) {
1117ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        mMoreActionsEnabled = enable;
1127ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    }
1137ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout
1147ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    /**
1157ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout     * Returns true if secondary actions are enabled.
1167ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout     */
1177ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    public boolean areMoreActionsEnabled() {
1187ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        return mMoreActionsEnabled;
1197ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    }
1207ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout
1217ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    @Override
1227ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    public Presenter.ViewHolder onCreateViewHolder(ViewGroup parent) {
1237ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        View v = LayoutInflater.from(parent.getContext())
1247ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout            .inflate(getLayoutResourceId(), parent, false);
1257ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        return new ViewHolder(v);
1267ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    }
1277ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout
1287ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    @Override
1297ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    public void onBindViewHolder(Presenter.ViewHolder holder, Object item) {
1307ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        super.onBindViewHolder(holder, item);
1317ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout
1327ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        ViewHolder vh = (ViewHolder) holder;
1337ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        BoundData data = (BoundData) item;
1347ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        if (vh.mMoreActionsAdapter != data.secondaryActionsAdapter) {
1357ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout            vh.mMoreActionsAdapter = data.secondaryActionsAdapter;
1367ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout            vh.mMoreActionsAdapter.registerObserver(vh.mMoreActionsObserver);
1377ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        }
1387ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        if (mMoreActionsEnabled) {
1397ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout            vh.showMoreActions();
1407ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        }
1417ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    }
1427ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout
1437ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    @Override
1447ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    public void onUnbindViewHolder(Presenter.ViewHolder holder) {
1457ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        super.onUnbindViewHolder(holder);
1467ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        ViewHolder vh = (ViewHolder) holder;
1477ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        vh.mMoreActionsAdapter.unregisterObserver(vh.mMoreActionsObserver);
1487ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout        vh.mMoreActionsAdapter = null;
1497ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout    }
1507ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7Craig Stout}
151