summaryrefslogtreecommitdiff
path: root/src/com/android/customization/picker/clock/ui/fragment/ClockSettingsFragment.kt
blob: f4684d882411901e22fdfc78812e53d8ba37f79b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.customization.picker.clock.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.cardview.widget.CardView
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.get
import com.android.customization.module.ThemePickerInjector
import com.android.customization.picker.clock.ui.binder.ClockSettingsBinder
import com.android.systemui.shared.clocks.shared.model.ClockPreviewConstants
import com.android.wallpaper.R
import com.android.wallpaper.module.CustomizationSections
import com.android.wallpaper.module.InjectorProvider
import com.android.wallpaper.picker.AppbarFragment
import com.android.wallpaper.picker.customization.ui.binder.ScreenPreviewBinder
import com.android.wallpaper.picker.customization.ui.viewmodel.ScreenPreviewViewModel
import com.android.wallpaper.util.PreviewUtils
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.suspendCancellableCoroutine

@OptIn(ExperimentalCoroutinesApi::class)
class ClockSettingsFragment : AppbarFragment() {
    companion object {
        const val DESTINATION_ID = "clock_settings"

        @JvmStatic
        fun newInstance(): ClockSettingsFragment {
            return ClockSettingsFragment()
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val view =
            inflater.inflate(
                R.layout.fragment_clock_settings,
                container,
                false,
            )
        setUpToolbar(view)

        val context = requireContext()
        val activity = requireActivity()
        val injector = InjectorProvider.getInjector() as ThemePickerInjector

        val lockScreenView: CardView = view.requireViewById(R.id.lock_preview)
        val colorViewModel = injector.getWallpaperColorsViewModel()
        val displayUtils = injector.getDisplayUtils(context)
        ScreenPreviewBinder.bind(
            activity = activity,
            previewView = lockScreenView,
            viewModel =
                ScreenPreviewViewModel(
                    previewUtils =
                        PreviewUtils(
                            context = context,
                            authority =
                                resources.getString(
                                    R.string.lock_screen_preview_provider_authority,
                                ),
                        ),
                    wallpaperInfoProvider = { forceReload ->
                        suspendCancellableCoroutine { continuation ->
                            injector
                                .getCurrentWallpaperInfoFactory(context)
                                .createCurrentWallpaperInfos(
                                    { homeWallpaper, lockWallpaper, _ ->
                                        continuation.resume(
                                            lockWallpaper ?: homeWallpaper,
                                            null,
                                        )
                                    },
                                    forceReload,
                                )
                        }
                    },
                    onWallpaperColorChanged = { colors ->
                        colorViewModel.setLockWallpaperColors(colors)
                    },
                    initialExtrasProvider = {
                        Bundle().apply {
                            // Hide the clock from the system UI rendered preview so we can
                            // place the carousel on top of it.
                            putBoolean(
                                ClockPreviewConstants.KEY_HIDE_CLOCK,
                                true,
                            )
                        }
                    },
                    wallpaperInteractor = injector.getWallpaperInteractor(requireContext()),
                    screen = CustomizationSections.Screen.LOCK_SCREEN,
                ),
            lifecycleOwner = this,
            offsetToStart = displayUtils.isSingleDisplayOrUnfoldedHorizontalHinge(activity),
            onWallpaperPreviewDirty = { activity.recreate() },
        )

        ClockSettingsBinder.bind(
            view,
            ViewModelProvider(
                    this,
                    injector.getClockSettingsViewModelFactory(
                        context,
                        injector.getWallpaperColorsViewModel(),
                        injector.getClockViewFactory(activity),
                    ),
                )
                .get(),
            injector.getClockViewFactory(activity),
            viewLifecycleOwner,
        )

        return view
    }

    override fun getDefaultTitle(): CharSequence {
        return requireContext().getString(R.string.clock_color_and_size_title)
    }

    override fun getToolbarColorId(): Int {
        return android.R.color.transparent
    }
}