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.graphics.drawable.Drawable;
17import android.support.v17.leanback.R;
18import android.text.TextUtils;
19import android.view.LayoutInflater;
20import android.view.View;
21import android.view.ViewGroup;
22import android.widget.Button;
23
24class ActionPresenterSelector extends PresenterSelector {
25
26    private final Presenter mOneLineActionPresenter = new OneLineActionPresenter();
27    private final Presenter mTwoLineActionPresenter = new TwoLineActionPresenter();
28
29    @Override
30    public Presenter getPresenter(Object item) {
31        Action action = (Action) item;
32        if (TextUtils.isEmpty(action.getLabel2())) {
33            return mOneLineActionPresenter;
34        } else {
35            return mTwoLineActionPresenter;
36        }
37    }
38
39    static class ActionViewHolder extends Presenter.ViewHolder {
40        Action mAction;
41        Button mButton;
42        int mLayoutDirection;
43
44        public ActionViewHolder(View view, int layoutDirection) {
45            super(view);
46            mButton = (Button) view.findViewById(R.id.lb_action_button);
47            mLayoutDirection = layoutDirection;
48        }
49    }
50
51    class OneLineActionPresenter extends Presenter {
52        @Override
53        public ViewHolder onCreateViewHolder(ViewGroup parent) {
54            View v = LayoutInflater.from(parent.getContext())
55                .inflate(R.layout.lb_action_1_line, parent, false);
56            return new ActionViewHolder(v, parent.getLayoutDirection());
57        }
58
59        @Override
60        public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
61            Action action = (Action) item;
62            ActionViewHolder vh = (ActionViewHolder) viewHolder;
63            vh.mAction = action;
64            vh.mButton.setText(action.getLabel1());
65        }
66
67        @Override
68        public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
69            ((ActionViewHolder) viewHolder).mAction = null;
70        }
71    }
72
73    class TwoLineActionPresenter extends Presenter {
74        @Override
75        public ViewHolder onCreateViewHolder(ViewGroup parent) {
76            View v = LayoutInflater.from(parent.getContext())
77                .inflate(R.layout.lb_action_2_lines, parent, false);
78            return new ActionViewHolder(v, parent.getLayoutDirection());
79        }
80
81        @Override
82        public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {
83            Action action = (Action) item;
84            ActionViewHolder vh = (ActionViewHolder) viewHolder;
85            Drawable icon = action.getIcon();
86            vh.mAction = action;
87
88            if (icon != null) {
89                final int startPadding = vh.view.getResources()
90                        .getDimensionPixelSize(R.dimen.lb_action_with_icon_padding_start);
91                final int endPadding = vh.view.getResources()
92                        .getDimensionPixelSize(R.dimen.lb_action_with_icon_padding_end);
93                vh.view.setPaddingRelative(startPadding, 0, endPadding, 0);
94            } else {
95                final int padding = vh.view.getResources()
96                        .getDimensionPixelSize(R.dimen.lb_action_padding_horizontal);
97                vh.view.setPaddingRelative(padding, 0, padding, 0);
98            }
99            if (vh.mLayoutDirection == View.LAYOUT_DIRECTION_RTL) {
100                vh.mButton.setCompoundDrawablesWithIntrinsicBounds(null, null, icon, null);
101            } else {
102                vh.mButton.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
103            }
104
105            CharSequence line1 = action.getLabel1();
106            CharSequence line2 = action.getLabel2();
107            if (TextUtils.isEmpty(line1)) {
108                vh.mButton.setText(line2);
109            } else if (TextUtils.isEmpty(line2)) {
110                vh.mButton.setText(line1);
111            } else {
112                vh.mButton.setText(line1 + "\n" + line2);
113            }
114        }
115
116        @Override
117        public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
118            ActionViewHolder vh = (ActionViewHolder) viewHolder;
119            vh.mButton.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
120            vh.view.setPadding(0, 0, 0, 0);
121            vh.mAction = null;
122        }
123    }
124}
125