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