ControlButtonPresenterSelector.java revision 05805862fe972a3e3c0d199b7eb3a80fc5bdd7b6
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.widget;
15
16import android.support.v17.leanback.R;
17import android.text.TextUtils;
18import android.view.LayoutInflater;
19import android.view.View;
20import android.view.ViewGroup;
21import android.view.accessibility.AccessibilityEvent;
22import android.widget.ImageView;
23
24/**
25 * ControlButtonPresenterSelector displays primary and secondary
26 * controls for a {@link PlaybackControlsRow}.
27 *
28 * Binds to items of type {@link Action}.
29 */
30public class ControlButtonPresenterSelector extends PresenterSelector {
31
32    private final Presenter mPrimaryPresenter =
33            new ControlButtonPresenter(R.layout.lb_control_button_primary);
34    private final Presenter mSecondaryPresenter =
35            new ControlButtonPresenter(R.layout.lb_control_button_secondary);
36
37    /**
38     * Returns the presenter for primary controls.
39     */
40    public Presenter getPrimaryPresenter() {
41        return mPrimaryPresenter;
42    }
43
44    /**
45     * Returns the presenter for secondary controls.
46     */
47    public Presenter getSecondaryPresenter() {
48        return mSecondaryPresenter;
49    }
50
51    /**
52     * Always returns the presenter for primary controls.
53     */
54    public Presenter getPresenter(Object item) {
55        return mPrimaryPresenter;
56    }
57
58    static class ActionViewHolder extends Presenter.ViewHolder {
59        ImageView mIcon;
60        View mFocusableView;
61
62        public ActionViewHolder(View view) {
63            super(view);
64            mIcon = (ImageView) view.findViewById(R.id.icon);
65            mFocusableView = view.findViewById(R.id.button);
66        }
67    }
68
69    static class ControlButtonPresenter extends Presenter {
70        private int mLayoutResourceId;
71
72        ControlButtonPresenter(int layoutResourceId) {
73            mLayoutResourceId = layoutResourceId;
74        }
75
76        @Override
77        public ViewHolder onCreateViewHolder(ViewGroup parent) {
78            View v = LayoutInflater.from(parent.getContext())
79                    .inflate(mLayoutResourceId, parent, false);
80            return new ActionViewHolder(v);
81        }
82
83        @Override
84        public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
85            Action action = (Action) item;
86            ActionViewHolder vh = (ActionViewHolder) viewHolder;
87            vh.mIcon.setImageDrawable(action.getIcon());
88            CharSequence contentDescription = !TextUtils.isEmpty(action.getLabel1()) ?
89                action.getLabel1() : action.getLabel2();
90            if (!TextUtils.equals(vh.mFocusableView.getContentDescription(), contentDescription)) {
91                vh.mFocusableView.setContentDescription(contentDescription);
92                vh.mFocusableView.sendAccessibilityEvent(
93                        AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
94            }
95        }
96
97        @Override
98        public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
99            ActionViewHolder vh = (ActionViewHolder) viewHolder;
100            vh.mIcon.setImageDrawable(null);
101            vh.mFocusableView.setContentDescription(null);
102        }
103
104        @Override
105        public void setOnClickListener(Presenter.ViewHolder viewHolder,
106                View.OnClickListener listener) {
107            ((ActionViewHolder) viewHolder).mFocusableView.setOnClickListener(listener);
108        }
109    }
110}
111