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.qs.tiles.impl.uimodenight.domain 18 19 import android.app.UiModeManager 20 import android.content.res.Resources 21 import android.content.res.Resources.Theme 22 import android.text.TextUtils 23 import com.android.systemui.common.shared.model.Icon 24 import com.android.systemui.dagger.qualifiers.Main 25 import com.android.systemui.qs.tiles.base.interactor.QSTileDataToStateMapper 26 import com.android.systemui.qs.tiles.impl.uimodenight.domain.model.UiModeNightTileModel 27 import com.android.systemui.qs.tiles.viewmodel.QSTileConfig 28 import com.android.systemui.qs.tiles.viewmodel.QSTileState 29 import com.android.systemui.res.R 30 import java.time.LocalTime 31 import java.time.format.DateTimeFormatter 32 import javax.inject.Inject 33 34 /** Maps [UiModeNightTileModel] to [QSTileState]. */ 35 class UiModeNightTileMapper 36 @Inject 37 constructor( 38 @Main private val resources: Resources, 39 private val theme: Theme, 40 ) : QSTileDataToStateMapper<UiModeNightTileModel> { 41 companion object { 42 val formatter12Hour: DateTimeFormatter = DateTimeFormatter.ofPattern("hh:mm a") 43 val formatter24Hour: DateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm") 44 } mapnull45 override fun map(config: QSTileConfig, data: UiModeNightTileModel): QSTileState = 46 with(data) { 47 QSTileState.build(resources, theme, config.uiConfig) { 48 var shouldSetSecondaryLabel = false 49 50 if (isPowerSave) { 51 secondaryLabel = 52 resources.getString( 53 R.string.quick_settings_dark_mode_secondary_label_battery_saver 54 ) 55 } else if (uiMode == UiModeManager.MODE_NIGHT_AUTO && isLocationEnabled) { 56 secondaryLabel = 57 resources.getString( 58 if (isNightMode) 59 R.string.quick_settings_dark_mode_secondary_label_until_sunrise 60 else R.string.quick_settings_dark_mode_secondary_label_on_at_sunset 61 ) 62 } else if (uiMode == UiModeManager.MODE_NIGHT_CUSTOM) { 63 if (nightModeCustomType == UiModeManager.MODE_NIGHT_CUSTOM_TYPE_SCHEDULE) { 64 val time: LocalTime = 65 if (isNightMode) { 66 customNightModeEnd 67 } else { 68 customNightModeStart 69 } 70 71 val formatter: DateTimeFormatter = 72 if (is24HourFormat) formatter24Hour else formatter12Hour 73 74 secondaryLabel = 75 resources.getString( 76 if (isNightMode) 77 R.string.quick_settings_dark_mode_secondary_label_until 78 else R.string.quick_settings_dark_mode_secondary_label_on_at, 79 formatter.format(time) 80 ) 81 } else if ( 82 nightModeCustomType == UiModeManager.MODE_NIGHT_CUSTOM_TYPE_BEDTIME 83 ) { 84 secondaryLabel = 85 resources.getString( 86 if (isNightMode) 87 R.string 88 .quick_settings_dark_mode_secondary_label_until_bedtime_ends 89 else R.string.quick_settings_dark_mode_secondary_label_on_at_bedtime 90 ) 91 } else { 92 secondaryLabel = null // undefined type of nightModeCustomType 93 shouldSetSecondaryLabel = true 94 } 95 } else { 96 secondaryLabel = null 97 shouldSetSecondaryLabel = true 98 } 99 100 contentDescription = 101 if (TextUtils.isEmpty(secondaryLabel)) label 102 else TextUtils.concat(label, ", ", secondaryLabel) 103 if (isPowerSave) { 104 activationState = QSTileState.ActivationState.UNAVAILABLE 105 if (shouldSetSecondaryLabel) 106 secondaryLabel = resources.getStringArray(R.array.tile_states_dark)[0] 107 } else { 108 activationState = 109 if (isNightMode) QSTileState.ActivationState.ACTIVE 110 else QSTileState.ActivationState.INACTIVE 111 112 if (shouldSetSecondaryLabel) { 113 secondaryLabel = 114 if (activationState == QSTileState.ActivationState.INACTIVE) 115 resources.getStringArray(R.array.tile_states_dark)[1] 116 else resources.getStringArray(R.array.tile_states_dark)[2] 117 } 118 } 119 120 iconRes = 121 if (activationState == QSTileState.ActivationState.ACTIVE) 122 R.drawable.qs_light_dark_theme_icon_on 123 else R.drawable.qs_light_dark_theme_icon_off 124 val loadedIcon = 125 Icon.Loaded(resources.getDrawable(iconRes!!, theme), contentDescription = null) 126 icon = { loadedIcon } 127 128 supportedActions = 129 if (activationState == QSTileState.ActivationState.UNAVAILABLE) 130 setOf(QSTileState.UserAction.LONG_CLICK) 131 else setOf(QSTileState.UserAction.CLICK, QSTileState.UserAction.LONG_CLICK) 132 } 133 } 134 } 135