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