1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.systemui.controls.management
import android.text.TextUtils
import android.util.ArrayMap
import com.android.systemui.controls.ControlStatus
import com.android.systemui.controls.controller.ControlInfo
/**
* This model is used to show controls separated by zones.
*
* The model will sort the controls and zones in the following manner:
* * The zones will be sorted in a first seen basis
* * The controls in each zone will be sorted in a first seen basis.
*
* The controls passed should belong to the same structure, as an instance of this model will be
* created for each structure.
*
* The list of favorite ids can contain ids for controls not passed to this model. Those will be
* filtered out.
*
* @property controls List of controls as returned by loading
* @property initialFavoriteIds sorted ids of favorite controls.
* @property noZoneString text to use as header for all controls that have blank or `null` zone.
* @property controlsModelCallback callback to notify that favorites have changed for the first time
*/
class AllModel(
private val controls: List<ControlStatus>,
initialFavoriteIds: List<String>,
private val emptyZoneString: CharSequence,
private val controlsModelCallback: ControlsModel.ControlsModelCallback
) : ControlsModel {
private var modified = false
override val moveHelper = null
override val favorites: List<ControlInfo>
get() = favoriteIds.mapNotNull { id ->
val control = controls.firstOrNull { it.control.controlId == id }?.control
control?.let {
ControlInfo.fromControl(it)
}
}
private val favoriteIds = run {
val ids = controls.mapTo(HashSet()) { it.control.controlId }
initialFavoriteIds.filter { it in ids }.toMutableList()
}
override val elements: List<ElementWrapper> = createWrappers(controls)
override fun changeFavoriteStatus(controlId: String, favorite: Boolean) {
val toChange = elements.firstOrNull {
it is ControlStatusWrapper && it.controlStatus.control.controlId == controlId
} as ControlStatusWrapper?
if (favorite == toChange?.controlStatus?.favorite) return
val changed: Boolean = if (favorite) {
favoriteIds.add(controlId)
} else {
favoriteIds.remove(controlId)
}
if (changed && !modified) {
modified = true
controlsModelCallback.onFirstChange()
}
toChange?.let {
it.controlStatus.favorite = favorite
}
}
private fun createWrappers(list: List<ControlStatus>): List<ElementWrapper> {
val map = list.groupByTo(OrderedMap(ArrayMap<CharSequence, MutableList<ControlStatus>>())) {
it.control.zone ?: ""
}
val output = mutableListOf<ElementWrapper>()
var emptyZoneValues: Sequence<ControlStatusWrapper>? = null
for (zoneName in map.orderedKeys) {
val values = map.getValue(zoneName).asSequence().map { ControlStatusWrapper(it) }
if (TextUtils.isEmpty(zoneName)) {
emptyZoneValues = values
} else {
output.add(ZoneNameWrapper(zoneName))
output.addAll(values)
}
}
// Add controls with empty zone at the end
if (emptyZoneValues != null) {
if (map.size != 1) {
output.add(ZoneNameWrapper(emptyZoneString))
}
output.addAll(emptyZoneValues)
}
return output
}
private class OrderedMap<K, V>(private val map: MutableMap<K, V>) : MutableMap<K, V> by map {
val orderedKeys = mutableListOf<K>()
override fun put(key: K, value: V): V? {
if (key !in map) {
orderedKeys.add(key)
}
return map.put(key, value)
}
override fun clear() {
orderedKeys.clear()
map.clear()
}
override fun remove(key: K): V? {
val removed = map.remove(key)
if (removed != null) {
orderedKeys.remove(key)
}
return removed
}
}
}
|