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, View itemView)44     public IndividualHolder(Activity hostActivity, int tileHeightPx, View itemView) {
45         super(itemView);
46 
47         mActivity = hostActivity;
48         mTileLayout = itemView.findViewById(R.id.tile);
49         mThumbnailView = (ImageView) itemView.findViewById(R.id.thumbnail);
50         mOverlayIconView = (ImageView) itemView.findViewById(R.id.overlay_icon);
51         mTitleView = (TextView) itemView.findViewById(R.id.title);
52         mWallpaperContainer = itemView.findViewById(R.id.wallpaper_container);
53 
54         mWallpaperContainer.getLayoutParams().height = tileHeightPx;
55     }
56 
57     /**
58      * Binds the given wallpaper to this IndividualHolder.
59      */
bindWallpaper(WallpaperInfo wallpaper)60     public void bindWallpaper(WallpaperInfo wallpaper) {
61         mWallpaper = wallpaper;
62 
63         String title = wallpaper.getTitle(mActivity);
64 
65         List<String> attributions = wallpaper.getAttributions(mActivity);
66         String firstAttribution = attributions.size() > 0 ? attributions.get(0) : null;
67 
68         if (title != null) {
69             mTitleView.setText(title);
70             mTitleView.setVisibility(View.VISIBLE);
71             mTileLayout.setContentDescription(title);
72         } else if (firstAttribution != null) {
73             mTileLayout.setContentDescription(firstAttribution);
74         }
75 
76         Drawable overlayIcon = wallpaper.getOverlayIcon(mActivity);
77         if (overlayIcon != null) {
78             mOverlayIconView.setImageDrawable(overlayIcon);
79         } else {
80             wallpaper.getThumbAsset(
81                     mActivity.getApplicationContext()).loadDrawable(mActivity, mThumbnailView,
82                     ResourceUtils.getColorAttr(mActivity, android.R.attr.colorSecondary));
83         }
84     }
85 }
86