1 /*
2  * Copyright (C) 2022 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.floatingsheetcontent
17 
18 import android.content.Context
19 import android.os.Handler
20 import android.os.Looper
21 import android.util.AttributeSet
22 import android.widget.Button
23 import android.widget.LinearLayout
24 import android.widget.TextView
25 import com.android.wallpaper.R
26 import com.android.wallpaper.model.WallpaperInfo
27 import java.util.concurrent.Executors
28 
29 /** A view for displaying wallpaper info. */
30 class WallpaperInfoView(context: Context?, attrs: AttributeSet?) : LinearLayout(context, attrs) {
31 
32     private val executorService = Executors.newCachedThreadPool()
33     private var title: TextView? = null
34     private var subtitle1: TextView? = null
35     private var subtitle2: TextView? = null
36     private var exploreButton: Button? = null
37 
onFinishInflatenull38     override fun onFinishInflate() {
39         super.onFinishInflate()
40         title = findViewById(R.id.wallpaper_info_title)
41         subtitle1 = findViewById(R.id.wallpaper_info_subtitle1)
42         subtitle2 = findViewById(R.id.wallpaper_info_subtitle2)
43         exploreButton = findViewById(R.id.wallpaper_info_explore_button)
44     }
45 
46     /** Populates wallpaper info. */
populateWallpaperInfonull47     fun populateWallpaperInfo(
48         wallpaperInfo: WallpaperInfo,
49         actionLabel: CharSequence?,
50         shouldShowExploreButton: Boolean,
51         exploreButtonClickListener: OnClickListener?
52     ) {
53         loadWallpaperInfoData(
54             wallpaperInfo.getAttributions(context),
55             actionLabel,
56             shouldShowExploreButton,
57             exploreButtonClickListener,
58             shouldShowMetadata(wallpaperInfo)
59         )
60     }
61 
loadWallpaperInfoDatanull62     private fun loadWallpaperInfoData(
63         attributions: List<String?>?,
64         actionLabel: CharSequence?,
65         shouldShowExploreButton: Boolean,
66         exploreButtonClickListener: OnClickListener?,
67         shouldShowMetadata: Boolean,
68     ) {
69 
70         executorService.execute {
71             Handler(Looper.getMainLooper()).post {
72 
73                 // Reset wallpaper information UI
74                 title?.text = ""
75                 subtitle1?.text = ""
76                 subtitle1?.visibility = GONE
77                 subtitle2?.text = ""
78                 subtitle2?.visibility = GONE
79                 exploreButton?.text = ""
80                 exploreButton?.setOnClickListener(null)
81                 exploreButton?.visibility = GONE
82 
83                 if (attributions != null && attributions.size > 0 && attributions[0] != null) {
84                     title?.text = attributions[0]
85                 }
86                 if (shouldShowMetadata) {
87                     if (attributions != null && attributions.size > 1 && attributions[1] != null) {
88                         subtitle1?.visibility = VISIBLE
89                         subtitle1?.text = attributions[1]
90                     }
91                     if (attributions != null && attributions.size > 2 && attributions[2] != null) {
92                         subtitle2?.visibility = VISIBLE
93                         subtitle2?.text = attributions[2]
94                     }
95                     if (shouldShowExploreButton) {
96                         exploreButton?.visibility = VISIBLE
97                         exploreButton?.text = actionLabel
98                         exploreButton?.setOnClickListener(exploreButtonClickListener)
99                     }
100                 }
101             }
102         }
103     }
104 
shouldShowMetadatanull105     private fun shouldShowMetadata(wallpaperInfo: WallpaperInfo): Boolean {
106         val wallpaperComponent = wallpaperInfo.wallpaperComponent
107         return wallpaperComponent == null || wallpaperComponent.showMetadataInPreview
108     }
109 }
110