1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.tv.dvr.ui.browse; 18 19 import android.graphics.drawable.Drawable; 20 import androidx.leanback.R; 21 import androidx.leanback.widget.Action; 22 import androidx.leanback.widget.Presenter; 23 import androidx.leanback.widget.PresenterSelector; 24 import android.text.TextUtils; 25 import android.view.LayoutInflater; 26 import android.view.View; 27 import android.view.ViewGroup; 28 import android.widget.Button; 29 30 /** 31 * This class is adapted from Leanback's library, which does not support action icon with one-line 32 * label. This class modified its getPresenter method to support the above situation. 33 */ 34 public class ActionPresenterSelector extends PresenterSelector { 35 private final Presenter mOneLineActionPresenter = new OneLineActionPresenter(); 36 private final Presenter mTwoLineActionPresenter = new TwoLineActionPresenter(); 37 private final Presenter[] mPresenters = 38 new Presenter[] {mOneLineActionPresenter, mTwoLineActionPresenter}; 39 40 @Override getPresenter(Object item)41 public Presenter getPresenter(Object item) { 42 Action action = (Action) item; 43 if (TextUtils.isEmpty(action.getLabel2()) && action.getIcon() == null) { 44 return mOneLineActionPresenter; 45 } else { 46 return mTwoLineActionPresenter; 47 } 48 } 49 50 @Override getPresenters()51 public Presenter[] getPresenters() { 52 return mPresenters; 53 } 54 55 static class ActionViewHolder extends Presenter.ViewHolder { 56 Action mAction; 57 Button mButton; 58 int mLayoutDirection; 59 ActionViewHolder(View view, int layoutDirection)60 public ActionViewHolder(View view, int layoutDirection) { 61 super(view); 62 mButton = (Button) view.findViewById(R.id.lb_action_button); 63 mLayoutDirection = layoutDirection; 64 } 65 } 66 67 class OneLineActionPresenter extends Presenter { 68 @Override onCreateViewHolder(ViewGroup parent)69 public ViewHolder onCreateViewHolder(ViewGroup parent) { 70 View v = 71 LayoutInflater.from(parent.getContext()) 72 .inflate(R.layout.lb_action_1_line, parent, false); 73 return new ActionViewHolder(v, parent.getLayoutDirection()); 74 } 75 76 @Override onBindViewHolder(Presenter.ViewHolder viewHolder, Object item)77 public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) { 78 Action action = (Action) item; 79 ActionViewHolder vh = (ActionViewHolder) viewHolder; 80 vh.mAction = action; 81 vh.mButton.setText(action.getLabel1()); 82 } 83 84 @Override onUnbindViewHolder(Presenter.ViewHolder viewHolder)85 public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) { 86 ((ActionViewHolder) viewHolder).mAction = null; 87 } 88 } 89 90 class TwoLineActionPresenter extends Presenter { 91 @Override onCreateViewHolder(ViewGroup parent)92 public ViewHolder onCreateViewHolder(ViewGroup parent) { 93 View v = 94 LayoutInflater.from(parent.getContext()) 95 .inflate(R.layout.lb_action_2_lines, parent, false); 96 return new ActionViewHolder(v, parent.getLayoutDirection()); 97 } 98 99 @Override onBindViewHolder(Presenter.ViewHolder viewHolder, Object item)100 public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) { 101 Action action = (Action) item; 102 ActionViewHolder vh = (ActionViewHolder) viewHolder; 103 Drawable icon = action.getIcon(); 104 vh.mAction = action; 105 106 if (icon != null) { 107 final int startPadding = 108 vh.view 109 .getResources() 110 .getDimensionPixelSize(R.dimen.lb_action_with_icon_padding_start); 111 final int endPadding = 112 vh.view 113 .getResources() 114 .getDimensionPixelSize(R.dimen.lb_action_with_icon_padding_end); 115 vh.view.setPaddingRelative(startPadding, 0, endPadding, 0); 116 } else { 117 final int padding = 118 vh.view 119 .getResources() 120 .getDimensionPixelSize(R.dimen.lb_action_padding_horizontal); 121 vh.view.setPaddingRelative(padding, 0, padding, 0); 122 } 123 vh.mButton.setCompoundDrawablesRelativeWithIntrinsicBounds(icon, null, null, null); 124 125 CharSequence line1 = action.getLabel1(); 126 CharSequence line2 = action.getLabel2(); 127 if (TextUtils.isEmpty(line1)) { 128 vh.mButton.setText(line2); 129 } else if (TextUtils.isEmpty(line2)) { 130 vh.mButton.setText(line1); 131 } else { 132 vh.mButton.setText(line1 + "\n" + line2); 133 } 134 } 135 136 @Override onUnbindViewHolder(Presenter.ViewHolder viewHolder)137 public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) { 138 ActionViewHolder vh = (ActionViewHolder) viewHolder; 139 vh.mButton.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, null, null); 140 vh.view.setPadding(0, 0, 0, 0); 141 vh.mAction = null; 142 } 143 } 144 } 145