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.statusbar.pipeline.shared.ui.model
18 
19 import android.content.Context
20 import android.graphics.drawable.Drawable
21 import android.service.quicksettings.Tile
22 import com.android.settingslib.graph.SignalDrawable
23 import com.android.systemui.common.shared.model.Text
24 import com.android.systemui.common.shared.model.Text.Companion.loadText
25 import com.android.systemui.plugins.qs.QSTile
26 import com.android.systemui.qs.tileimpl.QSTileImpl
27 
28 /** Model describing the state that the QS Internet tile should be in. */
29 sealed interface InternetTileModel {
30     val secondaryTitle: String?
31     val secondaryLabel: Text?
32     val iconId: Int?
33     val icon: QSTile.Icon?
34 
35     fun applyTo(state: QSTile.SignalState, context: Context) {
36         if (secondaryLabel != null) {
37             state.secondaryLabel = secondaryLabel.loadText(context)
38         } else {
39             state.secondaryLabel = secondaryTitle
40         }
41 
42         // inout indicators are unused
43         state.activityIn = false
44         state.activityOut = false
45 
46         // To support both SignalDrawable and other icons, give priority to icons over IDs
47         if (icon != null) {
48             state.icon = icon
49         } else if (iconId != null) {
50             state.icon = QSTileImpl.ResourceIcon.get(iconId!!)
51         }
52 
53         state.state =
54             if (this is Active) {
55                 Tile.STATE_ACTIVE
56             } else {
57                 Tile.STATE_INACTIVE
58             }
59     }
60 
61     data class Active(
62         override val secondaryTitle: String? = null,
63         override val secondaryLabel: Text? = null,
64         override val iconId: Int? = null,
65         override val icon: QSTile.Icon? = null,
66     ) : InternetTileModel
67 
68     data class Inactive(
69         override val secondaryTitle: String? = null,
70         override val secondaryLabel: Text? = null,
71         override val iconId: Int? = null,
72         override val icon: QSTile.Icon? = null,
73     ) : InternetTileModel
74 }
75 
76 /**
77  * [QSTile.Icon]-compatible container class for us to marshal the compacted [SignalDrawable] state
78  * across to the internet tile.
79  */
80 data class SignalIcon(val state: Int) : QSTile.Icon() {
81 
82     override fun getDrawable(context: Context): Drawable {
83         val d = SignalDrawable(context)
84         d.setLevel(state)
85         return d
86     }
87 
88     override fun toString(): String {
89         return String.format("SignalIcon[mState=0x%08x]", state)
90     }
91 }
92