1 /* 2 * Copyright (C) 2023 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.preview.ui.section 19 20 import android.app.Activity 21 import android.content.Context 22 import android.view.ViewGroup 23 import android.view.ViewStub 24 import androidx.lifecycle.LifecycleOwner 25 import androidx.lifecycle.lifecycleScope 26 import com.android.customization.picker.clock.ui.binder.ClockCarouselViewBinder 27 import com.android.customization.picker.clock.ui.view.ClockCarouselView 28 import com.android.customization.picker.clock.ui.view.ClockViewFactory 29 import com.android.customization.picker.clock.ui.viewmodel.ClockCarouselViewModel 30 import com.android.wallpaper.R 31 import com.android.wallpaper.model.CustomizationSectionController 32 import com.android.wallpaper.model.WallpaperColorsViewModel 33 import com.android.wallpaper.module.CurrentWallpaperInfoFactory 34 import com.android.wallpaper.module.CustomizationSections 35 import com.android.wallpaper.picker.customization.domain.interactor.WallpaperInteractor 36 import com.android.wallpaper.picker.customization.ui.section.ScreenPreviewSectionController 37 import com.android.wallpaper.picker.customization.ui.section.ScreenPreviewView 38 import com.android.wallpaper.util.DisplayUtils 39 import kotlinx.coroutines.launch 40 41 /** Controls the screen preview section. */ 42 class PreviewWithClockCarouselSectionController( 43 activity: Activity, 44 private val lifecycleOwner: LifecycleOwner, 45 private val initialScreen: CustomizationSections.Screen, 46 wallpaperInfoFactory: CurrentWallpaperInfoFactory, 47 colorViewModel: WallpaperColorsViewModel, 48 displayUtils: DisplayUtils, 49 private val clockCarouselViewModel: ClockCarouselViewModel, 50 private val clockViewFactory: ClockViewFactory, 51 navigator: CustomizationSectionController.CustomizationSectionNavigationController, 52 wallpaperInteractor: WallpaperInteractor, 53 ) : 54 ScreenPreviewSectionController( 55 activity, 56 lifecycleOwner, 57 initialScreen, 58 wallpaperInfoFactory, 59 colorViewModel, 60 displayUtils, 61 navigator, 62 wallpaperInteractor, 63 ) { 64 65 private var clockCarouselBinding: ClockCarouselViewBinder.Binding? = null 66 67 override val hideLockScreenClockPreview = true 68 createViewnull69 override fun createView(context: Context): ScreenPreviewView { 70 val view = super.createView(context) 71 val carouselViewStub: ViewStub = view.requireViewById(R.id.clock_carousel_view_stub) 72 carouselViewStub.layoutResource = R.layout.clock_carousel_view 73 val carouselView = carouselViewStub.inflate() as ClockCarouselView 74 75 // TODO (b/270716937) We should handle the single clock case in the clock carousel itself 76 val singleClockViewStub: ViewStub = view.requireViewById(R.id.single_clock_view_stub) 77 singleClockViewStub.layoutResource = R.layout.single_clock_view 78 val singleClockView = singleClockViewStub.inflate() as ViewGroup 79 lifecycleOwner.lifecycleScope.launch { 80 clockCarouselBinding = 81 ClockCarouselViewBinder.bind( 82 carouselView = carouselView, 83 singleClockView = singleClockView, 84 viewModel = clockCarouselViewModel, 85 clockViewFactory = clockViewFactory, 86 lifecycleOwner = lifecycleOwner, 87 ) 88 onScreenSwitched( 89 isOnLockScreen = initialScreen == CustomizationSections.Screen.LOCK_SCREEN 90 ) 91 } 92 return view 93 } 94 onScreenSwitchednull95 override fun onScreenSwitched(isOnLockScreen: Boolean) { 96 super.onScreenSwitched(isOnLockScreen) 97 if (isOnLockScreen) { 98 clockCarouselBinding?.show() 99 } else { 100 clockCarouselBinding?.hide() 101 } 102 } 103 } 104