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.qs.pipeline.domain.autoaddable
18 
19 import android.content.res.Resources
20 import android.util.Log
21 import com.android.systemui.res.R
22 import com.android.systemui.qs.pipeline.domain.model.AutoAddable
23 import com.android.systemui.qs.pipeline.shared.TileSpec
24 
25 object AutoAddableSettingList {
26 
27     /** Parses [R.array.config_quickSettingsAutoAdd] into a collection of [AutoAddableSetting]. */
parseSettingsResourcenull28     fun parseSettingsResource(
29         resources: Resources,
30         autoAddableSettingFactory: AutoAddableSetting.Factory,
31     ): Iterable<AutoAddable> {
32         val autoAddList = resources.getStringArray(R.array.config_quickSettingsAutoAdd)
33         return autoAddList.mapNotNull {
34             val elements = it.split(SETTING_SEPARATOR, limit = 2)
35             if (elements.size == 2) {
36                 val setting = elements[0]
37                 val spec = elements[1]
38                 val tileSpec = TileSpec.create(spec)
39                 if (tileSpec == TileSpec.Invalid) {
40                     Log.w(TAG, "Malformed item in array: $it")
41                     null
42                 } else {
43                     autoAddableSettingFactory.create(setting, TileSpec.create(spec))
44                 }
45             } else {
46                 Log.w(TAG, "Malformed item in array: $it")
47                 null
48             }
49         }
50     }
51 
52     private const val SETTING_SEPARATOR = ":"
53     private const val TAG = "AutoAddableSettingList"
54 }
55