• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.launcher3.icons.cache
17 
18 import androidx.annotation.IntDef
19 import kotlin.annotation.AnnotationRetention.SOURCE
20 
21 /** Flags to control cache lookup behavior */
22 data class CacheLookupFlag private constructor(@LookupFlag private val flag: Int) {
23 
24     /**
25      * Cache will try to load the low res version of the entry unless a high-res is already in
26      * memory
27      */
useLowResnull28     fun useLowRes() = hasFlag(USE_LOW_RES)
29 
30     @JvmOverloads fun withUseLowRes(useLowRes: Boolean = true) = updateMask(USE_LOW_RES, useLowRes)
31 
32     /** Cache will try to lookup the package entry for the item, if the object entry fails */
33     fun usePackageIcon() = hasFlag(USE_PACKAGE_ICON)
34 
35     @JvmOverloads
36     fun withUsePackageIcon(usePackageIcon: Boolean = true) =
37         updateMask(USE_PACKAGE_ICON, usePackageIcon)
38 
39     /**
40      * Entry will not be added to the memory cache if it was not already added by a previous lookup
41      */
42     fun skipAddToMemCache() = hasFlag(SKIP_ADD_TO_MEM_CACHE)
43 
44     @JvmOverloads
45     fun withSkipAddToMemCache(skipAddToMemCache: Boolean = true) =
46         updateMask(SKIP_ADD_TO_MEM_CACHE, skipAddToMemCache)
47 
48     private fun hasFlag(@LookupFlag mask: Int) = flag.and(mask) != 0
49 
50     private fun updateMask(@LookupFlag mask: Int, addMask: Boolean) =
51         if (addMask) flagCache[flag.or(mask)] else flagCache[flag.and(mask.inv())]
52 
53     /** Returns `true` if this flag has less UI information then [other] */
54     fun isVisuallyLessThan(other: CacheLookupFlag): Boolean {
55         return useLowRes() && !other.useLowRes()
56     }
57 
58     @Retention(SOURCE)
59     @IntDef(value = [USE_LOW_RES, USE_PACKAGE_ICON, SKIP_ADD_TO_MEM_CACHE], flag = true)
60     /** Various options to control cache lookup */
61     private annotation class LookupFlag
62 
63     companion object {
64         private const val USE_LOW_RES: Int = 1 shl 0
65         private const val USE_PACKAGE_ICON: Int = 1 shl 1
66         private const val SKIP_ADD_TO_MEM_CACHE: Int = 1 shl 2
67 
<lambda>null68         private val flagCache = Array(8) { CacheLookupFlag(it) }
69 
70         @JvmField val DEFAULT_LOOKUP_FLAG = CacheLookupFlag(0)
71     }
72 }
73