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