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