PlaybackControlsRowPresenter.java revision 7ab1edf2b49f3cdcb9db7a1c60d0dc1e17a9aef7
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.content.Context; 18import android.graphics.Color; 19import android.util.TypedValue; 20import android.view.LayoutInflater; 21import android.view.View; 22import android.view.ViewGroup; 23import android.view.ViewGroup.LayoutParams; 24import android.view.ViewGroup.MarginLayoutParams; 25import android.widget.ImageView; 26 27/** 28 * A PlaybackControlsRowPresenter renders a {@link PlaybackControlsRow} to display a 29 * series of playback control buttons. Typically this row will be the first row in a fragment 30 * such as the {@link android.support.v17.leanback.app.PlaybackOverlayFragment 31 * PlaybackControlsFragment}. 32 */ 33public class PlaybackControlsRowPresenter extends RowPresenter { 34 35 /** 36 * A ViewHolder for the PlaybackControlsRow. 37 */ 38 public static class ViewHolder extends RowPresenter.ViewHolder { 39 final ViewGroup mCard; 40 final ImageView mImageView; 41 final ViewGroup mDescriptionDock; 42 final ViewGroup mControlsDock; 43 final ViewGroup mSecondaryControlsDock; 44 final View mSpacer; 45 int mCardHeight; 46 int mControlsDockMarginStart; 47 int mControlsDockMarginEnd; 48 Presenter.ViewHolder mDescriptionVh; 49 Presenter.ViewHolder mControlsVh; 50 Presenter.ViewHolder mSecondaryControlsVh; 51 PlaybackControlsPresenter.BoundData mControlsBoundData = 52 new PlaybackControlsPresenter.BoundData(); 53 ControlBarPresenter.BoundData mSecondaryBoundData = new ControlBarPresenter.BoundData(); 54 55 ViewHolder(View rootView) { 56 super(rootView); 57 mCard = (ViewGroup) rootView.findViewById(R.id.controls_card); 58 mImageView = (ImageView) rootView.findViewById(R.id.image); 59 mDescriptionDock = (ViewGroup) rootView.findViewById(R.id.description_dock); 60 mControlsDock = (ViewGroup) rootView.findViewById(R.id.controls_dock); 61 mSecondaryControlsDock = 62 (ViewGroup) rootView.findViewById(R.id.secondary_controls_dock); 63 mSpacer = rootView.findViewById(R.id.spacer); 64 } 65 66 Presenter getPresenter(Object item, boolean primary) { 67 ObjectAdapter adapter = primary ? 68 ((PlaybackControlsRow) getRow()).getPrimaryActionsAdapter() : 69 ((PlaybackControlsRow) getRow()).getSecondaryActionsAdapter(); 70 if (adapter.getPresenterSelector() instanceof ControlButtonPresenterSelector) { 71 ControlButtonPresenterSelector selector = 72 (ControlButtonPresenterSelector) adapter.getPresenterSelector(); 73 return primary ? selector.getPrimaryPresenter() : 74 selector.getSecondaryPresenter(); 75 } 76 return adapter.getPresenter(item); 77 } 78 } 79 80 private int mBackgroundColor = Color.TRANSPARENT; 81 private boolean mBackgroundColorSet; 82 private boolean mSecondaryActionsHidden; 83 private Presenter mDescriptionPresenter; 84 private PlaybackControlsPresenter mPlaybackControlsPresenter; 85 private ControlBarPresenter mSecondaryControlsPresenter; 86 87 /** 88 * Constructor for a PlaybackControlsRowPresenter. 89 * 90 * @param descriptionPresenter Presenter for displaying item details. 91 */ 92 public PlaybackControlsRowPresenter(Presenter descriptionPresenter) { 93 setHeaderPresenter(null); 94 setSelectEffectEnabled(false); 95 96 mDescriptionPresenter = descriptionPresenter; 97 mPlaybackControlsPresenter = new PlaybackControlsPresenter(R.layout.lb_playback_controls); 98 mSecondaryControlsPresenter = new ControlBarPresenter(R.layout.lb_control_bar); 99 } 100 101 /** 102 * Constructor for a PlaybackControlsRowPresenter. 103 */ 104 public PlaybackControlsRowPresenter() { 105 this(null); 106 } 107 108 /** 109 * Sets the listener for {@link Action} click events. 110 */ 111 public void setOnActionClickedListener(OnActionClickedListener listener) { 112 mPlaybackControlsPresenter.setOnActionClickedListener(listener); 113 mSecondaryControlsPresenter.setOnActionClickedListener(listener); 114 } 115 116 /** 117 * Gets the listener for {@link Action} click events. 118 */ 119 public OnActionClickedListener getOnActionClickedListener() { 120 return mPlaybackControlsPresenter.getOnActionClickedListener(); 121 } 122 123 /** 124 * Sets the background color. If not set, a default from the theme will be used. 125 */ 126 public void setBackgroundColor(int color) { 127 mBackgroundColor = color; 128 mBackgroundColorSet = true; 129 } 130 131 /** 132 * Returns the background color. If no background color was set, transparent 133 * is returned. 134 */ 135 public int getBackgroundColor() { 136 return mBackgroundColor; 137 } 138 139 /** 140 * Sets the secondary actions to be hidden behind a "more actions" button. 141 * When "more actions" is selected, the primary actions are replaced with 142 * the secondary actions. 143 */ 144 public void setSecondaryActionsHidden(boolean hidden) { 145 mSecondaryActionsHidden = hidden; 146 } 147 148 /** 149 * Returns true if secondary actions are hidden. 150 */ 151 public boolean areSecondaryActionsHidden() { 152 return mSecondaryActionsHidden; 153 } 154 155 private int getDefaultBackgroundColor(Context context) { 156 TypedValue outValue = new TypedValue(); 157 context.getTheme().resolveAttribute(R.attr.defaultBrandColor, outValue, true); 158 return context.getResources().getColor(outValue.resourceId); 159 } 160 161 @Override 162 protected RowPresenter.ViewHolder createRowViewHolder(ViewGroup parent) { 163 View v = LayoutInflater.from(parent.getContext()) 164 .inflate(R.layout.lb_playback_controls_row, parent, false); 165 ViewHolder vh = new ViewHolder(v); 166 initRow(vh); 167 return vh; 168 } 169 170 private void initRow(ViewHolder vh) { 171 vh.mCardHeight = vh.mCard.getLayoutParams().height; 172 173 MarginLayoutParams lp = (MarginLayoutParams) vh.mControlsDock.getLayoutParams(); 174 vh.mControlsDockMarginStart = lp.getMarginStart(); 175 vh.mControlsDockMarginEnd = lp.getMarginEnd(); 176 177 if (mDescriptionPresenter != null) { 178 vh.mDescriptionVh = mDescriptionPresenter.onCreateViewHolder(vh.mDescriptionDock); 179 vh.mDescriptionDock.addView(vh.mDescriptionVh.view); 180 } 181 182 vh.mControlsVh = mPlaybackControlsPresenter.onCreateViewHolder(vh.mControlsDock); 183 vh.mControlsDock.addView(vh.mControlsVh.view); 184 185 vh.mSecondaryControlsVh = 186 mSecondaryControlsPresenter.onCreateViewHolder(vh.mSecondaryControlsDock); 187 if (!mSecondaryActionsHidden) { 188 vh.mSecondaryControlsDock.addView(vh.mSecondaryControlsVh.view); 189 } 190 } 191 192 private void setBackground(View view) { 193 view.setBackgroundColor(mBackgroundColorSet ? 194 mBackgroundColor : getDefaultBackgroundColor(view.getContext())); 195 ShadowHelper.getInstance().setZ(view, 0f); 196 } 197 198 @Override 199 protected void onBindRowViewHolder(RowPresenter.ViewHolder holder, Object item) { 200 super.onBindRowViewHolder(holder, item); 201 202 ViewHolder vh = (ViewHolder) holder; 203 PlaybackControlsRow row = (PlaybackControlsRow) vh.getRow(); 204 205 mPlaybackControlsPresenter.enableSecondaryActions(mSecondaryActionsHidden); 206 207 if (row.getItem() == null) { 208 LayoutParams lp = vh.mCard.getLayoutParams(); 209 lp.height = LayoutParams.WRAP_CONTENT; 210 vh.mCard.setLayoutParams(lp); 211 vh.mDescriptionDock.setVisibility(View.GONE); 212 vh.mSpacer.setVisibility(View.GONE); 213 } else { 214 LayoutParams lp = vh.mCard.getLayoutParams(); 215 lp.height = vh.mCardHeight; 216 vh.mCard.setLayoutParams(lp); 217 vh.mDescriptionDock.setVisibility(View.VISIBLE); 218 if (vh.mDescriptionVh != null) { 219 mDescriptionPresenter.onBindViewHolder(vh.mDescriptionVh, row.getItem()); 220 } 221 vh.mSpacer.setVisibility(View.VISIBLE); 222 } 223 224 MarginLayoutParams lp = (MarginLayoutParams) vh.mControlsDock.getLayoutParams(); 225 if (row.getImageDrawable() == null || row.getItem() == null) { 226 setBackground(vh.mControlsDock); 227 vh.mCard.setBackgroundColor(Color.TRANSPARENT); 228 lp.setMarginStart(0); 229 lp.setMarginEnd(0); 230 } else { 231 vh.mImageView.setImageDrawable(row.getImageDrawable()); 232 setBackground(vh.mCard); 233 vh.mControlsDock.setBackgroundColor(Color.TRANSPARENT); 234 lp.setMarginStart(vh.mControlsDockMarginStart); 235 lp.setMarginEnd(vh.mControlsDockMarginEnd); 236 } 237 vh.mControlsDock.setLayoutParams(lp); 238 239 vh.mControlsBoundData.adapter = row.getPrimaryActionsAdapter(); 240 vh.mControlsBoundData.secondaryActionsAdapter = row.getSecondaryActionsAdapter(); 241 vh.mControlsBoundData.presenter = vh.getPresenter( 242 row.getPrimaryActionsAdapter().get(0), true); 243 mPlaybackControlsPresenter.onBindViewHolder(vh.mControlsVh, vh.mControlsBoundData); 244 245 vh.mSecondaryBoundData.adapter = row.getSecondaryActionsAdapter(); 246 vh.mSecondaryBoundData.presenter = vh.getPresenter( 247 row.getSecondaryActionsAdapter().get(0), false); 248 mSecondaryControlsPresenter.onBindViewHolder(vh.mSecondaryControlsVh, 249 vh.mSecondaryBoundData); 250 } 251 252 @Override 253 protected void onUnbindRowViewHolder(RowPresenter.ViewHolder holder) { 254 super.onUnbindRowViewHolder(holder); 255 256 ViewHolder vh = (ViewHolder) holder; 257 if (vh.mDescriptionVh != null) { 258 mDescriptionPresenter.onUnbindViewHolder(vh.mDescriptionVh); 259 } 260 mPlaybackControlsPresenter.onUnbindViewHolder(vh.mControlsVh); 261 mSecondaryControlsPresenter.onUnbindViewHolder(vh.mSecondaryControlsVh); 262 } 263} 264