• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         executorService.execute {
54             val attributions = wallpaperInfo.getAttributions(context)
55             Handler(Looper.getMainLooper()).post {
56 
57                 // Reset wallpaper information UI
58                 title?.text = ""
59                 subtitle1?.text = ""
60                 subtitle1?.visibility = GONE
61                 subtitle2?.text = ""
62                 subtitle2?.visibility = GONE
63                 exploreButton?.text = ""
64                 exploreButton?.setOnClickListener(null)
65                 exploreButton?.visibility = GONE
66                 if (attributions.size > 0 && attributions[0] != null) {
67                     title?.text = attributions[0]
68                 }
69                 if (shouldShowMetadata(wallpaperInfo)) {
70                     if (attributions.size > 1 && attributions[1] != null) {
71                         subtitle1?.visibility = VISIBLE
72                         subtitle1?.text = attributions[1]
73                     }
74                     if (attributions.size > 2 && attributions[2] != null) {
75                         subtitle2?.visibility = VISIBLE
76                         subtitle2?.text = attributions[2]
77                     }
78                     if (shouldShowExploreButton) {
79                         exploreButton?.visibility = VISIBLE
80                         exploreButton?.text = actionLabel
81                         exploreButton?.setOnClickListener(exploreButtonClickListener)
82                     }
83                 }
84             }
85         }
86     }
87 
shouldShowMetadatanull88     private fun shouldShowMetadata(wallpaperInfo: WallpaperInfo): Boolean {
89         val wallpaperComponent = wallpaperInfo.wallpaperComponent
90         return wallpaperComponent == null || wallpaperComponent.showMetadataInPreview
91     }
92 }
93