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 package com.android.systemui.keyguard.ui.viewmodel
18 
19 import android.content.Context
20 import android.content.res.Resources
21 import com.android.systemui.R
22 import com.android.systemui.dagger.qualifiers.Application
23 import com.android.systemui.keyguard.domain.interactor.KeyguardClockInteractor
24 import com.android.systemui.keyguard.shared.model.SettingsClockSize
25 import javax.inject.Inject
26 import kotlinx.coroutines.flow.Flow
27 import kotlinx.coroutines.flow.combine
28 import kotlinx.coroutines.flow.map
29 
30 /** View model for the smartspace. */
31 class KeyguardPreviewSmartspaceViewModel
32 @Inject
33 constructor(
34     @Application private val context: Context,
35     interactor: KeyguardClockInteractor,
36 ) {
37 
38     val smartspaceTopPadding: Flow<Int> =
39         interactor.selectedClockSize.map {
40             when (it) {
41                 SettingsClockSize.DYNAMIC -> getLargeClockSmartspaceTopPadding(context.resources)
42                 SettingsClockSize.SMALL -> getSmallClockSmartspaceTopPadding(context.resources)
43             }
44         }
45 
46     val shouldHideSmartspace: Flow<Boolean> =
47         combine(
48                 interactor.selectedClockSize,
49                 interactor.currentClockId,
50                 ::Pair,
51             )
52             .map { (size, currentClockId) ->
53                 when (size) {
54                     // TODO (b/284122375) This is temporary. We should use clockController
55                     //      .largeClock.config.hasCustomWeatherDataDisplay instead, but
56                     //      ClockRegistry.createCurrentClock is not reliable.
57                     SettingsClockSize.DYNAMIC -> currentClockId == "DIGITAL_CLOCK_WEATHER"
58                     SettingsClockSize.SMALL -> false
59                 }
60             }
61 
62     companion object {
63         fun getLargeClockSmartspaceTopPadding(resources: Resources): Int {
64             return with(resources) {
65                 getDimensionPixelSize(R.dimen.status_bar_header_height_keyguard) +
66                     getDimensionPixelSize(R.dimen.keyguard_smartspace_top_offset) +
67                     getDimensionPixelSize(R.dimen.keyguard_clock_top_margin)
68             }
69         }
70 
71         fun getSmallClockSmartspaceTopPadding(resources: Resources): Int {
72             return with(resources) {
73                 getStatusBarHeight(this) +
74                     getDimensionPixelSize(R.dimen.small_clock_padding_top) +
75                     getDimensionPixelSize(R.dimen.small_clock_height)
76             }
77         }
78 
79         fun getStatusBarHeight(resource: Resources): Int {
80             var result = 0
81             val resourceId: Int = resource.getIdentifier("status_bar_height", "dimen", "android")
82             if (resourceId > 0) {
83                 result = resource.getDimensionPixelSize(resourceId)
84             }
85             return result
86         }
87     }
88 }
89