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