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