1 /* 2 * Copyright 2018 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.car.media.browse; 18 19 import android.content.Context; 20 import android.text.TextUtils; 21 import android.util.Size; 22 import android.view.View; 23 import android.view.ViewGroup; 24 import android.widget.ImageView; 25 import android.widget.TextView; 26 27 import androidx.recyclerview.widget.RecyclerView; 28 29 import com.android.car.apps.common.imaging.ImageViewBinder; 30 import com.android.car.apps.common.util.ViewUtils; 31 import com.android.car.media.MediaAppConfig; 32 import com.android.car.media.common.MediaItemMetadata; 33 34 /** 35 * Generic {@link RecyclerView.ViewHolder} to use for all views in the {@link BrowseAdapter} 36 */ 37 public class BrowseViewHolder extends RecyclerView.ViewHolder { 38 private final TextView mTitle; 39 private final TextView mSubtitle; 40 private final ImageView mAlbumArt; 41 private final ViewGroup mContainer; 42 private final ImageView mRightArrow; 43 private final ImageView mTitleDownloadIcon; 44 private final ImageView mTitleExplicitIcon; 45 private final ImageView mSubTitleDownloadIcon; 46 private final ImageView mSubTitleExplicitIcon; 47 48 private final ImageViewBinder<MediaItemMetadata.ArtworkRef> mAlbumArtBinder; 49 50 /** 51 * Creates a {@link BrowseViewHolder} for the given view. 52 */ BrowseViewHolder(View itemView)53 BrowseViewHolder(View itemView) { 54 super(itemView); 55 mTitle = itemView.findViewById(com.android.car.media.R.id.title); 56 mSubtitle = itemView.findViewById(com.android.car.media.R.id.subtitle); 57 mAlbumArt = itemView.findViewById(com.android.car.media.R.id.thumbnail); 58 mContainer = itemView.findViewById(com.android.car.media.R.id.item_container); 59 mRightArrow = itemView.findViewById(com.android.car.media.R.id.right_arrow); 60 mTitleDownloadIcon = itemView.findViewById( 61 com.android.car.media.R.id.download_icon_with_title); 62 mTitleExplicitIcon = itemView.findViewById( 63 com.android.car.media.R.id.explicit_icon_with_title); 64 mSubTitleDownloadIcon = itemView.findViewById( 65 com.android.car.media.R.id.download_icon_with_subtitle); 66 mSubTitleExplicitIcon = itemView.findViewById( 67 com.android.car.media.R.id.explicit_icon_with_subtitle); 68 69 Size maxArtSize = MediaAppConfig.getMediaItemsBitmapMaxSize(itemView.getContext()); 70 mAlbumArtBinder = new ImageViewBinder<>(maxArtSize, mAlbumArt); 71 } 72 73 74 /** 75 * Updates this {@link BrowseViewHolder} with the given data 76 */ bind(Context context, BrowseViewData data)77 public void bind(Context context, BrowseViewData data) { 78 79 boolean hasMediaItem = data.mMediaItem != null; 80 boolean showSubtitle = hasMediaItem && !TextUtils.isEmpty(data.mMediaItem.getSubtitle()); 81 82 if (mTitle != null) { 83 mTitle.setText(data.mText != null ? data.mText : 84 hasMediaItem ? data.mMediaItem.getTitle() : null); 85 } 86 if (mSubtitle != null) { 87 mSubtitle.setText(hasMediaItem ? data.mMediaItem.getSubtitle() : null); 88 ViewUtils.setVisible(mSubtitle, showSubtitle); 89 } 90 91 mAlbumArtBinder.setImage(context, hasMediaItem ? data.mMediaItem.getArtworkKey() : null); 92 93 if (mContainer != null && data.mOnClickListener != null) { 94 mContainer.setOnClickListener(data.mOnClickListener); 95 } 96 ViewUtils.setVisible(mRightArrow, hasMediaItem && data.mMediaItem.isBrowsable()); 97 98 // Adjust the positioning of the explicit and downloaded icons. If there is a subtitle, then 99 // the icons should show on the subtitle row, otherwise they should show on the title row. 100 boolean downloaded = hasMediaItem && data.mMediaItem.isDownloaded(); 101 boolean explicit = hasMediaItem && data.mMediaItem.isExplicit(); 102 ViewUtils.setVisible(mTitleDownloadIcon, !showSubtitle && downloaded); 103 ViewUtils.setVisible(mTitleExplicitIcon, !showSubtitle && explicit); 104 ViewUtils.setVisible(mSubTitleDownloadIcon, showSubtitle && downloaded); 105 ViewUtils.setVisible(mSubTitleExplicitIcon, showSubtitle && explicit); 106 } 107 onViewAttachedToWindow(Context context)108 void onViewAttachedToWindow(Context context) { 109 mAlbumArtBinder.maybeRestartLoading(context); 110 } 111 onViewDetachedFromWindow(Context context)112 void onViewDetachedFromWindow(Context context) { 113 mAlbumArtBinder.maybeCancelLoading(context); 114 } 115 } 116