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