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.intentresolver.contentpreview.payloadtoggle.ui.composable
17
18 import android.content.Context
19 import android.content.ContextWrapper
20 import android.content.res.Resources
21 import androidx.compose.foundation.Image
22 import androidx.compose.runtime.Composable
23 import androidx.compose.runtime.CompositionLocalProvider
24 import androidx.compose.ui.Modifier
25 import androidx.compose.ui.graphics.ColorFilter
26 import androidx.compose.ui.graphics.asImageBitmap
27 import androidx.compose.ui.platform.LocalContext
28 import androidx.compose.ui.res.painterResource
29 import com.android.intentresolver.icon.AdaptiveIcon
30 import com.android.intentresolver.icon.BitmapIcon
31 import com.android.intentresolver.icon.ComposeIcon
32 import com.android.intentresolver.icon.ResourceIcon
33
34 @Composable
Imagenull35 fun Image(icon: ComposeIcon, modifier: Modifier = Modifier, colorFilter: ColorFilter? = null) {
36 when (icon) {
37 is AdaptiveIcon -> Image(icon.wrapped, modifier, colorFilter = colorFilter)
38 is BitmapIcon ->
39 Image(
40 icon.bitmap.asImageBitmap(),
41 contentDescription = null,
42 modifier = modifier,
43 colorFilter = colorFilter
44 )
45 is ResourceIcon -> {
46 val localContext = LocalContext.current
47 val wrappedContext: Context =
48 object : ContextWrapper(localContext) {
49 override fun getResources(): Resources = icon.res
50 }
51 CompositionLocalProvider(LocalContext provides wrappedContext) {
52 Image(
53 painterResource(icon.resId),
54 contentDescription = null,
55 modifier = modifier,
56 colorFilter = colorFilter
57 )
58 }
59 }
60 }
61 }
62