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