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 * ControlButtonPresenterSelector displays primary and secondary 27 * controls for a {@link PlaybackControlsRow}. 28 * 29 * Binds to items of type {@link Action}. 30 */ 31public class ControlButtonPresenterSelector extends PresenterSelector { 32 33 private final Presenter mPrimaryPresenter = 34 new ControlButtonPresenter(R.layout.lb_control_button_primary); 35 private final Presenter mSecondaryPresenter = 36 new ControlButtonPresenter(R.layout.lb_control_button_secondary); 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 public Presenter getPresenter(Object item) { 56 return mPrimaryPresenter; 57 } 58 59 static class ActionViewHolder extends Presenter.ViewHolder { 60 ImageView mIcon; 61 TextView mLabel; 62 View mFocusableView; 63 64 public ActionViewHolder(View view) { 65 super(view); 66 mIcon = (ImageView) view.findViewById(R.id.icon); 67 mLabel = (TextView) view.findViewById(R.id.label); 68 mFocusableView = view.findViewById(R.id.button); 69 } 70 } 71 72 static class ControlButtonPresenter extends Presenter { 73 private int mLayoutResourceId; 74 75 ControlButtonPresenter(int layoutResourceId) { 76 mLayoutResourceId = layoutResourceId; 77 } 78 79 @Override 80 public ViewHolder onCreateViewHolder(ViewGroup parent) { 81 View v = LayoutInflater.from(parent.getContext()) 82 .inflate(mLayoutResourceId, parent, false); 83 return new ActionViewHolder(v); 84 } 85 86 @Override 87 public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) { 88 Action action = (Action) item; 89 ActionViewHolder vh = (ActionViewHolder) viewHolder; 90 vh.mIcon.setImageDrawable(action.getIcon()); 91 if (vh.mLabel != null) { 92 if (action.getIcon() == null) { 93 vh.mLabel.setText(action.getLabel1()); 94 } else { 95 vh.mLabel.setText(null); 96 } 97 } 98 CharSequence contentDescription = TextUtils.isEmpty(action.getLabel2()) ? 99 action.getLabel1() : action.getLabel2(); 100 if (!TextUtils.equals(vh.mFocusableView.getContentDescription(), contentDescription)) { 101 vh.mFocusableView.setContentDescription(contentDescription); 102 vh.mFocusableView.sendAccessibilityEvent( 103 AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED); 104 } 105 } 106 107 @Override 108 public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) { 109 ActionViewHolder vh = (ActionViewHolder) viewHolder; 110 vh.mIcon.setImageDrawable(null); 111 if (vh.mLabel != null) { 112 vh.mLabel.setText(null); 113 } 114 vh.mFocusableView.setContentDescription(null); 115 } 116 117 @Override 118 public void setOnClickListener(Presenter.ViewHolder viewHolder, 119 View.OnClickListener listener) { 120 ((ActionViewHolder) viewHolder).mFocusableView.setOnClickListener(listener); 121 } 122 } 123} 124