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 com.android.systemui.plugins.qs.QSTile
22 import com.android.systemui.qs.external.CustomTile
23 import com.android.systemui.qs.nano.QsTileState
24 import com.android.systemui.util.nano.ComponentNameProto
25 
26 fun QSTile.State.toProto(): QsTileState? {
27     if (TextUtils.isEmpty(spec)) return null
28     val state = QsTileState()
29     if (spec.startsWith(CustomTile.PREFIX)) {
30         val protoComponentName = ComponentNameProto()
31         val tileComponentName = CustomTile.getComponentFromSpec(spec)
32         protoComponentName.packageName = tileComponentName.packageName
33         protoComponentName.className = tileComponentName.className
34         state.componentName = protoComponentName
35     } else {
36         state.spec = spec
37     }
38     state.state =
39         when (this.state) {
40             Tile.STATE_UNAVAILABLE -> QsTileState.UNAVAILABLE
41             Tile.STATE_INACTIVE -> QsTileState.INACTIVE
42             Tile.STATE_ACTIVE -> QsTileState.ACTIVE
43             else -> QsTileState.UNAVAILABLE
44         }
45     label?.let { state.label = it.toString() }
46     secondaryLabel?.let { state.secondaryLabel = it.toString() }
47     if (this is QSTile.BooleanState) {
48         state.booleanState = value
49     }
50     return state
51 }
52