1 /* 2 * Copyright (C) 2020 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.widget; 17 18 import android.content.Context; 19 import android.util.AttributeSet; 20 import android.view.View; 21 import android.widget.Button; 22 import android.widget.LinearLayout; 23 import android.widget.TextView; 24 25 import androidx.annotation.NonNull; 26 import androidx.annotation.Nullable; 27 28 import com.android.wallpaper.R; 29 import com.android.wallpaper.model.WallpaperInfo; 30 31 import java.util.List; 32 33 /** A view for displaying wallpaper info. */ 34 public class WallpaperInfoView extends LinearLayout { 35 private TextView mTitle; 36 private TextView mSubtitle1; 37 private TextView mSubtitle2; 38 private Button mExploreButton; 39 WallpaperInfoView(Context context, @Nullable AttributeSet attrs)40 public WallpaperInfoView(Context context, @Nullable AttributeSet attrs) { 41 super(context, attrs); 42 } 43 44 @Override onFinishInflate()45 protected void onFinishInflate() { 46 super.onFinishInflate(); 47 mTitle = findViewById(R.id.wallpaper_info_title); 48 mSubtitle1 = findViewById(R.id.wallpaper_info_subtitle1); 49 mSubtitle2 = findViewById(R.id.wallpaper_info_subtitle2); 50 mExploreButton = findViewById(R.id.wallpaper_info_explore_button); 51 } 52 53 /** Populates wallpaper info. */ populateWallpaperInfo(@onNull WallpaperInfo wallpaperInfo, CharSequence actionLabel, boolean shouldShowExploreButton, OnClickListener exploreButtonClickListener)54 public void populateWallpaperInfo(@NonNull WallpaperInfo wallpaperInfo, 55 CharSequence actionLabel, 56 boolean shouldShowExploreButton, 57 OnClickListener exploreButtonClickListener) { 58 final List<String> attributions = wallpaperInfo.getAttributions(getContext()); 59 60 // Reset 61 mTitle.setText(null); 62 mSubtitle1.setText(null); 63 mSubtitle1.setVisibility(View.GONE); 64 mSubtitle2.setText(null); 65 mSubtitle2.setVisibility(View.GONE); 66 mExploreButton.setText(null); 67 mExploreButton.setOnClickListener(null); 68 mExploreButton.setVisibility(View.GONE); 69 70 if (attributions.size() > 0 && attributions.get(0) != null) { 71 mTitle.setText(attributions.get(0)); 72 } 73 74 if (shouldShowMetadata(wallpaperInfo)) { 75 if (attributions.size() > 1 && attributions.get(1) != null) { 76 mSubtitle1.setVisibility(View.VISIBLE); 77 mSubtitle1.setText(attributions.get(1)); 78 } 79 80 if (attributions.size() > 2 && attributions.get(2) != null) { 81 mSubtitle2.setVisibility(View.VISIBLE); 82 mSubtitle2.setText(attributions.get(2)); 83 } 84 85 if (shouldShowExploreButton) { 86 mExploreButton.setVisibility(View.VISIBLE); 87 mExploreButton.setText(actionLabel); 88 mExploreButton.setOnClickListener(exploreButtonClickListener); 89 } 90 } 91 } 92 shouldShowMetadata(WallpaperInfo wallpaperInfo)93 private boolean shouldShowMetadata(WallpaperInfo wallpaperInfo) { 94 android.app.WallpaperInfo wallpaperComponent = wallpaperInfo.getWallpaperComponent(); 95 return wallpaperComponent == null || wallpaperComponent.getShowMetadataInPreview(); 96 } 97 } 98