1 /*
<lambda>null2 * 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.intentresolver.icon
17
18 import android.content.ContentResolver
19 import android.content.pm.PackageManager
20 import android.content.res.Resources
21 import android.graphics.Bitmap
22 import android.graphics.BitmapFactory
23 import android.graphics.drawable.Icon
24 import java.io.File
25 import java.io.FileInputStream
26
27 sealed interface ComposeIcon
28
29 data class BitmapIcon(val bitmap: Bitmap) : ComposeIcon
30
31 data class ResourceIcon(val resId: Int, val res: Resources) : ComposeIcon
32
33 @JvmInline value class AdaptiveIcon(val wrapped: ComposeIcon) : ComposeIcon
34
35 fun Icon.toComposeIcon(pm: PackageManager, resolver: ContentResolver): ComposeIcon? {
36 return when (type) {
37 Icon.TYPE_BITMAP -> BitmapIcon(bitmap)
38 Icon.TYPE_RESOURCE -> pm.resourcesForPackage(resPackage)?.let { ResourceIcon(resId, it) }
39 Icon.TYPE_DATA ->
40 BitmapIcon(BitmapFactory.decodeByteArray(dataBytes, dataOffset, dataLength))
41 Icon.TYPE_URI -> uriIcon(resolver)
42 Icon.TYPE_ADAPTIVE_BITMAP -> AdaptiveIcon(BitmapIcon(bitmap))
43 Icon.TYPE_URI_ADAPTIVE_BITMAP -> uriIcon(resolver)?.let { AdaptiveIcon(it) }
44 else -> error("unexpected icon type: $type")
45 }
46 }
47
toComposeIconnull48 fun Icon.toComposeIcon(resources: Resources?, resolver: ContentResolver): ComposeIcon? {
49 return when (type) {
50 Icon.TYPE_BITMAP -> BitmapIcon(bitmap)
51 Icon.TYPE_RESOURCE -> resources?.let { ResourceIcon(resId, resources) }
52 Icon.TYPE_DATA ->
53 BitmapIcon(BitmapFactory.decodeByteArray(dataBytes, dataOffset, dataLength))
54 Icon.TYPE_URI -> uriIcon(resolver)
55 Icon.TYPE_ADAPTIVE_BITMAP -> AdaptiveIcon(BitmapIcon(bitmap))
56 Icon.TYPE_URI_ADAPTIVE_BITMAP -> uriIcon(resolver)?.let { AdaptiveIcon(it) }
57 else -> error("unexpected icon type: $type")
58 }
59 }
60
61 // TODO: this is probably constant and doesn't need to be re-queried for each icon
PackageManagernull62 fun PackageManager.resourcesForPackage(pkgName: String): Resources? {
63 return if (pkgName == "android") {
64 Resources.getSystem()
65 } else {
66 runCatching {
67 this@resourcesForPackage.getApplicationInfo(
68 pkgName,
69 PackageManager.MATCH_UNINSTALLED_PACKAGES or
70 PackageManager.GET_SHARED_LIBRARY_FILES
71 )
72 }
73 .getOrNull()
74 ?.let { ai -> getResourcesForApplication(ai) }
75 }
76 }
77
Iconnull78 private fun Icon.uriIcon(resolver: ContentResolver): BitmapIcon? {
79 return runCatching {
80 when (uri.scheme) {
81 ContentResolver.SCHEME_CONTENT,
82 ContentResolver.SCHEME_FILE -> resolver.openInputStream(uri)
83 else -> FileInputStream(File(uriString))
84 }
85 }
86 .getOrNull()
87 ?.let { inStream -> BitmapIcon(BitmapFactory.decodeStream(inStream)) }
88 }
89