1 /*
<lambda>null2  * 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.content.Intent
20 import com.android.wallpaper.R
21 import com.android.wallpaper.model.WallpaperInfo
22 import com.android.wallpaper.module.InjectorProvider
23 import com.android.wallpaper.picker.WallpaperInfoHelper
24 
25 /** Floating Sheet Content for displaying wallpaper info */
26 class WallpaperInfoContent(private var context: Context, private val wallpaper: WallpaperInfo?) :
27     FloatingSheetContent<WallpaperInfoView>(context) {
28 
29     private var exploreIntent: Intent? = null
30     private var actionLabel: CharSequence? = null
31     private var wallpaperInfoView: WallpaperInfoView? = null
32     override val viewId: Int
33         get() = R.layout.floating_sheet_wallpaper_info_view
34 
35     /** Gets called when the content view is created or recreated by [FloatingSheetContent] */
36     override fun onViewCreated(view: WallpaperInfoView) {
37         wallpaperInfoView = view
38         context = view.context
39         initializeWallpaperContent()
40     }
41 
42     private fun initializeWallpaperContent() {
43         if (wallpaper == null) {
44             return
45         }
46         if (actionLabel == null) {
47             setUpExploreIntentAndLabel { populateWallpaperInfo(wallpaperInfoView) }
48         } else {
49             populateWallpaperInfo(wallpaperInfoView)
50         }
51     }
52 
53     private fun setUpExploreIntentAndLabel(callback: Runnable?) {
54         WallpaperInfoHelper.loadExploreIntent(context, wallpaper!!) {
55             actionLabel: CharSequence?,
56             exploreIntent: Intent? ->
57             this.actionLabel = actionLabel
58             this.exploreIntent = exploreIntent
59             callback?.run()
60         }
61     }
62 
63     private fun onExploreClicked() {
64         val injector = InjectorProvider.getInjector()
65         injector.getUserEventLogger().logWallpaperExploreButtonClicked()
66         context.startActivity(exploreIntent)
67     }
68 
69     private fun populateWallpaperInfo(view: WallpaperInfoView?) {
70         view!!.populateWallpaperInfo(
71             wallpaper!!,
72             actionLabel,
73             WallpaperInfoHelper.shouldShowExploreButton(context, exploreIntent)
74         ) {
75             onExploreClicked()
76         }
77     }
78 }
79