Pinyao Ting | ee191b1 | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.systemui.bubbles.storage |
| 17 | |
| 18 | import javax.inject.Inject |
| 19 | import javax.inject.Singleton |
| 20 | |
| 21 | private 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 |
| 28 | class 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 Ting | ee191b1 | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 42 | * 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 Ting | 76ed7ba | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 52 | entities.addAll(bubbles) |
Pinyao Ting | ee191b1 | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Synchronized |
| 56 | fun removeBubbles(bubbles: List<BubbleXmlEntity>) { |
| 57 | bubbles.forEach { entities.remove(it) } |
| 58 | } |
| 59 | } |