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  */
17 
18 package com.android.customization.picker.quickaffordance.ui.binder
19 
20 import android.app.Activity
21 import android.os.Bundle
22 import androidx.cardview.widget.CardView
23 import androidx.lifecycle.Lifecycle
24 import androidx.lifecycle.LifecycleOwner
25 import androidx.lifecycle.flowWithLifecycle
26 import androidx.lifecycle.lifecycleScope
27 import com.android.customization.picker.quickaffordance.ui.viewmodel.KeyguardQuickAffordancePickerViewModel
28 import com.android.systemui.shared.quickaffordance.shared.model.KeyguardPreviewConstants
29 import com.android.wallpaper.R
30 import com.android.wallpaper.picker.customization.ui.binder.ScreenPreviewBinder
31 import kotlinx.coroutines.launch
32 
33 object KeyguardQuickAffordancePreviewBinder {
34 
35     /** Binds view for the preview of the lock screen. */
36     @JvmStatic
37     fun bind(
38         activity: Activity,
39         previewView: CardView,
40         viewModel: KeyguardQuickAffordancePickerViewModel,
41         lifecycleOwner: LifecycleOwner,
42         offsetToStart: Boolean,
43     ) {
44         val binding =
45             ScreenPreviewBinder.bind(
46                 activity = activity,
47                 previewView = previewView,
48                 viewModel = viewModel.preview,
49                 lifecycleOwner = lifecycleOwner,
50                 offsetToStart = offsetToStart,
51                 dimWallpaper = true,
52                 onWallpaperPreviewDirty = { activity.recreate() },
53             )
54 
55         previewView.contentDescription =
56             previewView.context.getString(
57                 R.string.lockscreen_wallpaper_preview_card_content_description
58             )
59 
60         lifecycleOwner.lifecycleScope.launch {
61             viewModel.selectedSlotId
62                 .flowWithLifecycle(lifecycleOwner.lifecycle, Lifecycle.State.STARTED)
63                 .collect { slotId ->
64                     binding.sendMessage(
65                         KeyguardPreviewConstants.MESSAGE_ID_SLOT_SELECTED,
66                         Bundle().apply { putString(KeyguardPreviewConstants.KEY_SLOT_ID, slotId) },
67                     )
68                 }
69         }
70     }
71 }
72