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.internet.domain
18 
19 import android.content.Context
20 import android.content.res.Resources
21 import android.widget.Switch
22 import com.android.systemui.common.shared.model.ContentDescription.Companion.loadContentDescription
23 import com.android.systemui.common.shared.model.Icon
24 import com.android.systemui.common.shared.model.Text.Companion.loadText
25 import com.android.systemui.dagger.qualifiers.Main
26 import com.android.systemui.qs.tiles.base.interactor.QSTileDataToStateMapper
27 import com.android.systemui.qs.tiles.impl.internet.domain.model.InternetTileModel
28 import com.android.systemui.qs.tiles.viewmodel.QSTileConfig
29 import com.android.systemui.qs.tiles.viewmodel.QSTileState
30 import com.android.systemui.res.R
31 import javax.inject.Inject
32 
33 /** Maps [InternetTileModel] to [QSTileState]. */
34 class InternetTileMapper
35 @Inject
36 constructor(
37     @Main private val resources: Resources,
38     private val theme: Resources.Theme,
39     private val context: Context,
40 ) : QSTileDataToStateMapper<InternetTileModel> {
41 
mapnull42     override fun map(config: QSTileConfig, data: InternetTileModel): QSTileState =
43         QSTileState.build(resources, theme, config.uiConfig) {
44             label = resources.getString(R.string.quick_settings_internet_label)
45             expandedAccessibilityClass = Switch::class
46 
47             if (data.secondaryLabel != null) {
48                 secondaryLabel = data.secondaryLabel.loadText(context)
49             } else {
50                 secondaryLabel = data.secondaryTitle
51             }
52 
53             stateDescription = data.stateDescription.loadContentDescription(context)
54             contentDescription = data.contentDescription.loadContentDescription(context)
55 
56             iconRes = data.iconId
57             if (data.icon != null) {
58                 this.icon = { data.icon }
59             } else if (data.iconId != null) {
60                 val loadedIcon =
61                     Icon.Loaded(
62                         resources.getDrawable(data.iconId!!, theme),
63                         contentDescription = null
64                     )
65                 this.icon = { loadedIcon }
66             }
67 
68             sideViewIcon = QSTileState.SideViewIcon.Chevron
69 
70             activationState =
71                 if (data is InternetTileModel.Active) QSTileState.ActivationState.ACTIVE
72                 else QSTileState.ActivationState.INACTIVE
73 
74             supportedActions =
75                 setOf(QSTileState.UserAction.CLICK, QSTileState.UserAction.LONG_CLICK)
76         }
77 }
78