1 /* <lambda>null2 * 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 package com.android.customization.picker.clock.ui.fragment 17 18 import android.os.Bundle 19 import android.view.LayoutInflater 20 import android.view.View 21 import android.view.ViewGroup 22 import androidx.cardview.widget.CardView 23 import androidx.core.content.ContextCompat 24 import androidx.core.view.isVisible 25 import androidx.lifecycle.ViewModelProvider 26 import androidx.lifecycle.get 27 import androidx.transition.Transition 28 import androidx.transition.doOnStart 29 import com.android.customization.module.ThemePickerInjector 30 import com.android.customization.picker.clock.ui.binder.ClockSettingsBinder 31 import com.android.systemui.shared.clocks.shared.model.ClockPreviewConstants 32 import com.android.wallpaper.R 33 import com.android.wallpaper.module.CustomizationSections 34 import com.android.wallpaper.module.InjectorProvider 35 import com.android.wallpaper.picker.AppbarFragment 36 import com.android.wallpaper.picker.customization.ui.binder.ScreenPreviewBinder 37 import com.android.wallpaper.picker.customization.ui.viewmodel.ScreenPreviewViewModel 38 import com.android.wallpaper.util.PreviewUtils 39 import kotlinx.coroutines.ExperimentalCoroutinesApi 40 import kotlinx.coroutines.suspendCancellableCoroutine 41 42 @OptIn(ExperimentalCoroutinesApi::class) 43 class ClockSettingsFragment : AppbarFragment() { 44 companion object { 45 const val DESTINATION_ID = "clock_settings" 46 47 @JvmStatic 48 fun newInstance(): ClockSettingsFragment { 49 return ClockSettingsFragment() 50 } 51 } 52 53 override fun onCreateView( 54 inflater: LayoutInflater, 55 container: ViewGroup?, 56 savedInstanceState: Bundle? 57 ): View { 58 val view = 59 inflater.inflate( 60 R.layout.fragment_clock_settings, 61 container, 62 false, 63 ) 64 setUpToolbar(view) 65 66 val context = requireContext() 67 val activity = requireActivity() 68 val injector = InjectorProvider.getInjector() as ThemePickerInjector 69 70 val lockScreenView: CardView = view.requireViewById(R.id.lock_preview) 71 val colorViewModel = injector.getWallpaperColorsViewModel() 72 val displayUtils = injector.getDisplayUtils(context) 73 ScreenPreviewBinder.bind( 74 activity = activity, 75 previewView = lockScreenView, 76 viewModel = 77 ScreenPreviewViewModel( 78 previewUtils = 79 PreviewUtils( 80 context = context, 81 authority = 82 resources.getString( 83 R.string.lock_screen_preview_provider_authority, 84 ), 85 ), 86 wallpaperInfoProvider = { forceReload -> 87 suspendCancellableCoroutine { continuation -> 88 injector 89 .getCurrentWallpaperInfoFactory(context) 90 .createCurrentWallpaperInfos( 91 { homeWallpaper, lockWallpaper, _ -> 92 continuation.resume( 93 lockWallpaper ?: homeWallpaper, 94 null, 95 ) 96 }, 97 forceReload, 98 ) 99 } 100 }, 101 onWallpaperColorChanged = { colors -> 102 colorViewModel.setLockWallpaperColors(colors) 103 }, 104 initialExtrasProvider = { 105 Bundle().apply { 106 // Hide the clock from the system UI rendered preview so we can 107 // place the carousel on top of it. 108 putBoolean( 109 ClockPreviewConstants.KEY_HIDE_CLOCK, 110 true, 111 ) 112 } 113 }, 114 wallpaperInteractor = injector.getWallpaperInteractor(requireContext()), 115 screen = CustomizationSections.Screen.LOCK_SCREEN, 116 ), 117 lifecycleOwner = this, 118 offsetToStart = displayUtils.isSingleDisplayOrUnfoldedHorizontalHinge(activity), 119 onWallpaperPreviewDirty = { activity.recreate() }, 120 ) 121 122 ClockSettingsBinder.bind( 123 view, 124 ViewModelProvider( 125 this, 126 injector.getClockSettingsViewModelFactory( 127 context, 128 injector.getWallpaperColorsViewModel(), 129 injector.getClockViewFactory(activity), 130 ), 131 ) 132 .get(), 133 injector.getClockViewFactory(activity), 134 viewLifecycleOwner, 135 ) 136 137 (returnTransition as? Transition)?.doOnStart { lockScreenView.isVisible = false } 138 139 return view 140 } 141 142 override fun getDefaultTitle(): CharSequence { 143 return requireContext().getString(R.string.clock_color_and_size_title) 144 } 145 146 override fun getToolbarColorId(): Int { 147 return android.R.color.transparent 148 } 149 150 override fun getToolbarTextColor(): Int { 151 return ContextCompat.getColor(requireContext(), R.color.system_on_surface) 152 } 153 } 154