blob: e1f675b8358390a630281a58c4461162d4cb9f22 [file] [log] [blame]
Pinyao Tingee191b12020-04-29 18:35:39 -07001/*
2 * Copyright (C) 2020 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 */
16package com.android.systemui.bubbles.storage
17
18import javax.inject.Inject
19import javax.inject.Singleton
20
21private const val CAPACITY = 16
22
23/**
24 * BubbleVolatileRepository holds the most updated snapshot of list of bubbles for in-memory
25 * manipulation.
26 */
27@Singleton
28class BubbleVolatileRepository @Inject constructor() {
29 /**
30 * An ordered set of bubbles based on their natural ordering.
31 */
32 private val entities = mutableSetOf<BubbleXmlEntity>()
33
34 /**
35 * Returns a snapshot of all the bubbles.
36 */
37 val bubbles: List<BubbleXmlEntity>
38 @Synchronized
39 get() = entities.toList()
40
41 /**
Pinyao Tingee191b12020-04-29 18:35:39 -070042 * Add the bubbles to memory and perform a de-duplication. In case a bubble already exists,
43 * it will be moved to the last.
44 */
45 @Synchronized
46 fun addBubbles(bubbles: List<BubbleXmlEntity>) {
47 if (bubbles.isEmpty()) return
48 bubbles.forEach { entities.remove(it) }
49 if (entities.size + bubbles.size >= CAPACITY) {
50 entities.drop(entities.size + bubbles.size - CAPACITY)
51 }
Pinyao Ting76ed7ba2020-04-29 18:35:39 -070052 entities.addAll(bubbles)
Pinyao Tingee191b12020-04-29 18:35:39 -070053 }
54
55 @Synchronized
56 fun removeBubbles(bubbles: List<BubbleXmlEntity>) {
57 bubbles.forEach { entities.remove(it) }
58 }
59}