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 |
| 17 | |
Pinyao Ting | d4c1ba9 | 2020-05-04 20:29:56 -0700 | [diff] [blame] | 18 | import android.annotation.SuppressLint |
Pinyao Ting | ee191b1 | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 19 | import android.annotation.UserIdInt |
Pinyao Ting | d4c1ba9 | 2020-05-04 20:29:56 -0700 | [diff] [blame] | 20 | import android.content.pm.LauncherApps |
| 21 | import android.content.pm.LauncherApps.ShortcutQuery.FLAG_MATCH_DYNAMIC |
| 22 | import android.content.pm.LauncherApps.ShortcutQuery.FLAG_MATCH_PINNED_BY_ANY_LAUNCHER |
| 23 | import android.content.pm.LauncherApps.ShortcutQuery.FLAG_MATCH_CACHED |
| 24 | import android.os.UserHandle |
Pinyao Ting | 76ed7ba | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 25 | import android.util.Log |
Pinyao Ting | d4c1ba9 | 2020-05-04 20:29:56 -0700 | [diff] [blame] | 26 | import com.android.systemui.bubbles.storage.BubbleEntity |
Pinyao Ting | ee191b1 | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 27 | import com.android.systemui.bubbles.storage.BubblePersistentRepository |
| 28 | import com.android.systemui.bubbles.storage.BubbleVolatileRepository |
Pinyao Ting | ee191b1 | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 29 | import kotlinx.coroutines.CoroutineScope |
| 30 | import kotlinx.coroutines.Dispatchers |
| 31 | import kotlinx.coroutines.Job |
| 32 | import kotlinx.coroutines.cancelAndJoin |
| 33 | import kotlinx.coroutines.launch |
| 34 | import kotlinx.coroutines.yield |
Pinyao Ting | d4c1ba9 | 2020-05-04 20:29:56 -0700 | [diff] [blame] | 35 | |
Pinyao Ting | ee191b1 | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 36 | import javax.inject.Inject |
| 37 | import javax.inject.Singleton |
| 38 | |
| 39 | @Singleton |
| 40 | internal class BubbleDataRepository @Inject constructor( |
| 41 | private val volatileRepository: BubbleVolatileRepository, |
Pinyao Ting | d4c1ba9 | 2020-05-04 20:29:56 -0700 | [diff] [blame] | 42 | private val persistentRepository: BubblePersistentRepository, |
| 43 | private val launcherApps: LauncherApps |
Pinyao Ting | ee191b1 | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 44 | ) { |
| 45 | |
| 46 | private val ioScope = CoroutineScope(Dispatchers.IO) |
Pinyao Ting | 76ed7ba | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 47 | private val uiScope = CoroutineScope(Dispatchers.Main) |
Pinyao Ting | ee191b1 | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 48 | private var job: Job? = null |
| 49 | |
| 50 | /** |
| 51 | * Adds the bubble in memory, then persists the snapshot after adding the bubble to disk |
| 52 | * asynchronously. |
| 53 | */ |
Pinyao Ting | 76ed7ba | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 54 | fun addBubble(@UserIdInt userId: Int, bubble: Bubble) = addBubbles(userId, listOf(bubble)) |
Pinyao Ting | ee191b1 | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Adds the bubble in memory, then persists the snapshot after adding the bubble to disk |
| 58 | * asynchronously. |
| 59 | */ |
| 60 | fun addBubbles(@UserIdInt userId: Int, bubbles: List<Bubble>) { |
Pinyao Ting | 76ed7ba | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 61 | if (DEBUG) Log.d(TAG, "adding ${bubbles.size} bubbles") |
| 62 | val entities = transform(userId, bubbles).also(volatileRepository::addBubbles) |
| 63 | if (entities.isNotEmpty()) persistToDisk() |
Pinyao Ting | ee191b1 | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Pinyao Ting | 76ed7ba | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 66 | /** |
| 67 | * Removes the bubbles from memory, then persists the snapshot to disk asynchronously. |
| 68 | */ |
Pinyao Ting | ee191b1 | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 69 | fun removeBubbles(@UserIdInt userId: Int, bubbles: List<Bubble>) { |
Pinyao Ting | 76ed7ba | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 70 | if (DEBUG) Log.d(TAG, "removing ${bubbles.size} bubbles") |
| 71 | val entities = transform(userId, bubbles).also(volatileRepository::removeBubbles) |
| 72 | if (entities.isNotEmpty()) persistToDisk() |
| 73 | } |
| 74 | |
Pinyao Ting | d4c1ba9 | 2020-05-04 20:29:56 -0700 | [diff] [blame] | 75 | private fun transform(userId: Int, bubbles: List<Bubble>): List<BubbleEntity> { |
Pinyao Ting | 76ed7ba | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 76 | return bubbles.mapNotNull { b -> |
Pinyao Ting | 293b83d | 2020-05-06 17:10:56 -0700 | [diff] [blame] | 77 | var shortcutId = b.shortcutInfo?.id |
| 78 | if (shortcutId == null) shortcutId = b.entry?.bubbleMetadata?.shortcutId |
| 79 | if (shortcutId == null) shortcutId = b.entry?.ranking?.shortcutInfo?.id |
| 80 | if (shortcutId == null) return@mapNotNull null |
Pinyao Ting | d4c1ba9 | 2020-05-04 20:29:56 -0700 | [diff] [blame] | 81 | BubbleEntity(userId, b.packageName, shortcutId) |
Pinyao Ting | 76ed7ba | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 82 | } |
Pinyao Ting | ee191b1 | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Persists the bubbles to disk. When being called multiple times, it waits for first ongoing |
| 87 | * write operation to finish then run another write operation exactly once. |
| 88 | * |
| 89 | * e.g. |
| 90 | * Job A started -> blocking I/O |
| 91 | * Job B started, cancels A, wait for blocking I/O in A finishes |
| 92 | * Job C started, cancels B, wait for job B to finish |
| 93 | * Job D started, cancels C, wait for job C to finish |
| 94 | * Job A completed |
| 95 | * Job B resumes and reaches yield() and is then cancelled |
| 96 | * Job C resumes and reaches yield() and is then cancelled |
| 97 | * Job D resumes and performs another blocking I/O |
| 98 | */ |
| 99 | private fun persistToDisk() { |
| 100 | val prev = job |
| 101 | job = ioScope.launch { |
| 102 | // if there was an ongoing disk I/O operation, they can be cancelled |
| 103 | prev?.cancelAndJoin() |
| 104 | // check for cancellation before disk I/O |
| 105 | yield() |
| 106 | // save to disk |
| 107 | persistentRepository.persistsToDisk(volatileRepository.bubbles) |
| 108 | } |
| 109 | } |
Pinyao Ting | 76ed7ba | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Load bubbles from disk. |
| 113 | */ |
Pinyao Ting | d4c1ba9 | 2020-05-04 20:29:56 -0700 | [diff] [blame] | 114 | @SuppressLint("WrongConstant") |
Pinyao Ting | 76ed7ba | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 115 | fun loadBubbles(cb: (List<Bubble>) -> Unit) = ioScope.launch { |
Pinyao Ting | d4c1ba9 | 2020-05-04 20:29:56 -0700 | [diff] [blame] | 116 | /** |
| 117 | * Load BubbleEntity from disk. |
| 118 | * e.g. |
| 119 | * [ |
| 120 | * BubbleEntity(0, "com.example.messenger", "id-2"), |
| 121 | * BubbleEntity(10, "com.example.chat", "my-id1") |
| 122 | * BubbleEntity(0, "com.example.messenger", "id-1") |
| 123 | * ] |
| 124 | */ |
| 125 | val entities = persistentRepository.readFromDisk() |
| 126 | volatileRepository.addBubbles(entities) |
| 127 | /** |
| 128 | * Extract userId/packageName from these entities. |
| 129 | * e.g. |
| 130 | * [ |
| 131 | * ShortcutKey(0, "com.example.messenger"), ShortcutKey(0, "com.example.chat") |
| 132 | * ] |
| 133 | */ |
| 134 | val shortcutKeys = entities.map { ShortcutKey(it.userId, it.packageName) }.toSet() |
| 135 | /** |
| 136 | * Retrieve shortcuts with given userId/packageName combination, then construct a mapping |
| 137 | * between BubbleEntity and ShortcutInfo. |
| 138 | * e.g. |
| 139 | * { |
| 140 | * BubbleEntity(0, "com.example.messenger", "id-0") -> |
| 141 | * ShortcutInfo(userId=0, pkg="com.example.messenger", id="id-0"), |
| 142 | * BubbleEntity(0, "com.example.messenger", "id-2") -> |
| 143 | * ShortcutInfo(userId=0, pkg="com.example.messenger", id="id-2"), |
| 144 | * BubbleEntity(10, "com.example.chat", "id-1") -> |
| 145 | * ShortcutInfo(userId=10, pkg="com.example.chat", id="id-1"), |
| 146 | * BubbleEntity(10, "com.example.chat", "id-3") -> |
| 147 | * ShortcutInfo(userId=10, pkg="com.example.chat", id="id-3") |
| 148 | * } |
| 149 | */ |
| 150 | val shortcutMap = shortcutKeys.flatMap { key -> |
| 151 | launcherApps.getShortcuts( |
| 152 | LauncherApps.ShortcutQuery() |
| 153 | .setPackage(key.pkg) |
| 154 | .setQueryFlags(SHORTCUT_QUERY_FLAG), UserHandle.of(key.userId)) |
| 155 | ?.map { BubbleEntity(key.userId, key.pkg, it.id) to it } ?: emptyList() |
| 156 | }.toMap() |
| 157 | // For each entity loaded from xml, find the corresponding ShortcutInfo then convert them |
| 158 | // into Bubble. |
| 159 | val bubbles = entities.mapNotNull { entity -> shortcutMap[entity]?.let { Bubble(it) } } |
| 160 | uiScope.launch { cb(bubbles) } |
Pinyao Ting | 76ed7ba | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 161 | } |
Pinyao Ting | d4c1ba9 | 2020-05-04 20:29:56 -0700 | [diff] [blame] | 162 | |
| 163 | private data class ShortcutKey(val userId: Int, val pkg: String) |
Pinyao Ting | ee191b1 | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 164 | } |
Pinyao Ting | 76ed7ba | 2020-04-29 18:35:39 -0700 | [diff] [blame] | 165 | |
| 166 | private const val TAG = "BubbleDataRepository" |
| 167 | private const val DEBUG = false |
Pinyao Ting | d4c1ba9 | 2020-05-04 20:29:56 -0700 | [diff] [blame] | 168 | private const val SHORTCUT_QUERY_FLAG = |
| 169 | FLAG_MATCH_DYNAMIC or FLAG_MATCH_PINNED_BY_ANY_LAUNCHER or FLAG_MATCH_CACHED |