1 /*
2  * Copyright (C) 2024 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.rotation.ui.mapper
18 
19 import android.content.res.Resources
20 import com.android.systemui.common.shared.model.Icon
21 import com.android.systemui.dagger.qualifiers.Main
22 import com.android.systemui.qs.tiles.base.interactor.QSTileDataToStateMapper
23 import com.android.systemui.qs.tiles.impl.rotation.domain.model.RotationLockTileModel
24 import com.android.systemui.qs.tiles.viewmodel.QSTileConfig
25 import com.android.systemui.qs.tiles.viewmodel.QSTileState
26 import com.android.systemui.res.R
27 import com.android.systemui.statusbar.policy.DevicePostureController
28 import javax.inject.Inject
29 
30 /** Maps [RotationLockTileModel] to [QSTileState]. */
31 class RotationLockTileMapper
32 @Inject
33 constructor(
34     @Main private val resources: Resources,
35     private val theme: Resources.Theme,
36     private val devicePostureController: DevicePostureController
37 ) : QSTileDataToStateMapper<RotationLockTileModel> {
mapnull38     override fun map(config: QSTileConfig, data: RotationLockTileModel): QSTileState =
39         QSTileState.build(resources, theme, config.uiConfig) {
40             this.label = resources.getString(R.string.quick_settings_rotation_unlocked_label)
41             this.contentDescription =
42                 resources.getString(R.string.accessibility_quick_settings_rotation)
43 
44             if (data.isRotationLocked) {
45                 activationState = QSTileState.ActivationState.INACTIVE
46                 this.secondaryLabel = EMPTY_SECONDARY_STRING
47                 iconRes = R.drawable.qs_auto_rotate_icon_off
48             } else {
49                 activationState = QSTileState.ActivationState.ACTIVE
50                 this.secondaryLabel =
51                     if (data.isCameraRotationEnabled) {
52                         resources.getString(R.string.rotation_lock_camera_rotation_on)
53                     } else {
54                         EMPTY_SECONDARY_STRING
55                     }
56                 this.iconRes = R.drawable.qs_auto_rotate_icon_on
57             }
58             this.icon = {
59                 Icon.Loaded(resources.getDrawable(iconRes!!, theme), contentDescription = null)
60             }
61             if (isDeviceFoldable()) {
62                 this.secondaryLabel = getSecondaryLabelWithPosture(this.activationState)
63             }
64             this.stateDescription = this.secondaryLabel
65             this.sideViewIcon = QSTileState.SideViewIcon.None
66             supportedActions =
67                 setOf(QSTileState.UserAction.CLICK, QSTileState.UserAction.LONG_CLICK)
68         }
69 
isDeviceFoldablenull70     private fun isDeviceFoldable(): Boolean {
71         val intArray = resources.getIntArray(com.android.internal.R.array.config_foldedDeviceStates)
72         return intArray.isNotEmpty()
73     }
74 
getSecondaryLabelWithPosturenull75     private fun getSecondaryLabelWithPosture(activationState: QSTileState.ActivationState): String {
76         val stateNames = resources.getStringArray(R.array.tile_states_rotation)
77         val stateName =
78             stateNames[
79                 if (activationState == QSTileState.ActivationState.ACTIVE) ON_INDEX else OFF_INDEX]
80         val posture =
81             if (
82                 devicePostureController.devicePosture ==
83                     DevicePostureController.DEVICE_POSTURE_CLOSED
84             )
85                 resources.getString(R.string.quick_settings_rotation_posture_folded)
86             else resources.getString(R.string.quick_settings_rotation_posture_unfolded)
87 
88         return resources.getString(
89             R.string.rotation_tile_with_posture_secondary_label_template,
90             stateName,
91             posture
92         )
93     }
94 
95     private companion object {
96         const val EMPTY_SECONDARY_STRING = ""
97         const val OFF_INDEX = 1
98         const val ON_INDEX = 2
99     }
100 }
101