1 /*
2  * Copyright (C) 2017 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 package com.android.wallpaper.picker.individual;
17 
18 import android.app.Activity;
19 import android.graphics.drawable.Drawable;
20 import android.view.View;
21 import android.widget.ImageView;
22 import android.widget.TextView;
23 
24 import androidx.recyclerview.widget.RecyclerView.ViewHolder;
25 
26 import com.android.wallpaper.R;
27 import com.android.wallpaper.model.WallpaperInfo;
28 import com.android.wallpaper.util.ResourceUtils;
29 
30 import java.util.List;
31 
32 /**
33  * Base class for ViewHolders for individual wallpaper tiles.
34  */
35 abstract class IndividualHolder extends ViewHolder {
36     protected Activity mActivity;
37     protected View mTileLayout;
38     protected View mWallpaperContainer;
39     protected ImageView mThumbnailView;
40     protected ImageView mOverlayIconView;
41     protected TextView mTitleView;
42     protected WallpaperInfo mWallpaper;
43 
IndividualHolder(Activity hostActivity, int tileHeightPx, int tileWidthPx, View itemView)44     IndividualHolder(Activity hostActivity, int tileHeightPx, int tileWidthPx,
45              View itemView) {
46         super(itemView);
47 
48         mActivity = hostActivity;
49         mTileLayout = itemView.findViewById(R.id.tile);
50         mThumbnailView = (ImageView) itemView.findViewById(R.id.thumbnail);
51         mOverlayIconView = (ImageView) itemView.findViewById(R.id.overlay_icon);
52         mTitleView = (TextView) itemView.findViewById(R.id.title);
53         mWallpaperContainer = itemView.findViewById(R.id.wallpaper_container);
54         mTileLayout.getLayoutParams().width = tileWidthPx;
55 
56         mWallpaperContainer.getLayoutParams().height = tileHeightPx;
57     }
58 
59     /**
60      * Binds the given wallpaper to this IndividualHolder.
61      */
bindWallpaper(WallpaperInfo wallpaper)62     public void bindWallpaper(WallpaperInfo wallpaper) {
63         mWallpaper = wallpaper;
64 
65         String title = wallpaper.getTitle(mActivity);
66 
67         List<String> attributions = wallpaper.getAttributions(mActivity);
68         String firstAttribution = attributions.size() > 0 ? attributions.get(0) : null;
69 
70         if (title != null) {
71             mTitleView.setText(title);
72             mTitleView.setVisibility(View.VISIBLE);
73             mTileLayout.setContentDescription(title);
74         } else if (firstAttribution != null) {
75             String contentDescription = wallpaper.getContentDescription(mActivity);
76             mTileLayout.setContentDescription(
77                     contentDescription != null ? contentDescription : firstAttribution);
78         }
79 
80         Drawable overlayIcon = wallpaper.getOverlayIcon(mActivity);
81         if (overlayIcon != null) {
82             mOverlayIconView.setImageDrawable(overlayIcon);
83         } else {
84             wallpaper.getThumbAsset(
85                     mActivity.getApplicationContext()).loadDrawable(mActivity, mThumbnailView,
86                     ResourceUtils.getColorAttr(mActivity, android.R.attr.colorSecondary));
87         }
88     }
89 }
90