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