10246318f27a905a31df5a8af445cfe67d31dfb68Dake Gupackage android.support.v17.leanback.widget; 20246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu 30246318f27a905a31df5a8af445cfe67d31dfb68Dake Guimport android.support.v17.leanback.R; 40246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu 50246318f27a905a31df5a8af445cfe67d31dfb68Dake Guimport android.graphics.drawable.Drawable; 60246318f27a905a31df5a8af445cfe67d31dfb68Dake Guimport android.view.LayoutInflater; 70246318f27a905a31df5a8af445cfe67d31dfb68Dake Guimport android.view.View; 80246318f27a905a31df5a8af445cfe67d31dfb68Dake Guimport android.view.ViewGroup; 90246318f27a905a31df5a8af445cfe67d31dfb68Dake Guimport android.view.ViewGroup.MarginLayoutParams; 100246318f27a905a31df5a8af445cfe67d31dfb68Dake Guimport android.widget.ImageView; 110246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu 120246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu/** 130246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu * Presenter that responsible to create a ImageView and bind to DetailsOverviewRow. The default 140246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu * implementation uses {@link DetailsOverviewRow#getImageDrawable()} and binds to {@link ImageView}. 150246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu * <p> 16adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * Default implementation assumes no scaleType on ImageView and uses intrinsic width and height of 17adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * {@link DetailsOverviewRow#getImageDrawable()} to initialize ImageView's layout params. To 18adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * specify a fixed size and/or specify a scapeType, subclass should change ImageView's layout params 19adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * and scaleType in {@link #onCreateView(ViewGroup)}. 20adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * <p> 210246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu * Subclass may override and has its own image view. Subclass may also download image from URL 220246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu * instead of using {@link DetailsOverviewRow#getImageDrawable()}. It's subclass's responsibility to 230246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu * call {@link FullWidthDetailsOverviewRowPresenter#notifyOnBindLogo(FullWidthDetailsOverviewRowPresenter.ViewHolder)} 240246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu * whenever {@link #isBoundToImage(ViewHolder, DetailsOverviewRow)} turned to true so that activity 250246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu * transition can be started. 260246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu */ 270246318f27a905a31df5a8af445cfe67d31dfb68Dake Gupublic class DetailsOverviewLogoPresenter extends Presenter { 280246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu 29adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu /** 30adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * ViewHolder for Logo view of DetailsOverviewRow. 31adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu */ 320246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu public static class ViewHolder extends Presenter.ViewHolder { 330246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu 340246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu protected FullWidthDetailsOverviewRowPresenter mParentPresenter; 350246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu protected FullWidthDetailsOverviewRowPresenter.ViewHolder mParentViewHolder; 36adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu private boolean mSizeFromDrawableIntrinsic; 370246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu 380246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu public ViewHolder(View view) { 390246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu super(view); 400246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu } 41adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu 42adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu public FullWidthDetailsOverviewRowPresenter getParentPresenter() { 43adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu return mParentPresenter; 44adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu } 45adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu 46adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu public FullWidthDetailsOverviewRowPresenter.ViewHolder getParentViewHolder() { 47adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu return mParentViewHolder; 48adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu } 49adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu 50adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu /** 51adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * @return True if layout size of ImageView should be changed to intrinsic size of Drawable, 52adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * false otherwise. Used by 53adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * {@link DetailsOverviewLogoPresenter#onBindViewHolder(Presenter.ViewHolder, Object)} 54adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * . 55adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * 56adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * @see DetailsOverviewLogoPresenter#onCreateView(ViewGroup) 57adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * @see DetailsOverviewLogoPresenter#onBindViewHolder(Presenter.ViewHolder, Object) 58adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu */ 59adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu public boolean isSizeFromDrawableIntrinsic() { 60adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu return mSizeFromDrawableIntrinsic; 61adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu } 62adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu 63adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu /** 64adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * Change if the ImageView layout size should be synchronized to Drawable intrinsic size. 65adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * Used by 66adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * {@link DetailsOverviewLogoPresenter#onBindViewHolder(Presenter.ViewHolder, Object)}. 67adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * 68adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * @param sizeFromDrawableIntrinsic True if layout size of ImageView should be changed to 69adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * intrinsic size of Drawable, false otherwise. 70adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * 71adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * @see DetailsOverviewLogoPresenter#onCreateView(ViewGroup) 72adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * @see DetailsOverviewLogoPresenter#onBindViewHolder(Presenter.ViewHolder, Object) 73adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu */ 74adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu public void setSizeFromDrawableIntrinsic(boolean sizeFromDrawableIntrinsic) { 75adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu mSizeFromDrawableIntrinsic = sizeFromDrawableIntrinsic; 76adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu } 77adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu } 78adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu 79adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu /** 80adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * Create a View for the Logo, default implementation loads from 81adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * {@link R.layout#lb_fullwidth_details_overview_logo}. Subclass may override this method to use 82adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * a fixed layout size and change ImageView scaleType. If the layout params is WRAP_CONTENT for 83adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * both width and size, the ViewHolder would be using intrinsic size of Drawable in 84adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * {@link #onBindViewHolder(Presenter.ViewHolder, Object)}. 85adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * 86adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * @param parent Parent view. 87adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * @return View created for the logo. 88adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu */ 89adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu public View onCreateView(ViewGroup parent) { 90adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu return LayoutInflater.from(parent.getContext()) 91adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu .inflate(R.layout.lb_fullwidth_details_overview_logo, parent, false); 920246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu } 930246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu 940246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu @Override 950246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu public Presenter.ViewHolder onCreateViewHolder(ViewGroup parent) { 96adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu View view = onCreateView(parent); 97adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu ViewHolder vh = new ViewHolder(view); 98adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu ViewGroup.LayoutParams lp = view.getLayoutParams(); 99adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu vh.setSizeFromDrawableIntrinsic(lp.width == ViewGroup.LayoutParams.WRAP_CONTENT && 100adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu lp.width == ViewGroup.LayoutParams.WRAP_CONTENT); 101adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu return vh; 1020246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu } 1030246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu 1040246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu /** 1050246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu * Called from {@link FullWidthDetailsOverviewRowPresenter} to setup FullWidthDetailsOverviewRowPresenter 1060246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu * and FullWidthDetailsOverviewRowPresenter.ViewHolder that hosts the logo. 1070246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu * @param viewHolder 1080246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu * @param parentViewHolder 1090246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu * @param parentPresenter 1100246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu */ 1110246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu public void setContext(ViewHolder viewHolder, 1120246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu FullWidthDetailsOverviewRowPresenter.ViewHolder parentViewHolder, 1130246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu FullWidthDetailsOverviewRowPresenter parentPresenter) { 1140246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu viewHolder.mParentViewHolder = parentViewHolder; 1150246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu viewHolder.mParentPresenter = parentPresenter; 1160246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu } 1170246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu 1180246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu /** 1190246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu * Returns true if the logo view is bound to image. Subclass may override. The default 1200246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu * implementation returns true when {@link DetailsOverviewRow#getImageDrawable()} is not null. 1210246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu * If subclass of DetailsOverviewLogoPresenter manages its own image drawable, it should 1220246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu * override this function to report status correctly and invoke 1230246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu * {@link FullWidthDetailsOverviewRowPresenter#notifyOnBindLogo(FullWidthDetailsOverviewRowPresenter.ViewHolder)} 1240246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu * when image view is bound to the drawable. 1250246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu */ 1260246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu public boolean isBoundToImage(ViewHolder viewHolder, DetailsOverviewRow row) { 1270246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu return row != null && row.getImageDrawable() != null; 1280246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu } 1290246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu 130adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu /** 131adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * Bind logo View to drawble of DetailsOverviewRow and call notifyOnBindLogo(). The 132adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * default implementation assumes the Logo View is an ImageView and change layout size to 133adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * intrinsic size of ImageDrawable if {@link ViewHolder#isSizeFromDrawableIntrinsic()} is true. 134adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * @param viewHolder ViewHolder to bind. 135adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu * @param item DetailsOverviewRow object to bind. 136adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu */ 1370246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu @Override 1380246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) { 1390246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu DetailsOverviewRow row = (DetailsOverviewRow) item; 1400246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu ImageView imageView = ((ImageView) viewHolder.view); 1410246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu imageView.setImageDrawable(row.getImageDrawable()); 1420246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu if (isBoundToImage((ViewHolder) viewHolder, row)) { 1430246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu ViewHolder vh = (ViewHolder) viewHolder; 144adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu if (vh.isSizeFromDrawableIntrinsic()) { 145adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu ViewGroup.LayoutParams lp = imageView.getLayoutParams(); 146adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu lp.width = row.getImageDrawable().getIntrinsicWidth(); 147adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu lp.height = row.getImageDrawable().getIntrinsicHeight(); 148adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu if (imageView.getMaxWidth() > 0 || imageView.getMaxHeight() > 0) { 149adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu float maxScaleWidth = 1f; 150adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu if (imageView.getMaxWidth() > 0) { 151adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu if (lp.width > imageView.getMaxWidth()) { 152adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu maxScaleWidth = imageView.getMaxWidth() / (float) lp.width; 153adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu } 154adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu } 155adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu float maxScaleHeight = 1f; 156adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu if (imageView.getMaxHeight() > 0) { 157adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu if (lp.height > imageView.getMaxHeight()) { 158adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu maxScaleHeight = imageView.getMaxHeight() / (float) lp.height; 159adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu } 160adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu } 161adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu float scale = Math.min(maxScaleWidth, maxScaleHeight); 162adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu lp.width = (int) (lp.width * scale); 163adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu lp.height = (int) (lp.height * scale); 164adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu } 165adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu imageView.setLayoutParams(lp); 166adb84c07a691bd044b470c2e2ffe6c3ae630ad50Dake Gu } 1670246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu vh.mParentPresenter.notifyOnBindLogo(vh.mParentViewHolder); 1680246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu } 1690246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu } 1700246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu 1710246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu @Override 1720246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) { 1730246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu } 1740246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu 1750246318f27a905a31df5a8af445cfe67d31dfb68Dake Gu} 176