ControlButtonPresenterSelector.java revision a00bada00bff4a58436a39472ab14ccb7a8f619d
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;
23import android.widget.TextView;
24
25/**
26 * Displays primary and secondary 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        TextView mLabel;
61        View mFocusableView;
62
63        public ActionViewHolder(View view) {
64            super(view);
65            mIcon = (ImageView) view.findViewById(R.id.icon);
66            mLabel = (TextView) view.findViewById(R.id.label);
67            mFocusableView = view.findViewById(R.id.button);
68        }
69    }
70
71    static class ControlButtonPresenter extends Presenter {
72        private int mLayoutResourceId;
73
74        ControlButtonPresenter(int layoutResourceId) {
75            mLayoutResourceId = layoutResourceId;
76        }
77
78        @Override
79        public ViewHolder onCreateViewHolder(ViewGroup parent) {
80            View v = LayoutInflater.from(parent.getContext())
81                    .inflate(mLayoutResourceId, parent, false);
82            return new ActionViewHolder(v);
83        }
84
85        @Override
86        public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
87            Action action = (Action) item;
88            ActionViewHolder vh = (ActionViewHolder) viewHolder;
89            vh.mIcon.setImageDrawable(action.getIcon());
90            if (vh.mLabel != null) {
91                if (action.getIcon() == null) {
92                    vh.mLabel.setText(action.getLabel1());
93                } else {
94                    vh.mLabel.setText(null);
95                }
96            }
97            CharSequence contentDescription = TextUtils.isEmpty(action.getLabel2()) ?
98                action.getLabel1() : action.getLabel2();
99            if (!TextUtils.equals(vh.mFocusableView.getContentDescription(), contentDescription)) {
100                vh.mFocusableView.setContentDescription(contentDescription);
101                vh.mFocusableView.sendAccessibilityEvent(
102                        AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
103            }
104        }
105
106        @Override
107        public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
108            ActionViewHolder vh = (ActionViewHolder) viewHolder;
109            vh.mIcon.setImageDrawable(null);
110            if (vh.mLabel != null) {
111                vh.mLabel.setText(null);
112            }
113            vh.mFocusableView.setContentDescription(null);
114        }
115
116        @Override
117        public void setOnClickListener(Presenter.ViewHolder viewHolder,
118                View.OnClickListener listener) {
119            ((ActionViewHolder) viewHolder).mFocusableView.setOnClickListener(listener);
120        }
121    }
122}
123