1 /*
2  * Copyright (C) 2022 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
18 
19 import android.service.quicksettings.Tile
20 import android.text.TextUtils
21 import android.widget.Switch
22 import com.android.systemui.plugins.qs.QSTile
23 import com.android.systemui.qs.external.CustomTile
24 import com.android.systemui.qs.nano.QsTileState
25 import com.android.systemui.util.nano.ComponentNameProto
26 
QSTilenull27 fun QSTile.State.toProto(): QsTileState? {
28     if (TextUtils.isEmpty(spec)) return null
29     val state = QsTileState()
30     if (spec.startsWith(CustomTile.PREFIX)) {
31         val protoComponentName = ComponentNameProto()
32         val tileComponentName = CustomTile.getComponentFromSpec(spec)
33         protoComponentName.packageName = tileComponentName.packageName
34         protoComponentName.className = tileComponentName.className
35         state.componentName = protoComponentName
36     } else {
37         state.spec = spec
38     }
39     state.state =
40         when (this.state) {
41             Tile.STATE_UNAVAILABLE -> QsTileState.UNAVAILABLE
42             Tile.STATE_INACTIVE -> QsTileState.INACTIVE
43             Tile.STATE_ACTIVE -> QsTileState.ACTIVE
44             else -> QsTileState.UNAVAILABLE
45         }
46     label?.let { state.label = it.toString() }
47     secondaryLabel?.let { state.secondaryLabel = it.toString() }
48     if (expandedAccessibilityClassName == Switch::class.java.name) {
49         state.booleanState = state.state == QsTileState.ACTIVE
50     }
51     return state
52 }
53